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-2008
41 #include <sys/types.h>
47 #include "prototypes.h"
49 static int ef_realloc_cache(struct empfile *, int);
50 static int fillcache(struct empfile *, int);
51 static int do_read(struct empfile *, void *, int, int);
52 static int do_write(struct empfile *, void *, int, int);
53 static unsigned get_seqno(struct empfile *, int);
54 static void new_seqno(struct empfile *, void *);
55 static void do_blank(struct empfile *, void *, int, int);
56 static int ef_check(int);
59 * Open the file-backed table TYPE (EF_SECTOR, ...).
60 * HOW are flags to control operation. Naturally, immutable flags are
62 * If NELT is non-negative, the table must have that many elements.
63 * Return non-zero on success, zero on failure.
64 * You must call ef_close() before the next ef_open().
67 ef_open(int type, int how, int nelt)
71 int oflags, fd, fsiz, nslots;
73 if (ef_check(type) < 0)
75 if (CANT_HAPPEN(how & EFF_IMMUTABLE))
76 how &= ~EFF_IMMUTABLE;
80 if (CANT_HAPPEN(ep->fd >= 0))
83 if (how & EFF_PRIVATE)
86 oflags |= O_CREAT | O_TRUNC;
90 if ((fd = open(ep->file, oflags, S_IRWUG)) < 0) {
91 logerror("Can't open %s (%s)", ep->file, strerror(errno));
95 lock.l_type = how & EFF_PRIVATE ? F_RDLCK : F_WRLCK;
96 lock.l_whence = SEEK_SET;
97 lock.l_start = lock.l_len = 0;
98 if (fcntl(fd, F_SETLK, &lock) == -1) {
99 logerror("Can't lock %s (%s)", ep->file, strerror(errno));
106 if (fsiz % ep->size) {
107 logerror("Can't open %s (file size not a multiple of record size %d)",
112 ep->fids = fsiz / ep->size;
113 if (nelt >= 0 && nelt != ep->fids) {
114 logerror("Can't open %s (got %d records instead of %d)",
115 ep->file, ep->fids, nelt);
121 if (ep->flags & EFF_STATIC) {
122 /* ep->cache already points to space for ep->csize elements */
124 if (ep->fids > ep->csize) {
125 logerror("Can't open %s: file larger than %d bytes",
126 ep->file, ep->fids * ep->size);
132 if (CANT_HAPPEN(ep->cache))
137 nslots = blksize(fd) / ep->size;
138 if (!ef_realloc_cache(ep, nslots)) {
139 logerror("Can't map %s (%s)", ep->file, strerror(errno));
146 ep->flags = (ep->flags & EFF_IMMUTABLE) | (how & ~EFF_CREATE);
149 /* map file into cache */
150 if ((how & EFF_MEM) && ep->fids) {
151 if (fillcache(ep, 0) != ep->fids) {
152 ep->cids = 0; /* prevent cache flush */
153 ep->flags &= EFF_IMMUTABLE; /* maintain invariant */
163 * Reallocate cache for table EP to hold COUNT slots.
164 * The table must not be allocated statically.
165 * The cache may still be unmapped.
166 * If reallocation succeeds, any pointers obtained from ef_ptr()
168 * If it fails, the cache is unchanged, and errno is set.
169 * Return non-zero on success, zero on failure.
172 ef_realloc_cache(struct empfile *ep, int count)
176 if (CANT_HAPPEN(ep->flags & EFF_STATIC))
178 if (CANT_HAPPEN(count < 0))
182 * Avoid zero slots, because that can lead to null cache, which
183 * would be interpreted as unmapped cache.
187 cache = realloc(ep->cache, count * ep->size);
197 * Close the file-backed table TYPE (EF_SECTOR, ...).
198 * Return non-zero on success, zero on failure.
206 retval = ef_flush(type);
208 ep->flags &= EFF_IMMUTABLE;
209 if (!(ep->flags & EFF_STATIC)) {
213 if (close(ep->fd) < 0) {
214 logerror("Error closing %s (%s)", ep->file, strerror(errno));
222 * Flush table TYPE (EF_SECTOR, ...) to disk.
223 * Do nothing if the table is privately mapped.
224 * Update timestamps of written elements if table is EFF_TYPED.
225 * Return non-zero on success, zero on failure.
232 if (ef_check(type) < 0)
235 if (ep->flags & EFF_PRIVATE)
236 return 1; /* nothing to do */
237 if (CANT_HAPPEN(ep->fd < 0))
240 * We don't know which cache entries are dirty. ef_write() writes
241 * through, but direct updates through ef_ptr() don't. They are
242 * allowed only with EFF_MEM. Assume the whole cash is dirty
245 if (ep->flags & EFF_MEM) {
246 if (do_write(ep, ep->cache, ep->baseid, ep->cids) < 0)
254 * Return pointer to element ID in table TYPE if it exists, else NULL.
255 * The table must be fully cached, i.e. flags & EFF_MEM.
256 * The caller is responsible for flushing changes he makes.
259 ef_ptr(int type, int id)
263 if (ef_check(type) < 0)
266 if (CANT_HAPPEN(!(ep->flags & EFF_MEM) || !ep->cache))
268 if (id < 0 || id >= ep->fids)
270 return ep->cache + ep->size * id;
274 * Read element ID from table TYPE into buffer INTO.
275 * FIXME pass buffer size!
276 * Return non-zero on success, zero on failure.
279 ef_read(int type, int id, void *into)
284 if (ef_check(type) < 0)
287 if (CANT_HAPPEN(!ep->cache))
289 if (id < 0 || id >= ep->fids)
292 if (ep->flags & EFF_MEM) {
293 from = ep->cache + id * ep->size;
295 if (ep->baseid + ep->cids <= id || ep->baseid > id) {
296 if (fillcache(ep, id) < 1)
299 from = ep->cache + (id - ep->baseid) * ep->size;
301 memcpy(into, from, ep->size);
304 ep->postread(id, into);
309 * Fill cache of file-backed EP with elements starting at ID.
310 * If any were read, return their number.
311 * Else return -1 and leave the cache unchanged.
314 fillcache(struct empfile *ep, int id)
318 if (CANT_HAPPEN(!ep->cache))
321 ret = do_read(ep, ep->cache, id, MIN(ep->csize, ep->fids - id));
331 do_read(struct empfile *ep, void *buf, int id, int count)
336 if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
339 if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
340 logerror("Error seeking %s to elt %d (%s)",
341 ep->file, id, strerror(errno));
346 n = count * ep->size;
348 ret = read(ep->fd, p, n);
350 if (errno != EINTR) {
351 logerror("Error reading %s elt %d (%s)",
353 id + (int)((p - (char *)buf) / ep->size),
357 } else if (ret == 0) {
358 logerror("Unexpected EOF reading %s elt %d",
359 ep->file, id + (int)((p - (char *)buf) / ep->size));
367 return (p - (char *)buf) / ep->size;
371 * Write COUNT elements starting at ID from BUF to file-backed EP.
372 * Update the timestamp if the table is EFF_TYPED.
373 * Don't actually write if table is privately mapped.
374 * Return 0 on success, -1 on error (file may be corrupt then).
377 do_write(struct empfile *ep, void *buf, int id, int count)
381 struct emptypedstr *elt;
384 if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
387 if (ep->flags & EFF_TYPED) {
388 now = ep->flags & EFF_NOTIME ? (time_t)-1 : time(NULL);
389 for (i = 0; i < count; i++) {
391 * TODO Oopses here could be due to bad data corruption.
392 * Fail instead of attempting to recover?
394 elt = (struct emptypedstr *)((char *)buf + i * ep->size);
395 if (CANT_HAPPEN(elt->ef_type != ep->uid))
396 elt->ef_type = ep->uid;
397 if (CANT_HAPPEN(elt->uid != id + i))
399 if (now != (time_t)-1)
400 elt->timestamp = now;
404 if (ep->flags & EFF_PRIVATE)
407 if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
408 logerror("Error seeking %s to elt %d (%s)",
409 ep->file, id, strerror(errno));
414 n = count * ep->size;
416 ret = write(ep->fd, p, n);
418 if (errno != EINTR) {
419 logerror("Error writing %s elt %d (%s)",
421 id + (int)((p - (char *)buf) / ep->size),
435 * Write element ID into table TYPE from buffer FROM.
436 * FIXME pass buffer size!
437 * Update timestamp in FROM if table is EFF_TYPED.
438 * If table is file-backed and not privately mapped, write through
439 * cache straight to disk.
440 * Cannot write beyond the end of fully cached table (flags & EFF_MEM).
441 * Can write at the end of partially cached table.
442 * Return non-zero on success, zero on failure.
445 ef_write(int type, int id, void *from)
450 if (ef_check(type) < 0)
453 if (CANT_HAPPEN((ep->flags & (EFF_MEM | EFF_PRIVATE)) == EFF_PRIVATE))
456 ep->prewrite(id, from);
457 if (CANT_HAPPEN((ep->flags & EFF_MEM) ? id >= ep->fids : id > ep->fids))
458 return 0; /* not implemented */
461 if (do_write(ep, from, id, 1) < 0)
464 if (id >= ep->baseid && id < ep->baseid + ep->cids) {
465 /* update the cache if necessary */
466 to = ep->cache + (id - ep->baseid) * ep->size;
468 memcpy(to, from, ep->size);
470 if (id >= ep->fids) {
471 /* write beyond end of file extends it, take note */
479 * BUF is an element of table TYPE.
480 * ID is its new element ID.
481 * If table is EFF_TYPED, change id and sequence number stored in BUF.
485 ef_set_uid(int type, void *buf, int uid)
487 struct emptypedstr *elt;
490 if (ef_check(type) < 0)
493 if (!(ep->flags & EFF_TYPED))
499 elt->seqno = get_seqno(ep, uid);
503 * Return sequence number of element ID in table EP.
504 * Return zero if table is not EFF_TYPED (it has no sequence number
508 get_seqno(struct empfile *ep, int id)
510 struct emptypedstr *elt;
512 if (!(ep->flags & EFF_TYPED))
514 if (id < 0 || id >= ep->fids)
516 if (id >= ep->baseid && id < ep->baseid + ep->cids)
517 elt = (void *)(ep->cache + (id - ep->baseid) * ep->size);
519 /* need a buffer, steal last cache slot */
520 if (ep->cids == ep->csize)
522 elt = (void *)(ep->cache + ep->cids * ep->size);
523 if (do_read(ep, elt, id, 1) < 0)
524 return 0; /* deep trouble */
530 * Increment sequence number in BUF, which is about to be written to EP.
531 * Do nothing if table is not EFF_TYPED (it has no sequence number
535 new_seqno(struct empfile *ep, void *buf)
537 struct emptypedstr *elt = buf;
540 if (!(ep->flags & EFF_TYPED))
542 old_seqno = get_seqno(ep, elt->uid);
543 if (CANT_HAPPEN(old_seqno != elt->seqno))
544 old_seqno = MAX(old_seqno, elt->seqno);
545 elt->seqno = old_seqno + 1;
549 * Extend table TYPE by COUNT elements.
550 * Any pointers obtained from ef_ptr() become invalid.
551 * Return non-zero on success, zero on failure.
554 ef_extend(int type, int count)
558 int need_sentinel, i, id;
560 if (ef_check(type) < 0)
563 if (CANT_HAPPEN(count < 0))
567 if (ep->flags & EFF_MEM) {
568 need_sentinel = (ep->flags & EFF_SENTINEL) != 0;
569 if (id + count + need_sentinel > ep->csize) {
570 if (ep->flags & EFF_STATIC) {
571 logerror("Can't extend %s beyond %d elements",
572 ep->name, ep->csize - need_sentinel);
575 if (!ef_realloc_cache(ep, id + count + need_sentinel)) {
576 logerror("Can't extend %s to %d elements (%s)",
577 ep->name, id + count, strerror(errno));
581 p = ep->cache + id * ep->size;
582 do_blank(ep, p, id, count);
584 if (do_write(ep, p, id, count) < 0)
588 memset(ep->cache + (id + count) * ep->size, 0, ep->size);
589 ep->cids = id + count;
591 /* need a buffer, steal last cache slot */
592 if (ep->cids == ep->csize)
594 p = ep->cache + ep->cids * ep->size;
595 for (i = 0; i < count; i++) {
596 do_blank(ep, p, id + i, 1);
597 if (do_write(ep, p, id + i, 1) < 0)
601 ep->fids = id + count;
606 * Initialize element ID for EP in BUF.
607 * FIXME pass buffer size!
610 ef_blank(int type, int id, void *buf)
613 struct emptypedstr *elt;
615 if (ef_check(type) < 0)
618 do_blank(ep, buf, id, 1);
619 if (ep->flags & EFF_TYPED) {
621 elt->seqno = get_seqno(ep, elt->uid);
626 * Initialize COUNT elements of EP in BUF, starting with element ID.
629 do_blank(struct empfile *ep, void *buf, int id, int count)
632 struct emptypedstr *elt;
634 memset(buf, 0, count * ep->size);
635 if (ep->flags & EFF_TYPED) {
636 for (i = 0; i < count; i++) {
637 elt = (struct emptypedstr *)((char *)buf + i * ep->size);
638 elt->ef_type = ep->uid;
645 * Truncate table TYPE to COUNT elements.
646 * Any pointers obtained from ef_ptr() become invalid.
647 * Return non-zero on success, zero on failure.
650 ef_truncate(int type, int count)
655 if (ef_check(type) < 0)
658 if (CANT_HAPPEN(count < 0 || count > ep->fids))
661 if (ep->fd >= 0 && !(ep->flags & EFF_PRIVATE)) {
662 if (ftruncate(ep->fd, count * ep->size) < 0) {
663 logerror("Can't truncate %s to %d elements (%s)",
664 ep->file, count, strerror(errno));
670 if (ep->flags & EFF_MEM) {
671 need_sentinel = (ep->flags & EFF_SENTINEL) != 0;
672 if (!(ep->flags & EFF_STATIC)) {
673 if (!ef_realloc_cache(ep, count + need_sentinel)) {
674 logerror("Can't shrink %s cache after truncate (%s)",
675 ep->name, strerror(errno));
676 /* continue with unshrunk cache */
680 memset(ep->cache + count * ep->size, 0, ep->size);
683 if (ep->baseid >= count)
685 else if (ep->cids > count - ep->baseid)
686 ep->cids = count - ep->baseid;
695 if (ef_check(type) < 0)
697 return empfile[type].cadef;
703 if (ef_check(type) < 0)
705 return empfile[type].fids;
711 if (ef_check(type) < 0)
713 return empfile[type].flags;
719 if (ef_check(type) < 0)
721 if (empfile[type].fd <= 0)
723 return fdate(empfile[type].fd);
727 * Search for a table matching NAME, return its table type.
728 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
732 ef_byname(char *name)
734 return stmtch(name, empfile, offsetof(struct empfile, name),
739 * Search CHOICES[] for a table type matching NAME, return it.
740 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
742 * CHOICES[] must be terminated with a negative value.
745 ef_byname_from(char *name, int choices[])
751 for (p = choices; *p >= 0; p++) {
752 if (ef_check(*p) < 0)
754 switch (mineq(name, empfile[*p].name)) {
772 if (ef_check(type) < 0)
773 return "bad ef_type";
774 return empfile[type].name;
780 if (CANT_HAPPEN((unsigned)type >= EF_MAX))
786 * Ensure table contains element ID.
787 * If necessary, extend it in steps of COUNT elements.
788 * Return non-zero on success, zero on failure.
791 ef_ensure_space(int type, int id, int count)
793 if (ef_check(type) < 0)
797 while (id >= empfile[type].fids) {
798 if (!ef_extend(type, count))