2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * file.c: Operations on Empire tables (`files' for historical reasons)
30 * Known contributors to this file:
33 * Markus Armbruster, 2005-2006
41 #include <sys/types.h>
47 #include "prototypes.h"
49 static int fillcache(struct empfile *, int);
50 static int do_write(struct empfile *, void *, int, int);
53 * Open the file-backed table TYPE (EF_SECTOR, ...).
54 * HOW are flags to control operation. Naturally, immutable flags are
56 * Return non-zero on success, zero on failure.
57 * You must call ef_close() before the next ef_open().
60 ef_open(int type, int how)
64 int oflags, fd, fsiz, size;
66 if (ef_check(type) < 0)
68 if (CANT_HAPPEN(how & EFF_IMMUTABLE))
69 how &= ~EFF_IMMUTABLE;
73 if (CANT_HAPPEN(ep->fd >= 0))
79 oflags |= O_CREAT | O_TRUNC;
83 if ((fd = open(ep->file, oflags, S_IRWUG)) < 0) {
84 logerror("Can't open %s (%s)", ep->file, strerror(errno));
88 lock.l_type = how & EFF_RDONLY ? F_RDLCK : F_WRLCK;
89 lock.l_whence = SEEK_SET;
90 lock.l_start = lock.l_len = 0;
91 if (fcntl(fd, F_SETLK, &lock) == -1) {
92 logerror("Can't lock %s (%s)", ep->file, strerror(errno));
99 if (fsiz % ep->size) {
100 logerror("Can't open %s (file size not a multiple of record size %d)",
105 ep->fids = fsiz / ep->size;
108 if (ep->flags & EFF_STATIC) {
109 /* ep->cache already points to space for ep->csize elements */
111 if (ep->fids > ep->csize) {
112 logerror("Can't open %s: file larger than %d bytes",
113 ep->file, ep->fids * ep->size);
120 ep->csize = ep->fids;
122 ep->csize = blksize(fd) / ep->size;
123 /* 0 could lead to null cache, which confuses assertions */
126 size = ep->csize * ep->size;
127 if (CANT_HAPPEN(ep->cache))
129 ep->cache = malloc(size);
130 if (ep->cache == NULL) {
131 logerror("Can't open %s: out of memory", ep->file);
138 ep->flags = (ep->flags & EFF_IMMUTABLE) | (how & ~EFF_CREATE);
141 /* map file into cache */
142 if ((how & EFF_MEM) && ep->fids) {
143 if (fillcache(ep, 0) != ep->fids) {
144 ep->cids = 0; /* prevent cache flush */
145 ep->flags &= EFF_IMMUTABLE; /* maintain invariant */
152 * Could close fd if both EFF_RDONLY and EFF_MEM, but that doesn't
153 * happen, so don't bother.
160 * Close the file-backed table TYPE (EF_SECTOR, ...).
161 * Return non-zero on success, zero on failure.
169 retval = ef_flush(type);
171 ep->flags &= EFF_IMMUTABLE;
172 if (!(ep->flags & EFF_STATIC)) {
176 if (close(ep->fd) < 0) {
177 logerror("Error closing %s (%s)", ep->name, strerror(errno));
185 * Flush file-backed table TYPE (EF_SECTOR, ...) to disk.
186 * Return non-zero on success, zero on failure.
193 if (ef_check(type) < 0)
196 if (CANT_HAPPEN(ep->fd < 0))
199 * We don't know which cache entries are dirty. ef_write() writes
200 * through, but direct updates through ef_ptr() don't. They are
201 * allowed only with EFF_MEM. Assume the whole cash is dirty
204 if (!(ep->flags & EFF_RDONLY) && (ep->flags & EFF_MEM))
205 return do_write(ep, ep->cache, ep->baseid, ep->cids) >= 0;
211 * Return pointer to element ID in table TYPE if it exists, else NULL.
212 * The table must be fully cached, i.e. flags & EFF_MEM.
213 * The caller is responsible for flushing changes he makes.
216 ef_ptr(int type, int id)
220 if (ef_check(type) < 0)
223 if (CANT_HAPPEN(!(ep->flags & EFF_MEM) || !ep->cache))
225 if (id < 0 || id >= ep->fids)
227 return ep->cache + ep->size * id;
231 * Read element ID from table TYPE into buffer INTO.
232 * FIXME pass buffer size!
233 * Return non-zero on success, zero on failure.
236 ef_read(int type, int id, void *into)
241 if (ef_check(type) < 0)
244 if (CANT_HAPPEN(!ep->cache))
246 if (id < 0 || id >= ep->fids)
249 if (ep->flags & EFF_MEM) {
250 from = ep->cache + id * ep->size;
252 if (ep->baseid + ep->cids <= id || ep->baseid > id) {
253 if (fillcache(ep, id) < 1)
256 from = ep->cache + (id - ep->baseid) * ep->size;
258 memcpy(into, from, ep->size);
261 ep->postread(id, into);
266 * Fill cache of EP with elements starting at ID.
267 * If any were read, return their number.
268 * Else return -1 and leave the cache unchanged.
271 fillcache(struct empfile *ep, int start)
276 if (CANT_HAPPEN(ep->fd < 0 || !ep->cache))
279 if (lseek(ep->fd, start * ep->size, SEEK_SET) == (off_t)-1) {
280 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
285 n = ep->csize * ep->size;
287 ret = read(ep->fd, p, n);
289 if (errno != EAGAIN) {
290 logerror("Error reading %s (%s)", ep->file, strerror(errno));
293 } else if (ret == 0) {
302 return -1; /* nothing read, old cache still ok */
305 ep->cids = (p - ep->cache) / ep->size;
310 * Write COUNT elements from BUF to EP, starting at ID.
311 * Return 0 on success, -1 on error.
314 do_write(struct empfile *ep, void *buf, int id, int count)
319 if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
322 if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
323 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
328 n = count * ep->size;
330 ret = write(ep->fd, p, n);
332 if (errno != EAGAIN) {
333 logerror("Error writing %s (%s)", ep->file, strerror(errno));
334 /* FIXME if this extended file, truncate back to old size */
347 * Write element ID into file-backed table TYPE from buffer FROM.
348 * FIXME pass buffer size!
349 * Write through cache straight to disk.
350 * Cannot write beyond the end of fully cached table (flags & EFF_MEM).
351 * Can write at the end of partially cached table.
352 * Return non-zero on success, zero on failure.
355 ef_write(int type, int id, void *from)
360 if (ef_check(type) < 0)
364 ep->prewrite(id, from);
365 if (CANT_HAPPEN((ep->flags & EFF_MEM) ? id >= ep->fids : id > ep->fids))
366 return 0; /* not implemented */
367 if (do_write(ep, from, id, 1) < 0)
369 if (id >= ep->baseid && id < ep->baseid + ep->cids) {
370 /* update the cache if necessary */
371 to = ep->cache + (id - ep->baseid) * ep->size;
373 memcpy(to, from, ep->size);
375 if (id >= ep->fids) {
376 /* write beyond end of file extends it, take note */
383 * Extend the file-backed table TYPE by COUNT elements.
384 * Return non-zero on success, zero on failure.
387 ef_extend(int type, int count)
393 if (ef_check(type) < 0)
396 if (CANT_HAPPEN(ep->fd < 0 || count < 0))
399 tmpobj = calloc(1, ep->size);
401 for (i = 0; i < count; i++) {
403 ep->init(id + i, tmpobj);
404 if (do_write(ep, tmpobj, id + i, 1) < 0)
409 if (ep->flags & EFF_MEM) {
410 /* FIXME lazy bastards... do this right */
411 /* XXX this will cause problems if there are ef_ptrs (to the
412 * old allocated structure) active when we do the re-open */
413 how = ep->flags & ~EFF_IMMUTABLE;
426 return empfile[type].cadef;
432 return empfile[type].fids;
438 return empfile[type].flags;
444 if (empfile[type].fd <= 0)
446 return fdate(empfile[type].fd);
450 * Search for a table matching NAME, return its table type.
451 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
455 ef_byname(char *name)
457 return stmtch(name, empfile, offsetof(struct empfile, name),
462 * Search CHOICES[] for a table type matching NAME, return it.
463 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
465 * CHOICES[] must be terminated with a negative value.
468 ef_byname_from(char *name, int choices[])
474 for (p = choices; *p >= 0; p++) {
475 if (ef_check(*p) < 0)
477 switch (mineq(name, empfile[*p].name)) {
495 if (ef_check(type) < 0)
496 return "bad ef_type";
497 return empfile[type].name;
503 if (CANT_HAPPEN((unsigned)type >= EF_MAX))
509 * Ensure file-backed table contains ID.
510 * If necessary, extend it in steps of COUNT elements.
511 * Return non-zero on success, zero on failure.
514 ef_ensure_space(int type, int id, int count)
516 if (ef_check(type) < 0)
520 while (id >= empfile[type].fids) {
521 if (!ef_extend(type, count))