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 flags to control operation. Naturally, immutable flags are
59 * Return non-zero on success, zero on failure.
60 * You must call ef_close() before the next ef_open().
63 ef_open(int type, int how)
66 int oflags, fd, fsiz, size;
68 if (ef_check(type) < 0)
70 if (CANT_HAPPEN(how & EFF_IMMUTABLE))
71 how &= ~EFF_IMMUTABLE;
75 if (CANT_HAPPEN(ep->fd >= 0))
81 oflags |= O_CREAT | O_TRUNC;
85 if ((fd = open(ep->file, oflags, 0660)) < 0) {
86 logerror("Can't open %s (%s)", ep->file, strerror(errno));
92 if (fsiz % ep->size) {
93 logerror("Can't open %s (file size not a multiple of record size %d)",
98 ep->fids = fsiz / ep->size;
101 if (ep->flags & EFF_STATIC) {
102 /* ep->cache already points to space for e->csize elements */
104 if (ep->fids > ep->csize) {
105 logerror("Can't open %s: file larger than %d bytes",
106 ep->file, ep->fids * ep->size);
113 ep->csize = ep->fids;
115 ep->csize = max(1, blksize(fd) / ep->size);
116 size = ep->csize * ep->size;
117 if (CANT_HAPPEN(ep->cache))
119 ep->cache = malloc(size);
120 if (ep->cache == NULL && size) {
121 logerror("Can't open %s: out of memory", ep->file);
128 ep->flags = (ep->flags & EFF_IMMUTABLE) | (how & ~EFF_CREATE);
131 /* map file into cache */
132 if ((how & EFF_MEM) && ep->fids) {
133 if (fillcache(ep, 0) != ep->fids) {
134 ep->cids = 0; /* prevent cache flush */
135 ep->flags &= EFF_IMMUTABLE; /* maintain invariant */
142 * Could close fd if both EFF_RDONLY and EFF_MEM, but that doesn't
143 * happen, so don't bother.
150 * Close the file-backed table TYPE (EF_SECTOR, ...).
151 * Return non-zero on success, zero on failure.
159 retval = ef_flush(type);
161 ep->flags &= EFF_IMMUTABLE;
162 if (!(ep->flags & EFF_STATIC)) {
166 if (close(ep->fd) < 0) {
167 logerror("Error closing %s (%s)", ep->name, strerror(errno));
175 * Flush file-backed table TYPE (EF_SECTOR, ...) to disk.
176 * Return non-zero on success, zero on failure.
183 if (ef_check(type) < 0)
186 if (CANT_HAPPEN(ep->fd < 0))
189 * We don't know which cache entries are dirty. ef_write() writes
190 * through, but direct updates through ef_ptr() don't. They are
191 * allowed only with EFF_MEM. Assume the whole cash is dirty
194 if (!(ep->flags & EFF_RDONLY) && (ep->flags & EFF_MEM))
195 return do_write(ep, ep->cache, ep->baseid, ep->cids) >= 0;
201 * Return pointer to element ID in table TYPE if it exists, else NULL.
202 * The table must be fully cached, i.e. flags & EFF_MEM.
203 * The caller is responsible for flushing changes he makes.
206 ef_ptr(int type, int id)
210 if (ef_check(type) < 0)
213 if (CANT_HAPPEN(!(ep->flags & EFF_MEM) || !ep->cache))
215 if (id < 0 || id >= ep->fids) {
216 CANT_HAPPEN(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) {
239 CANT_HAPPEN(id != ep->fids);
243 if (ep->flags & EFF_MEM) {
244 from = ep->cache + id * ep->size;
246 if (ep->baseid + ep->cids <= id || ep->baseid > id) {
247 if (fillcache(ep, id) < 1)
250 from = ep->cache + (id - ep->baseid) * ep->size;
252 memcpy(into, from, ep->size);
255 ep->postread(id, into);
260 * Fill cache of EP with elements starting at ID.
261 * If any were read, return their number.
262 * Else return -1 and leave the cache unchanged.
265 fillcache(struct empfile *ep, int start)
270 if (CANT_HAPPEN(ep->fd < 0 || !ep->cache))
273 if (lseek(ep->fd, start * ep->size, SEEK_SET) == (off_t)-1) {
274 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
279 n = ep->csize * ep->size;
281 ret = read(ep->fd, p, n);
283 if (errno != EAGAIN) {
284 logerror("Error reading %s (%s)", ep->file, strerror(errno));
287 } else if (ret == 0) {
296 return -1; /* nothing read, old cache still ok */
299 ep->cids = (p - ep->cache) / ep->size;
304 * Write COUNT elements from BUF to EP, starting at ID.
305 * Return 0 on success, -1 on error.
308 do_write(struct empfile *ep, void *buf, int id, int count)
313 if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
316 if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
317 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
322 n = count * ep->size;
324 ret = write(ep->fd, p, n);
326 if (errno != EAGAIN) {
327 logerror("Error writing %s (%s)", ep->file, strerror(errno));
328 /* FIXME if this extended file, truncate back to old size */
341 * Write element ID into file-backed table TYPE from buffer FROM.
342 * FIXME pass buffer size!
343 * Write through cache straight to disk.
344 * Cannot write beyond the end of fully cached table (flags & EFF_MEM).
345 * Can write at the end of partially cached table.
346 * Return non-zero on success, zero on failure.
349 ef_write(int type, int id, void *from)
354 if (ef_check(type) < 0)
358 ep->prewrite(id, from);
359 if (CANT_HAPPEN((ep->flags & EFF_MEM) ? id >= ep->fids : id > ep->fids))
360 return 0; /* not implemented */
361 if (do_write(ep, from, id, 1) < 0)
363 if (id >= ep->baseid && id < ep->baseid + ep->cids) {
364 /* update the cache if necessary */
365 to = ep->cache + (id - ep->baseid) * ep->size;
366 memcpy(to, from, ep->size);
368 if (id >= ep->fids) {
369 /* write beyond end of file extends it, take note */
376 * Extend the file-backed table TYPE by COUNT elements.
377 * Return non-zero on success, zero on failure.
380 ef_extend(int type, int count)
386 if (ef_check(type) < 0)
389 if (CANT_HAPPEN(ep->fd < 0 || count < 0))
392 tmpobj = calloc(1, ep->size);
394 for (i = 0; i < count; i++) {
396 ep->init(id + i, tmpobj);
397 if (do_write(ep, tmpobj, id + i, 1) < 0)
402 if (ep->flags & EFF_MEM) {
403 /* FIXME lazy bastards... do this right */
404 /* XXX this will cause problems if there are ef_ptrs (to the
405 * old allocated structure) active when we do the re-open */
406 how = ep->flags & ~EFF_IMMUTABLE;
419 return empfile[type].cadef;
425 return empfile[type].fids;
431 return empfile[type].flags;
437 if (empfile[type].fd <= 0)
439 return fdate(empfile[type].fd);
443 * Search for a table matching NAME, return its table type.
444 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
448 ef_byname(char *name)
450 return stmtch(name, empfile, offsetof(struct empfile, name),
455 * Search CHOICES[] for a table type matching NAME, return it.
456 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
458 * CHOICES[] must be terminated with a negative value.
461 ef_byname_from(char *name, int choices[])
467 for (p = choices; *p >= 0; p++) {
468 if (ef_check(*p) < 0)
470 switch (mineq(name, empfile[*p].name)) {
488 if (ef_check(type) < 0)
489 return "bad ef_type";
490 return empfile[type].name;
496 if (CANT_HAPPEN((unsigned)type >= EF_MAX))
502 * Ensure file-backed table contains ID.
503 * If necessary, extend it in steps of COUNT elements.
504 * Return non-zero on success, zero on failure.
507 ef_ensure_space(int type, int id, int count)
509 if (ef_check(type) < 0)
513 while (id >= empfile[type].fids) {
514 if (!ef_extend(type, count))
521 ef_fix_size(struct empfile *ep, int n)
523 ep->cids = ep->fids = n;
528 ef_init_chr(int type, size_t size, ptrdiff_t name_offs)
530 struct empfile *ep = &empfile[type];
534 *((char **)(p + name_offs)) && **((char **)(p + name_offs));
536 ep->cids = ep->fids = (p - ep->cache) / size;
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 ef_init_chr(EF_SHIP_CHR,
554 sizeof(struct mchrstr), offsetof(struct mchrstr, m_name));
555 ef_init_chr(EF_PLANE_CHR,
556 sizeof(struct plchrstr), offsetof(struct plchrstr, pl_name));
557 ef_init_chr(EF_LAND_CHR,
558 sizeof(struct lchrstr), offsetof(struct lchrstr, l_name));
559 ef_init_chr(EF_NUKE_CHR,
560 sizeof(struct nchrstr), offsetof(struct nchrstr, n_name));
562 ca = (struct castr *)empfile[EF_META].cache;
563 for (i = 0; ca[i].ca_name; i++) ;
564 ef_fix_size(&empfile[EF_META], i);
566 for (ep = empfile; ep->uid >= 0; ep++) {
567 if (ep->cadef == symbol_ca) {
568 lup = (struct symbol *)ep->cache;
569 for (i = 0; lup[i].name; i++) ;