]> git.pond.sub.org Git - empserver/blob - src/lib/common/file.c
Cleanup #includes of (mostly a long time) unused header files.
[empserver] / src / lib / common / file.c
1 /*
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
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  file.c: Misc. operations on files
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  *     Steve McClure, 2000
33  */
34
35 #include <string.h>
36 #include <fcntl.h>
37 #include <signal.h>
38 #include <stdlib.h>
39 #if !defined(_WIN32)
40 #include <unistd.h>
41 #endif
42 #include "misc.h"
43 #include "nsc.h"
44 #include "file.h"
45 #include "common.h"
46 #include "gen.h"
47
48
49 static void fillcache(struct empfile *ep, int start);
50
51 int
52 ef_open(int type, int mode, int how)
53 {
54     register struct empfile *ep;
55     static int block;
56     int size;
57
58 #if defined(_WIN32)
59     mode |= _O_BINARY;
60 #endif
61     if (ef_check(type) < 0)
62         return 0;
63     ep = &empfile[type];
64     if ((ep->fd = open(ep->file, mode, 0660)) < 0) {
65         logerror("%s: open failed", ep->file);
66         return 0;
67     }
68     if (block == 0)
69         block = blksize(ep->fd);
70     ep->baseid = 0;
71     ep->cids = 0;
72     ep->mode = mode;
73     ep->flags |= how;
74     ep->fids = fsize(ep->fd) / ep->size;
75     if (ep->flags & EFF_MEM)
76         ep->csize = ep->fids;
77     else
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);
83         return 0;
84     }
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);
88             return 0;
89         }
90         ep->cids = size / ep->size;
91     }
92     return 1;
93 }
94
95 int
96 ef_close(int type)
97 {
98     register struct empfile *ep;
99     int r;
100
101     if (ef_check(type) < 0)
102         return 0;
103     ep = &empfile[type];
104     if (ep->cache == 0) {
105         /* no cache implies never opened */
106         return 0;
107     }
108     ef_flush(type);
109     ep->flags &= ~EFF_MEM;
110     free(ep->cache);
111     ep->cache = 0;
112     if ((r = close(ep->fd)) < 0) {
113         logerror("ef_close: %s close(%d) -> %d", ep->name, ep->fd, r);
114     }
115     return 1;
116 }
117
118 int
119 ef_flush(int type)
120 {
121     register struct empfile *ep;
122     int size;
123     int r;
124
125     if (ef_check(type) < 0)
126         return 0;
127     ep = &empfile[type];
128     if (ep->cache == 0) {
129         /* no cache implies never opened */
130         return 0;
131     }
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);
137             return 0;
138         }
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);
142             return 0;
143         }
144     }
145     /*ef_zapcache(type); */
146     return 1;
147 }
148
149 s_char *
150 ef_ptr(int type, int id)
151 {
152     register struct empfile *ep;
153
154     if (ef_check(type) < 0)
155         return 0;
156     ep = &empfile[type];
157     if (id < 0 || id >= ep->fids)
158         return 0;
159     if ((ep->flags & EFF_MEM) == 0) {
160         logerror("ef_ptr: (%s) only valid for EFF_MEM entries", ep->file);
161         return 0;
162     }
163     return (s_char *)(ep->cache + ep->size * id);
164 }
165
166 /*
167  * buffered read.  Tries to read a large number of items.
168  * This system won't work if item size is > sizeof buffer area.
169  */
170 int
171 ef_read(int type, int id, void *ptr)
172 {
173     register struct empfile *ep;
174     void *from;
175
176     if (ef_check(type) < 0)
177         return 0;
178     ep = &empfile[type];
179     if (id < 0)
180         return 0;
181     if (ep->flags & EFF_MEM) {
182         if (id >= ep->fids)
183             return 0;
184         from = ep->cache + (id * ep->size);
185     } else {
186         if (id >= ep->fids) {
187             ep->fids = fsize(ep->fd) / ep->size;
188             if (id >= ep->fids)
189                 return 0;
190         }
191         if (ep->baseid + ep->cids <= id || ep->baseid > id)
192             fillcache(ep, id);
193         from = ep->cache + (id - ep->baseid) * ep->size;
194     }
195     memcpy(ptr, from, ep->size);
196
197     if (ep->postread)
198         ep->postread(id, ptr);
199     return 1;
200 }
201
202 static void
203 fillcache(struct empfile *ep, int start)
204 {
205     int n;
206
207     ep->baseid = 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;
211 }
212
213 #ifdef notdef
214 /*
215  * no-buffered read
216  * zaps read cache
217  */
218 int
219 ef_nbread(int type, int id, void *ptr)
220 {
221     register struct empfile *ep;
222     int r;
223
224     if (ef_check(type) < 0)
225         return 0;
226     ep = &empfile[type];
227     if (id < 0)
228         return 0;
229     if (id >= ep->fids) {
230         ep->fids = fsize(ep->fd) / ep->size;
231         if (id >= ep->fids)
232             return 0;
233     }
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);
237         return 0;
238     }
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);
242         return 0;
243     }
244     ef_zapcache(type);
245     if (ep->postread)
246         ep->postread(id, ptr);
247     return 1;
248 }
249 #endif
250
251 /*
252  * buffered write.  Modifies read cache (if applicable)
253  * and writes through to disk.
254  */
255 int
256 ef_write(int type, int id, void *ptr)
257 {
258     register int r;
259     register struct empfile *ep;
260     s_char *to;
261
262     if (ef_check(type) < 0)
263         return 0;
264     ep = &empfile[type];
265     if (id > 65536) {
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);
268         return 0;
269     }
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);
273         return 0;
274     }
275     if (ep->prewrite)
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);
280         return 0;
281     }
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);
286     }
287     if (id > ep->fids) {
288         logerror("WARNING ef_write: expanded %s by more than one id",
289                  ep->name);
290         log_last_commands();
291     }
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",
295                      ep->name, ep->fids);
296         } else {
297             /* write expanded file; ep->fids = last id + 1 */
298             ep->fids = id + 1;
299         }
300     }
301     return 1;
302 }
303
304 #ifdef notdef
305 /*
306  * no-buffered write
307  * zaps read cache
308  */
309 int
310 ef_nbwrite(int type, int id, void *ptr)
311 {
312     register struct empfile *ep;
313     register int r;
314
315     if (ef_check(type) < 0)
316         return 0;
317     ep = &empfile[type];
318     if (id > 65536) {
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);
321         return 0;
322     }
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);
326         return 0;
327     }
328     if (ep->prewrite)
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);
333         return 0;
334     }
335     ef_zapcache(type);
336     if (id >= ep->fids) {
337         /* write expanded file; ep->fids = last id + 1 */
338         ep->fids = id + 1;
339     }
340     return 1;
341 }
342 #endif
343
344 int
345 ef_extend(int type, int count)
346 {
347     register struct empfile *ep;
348     char *ptr;
349     int cur, max;
350     int mode, how;
351     int r;
352
353     if (ef_check(type) < 0)
354         return 0;
355     ep = &empfile[type];
356     max = ep->fids + count;
357     cur = ep->fids;
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);
362         return 0;
363     }
364     for (cur = ep->fids; cur < max; cur++) {
365         if (ep->init)
366             ep->init(cur, ptr);
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);
370             return 0;
371         }
372     }
373     free(ptr);
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 */
377         mode = ep->mode;
378         how = ep->flags;
379         ef_close(type);
380         ef_open(type, mode, how);
381     } else {
382         ep->fids += count;
383     }
384     return 1;
385 }
386
387 void
388 ef_zapcache(int type)
389 {
390     register struct empfile *ep = &empfile[type];
391     if ((ep->flags & EFF_MEM) == 0) {
392         ep->cids = 0;
393         ep->baseid = -1;
394     }
395 }
396
397 struct castr *
398 ef_cadef(int type)
399 {
400     return empfile[type].cadef;
401 }
402
403 int
404 ef_nelem(int type)
405 {
406     return empfile[type].fids;
407 }
408
409 int
410 ef_flags(int type)
411 {
412     return empfile[type].flags;
413 }
414
415 time_t
416 ef_mtime(int type)
417 {
418     if (empfile[type].fd <= 0)
419         return 0;
420     return fdate(empfile[type].fd);
421 }
422
423 u_short *
424 ef_items(int type, void *sp)
425 {
426     register struct empfile *ef;
427
428     if (ef_check(type) < 0)
429         return 0;
430     ef = &empfile[type];
431     if ((ef->flags & EFF_COM) == 0)
432         return 0;
433     return (u_short *)((char *)sp + ef->itemoffs);
434 }
435
436 int
437 ef_byname(s_char *name)
438 {
439     register struct empfile *ef;
440     register int i;
441     int len;
442
443     len = strlen(name);
444     for (i = 0; i < EF_MAX; i++) {
445         ef = &empfile[i];
446         if (strncmp(ef->name, name, min(len, strlen(ef->name))) == 0)
447             return i;
448     }
449     return -1;
450 }
451
452 s_char *
453 ef_nameof(int type)
454 {
455     if (type < 0 || type >= EF_MAX)
456         return "bad item type";
457     return empfile[type].name;
458 }
459
460 int
461 ef_check(int type)
462 {
463     if (type < 0 || type >= EF_MAX) {
464         logerror("ef_ptr: bad EF_type %d\n", type);
465         return -1;
466     }
467     return 0;
468 }
469
470 int
471 ef_ensure_space(int type, int id, int count)
472 {
473     while (id >= empfile[type].fids) {
474         if (!ef_extend(type, count))
475             return 0;
476     }
477     return 1;
478 }