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: Misc. operations on files
30 * Known contributors to this file:
50 static int fillcache(struct empfile *, int);
51 static int do_write(struct empfile *, void *, int, int);
55 * Open the binary file for 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))
71 if (CANT_HAPPEN(ep->fd >= 0))
77 oflags |= O_CREAT | O_TRUNC;
81 if ((fd = open(ep->file, oflags, 0660)) < 0) {
82 logerror("Can't open %s (%s)", ep->file, strerror(errno));
86 if (fsiz % ep->size) {
87 logerror("Can't open %s (file size not a multiple of record size %d)",
92 ep->fids = fsiz / ep->size;
96 ep->csize = max(1, blksize(fd) / ep->size);
97 size = ep->csize * ep->size;
99 ep->cache = malloc(size);
100 if (ep->cache == NULL) {
101 logerror("Can't open %s: out of memory", ep->file);
110 ep->flags = (ep->flags & ~EFF_OPEN) | (how & ~EFF_CREATE);
112 if ((how & EFF_MEM) && ep->fids) {
113 if (fillcache(ep, 0) != ep->fids) {
114 ep->cids = 0; /* prevent cache flush */
115 ep->flags &= ~EFF_OPEN; /* maintain invariant */
124 * Close the file containing objects of the type 'type', flushing the cache
133 retval = ef_flush(type);
135 ep->flags &= ~EFF_OPEN;
138 if (close(ep->fd) < 0) {
139 logerror("Error closing %s (%s)", ep->name, strerror(errno));
147 * Flush the cache of the file containing objects of type 'type' to disk.
154 if (ef_check(type) < 0)
157 if (CANT_HAPPEN(ep->fd < 0))
160 * We don't know which cache entries are dirty. ef_write() writes
161 * through, but direct updates through ef_ptr() don't. They are
162 * allowed only with EFF_MEM. Assume the whole cash is dirty
165 if (!(ep->flags & EFF_RDONLY) && (ep->flags & EFF_MEM))
166 return do_write(ep, ep->cache, ep->baseid, ep->cids) >= 0;
172 * Return a pointer the id 'id' of object of type 'type' in the cache.
175 ef_ptr(int type, int id)
179 if (ef_check(type) < 0)
182 if (CANT_HAPPEN(!(ep->flags & EFF_MEM)))
184 if (id < 0 || id >= ep->fids)
185 return NULL; /* FIXME can this happen? */
186 return ep->cache + ep->size * id;
190 * buffered read. Tries to read a large number of items.
191 * This system won't work if item size is > sizeof buffer area.
194 ef_read(int type, int id, void *into)
199 if (ef_check(type) < 0)
202 if (CANT_HAPPEN(!ep->cache))
205 return 0; /* FIXME can this happen? */
209 if (ep->flags & EFF_MEM) {
210 from = ep->cache + id * ep->size;
212 if (ep->baseid + ep->cids <= id || ep->baseid > id) {
213 if (fillcache(ep, id) < 1)
216 from = ep->cache + (id - ep->baseid) * ep->size;
218 memcpy(into, from, ep->size);
221 ep->postread(id, into);
226 * Fill cache of EP with elements starting at ID.
227 * If any were read, return their number.
228 * Else return -1 and leave the cache unchanged.
231 fillcache(struct empfile *ep, int start)
236 if (CANT_HAPPEN(ep->fd < 0 || !ep->cache))
239 if (lseek(ep->fd, start * ep->size, SEEK_SET) == (off_t)-1) {
240 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
245 n = ep->csize * ep->size;
247 ret = read(ep->fd, p, n);
249 if (errno != EAGAIN) {
250 logerror("Error reading %s (%s)", ep->file, strerror(errno));
253 } else if (ret == 0) {
262 return -1; /* nothing read, old cache still ok */
265 ep->cids = (p - ep->cache) / ep->size;
270 * Write COUNT elements from BUF to EP, starting at ID.
271 * Return 0 on success, -1 on error.
274 do_write(struct empfile *ep, void *buf, int id, int count)
279 if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
282 if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
283 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
288 n = count * ep->size;
290 ret = write(ep->fd, p, n);
292 if (errno != EAGAIN) {
293 logerror("Error writing %s (%s)", ep->file, strerror(errno));
294 /* FIXME if this extended file, truncate back to old size */
307 * buffered write. Modifies read cache (if applicable)
308 * and writes through to disk.
311 ef_write(int type, int id, void *from)
316 if (ef_check(type) < 0)
319 if (CANT_HAPPEN(ep->fd < 0))
322 ep->prewrite(id, from);
323 if (CANT_HAPPEN((ep->flags & EFF_MEM) ? id >= ep->fids : id > ep->fids))
324 return 0; /* not implemented */
325 if (do_write(ep, from, id, 1) < 0)
327 if (id >= ep->baseid && id < ep->baseid + ep->cids) {
328 /* update the cache if necessary */
329 to = ep->cache + (id - ep->baseid) * ep->size;
330 memcpy(to, from, ep->size);
332 if (id >= ep->fids) {
333 /* write beyond end of file extends it, take note */
340 * Grow the file containing objects of the type 'type' by 'count' objects.
343 ef_extend(int type, int count)
349 if (ef_check(type) < 0)
352 if (CANT_HAPPEN(ep->fd < 0 || count < 0))
355 tmpobj = calloc(1, ep->size);
357 for (i = 0; i < count; i++) {
359 ep->init(id + i, tmpobj);
360 if (do_write(ep, tmpobj, id + i, 1) < 0)
365 if (ep->flags & EFF_MEM) {
366 /* FIXME lazy bastards... do this right */
367 /* XXX this will cause problems if there are ef_ptrs (to the
368 * old allocated structure) active when we do the re-open */
369 how = ep->flags & EFF_OPEN;
380 * Mark the cache for the file containing objects of type 'type' as unused.
383 ef_zapcache(int type)
385 struct empfile *ep = &empfile[type];
386 if ((ep->flags & EFF_MEM) == 0) {
395 return empfile[type].cadef;
401 return empfile[type].fids;
407 return empfile[type].flags;
413 if (empfile[type].fd <= 0)
415 return fdate(empfile[type].fd);
419 * Search empfile[0..EF_MAX-1] for element named NAME.
420 * Return its index in empfile[] if found, else -1.
423 ef_byname(char *name)
430 for (i = 0; i < EF_MAX; i++) {
432 if (strncmp(ef->name, name, min(len, strlen(ef->name))) == 0)
441 if (type < 0 || type >= EF_MAX)
442 return "bad item type";
443 return empfile[type].name;
449 if (type < 0 || type >= EF_MAX) {
450 logerror("ef_ptr: bad EF_type %d\n", type);
457 ef_ensure_space(int type, int id, int count)
459 while (id >= empfile[type].fids) {
460 if (!ef_extend(type, count))