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
51 static int fillcache(struct empfile *, int);
52 static int do_write(struct empfile *, void *, int, int);
55 * Open the file-backed table TYPE (EF_SECTOR, ...).
56 * HOW are EFF_OPEN flags to control operation.
57 * Return non-zero on success, zero on failure.
58 * You must call ef_close() before the next ef_open().
61 ef_open(int type, int how)
64 int oflags, fd, fsiz, size;
66 if (ef_check(type) < 0)
68 if (CANT_HAPPEN(how & ~EFF_OPEN))
73 if (CANT_HAPPEN(ep->fd >= 0))
79 oflags |= O_CREAT | O_TRUNC;
83 if ((fd = open(ep->file, oflags, 0660)) < 0) {
84 logerror("Can't open %s (%s)", ep->file, strerror(errno));
90 if (fsiz % ep->size) {
91 logerror("Can't open %s (file size not a multiple of record size %d)",
96 ep->fids = fsiz / ep->size;
99 if (ep->flags & EFF_STATIC) {
100 /* ep->cache already points to space for e->csize elements */
102 if (ep->fids > ep->csize) {
103 logerror("Can't open %s: file larger than %d bytes",
104 ep->file, ep->fids * ep->size);
111 ep->csize = ep->fids;
113 ep->csize = max(1, blksize(fd) / ep->size);
114 size = ep->csize * ep->size;
115 ep->cache = malloc(size);
116 if (ep->cache == NULL && size) {
117 logerror("Can't open %s: out of memory", ep->file);
124 ep->flags = (ep->flags & ~EFF_OPEN) | (how & ~EFF_CREATE);
127 /* map file into cache */
128 if ((how & EFF_MEM) && ep->fids) {
129 if (fillcache(ep, 0) != ep->fids) {
130 ep->cids = 0; /* prevent cache flush */
131 ep->flags &= ~EFF_OPEN; /* maintain invariant */
141 * Close the file-backed table TYPE (EF_SECTOR, ...).
142 * Return non-zero on success, zero on failure.
150 retval = ef_flush(type);
152 ep->flags &= ~EFF_OPEN;
153 if (!(ep->flags & EFF_STATIC)) {
157 if (close(ep->fd) < 0) {
158 logerror("Error closing %s (%s)", ep->name, strerror(errno));
166 * Flush file-backed table TYPE (EF_SECTOR, ...) to disk.
167 * Return non-zero on success, zero on failure.
174 if (ef_check(type) < 0)
177 if (CANT_HAPPEN(ep->fd < 0))
180 * We don't know which cache entries are dirty. ef_write() writes
181 * through, but direct updates through ef_ptr() don't. They are
182 * allowed only with EFF_MEM. Assume the whole cash is dirty
185 if (!(ep->flags & EFF_RDONLY) && (ep->flags & EFF_MEM))
186 return do_write(ep, ep->cache, ep->baseid, ep->cids) >= 0;
192 * Return pointer to element ID in table TYPE if it exists, else NULL.
193 * The table must be fully cached, i.e. flags & EFF_MEM.
194 * The caller is responsible for flushing changes he makes.
197 ef_ptr(int type, int id)
201 if (ef_check(type) < 0)
204 if (CANT_HAPPEN(!(ep->flags & EFF_MEM)))
206 if (id < 0 || id >= ep->fids)
207 return NULL; /* FIXME can this happen? */
208 return ep->cache + ep->size * id;
212 * Read element ID from table TYPE into buffer INTO.
213 * FIXME pass buffer size!
214 * Return non-zero on success, zero on failure.
217 ef_read(int type, int id, void *into)
222 if (ef_check(type) < 0)
225 if (CANT_HAPPEN(!ep->cache))
228 return 0; /* FIXME can this happen? */
232 if (ep->flags & EFF_MEM) {
233 from = ep->cache + id * ep->size;
235 if (ep->baseid + ep->cids <= id || ep->baseid > id) {
236 if (fillcache(ep, id) < 1)
239 from = ep->cache + (id - ep->baseid) * ep->size;
241 memcpy(into, from, ep->size);
244 ep->postread(id, into);
249 * Fill cache of EP with elements starting at ID.
250 * If any were read, return their number.
251 * Else return -1 and leave the cache unchanged.
254 fillcache(struct empfile *ep, int start)
259 if (CANT_HAPPEN(ep->fd < 0 || !ep->cache))
262 if (lseek(ep->fd, start * ep->size, SEEK_SET) == (off_t)-1) {
263 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
268 n = ep->csize * ep->size;
270 ret = read(ep->fd, p, n);
272 if (errno != EAGAIN) {
273 logerror("Error reading %s (%s)", ep->file, strerror(errno));
276 } else if (ret == 0) {
285 return -1; /* nothing read, old cache still ok */
288 ep->cids = (p - ep->cache) / ep->size;
293 * Write COUNT elements from BUF to EP, starting at ID.
294 * Return 0 on success, -1 on error.
297 do_write(struct empfile *ep, void *buf, int id, int count)
302 if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
305 if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
306 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
311 n = count * ep->size;
313 ret = write(ep->fd, p, n);
315 if (errno != EAGAIN) {
316 logerror("Error writing %s (%s)", ep->file, strerror(errno));
317 /* FIXME if this extended file, truncate back to old size */
330 * Write element ID into file-backed table TYPE from buffer FROM.
331 * FIXME pass buffer size!
332 * Write through cache straight to disk.
333 * Cannot write beyond the end of fully cached table (flags & EFF_MEM).
334 * Can write at the end of partially cached table.
335 * Return non-zero on success, zero on failure.
338 ef_write(int type, int id, void *from)
343 if (ef_check(type) < 0)
346 if (CANT_HAPPEN(ep->fd < 0))
349 ep->prewrite(id, from);
350 if (CANT_HAPPEN((ep->flags & EFF_MEM) ? id >= ep->fids : id > ep->fids))
351 return 0; /* not implemented */
352 if (do_write(ep, from, id, 1) < 0)
354 if (id >= ep->baseid && id < ep->baseid + ep->cids) {
355 /* update the cache if necessary */
356 to = ep->cache + (id - ep->baseid) * ep->size;
357 memcpy(to, from, ep->size);
359 if (id >= ep->fids) {
360 /* write beyond end of file extends it, take note */
367 * Extend the file-backed table TYPE by COUNT elements.
368 * Return the ID of the first new element, or -1 on failure.
371 ef_extend(int type, int count)
377 if (ef_check(type) < 0)
380 if (CANT_HAPPEN(ep->fd < 0 || count < 0))
383 tmpobj = calloc(1, ep->size);
385 for (i = 0; i < count; i++) {
387 ep->init(id + i, tmpobj);
388 if (do_write(ep, tmpobj, id + i, 1) < 0)
393 if (ep->flags & EFF_MEM) {
394 /* FIXME lazy bastards... do this right */
395 /* XXX this will cause problems if there are ef_ptrs (to the
396 * old allocated structure) active when we do the re-open */
397 how = ep->flags & EFF_OPEN;
410 return empfile[type].cadef;
416 return empfile[type].fids;
422 return empfile[type].flags;
428 if (empfile[type].fd <= 0)
430 return fdate(empfile[type].fd);
434 * Search for a table matching NAME, return its table type.
435 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
439 ef_byname(char *name)
441 return stmtch(name, empfile, offsetof(struct empfile, name),
446 * Search CHOICES[] for a table type matching NAME, return it.
447 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
449 * CHOICES[] must be terminated with a negative value.
452 ef_byname_from(char *name, int choices[])
458 for (p = choices; *p >= 0; p++) {
459 if (ef_check(*p) < 0)
461 switch (mineq(name, empfile[*p].name)) {
479 if (ef_check(type) < 0)
480 return "bad ef_type";
481 return empfile[type].name;
487 if (CANT_HAPPEN((unsigned)type >= EF_MAX))
493 * Ensure file-backed table contains ID.
494 * If necessary, extend it in steps of COUNT elements.
495 * Return non-zero on success, zero on failure.
498 ef_ensure_space(int type, int id, int count)
500 if (ef_check(type) < 0)
503 while (id >= empfile[type].fids) {
504 if (!ef_extend(type, count))