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);
58 * Open the file-backed table TYPE (EF_SECTOR, ...).
59 * HOW are flags to control operation. Naturally, immutable flags are
61 * If NELT is non-negative, the table must have that many elements.
62 * Return non-zero on success, zero on failure.
63 * You must call ef_close() before the next ef_open().
66 ef_open(int type, int how, int nelt)
70 int oflags, fd, fsiz, nslots;
72 if (ef_check(type) < 0)
74 if (CANT_HAPPEN(how & EFF_IMMUTABLE))
75 how &= ~EFF_IMMUTABLE;
79 if (CANT_HAPPEN(ep->fd >= 0))
82 if (how & EFF_PRIVATE)
85 oflags |= O_CREAT | O_TRUNC;
89 if ((fd = open(ep->file, oflags, S_IRWUG)) < 0) {
90 logerror("Can't open %s (%s)", ep->file, strerror(errno));
94 lock.l_type = how & EFF_PRIVATE ? F_RDLCK : F_WRLCK;
95 lock.l_whence = SEEK_SET;
96 lock.l_start = lock.l_len = 0;
97 if (fcntl(fd, F_SETLK, &lock) == -1) {
98 logerror("Can't lock %s (%s)", ep->file, strerror(errno));
105 if (fsiz % ep->size) {
106 logerror("Can't open %s (file size not a multiple of record size %d)",
111 ep->fids = fsiz / ep->size;
112 if (nelt >= 0 && nelt != ep->fids) {
113 logerror("Can't open %s (got %d records instead of %d)",
114 ep->file, ep->fids, nelt);
120 if (ep->flags & EFF_STATIC) {
121 /* ep->cache already points to space for ep->csize elements */
123 if (ep->fids > ep->csize) {
124 logerror("Can't open %s: file larger than %d bytes",
125 ep->file, ep->fids * ep->size);
131 if (CANT_HAPPEN(ep->cache))
136 nslots = blksize(fd) / ep->size;
137 if (!ef_realloc_cache(ep, nslots)) {
138 logerror("Can't map %s (%s)", ep->file, strerror(errno));
145 ep->flags = (ep->flags & EFF_IMMUTABLE) | (how & ~EFF_CREATE);
148 /* map file into cache */
149 if ((how & EFF_MEM) && ep->fids) {
150 if (fillcache(ep, 0) != ep->fids) {
151 ep->cids = 0; /* prevent cache flush */
152 ep->flags &= EFF_IMMUTABLE; /* maintain invariant */
162 * Reallocate cache for table EP to hold COUNT slots.
163 * The table must not be allocated statically.
164 * The cache may still be unmapped.
165 * If reallocation succeeds, any pointers obtained from ef_ptr()
167 * If it fails, the cache is unchanged, and errno is set.
168 * Return non-zero on success, zero on failure.
171 ef_realloc_cache(struct empfile *ep, int count)
175 if (CANT_HAPPEN(ep->flags & EFF_STATIC))
177 if (CANT_HAPPEN(count < 0))
181 * Avoid zero slots, because that can lead to null cache, which
182 * would be interpreted as unmapped cache.
186 cache = realloc(ep->cache, count * ep->size);
196 * Close the file-backed table TYPE (EF_SECTOR, ...).
197 * Return non-zero on success, zero on failure.
205 retval = ef_flush(type);
207 ep->flags &= EFF_IMMUTABLE;
208 if (!(ep->flags & EFF_STATIC)) {
212 if (close(ep->fd) < 0) {
213 logerror("Error closing %s (%s)", ep->file, strerror(errno));
221 * Flush table TYPE (EF_SECTOR, ...) to disk.
222 * Do nothing if the table is privately mapped.
223 * Update timestamps of written elements if table is EFF_TYPED.
224 * Return non-zero on success, zero on failure.
231 if (ef_check(type) < 0)
234 if (ep->flags & EFF_PRIVATE)
235 return 1; /* nothing to do */
236 if (CANT_HAPPEN(ep->fd < 0))
239 * We don't know which cache entries are dirty. ef_write() writes
240 * through, but direct updates through ef_ptr() don't. They are
241 * allowed only with EFF_MEM. Assume the whole cash is dirty
244 if (ep->flags & EFF_MEM) {
245 if (do_write(ep, ep->cache, ep->baseid, ep->cids) < 0)
253 * Return pointer to element ID in table TYPE if it exists, else NULL.
254 * The table must be fully cached, i.e. flags & EFF_MEM.
255 * The caller is responsible for flushing changes he makes.
258 ef_ptr(int type, int id)
262 if (ef_check(type) < 0)
265 if (CANT_HAPPEN(!(ep->flags & EFF_MEM) || !ep->cache))
267 if (id < 0 || id >= ep->fids)
269 return ep->cache + ep->size * id;
273 * Read element ID from table TYPE into buffer INTO.
274 * FIXME pass buffer size!
275 * Return non-zero on success, zero on failure.
278 ef_read(int type, int id, void *into)
283 if (ef_check(type) < 0)
286 if (CANT_HAPPEN(!ep->cache))
288 if (id < 0 || id >= ep->fids)
291 if (ep->flags & EFF_MEM) {
292 from = ep->cache + id * ep->size;
294 if (ep->baseid + ep->cids <= id || ep->baseid > id) {
295 if (fillcache(ep, id) < 1)
298 from = ep->cache + (id - ep->baseid) * ep->size;
300 memcpy(into, from, ep->size);
303 ep->postread(id, into);
308 * Fill cache of file-backed EP with elements starting at ID.
309 * If any were read, return their number.
310 * Else return -1 and leave the cache unchanged.
313 fillcache(struct empfile *ep, int id)
317 if (CANT_HAPPEN(!ep->cache))
320 ret = do_read(ep, ep->cache, id, MIN(ep->csize, ep->fids - id));
330 do_read(struct empfile *ep, void *buf, int id, int count)
335 if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
338 if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
339 logerror("Error seeking %s to elt %d (%s)",
340 ep->file, id, strerror(errno));
345 n = count * ep->size;
347 ret = read(ep->fd, p, n);
349 if (errno != EINTR) {
350 logerror("Error reading %s elt %d (%s)",
352 id + (int)((p - (char *)buf) / ep->size),
356 } else if (ret == 0) {
357 logerror("Unexpected EOF reading %s elt %d",
358 ep->file, id + (int)((p - (char *)buf) / ep->size));
366 return (p - (char *)buf) / ep->size;
370 * Write COUNT elements starting at ID from BUF to file-backed EP.
371 * Update the timestamp if the table is EFF_TYPED.
372 * Don't actually write if table is privately mapped.
373 * Return 0 on success, -1 on error (file may be corrupt then).
376 do_write(struct empfile *ep, void *buf, int id, int count)
380 struct emptypedstr *elt;
383 if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
386 if (ep->flags & EFF_TYPED) {
387 now = ep->flags & EFF_NOTIME ? (time_t)-1 : time(NULL);
388 for (i = 0; i < count; i++) {
390 * TODO Oopses here could be due to bad data corruption.
391 * Fail instead of attempting to recover?
393 elt = (struct emptypedstr *)((char *)buf + i * ep->size);
394 if (CANT_HAPPEN(elt->ef_type != ep->uid))
395 elt->ef_type = ep->uid;
396 if (CANT_HAPPEN(elt->uid != id + i))
398 if (now != (time_t)-1)
399 elt->timestamp = now;
403 if (ep->flags & EFF_PRIVATE)
406 if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
407 logerror("Error seeking %s to elt %d (%s)",
408 ep->file, id, strerror(errno));
413 n = count * ep->size;
415 ret = write(ep->fd, p, n);
417 if (errno != EINTR) {
418 logerror("Error writing %s elt %d (%s)",
420 id + (int)((p - (char *)buf) / ep->size),
434 * Write element ID into table TYPE from buffer FROM.
435 * FIXME pass buffer size!
436 * Update timestamp in FROM if table is EFF_TYPED.
437 * If table is file-backed and not privately mapped, write through
438 * cache straight to disk.
439 * Cannot write beyond the end of fully cached table (flags & EFF_MEM).
440 * Can write at the end of partially cached table.
441 * Return non-zero on success, zero on failure.
444 ef_write(int type, int id, void *from)
449 if (ef_check(type) < 0)
452 if (CANT_HAPPEN((ep->flags & (EFF_MEM | EFF_PRIVATE)) == EFF_PRIVATE))
455 ep->prewrite(id, from);
456 if (CANT_HAPPEN((ep->flags & EFF_MEM) ? id >= ep->fids : id > ep->fids))
457 return 0; /* not implemented */
460 if (do_write(ep, from, id, 1) < 0)
463 if (id >= ep->baseid && id < ep->baseid + ep->cids) {
464 /* update the cache if necessary */
465 to = ep->cache + (id - ep->baseid) * ep->size;
467 memcpy(to, from, ep->size);
469 if (id >= ep->fids) {
470 /* write beyond end of file extends it, take note */
478 * BUF is an element of table TYPE.
479 * ID is its new element ID.
480 * If table is EFF_TYPED, change id and sequence number stored in BUF.
484 ef_set_uid(int type, void *buf, int uid)
486 struct emptypedstr *elt;
489 if (ef_check(type) < 0)
492 if (!(ep->flags & EFF_TYPED))
498 elt->seqno = get_seqno(ep, uid);
502 * Return sequence number of element ID in table EP.
503 * Return zero if table is not EFF_TYPED (it has no sequence number
507 get_seqno(struct empfile *ep, int id)
509 struct emptypedstr *elt;
511 if (!(ep->flags & EFF_TYPED))
513 if (id < 0 || id >= ep->fids)
515 if (id >= ep->baseid && id < ep->baseid + ep->cids)
516 elt = (void *)(ep->cache + (id - ep->baseid) * ep->size);
518 /* need a buffer, steal last cache slot */
519 if (ep->cids == ep->csize)
521 elt = (void *)(ep->cache + ep->cids * ep->size);
522 if (do_read(ep, elt, id, 1) < 0)
523 return 0; /* deep trouble */
529 * Increment sequence number in BUF, which is about to be written to EP.
530 * Do nothing if table is not EFF_TYPED (it has no sequence number
534 new_seqno(struct empfile *ep, void *buf)
536 struct emptypedstr *elt = buf;
539 if (!(ep->flags & EFF_TYPED))
541 old_seqno = get_seqno(ep, elt->uid);
542 if (CANT_HAPPEN(old_seqno != elt->seqno))
543 old_seqno = MAX(old_seqno, elt->seqno);
544 elt->seqno = old_seqno + 1;
548 * Extend table TYPE by COUNT elements.
549 * Any pointers obtained from ef_ptr() become invalid.
550 * Return non-zero on success, zero on failure.
553 ef_extend(int type, int count)
557 int need_sentinel, i, id;
559 if (ef_check(type) < 0)
562 if (CANT_HAPPEN(count < 0))
566 if (ep->flags & EFF_MEM) {
567 need_sentinel = (ep->flags & EFF_SENTINEL) != 0;
568 if (id + count + need_sentinel > ep->csize) {
569 if (ep->flags & EFF_STATIC) {
570 logerror("Can't extend %s beyond %d elements",
571 ep->name, ep->csize - need_sentinel);
574 if (!ef_realloc_cache(ep, id + count + need_sentinel)) {
575 logerror("Can't extend %s to %d elements (%s)",
576 ep->name, id + count, strerror(errno));
580 p = ep->cache + id * ep->size;
581 do_blank(ep, p, id, count);
583 if (do_write(ep, p, id, count) < 0)
587 memset(ep->cache + (id + count) * ep->size, 0, ep->size);
588 ep->cids = id + count;
590 /* need a buffer, steal last cache slot */
591 if (ep->cids == ep->csize)
593 p = ep->cache + ep->cids * ep->size;
594 for (i = 0; i < count; i++) {
595 do_blank(ep, p, id + i, 1);
596 if (do_write(ep, p, id + i, 1) < 0)
600 ep->fids = id + count;
605 * Initialize element ID for EP in BUF.
606 * FIXME pass buffer size!
609 ef_blank(int type, int id, void *buf)
612 struct emptypedstr *elt;
614 if (ef_check(type) < 0)
617 do_blank(ep, buf, id, 1);
618 if (ep->flags & EFF_TYPED) {
620 elt->seqno = get_seqno(ep, elt->uid);
625 * Initialize COUNT elements of EP in BUF, starting with element ID.
628 do_blank(struct empfile *ep, void *buf, int id, int count)
631 struct emptypedstr *elt;
633 memset(buf, 0, count * ep->size);
634 if (ep->flags & EFF_TYPED) {
635 for (i = 0; i < count; i++) {
636 elt = (struct emptypedstr *)((char *)buf + i * ep->size);
637 elt->ef_type = ep->uid;
644 * Truncate table TYPE to COUNT elements.
645 * Any pointers obtained from ef_ptr() become invalid.
646 * Return non-zero on success, zero on failure.
649 ef_truncate(int type, int count)
654 if (ef_check(type) < 0)
657 if (CANT_HAPPEN(count < 0 || count > ep->fids))
660 if (ep->fd >= 0 && !(ep->flags & EFF_PRIVATE)) {
661 if (ftruncate(ep->fd, count * ep->size) < 0) {
662 logerror("Can't truncate %s to %d elements (%s)",
663 ep->file, count, strerror(errno));
669 if (ep->flags & EFF_MEM) {
670 need_sentinel = (ep->flags & EFF_SENTINEL) != 0;
671 if (!(ep->flags & EFF_STATIC)) {
672 if (!ef_realloc_cache(ep, count + need_sentinel)) {
673 logerror("Can't shrink %s cache after truncate (%s)",
674 ep->name, strerror(errno));
675 /* continue with unshrunk cache */
679 memset(ep->cache + count * ep->size, 0, ep->size);
682 if (ep->baseid >= count)
684 else if (ep->cids > count - ep->baseid)
685 ep->cids = count - ep->baseid;
694 return empfile[type].cadef;
700 return empfile[type].fids;
706 return empfile[type].flags;
712 if (empfile[type].fd <= 0)
714 return fdate(empfile[type].fd);
718 * Search for a table matching NAME, return its table type.
719 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
723 ef_byname(char *name)
725 return stmtch(name, empfile, offsetof(struct empfile, name),
730 * Search CHOICES[] for a table type matching NAME, return it.
731 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
733 * CHOICES[] must be terminated with a negative value.
736 ef_byname_from(char *name, int choices[])
742 for (p = choices; *p >= 0; p++) {
743 if (ef_check(*p) < 0)
745 switch (mineq(name, empfile[*p].name)) {
763 if (ef_check(type) < 0)
764 return "bad ef_type";
765 return empfile[type].name;
771 if (CANT_HAPPEN((unsigned)type >= EF_MAX))
777 * Ensure table contains element ID.
778 * If necessary, extend it in steps of COUNT elements.
779 * Return non-zero on success, zero on failure.
782 ef_ensure_space(int type, int id, int count)
784 if (ef_check(type) < 0)
788 while (id >= empfile[type].fids) {
789 if (!ef_extend(type, count))