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 * HOW are EFF_OPEN flags to control operation.
54 * Return non-zero on success, zero on failure.
55 * You must call ef_close() before the next ef_open().
58 ef_open(int type, int how)
61 int oflags, fsiz, size;
63 if (ef_check(type) < 0)
65 if (CANT_HAPPEN(how & ~EFF_OPEN))
68 if (CANT_HAPPEN(ep->fd >= 0))
74 oflags |= O_CREAT | O_TRUNC;
78 if ((ep->fd = open(ep->file, oflags, 0660)) < 0) {
79 logerror("%s: open failed", ep->file);
84 ep->flags = (ep->flags & ~EFF_OPEN) | (how ^ ~EFF_CREATE);
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;
93 if (ep->flags & EFF_MEM)
96 ep->csize = max(1, blksize(ep->fd) / ep->size);
97 size = ep->csize * ep->size;
98 ep->cache = malloc(size);
99 if (ep->cache == NULL) {
100 logerror("ef_open: %s malloc(%d) failed\n", ep->file, size);
103 if (ep->flags & EFF_MEM) {
104 if (read(ep->fd, ep->cache, size) != size) {
105 logerror("ef_open: read(%s) failed\n", ep->file);
108 ep->cids = size / ep->size;
114 * Close the file containing objects of the type 'type', flushing the cache
123 if (ef_check(type) < 0)
126 if (ep->cache == NULL) {
127 /* no cache implies never opened */
131 ep->flags &= ~EFF_MEM;
134 if ((r = close(ep->fd)) < 0) {
135 logerror("ef_close: %s close(%d) -> %d", ep->name, ep->fd, r);
142 * Flush the cache of the file containing objects of type 'type' to disk.
151 if (ef_check(type) < 0)
154 if (ep->cache == NULL) {
155 /* no cache implies never opened */
159 * We don't know which cache entries are dirty. ef_write() writes
160 * through, but direct updates through ef_ptr() don't. They are
161 * allowed only with EFF_MEM. Assume the whole cash is dirty
164 if (!(ep->flags & EFF_RDONLY) && (ep->flags & EFF_MEM)) {
165 size = ep->csize * ep->size;
166 if ((r = lseek(ep->fd, 0L, SEEK_SET)) < 0) {
167 logerror("ef_flush: %s cache lseek(%d, 0L, SEEK_SET) -> %d",
168 ep->name, ep->fd, r);
171 if (write(ep->fd, ep->cache, size) != size) {
172 logerror("ef_flush: %s cache write(%d, %p, %d) -> %d",
173 ep->name, ep->fd, ep->cache, ep->size, r);
181 * Return a pointer the id 'id' of object of type 'type' in the cache.
184 ef_ptr(int type, int id)
188 if (ef_check(type) < 0)
191 if (id < 0 || id >= ep->fids)
193 if ((ep->flags & EFF_MEM) == 0) {
194 logerror("ef_ptr: (%s) only valid for EFF_MEM entries", ep->file);
197 return ep->cache + ep->size * id;
201 * buffered read. Tries to read a large number of items.
202 * This system won't work if item size is > sizeof buffer area.
205 ef_read(int type, int id, void *into)
210 if (ef_check(type) < 0)
215 if (ep->flags & EFF_MEM) {
218 from = ep->cache + (id * ep->size);
220 if (id >= ep->fids) {
221 ep->fids = fsize(ep->fd) / ep->size;
225 if (ep->baseid + ep->cids <= id || ep->baseid > id)
227 from = ep->cache + (id - ep->baseid) * ep->size;
229 memcpy(into, from, ep->size);
232 ep->postread(id, into);
237 fillcache(struct empfile *ep, int start)
242 lseek(ep->fd, start * ep->size, SEEK_SET);
243 n = read(ep->fd, ep->cache, ep->csize * ep->size);
244 ep->cids = n / ep->size;
248 * buffered write. Modifies read cache (if applicable)
249 * and writes through to disk.
252 ef_write(int type, int id, void *from)
258 if (ef_check(type) < 0)
262 /* largest unit id; this may bite us in large games */
263 logerror("ef_write: type %d id %d is too large!\n", type, id);
266 if ((r = lseek(ep->fd, id * ep->size, SEEK_SET)) < 0) {
267 logerror("ef_write: %s #%d lseek(%d, %d, SEEK_SET) -> %d",
268 ep->name, id, ep->fd, id * ep->size, r);
272 ep->prewrite(id, from);
273 if ((r = write(ep->fd, from, ep->size)) != ep->size) {
274 logerror("ef_write: %s #%d write(%d, %p, %d) -> %d",
275 ep->name, id, ep->fd, from, ep->size, r);
278 if (id >= ep->baseid && id < ep->baseid + ep->cids) {
279 /* update the cache if necessary */
280 to = ep->cache + (id - ep->baseid) * ep->size;
281 memcpy(to, from, ep->size);
283 CANT_HAPPEN(id > ep->fids);
284 if (id >= ep->fids) {
285 if (ep->flags & EFF_MEM) {
286 logerror("file %s went beyond %d items; won't be able toread item w/o restart",
289 /* write expanded file; ep->fids = last id + 1 */
297 * Grow the file containing objects of the type 'type' by 'count' objects.
300 ef_extend(int type, int count)
308 if (ef_check(type) < 0)
311 max = ep->fids + count;
313 tmpobj = calloc(1, ep->size);
314 if ((r = lseek(ep->fd, ep->fids * ep->size, SEEK_SET)) < 0) {
315 logerror("ef_extend: %s +#%d lseek(%d, %d, SEEK_SET) -> %d",
316 ep->name, count, ep->fd, ep->fids * ep->size, r);
320 for (cur = ep->fids; cur < max; cur++) {
322 ep->init(cur, tmpobj);
323 if ((r = write(ep->fd, tmpobj, ep->size)) != ep->size) {
324 logerror("ef_extend: %s +#%d write(%d, %p, %d) -> %d",
325 ep->name, count, ep->fd, tmpobj, ep->size, r);
331 if (ep->flags & EFF_MEM) {
332 /* XXX this will cause problems if there are ef_ptrs (to the
333 * old allocated structure) active when we do the re-open */
344 * Mark the cache for the file containing objects of type 'type' as unused.
347 ef_zapcache(int type)
349 struct empfile *ep = &empfile[type];
350 if ((ep->flags & EFF_MEM) == 0) {
359 return empfile[type].cadef;
365 return empfile[type].fids;
371 return empfile[type].flags;
377 if (empfile[type].fd <= 0)
379 return fdate(empfile[type].fd);
383 * Search empfile[0..EF_MAX-1] for element named NAME.
384 * Return its index in empfile[] if found, else -1.
387 ef_byname(char *name)
394 for (i = 0; i < EF_MAX; i++) {
396 if (strncmp(ef->name, name, min(len, strlen(ef->name))) == 0)
405 if (type < 0 || type >= EF_MAX)
406 return "bad item type";
407 return empfile[type].name;
413 if (type < 0 || type >= EF_MAX) {
414 logerror("ef_ptr: bad EF_type %d\n", type);
421 ef_ensure_space(int type, int id, int count)
423 while (id >= empfile[type].fids) {
424 if (!ef_extend(type, count))