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)
70 int oflags, fd, fsiz, size;
72 if (ef_check(type) < 0)
74 if (CANT_HAPPEN(how & EFF_IMMUTABLE))
75 how &= ~EFF_IMMUTABLE;
79 if (CANT_HAPPEN(ep->fd >= 0))
85 oflags |= O_CREAT | O_TRUNC;
89 if ((fd = open(ep->file, oflags, 0660)) < 0) {
90 logerror("Can't open %s (%s)", ep->file, strerror(errno));
94 lock.l_type = how & EFF_RDONLY ? F_RDLCK : F_WRLCK;
95 lock.l_whence = SEEK_SET;
96 lock.l_start = lock.l_len = 0;
97 if (fcntl(fd, F_SETLK, &lock) == -1) {
98 logerror("Can't lock %s (%s)", ep->file, strerror(errno));
104 if (fsiz % ep->size) {
105 logerror("Can't open %s (file size not a multiple of record size %d)",
110 ep->fids = fsiz / ep->size;
113 if (ep->flags & EFF_STATIC) {
114 /* ep->cache already points to space for ep->csize elements */
116 if (ep->fids > ep->csize) {
117 logerror("Can't open %s: file larger than %d bytes",
118 ep->file, ep->fids * ep->size);
125 ep->csize = ep->fids;
127 ep->csize = blksize(fd) / ep->size;
128 /* 0 could lead to null cache, which confuses assertions */
131 size = ep->csize * ep->size;
132 if (CANT_HAPPEN(ep->cache))
134 ep->cache = malloc(size);
135 if (ep->cache == NULL) {
136 logerror("Can't open %s: out of memory", ep->file);
143 ep->flags = (ep->flags & EFF_IMMUTABLE) | (how & ~EFF_CREATE);
146 /* map file into cache */
147 if ((how & EFF_MEM) && ep->fids) {
148 if (fillcache(ep, 0) != ep->fids) {
149 ep->cids = 0; /* prevent cache flush */
150 ep->flags &= EFF_IMMUTABLE; /* maintain invariant */
157 * Could close fd if both EFF_RDONLY and EFF_MEM, but that doesn't
158 * happen, so don't bother.
165 * Close the file-backed table TYPE (EF_SECTOR, ...).
166 * Return non-zero on success, zero on failure.
174 retval = ef_flush(type);
176 ep->flags &= EFF_IMMUTABLE;
177 if (!(ep->flags & EFF_STATIC)) {
181 if (close(ep->fd) < 0) {
182 logerror("Error closing %s (%s)", ep->name, strerror(errno));
190 * Flush file-backed table TYPE (EF_SECTOR, ...) to disk.
191 * Return non-zero on success, zero on failure.
198 if (ef_check(type) < 0)
201 if (CANT_HAPPEN(ep->fd < 0))
204 * We don't know which cache entries are dirty. ef_write() writes
205 * through, but direct updates through ef_ptr() don't. They are
206 * allowed only with EFF_MEM. Assume the whole cash is dirty
209 if (!(ep->flags & EFF_RDONLY) && (ep->flags & EFF_MEM))
210 return do_write(ep, ep->cache, ep->baseid, ep->cids) >= 0;
216 * Return pointer to element ID in table TYPE if it exists, else NULL.
217 * The table must be fully cached, i.e. flags & EFF_MEM.
218 * The caller is responsible for flushing changes he makes.
221 ef_ptr(int type, int id)
225 if (ef_check(type) < 0)
228 if (CANT_HAPPEN(!(ep->flags & EFF_MEM) || !ep->cache))
230 if (id < 0 || id >= ep->fids)
232 return ep->cache + ep->size * id;
236 * Read element ID from table TYPE into buffer INTO.
237 * FIXME pass buffer size!
238 * Return non-zero on success, zero on failure.
241 ef_read(int type, int id, void *into)
246 if (ef_check(type) < 0)
249 if (CANT_HAPPEN(!ep->cache))
251 if (id < 0 || id >= ep->fids)
254 if (ep->flags & EFF_MEM) {
255 from = ep->cache + id * ep->size;
257 if (ep->baseid + ep->cids <= id || ep->baseid > id) {
258 if (fillcache(ep, id) < 1)
261 from = ep->cache + (id - ep->baseid) * ep->size;
263 memcpy(into, from, ep->size);
266 ep->postread(id, into);
271 * Fill cache of EP with elements starting at ID.
272 * If any were read, return their number.
273 * Else return -1 and leave the cache unchanged.
276 fillcache(struct empfile *ep, int start)
281 if (CANT_HAPPEN(ep->fd < 0 || !ep->cache))
284 if (lseek(ep->fd, start * ep->size, SEEK_SET) == (off_t)-1) {
285 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
290 n = ep->csize * ep->size;
292 ret = read(ep->fd, p, n);
294 if (errno != EAGAIN) {
295 logerror("Error reading %s (%s)", ep->file, strerror(errno));
298 } else if (ret == 0) {
307 return -1; /* nothing read, old cache still ok */
310 ep->cids = (p - ep->cache) / ep->size;
315 * Write COUNT elements from BUF to EP, starting at ID.
316 * Return 0 on success, -1 on error.
319 do_write(struct empfile *ep, void *buf, int id, int count)
324 if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
327 if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
328 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
333 n = count * ep->size;
335 ret = write(ep->fd, p, n);
337 if (errno != EAGAIN) {
338 logerror("Error writing %s (%s)", ep->file, strerror(errno));
339 /* FIXME if this extended file, truncate back to old size */
352 * Write element ID into file-backed table TYPE from buffer FROM.
353 * FIXME pass buffer size!
354 * Write through cache straight to disk.
355 * Cannot write beyond the end of fully cached table (flags & EFF_MEM).
356 * Can write at the end of partially cached table.
357 * Return non-zero on success, zero on failure.
360 ef_write(int type, int id, void *from)
365 if (ef_check(type) < 0)
369 ep->prewrite(id, from);
370 if (CANT_HAPPEN((ep->flags & EFF_MEM) ? id >= ep->fids : id > ep->fids))
371 return 0; /* not implemented */
372 if (do_write(ep, from, id, 1) < 0)
374 if (id >= ep->baseid && id < ep->baseid + ep->cids) {
375 /* update the cache if necessary */
376 to = ep->cache + (id - ep->baseid) * ep->size;
378 memcpy(to, from, ep->size);
380 if (id >= ep->fids) {
381 /* write beyond end of file extends it, take note */
388 * Extend the file-backed table TYPE by COUNT elements.
389 * Return non-zero on success, zero on failure.
392 ef_extend(int type, int count)
398 if (ef_check(type) < 0)
401 if (CANT_HAPPEN(ep->fd < 0 || count < 0))
404 tmpobj = calloc(1, ep->size);
406 for (i = 0; i < count; i++) {
408 ep->init(id + i, tmpobj);
409 if (do_write(ep, tmpobj, id + i, 1) < 0)
414 if (ep->flags & EFF_MEM) {
415 /* FIXME lazy bastards... do this right */
416 /* XXX this will cause problems if there are ef_ptrs (to the
417 * old allocated structure) active when we do the re-open */
418 how = ep->flags & ~EFF_IMMUTABLE;
431 return empfile[type].cadef;
437 return empfile[type].fids;
443 return empfile[type].flags;
449 if (empfile[type].fd <= 0)
451 return fdate(empfile[type].fd);
455 * Search for a table matching NAME, return its table type.
456 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
460 ef_byname(char *name)
462 return stmtch(name, empfile, offsetof(struct empfile, name),
467 * Search CHOICES[] for a table type matching NAME, return it.
468 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
470 * CHOICES[] must be terminated with a negative value.
473 ef_byname_from(char *name, int choices[])
479 for (p = choices; *p >= 0; p++) {
480 if (ef_check(*p) < 0)
482 switch (mineq(name, empfile[*p].name)) {
500 if (ef_check(type) < 0)
501 return "bad ef_type";
502 return empfile[type].name;
508 if (CANT_HAPPEN((unsigned)type >= EF_MAX))
514 * Ensure file-backed table contains ID.
515 * If necessary, extend it in steps of COUNT elements.
516 * Return non-zero on success, zero on failure.
519 ef_ensure_space(int type, int id, int count)
521 if (ef_check(type) < 0)
525 while (id >= empfile[type].fids) {
526 if (!ef_extend(type, count))
533 ef_fix_size(struct empfile *ep, int n)
535 ep->cids = ep->fids = n;
540 * Initialize Empire tables.
541 * Must be called once, before using anything else from this module.
551 empfile[EF_MAP].size = empfile[EF_BMAP].size = (WORLD_X * WORLD_Y) / 2;
553 ca = (struct castr *)empfile[EF_META].cache;
554 for (i = 0; ca[i].ca_name; i++) ;
555 ef_fix_size(&empfile[EF_META], i);
557 for (ep = empfile; ep->uid >= 0; ep++) {
558 if (ep->cadef == symbol_ca) {
559 lup = (struct symbol *)ep->cache;
560 for (i = 0; lup[i].name; i++) ;