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 *ep, int start);
53 * Open the binary file for table TYPE (EF_SECTOR, ...).
54 * HOW are EFF_OPEN flags to control operation.
55 * Return non-zero on success, zero on failure.
56 * You must call ef_close() before the next ef_open().
59 ef_open(int type, int how)
62 int oflags, fsiz, size;
64 if (ef_check(type) < 0)
66 if (CANT_HAPPEN(how & ~EFF_OPEN))
69 if (CANT_HAPPEN(ep->fd >= 0))
75 oflags |= O_CREAT | O_TRUNC;
79 if ((ep->fd = open(ep->file, oflags, 0660)) < 0) {
80 logerror("%s: open failed", ep->file);
85 ep->flags = (ep->flags & ~EFF_OPEN) | (how ^ ~EFF_CREATE);
87 if (fsiz % ep->size) {
88 logerror("Can't open %s (file size not a multiple of record size %d)",
93 ep->fids = fsiz / ep->size;
94 if (ep->flags & EFF_MEM)
97 ep->csize = max(1, blksize(ep->fd) / ep->size);
98 size = ep->csize * ep->size;
99 ep->cache = malloc(size);
100 if (ep->cache == NULL) {
101 logerror("ef_open: %s malloc(%d) failed\n", ep->file, size);
104 if (ep->flags & EFF_MEM) {
105 if (fillcache(ep, 0) != ep->fids) {
113 * Close the file containing objects of the type 'type', flushing the cache
122 if (ef_check(type) < 0)
125 if (ep->cache == NULL) {
126 /* no cache implies never opened */
130 ep->flags &= ~EFF_MEM;
133 if ((r = close(ep->fd)) < 0) {
134 logerror("ef_close: %s close(%d) -> %d", ep->name, ep->fd, r);
141 * 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 size = ep->csize * ep->size;
165 if ((r = lseek(ep->fd, 0L, SEEK_SET)) < 0) {
166 logerror("ef_flush: %s cache lseek(%d, 0L, SEEK_SET) -> %d",
167 ep->name, ep->fd, r);
170 if (write(ep->fd, ep->cache, size) != size) {
171 logerror("ef_flush: %s cache write(%d, %p, %d) -> %d",
172 ep->name, ep->fd, ep->cache, ep->size, r);
180 * Return a pointer the id 'id' of object of type 'type' in the cache.
183 ef_ptr(int type, int id)
187 if (ef_check(type) < 0)
190 if (id < 0 || id >= ep->fids)
192 if ((ep->flags & EFF_MEM) == 0) {
193 logerror("ef_ptr: (%s) only valid for EFF_MEM entries", ep->file);
196 return ep->cache + ep->size * id;
200 * buffered read. Tries to read a large number of items.
201 * This system won't work if item size is > sizeof buffer area.
204 ef_read(int type, int id, void *into)
209 if (ef_check(type) < 0)
214 if (ep->flags & EFF_MEM) {
217 from = ep->cache + (id * ep->size);
219 if (id >= ep->fids) {
220 ep->fids = fsize(ep->fd) / ep->size;
224 if (ep->baseid + ep->cids <= id || ep->baseid > id)
225 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 * buffered write. Modifies read cache (if applicable)
282 * and writes through to disk.
285 ef_write(int type, int id, void *from)
291 if (ef_check(type) < 0)
295 /* largest unit id; this may bite us in large games */
296 logerror("ef_write: type %d id %d is too large!\n", type, id);
299 if ((r = lseek(ep->fd, id * ep->size, SEEK_SET)) < 0) {
300 logerror("ef_write: %s #%d lseek(%d, %d, SEEK_SET) -> %d",
301 ep->name, id, ep->fd, id * ep->size, r);
305 ep->prewrite(id, from);
306 if ((r = write(ep->fd, from, ep->size)) != ep->size) {
307 logerror("ef_write: %s #%d write(%d, %p, %d) -> %d",
308 ep->name, id, ep->fd, from, ep->size, r);
311 if (id >= ep->baseid && id < ep->baseid + ep->cids) {
312 /* update the cache if necessary */
313 to = ep->cache + (id - ep->baseid) * ep->size;
314 memcpy(to, from, ep->size);
316 CANT_HAPPEN(id > ep->fids);
317 if (id >= ep->fids) {
318 if (ep->flags & EFF_MEM) {
319 logerror("file %s went beyond %d items; won't be able toread item w/o restart",
322 /* write expanded file; ep->fids = last id + 1 */
330 * Grow the file containing objects of the type 'type' by 'count' objects.
333 ef_extend(int type, int count)
341 if (ef_check(type) < 0)
344 max = ep->fids + count;
346 tmpobj = calloc(1, ep->size);
347 if ((r = lseek(ep->fd, ep->fids * ep->size, SEEK_SET)) < 0) {
348 logerror("ef_extend: %s +#%d lseek(%d, %d, SEEK_SET) -> %d",
349 ep->name, count, ep->fd, ep->fids * ep->size, r);
353 for (cur = ep->fids; cur < max; cur++) {
355 ep->init(cur, tmpobj);
356 if ((r = write(ep->fd, tmpobj, ep->size)) != ep->size) {
357 logerror("ef_extend: %s +#%d write(%d, %p, %d) -> %d",
358 ep->name, count, ep->fd, tmpobj, ep->size, r);
364 if (ep->flags & EFF_MEM) {
365 /* XXX this will cause problems if there are ef_ptrs (to the
366 * old allocated structure) active when we do the re-open */
377 * Mark the cache for the file containing objects of type 'type' as unused.
380 ef_zapcache(int type)
382 struct empfile *ep = &empfile[type];
383 if ((ep->flags & EFF_MEM) == 0) {
392 return empfile[type].cadef;
398 return empfile[type].fids;
404 return empfile[type].flags;
410 if (empfile[type].fd <= 0)
412 return fdate(empfile[type].fd);
416 * Search empfile[0..EF_MAX-1] for element named NAME.
417 * Return its index in empfile[] if found, else -1.
420 ef_byname(char *name)
427 for (i = 0; i < EF_MAX; i++) {
429 if (strncmp(ef->name, name, min(len, strlen(ef->name))) == 0)
438 if (type < 0 || type >= EF_MAX)
439 return "bad item type";
440 return empfile[type].name;
446 if (type < 0 || type >= EF_MAX) {
447 logerror("ef_ptr: bad EF_type %d\n", type);
454 ef_ensure_space(int type, int id, int count)
456 while (id >= empfile[type].fids) {
457 if (!ef_extend(type, count))