]> git.pond.sub.org Git - empserver/blob - src/lib/common/file.c
48b3c8ad25304a5e41fa289a0ebdc501387edd96
[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 /*
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).
55  */
56 int
57 ef_open(int type, int mode, int how)
58 {
59     struct empfile *ep;
60     static int block;
61     int size;
62
63 #if defined(_WIN32)
64     mode |= _O_BINARY;
65 #endif
66     if (ef_check(type) < 0)
67         return 0;
68     ep = &empfile[type];
69     if ((ep->fd = open(ep->file, mode, 0660)) < 0) {
70         logerror("%s: open failed", ep->file);
71         return 0;
72     }
73     if (block == 0)
74         block = blksize(ep->fd);
75     ep->baseid = 0;
76     ep->cids = 0;
77     ep->mode = mode;
78     ep->flags |= how;
79     ep->fids = fsize(ep->fd) / ep->size;
80     if (ep->flags & EFF_MEM)
81         ep->csize = ep->fids;
82     else
83         ep->csize = block / ep->size;
84     size = ep->csize * ep->size;
85     ep->cache = malloc(size);
86     if ((ep->cache == NULL) && (size != 0)) {
87         logerror("ef_open: %s malloc(%d) failed\n", ep->file, size);
88         return 0;
89     }
90     if (ep->flags & EFF_MEM) {
91         if (read(ep->fd, ep->cache, size) != size) {
92             logerror("ef_open: read(%s) failed\n", ep->file);
93             return 0;
94         }
95         ep->cids = size / ep->size;
96     }
97     return 1;
98 }
99
100 /*
101  * Close the file containing objects of the type 'type', flushing the cache
102  * if applicable.
103  */
104 int
105 ef_close(int type)
106 {
107     struct empfile *ep;
108     int r;
109
110     if (ef_check(type) < 0)
111         return 0;
112     ep = &empfile[type];
113     if (ep->cache == NULL) {
114         /* no cache implies never opened */
115         return 0;
116     }
117     ef_flush(type);
118     ep->flags &= ~EFF_MEM;
119     free(ep->cache);
120     ep->cache = NULL;
121     if ((r = close(ep->fd)) < 0) {
122         logerror("ef_close: %s close(%d) -> %d", ep->name, ep->fd, r);
123     }
124     return 1;
125 }
126
127 /*
128  * Flush the cache of the file containing objects of type 'type' to disk.
129  */
130 int
131 ef_flush(int type)
132 {
133     struct empfile *ep;
134     int size;
135     int r;
136
137     if (ef_check(type) < 0)
138         return 0;
139     ep = &empfile[type];
140     if (ep->cache == NULL) {
141         /* no cache implies never opened */
142         return 0;
143     }
144     size = ep->csize * ep->size;
145     if (ep->mode > 0 && (ep->flags & EFF_MEM)) {
146         if ((r = lseek(ep->fd, 0L, SEEK_SET)) < 0) {
147             logerror("ef_flush: %s cache lseek(%d, 0L, SEEK_SET) -> %d",
148                      ep->name, ep->fd, r);
149             return 0;
150         }
151         if (write(ep->fd, ep->cache, size) != size) {
152             logerror("ef_flush: %s cache write(%d, %p, %d) -> %d",
153                      ep->name, ep->fd, ep->cache, ep->size, r);
154             return 0;
155         }
156     }
157     /*ef_zapcache(type); */
158     return 1;
159 }
160
161 /*
162  * Return a pointer the id 'id' of object of type 'type' in the cache.
163  */
164 char *
165 ef_ptr(int type, int id)
166 {
167     struct empfile *ep;
168
169     if (ef_check(type) < 0)
170         return NULL;
171     ep = &empfile[type];
172     if (id < 0 || id >= ep->fids)
173         return NULL;
174     if ((ep->flags & EFF_MEM) == 0) {
175         logerror("ef_ptr: (%s) only valid for EFF_MEM entries", ep->file);
176         return NULL;
177     }
178     return (ep->cache + ep->size * id);
179 }
180
181 /*
182  * buffered read.  Tries to read a large number of items.
183  * This system won't work if item size is > sizeof buffer area.
184  */
185 int
186 ef_read(int type, int id, void *into)
187 {
188     struct empfile *ep;
189     void *from;
190
191     if (ef_check(type) < 0)
192         return 0;
193     ep = &empfile[type];
194     if (id < 0)
195         return 0;
196     if (ep->flags & EFF_MEM) {
197         if (id >= ep->fids)
198             return 0;
199         from = ep->cache + (id * ep->size);
200     } else {
201         if (id >= ep->fids) {
202             ep->fids = fsize(ep->fd) / ep->size;
203             if (id >= ep->fids)
204                 return 0;
205         }
206         if (ep->baseid + ep->cids <= id || ep->baseid > id)
207             fillcache(ep, id);
208         from = ep->cache + (id - ep->baseid) * ep->size;
209     }
210     memcpy(into, from, ep->size);
211
212     if (ep->postread)
213         ep->postread(id, into);
214     return 1;
215 }
216
217 static void
218 fillcache(struct empfile *ep, int start)
219 {
220     int n;
221
222     ep->baseid = start;
223     lseek(ep->fd, start * ep->size, SEEK_SET);
224     n = read(ep->fd, ep->cache, ep->csize * ep->size);
225     ep->cids = n / ep->size;
226 }
227
228 /*
229  * buffered write.  Modifies read cache (if applicable)
230  * and writes through to disk.
231  */
232 int
233 ef_write(int type, int id, void *from)
234 {
235     int r;
236     struct empfile *ep;
237     char *to;
238
239     if (ef_check(type) < 0)
240         return 0;
241     ep = &empfile[type];
242     if (id > 65536) {
243         /* largest unit id; this may bite us in large games */
244         logerror("ef_write: type %d id %d is too large!\n", type, id);
245         return 0;
246     }
247     if ((r = lseek(ep->fd, id * ep->size, SEEK_SET)) < 0) {
248         logerror("ef_write: %s #%d lseek(%d, %d, SEEK_SET) -> %d",
249                  ep->name, id, ep->fd, id * ep->size, r);
250         return 0;
251     }
252     if (ep->prewrite)
253         ep->prewrite(id, from);
254     if ((r = write(ep->fd, from, ep->size)) != ep->size) {
255         logerror("ef_write: %s #%d write(%d, %p, %d) -> %d",
256                  ep->name, id, ep->fd, from, ep->size, r);
257         return 0;
258     }
259     if (id >= ep->baseid && id < ep->baseid + ep->cids) {
260         /* update the cache if necessary */
261         to = ep->cache + (id - ep->baseid) * ep->size;
262         memcpy(to, from, ep->size);
263     }
264     if (id > ep->fids) {
265         logerror("WARNING ef_write: expanded %s by more than one id",
266                  ep->name);
267         log_last_commands();
268     }
269     if (id >= ep->fids) {
270         if (ep->flags & EFF_MEM) {
271             logerror("file %s went beyond %d items; won't be able toread item w/o restart",
272                      ep->name, ep->fids);
273         } else {
274             /* write expanded file; ep->fids = last id + 1 */
275             ep->fids = id + 1;
276         }
277     }
278     return 1;
279 }
280
281 /*
282  * Grow the file containing objects of the type 'type' by 'count' objects.
283  */
284 int
285 ef_extend(int type, int count)
286 {
287     struct empfile *ep;
288     char *tmpobj;
289     int cur, max;
290     int mode, how;
291     int r;
292
293     if (ef_check(type) < 0)
294         return 0;
295     ep = &empfile[type];
296     max = ep->fids + count;
297     cur = ep->fids;
298     tmpobj = calloc(1, ep->size);
299     if ((r = lseek(ep->fd, ep->fids * ep->size, SEEK_SET)) < 0) {
300         logerror("ef_extend: %s +#%d lseek(%d, %d, SEEK_SET) -> %d",
301                  ep->name, count, ep->fd, ep->fids * ep->size, r);
302         return 0;
303     }
304     for (cur = ep->fids; cur < max; cur++) {
305         if (ep->init)
306             ep->init(cur, tmpobj);
307         if ((r = write(ep->fd, tmpobj, ep->size)) != ep->size) {
308             logerror("ef_extend: %s +#%d write(%d, %p, %d) -> %d",
309                      ep->name, count, ep->fd, tmpobj, ep->size, r);
310             return 0;
311         }
312     }
313     free(tmpobj);
314     if (ep->flags & EFF_MEM) {
315         /* XXX this will cause problems if there are ef_ptrs (to the
316          * old allocated structure) active when we do the re-open */
317         mode = ep->mode;
318         how = ep->flags;
319         ef_close(type);
320         ef_open(type, mode, how);
321     } else {
322         ep->fids += count;
323     }
324     return 1;
325 }
326
327 /*
328  * Mark the cache for the file containing objects of type 'type' as unused.
329  */
330 void
331 ef_zapcache(int type)
332 {
333     struct empfile *ep = &empfile[type];
334     if ((ep->flags & EFF_MEM) == 0) {
335         ep->cids = 0;
336         ep->baseid = -1;
337     }
338 }
339
340 struct castr *
341 ef_cadef(int type)
342 {
343     return empfile[type].cadef;
344 }
345
346 int
347 ef_nelem(int type)
348 {
349     return empfile[type].fids;
350 }
351
352 int
353 ef_flags(int type)
354 {
355     return empfile[type].flags;
356 }
357
358 time_t
359 ef_mtime(int type)
360 {
361     if (empfile[type].fd <= 0)
362         return 0;
363     return fdate(empfile[type].fd);
364 }
365
366 /*
367  * Return the filedescriptor used for the file containing objects of type
368  * 'type'.
369  */
370 int
371 ef_byname(char *name)
372 {
373     struct empfile *ef;
374     int i;
375     int len;
376
377     len = strlen(name);
378     for (i = 0; i < EF_MAX; i++) {
379         ef = &empfile[i];
380         if (strncmp(ef->name, name, min(len, strlen(ef->name))) == 0)
381             return i;
382     }
383     return -1;
384 }
385
386 char *
387 ef_nameof(int type)
388 {
389     if (type < 0 || type >= EF_MAX)
390         return "bad item type";
391     return empfile[type].name;
392 }
393
394 int
395 ef_check(int type)
396 {
397     if (type < 0 || type >= EF_MAX) {
398         logerror("ef_ptr: bad EF_type %d\n", type);
399         return -1;
400     }
401     return 0;
402 }
403
404 int
405 ef_ensure_space(int type, int id, int count)
406 {
407     while (id >= empfile[type].fids) {
408         if (!ef_extend(type, count))
409             return 0;
410     }
411     return 1;
412 }