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;
100 ep->csize = max(1, blksize(fd) / ep->size);
101 size = ep->csize * ep->size;
103 ep->cache = malloc(size);
104 if (ep->cache == NULL) {
105 logerror("Can't open %s: out of memory", ep->file);
114 ep->flags = (ep->flags & ~EFF_OPEN) | (how & ~EFF_CREATE);
117 /* map file into cache */
118 if ((how & EFF_MEM) && ep->fids) {
119 if (fillcache(ep, 0) != ep->fids) {
120 ep->cids = 0; /* prevent cache flush */
121 ep->flags &= ~EFF_OPEN; /* maintain invariant */
131 * Close the file-backed table TYPE (EF_SECTOR, ...).
132 * Return non-zero on success, zero on failure.
140 retval = ef_flush(type);
142 ep->flags &= ~EFF_OPEN;
145 if (close(ep->fd) < 0) {
146 logerror("Error closing %s (%s)", ep->name, strerror(errno));
154 * Flush file-backed table TYPE (EF_SECTOR, ...) to disk.
155 * Return non-zero on success, zero on failure.
162 if (ef_check(type) < 0)
165 if (CANT_HAPPEN(ep->fd < 0))
168 * We don't know which cache entries are dirty. ef_write() writes
169 * through, but direct updates through ef_ptr() don't. They are
170 * allowed only with EFF_MEM. Assume the whole cash is dirty
173 if (!(ep->flags & EFF_RDONLY) && (ep->flags & EFF_MEM))
174 return do_write(ep, ep->cache, ep->baseid, ep->cids) >= 0;
180 * Return pointer to element ID in table TYPE if it exists, else NULL.
181 * The table must be fully cached, i.e. flags & EFF_MEM.
182 * The caller is responsible for flushing changes he makes.
185 ef_ptr(int type, int id)
189 if (ef_check(type) < 0)
192 if (CANT_HAPPEN(!(ep->flags & EFF_MEM)))
194 if (id < 0 || id >= ep->fids)
195 return NULL; /* FIXME can this happen? */
196 return ep->cache + ep->size * id;
200 * Read element ID from table TYPE into buffer INTO.
201 * FIXME pass buffer size!
202 * Return non-zero on success, zero on failure.
205 ef_read(int type, int id, void *into)
210 if (ef_check(type) < 0)
213 if (CANT_HAPPEN(!ep->cache))
216 return 0; /* FIXME can this happen? */
220 if (ep->flags & EFF_MEM) {
221 from = ep->cache + id * ep->size;
223 if (ep->baseid + ep->cids <= id || ep->baseid > id) {
224 if (fillcache(ep, id) < 1)
227 from = ep->cache + (id - ep->baseid) * ep->size;
229 memcpy(into, from, ep->size);
232 ep->postread(id, into);
237 * Fill cache of EP with elements starting at ID.
238 * If any were read, return their number.
239 * Else return -1 and leave the cache unchanged.
242 fillcache(struct empfile *ep, int start)
247 if (CANT_HAPPEN(ep->fd < 0 || !ep->cache))
250 if (lseek(ep->fd, start * ep->size, SEEK_SET) == (off_t)-1) {
251 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
256 n = ep->csize * ep->size;
258 ret = read(ep->fd, p, n);
260 if (errno != EAGAIN) {
261 logerror("Error reading %s (%s)", ep->file, strerror(errno));
264 } else if (ret == 0) {
273 return -1; /* nothing read, old cache still ok */
276 ep->cids = (p - ep->cache) / ep->size;
281 * Write COUNT elements from BUF to EP, starting at ID.
282 * Return 0 on success, -1 on error.
285 do_write(struct empfile *ep, void *buf, int id, int count)
290 if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
293 if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
294 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
299 n = count * ep->size;
301 ret = write(ep->fd, p, n);
303 if (errno != EAGAIN) {
304 logerror("Error writing %s (%s)", ep->file, strerror(errno));
305 /* FIXME if this extended file, truncate back to old size */
318 * Write element ID into file-backed table TYPE from buffer FROM.
319 * FIXME pass buffer size!
320 * Write through cache straight to disk.
321 * Cannot write beyond the end of fully cached table (flags & EFF_MEM).
322 * Can write at the end of partially cached table.
323 * Return non-zero on success, zero on failure.
326 ef_write(int type, int id, void *from)
331 if (ef_check(type) < 0)
334 if (CANT_HAPPEN(ep->fd < 0))
337 ep->prewrite(id, from);
338 if (CANT_HAPPEN((ep->flags & EFF_MEM) ? id >= ep->fids : id > ep->fids))
339 return 0; /* not implemented */
340 if (do_write(ep, from, id, 1) < 0)
342 if (id >= ep->baseid && id < ep->baseid + ep->cids) {
343 /* update the cache if necessary */
344 to = ep->cache + (id - ep->baseid) * ep->size;
345 memcpy(to, from, ep->size);
347 if (id >= ep->fids) {
348 /* write beyond end of file extends it, take note */
355 * Extend the file-backed table TYPE by COUNT elements.
356 * Return the ID of the first new element, or -1 on failure.
359 ef_extend(int type, int count)
365 if (ef_check(type) < 0)
368 if (CANT_HAPPEN(ep->fd < 0 || count < 0))
371 tmpobj = calloc(1, ep->size);
373 for (i = 0; i < count; i++) {
375 ep->init(id + i, tmpobj);
376 if (do_write(ep, tmpobj, id + i, 1) < 0)
381 if (ep->flags & EFF_MEM) {
382 /* FIXME lazy bastards... do this right */
383 /* XXX this will cause problems if there are ef_ptrs (to the
384 * old allocated structure) active when we do the re-open */
385 how = ep->flags & EFF_OPEN;
398 return empfile[type].cadef;
404 return empfile[type].fids;
410 return empfile[type].flags;
416 if (empfile[type].fd <= 0)
418 return fdate(empfile[type].fd);
422 * Search empfile[0..EF_MAX-1] for element named NAME.
423 * Return its index in empfile[] if found, else -1.
426 ef_byname(char *name)
433 for (i = 0; i < EF_MAX; i++) {
435 if (strncmp(ef->name, name, min(len, strlen(ef->name))) == 0)
444 if (ef_check(type) < 0)
445 return "bad ef_type";
446 return empfile[type].name;
452 if (CANT_HAPPEN((unsigned)type >= EF_MAX))
458 * Ensure file-backed table contains ID.
459 * If necessary, extend it in steps of COUNT elements.
460 * Return non-zero on success, zero on failure.
463 ef_ensure_space(int type, int id, int count)
465 if (ef_check(type) < 0)
468 while (id >= empfile[type].fids) {
469 if (!ef_extend(type, count))