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-2006
41 #include <sys/types.h>
48 #include "prototypes.h"
50 static int fillcache(struct empfile *, int);
51 static int do_write(struct empfile *, void *, int, int);
54 * Open the file-backed table TYPE (EF_SECTOR, ...).
55 * HOW are flags to control operation. Naturally, immutable flags are
57 * Return non-zero on success, zero on failure.
58 * You must call ef_close() before the next ef_open().
61 ef_open(int type, int how)
65 int oflags, fd, fsiz, size;
67 if (ef_check(type) < 0)
69 if (CANT_HAPPEN(how & EFF_IMMUTABLE))
70 how &= ~EFF_IMMUTABLE;
74 if (CANT_HAPPEN(ep->fd >= 0))
80 oflags |= O_CREAT | O_TRUNC;
84 if ((fd = open(ep->file, oflags, S_IRWUG)) < 0) {
85 logerror("Can't open %s (%s)", ep->file, strerror(errno));
89 lock.l_type = how & EFF_RDONLY ? F_RDLCK : F_WRLCK;
90 lock.l_whence = SEEK_SET;
91 lock.l_start = lock.l_len = 0;
92 if (fcntl(fd, F_SETLK, &lock) == -1) {
93 logerror("Can't lock %s (%s)", ep->file, strerror(errno));
100 if (fsiz % ep->size) {
101 logerror("Can't open %s (file size not a multiple of record size %d)",
106 ep->fids = fsiz / ep->size;
109 if (ep->flags & EFF_STATIC) {
110 /* ep->cache already points to space for ep->csize elements */
112 if (ep->fids > ep->csize) {
113 logerror("Can't open %s: file larger than %d bytes",
114 ep->file, ep->fids * ep->size);
121 ep->csize = ep->fids;
123 ep->csize = blksize(fd) / ep->size;
124 /* 0 could lead to null cache, which confuses assertions */
127 size = ep->csize * ep->size;
128 if (CANT_HAPPEN(ep->cache))
130 ep->cache = malloc(size);
131 if (ep->cache == NULL) {
132 logerror("Can't open %s: out of memory", ep->file);
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 */
153 * Could close fd if both EFF_RDONLY and EFF_MEM, but that doesn't
154 * happen, so don't bother.
161 * Close the file-backed table TYPE (EF_SECTOR, ...).
162 * Return non-zero on success, zero on failure.
170 retval = ef_flush(type);
172 ep->flags &= EFF_IMMUTABLE;
173 if (!(ep->flags & EFF_STATIC)) {
177 if (close(ep->fd) < 0) {
178 logerror("Error closing %s (%s)", ep->name, strerror(errno));
186 * Flush file-backed table TYPE (EF_SECTOR, ...) to disk.
187 * Return non-zero on success, zero on failure.
194 if (ef_check(type) < 0)
197 if (CANT_HAPPEN(ep->fd < 0))
200 * We don't know which cache entries are dirty. ef_write() writes
201 * through, but direct updates through ef_ptr() don't. They are
202 * allowed only with EFF_MEM. Assume the whole cash is dirty
205 if (!(ep->flags & EFF_RDONLY) && (ep->flags & EFF_MEM))
206 return do_write(ep, ep->cache, ep->baseid, ep->cids) >= 0;
212 * Return pointer to element ID in table TYPE if it exists, else NULL.
213 * The table must be fully cached, i.e. flags & EFF_MEM.
214 * The caller is responsible for flushing changes he makes.
217 ef_ptr(int type, int id)
221 if (ef_check(type) < 0)
224 if (CANT_HAPPEN(!(ep->flags & EFF_MEM) || !ep->cache))
226 if (id < 0 || id >= ep->fids)
228 return ep->cache + ep->size * id;
232 * Read element ID from table TYPE into buffer INTO.
233 * FIXME pass buffer size!
234 * Return non-zero on success, zero on failure.
237 ef_read(int type, int id, void *into)
242 if (ef_check(type) < 0)
245 if (CANT_HAPPEN(!ep->cache))
247 if (id < 0 || id >= ep->fids)
250 if (ep->flags & EFF_MEM) {
251 from = ep->cache + id * ep->size;
253 if (ep->baseid + ep->cids <= id || ep->baseid > id) {
254 if (fillcache(ep, id) < 1)
257 from = ep->cache + (id - ep->baseid) * ep->size;
259 memcpy(into, from, ep->size);
262 ep->postread(id, into);
267 * Fill cache of EP with elements starting at ID.
268 * If any were read, return their number.
269 * Else return -1 and leave the cache unchanged.
272 fillcache(struct empfile *ep, int start)
277 if (CANT_HAPPEN(ep->fd < 0 || !ep->cache))
280 if (lseek(ep->fd, start * ep->size, SEEK_SET) == (off_t)-1) {
281 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
286 n = ep->csize * ep->size;
288 ret = read(ep->fd, p, n);
290 if (errno != EAGAIN) {
291 logerror("Error reading %s (%s)", ep->file, strerror(errno));
294 } else if (ret == 0) {
303 return -1; /* nothing read, old cache still ok */
306 ep->cids = (p - ep->cache) / ep->size;
311 * Write COUNT elements from BUF to EP, starting at ID.
312 * Return 0 on success, -1 on error.
315 do_write(struct empfile *ep, void *buf, int id, int count)
320 if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
323 if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
324 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
329 n = count * ep->size;
331 ret = write(ep->fd, p, n);
333 if (errno != EAGAIN) {
334 logerror("Error writing %s (%s)", ep->file, strerror(errno));
335 /* FIXME if this extended file, truncate back to old size */
348 * Write element ID into file-backed table TYPE from buffer FROM.
349 * FIXME pass buffer size!
350 * Write through cache straight to disk.
351 * Cannot write beyond the end of fully cached table (flags & EFF_MEM).
352 * Can write at the end of partially cached table.
353 * Return non-zero on success, zero on failure.
356 ef_write(int type, int id, void *from)
361 if (ef_check(type) < 0)
365 ep->prewrite(id, from);
366 if (CANT_HAPPEN((ep->flags & EFF_MEM) ? id >= ep->fids : id > ep->fids))
367 return 0; /* not implemented */
368 if (do_write(ep, from, id, 1) < 0)
370 if (id >= ep->baseid && id < ep->baseid + ep->cids) {
371 /* update the cache if necessary */
372 to = ep->cache + (id - ep->baseid) * ep->size;
374 memcpy(to, from, ep->size);
376 if (id >= ep->fids) {
377 /* write beyond end of file extends it, take note */
384 * Extend the file-backed table TYPE by COUNT elements.
385 * Return non-zero on success, zero on failure.
388 ef_extend(int type, int count)
394 if (ef_check(type) < 0)
397 if (CANT_HAPPEN(ep->fd < 0 || count < 0))
400 tmpobj = calloc(1, ep->size);
402 for (i = 0; i < count; i++) {
404 ep->init(id + i, tmpobj);
405 if (do_write(ep, tmpobj, id + i, 1) < 0)
410 if (ep->flags & EFF_MEM) {
411 /* FIXME lazy bastards... do this right */
412 /* XXX this will cause problems if there are ef_ptrs (to the
413 * old allocated structure) active when we do the re-open */
414 how = ep->flags & ~EFF_IMMUTABLE;
427 return empfile[type].cadef;
433 return empfile[type].fids;
439 return empfile[type].flags;
445 if (empfile[type].fd <= 0)
447 return fdate(empfile[type].fd);
451 * Search for a table matching NAME, return its table type.
452 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
456 ef_byname(char *name)
458 return stmtch(name, empfile, offsetof(struct empfile, name),
463 * Search CHOICES[] for a table type matching NAME, return it.
464 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
466 * CHOICES[] must be terminated with a negative value.
469 ef_byname_from(char *name, int choices[])
475 for (p = choices; *p >= 0; p++) {
476 if (ef_check(*p) < 0)
478 switch (mineq(name, empfile[*p].name)) {
496 if (ef_check(type) < 0)
497 return "bad ef_type";
498 return empfile[type].name;
504 if (CANT_HAPPEN((unsigned)type >= EF_MAX))
510 * Ensure file-backed table contains ID.
511 * If necessary, extend it in steps of COUNT elements.
512 * Return non-zero on success, zero on failure.
515 ef_ensure_space(int type, int id, int count)
517 if (ef_check(type) < 0)
521 while (id >= empfile[type].fids) {
522 if (!ef_extend(type, count))
529 ef_fix_size(struct empfile *ep, int n)
531 ep->cids = ep->fids = n;
536 * Initialize Empire tables.
537 * Must be called once, before using anything else from this module.
547 empfile[EF_MAP].size = empfile[EF_BMAP].size = WORLD_SZ();
549 ca = (struct castr *)empfile[EF_META].cache;
550 for (i = 0; ca[i].ca_name; i++) ;
551 ef_fix_size(&empfile[EF_META], i);
553 for (ep = empfile; ep->uid >= 0; ep++) {
554 if (ep->cadef == symbol_ca) {
555 lup = (struct symbol *)ep->cache;
556 for (i = 0; lup[i].name; i++) ;