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)
63 if (ef_check(type) < 0)
66 if ((ep->fd = open(ep->file, mode, 0660)) < 0) {
67 logerror("%s: open failed", ep->file);
71 block = blksize(ep->fd);
76 ep->fids = fsize(ep->fd) / ep->size;
77 if (ep->flags & EFF_MEM)
80 ep->csize = block / ep->size;
81 size = ep->csize * ep->size;
82 ep->cache = malloc(size);
83 if ((ep->cache == NULL) && (size != 0)) {
84 logerror("ef_open: %s malloc(%d) failed\n", ep->file, size);
87 if (ep->flags & EFF_MEM) {
88 if (read(ep->fd, ep->cache, size) != size) {
89 logerror("ef_open: read(%s) failed\n", ep->file);
92 ep->cids = size / ep->size;
98 * Close the file containing objects of the type 'type', flushing the cache
107 if (ef_check(type) < 0)
110 if (ep->cache == NULL) {
111 /* no cache implies never opened */
115 ep->flags &= ~EFF_MEM;
118 if ((r = close(ep->fd)) < 0) {
119 logerror("ef_close: %s close(%d) -> %d", ep->name, ep->fd, r);
125 * Flush the cache of the file containing objects of type 'type' to disk.
134 if (ef_check(type) < 0)
137 if (ep->cache == NULL) {
138 /* no cache implies never opened */
141 size = ep->csize * ep->size;
142 if (ep->mode > 0 && (ep->flags & EFF_MEM)) {
143 if ((r = lseek(ep->fd, 0L, SEEK_SET)) < 0) {
144 logerror("ef_flush: %s cache lseek(%d, 0L, SEEK_SET) -> %d",
145 ep->name, ep->fd, r);
148 if (write(ep->fd, ep->cache, size) != size) {
149 logerror("ef_flush: %s cache write(%d, %p, %d) -> %d",
150 ep->name, ep->fd, ep->cache, ep->size, r);
154 /*ef_zapcache(type); */
159 * Return a pointer the id 'id' of object of type 'type' in the cache.
162 ef_ptr(int type, int id)
166 if (ef_check(type) < 0)
169 if (id < 0 || id >= ep->fids)
171 if ((ep->flags & EFF_MEM) == 0) {
172 logerror("ef_ptr: (%s) only valid for EFF_MEM entries", ep->file);
175 return (ep->cache + ep->size * id);
179 * buffered read. Tries to read a large number of items.
180 * This system won't work if item size is > sizeof buffer area.
183 ef_read(int type, int id, void *into)
188 if (ef_check(type) < 0)
193 if (ep->flags & EFF_MEM) {
196 from = ep->cache + (id * ep->size);
198 if (id >= ep->fids) {
199 ep->fids = fsize(ep->fd) / ep->size;
203 if (ep->baseid + ep->cids <= id || ep->baseid > id)
205 from = ep->cache + (id - ep->baseid) * ep->size;
207 memcpy(into, from, ep->size);
210 ep->postread(id, into);
215 fillcache(struct empfile *ep, int start)
220 lseek(ep->fd, start * ep->size, SEEK_SET);
221 n = read(ep->fd, ep->cache, ep->csize * ep->size);
222 ep->cids = n / ep->size;
226 * buffered write. Modifies read cache (if applicable)
227 * and writes through to disk.
230 ef_write(int type, int id, void *from)
236 if (ef_check(type) < 0)
240 /* largest unit id; this may bite us in large games */
241 logerror("ef_write: type %d id %d is too large!\n", type, id);
244 if ((r = lseek(ep->fd, id * ep->size, SEEK_SET)) < 0) {
245 logerror("ef_write: %s #%d lseek(%d, %d, SEEK_SET) -> %d",
246 ep->name, id, ep->fd, id * ep->size, r);
250 ep->prewrite(id, from);
251 if ((r = write(ep->fd, from, ep->size)) != ep->size) {
252 logerror("ef_write: %s #%d write(%d, %p, %d) -> %d",
253 ep->name, id, ep->fd, from, ep->size, r);
256 if (id >= ep->baseid && id < ep->baseid + ep->cids) {
257 /* update the cache if necessary */
258 to = ep->cache + (id - ep->baseid) * ep->size;
259 memcpy(to, from, ep->size);
262 logerror("WARNING ef_write: expanded %s by more than one id",
266 if (id >= ep->fids) {
267 if (ep->flags & EFF_MEM) {
268 logerror("file %s went beyond %d items; won't be able toread item w/o restart",
271 /* write expanded file; ep->fids = last id + 1 */
279 * Grow the file containing objects of the type 'type' by 'count' objects.
282 ef_extend(int type, int count)
290 if (ef_check(type) < 0)
293 max = ep->fids + count;
295 tmpobj = calloc(1, ep->size);
296 if ((r = lseek(ep->fd, ep->fids * ep->size, SEEK_SET)) < 0) {
297 logerror("ef_extend: %s +#%d lseek(%d, %d, SEEK_SET) -> %d",
298 ep->name, count, ep->fd, ep->fids * ep->size, r);
302 for (cur = ep->fids; cur < max; cur++) {
304 ep->init(cur, tmpobj);
305 if ((r = write(ep->fd, tmpobj, ep->size)) != ep->size) {
306 logerror("ef_extend: %s +#%d write(%d, %p, %d) -> %d",
307 ep->name, count, ep->fd, tmpobj, ep->size, r);
313 if (ep->flags & EFF_MEM) {
314 /* XXX this will cause problems if there are ef_ptrs (to the
315 * old allocated structure) active when we do the re-open */
319 ef_open(type, mode, how);
327 * Mark the cache for the file containing objects of type 'type' as unused.
330 ef_zapcache(int type)
332 struct empfile *ep = &empfile[type];
333 if ((ep->flags & EFF_MEM) == 0) {
342 return empfile[type].cadef;
348 return empfile[type].fids;
354 return empfile[type].flags;
360 if (empfile[type].fd <= 0)
362 return fdate(empfile[type].fd);
366 * Return the filedescriptor used for the file containing objects of type
370 ef_byname(char *name)
377 for (i = 0; i < EF_MAX; i++) {
379 if (strncmp(ef->name, name, min(len, strlen(ef->name))) == 0)
388 if (type < 0 || type >= EF_MAX)
389 return "bad item type";
390 return empfile[type].name;
396 if (type < 0 || type >= EF_MAX) {
397 logerror("ef_ptr: bad EF_type %d\n", type);
404 ef_ensure_space(int type, int id, int count)
406 while (id >= empfile[type].fids) {
407 if (!ef_extend(type, count))