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__)
45 #include <sys/types.h>
54 #include "prototypes.h"
56 static int fillcache(struct empfile *, int);
57 static int do_write(struct empfile *, void *, int, int);
60 * Open the file-backed table TYPE (EF_SECTOR, ...).
61 * HOW are flags to control operation. Naturally, immutable flags are
63 * Return non-zero on success, zero on failure.
64 * You must call ef_close() before the next ef_open().
67 ef_open(int type, int how)
73 int oflags, fd, fsiz, size;
75 if (ef_check(type) < 0)
77 if (CANT_HAPPEN(how & EFF_IMMUTABLE))
78 how &= ~EFF_IMMUTABLE;
82 if (CANT_HAPPEN(ep->fd >= 0))
88 oflags |= O_CREAT | O_TRUNC;
91 if ((fd = sopen(ep->file, oflags,
92 how & EFF_RDONLY ? SH_DENYWR : SH_DENYRW,
94 logerror("Can't open %s (%s)", ep->file, strerror(errno));
98 if ((fd = open(ep->file, oflags, 0660)) < 0) {
99 logerror("Can't open %s (%s)", ep->file, strerror(errno));
103 lock.l_type = how & EFF_RDONLY ? F_RDLCK : F_WRLCK;
104 lock.l_whence = SEEK_SET;
105 lock.l_start = lock.l_len = 0;
106 if (fcntl(fd, F_SETLK, &lock) == -1) {
107 logerror("Can't lock %s (%s)", ep->file, strerror(errno));
114 if (fsiz % ep->size) {
115 logerror("Can't open %s (file size not a multiple of record size %d)",
120 ep->fids = fsiz / ep->size;
123 if (ep->flags & EFF_STATIC) {
124 /* ep->cache already points to space for ep->csize elements */
126 if (ep->fids > ep->csize) {
127 logerror("Can't open %s: file larger than %d bytes",
128 ep->file, ep->fids * ep->size);
135 ep->csize = ep->fids;
137 ep->csize = blksize(fd) / ep->size;
138 /* 0 could lead to null cache, which confuses assertions */
141 size = ep->csize * ep->size;
142 if (CANT_HAPPEN(ep->cache))
144 ep->cache = malloc(size);
145 if (ep->cache == NULL) {
146 logerror("Can't open %s: out of memory", ep->file);
153 ep->flags = (ep->flags & EFF_IMMUTABLE) | (how & ~EFF_CREATE);
156 /* map file into cache */
157 if ((how & EFF_MEM) && ep->fids) {
158 if (fillcache(ep, 0) != ep->fids) {
159 ep->cids = 0; /* prevent cache flush */
160 ep->flags &= EFF_IMMUTABLE; /* maintain invariant */
167 * Could close fd if both EFF_RDONLY and EFF_MEM, but that doesn't
168 * happen, so don't bother.
175 * Close the file-backed table TYPE (EF_SECTOR, ...).
176 * Return non-zero on success, zero on failure.
184 retval = ef_flush(type);
186 ep->flags &= EFF_IMMUTABLE;
187 if (!(ep->flags & EFF_STATIC)) {
191 if (close(ep->fd) < 0) {
192 logerror("Error closing %s (%s)", ep->name, strerror(errno));
200 * Flush file-backed table TYPE (EF_SECTOR, ...) to disk.
201 * Return non-zero on success, zero on failure.
208 if (ef_check(type) < 0)
211 if (CANT_HAPPEN(ep->fd < 0))
214 * We don't know which cache entries are dirty. ef_write() writes
215 * through, but direct updates through ef_ptr() don't. They are
216 * allowed only with EFF_MEM. Assume the whole cash is dirty
219 if (!(ep->flags & EFF_RDONLY) && (ep->flags & EFF_MEM))
220 return do_write(ep, ep->cache, ep->baseid, ep->cids) >= 0;
226 * Return pointer to element ID in table TYPE if it exists, else NULL.
227 * The table must be fully cached, i.e. flags & EFF_MEM.
228 * The caller is responsible for flushing changes he makes.
231 ef_ptr(int type, int id)
235 if (ef_check(type) < 0)
238 if (CANT_HAPPEN(!(ep->flags & EFF_MEM) || !ep->cache))
240 if (id < 0 || id >= ep->fids)
242 return ep->cache + ep->size * id;
246 * Read element ID from table TYPE into buffer INTO.
247 * FIXME pass buffer size!
248 * Return non-zero on success, zero on failure.
251 ef_read(int type, int id, void *into)
256 if (ef_check(type) < 0)
259 if (CANT_HAPPEN(!ep->cache))
261 if (id < 0 || id >= ep->fids)
264 if (ep->flags & EFF_MEM) {
265 from = ep->cache + id * ep->size;
267 if (ep->baseid + ep->cids <= id || ep->baseid > id) {
268 if (fillcache(ep, id) < 1)
271 from = ep->cache + (id - ep->baseid) * ep->size;
273 memcpy(into, from, ep->size);
276 ep->postread(id, into);
281 * Fill cache of EP with elements starting at ID.
282 * If any were read, return their number.
283 * Else return -1 and leave the cache unchanged.
286 fillcache(struct empfile *ep, int start)
291 if (CANT_HAPPEN(ep->fd < 0 || !ep->cache))
294 if (lseek(ep->fd, start * ep->size, SEEK_SET) == (off_t)-1) {
295 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
300 n = ep->csize * ep->size;
302 ret = read(ep->fd, p, n);
304 if (errno != EAGAIN) {
305 logerror("Error reading %s (%s)", ep->file, strerror(errno));
308 } else if (ret == 0) {
317 return -1; /* nothing read, old cache still ok */
320 ep->cids = (p - ep->cache) / ep->size;
325 * Write COUNT elements from BUF to EP, starting at ID.
326 * Return 0 on success, -1 on error.
329 do_write(struct empfile *ep, void *buf, int id, int count)
334 if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
337 if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
338 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
343 n = count * ep->size;
345 ret = write(ep->fd, p, n);
347 if (errno != EAGAIN) {
348 logerror("Error writing %s (%s)", ep->file, strerror(errno));
349 /* FIXME if this extended file, truncate back to old size */
362 * Write element ID into file-backed table TYPE from buffer FROM.
363 * FIXME pass buffer size!
364 * Write through cache straight to disk.
365 * Cannot write beyond the end of fully cached table (flags & EFF_MEM).
366 * Can write at the end of partially cached table.
367 * Return non-zero on success, zero on failure.
370 ef_write(int type, int id, void *from)
375 if (ef_check(type) < 0)
379 ep->prewrite(id, from);
380 if (CANT_HAPPEN((ep->flags & EFF_MEM) ? id >= ep->fids : id > ep->fids))
381 return 0; /* not implemented */
382 if (do_write(ep, from, id, 1) < 0)
384 if (id >= ep->baseid && id < ep->baseid + ep->cids) {
385 /* update the cache if necessary */
386 to = ep->cache + (id - ep->baseid) * ep->size;
388 memcpy(to, from, ep->size);
390 if (id >= ep->fids) {
391 /* write beyond end of file extends it, take note */
398 * Extend the file-backed table TYPE by COUNT elements.
399 * Return non-zero on success, zero on failure.
402 ef_extend(int type, int count)
408 if (ef_check(type) < 0)
411 if (CANT_HAPPEN(ep->fd < 0 || count < 0))
414 tmpobj = calloc(1, ep->size);
416 for (i = 0; i < count; i++) {
418 ep->init(id + i, tmpobj);
419 if (do_write(ep, tmpobj, id + i, 1) < 0)
424 if (ep->flags & EFF_MEM) {
425 /* FIXME lazy bastards... do this right */
426 /* XXX this will cause problems if there are ef_ptrs (to the
427 * old allocated structure) active when we do the re-open */
428 how = ep->flags & ~EFF_IMMUTABLE;
441 return empfile[type].cadef;
447 return empfile[type].fids;
453 return empfile[type].flags;
459 if (empfile[type].fd <= 0)
461 return fdate(empfile[type].fd);
465 * Search for a table matching NAME, return its table type.
466 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
470 ef_byname(char *name)
472 return stmtch(name, empfile, offsetof(struct empfile, name),
477 * Search CHOICES[] for a table type matching NAME, return it.
478 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
480 * CHOICES[] must be terminated with a negative value.
483 ef_byname_from(char *name, int choices[])
489 for (p = choices; *p >= 0; p++) {
490 if (ef_check(*p) < 0)
492 switch (mineq(name, empfile[*p].name)) {
510 if (ef_check(type) < 0)
511 return "bad ef_type";
512 return empfile[type].name;
518 if (CANT_HAPPEN((unsigned)type >= EF_MAX))
524 * Ensure file-backed table contains ID.
525 * If necessary, extend it in steps of COUNT elements.
526 * Return non-zero on success, zero on failure.
529 ef_ensure_space(int type, int id, int count)
531 if (ef_check(type) < 0)
535 while (id >= empfile[type].fids) {
536 if (!ef_extend(type, count))
543 ef_fix_size(struct empfile *ep, int n)
545 ep->cids = ep->fids = n;
550 * Initialize Empire tables.
551 * Must be called once, before using anything else from this module.
561 empfile[EF_MAP].size = empfile[EF_BMAP].size = (WORLD_X * WORLD_Y) / 2;
563 ca = (struct castr *)empfile[EF_META].cache;
564 for (i = 0; ca[i].ca_name; i++) ;
565 ef_fix_size(&empfile[EF_META], i);
567 for (ep = empfile; ep->uid >= 0; ep++) {
568 if (ep->cadef == symbol_ca) {
569 lup = (struct symbol *)ep->cache;
570 for (i = 0; lup[i].name; i++) ;