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
54 static int fillcache(struct empfile *, int);
55 static int do_write(struct empfile *, void *, int, int);
58 * Open the file-backed table TYPE (EF_SECTOR, ...).
59 * HOW are flags to control operation. Naturally, immutable flags are
61 * Return non-zero on success, zero on failure.
62 * You must call ef_close() before the next ef_open().
65 ef_open(int type, int how)
68 int oflags, fd, fsiz, size;
70 if (ef_check(type) < 0)
72 if (CANT_HAPPEN(how & EFF_IMMUTABLE))
73 how &= ~EFF_IMMUTABLE;
77 if (CANT_HAPPEN(ep->fd >= 0))
83 oflags |= O_CREAT | O_TRUNC;
87 if ((fd = open(ep->file, oflags, 0660)) < 0) {
88 logerror("Can't open %s (%s)", ep->file, strerror(errno));
94 if (fsiz % ep->size) {
95 logerror("Can't open %s (file size not a multiple of record size %d)",
100 ep->fids = fsiz / ep->size;
103 if (ep->flags & EFF_STATIC) {
104 /* ep->cache already points to space for ep->csize elements */
106 if (ep->fids > ep->csize) {
107 logerror("Can't open %s: file larger than %d bytes",
108 ep->file, ep->fids * ep->size);
115 ep->csize = ep->fids;
117 ep->csize = MAX(1, blksize(fd) / ep->size);
118 size = ep->csize * ep->size;
119 if (CANT_HAPPEN(ep->cache))
121 ep->cache = malloc(size);
122 if (ep->cache == NULL && size) {
123 logerror("Can't open %s: out of memory", ep->file);
130 ep->flags = (ep->flags & EFF_IMMUTABLE) | (how & ~EFF_CREATE);
133 /* map file into cache */
134 if ((how & EFF_MEM) && ep->fids) {
135 if (fillcache(ep, 0) != ep->fids) {
136 ep->cids = 0; /* prevent cache flush */
137 ep->flags &= EFF_IMMUTABLE; /* maintain invariant */
144 * Could close fd if both EFF_RDONLY and EFF_MEM, but that doesn't
145 * happen, so don't bother.
152 * Close the file-backed table TYPE (EF_SECTOR, ...).
153 * Return non-zero on success, zero on failure.
161 retval = ef_flush(type);
163 ep->flags &= EFF_IMMUTABLE;
164 if (!(ep->flags & EFF_STATIC)) {
168 if (close(ep->fd) < 0) {
169 logerror("Error closing %s (%s)", ep->name, strerror(errno));
177 * Flush file-backed table TYPE (EF_SECTOR, ...) to disk.
178 * Return non-zero on success, zero on failure.
185 if (ef_check(type) < 0)
188 if (CANT_HAPPEN(ep->fd < 0))
191 * We don't know which cache entries are dirty. ef_write() writes
192 * through, but direct updates through ef_ptr() don't. They are
193 * allowed only with EFF_MEM. Assume the whole cash is dirty
196 if (!(ep->flags & EFF_RDONLY) && (ep->flags & EFF_MEM))
197 return do_write(ep, ep->cache, ep->baseid, ep->cids) >= 0;
203 * Return pointer to element ID in table TYPE if it exists, else NULL.
204 * The table must be fully cached, i.e. flags & EFF_MEM.
205 * The caller is responsible for flushing changes he makes.
208 ef_ptr(int type, int id)
212 if (ef_check(type) < 0)
215 if (CANT_HAPPEN(!(ep->flags & EFF_MEM) || !ep->cache))
217 if (id < 0 || id >= ep->fids)
219 return ep->cache + ep->size * id;
223 * Read element ID from table TYPE into buffer INTO.
224 * FIXME pass buffer size!
225 * Return non-zero on success, zero on failure.
228 ef_read(int type, int id, void *into)
233 if (ef_check(type) < 0)
236 if (CANT_HAPPEN(!ep->cache))
238 if (id < 0 || id >= ep->fids)
241 if (ep->flags & EFF_MEM) {
242 from = ep->cache + id * ep->size;
244 if (ep->baseid + ep->cids <= id || ep->baseid > id) {
245 if (fillcache(ep, id) < 1)
248 from = ep->cache + (id - ep->baseid) * ep->size;
250 memcpy(into, from, ep->size);
253 ep->postread(id, into);
258 * Fill cache of EP with elements starting at ID.
259 * If any were read, return their number.
260 * Else return -1 and leave the cache unchanged.
263 fillcache(struct empfile *ep, int start)
268 if (CANT_HAPPEN(ep->fd < 0 || !ep->cache))
271 if (lseek(ep->fd, start * ep->size, SEEK_SET) == (off_t)-1) {
272 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
277 n = ep->csize * ep->size;
279 ret = read(ep->fd, p, n);
281 if (errno != EAGAIN) {
282 logerror("Error reading %s (%s)", ep->file, strerror(errno));
285 } else if (ret == 0) {
294 return -1; /* nothing read, old cache still ok */
297 ep->cids = (p - ep->cache) / ep->size;
302 * Write COUNT elements from BUF to EP, starting at ID.
303 * Return 0 on success, -1 on error.
306 do_write(struct empfile *ep, void *buf, int id, int count)
311 if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
314 if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
315 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
320 n = count * ep->size;
322 ret = write(ep->fd, p, n);
324 if (errno != EAGAIN) {
325 logerror("Error writing %s (%s)", ep->file, strerror(errno));
326 /* FIXME if this extended file, truncate back to old size */
339 * Write element ID into file-backed table TYPE from buffer FROM.
340 * FIXME pass buffer size!
341 * Write through cache straight to disk.
342 * Cannot write beyond the end of fully cached table (flags & EFF_MEM).
343 * Can write at the end of partially cached table.
344 * Return non-zero on success, zero on failure.
347 ef_write(int type, int id, void *from)
352 if (ef_check(type) < 0)
356 ep->prewrite(id, from);
357 if (CANT_HAPPEN((ep->flags & EFF_MEM) ? id >= ep->fids : id > ep->fids))
358 return 0; /* not implemented */
359 if (do_write(ep, from, id, 1) < 0)
361 if (id >= ep->baseid && id < ep->baseid + ep->cids) {
362 /* update the cache if necessary */
363 to = ep->cache + (id - ep->baseid) * ep->size;
365 memcpy(to, from, ep->size);
367 if (id >= ep->fids) {
368 /* write beyond end of file extends it, take note */
375 * Extend the file-backed table TYPE by COUNT elements.
376 * Return non-zero on success, zero on failure.
379 ef_extend(int type, int count)
385 if (ef_check(type) < 0)
388 if (CANT_HAPPEN(ep->fd < 0 || count < 0))
391 tmpobj = calloc(1, ep->size);
393 for (i = 0; i < count; i++) {
395 ep->init(id + i, tmpobj);
396 if (do_write(ep, tmpobj, id + i, 1) < 0)
401 if (ep->flags & EFF_MEM) {
402 /* FIXME lazy bastards... do this right */
403 /* XXX this will cause problems if there are ef_ptrs (to the
404 * old allocated structure) active when we do the re-open */
405 how = ep->flags & ~EFF_IMMUTABLE;
418 return empfile[type].cadef;
424 return empfile[type].fids;
430 return empfile[type].flags;
436 if (empfile[type].fd <= 0)
438 return fdate(empfile[type].fd);
442 * Search for a table matching NAME, return its table type.
443 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
447 ef_byname(char *name)
449 return stmtch(name, empfile, offsetof(struct empfile, name),
454 * Search CHOICES[] for a table type matching NAME, return it.
455 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
457 * CHOICES[] must be terminated with a negative value.
460 ef_byname_from(char *name, int choices[])
466 for (p = choices; *p >= 0; p++) {
467 if (ef_check(*p) < 0)
469 switch (mineq(name, empfile[*p].name)) {
487 if (ef_check(type) < 0)
488 return "bad ef_type";
489 return empfile[type].name;
495 if (CANT_HAPPEN((unsigned)type >= EF_MAX))
501 * Ensure file-backed table contains ID.
502 * If necessary, extend it in steps of COUNT elements.
503 * Return non-zero on success, zero on failure.
506 ef_ensure_space(int type, int id, int count)
508 if (ef_check(type) < 0)
512 while (id >= empfile[type].fids) {
513 if (!ef_extend(type, count))
520 ef_fix_size(struct empfile *ep, int n)
522 ep->cids = ep->fids = n;
527 * Initialize Empire tables.
528 * Must be called once, before using anything else from this module.
538 empfile[EF_MAP].size = empfile[EF_BMAP].size = (WORLD_X * WORLD_Y) / 2;
540 ca = (struct castr *)empfile[EF_META].cache;
541 for (i = 0; ca[i].ca_name; i++) ;
542 ef_fix_size(&empfile[EF_META], i);
544 for (ep = empfile; ep->uid >= 0; ep++) {
545 if (ep->cadef == symbol_ca) {
546 lup = (struct symbol *)ep->cache;
547 for (i = 0; lup[i].name; i++) ;