2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, 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 file containing objects of the type 'type' with mode 'mode'.
53 * 'how' sets whether the file should be cached (EFF_MEM), or type specific
54 * flags (EFF_XY,EFF_OWNER,EFF,EFF_GROUP).
57 ef_open(int type, int mode, int how)
66 if (ef_check(type) < 0)
69 if ((ep->fd = open(ep->file, mode, 0660)) < 0) {
70 logerror("%s: open failed", ep->file);
74 block = blksize(ep->fd);
79 ep->fids = fsize(ep->fd) / ep->size;
80 if (ep->flags & EFF_MEM)
83 ep->csize = block / ep->size;
84 size = ep->csize * ep->size;
85 ep->cache = malloc(size);
86 if ((ep->cache == NULL) && (size != 0)) {
87 logerror("ef_open: %s malloc(%d) failed\n", ep->file, size);
90 if (ep->flags & EFF_MEM) {
91 if (read(ep->fd, ep->cache, size) != size) {
92 logerror("ef_open: read(%s) failed\n", ep->file);
95 ep->cids = size / ep->size;
101 * Close the file containing objects of the type 'type', flushing the cache
110 if (ef_check(type) < 0)
113 if (ep->cache == NULL) {
114 /* no cache implies never opened */
118 ep->flags &= ~EFF_MEM;
121 if ((r = close(ep->fd)) < 0) {
122 logerror("ef_close: %s close(%d) -> %d", ep->name, ep->fd, r);
128 * Flush the cache of the file containing objects of type 'type' to disk.
137 if (ef_check(type) < 0)
140 if (ep->cache == NULL) {
141 /* no cache implies never opened */
144 size = ep->csize * ep->size;
145 if (ep->mode > 0 && (ep->flags & EFF_MEM)) {
146 if ((r = lseek(ep->fd, 0L, SEEK_SET)) < 0) {
147 logerror("ef_flush: %s cache lseek(%d, 0L, SEEK_SET) -> %d",
148 ep->name, ep->fd, r);
151 if (write(ep->fd, ep->cache, size) != size) {
152 logerror("ef_flush: %s cache write(%d, %p, %d) -> %d",
153 ep->name, ep->fd, ep->cache, ep->size, r);
157 /*ef_zapcache(type); */
162 * Return a pointer the id 'id' of object of type 'type' in the cache.
165 ef_ptr(int type, int id)
169 if (ef_check(type) < 0)
172 if (id < 0 || id >= ep->fids)
174 if ((ep->flags & EFF_MEM) == 0) {
175 logerror("ef_ptr: (%s) only valid for EFF_MEM entries", ep->file);
178 return (ep->cache + ep->size * id);
182 * buffered read. Tries to read a large number of items.
183 * This system won't work if item size is > sizeof buffer area.
186 ef_read(int type, int id, void *into)
191 if (ef_check(type) < 0)
196 if (ep->flags & EFF_MEM) {
199 from = ep->cache + (id * ep->size);
201 if (id >= ep->fids) {
202 ep->fids = fsize(ep->fd) / ep->size;
206 if (ep->baseid + ep->cids <= id || ep->baseid > id)
208 from = ep->cache + (id - ep->baseid) * ep->size;
210 memcpy(into, from, ep->size);
213 ep->postread(id, into);
218 fillcache(struct empfile *ep, int start)
223 lseek(ep->fd, start * ep->size, SEEK_SET);
224 n = read(ep->fd, ep->cache, ep->csize * ep->size);
225 ep->cids = n / ep->size;
229 * buffered write. Modifies read cache (if applicable)
230 * and writes through to disk.
233 ef_write(int type, int id, void *from)
239 if (ef_check(type) < 0)
243 /* largest unit id; this may bite us in large games */
244 logerror("ef_write: type %d id %d is too large!\n", type, id);
247 if ((r = lseek(ep->fd, id * ep->size, SEEK_SET)) < 0) {
248 logerror("ef_write: %s #%d lseek(%d, %d, SEEK_SET) -> %d",
249 ep->name, id, ep->fd, id * ep->size, r);
253 ep->prewrite(id, from);
254 if ((r = write(ep->fd, from, ep->size)) != ep->size) {
255 logerror("ef_write: %s #%d write(%d, %p, %d) -> %d",
256 ep->name, id, ep->fd, from, ep->size, r);
259 if (id >= ep->baseid && id < ep->baseid + ep->cids) {
260 /* update the cache if necessary */
261 to = ep->cache + (id - ep->baseid) * ep->size;
262 memcpy(to, from, ep->size);
265 logerror("WARNING ef_write: expanded %s by more than one id",
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 * Return the filedescriptor used for the file containing objects of type
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))