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_write(struct empfile *, void *, int, int, time_t);
52 static void do_blank(struct empfile *, void *, int, int);
55 * Open the file-backed table TYPE (EF_SECTOR, ...).
56 * HOW are flags to control operation. Naturally, immutable flags are
58 * If NELT is non-negative, the table must have that many elements.
59 * Return non-zero on success, zero on failure.
60 * You must call ef_close() before the next ef_open().
63 ef_open(int type, int how, int nelt)
67 int oflags, fd, fsiz, nslots;
69 if (ef_check(type) < 0)
71 if (CANT_HAPPEN(how & EFF_IMMUTABLE))
72 how &= ~EFF_IMMUTABLE;
76 if (CANT_HAPPEN(ep->fd >= 0))
79 if (how & EFF_PRIVATE)
82 oflags |= O_CREAT | O_TRUNC;
86 if ((fd = open(ep->file, oflags, S_IRWUG)) < 0) {
87 logerror("Can't open %s (%s)", ep->file, strerror(errno));
91 lock.l_type = how & EFF_PRIVATE ? F_RDLCK : F_WRLCK;
92 lock.l_whence = SEEK_SET;
93 lock.l_start = lock.l_len = 0;
94 if (fcntl(fd, F_SETLK, &lock) == -1) {
95 logerror("Can't lock %s (%s)", ep->file, strerror(errno));
102 if (fsiz % ep->size) {
103 logerror("Can't open %s (file size not a multiple of record size %d)",
108 ep->fids = fsiz / ep->size;
109 if (nelt >= 0 && nelt != ep->fids) {
110 logerror("Can't open %s (got %d records instead of %d)",
111 ep->file, ep->fids, nelt);
117 if (ep->flags & EFF_STATIC) {
118 /* ep->cache already points to space for ep->csize elements */
120 if (ep->fids > ep->csize) {
121 logerror("Can't open %s: file larger than %d bytes",
122 ep->file, ep->fids * ep->size);
128 if (CANT_HAPPEN(ep->cache))
133 nslots = blksize(fd) / ep->size;
134 if (!ef_realloc_cache(ep, nslots)) {
135 logerror("Can't map %s (%s)", ep->file, strerror(errno));
142 ep->flags = (ep->flags & EFF_IMMUTABLE) | (how & ~EFF_CREATE);
145 /* map file into cache */
146 if ((how & EFF_MEM) && ep->fids) {
147 if (fillcache(ep, 0) != ep->fids) {
148 ep->cids = 0; /* prevent cache flush */
149 ep->flags &= EFF_IMMUTABLE; /* maintain invariant */
159 * Reallocate cache for table EP to hold COUNT slots.
160 * The table must not be allocated statically.
161 * The cache may still be unmapped.
162 * If reallocation succeeds, any pointers obtained from ef_ptr()
164 * If it fails, the cache is unchanged, and errno is set.
165 * Return non-zero on success, zero on failure.
168 ef_realloc_cache(struct empfile *ep, int count)
172 if (CANT_HAPPEN(ep->flags & EFF_STATIC))
174 if (CANT_HAPPEN(count < 0))
178 * Avoid zero slots, because that can lead to null cache, which
179 * would be interpreted as unmapped cache.
183 cache = realloc(ep->cache, count * ep->size);
193 * Close the file-backed table TYPE (EF_SECTOR, ...).
194 * Return non-zero on success, zero on failure.
202 retval = ef_flush(type);
204 ep->flags &= EFF_IMMUTABLE;
205 if (!(ep->flags & EFF_STATIC)) {
209 if (close(ep->fd) < 0) {
210 logerror("Error closing %s (%s)", ep->name, strerror(errno));
218 * Flush table TYPE (EF_SECTOR, ...) to disk.
219 * Does nothing if the table is privately mapped.
220 * Return non-zero on success, zero on failure.
227 if (ef_check(type) < 0)
230 if (ep->flags & EFF_PRIVATE)
231 return 1; /* nothing to do */
232 if (CANT_HAPPEN(ep->fd < 0))
235 * We don't know which cache entries are dirty. ef_write() writes
236 * through, but direct updates through ef_ptr() don't. They are
237 * allowed only with EFF_MEM. Assume the whole cash is dirty
240 if (ep->flags & EFF_MEM) {
241 if (do_write(ep, ep->cache, ep->baseid, ep->cids, time(NULL)) < 0)
249 * Return pointer to element ID in table TYPE if it exists, else NULL.
250 * The table must be fully cached, i.e. flags & EFF_MEM.
251 * The caller is responsible for flushing changes he makes.
254 ef_ptr(int type, int id)
258 if (ef_check(type) < 0)
261 if (CANT_HAPPEN(!(ep->flags & EFF_MEM) || !ep->cache))
263 if (id < 0 || id >= ep->fids)
265 return ep->cache + ep->size * id;
269 * Read element ID from table TYPE into buffer INTO.
270 * FIXME pass buffer size!
271 * Return non-zero on success, zero on failure.
274 ef_read(int type, int id, void *into)
279 if (ef_check(type) < 0)
282 if (CANT_HAPPEN(!ep->cache))
284 if (id < 0 || id >= ep->fids)
287 if (ep->flags & EFF_MEM) {
288 from = ep->cache + id * ep->size;
290 if (ep->baseid + ep->cids <= id || ep->baseid > id) {
291 if (fillcache(ep, id) < 1)
294 from = ep->cache + (id - ep->baseid) * ep->size;
296 memcpy(into, from, ep->size);
299 ep->postread(id, into);
304 * Fill cache of file-backed EP with elements starting at ID.
305 * If any were read, return their number.
306 * Else return -1 and leave the cache unchanged.
309 fillcache(struct empfile *ep, int id)
314 if (CANT_HAPPEN(ep->fd < 0 || !ep->cache))
317 if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
318 logerror("Error seeking %s to elt %d (%s)",
319 ep->file, id, strerror(errno));
324 n = MIN(ep->csize, ep->fids - id) * ep->size;
326 ret = read(ep->fd, p, n);
328 if (errno != EINTR) {
329 logerror("Error reading %s elt %d (%s)",
331 id + (int)((p - ep->cache) / ep->size),
335 } else if (ret == 0) {
336 logerror("Unexpected EOF reading %s elt %d",
337 ep->file, id + (int)((p - ep->cache) / ep->size));
346 return -1; /* nothing read, old cache still ok */
349 ep->cids = (p - ep->cache) / ep->size;
354 * Write COUNT elements starting at ID from BUF to file-backed EP.
355 * Set the timestamp to NOW if the table has those.
356 * Return 0 on success, -1 on error (file may be corrupt then).
359 do_write(struct empfile *ep, void *buf, int id, int count, time_t now)
363 struct emptypedstr *elt;
365 if (CANT_HAPPEN(ep->fd < 0 || (ep->flags & EFF_PRIVATE)
366 || id < 0 || count < 0))
369 if (ep->flags & EFF_TYPED) {
370 for (i = 0; i < count; i++) {
372 * TODO Oopses here could be due to bad data corruption.
373 * Fail instead of attempting to recover?
375 elt = (struct emptypedstr *)((char *)buf + i * ep->size);
376 if (CANT_HAPPEN(elt->ef_type != ep->uid))
377 elt->ef_type = ep->uid;
378 if (CANT_HAPPEN(elt->uid != id + i))
380 elt->timestamp = now;
384 if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
385 logerror("Error seeking %s to elt %d (%s)",
386 ep->file, id, strerror(errno));
391 n = count * ep->size;
393 ret = write(ep->fd, p, n);
395 if (errno != EINTR) {
396 logerror("Error writing %s elt %d (%s)",
398 id + (int)((p - (char *)buf) / ep->size),
412 * Write element ID into table TYPE from buffer FROM.
413 * FIXME pass buffer size!
414 * If table is file-backed and not privately mapped, write through
415 * cache straight to disk.
416 * Cannot write beyond the end of fully cached table (flags & EFF_MEM).
417 * Can write at the end of partially cached table.
418 * Return non-zero on success, zero on failure.
421 ef_write(int type, int id, void *from)
426 if (ef_check(type) < 0)
429 if (CANT_HAPPEN((ep->flags & (EFF_MEM | EFF_PRIVATE)) == EFF_PRIVATE))
432 ep->prewrite(id, from);
433 if (CANT_HAPPEN((ep->flags & EFF_MEM) ? id >= ep->fids : id > ep->fids))
434 return 0; /* not implemented */
435 if (!(ep->flags & EFF_PRIVATE)) {
436 if (do_write(ep, from, id, 1, time(NULL)) < 0)
439 if (id >= ep->baseid && id < ep->baseid + ep->cids) {
440 /* update the cache if necessary */
441 to = ep->cache + (id - ep->baseid) * ep->size;
443 memcpy(to, from, ep->size);
445 if (id >= ep->fids) {
446 /* write beyond end of file extends it, take note */
453 * Extend table TYPE by COUNT elements.
454 * Any pointers obtained from ef_ptr() become invalid.
455 * Return non-zero on success, zero on failure.
458 ef_extend(int type, int count)
463 time_t now = time(NULL);
465 if (ef_check(type) < 0)
468 if (CANT_HAPPEN(count < 0))
472 if (ep->flags & EFF_MEM) {
473 if (id + count > ep->csize) {
474 if (ep->flags & EFF_STATIC) {
475 logerror("Can't extend %s beyond %d elements",
476 ep->file, ep->csize);
479 if (!ef_realloc_cache(ep, id + count)) {
480 logerror("Can't extend %s to %d elements (%s)",
481 ep->file, id + count, strerror(errno));
485 p = ep->cache + id * ep->size;
486 do_blank(ep, p, id, count);
487 if (ep->fd >= 0 && !(ep->flags & EFF_PRIVATE)) {
488 if (do_write(ep, p, id, count, now) < 0)
493 /* need a buffer, steal last cache slot */
494 if (ep->cids == ep->csize)
496 p = ep->cache + ep->cids * ep->size;
497 for (i = 0; i < count; i++) {
498 do_blank(ep, p, id + i, 1);
499 if (do_write(ep, p, id + i, 1, now) < 0)
508 * Initialize element ID for EP in BUF.
509 * FIXME pass buffer size!
512 ef_blank(int type, int id, void *buf)
514 if (ef_check(type) < 0)
516 do_blank(&empfile[type], buf, id, 1);
520 * Initialize COUNT elements of EP in BUF, starting with element ID.
523 do_blank(struct empfile *ep, void *buf, int id, int count)
526 struct emptypedstr *elt;
528 memset(buf, 0, count * ep->size);
529 if (ep->flags & EFF_TYPED) {
530 for (i = 0; i < count; i++) {
531 elt = (struct emptypedstr *)((char *)buf + i * ep->size);
532 elt->ef_type = ep->uid;
539 * Truncate table TYPE to COUNT elements.
540 * Any pointers obtained from ef_ptr() become invalid.
541 * Return non-zero on success, zero on failure.
544 ef_truncate(int type, int count)
548 if (ef_check(type) < 0)
551 if (CANT_HAPPEN(count < 0 || count > ep->fids))
554 if (ep->fd >= 0 && !(ep->flags & EFF_PRIVATE)) {
555 if (ftruncate(ep->fd, count * ep->size) < 0) {
556 logerror("Can't truncate %s to %d elements (%s)",
557 ep->file, count, strerror(errno));
563 if (ep->flags & EFF_MEM) {
564 if (!(ep->flags & EFF_STATIC)) {
565 if (!ef_realloc_cache(ep, count)) {
566 logerror("Can't shrink cache after truncate");
567 /* continue with unshrunk cache */
572 if (ep->baseid >= count)
574 else if (ep->cids > count - ep->baseid)
575 ep->cids = count - ep->baseid;
584 return empfile[type].cadef;
590 return empfile[type].fids;
596 return empfile[type].flags;
602 if (empfile[type].fd <= 0)
604 return fdate(empfile[type].fd);
608 * Search for a table matching NAME, return its table type.
609 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
613 ef_byname(char *name)
615 return stmtch(name, empfile, offsetof(struct empfile, name),
620 * Search CHOICES[] for a table type matching NAME, return it.
621 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
623 * CHOICES[] must be terminated with a negative value.
626 ef_byname_from(char *name, int choices[])
632 for (p = choices; *p >= 0; p++) {
633 if (ef_check(*p) < 0)
635 switch (mineq(name, empfile[*p].name)) {
653 if (ef_check(type) < 0)
654 return "bad ef_type";
655 return empfile[type].name;
661 if (CANT_HAPPEN((unsigned)type >= EF_MAX))
667 * Ensure file-backed table contains ID.
668 * If necessary, extend it in steps of COUNT elements.
669 * Return non-zero on success, zero on failure.
672 ef_ensure_space(int type, int id, int count)
674 if (ef_check(type) < 0)
678 while (id >= empfile[type].fids) {
679 if (!ef_extend(type, count))