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 ef_open(int type, int mode, int how)
54 register struct empfile *ep;
61 if (ef_check(type) < 0)
64 if ((ep->fd = open(ep->file, mode, 0660)) < 0) {
65 logerror("%s: open failed", ep->file);
69 block = blksize(ep->fd);
74 ep->fids = fsize(ep->fd) / ep->size;
75 if (ep->flags & EFF_MEM)
78 ep->csize = block / ep->size;
79 size = ep->csize * ep->size;
80 ep->cache = (s_char *)malloc(size);
81 if ((ep->cache == 0) && (size != 0)) {
82 logerror("ef_open: %s malloc(%d) failed\n", ep->file, size);
85 if (ep->flags & EFF_MEM) {
86 if (read(ep->fd, ep->cache, size) != size) {
87 logerror("ef_open: read(%s) failed\n", ep->file);
90 ep->cids = size / ep->size;
98 register struct empfile *ep;
101 if (ef_check(type) < 0)
104 if (ep->cache == 0) {
105 /* no cache implies never opened */
109 ep->flags &= ~EFF_MEM;
112 if ((r = close(ep->fd)) < 0) {
113 logerror("ef_close: %s close(%d) -> %d", ep->name, ep->fd, r);
121 register struct empfile *ep;
125 if (ef_check(type) < 0)
128 if (ep->cache == 0) {
129 /* no cache implies never opened */
132 size = ep->csize * ep->size;
133 if (ep->mode > 0 && (ep->flags & EFF_MEM)) {
134 if ((r = lseek(ep->fd, 0L, 0)) < 0) {
135 logerror("ef_flush: %s cache lseek(%d, 0L, 0) -> %d",
136 ep->name, ep->fd, r);
139 if (write(ep->fd, ep->cache, size) != size) {
140 logerror("ef_flush: %s cache write(%d, %p, %d) -> %d",
141 ep->name, ep->fd, (void *)ep->cache, ep->size, r);
145 /*ef_zapcache(type); */
150 ef_ptr(int type, int id)
152 register struct empfile *ep;
154 if (ef_check(type) < 0)
157 if (id < 0 || id >= ep->fids)
159 if ((ep->flags & EFF_MEM) == 0) {
160 logerror("ef_ptr: (%s) only valid for EFF_MEM entries", ep->file);
163 return (s_char *)(ep->cache + ep->size * id);
167 * buffered read. Tries to read a large number of items.
168 * This system won't work if item size is > sizeof buffer area.
171 ef_read(int type, int id, void *ptr)
173 register struct empfile *ep;
176 if (ef_check(type) < 0)
181 if (ep->flags & EFF_MEM) {
184 from = ep->cache + (id * ep->size);
186 if (id >= ep->fids) {
187 ep->fids = fsize(ep->fd) / ep->size;
191 if (ep->baseid + ep->cids <= id || ep->baseid > id)
193 from = ep->cache + (id - ep->baseid) * ep->size;
195 memcpy(ptr, from, ep->size);
198 ep->postread(id, ptr);
203 fillcache(struct empfile *ep, int start)
208 lseek(ep->fd, start * ep->size, 0);
209 n = read(ep->fd, ep->cache, ep->csize * ep->size);
210 ep->cids = n / ep->size;
219 ef_nbread(int type, int id, void *ptr)
221 register struct empfile *ep;
224 if (ef_check(type) < 0)
229 if (id >= ep->fids) {
230 ep->fids = fsize(ep->fd) / ep->size;
234 if ((r = lseek(ep->fd, id * ep->size, 0)) < 0) {
235 logerror("ef_nbread: %s #%d lseek(%d, %d, 0) -> %d",
236 ep->name, id, ep->fd, id * ep->size, r);
239 if ((r = read(ep->fd, ptr, ep->size)) != ep->size) {
240 logerror("ef_nbread: %s #%d read(%d, %x, %d) -> %d",
241 ep->name, id, ep->fd, ptr, ep->size, r);
246 ep->postread(id, ptr);
252 * buffered write. Modifies read cache (if applicable)
253 * and writes through to disk.
256 ef_write(int type, int id, void *ptr)
259 register struct empfile *ep;
262 if (ef_check(type) < 0)
266 /* largest unit id; this may bite us in large games */
267 logerror("ef_write: type %d id %d is too large!\n", type, id);
270 if ((r = lseek(ep->fd, id * ep->size, 0)) < 0) {
271 logerror("ef_write: %s #%d lseek(%d, %d, 0) -> %d",
272 ep->name, id, ep->fd, id * ep->size, r);
276 ep->prewrite(id, ptr);
277 if ((r = write(ep->fd, ptr, ep->size)) != ep->size) {
278 logerror("ef_write: %s #%d write(%d, %p, %d) -> %d",
279 ep->name, id, ep->fd, (void *)ptr, ep->size, r);
282 if (id >= ep->baseid && id < ep->baseid + ep->cids) {
283 /* update the cache if necessary */
284 to = ep->cache + (id - ep->baseid) * ep->size;
285 memcpy(to, ptr, ep->size);
288 logerror("WARNING ef_write: expanded %s by more than one id",
292 if (id >= ep->fids) {
293 if (ep->flags & EFF_MEM) {
294 logerror("file %s went beyond %d items; won't be able toread item w/o restart",
297 /* write expanded file; ep->fids = last id + 1 */
310 ef_nbwrite(int type, int id, void *ptr)
312 register struct empfile *ep;
315 if (ef_check(type) < 0)
319 /* largest unit id; this may bite us in large games */
320 logerror("ef_nbwrite: %s id %d is too large!\n", ep->name, id);
323 if ((r = lseek(ep->fd, id * ep->size, 0)) < 0) {
324 logerror("ef_nbwrite: %s #%d lseek(%d, %d, 0) -> %d",
325 ep->name, id, ep->fd, id * ep->size, r);
329 ep->prewrite(id, ptr);
330 if ((r = write(ep->fd, ptr, ep->size)) != ep->size) {
331 logerror("ef_nbwrite: %s #%d write(%d, %x, %d) -> %d",
332 ep->name, id, ep->fd, ptr, ep->size, r);
336 if (id >= ep->fids) {
337 /* write expanded file; ep->fids = last id + 1 */
345 ef_extend(int type, int count)
347 register struct empfile *ep;
353 if (ef_check(type) < 0)
356 max = ep->fids + count;
358 ptr = (s_char *)calloc(1, ep->size);
359 if ((r = lseek(ep->fd, ep->fids * ep->size, 0)) < 0) {
360 logerror("ef_extend: %s +#%d lseek(%d, %d, 0) -> %d",
361 ep->name, count, ep->fd, ep->fids * ep->size, r);
364 for (cur = ep->fids; cur < max; cur++) {
367 if ((r = write(ep->fd, ptr, ep->size)) != ep->size) {
368 logerror("ef_extend: %s +#%d write(%d, %p, %d) -> %d",
369 ep->name, count, ep->fd, (void *)ptr, ep->size, r);
374 if (ep->flags & EFF_MEM) {
375 /* XXX this will cause problems if there are ef_ptrs (to the
376 * old allocated structure) active when we do the re-open */
380 ef_open(type, mode, how);
388 ef_zapcache(int type)
390 register struct empfile *ep = &empfile[type];
391 if ((ep->flags & EFF_MEM) == 0) {
400 return empfile[type].cadef;
406 return empfile[type].fids;
412 return empfile[type].flags;
418 if (empfile[type].fd <= 0)
420 return fdate(empfile[type].fd);
424 ef_items(int type, void *sp)
426 register struct empfile *ef;
428 if (ef_check(type) < 0)
431 if ((ef->flags & EFF_COM) == 0)
433 return (u_short *)((char *)sp + ef->itemoffs);
437 ef_byname(s_char *name)
439 register struct empfile *ef;
444 for (i = 0; i < EF_MAX; i++) {
446 if (strncmp(ef->name, name, min(len, strlen(ef->name))) == 0)
455 if (type < 0 || type >= EF_MAX)
456 return "bad item type";
457 return empfile[type].name;
463 if (type < 0 || type >= EF_MAX) {
464 logerror("ef_ptr: bad EF_type %d\n", type);
471 ef_ensure_space(int type, int id, int count)
473 while (id >= empfile[type].fids) {
474 if (!ef_extend(type, count))