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)
183 if (CANT_HAPPEN(!(ep->flags & EFF_MEM)))
185 if (id < 0 || id >= ep->fids)
186 return NULL; /* FIXME can this happen? */
187 return ep->cache + ep->size * id;
191 * buffered read. Tries to read a large number of items.
192 * This system won't work if item size is > sizeof buffer area.
195 ef_read(int type, int id, void *into)
200 if (ef_check(type) < 0)
205 if (ep->flags & EFF_MEM) {
208 from = ep->cache + (id * ep->size);
210 if (id >= ep->fids) {
211 ep->fids = fsize(ep->fd) / ep->size;
215 if (ep->baseid + ep->cids <= id || ep->baseid > id)
216 if (fillcache(ep, id) < 1)
218 from = ep->cache + (id - ep->baseid) * ep->size;
220 memcpy(into, from, ep->size);
223 ep->postread(id, into);
228 * Fill cache of EP with elements starting at ID.
229 * If any were read, return their number.
230 * Else return -1 and leave the cache unchanged.
233 fillcache(struct empfile *ep, int start)
238 if (CANT_HAPPEN(ep->fd < 0 || !ep->cache))
241 if (lseek(ep->fd, start * ep->size, SEEK_SET) == (off_t)-1) {
242 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
247 n = ep->csize * ep->size;
249 ret = read(ep->fd, p, n);
251 if (errno != EAGAIN) {
252 logerror("Error reading %s (%s)", ep->file, strerror(errno));
255 } else if (ret == 0) {
264 return -1; /* nothing read, old cache still ok */
267 ep->cids = (p - ep->cache) / ep->size;
272 * Write COUNT elements from BUF to EP, starting at ID.
273 * Return 0 on success, -1 on error.
276 do_write(struct empfile *ep, void *buf, int id, int count)
281 if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
284 if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
285 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
290 n = count * ep->size;
292 ret = write(ep->fd, p, n);
294 if (errno != EAGAIN) {
295 logerror("Error writing %s (%s)", ep->file, strerror(errno));
308 * buffered write. Modifies read cache (if applicable)
309 * and writes through to disk.
312 ef_write(int type, int id, void *from)
317 if (ef_check(type) < 0)
321 /* largest unit id; this may bite us in large games */
322 logerror("ef_write: type %d id %d is too large!\n", type, id);
326 ep->prewrite(id, from);
327 if (CANT_HAPPEN((ep->flags & EFF_MEM) ? id >= ep->fids : id > ep->fids))
328 return 0; /* not implemented */
329 if (do_write(ep, from, id, 1) < 0)
331 if (id >= ep->baseid && id < ep->baseid + ep->cids) {
332 /* update the cache if necessary */
333 to = ep->cache + (id - ep->baseid) * ep->size;
334 memcpy(to, from, ep->size);
336 if (id >= ep->fids) {
337 /* write beyond end of file extends it, take note */
344 * Grow the file containing objects of the type 'type' by 'count' objects.
347 ef_extend(int type, int count)
355 if (ef_check(type) < 0)
358 max = ep->fids + count;
360 tmpobj = calloc(1, ep->size);
361 if ((r = lseek(ep->fd, ep->fids * ep->size, SEEK_SET)) < 0) {
362 logerror("ef_extend: %s +#%d lseek(%d, %d, SEEK_SET) -> %d",
363 ep->name, count, ep->fd, ep->fids * ep->size, r);
367 for (cur = ep->fids; cur < max; cur++) {
369 ep->init(cur, tmpobj);
370 if ((r = write(ep->fd, tmpobj, ep->size)) != ep->size) {
371 logerror("ef_extend: %s +#%d write(%d, %p, %d) -> %d",
372 ep->name, count, ep->fd, tmpobj, ep->size, r);
378 if (ep->flags & EFF_MEM) {
379 /* XXX this will cause problems if there are ef_ptrs (to the
380 * old allocated structure) active when we do the re-open */
391 * Mark the cache for the file containing objects of type 'type' as unused.
394 ef_zapcache(int type)
396 struct empfile *ep = &empfile[type];
397 if ((ep->flags & EFF_MEM) == 0) {
406 return empfile[type].cadef;
412 return empfile[type].fids;
418 return empfile[type].flags;
424 if (empfile[type].fd <= 0)
426 return fdate(empfile[type].fd);
430 * Search empfile[0..EF_MAX-1] for element named NAME.
431 * Return its index in empfile[] if found, else -1.
434 ef_byname(char *name)
441 for (i = 0; i < EF_MAX; i++) {
443 if (strncmp(ef->name, name, min(len, strlen(ef->name))) == 0)
452 if (type < 0 || type >= EF_MAX)
453 return "bad item type";
454 return empfile[type].name;
460 if (type < 0 || type >= EF_MAX) {
461 logerror("ef_ptr: bad EF_type %d\n", type);
468 ef_ensure_space(int type, int id, int count)
470 while (id >= empfile[type].fids) {
471 if (!ef_extend(type, count))