2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2009, 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-2009
41 #include <sys/types.h>
51 #include "prototypes.h"
53 static int open_locked(char *, int, mode_t);
54 static int ef_realloc_cache(struct empfile *, int);
55 static int fillcache(struct empfile *, int);
56 static int do_read(struct empfile *, void *, int, int);
57 static int do_write(struct empfile *, void *, int, int);
58 static unsigned get_seqno(struct empfile *, int);
59 static void new_seqno(struct empfile *, void *);
60 static void do_blank(struct empfile *, void *, int, int);
61 static int ef_check(int);
64 * Open the file-backed table TYPE (EF_SECTOR, ...).
65 * HOW are flags to control operation. Naturally, immutable flags are
67 * If NELT is non-negative, the table must have that many elements.
68 * Return non-zero on success, zero on failure.
69 * You must call ef_close() before the next ef_open().
72 ef_open(int type, int how, int nelt)
75 int oflags, fd, fsiz, nslots;
77 if (ef_check(type) < 0)
79 if (CANT_HAPPEN(how & EFF_IMMUTABLE))
80 how &= ~EFF_IMMUTABLE;
84 if (CANT_HAPPEN(ep->fd >= 0))
87 if (how & EFF_PRIVATE)
90 oflags |= O_CREAT | O_TRUNC;
91 fd = open_locked(ep->file, oflags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
93 logerror("Can't open %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;
106 if (nelt >= 0 && nelt != ep->fids) {
107 logerror("Can't open %s (got %d records instead of %d)",
108 ep->file, ep->fids, nelt);
114 if (ep->flags & EFF_STATIC) {
115 /* ep->cache already points to space for ep->csize elements */
117 if (ep->fids > ep->csize) {
118 logerror("Can't open %s: file larger than %d bytes",
119 ep->file, ep->fids * ep->size);
125 if (CANT_HAPPEN(ep->cache))
130 nslots = blksize(fd) / ep->size;
131 if (!ef_realloc_cache(ep, nslots)) {
132 logerror("Can't map %s (%s)", ep->file, strerror(errno));
139 ep->flags = (ep->flags & EFF_IMMUTABLE) | (how & ~EFF_CREATE);
142 /* map file into cache */
143 if ((how & EFF_MEM) && ep->fids) {
144 if (fillcache(ep, 0) != ep->fids) {
145 ep->cids = 0; /* prevent cache flush */
146 ep->flags &= EFF_IMMUTABLE; /* maintain invariant */
152 if (ep->onresize && ep->onresize(type) < 0)
158 open_locked(char *name, int oflags, mode_t mode)
160 int rdlonly = (oflags & O_ACCMODE) == O_RDONLY;
164 fd = _sopen(name, oflags | O_BINARY, rdlonly ? SH_DENYNO : SH_DENYWR,
171 fd = open(name, oflags, mode);
175 lock.l_type = rdlonly ? F_RDLCK : F_WRLCK;
176 lock.l_whence = SEEK_SET;
177 lock.l_start = lock.l_len = 0;
178 if (fcntl(fd, F_SETLK, &lock) == -1) {
187 * Reallocate cache for table EP to hold COUNT slots.
188 * The table must not be allocated statically.
189 * The cache may still be unmapped.
190 * If reallocation succeeds, any pointers obtained from ef_ptr()
192 * If it fails, the cache is unchanged, and errno is set.
193 * Return non-zero on success, zero on failure.
196 ef_realloc_cache(struct empfile *ep, int count)
200 if (CANT_HAPPEN(ep->flags & EFF_STATIC))
202 if (CANT_HAPPEN(count < 0))
206 * Avoid zero slots, because that can lead to null cache, which
207 * would be interpreted as unmapped cache.
211 cache = realloc(ep->cache, count * ep->size);
221 * Open the table TYPE as view of table BASE.
222 * Return non-zero on success, zero on failure.
223 * Beware: views work only as long as BASE doesn't change size!
224 * You must call ef_close(TYPE) before closing BASE.
227 ef_open_view(int type, int base)
231 if (CANT_HAPPEN(!EF_IS_VIEW(type)))
234 if (CANT_HAPPEN(!(ef_flags(base) & EFF_MEM)))
237 ep->cache = empfile[base].cache;
238 ep->csize = empfile[base].csize;
239 ep->flags |= EFF_MEM;
240 ep->baseid = empfile[base].baseid;
241 ep->cids = empfile[base].cids;
242 ep->fids = empfile[base].fids;
247 * Close the file-backed table TYPE (EF_SECTOR, ...).
248 * Return non-zero on success, zero on failure.
256 if (ef_check(type) < 0)
260 if (EF_IS_VIEW(type))
265 ep->flags &= EFF_IMMUTABLE;
266 if (!(ep->flags & EFF_STATIC)) {
270 if (close(ep->fd) < 0) {
271 logerror("Error closing %s (%s)", ep->file, strerror(errno));
276 ep->baseid = ep->cids = ep->fids = 0;
277 if (ep->onresize && ep->onresize(type) < 0)
283 * Flush file-backed table TYPE (EF_SECTOR, ...) to its backing file.
284 * Do nothing if the table is privately mapped.
285 * Update timestamps of written elements if table is EFF_TYPED.
286 * Return non-zero on success, zero on failure.
293 if (ef_check(type) < 0)
296 if (ep->flags & EFF_PRIVATE)
297 return 1; /* nothing to do */
298 if (CANT_HAPPEN(ep->fd < 0))
301 * We don't know which cache entries are dirty. ef_write() writes
302 * through, but direct updates through ef_ptr() don't. They are
303 * allowed only with EFF_MEM. Assume the whole cash is dirty
306 if (ep->flags & EFF_MEM) {
307 if (do_write(ep, ep->cache, ep->baseid, ep->cids) < 0)
315 * Return pointer to element ID in table TYPE if it exists, else NULL.
316 * The table must be fully cached, i.e. flags & EFF_MEM.
317 * The caller is responsible for flushing changes he makes.
320 ef_ptr(int type, int id)
324 if (ef_check(type) < 0)
327 if (CANT_HAPPEN(!(ep->flags & EFF_MEM) || !ep->cache))
329 if (id < 0 || id >= ep->fids)
331 return ep->cache + ep->size * id;
335 * Read element ID from table TYPE into buffer INTO.
336 * FIXME pass buffer size!
337 * Return non-zero on success, zero on failure.
340 ef_read(int type, int id, void *into)
345 if (ef_check(type) < 0)
348 if (CANT_HAPPEN(!ep->cache))
350 if (id < 0 || id >= ep->fids)
353 if (ep->flags & EFF_MEM) {
354 cachep = ep->cache + id * ep->size;
356 if (ep->baseid + ep->cids <= id || ep->baseid > id) {
357 if (fillcache(ep, id) < 1)
360 cachep = ep->cache + (id - ep->baseid) * ep->size;
362 memcpy(into, cachep, ep->size);
365 ep->postread(id, into);
370 * Fill cache of file-backed EP with elements starting at ID.
371 * If any were read, return their number.
372 * Else return -1 and leave the cache unchanged.
375 fillcache(struct empfile *ep, int id)
379 if (CANT_HAPPEN(!ep->cache))
382 ret = do_read(ep, ep->cache, id, MIN(ep->csize, ep->fids - id));
392 do_read(struct empfile *ep, void *buf, int id, int count)
397 if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
400 if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
401 logerror("Error seeking %s to elt %d (%s)",
402 ep->file, id, strerror(errno));
407 n = count * ep->size;
409 ret = read(ep->fd, p, n);
411 if (errno != EINTR) {
412 logerror("Error reading %s elt %d (%s)",
414 id + (int)((p - (char *)buf) / ep->size),
418 } else if (ret == 0) {
419 logerror("Unexpected EOF reading %s elt %d",
420 ep->file, id + (int)((p - (char *)buf) / ep->size));
428 return (p - (char *)buf) / ep->size;
432 * Write COUNT elements starting at ID from BUF to file-backed EP.
433 * Update the timestamp if the table is EFF_TYPED.
434 * Don't actually write if table is privately mapped.
435 * Return 0 on success, -1 on error (file may be corrupt then).
438 do_write(struct empfile *ep, void *buf, int id, int count)
442 struct emptypedstr *elt;
445 if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
448 if (ep->flags & EFF_TYPED) {
449 now = ep->flags & EFF_NOTIME ? (time_t)-1 : time(NULL);
450 for (i = 0; i < count; i++) {
452 * TODO Oopses here could be due to bad data corruption.
453 * Fail instead of attempting to recover?
455 elt = (struct emptypedstr *)((char *)buf + i * ep->size);
456 if (CANT_HAPPEN(elt->ef_type != ep->uid))
457 elt->ef_type = ep->uid;
458 if (CANT_HAPPEN(elt->uid != id + i))
460 if (now != (time_t)-1)
461 elt->timestamp = now;
465 if (ep->flags & EFF_PRIVATE)
468 if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
469 logerror("Error seeking %s to elt %d (%s)",
470 ep->file, id, strerror(errno));
475 n = count * ep->size;
477 ret = write(ep->fd, p, n);
479 if (errno != EINTR) {
480 logerror("Error writing %s elt %d (%s)",
482 id + (int)((p - (char *)buf) / ep->size),
496 * Write element ID into table TYPE from buffer FROM.
497 * FIXME pass buffer size!
498 * Update timestamp in FROM if table is EFF_TYPED.
499 * If table is file-backed and not privately mapped, write through
500 * cache straight to disk.
501 * Cannot write beyond the end of fully cached table (flags & EFF_MEM).
502 * Can write at the end of partially cached table.
503 * Return non-zero on success, zero on failure.
506 ef_write(int type, int id, void *from)
511 if (ef_check(type) < 0)
514 if (CANT_HAPPEN((ep->flags & (EFF_MEM | EFF_PRIVATE)) == EFF_PRIVATE))
516 if (CANT_HAPPEN((ep->flags & EFF_MEM) ? id >= ep->fids : id > ep->fids))
517 return 0; /* not implemented */
519 if (id >= ep->fids) {
520 /* write beyond end of file extends it, take note */
522 if (ep->onresize && ep->onresize(type) < 0)
525 if (id >= ep->baseid && id < ep->baseid + ep->cids)
526 cachep = ep->cache + (id - ep->baseid) * ep->size;
530 ep->prewrite(id, cachep, from);
532 if (do_write(ep, from, id, 1) < 0)
535 if (cachep && cachep != from) /* update the cache if necessary */
536 memcpy(cachep, from, ep->size);
542 * BUF is an element of table TYPE.
543 * ID is its new element ID.
544 * If table is EFF_TYPED, change id and sequence number stored in BUF.
548 ef_set_uid(int type, void *buf, int uid)
550 struct emptypedstr *elt;
553 if (ef_check(type) < 0)
556 if (!(ep->flags & EFF_TYPED))
562 elt->seqno = get_seqno(ep, uid);
566 * Return sequence number of element ID in table EP.
567 * Return zero if table is not EFF_TYPED (it has no sequence number
571 get_seqno(struct empfile *ep, int id)
573 struct emptypedstr *elt;
575 if (!(ep->flags & EFF_TYPED))
577 if (id < 0 || id >= ep->fids)
579 if (id >= ep->baseid && id < ep->baseid + ep->cids)
580 elt = (void *)(ep->cache + (id - ep->baseid) * ep->size);
582 /* need a buffer, steal last cache slot */
583 if (ep->cids == ep->csize)
585 elt = (void *)(ep->cache + ep->cids * ep->size);
586 if (do_read(ep, elt, id, 1) < 0)
587 return 0; /* deep trouble */
593 * Increment sequence number in BUF, which is about to be written to EP.
594 * Do nothing if table is not EFF_TYPED (it has no sequence number
598 new_seqno(struct empfile *ep, void *buf)
600 struct emptypedstr *elt = buf;
603 if (!(ep->flags & EFF_TYPED))
605 old_seqno = get_seqno(ep, elt->uid);
606 CANT_HAPPEN(old_seqno != elt->seqno);
607 elt->seqno = old_seqno + 1;
611 * Extend table TYPE by COUNT elements.
612 * Any pointers obtained from ef_ptr() become invalid.
613 * Return non-zero on success, zero on failure.
616 ef_extend(int type, int count)
620 int need_sentinel, i, id;
622 if (ef_check(type) < 0 || CANT_HAPPEN(EF_IS_VIEW(type)))
625 if (CANT_HAPPEN(count < 0))
629 if (ep->flags & EFF_MEM) {
630 need_sentinel = (ep->flags & EFF_SENTINEL) != 0;
631 if (id + count + need_sentinel > ep->csize) {
632 if (ep->flags & EFF_STATIC) {
633 logerror("Can't extend %s beyond %d elements",
634 ep->name, ep->csize - need_sentinel);
637 if (!ef_realloc_cache(ep, id + count + need_sentinel)) {
638 logerror("Can't extend %s to %d elements (%s)",
639 ep->name, id + count, strerror(errno));
643 p = ep->cache + id * ep->size;
644 do_blank(ep, p, id, count);
646 if (do_write(ep, p, id, count) < 0)
650 memset(ep->cache + (id + count) * ep->size, 0, ep->size);
651 ep->cids = id + count;
653 /* need a buffer, steal last cache slot */
654 if (ep->cids == ep->csize)
656 p = ep->cache + ep->cids * ep->size;
657 for (i = 0; i < count; i++) {
658 do_blank(ep, p, id + i, 1);
659 if (do_write(ep, p, id + i, 1) < 0)
663 ep->fids = id + count;
664 if (ep->onresize && ep->onresize(type) < 0)
670 * Initialize element ID for EP in BUF.
671 * FIXME pass buffer size!
674 ef_blank(int type, int id, void *buf)
677 struct emptypedstr *elt;
679 if (ef_check(type) < 0)
682 do_blank(ep, buf, id, 1);
683 if (ep->flags & EFF_TYPED) {
685 elt->seqno = get_seqno(ep, elt->uid);
690 * Initialize COUNT elements of EP in BUF, starting with element ID.
693 do_blank(struct empfile *ep, void *buf, int id, int count)
696 struct emptypedstr *elt;
698 memset(buf, 0, count * ep->size);
699 for (i = 0; i < count; i++) {
700 elt = (struct emptypedstr *)((char *)buf + i * ep->size);
701 if (ep->flags & EFF_TYPED) {
702 elt->ef_type = ep->uid;
711 * Truncate table TYPE to COUNT elements.
712 * Any pointers obtained from ef_ptr() become invalid.
713 * Return non-zero on success, zero on failure.
716 ef_truncate(int type, int count)
721 if (ef_check(type) < 0 || CANT_HAPPEN(EF_IS_VIEW(type)))
724 if (CANT_HAPPEN(count < 0 || count > ep->fids))
727 if (ep->fd >= 0 && !(ep->flags & EFF_PRIVATE)) {
728 if (ftruncate(ep->fd, count * ep->size) < 0) {
729 logerror("Can't truncate %s to %d elements (%s)",
730 ep->file, count, strerror(errno));
736 if (ep->flags & EFF_MEM) {
737 need_sentinel = (ep->flags & EFF_SENTINEL) != 0;
738 if (!(ep->flags & EFF_STATIC)) {
739 if (!ef_realloc_cache(ep, count + need_sentinel)) {
740 logerror("Can't shrink %s cache after truncate (%s)",
741 ep->name, strerror(errno));
742 /* continue with unshrunk cache */
746 memset(ep->cache + count * ep->size, 0, ep->size);
749 if (ep->baseid >= count)
751 else if (ep->cids > count - ep->baseid)
752 ep->cids = count - ep->baseid;
755 if (ep->onresize && ep->onresize(type) < 0)
763 if (ef_check(type) < 0)
765 return empfile[type].cadef;
771 if (ef_check(type) < 0)
773 return empfile[type].fids;
779 if (ef_check(type) < 0)
781 return empfile[type].flags;
787 if (ef_check(type) < 0)
789 if (empfile[type].fd <= 0)
791 return fdate(empfile[type].fd);
795 * Search for a table matching NAME, return its table type.
796 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
800 ef_byname(char *name)
802 return stmtch(name, empfile, offsetof(struct empfile, name),
807 * Search CHOICES[] for a table type matching NAME, return it.
808 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
810 * CHOICES[] must be terminated with a negative value.
813 ef_byname_from(char *name, int choices[])
819 for (p = choices; *p >= 0; p++) {
820 if (ef_check(*p) < 0)
822 switch (mineq(name, empfile[*p].name)) {
840 if (ef_check(type) < 0)
841 return "bad ef_type";
842 return empfile[type].name;
848 if (CANT_HAPPEN((unsigned)type >= EF_MAX))
854 * Ensure table contains element ID.
855 * If necessary, extend it in steps of COUNT elements.
856 * Return non-zero on success, zero on failure.
859 ef_ensure_space(int type, int id, int count)
861 if (ef_check(type) < 0)
865 while (id >= empfile[type].fids) {
866 if (!ef_extend(type, count))