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;
98 ep->cache = malloc(size);
99 if (ep->cache == NULL) {
100 logerror("Can't open %s: out of memory", ep->file);
106 ep->flags = (ep->flags & ~EFF_OPEN) | (how ^ ~EFF_CREATE);
109 if (fillcache(ep, 0) != ep->fids) {
110 ep->cids = 0; /* prevent cache flush */
111 ep->flags &= ~EFF_OPEN; /* maintain invariant */
120 * Close the file containing objects of the type 'type', flushing the cache
129 retval = ef_flush(type);
131 ep->flags &= ~EFF_OPEN;
134 if (close(ep->fd) < 0) {
135 logerror("Error closing %s (%s)", ep->name, strerror(errno));
143 * Flush the cache of the file containing objects of type 'type' to disk.
150 if (ef_check(type) < 0)
153 if (CANT_HAPPEN(ep->fd < 0))
156 * We don't know which cache entries are dirty. ef_write() writes
157 * through, but direct updates through ef_ptr() don't. They are
158 * allowed only with EFF_MEM. Assume the whole cash is dirty
161 if (!(ep->flags & EFF_RDONLY) && (ep->flags & EFF_MEM))
162 return do_write(ep, ep->cache, ep->baseid, ep->cids) >= 0;
168 * Return a pointer the id 'id' of object of type 'type' in the cache.
171 ef_ptr(int type, int id)
175 if (ef_check(type) < 0)
178 if (id < 0 || id >= ep->fids)
180 if ((ep->flags & EFF_MEM) == 0) {
181 logerror("ef_ptr: (%s) only valid for EFF_MEM entries", ep->file);
184 return ep->cache + ep->size * id;
188 * buffered read. Tries to read a large number of items.
189 * This system won't work if item size is > sizeof buffer area.
192 ef_read(int type, int id, void *into)
197 if (ef_check(type) < 0)
202 if (ep->flags & EFF_MEM) {
205 from = ep->cache + (id * ep->size);
207 if (id >= ep->fids) {
208 ep->fids = fsize(ep->fd) / ep->size;
212 if (ep->baseid + ep->cids <= id || ep->baseid > id)
213 if (fillcache(ep, id) < 1)
215 from = ep->cache + (id - ep->baseid) * ep->size;
217 memcpy(into, from, ep->size);
220 ep->postread(id, into);
225 * Fill cache of EP with elements starting at ID.
226 * If any were read, return their number.
227 * Else return -1 and leave the cache unchanged.
230 fillcache(struct empfile *ep, int start)
235 if (CANT_HAPPEN(ep->fd < 0 || !ep->cache))
238 if (lseek(ep->fd, start * ep->size, SEEK_SET) == (off_t)-1) {
239 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
244 n = ep->csize * ep->size;
246 ret = read(ep->fd, p, n);
248 if (errno != EAGAIN) {
249 logerror("Error reading %s (%s)", ep->file, strerror(errno));
252 } else if (ret == 0) {
261 return -1; /* nothing read, old cache still ok */
264 ep->cids = (p - ep->cache) / ep->size;
269 * Write COUNT elements from BUF to EP, starting at ID.
270 * Return 0 on success, -1 on error.
273 do_write(struct empfile *ep, void *buf, int id, int count)
278 if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
281 if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
282 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
287 n = count * ep->size;
289 ret = write(ep->fd, p, n);
291 if (errno != EAGAIN) {
292 logerror("Error writing %s (%s)", ep->file, strerror(errno));
305 * buffered write. Modifies read cache (if applicable)
306 * and writes through to disk.
309 ef_write(int type, int id, void *from)
314 if (ef_check(type) < 0)
318 /* largest unit id; this may bite us in large games */
319 logerror("ef_write: type %d id %d is too large!\n", type, id);
323 ep->prewrite(id, from);
324 if (CANT_HAPPEN((ep->flags & EFF_MEM) ? id >= ep->fids : id > ep->fids))
325 return 0; /* not implemented */
326 if (do_write(ep, from, id, 1) < 0)
328 if (id >= ep->baseid && id < ep->baseid + ep->cids) {
329 /* update the cache if necessary */
330 to = ep->cache + (id - ep->baseid) * ep->size;
331 memcpy(to, from, ep->size);
333 CANT_HAPPEN(id > ep->fids);
334 if (id >= ep->fids) {
335 /* write beyond end of file extends it, take note */
342 * Grow the file containing objects of the type 'type' by 'count' objects.
345 ef_extend(int type, int count)
353 if (ef_check(type) < 0)
356 max = ep->fids + count;
358 tmpobj = calloc(1, ep->size);
359 if ((r = lseek(ep->fd, ep->fids * ep->size, SEEK_SET)) < 0) {
360 logerror("ef_extend: %s +#%d lseek(%d, %d, SEEK_SET) -> %d",
361 ep->name, count, ep->fd, ep->fids * ep->size, r);
365 for (cur = ep->fids; cur < max; cur++) {
367 ep->init(cur, tmpobj);
368 if ((r = write(ep->fd, tmpobj, ep->size)) != ep->size) {
369 logerror("ef_extend: %s +#%d write(%d, %p, %d) -> %d",
370 ep->name, count, ep->fd, tmpobj, ep->size, r);
376 if (ep->flags & EFF_MEM) {
377 /* XXX this will cause problems if there are ef_ptrs (to the
378 * old allocated structure) active when we do the re-open */
389 * Mark the cache for the file containing objects of type 'type' as unused.
392 ef_zapcache(int type)
394 struct empfile *ep = &empfile[type];
395 if ((ep->flags & EFF_MEM) == 0) {
404 return empfile[type].cadef;
410 return empfile[type].fids;
416 return empfile[type].flags;
422 if (empfile[type].fd <= 0)
424 return fdate(empfile[type].fd);
428 * Search empfile[0..EF_MAX-1] for element named NAME.
429 * Return its index in empfile[] if found, else -1.
432 ef_byname(char *name)
439 for (i = 0; i < EF_MAX; i++) {
441 if (strncmp(ef->name, name, min(len, strlen(ef->name))) == 0)
450 if (type < 0 || type >= EF_MAX)
451 return "bad item type";
452 return empfile[type].name;
458 if (type < 0 || type >= EF_MAX) {
459 logerror("ef_ptr: bad EF_type %d\n", type);
466 ef_ensure_space(int type, int id, int count)
468 while (id >= empfile[type].fids) {
469 if (!ef_extend(type, count))