]> git.pond.sub.org Git - empserver/blob - src/lib/common/file.c
Indented with src/scripts/indent-emp.
[empserver] / src / lib / common / file.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 #ifdef Rel4
36 #include <string.h>
37 #endif /* Rel4 */
38 #include <fcntl.h>
39 #include <signal.h>
40 #include <stdlib.h>
41 #if !defined(_WIN32)
42 #include <unistd.h>
43 #endif
44 #include "misc.h"
45 #include "xy.h"
46 #include "nsc.h"
47 #include "file.h"
48 #include "match.h"
49 #include "struct.h"
50 #include "common.h"
51 #include "gen.h"
52
53 #ifdef Rel4
54 #include <unistd.h>
55 #endif
56
57 static void fillcache(struct empfile *ep, int start);
58
59 int
60 ef_open(int type, int mode, int how)
61 {
62     register struct empfile *ep;
63     static int block;
64     int size;
65
66 #if defined(_WIN32)
67     mode |= _O_BINARY;
68 #endif
69     if (ef_check(type) < 0)
70         return 0;
71     ep = &empfile[type];
72     if ((ep->fd = open(ep->file, mode, 0660)) < 0) {
73         logerror("%s: open failed", ep->file);
74         return 0;
75     }
76     if (block == 0)
77         block = blksize(ep->fd);
78     ep->baseid = 0;
79     ep->cids = 0;
80     ep->mode = mode;
81     ep->flags |= how;
82     ep->fids = fsize(ep->fd) / ep->size;
83     if (ep->flags & EFF_MEM)
84         ep->csize = ep->fids;
85     else
86         ep->csize = block / ep->size;
87     size = ep->csize * ep->size;
88     ep->cache = (s_char *)malloc(size);
89     if ((ep->cache == 0) && (size != 0)) {
90         logerror("ef_open: %s malloc(%d) failed\n", ep->file, size);
91         return 0;
92     }
93     if (ep->flags & EFF_MEM) {
94         if (read(ep->fd, ep->cache, size) != size) {
95             logerror("ef_open: read(%s) failed\n", ep->file);
96             return 0;
97         }
98         ep->cids = size / ep->size;
99     }
100     return 1;
101 }
102
103 int
104 ef_close(int type)
105 {
106     register struct empfile *ep;
107     int r;
108
109     if (ef_check(type) < 0)
110         return 0;
111     ep = &empfile[type];
112     if (ep->cache == 0) {
113         /* no cache implies never opened */
114         return 0;
115     }
116     ef_flush(type);
117     ep->flags &= ~EFF_MEM;
118     free(ep->cache);
119     ep->cache = 0;
120     if ((r = close(ep->fd)) < 0) {
121         logerror("ef_close: %s close(%d) -> %d", ep->name, ep->fd, r);
122     }
123     return 1;
124 }
125
126 int
127 ef_flush(int type)
128 {
129     register struct empfile *ep;
130     int size;
131     int r;
132
133     if (ef_check(type) < 0)
134         return 0;
135     ep = &empfile[type];
136     if (ep->cache == 0) {
137         /* no cache implies never opened */
138         return 0;
139     }
140     size = ep->csize * ep->size;
141     if (ep->mode > 0 && (ep->flags & EFF_MEM)) {
142         if ((r = lseek(ep->fd, 0L, 0)) < 0) {
143             logerror("ef_flush: %s cache lseek(%d, 0L, 0) -> %d",
144                      ep->name, ep->fd, r);
145             return 0;
146         }
147         if (write(ep->fd, ep->cache, size) != size) {
148             logerror("ef_flush: %s cache write(%d, %x, %d) -> %d",
149                      ep->name, ep->fd, ep->cache, ep->size, r);
150             return 0;
151         }
152     }
153     /*ef_zapcache(type); */
154     return 1;
155 }
156
157 s_char *
158 ef_ptr(int type, int id)
159 {
160     register struct empfile *ep;
161
162     if (ef_check(type) < 0)
163         return 0;
164     ep = &empfile[type];
165     if (id < 0 || id >= ep->fids)
166         return 0;
167     if ((ep->flags & EFF_MEM) == 0) {
168         logerror("ef_ptr: (%s) only valid for EFF_MEM entries", ep->file);
169         return 0;
170     }
171     return (s_char *)(ep->cache + ep->size * id);
172 }
173
174 /*
175  * buffered read.  Tries to read a large number of items.
176  * This system won't work if item size is > sizeof buffer area.
177  */
178 int
179 ef_read(int type, int id, caddr_t ptr)
180 {
181     register struct empfile *ep;
182     caddr_t from;
183
184     if (ef_check(type) < 0)
185         return 0;
186     ep = &empfile[type];
187     if (id < 0)
188         return 0;
189     if (ep->flags & EFF_MEM) {
190         if (id >= ep->fids)
191             return 0;
192         from = ep->cache + (id * ep->size);
193     } else {
194         if (id >= ep->fids) {
195             ep->fids = fsize(ep->fd) / ep->size;
196             if (id >= ep->fids)
197                 return 0;
198         }
199         if (ep->baseid + ep->cids <= id || ep->baseid > id)
200             fillcache(ep, id);
201         from = ep->cache + (id - ep->baseid) * ep->size;
202     }
203     bcopy(from, ptr, ep->size);
204
205     if (ep->postread)
206         ep->postread(id, ptr);
207     return 1;
208 }
209
210 static void
211 fillcache(struct empfile *ep, int start)
212 {
213     int n;
214
215     ep->baseid = start;
216     lseek(ep->fd, start * ep->size, 0);
217     n = read(ep->fd, ep->cache, ep->csize * ep->size);
218     ep->cids = n / ep->size;
219 }
220
221 #ifdef notdef
222 /*
223  * no-buffered read
224  * zaps read cache
225  */
226 int
227 ef_nbread(int type, int id, caddr_t ptr)
228 {
229     register struct empfile *ep;
230     int r;
231
232     if (ef_check(type) < 0)
233         return 0;
234     ep = &empfile[type];
235     if (id < 0)
236         return 0;
237     if (id >= ep->fids) {
238         ep->fids = fsize(ep->fd) / ep->size;
239         if (id >= ep->fids)
240             return 0;
241     }
242     if ((r = lseek(ep->fd, id * ep->size, 0)) < 0) {
243         logerror("ef_nbread: %s #%d lseek(%d, %d, 0) -> %d",
244                  ep->name, id, ep->fd, id * ep->size, r);
245         return 0;
246     }
247     if ((r = read(ep->fd, ptr, ep->size)) != ep->size) {
248         logerror("ef_nbread: %s #%d read(%d, %x, %d) -> %d",
249                  ep->name, id, ep->fd, ptr, ep->size, r);
250         return 0;
251     }
252     ef_zapcache(type);
253     if (ep->postread)
254         ep->postread(id, ptr);
255     return 1;
256 }
257 #endif
258
259 /*
260  * buffered write.  Modifies read cache (if applicable)
261  * and writes through to disk.
262  */
263 int
264 ef_write(int type, int id, caddr_t ptr)
265 {
266     register int r;
267     register struct empfile *ep;
268     s_char *to;
269
270     if (ef_check(type) < 0)
271         return 0;
272     ep = &empfile[type];
273     if (id > 65536) {
274         /* largest unit id; this may bite us in large games */
275         logerror("ef_write: type %d id %d is too large!\n", type, id);
276         return 0;
277     }
278     if ((r = lseek(ep->fd, id * ep->size, 0)) < 0) {
279         logerror("ef_write: %s #%d lseek(%d, %d, 0) -> %d",
280                  ep->name, id, ep->fd, id * ep->size, r);
281         return 0;
282     }
283     if (ep->prewrite)
284         ep->prewrite(id, ptr);
285     if ((r = write(ep->fd, ptr, ep->size)) != ep->size) {
286         logerror("ef_write: %s #%d write(%d, %x, %d) -> %d",
287                  ep->name, id, ep->fd, ptr, ep->size, r);
288         return 0;
289     }
290     if (id >= ep->baseid && id < ep->baseid + ep->cids) {
291         /* update the cache if necessary */
292         to = ep->cache + (id - ep->baseid) * ep->size;
293         bcopy(ptr, to, ep->size);
294     }
295     if (id > ep->fids) {
296         logerror("WARNING ef_write: expanded %s by more than one id",
297                  ep->name);
298         log_last_commands();
299     }
300     if (id >= ep->fids) {
301         if (ep->flags & EFF_MEM) {
302             logerror
303                 ("file %s went beyond %d items; won't be able toread item w/o restart",
304                  ep->name, ep->fids);
305         } else {
306             /* write expanded file; ep->fids = last id + 1 */
307             ep->fids = id + 1;
308         }
309     }
310     return 1;
311 }
312
313 #ifdef notdef
314 /*
315  * no-buffered write
316  * zaps read cache
317  */
318 int
319 ef_nbwrite(int type, int id, caddr_t ptr)
320 {
321     register struct empfile *ep;
322     register int r;
323
324     if (ef_check(type) < 0)
325         return 0;
326     ep = &empfile[type];
327     if (id > 65536) {
328         /* largest unit id; this may bite us in large games */
329         logerror("ef_nbwrite: %s id %d is too large!\n", ep->name, id);
330         return 0;
331     }
332     if ((r = lseek(ep->fd, id * ep->size, 0)) < 0) {
333         logerror("ef_nbwrite: %s #%d lseek(%d, %d, 0) -> %d",
334                  ep->name, id, ep->fd, id * ep->size, r);
335         return 0;
336     }
337     if (ep->prewrite)
338         ep->prewrite(id, ptr);
339     if ((r = write(ep->fd, ptr, ep->size)) != ep->size) {
340         logerror("ef_nbwrite: %s #%d write(%d, %x, %d) -> %d",
341                  ep->name, id, ep->fd, ptr, ep->size, r);
342         return 0;
343     }
344     ef_zapcache(type);
345     if (id >= ep->fids) {
346         /* write expanded file; ep->fids = last id + 1 */
347         ep->fids = id + 1;
348     }
349     return 1;
350 }
351 #endif
352
353 int
354 ef_extend(int type, int count)
355 {
356     register struct empfile *ep;
357     char *ptr;
358     int cur, max;
359     int mode, how;
360     int r;
361
362     if (ef_check(type) < 0)
363         return 0;
364     ep = &empfile[type];
365     max = ep->fids + count;
366     cur = ep->fids;
367     ptr = (s_char *)calloc(1, ep->size);
368     if ((r = lseek(ep->fd, ep->fids * ep->size, 0)) < 0) {
369         logerror("ef_extend: %s +#%d lseek(%d, %d, 0) -> %d",
370                  ep->name, count, ep->fd, ep->fids * ep->size, r);
371         return 0;
372     }
373     for (cur = ep->fids; cur < max; cur++) {
374         if (ep->init)
375             ep->init(cur, ptr);
376         if ((r = write(ep->fd, ptr, ep->size)) != ep->size) {
377             logerror("ef_extend: %s +#%d write(%d, %x, %d) -> %d",
378                      ep->name, count, ep->fd, ptr, ep->size, r);
379             return 0;
380         }
381     }
382     free(ptr);
383     if (ep->flags & EFF_MEM) {
384         /* XXX this will cause problems if there are ef_ptrs (to the
385          * old allocated structure) active when we do the re-open */
386         mode = ep->mode;
387         how = ep->flags;
388         ef_close(type);
389         ef_open(type, mode, how);
390     } else {
391         ep->fids += count;
392     }
393     return 1;
394 }
395
396 void
397 ef_zapcache(int type)
398 {
399     register struct empfile *ep = &empfile[type];
400     if ((ep->flags & EFF_MEM) == 0) {
401         ep->cids = 0;
402         ep->baseid = -1;
403     }
404 }
405
406 struct castr *
407 ef_cadef(int type)
408 {
409     return empfile[type].cadef;
410 }
411
412 int
413 ef_nelem(int type)
414 {
415     return empfile[type].fids;
416 }
417
418 int
419 ef_flags(int type)
420 {
421     return empfile[type].flags;
422 }
423
424 int
425 ef_lock(int type)
426 {
427     return file_lock(empfile[type].fd);
428 }
429
430 int
431 ef_unlock(int type)
432 {
433     return file_unlock(empfile[type].fd);
434 }
435
436 time_t
437 ef_mtime(int type)
438 {
439     extern time_t fdate(int fd);
440
441     if (empfile[type].fd <= 0)
442         return 0;
443     return fdate(empfile[type].fd);
444 }
445
446 int
447 ef_vars(int type, register s_char *sp, u_char **nvp, u_char **vp,
448         u_short **ap)
449 {
450     register struct empfile *ef;
451
452     if (ef_check(type) < 0)
453         return 0;
454     ef = &empfile[type];
455     if ((ef->flags & EFF_COM) == 0)
456         return -1;
457     *nvp = (u_char *)(sp + ef->varoffs[0]);
458     *vp = (u_char *)(sp + ef->varoffs[1]);
459     *ap = (u_short *)(sp + ef->varoffs[2]);
460     return ef->maxvars;
461 }
462
463 int
464 ef_byname(s_char *name)
465 {
466     register struct empfile *ef;
467     register int i;
468     int len;
469
470     len = strlen(name);
471     for (i = 0; i < EF_MAX; i++) {
472         ef = &empfile[i];
473         if (strncmp(ef->name, name, min(len, strlen(ef->name))) == 0)
474             return i;
475     }
476     return -1;
477 }
478
479 s_char *
480 ef_nameof(int type)
481 {
482     if (type < 0 || type >= EF_MAX)
483         return "bad item type";
484     return empfile[type].name;
485 }
486
487 int
488 ef_check(int type)
489 {
490     if (type < 0 || type >= EF_MAX) {
491         logerror("ef_ptr: bad EF_type %d\n", type);
492         return -1;
493     }
494     return 0;
495 }