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 * Open the table TYPE as view of table BASE.
198 * Return non-zero on success, zero on failure.
199 * Beware: views work only as long as BASE doesn't change size!
200 * You must call ef_close(TYPE) before closing BASE.
203 ef_open_view(int type, int base)
207 if (CANT_HAPPEN(!EF_IS_VIEW(type)))
210 if (CANT_HAPPEN(!(ef_flags(base) & EFF_MEM)))
213 ep->cache = empfile[base].cache;
214 ep->csize = empfile[base].csize;
215 ep->flags |= EFF_MEM;
216 ep->baseid = empfile[base].baseid;
217 ep->cids = empfile[base].cids;
218 ep->fids = empfile[base].fids;
223 * Close the file-backed table TYPE (EF_SECTOR, ...).
224 * Return non-zero on success, zero on failure.
232 if (ef_check(type) < 0)
236 if (EF_IS_VIEW(type))
241 ep->flags &= EFF_IMMUTABLE;
242 if (!(ep->flags & EFF_STATIC)) {
246 if (close(ep->fd) < 0) {
247 logerror("Error closing %s (%s)", ep->file, strerror(errno));
252 ep->baseid = ep->cids = ep->fids = 0;
257 * Flush file-backed table TYPE (EF_SECTOR, ...) to its backing file.
258 * Do nothing if the table is privately mapped.
259 * Update timestamps of written elements if table is EFF_TYPED.
260 * Return non-zero on success, zero on failure.
267 if (ef_check(type) < 0)
270 if (ep->flags & EFF_PRIVATE)
271 return 1; /* nothing to do */
272 if (CANT_HAPPEN(ep->fd < 0))
275 * We don't know which cache entries are dirty. ef_write() writes
276 * through, but direct updates through ef_ptr() don't. They are
277 * allowed only with EFF_MEM. Assume the whole cash is dirty
280 if (ep->flags & EFF_MEM) {
281 if (do_write(ep, ep->cache, ep->baseid, ep->cids) < 0)
289 * Return pointer to element ID in table TYPE if it exists, else NULL.
290 * The table must be fully cached, i.e. flags & EFF_MEM.
291 * The caller is responsible for flushing changes he makes.
294 ef_ptr(int type, int id)
298 if (ef_check(type) < 0)
301 if (CANT_HAPPEN(!(ep->flags & EFF_MEM) || !ep->cache))
303 if (id < 0 || id >= ep->fids)
305 return ep->cache + ep->size * id;
309 * Read element ID from table TYPE into buffer INTO.
310 * FIXME pass buffer size!
311 * Return non-zero on success, zero on failure.
314 ef_read(int type, int id, void *into)
319 if (ef_check(type) < 0)
322 if (CANT_HAPPEN(!ep->cache))
324 if (id < 0 || id >= ep->fids)
327 if (ep->flags & EFF_MEM) {
328 from = ep->cache + id * ep->size;
330 if (ep->baseid + ep->cids <= id || ep->baseid > id) {
331 if (fillcache(ep, id) < 1)
334 from = ep->cache + (id - ep->baseid) * ep->size;
336 memcpy(into, from, ep->size);
339 ep->postread(id, into);
344 * Fill cache of file-backed EP with elements starting at ID.
345 * If any were read, return their number.
346 * Else return -1 and leave the cache unchanged.
349 fillcache(struct empfile *ep, int id)
353 if (CANT_HAPPEN(!ep->cache))
356 ret = do_read(ep, ep->cache, id, MIN(ep->csize, ep->fids - id));
366 do_read(struct empfile *ep, void *buf, int id, int count)
371 if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
374 if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
375 logerror("Error seeking %s to elt %d (%s)",
376 ep->file, id, strerror(errno));
381 n = count * ep->size;
383 ret = read(ep->fd, p, n);
385 if (errno != EINTR) {
386 logerror("Error reading %s elt %d (%s)",
388 id + (int)((p - (char *)buf) / ep->size),
392 } else if (ret == 0) {
393 logerror("Unexpected EOF reading %s elt %d",
394 ep->file, id + (int)((p - (char *)buf) / ep->size));
402 return (p - (char *)buf) / ep->size;
406 * Write COUNT elements starting at ID from BUF to file-backed EP.
407 * Update the timestamp if the table is EFF_TYPED.
408 * Don't actually write if table is privately mapped.
409 * Return 0 on success, -1 on error (file may be corrupt then).
412 do_write(struct empfile *ep, void *buf, int id, int count)
416 struct emptypedstr *elt;
419 if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
422 if (ep->flags & EFF_TYPED) {
423 now = ep->flags & EFF_NOTIME ? (time_t)-1 : time(NULL);
424 for (i = 0; i < count; i++) {
426 * TODO Oopses here could be due to bad data corruption.
427 * Fail instead of attempting to recover?
429 elt = (struct emptypedstr *)((char *)buf + i * ep->size);
430 if (CANT_HAPPEN(elt->ef_type != ep->uid))
431 elt->ef_type = ep->uid;
432 if (CANT_HAPPEN(elt->uid != id + i))
434 if (now != (time_t)-1)
435 elt->timestamp = now;
439 if (ep->flags & EFF_PRIVATE)
442 if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
443 logerror("Error seeking %s to elt %d (%s)",
444 ep->file, id, strerror(errno));
449 n = count * ep->size;
451 ret = write(ep->fd, p, n);
453 if (errno != EINTR) {
454 logerror("Error writing %s elt %d (%s)",
456 id + (int)((p - (char *)buf) / ep->size),
470 * Write element ID into table TYPE from buffer FROM.
471 * FIXME pass buffer size!
472 * Update timestamp in FROM if table is EFF_TYPED.
473 * If table is file-backed and not privately mapped, write through
474 * cache straight to disk.
475 * Cannot write beyond the end of fully cached table (flags & EFF_MEM).
476 * Can write at the end of partially cached table.
477 * Return non-zero on success, zero on failure.
480 ef_write(int type, int id, void *from)
485 if (ef_check(type) < 0)
488 if (CANT_HAPPEN((ep->flags & (EFF_MEM | EFF_PRIVATE)) == EFF_PRIVATE))
491 ep->prewrite(id, from);
492 if (CANT_HAPPEN((ep->flags & EFF_MEM) ? id >= ep->fids : id > ep->fids))
493 return 0; /* not implemented */
496 if (do_write(ep, from, id, 1) < 0)
499 if (id >= ep->baseid && id < ep->baseid + ep->cids) {
500 /* update the cache if necessary */
501 to = ep->cache + (id - ep->baseid) * ep->size;
503 memcpy(to, from, ep->size);
505 if (id >= ep->fids) {
506 /* write beyond end of file extends it, take note */
514 * BUF is an element of table TYPE.
515 * ID is its new element ID.
516 * If table is EFF_TYPED, change id and sequence number stored in BUF.
520 ef_set_uid(int type, void *buf, int uid)
522 struct emptypedstr *elt;
525 if (ef_check(type) < 0)
528 if (!(ep->flags & EFF_TYPED))
534 elt->seqno = get_seqno(ep, uid);
538 * Return sequence number of element ID in table EP.
539 * Return zero if table is not EFF_TYPED (it has no sequence number
543 get_seqno(struct empfile *ep, int id)
545 struct emptypedstr *elt;
547 if (!(ep->flags & EFF_TYPED))
549 if (id < 0 || id >= ep->fids)
551 if (id >= ep->baseid && id < ep->baseid + ep->cids)
552 elt = (void *)(ep->cache + (id - ep->baseid) * ep->size);
554 /* need a buffer, steal last cache slot */
555 if (ep->cids == ep->csize)
557 elt = (void *)(ep->cache + ep->cids * ep->size);
558 if (do_read(ep, elt, id, 1) < 0)
559 return 0; /* deep trouble */
565 * Increment sequence number in BUF, which is about to be written to EP.
566 * Do nothing if table is not EFF_TYPED (it has no sequence number
570 new_seqno(struct empfile *ep, void *buf)
572 struct emptypedstr *elt = buf;
575 if (!(ep->flags & EFF_TYPED))
577 old_seqno = get_seqno(ep, elt->uid);
578 if (CANT_HAPPEN(old_seqno != elt->seqno))
579 old_seqno = MAX(old_seqno, elt->seqno);
580 elt->seqno = old_seqno + 1;
584 * Extend table TYPE by COUNT elements.
585 * Any pointers obtained from ef_ptr() become invalid.
586 * Return non-zero on success, zero on failure.
589 ef_extend(int type, int count)
593 int need_sentinel, i, id;
595 if (ef_check(type) < 0 || CANT_HAPPEN(EF_IS_VIEW(type)))
598 if (CANT_HAPPEN(count < 0))
602 if (ep->flags & EFF_MEM) {
603 need_sentinel = (ep->flags & EFF_SENTINEL) != 0;
604 if (id + count + need_sentinel > ep->csize) {
605 if (ep->flags & EFF_STATIC) {
606 logerror("Can't extend %s beyond %d elements",
607 ep->name, ep->csize - need_sentinel);
610 if (!ef_realloc_cache(ep, id + count + need_sentinel)) {
611 logerror("Can't extend %s to %d elements (%s)",
612 ep->name, id + count, strerror(errno));
616 p = ep->cache + id * ep->size;
617 do_blank(ep, p, id, count);
619 if (do_write(ep, p, id, count) < 0)
623 memset(ep->cache + (id + count) * ep->size, 0, ep->size);
624 ep->cids = id + count;
626 /* need a buffer, steal last cache slot */
627 if (ep->cids == ep->csize)
629 p = ep->cache + ep->cids * ep->size;
630 for (i = 0; i < count; i++) {
631 do_blank(ep, p, id + i, 1);
632 if (do_write(ep, p, id + i, 1) < 0)
636 ep->fids = id + count;
641 * Initialize element ID for EP in BUF.
642 * FIXME pass buffer size!
645 ef_blank(int type, int id, void *buf)
648 struct emptypedstr *elt;
650 if (ef_check(type) < 0)
653 do_blank(ep, buf, id, 1);
654 if (ep->flags & EFF_TYPED) {
656 elt->seqno = get_seqno(ep, elt->uid);
661 * Initialize COUNT elements of EP in BUF, starting with element ID.
664 do_blank(struct empfile *ep, void *buf, int id, int count)
667 struct emptypedstr *elt;
669 memset(buf, 0, count * ep->size);
670 if (ep->flags & EFF_TYPED) {
671 for (i = 0; i < count; i++) {
672 elt = (struct emptypedstr *)((char *)buf + i * ep->size);
673 elt->ef_type = ep->uid;
680 * Truncate table TYPE to COUNT elements.
681 * Any pointers obtained from ef_ptr() become invalid.
682 * Return non-zero on success, zero on failure.
685 ef_truncate(int type, int count)
690 if (ef_check(type) < 0 || CANT_HAPPEN(EF_IS_VIEW(type)))
693 if (CANT_HAPPEN(count < 0 || count > ep->fids))
696 if (ep->fd >= 0 && !(ep->flags & EFF_PRIVATE)) {
697 if (ftruncate(ep->fd, count * ep->size) < 0) {
698 logerror("Can't truncate %s to %d elements (%s)",
699 ep->file, count, strerror(errno));
705 if (ep->flags & EFF_MEM) {
706 need_sentinel = (ep->flags & EFF_SENTINEL) != 0;
707 if (!(ep->flags & EFF_STATIC)) {
708 if (!ef_realloc_cache(ep, count + need_sentinel)) {
709 logerror("Can't shrink %s cache after truncate (%s)",
710 ep->name, strerror(errno));
711 /* continue with unshrunk cache */
715 memset(ep->cache + count * ep->size, 0, ep->size);
718 if (ep->baseid >= count)
720 else if (ep->cids > count - ep->baseid)
721 ep->cids = count - ep->baseid;
730 if (ef_check(type) < 0)
732 return empfile[type].cadef;
738 if (ef_check(type) < 0)
740 return empfile[type].fids;
746 if (ef_check(type) < 0)
748 return empfile[type].flags;
754 if (ef_check(type) < 0)
756 if (empfile[type].fd <= 0)
758 return fdate(empfile[type].fd);
762 * Search for a table matching NAME, return its table type.
763 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
767 ef_byname(char *name)
769 return stmtch(name, empfile, offsetof(struct empfile, name),
774 * Search CHOICES[] for a table type matching NAME, return it.
775 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
777 * CHOICES[] must be terminated with a negative value.
780 ef_byname_from(char *name, int choices[])
786 for (p = choices; *p >= 0; p++) {
787 if (ef_check(*p) < 0)
789 switch (mineq(name, empfile[*p].name)) {
807 if (ef_check(type) < 0)
808 return "bad ef_type";
809 return empfile[type].name;
815 if (CANT_HAPPEN((unsigned)type >= EF_MAX))
821 * Ensure table contains element ID.
822 * If necessary, extend it in steps of COUNT elements.
823 * Return non-zero on success, zero on failure.
826 ef_ensure_space(int type, int id, int count)
828 if (ef_check(type) < 0)
832 while (id >= empfile[type].fids) {
833 if (!ef_extend(type, count))