2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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
40 #if !defined(__GNUC__)
47 #include <sys/types.h>
56 #include "prototypes.h"
58 static int fillcache(struct empfile *, int);
59 static int do_write(struct empfile *, void *, int, int);
62 * Open the file-backed table TYPE (EF_SECTOR, ...).
63 * HOW are flags to control operation. Naturally, immutable flags are
65 * Return non-zero on success, zero on failure.
66 * You must call ef_close() before the next ef_open().
69 ef_open(int type, int how)
75 int oflags, fd, fsiz, size;
77 if (ef_check(type) < 0)
79 if (CANT_HAPPEN(how & EFF_IMMUTABLE))
80 how &= ~EFF_IMMUTABLE;
84 if (CANT_HAPPEN(ep->fd >= 0))
90 oflags |= O_CREAT | O_TRUNC;
93 if ((fd = sopen(ep->file, oflags,
94 how & EFF_RDONLY ? SH_DENYNO : SH_DENYWR,
96 logerror("Can't open %s (%s)", ep->file, strerror(errno));
100 if ((fd = open(ep->file, oflags, 0660)) < 0) {
101 logerror("Can't open %s (%s)", ep->file, strerror(errno));
105 lock.l_type = how & EFF_RDONLY ? F_RDLCK : F_WRLCK;
106 lock.l_whence = SEEK_SET;
107 lock.l_start = lock.l_len = 0;
108 if (fcntl(fd, F_SETLK, &lock) == -1) {
109 logerror("Can't lock %s (%s)", ep->file, strerror(errno));
116 if (fsiz % ep->size) {
117 logerror("Can't open %s (file size not a multiple of record size %d)",
122 ep->fids = fsiz / ep->size;
125 if (ep->flags & EFF_STATIC) {
126 /* ep->cache already points to space for ep->csize elements */
128 if (ep->fids > ep->csize) {
129 logerror("Can't open %s: file larger than %d bytes",
130 ep->file, ep->fids * ep->size);
137 ep->csize = ep->fids;
139 ep->csize = blksize(fd) / ep->size;
140 /* 0 could lead to null cache, which confuses assertions */
143 size = ep->csize * ep->size;
144 if (CANT_HAPPEN(ep->cache))
146 ep->cache = malloc(size);
147 if (ep->cache == NULL) {
148 logerror("Can't open %s: out of memory", ep->file);
155 ep->flags = (ep->flags & EFF_IMMUTABLE) | (how & ~EFF_CREATE);
158 /* map file into cache */
159 if ((how & EFF_MEM) && ep->fids) {
160 if (fillcache(ep, 0) != ep->fids) {
161 ep->cids = 0; /* prevent cache flush */
162 ep->flags &= EFF_IMMUTABLE; /* maintain invariant */
169 * Could close fd if both EFF_RDONLY and EFF_MEM, but that doesn't
170 * happen, so don't bother.
177 * Close the file-backed table TYPE (EF_SECTOR, ...).
178 * Return non-zero on success, zero on failure.
186 retval = ef_flush(type);
188 ep->flags &= EFF_IMMUTABLE;
189 if (!(ep->flags & EFF_STATIC)) {
193 if (close(ep->fd) < 0) {
194 logerror("Error closing %s (%s)", ep->name, strerror(errno));
202 * Flush file-backed table TYPE (EF_SECTOR, ...) to disk.
203 * Return non-zero on success, zero on failure.
210 if (ef_check(type) < 0)
213 if (CANT_HAPPEN(ep->fd < 0))
216 * We don't know which cache entries are dirty. ef_write() writes
217 * through, but direct updates through ef_ptr() don't. They are
218 * allowed only with EFF_MEM. Assume the whole cash is dirty
221 if (!(ep->flags & EFF_RDONLY) && (ep->flags & EFF_MEM))
222 return do_write(ep, ep->cache, ep->baseid, ep->cids) >= 0;
228 * Return pointer to element ID in table TYPE if it exists, else NULL.
229 * The table must be fully cached, i.e. flags & EFF_MEM.
230 * The caller is responsible for flushing changes he makes.
233 ef_ptr(int type, int id)
237 if (ef_check(type) < 0)
240 if (CANT_HAPPEN(!(ep->flags & EFF_MEM) || !ep->cache))
242 if (id < 0 || id >= ep->fids)
244 return ep->cache + ep->size * id;
248 * Read element ID from table TYPE into buffer INTO.
249 * FIXME pass buffer size!
250 * Return non-zero on success, zero on failure.
253 ef_read(int type, int id, void *into)
258 if (ef_check(type) < 0)
261 if (CANT_HAPPEN(!ep->cache))
263 if (id < 0 || id >= ep->fids)
266 if (ep->flags & EFF_MEM) {
267 from = ep->cache + id * ep->size;
269 if (ep->baseid + ep->cids <= id || ep->baseid > id) {
270 if (fillcache(ep, id) < 1)
273 from = ep->cache + (id - ep->baseid) * ep->size;
275 memcpy(into, from, ep->size);
278 ep->postread(id, into);
283 * Fill cache of EP with elements starting at ID.
284 * If any were read, return their number.
285 * Else return -1 and leave the cache unchanged.
288 fillcache(struct empfile *ep, int start)
293 if (CANT_HAPPEN(ep->fd < 0 || !ep->cache))
296 if (lseek(ep->fd, start * ep->size, SEEK_SET) == (off_t)-1) {
297 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
302 n = ep->csize * ep->size;
304 ret = read(ep->fd, p, n);
306 if (errno != EAGAIN) {
307 logerror("Error reading %s (%s)", ep->file, strerror(errno));
310 } else if (ret == 0) {
319 return -1; /* nothing read, old cache still ok */
322 ep->cids = (p - ep->cache) / ep->size;
327 * Write COUNT elements from BUF to EP, starting at ID.
328 * Return 0 on success, -1 on error.
331 do_write(struct empfile *ep, void *buf, int id, int count)
336 if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
339 if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
340 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
345 n = count * ep->size;
347 ret = write(ep->fd, p, n);
349 if (errno != EAGAIN) {
350 logerror("Error writing %s (%s)", ep->file, strerror(errno));
351 /* FIXME if this extended file, truncate back to old size */
364 * Write element ID into file-backed table TYPE from buffer FROM.
365 * FIXME pass buffer size!
366 * Write through cache straight to disk.
367 * Cannot write beyond the end of fully cached table (flags & EFF_MEM).
368 * Can write at the end of partially cached table.
369 * Return non-zero on success, zero on failure.
372 ef_write(int type, int id, void *from)
377 if (ef_check(type) < 0)
381 ep->prewrite(id, from);
382 if (CANT_HAPPEN((ep->flags & EFF_MEM) ? id >= ep->fids : id > ep->fids))
383 return 0; /* not implemented */
384 if (do_write(ep, from, id, 1) < 0)
386 if (id >= ep->baseid && id < ep->baseid + ep->cids) {
387 /* update the cache if necessary */
388 to = ep->cache + (id - ep->baseid) * ep->size;
390 memcpy(to, from, ep->size);
392 if (id >= ep->fids) {
393 /* write beyond end of file extends it, take note */
400 * Extend the file-backed table TYPE by COUNT elements.
401 * Return non-zero on success, zero on failure.
404 ef_extend(int type, int count)
410 if (ef_check(type) < 0)
413 if (CANT_HAPPEN(ep->fd < 0 || count < 0))
416 tmpobj = calloc(1, ep->size);
418 for (i = 0; i < count; i++) {
420 ep->init(id + i, tmpobj);
421 if (do_write(ep, tmpobj, id + i, 1) < 0)
426 if (ep->flags & EFF_MEM) {
427 /* FIXME lazy bastards... do this right */
428 /* XXX this will cause problems if there are ef_ptrs (to the
429 * old allocated structure) active when we do the re-open */
430 how = ep->flags & ~EFF_IMMUTABLE;
443 return empfile[type].cadef;
449 return empfile[type].fids;
455 return empfile[type].flags;
461 if (empfile[type].fd <= 0)
463 return fdate(empfile[type].fd);
467 * Search for a table matching NAME, return its table type.
468 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
472 ef_byname(char *name)
474 return stmtch(name, empfile, offsetof(struct empfile, name),
479 * Search CHOICES[] for a table type matching NAME, return it.
480 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
482 * CHOICES[] must be terminated with a negative value.
485 ef_byname_from(char *name, int choices[])
491 for (p = choices; *p >= 0; p++) {
492 if (ef_check(*p) < 0)
494 switch (mineq(name, empfile[*p].name)) {
512 if (ef_check(type) < 0)
513 return "bad ef_type";
514 return empfile[type].name;
520 if (CANT_HAPPEN((unsigned)type >= EF_MAX))
526 * Ensure file-backed table contains ID.
527 * If necessary, extend it in steps of COUNT elements.
528 * Return non-zero on success, zero on failure.
531 ef_ensure_space(int type, int id, int count)
533 if (ef_check(type) < 0)
537 while (id >= empfile[type].fids) {
538 if (!ef_extend(type, count))
545 ef_fix_size(struct empfile *ep, int n)
547 ep->cids = ep->fids = n;
552 * Initialize Empire tables.
553 * Must be called once, before using anything else from this module.
563 empfile[EF_MAP].size = empfile[EF_BMAP].size = (WORLD_X * WORLD_Y) / 2;
565 ca = (struct castr *)empfile[EF_META].cache;
566 for (i = 0; ca[i].ca_name; i++) ;
567 ef_fix_size(&empfile[EF_META], i);
569 for (ep = empfile; ep->uid >= 0; ep++) {
570 if (ep->cadef == symbol_ca) {
571 lup = (struct symbol *)ep->cache;
572 for (i = 0; lup[i].name; i++) ;