2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * 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
52 static int fillcache(struct empfile *, int);
53 static int do_write(struct empfile *, void *, int, int);
56 * Open the file-backed table TYPE (EF_SECTOR, ...).
57 * HOW are EFF_OPEN flags to control operation.
58 * Return non-zero on success, zero on failure.
59 * You must call ef_close() before the next ef_open().
62 ef_open(int type, int how)
65 int oflags, fd, fsiz, size;
67 if (ef_check(type) < 0)
69 if (CANT_HAPPEN(how & ~EFF_OPEN))
74 if (CANT_HAPPEN(ep->fd >= 0))
80 oflags |= O_CREAT | O_TRUNC;
84 if ((fd = open(ep->file, oflags, 0660)) < 0) {
85 logerror("Can't open %s (%s)", ep->file, strerror(errno));
91 if (fsiz % ep->size) {
92 logerror("Can't open %s (file size not a multiple of record size %d)",
97 ep->fids = fsiz / ep->size;
100 if (ep->flags & EFF_STATIC) {
101 /* ep->cache already points to space for e->csize elements */
103 if (ep->fids > ep->csize) {
104 logerror("Can't open %s: file larger than %d bytes",
105 ep->file, ep->fids * ep->size);
112 ep->csize = ep->fids;
114 ep->csize = max(1, blksize(fd) / ep->size);
115 size = ep->csize * ep->size;
116 ep->cache = malloc(size);
117 if (ep->cache == NULL && size) {
118 logerror("Can't open %s: out of memory", ep->file);
125 ep->flags = (ep->flags & ~EFF_OPEN) | (how & ~EFF_CREATE);
128 /* map file into cache */
129 if ((how & EFF_MEM) && ep->fids) {
130 if (fillcache(ep, 0) != ep->fids) {
131 ep->cids = 0; /* prevent cache flush */
132 ep->flags &= ~EFF_OPEN; /* maintain invariant */
142 * Close the file-backed table TYPE (EF_SECTOR, ...).
143 * Return non-zero on success, zero on failure.
151 retval = ef_flush(type);
153 ep->flags &= ~EFF_OPEN;
154 if (!(ep->flags & EFF_STATIC)) {
158 if (close(ep->fd) < 0) {
159 logerror("Error closing %s (%s)", ep->name, strerror(errno));
167 * Flush file-backed table TYPE (EF_SECTOR, ...) to disk.
168 * Return non-zero on success, zero on failure.
175 if (ef_check(type) < 0)
178 if (CANT_HAPPEN(ep->fd < 0))
181 * We don't know which cache entries are dirty. ef_write() writes
182 * through, but direct updates through ef_ptr() don't. They are
183 * allowed only with EFF_MEM. Assume the whole cash is dirty
186 if (!(ep->flags & EFF_RDONLY) && (ep->flags & EFF_MEM))
187 return do_write(ep, ep->cache, ep->baseid, ep->cids) >= 0;
193 * Return pointer to element ID in table TYPE if it exists, else NULL.
194 * The table must be fully cached, i.e. flags & EFF_MEM.
195 * The caller is responsible for flushing changes he makes.
198 ef_ptr(int type, int id)
202 if (ef_check(type) < 0)
205 if (CANT_HAPPEN(!(ep->flags & EFF_MEM)))
207 if (id < 0 || id >= ep->fids)
208 return NULL; /* FIXME can this happen? */
209 return ep->cache + ep->size * id;
213 * Read element ID from table TYPE into buffer INTO.
214 * FIXME pass buffer size!
215 * Return non-zero on success, zero on failure.
218 ef_read(int type, int id, void *into)
223 if (ef_check(type) < 0)
226 if (CANT_HAPPEN(!ep->cache))
229 return 0; /* FIXME can this happen? */
233 if (ep->flags & EFF_MEM) {
234 from = ep->cache + id * ep->size;
236 if (ep->baseid + ep->cids <= id || ep->baseid > id) {
237 if (fillcache(ep, id) < 1)
240 from = ep->cache + (id - ep->baseid) * ep->size;
242 memcpy(into, from, ep->size);
245 ep->postread(id, into);
250 * Fill cache of EP with elements starting at ID.
251 * If any were read, return their number.
252 * Else return -1 and leave the cache unchanged.
255 fillcache(struct empfile *ep, int start)
260 if (CANT_HAPPEN(ep->fd < 0 || !ep->cache))
263 if (lseek(ep->fd, start * ep->size, SEEK_SET) == (off_t)-1) {
264 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
269 n = ep->csize * ep->size;
271 ret = read(ep->fd, p, n);
273 if (errno != EAGAIN) {
274 logerror("Error reading %s (%s)", ep->file, strerror(errno));
277 } else if (ret == 0) {
286 return -1; /* nothing read, old cache still ok */
289 ep->cids = (p - ep->cache) / ep->size;
294 * Write COUNT elements from BUF to EP, starting at ID.
295 * Return 0 on success, -1 on error.
298 do_write(struct empfile *ep, void *buf, int id, int count)
303 if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
306 if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
307 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
312 n = count * ep->size;
314 ret = write(ep->fd, p, n);
316 if (errno != EAGAIN) {
317 logerror("Error writing %s (%s)", ep->file, strerror(errno));
318 /* FIXME if this extended file, truncate back to old size */
331 * Write element ID into file-backed table TYPE from buffer FROM.
332 * FIXME pass buffer size!
333 * Write through cache straight to disk.
334 * Cannot write beyond the end of fully cached table (flags & EFF_MEM).
335 * Can write at the end of partially cached table.
336 * Return non-zero on success, zero on failure.
339 ef_write(int type, int id, void *from)
344 if (ef_check(type) < 0)
347 if (CANT_HAPPEN(ep->fd < 0))
350 ep->prewrite(id, from);
351 if (CANT_HAPPEN((ep->flags & EFF_MEM) ? id >= ep->fids : id > ep->fids))
352 return 0; /* not implemented */
353 if (do_write(ep, from, id, 1) < 0)
355 if (id >= ep->baseid && id < ep->baseid + ep->cids) {
356 /* update the cache if necessary */
357 to = ep->cache + (id - ep->baseid) * ep->size;
358 memcpy(to, from, ep->size);
360 if (id >= ep->fids) {
361 /* write beyond end of file extends it, take note */
368 * Extend the file-backed table TYPE by COUNT elements.
369 * Return the ID of the first new element, or -1 on failure.
372 ef_extend(int type, int count)
378 if (ef_check(type) < 0)
381 if (CANT_HAPPEN(ep->fd < 0 || count < 0))
384 tmpobj = calloc(1, ep->size);
386 for (i = 0; i < count; i++) {
388 ep->init(id + i, tmpobj);
389 if (do_write(ep, tmpobj, id + i, 1) < 0)
394 if (ep->flags & EFF_MEM) {
395 /* FIXME lazy bastards... do this right */
396 /* XXX this will cause problems if there are ef_ptrs (to the
397 * old allocated structure) active when we do the re-open */
398 how = ep->flags & EFF_OPEN;
411 return empfile[type].cadef;
417 return empfile[type].fids;
423 return empfile[type].flags;
429 if (empfile[type].fd <= 0)
431 return fdate(empfile[type].fd);
435 * Search for a table matching NAME, return its table type.
436 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
440 ef_byname(char *name)
442 return stmtch(name, empfile, offsetof(struct empfile, name),
447 * Search CHOICES[] for a table type matching NAME, return it.
448 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
450 * CHOICES[] must be terminated with a negative value.
453 ef_byname_from(char *name, int choices[])
459 for (p = choices; *p >= 0; p++) {
460 if (ef_check(*p) < 0)
462 switch (mineq(name, empfile[*p].name)) {
480 if (ef_check(type) < 0)
481 return "bad ef_type";
482 return empfile[type].name;
488 if (CANT_HAPPEN((unsigned)type >= EF_MAX))
494 * Ensure file-backed table contains ID.
495 * If necessary, extend it in steps of COUNT elements.
496 * Return non-zero on success, zero on failure.
499 ef_ensure_space(int type, int id, int count)
501 if (ef_check(type) < 0)
504 while (id >= empfile[type].fids) {
505 if (!ef_extend(type, count))
512 ef_fix_size(struct empfile *ep, int n)
514 ep->cids = ep->fids = n;
519 ef_init_chr(int type, size_t size, ptrdiff_t name_offs)
521 struct empfile *ep = &empfile[type];
524 for (p = ep->cache; **((char **)(p + name_offs)); p += size) ;
525 ef_fix_size(ep, (p - ep->cache) / size);
529 * Initialize Empire tables.
530 * Must be called once, before using anything else from this module.
540 empfile[EF_MAP].size = empfile[EF_BMAP].size = (WORLD_X * WORLD_Y) / 2;
542 ef_init_chr(EF_SHIP_CHR,
543 sizeof(struct mchrstr), offsetof(struct mchrstr, m_name));
544 ef_init_chr(EF_PLANE_CHR,
545 sizeof(struct plchrstr), offsetof(struct plchrstr, pl_name));
546 ef_init_chr(EF_LAND_CHR,
547 sizeof(struct lchrstr), offsetof(struct lchrstr, l_name));
548 ef_init_chr(EF_NUKE_CHR,
549 sizeof(struct nchrstr), offsetof(struct nchrstr, n_name));
551 ca = (struct castr *)empfile[EF_META].cache;
552 for (i = 0; ca[i].ca_name; i++) ;
553 ef_fix_size(&empfile[EF_META], i);
555 for (ep = empfile; ep->ef_uid >= 0; ep++) {
556 if (ep->cadef == symbol_ca) {
557 lup = (struct symbol *)ep->cache;
558 for (i = 0; lup[i].value; i++) ;