]> git.pond.sub.org Git - empserver/blob - src/lib/common/file.c
Import of Empire 4.2.12
[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",
122                         ep->name, ep->fd, r);
123         }
124         return 1;
125 }
126
127 int
128 ef_flush(int type)
129 {
130         register struct empfile *ep;
131         int     size;
132         int     r;
133
134         if (ef_check(type) < 0)
135                 return 0;
136         ep = &empfile[type];
137         if (ep->cache == 0) {
138                 /* no cache implies never opened */
139                 return 0;
140         }
141         size = ep->csize * ep->size;
142         if (ep->mode > 0 && (ep->flags & EFF_MEM)) {
143                 if ((r = lseek(ep->fd, 0L, 0)) < 0) {
144                         logerror("ef_flush: %s cache lseek(%d, 0L, 0) -> %d",
145                                 ep->name, ep->fd, r);
146                         return 0;
147                 }
148                 if (write(ep->fd, ep->cache, size) != size) {
149                         logerror("ef_flush: %s cache write(%d, %x, %d) -> %d",
150                                 ep->name, ep->fd, ep->cache, ep->size, r);
151                         return 0;
152                 }
153         }
154         /*ef_zapcache(type);*/
155         return 1;
156 }
157
158 s_char *
159 ef_ptr(int type, int id)
160 {
161         register struct empfile *ep;
162
163         if (ef_check(type) < 0)
164                 return 0;
165         ep = &empfile[type];
166         if (id < 0 || id >= ep->fids)
167                 return 0;
168         if ((ep->flags & EFF_MEM) == 0) {
169                 logerror("ef_ptr: (%s) only valid for EFF_MEM entries",
170                         ep->file);
171                 return 0;
172         }
173         return (s_char *) (ep->cache + ep->size * id);
174 }
175
176 /*
177  * buffered read.  Tries to read a large number of items.
178  * This system won't work if item size is > sizeof buffer area.
179  */
180 int
181 ef_read(int type, int id, caddr_t ptr)
182 {
183         register struct empfile *ep;
184         caddr_t from;
185
186         if (ef_check(type) < 0)
187                 return 0;
188         ep = &empfile[type];
189         if (id < 0)
190                 return 0;
191         if (ep->flags & EFF_MEM) {
192                 if (id >= ep->fids)
193                         return 0;
194                 from = ep->cache + (id * ep->size);
195         } else {
196                 if (id >= ep->fids) {
197                         ep->fids = fsize(ep->fd) / ep->size;
198                         if (id >= ep->fids)
199                                 return 0;
200                 }
201                 if (ep->baseid + ep->cids <= id || ep->baseid > id)
202                         fillcache(ep, id);
203                 from = ep->cache + (id - ep->baseid) * ep->size;
204         }
205         bcopy(from, ptr, ep->size);
206
207         if (ep->postread)
208                 ep->postread(id, ptr);
209         return 1;
210 }
211
212 static void
213 fillcache(struct empfile *ep, int start)
214 {
215         int     n;
216
217         ep->baseid = start;
218         lseek(ep->fd, start * ep->size, 0);
219         n = read(ep->fd, ep->cache, ep->csize * ep->size);
220         ep->cids = n / ep->size;
221 }
222
223 #ifdef notdef
224 /*
225  * no-buffered read
226  * zaps read cache
227  */
228 int
229 ef_nbread(int type, int id, caddr_t ptr)
230 {
231         register struct empfile *ep;
232         int     r;
233
234         if (ef_check(type) < 0)
235                 return 0;
236         ep = &empfile[type];
237         if (id < 0)
238                 return 0;
239         if (id >= ep->fids) {
240                 ep->fids = fsize(ep->fd) / ep->size;
241                 if (id >= ep->fids)
242                         return 0;
243         }
244         if ((r = lseek(ep->fd, id * ep->size, 0)) < 0) {
245                 logerror("ef_nbread: %s #%d lseek(%d, %d, 0) -> %d",
246                         ep->name, id, ep->fd, id * ep->size, r);
247                 return 0;
248         }
249         if ((r = read(ep->fd, ptr, ep->size)) != ep->size) {
250                 logerror("ef_nbread: %s #%d read(%d, %x, %d) -> %d",
251                         ep->name, id, ep->fd, ptr, ep->size, r);
252                 return 0;
253         }
254         ef_zapcache(type);
255         if (ep->postread)
256                 ep->postread(id, ptr);
257         return 1;
258 }
259 #endif
260
261 /*
262  * buffered write.  Modifies read cache (if applicable)
263  * and writes through to disk.
264  */
265 int
266 ef_write(int type, int id, caddr_t ptr)
267 {
268         register int r;
269         register struct empfile *ep;
270         s_char  *to;
271
272         if (ef_check(type) < 0)
273                 return 0;
274         ep = &empfile[type];
275         if (id > 65536) {
276                 /* largest unit id; this may bite us in large games */
277                 logerror("ef_write: type %d id %d is too large!\n", type, id);
278                 return 0;
279         }
280         if ((r = lseek(ep->fd, id * ep->size, 0)) < 0) {
281                 logerror("ef_write: %s #%d lseek(%d, %d, 0) -> %d",
282                         ep->name, id, ep->fd, id * ep->size, r);
283                 return 0;
284         }
285         if (ep->prewrite)
286                 ep->prewrite(id, ptr);
287         if ((r = write(ep->fd, ptr, ep->size)) != ep->size) {
288                 logerror("ef_write: %s #%d write(%d, %x, %d) -> %d",
289                         ep->name, id, ep->fd, ptr, ep->size, r);
290                 return 0;
291         }
292         if (id >= ep->baseid && id < ep->baseid + ep->cids) {
293                 /* update the cache if necessary */
294                 to = ep->cache + (id - ep->baseid) * ep->size;
295                 bcopy(ptr, to, ep->size);
296         }
297         if (id > ep->fids) {
298                 logerror("WARNING ef_write: expanded %s by more than one id",
299                         ep->name);
300                 log_last_commands();
301         }
302         if (id >= ep->fids) {
303                 if (ep->flags & EFF_MEM) {
304                         logerror("file %s went beyond %d items; won't be able toread item w/o restart", 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, u_short **ap)
448 {
449         register struct empfile *ef;
450
451         if (ef_check(type) < 0)
452                 return 0;
453         ef = &empfile[type];
454         if ((ef->flags & EFF_COM) == 0)
455                 return -1;
456         *nvp = (u_char *) (sp + ef->varoffs[0]);
457         *vp = (u_char *) (sp + ef->varoffs[1]);
458         *ap = (u_short *) (sp + ef->varoffs[2]);
459         return ef->maxvars;
460 }
461
462 int
463 ef_byname(s_char *name)
464 {
465         register struct empfile *ef;
466         register int i;
467         int     len;
468
469         len = strlen(name);
470         for (i=0; i<EF_MAX; i++) {
471                 ef = &empfile[i];
472                 if (strncmp(ef->name, name, min(len, strlen(ef->name))) == 0)
473                         return i;
474         }
475         return -1;
476 }
477
478 s_char *
479 ef_nameof(int type)
480 {
481         if (type < 0 || type >= EF_MAX)
482                 return "bad item type";
483         return empfile[type].name;
484 }
485
486 int
487 ef_check(int type)
488 {
489         if (type < 0 || type >= EF_MAX) {
490                 logerror("ef_ptr: bad EF_type %d\n", type);
491                 return -1;
492         }
493         return 0;
494 }