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
39 #if defined(_WIN32) && !defined(__GNUC__)
44 #include <sys/types.h>
53 #include "prototypes.h"
55 static int fillcache(struct empfile *, int);
56 static int do_write(struct empfile *, void *, int, int);
59 * Open the file-backed table TYPE (EF_SECTOR, ...).
60 * HOW are flags to control operation. Naturally, immutable flags are
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)
69 int oflags, fd, fsiz, size;
71 if (ef_check(type) < 0)
73 if (CANT_HAPPEN(how & EFF_IMMUTABLE))
74 how &= ~EFF_IMMUTABLE;
78 if (CANT_HAPPEN(ep->fd >= 0))
84 oflags |= O_CREAT | O_TRUNC;
88 if ((fd = open(ep->file, oflags, 0660)) < 0) {
89 logerror("Can't open %s (%s)", ep->file, strerror(errno));
95 if (fsiz % ep->size) {
96 logerror("Can't open %s (file size not a multiple of record size %d)",
101 ep->fids = fsiz / ep->size;
104 if (ep->flags & EFF_STATIC) {
105 /* ep->cache already points to space for ep->csize elements */
107 if (ep->fids > ep->csize) {
108 logerror("Can't open %s: file larger than %d bytes",
109 ep->file, ep->fids * ep->size);
116 ep->csize = ep->fids;
118 ep->csize = blksize(fd) / ep->size;
119 /* 0 could lead to null cache, which confuses assertions */
122 size = ep->csize * ep->size;
123 if (CANT_HAPPEN(ep->cache))
125 ep->cache = malloc(size);
126 if (ep->cache == NULL) {
127 logerror("Can't open %s: out of memory", ep->file);
134 ep->flags = (ep->flags & EFF_IMMUTABLE) | (how & ~EFF_CREATE);
137 /* map file into cache */
138 if ((how & EFF_MEM) && ep->fids) {
139 if (fillcache(ep, 0) != ep->fids) {
140 ep->cids = 0; /* prevent cache flush */
141 ep->flags &= EFF_IMMUTABLE; /* maintain invariant */
148 * Could close fd if both EFF_RDONLY and EFF_MEM, but that doesn't
149 * happen, so don't bother.
156 * Close the file-backed table TYPE (EF_SECTOR, ...).
157 * Return non-zero on success, zero on failure.
165 retval = ef_flush(type);
167 ep->flags &= EFF_IMMUTABLE;
168 if (!(ep->flags & EFF_STATIC)) {
172 if (close(ep->fd) < 0) {
173 logerror("Error closing %s (%s)", ep->name, strerror(errno));
181 * Flush file-backed table TYPE (EF_SECTOR, ...) to disk.
182 * Return non-zero on success, zero on failure.
189 if (ef_check(type) < 0)
192 if (CANT_HAPPEN(ep->fd < 0))
195 * We don't know which cache entries are dirty. ef_write() writes
196 * through, but direct updates through ef_ptr() don't. They are
197 * allowed only with EFF_MEM. Assume the whole cash is dirty
200 if (!(ep->flags & EFF_RDONLY) && (ep->flags & EFF_MEM))
201 return do_write(ep, ep->cache, ep->baseid, ep->cids) >= 0;
207 * Return pointer to element ID in table TYPE if it exists, else NULL.
208 * The table must be fully cached, i.e. flags & EFF_MEM.
209 * The caller is responsible for flushing changes he makes.
212 ef_ptr(int type, int id)
216 if (ef_check(type) < 0)
219 if (CANT_HAPPEN(!(ep->flags & EFF_MEM) || !ep->cache))
221 if (id < 0 || id >= ep->fids)
223 return ep->cache + ep->size * id;
227 * Read element ID from table TYPE into buffer INTO.
228 * FIXME pass buffer size!
229 * Return non-zero on success, zero on failure.
232 ef_read(int type, int id, void *into)
237 if (ef_check(type) < 0)
240 if (CANT_HAPPEN(!ep->cache))
242 if (id < 0 || id >= ep->fids)
245 if (ep->flags & EFF_MEM) {
246 from = ep->cache + id * ep->size;
248 if (ep->baseid + ep->cids <= id || ep->baseid > id) {
249 if (fillcache(ep, id) < 1)
252 from = ep->cache + (id - ep->baseid) * ep->size;
254 memcpy(into, from, ep->size);
257 ep->postread(id, into);
262 * Fill cache of EP with elements starting at ID.
263 * If any were read, return their number.
264 * Else return -1 and leave the cache unchanged.
267 fillcache(struct empfile *ep, int start)
272 if (CANT_HAPPEN(ep->fd < 0 || !ep->cache))
275 if (lseek(ep->fd, start * ep->size, SEEK_SET) == (off_t)-1) {
276 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
281 n = ep->csize * ep->size;
283 ret = read(ep->fd, p, n);
285 if (errno != EAGAIN) {
286 logerror("Error reading %s (%s)", ep->file, strerror(errno));
289 } else if (ret == 0) {
298 return -1; /* nothing read, old cache still ok */
301 ep->cids = (p - ep->cache) / ep->size;
306 * Write COUNT elements from BUF to EP, starting at ID.
307 * Return 0 on success, -1 on error.
310 do_write(struct empfile *ep, void *buf, int id, int count)
315 if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
318 if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
319 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
324 n = count * ep->size;
326 ret = write(ep->fd, p, n);
328 if (errno != EAGAIN) {
329 logerror("Error writing %s (%s)", ep->file, strerror(errno));
330 /* FIXME if this extended file, truncate back to old size */
343 * Write element ID into file-backed table TYPE from buffer FROM.
344 * FIXME pass buffer size!
345 * Write through cache straight to disk.
346 * Cannot write beyond the end of fully cached table (flags & EFF_MEM).
347 * Can write at the end of partially cached table.
348 * Return non-zero on success, zero on failure.
351 ef_write(int type, int id, void *from)
356 if (ef_check(type) < 0)
360 ep->prewrite(id, from);
361 if (CANT_HAPPEN((ep->flags & EFF_MEM) ? id >= ep->fids : id > ep->fids))
362 return 0; /* not implemented */
363 if (do_write(ep, from, id, 1) < 0)
365 if (id >= ep->baseid && id < ep->baseid + ep->cids) {
366 /* update the cache if necessary */
367 to = ep->cache + (id - ep->baseid) * ep->size;
369 memcpy(to, from, ep->size);
371 if (id >= ep->fids) {
372 /* write beyond end of file extends it, take note */
379 * Extend the file-backed table TYPE by COUNT elements.
380 * Return non-zero on success, zero on failure.
383 ef_extend(int type, int count)
389 if (ef_check(type) < 0)
392 if (CANT_HAPPEN(ep->fd < 0 || count < 0))
395 tmpobj = calloc(1, ep->size);
397 for (i = 0; i < count; i++) {
399 ep->init(id + i, tmpobj);
400 if (do_write(ep, tmpobj, id + i, 1) < 0)
405 if (ep->flags & EFF_MEM) {
406 /* FIXME lazy bastards... do this right */
407 /* XXX this will cause problems if there are ef_ptrs (to the
408 * old allocated structure) active when we do the re-open */
409 how = ep->flags & ~EFF_IMMUTABLE;
422 return empfile[type].cadef;
428 return empfile[type].fids;
434 return empfile[type].flags;
440 if (empfile[type].fd <= 0)
442 return fdate(empfile[type].fd);
446 * Search for a table matching NAME, return its table type.
447 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
451 ef_byname(char *name)
453 return stmtch(name, empfile, offsetof(struct empfile, name),
458 * Search CHOICES[] for a table type matching NAME, return it.
459 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
461 * CHOICES[] must be terminated with a negative value.
464 ef_byname_from(char *name, int choices[])
470 for (p = choices; *p >= 0; p++) {
471 if (ef_check(*p) < 0)
473 switch (mineq(name, empfile[*p].name)) {
491 if (ef_check(type) < 0)
492 return "bad ef_type";
493 return empfile[type].name;
499 if (CANT_HAPPEN((unsigned)type >= EF_MAX))
505 * Ensure file-backed table contains ID.
506 * If necessary, extend it in steps of COUNT elements.
507 * Return non-zero on success, zero on failure.
510 ef_ensure_space(int type, int id, int count)
512 if (ef_check(type) < 0)
516 while (id >= empfile[type].fids) {
517 if (!ef_extend(type, count))
524 ef_fix_size(struct empfile *ep, int n)
526 ep->cids = ep->fids = n;
531 * Initialize Empire tables.
532 * Must be called once, before using anything else from this module.
542 empfile[EF_MAP].size = empfile[EF_BMAP].size = (WORLD_X * WORLD_Y) / 2;
544 ca = (struct castr *)empfile[EF_META].cache;
545 for (i = 0; ca[i].ca_name; i++) ;
546 ef_fix_size(&empfile[EF_META], i);
548 for (ep = empfile; ep->uid >= 0; ep++) {
549 if (ep->cadef == symbol_ca) {
550 lup = (struct symbol *)ep->cache;
551 for (i = 0; lup[i].name; i++) ;