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:
49 static void fillcache(struct empfile *ep, int start);
52 * Open the binary file for table TYPE (EF_SECTOR, ...).
53 * MODE is passed to open().
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 mode, int how)
67 if (ef_check(type) < 0)
69 if (CANT_HAPPEN(how & ~EFF_OPEN))
72 if (CANT_HAPPEN(ep->fd >= 0))
74 if ((ep->fd = open(ep->file, mode, 0660)) < 0) {
75 logerror("%s: open failed", ep->file);
82 ep->fids = fsize(ep->fd) / ep->size;
83 if (ep->flags & EFF_MEM)
86 ep->csize = max(1, blksize(ep->fd) / ep->size);
87 size = ep->csize * ep->size;
88 ep->cache = malloc(size);
89 if (ep->cache == NULL) {
90 logerror("ef_open: %s malloc(%d) failed\n", ep->file, size);
93 if (ep->flags & EFF_MEM) {
94 if (read(ep->fd, ep->cache, size) != size) {
95 logerror("ef_open: read(%s) failed\n", ep->file);
98 ep->cids = size / ep->size;
104 * Close the file containing objects of the type 'type', flushing the cache
113 if (ef_check(type) < 0)
116 if (ep->cache == NULL) {
117 /* no cache implies never opened */
121 ep->flags &= ~EFF_MEM;
124 if ((r = close(ep->fd)) < 0) {
125 logerror("ef_close: %s close(%d) -> %d", ep->name, ep->fd, r);
132 * Flush the cache of the file containing objects of type 'type' to disk.
141 if (ef_check(type) < 0)
144 if (ep->cache == NULL) {
145 /* no cache implies never opened */
148 size = ep->csize * ep->size;
149 if (ep->mode > 0 && (ep->flags & EFF_MEM)) {
150 if ((r = lseek(ep->fd, 0L, SEEK_SET)) < 0) {
151 logerror("ef_flush: %s cache lseek(%d, 0L, SEEK_SET) -> %d",
152 ep->name, ep->fd, r);
155 if (write(ep->fd, ep->cache, size) != size) {
156 logerror("ef_flush: %s cache write(%d, %p, %d) -> %d",
157 ep->name, ep->fd, ep->cache, ep->size, r);
161 /*ef_zapcache(type); */
166 * Return a pointer the id 'id' of object of type 'type' in the cache.
169 ef_ptr(int type, int id)
173 if (ef_check(type) < 0)
176 if (id < 0 || id >= ep->fids)
178 if ((ep->flags & EFF_MEM) == 0) {
179 logerror("ef_ptr: (%s) only valid for EFF_MEM entries", ep->file);
182 return ep->cache + ep->size * id;
186 * buffered read. Tries to read a large number of items.
187 * This system won't work if item size is > sizeof buffer area.
190 ef_read(int type, int id, void *into)
195 if (ef_check(type) < 0)
200 if (ep->flags & EFF_MEM) {
203 from = ep->cache + (id * ep->size);
205 if (id >= ep->fids) {
206 ep->fids = fsize(ep->fd) / ep->size;
210 if (ep->baseid + ep->cids <= id || ep->baseid > id)
212 from = ep->cache + (id - ep->baseid) * ep->size;
214 memcpy(into, from, ep->size);
217 ep->postread(id, into);
222 fillcache(struct empfile *ep, int start)
227 lseek(ep->fd, start * ep->size, SEEK_SET);
228 n = read(ep->fd, ep->cache, ep->csize * ep->size);
229 ep->cids = n / ep->size;
233 * buffered write. Modifies read cache (if applicable)
234 * and writes through to disk.
237 ef_write(int type, int id, void *from)
243 if (ef_check(type) < 0)
247 /* largest unit id; this may bite us in large games */
248 logerror("ef_write: type %d id %d is too large!\n", type, id);
251 if ((r = lseek(ep->fd, id * ep->size, SEEK_SET)) < 0) {
252 logerror("ef_write: %s #%d lseek(%d, %d, SEEK_SET) -> %d",
253 ep->name, id, ep->fd, id * ep->size, r);
257 ep->prewrite(id, from);
258 if ((r = write(ep->fd, from, ep->size)) != ep->size) {
259 logerror("ef_write: %s #%d write(%d, %p, %d) -> %d",
260 ep->name, id, ep->fd, from, ep->size, r);
263 if (id >= ep->baseid && id < ep->baseid + ep->cids) {
264 /* update the cache if necessary */
265 to = ep->cache + (id - ep->baseid) * ep->size;
266 memcpy(to, from, ep->size);
268 CANT_HAPPEN(id > ep->fids);
269 if (id >= ep->fids) {
270 if (ep->flags & EFF_MEM) {
271 logerror("file %s went beyond %d items; won't be able toread item w/o restart",
274 /* write expanded file; ep->fids = last id + 1 */
282 * Grow the file containing objects of the type 'type' by 'count' objects.
285 ef_extend(int type, int count)
293 if (ef_check(type) < 0)
296 max = ep->fids + count;
298 tmpobj = calloc(1, ep->size);
299 if ((r = lseek(ep->fd, ep->fids * ep->size, SEEK_SET)) < 0) {
300 logerror("ef_extend: %s +#%d lseek(%d, %d, SEEK_SET) -> %d",
301 ep->name, count, ep->fd, ep->fids * ep->size, r);
305 for (cur = ep->fids; cur < max; cur++) {
307 ep->init(cur, tmpobj);
308 if ((r = write(ep->fd, tmpobj, ep->size)) != ep->size) {
309 logerror("ef_extend: %s +#%d write(%d, %p, %d) -> %d",
310 ep->name, count, ep->fd, tmpobj, ep->size, r);
316 if (ep->flags & EFF_MEM) {
317 /* XXX this will cause problems if there are ef_ptrs (to the
318 * old allocated structure) active when we do the re-open */
322 ef_open(type, mode, how);
330 * Mark the cache for the file containing objects of type 'type' as unused.
333 ef_zapcache(int type)
335 struct empfile *ep = &empfile[type];
336 if ((ep->flags & EFF_MEM) == 0) {
345 return empfile[type].cadef;
351 return empfile[type].fids;
357 return empfile[type].flags;
363 if (empfile[type].fd <= 0)
365 return fdate(empfile[type].fd);
369 * Search empfile[0..EF_MAX-1] for element named NAME.
370 * Return its index in empfile[] if found, else -1.
373 ef_byname(char *name)
380 for (i = 0; i < EF_MAX; i++) {
382 if (strncmp(ef->name, name, min(len, strlen(ef->name))) == 0)
391 if (type < 0 || type >= EF_MAX)
392 return "bad item type";
393 return empfile[type].name;
399 if (type < 0 || type >= EF_MAX) {
400 logerror("ef_ptr: bad EF_type %d\n", type);
407 ef_ensure_space(int type, int id, int count)
409 while (id >= empfile[type].fids) {
410 if (!ef_extend(type, count))