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, 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 ((ep->fd = open(ep->file, oflags, 0660)) < 0) {
82 logerror("%s: open failed", ep->file);
87 ep->flags = (ep->flags & ~EFF_OPEN) | (how ^ ~EFF_CREATE);
89 if (fsiz % ep->size) {
90 logerror("Can't open %s (file size not a multiple of record size %d)",
95 ep->fids = fsiz / ep->size;
96 if (ep->flags & EFF_MEM)
99 ep->csize = max(1, blksize(ep->fd) / ep->size);
100 size = ep->csize * ep->size;
101 ep->cache = malloc(size);
102 if (ep->cache == NULL) {
103 logerror("ef_open: %s malloc(%d) failed\n", ep->file, size);
106 if (ep->flags & EFF_MEM) {
107 if (fillcache(ep, 0) != ep->fids) {
115 * Close the file containing objects of the type 'type', flushing the cache
124 if (ef_check(type) < 0)
127 if (ep->cache == NULL) {
128 /* no cache implies never opened */
132 ep->flags &= ~EFF_MEM;
135 if ((r = close(ep->fd)) < 0) {
136 logerror("ef_close: %s close(%d) -> %d", ep->name, ep->fd, r);
143 * Flush the cache of the file containing objects of type 'type' to disk.
150 if (ef_check(type) < 0)
153 if (ep->cache == NULL) {
154 /* no cache implies never opened */
158 * We don't know which cache entries are dirty. ef_write() writes
159 * through, but direct updates through ef_ptr() don't. They are
160 * allowed only with EFF_MEM. Assume the whole cash is dirty
163 if (!(ep->flags & EFF_RDONLY) && (ep->flags & EFF_MEM))
164 return do_write(ep, ep->cache, ep->baseid, ep->cids) >= 0;
170 * Return a pointer the id 'id' of object of type 'type' in the cache.
173 ef_ptr(int type, int id)
177 if (ef_check(type) < 0)
180 if (id < 0 || id >= ep->fids)
182 if ((ep->flags & EFF_MEM) == 0) {
183 logerror("ef_ptr: (%s) only valid for EFF_MEM entries", ep->file);
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)
204 if (ep->flags & EFF_MEM) {
207 from = ep->cache + (id * ep->size);
209 if (id >= ep->fids) {
210 ep->fids = fsize(ep->fd) / ep->size;
214 if (ep->baseid + ep->cids <= id || ep->baseid > id)
215 if (fillcache(ep, id) < 1)
217 from = ep->cache + (id - ep->baseid) * ep->size;
219 memcpy(into, from, ep->size);
222 ep->postread(id, into);
227 * Fill cache of EP with elements starting at ID.
228 * If any were read, return their number.
229 * Else return -1 and leave the cache unchanged.
232 fillcache(struct empfile *ep, int start)
237 if (CANT_HAPPEN(ep->fd < 0 || !ep->cache))
240 if (lseek(ep->fd, start * ep->size, SEEK_SET) == (off_t)-1) {
241 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
246 n = ep->csize * ep->size;
248 ret = read(ep->fd, p, n);
250 if (errno != EAGAIN) {
251 logerror("Error reading %s (%s)", ep->file, strerror(errno));
254 } else if (ret == 0) {
263 return -1; /* nothing read, old cache still ok */
266 ep->cids = (p - ep->cache) / ep->size;
271 * Write COUNT elements from BUF to EP, starting at ID.
272 * Return 0 on success, -1 on error.
275 do_write(struct empfile *ep, void *buf, int id, int count)
280 if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
283 if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
284 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
289 n = count * ep->size;
291 ret = write(ep->fd, p, n);
293 if (errno != EAGAIN) {
294 logerror("Error writing %s (%s)", ep->file, strerror(errno));
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)
320 /* largest unit id; this may bite us in large games */
321 logerror("ef_write: type %d id %d is too large!\n", type, id);
325 ep->prewrite(id, from);
326 if (CANT_HAPPEN((ep->flags & EFF_MEM) ? id >= ep->fids : id > ep->fids))
327 return 0; /* not implemented */
328 if (do_write(ep, from, id, 1) < 0)
330 if (id >= ep->baseid && id < ep->baseid + ep->cids) {
331 /* update the cache if necessary */
332 to = ep->cache + (id - ep->baseid) * ep->size;
333 memcpy(to, from, ep->size);
335 CANT_HAPPEN(id > ep->fids);
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))