]> git.pond.sub.org Git - empserver/blob - src/lib/common/file.c
(EFF_OPEN): New.
[empserver] / src / lib / common / file.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 a the file for table TYPE (EF_SECTOR, ...).
53  * MODE is passed to open().
54  * HOW are EFF_OPEN flags to control operation.
55  * Return non-zero on success, zero on failure.
56  */
57 int
58 ef_open(int type, int mode, int how)
59 {
60     struct empfile *ep;
61     int size;
62
63 #if defined(_WIN32)
64     mode |= O_BINARY;
65 #endif
66     if (ef_check(type) < 0)
67         return 0;
68     if (CANT_HAPPEN(how & ~EFF_OPEN))
69         how &= EFF_OPEN;
70     ep = &empfile[type];
71     if ((ep->fd = open(ep->file, mode, 0660)) < 0) {
72         logerror("%s: open failed", ep->file);
73         return 0;
74     }
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 = max(1, blksize(ep->fd) / ep->size);
84     size = ep->csize * ep->size;
85     ep->cache = malloc(size);
86     if (ep->cache == NULL) {
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     CANT_HAPPEN(id > ep->fids);
265     if (id >= ep->fids) {
266         if (ep->flags & EFF_MEM) {
267             logerror("file %s went beyond %d items; won't be able toread item w/o restart",
268                      ep->name, ep->fids);
269         } else {
270             /* write expanded file; ep->fids = last id + 1 */
271             ep->fids = id + 1;
272         }
273     }
274     return 1;
275 }
276
277 /*
278  * Grow the file containing objects of the type 'type' by 'count' objects.
279  */
280 int
281 ef_extend(int type, int count)
282 {
283     struct empfile *ep;
284     char *tmpobj;
285     int cur, max;
286     int mode, how;
287     int r;
288
289     if (ef_check(type) < 0)
290         return 0;
291     ep = &empfile[type];
292     max = ep->fids + count;
293     cur = ep->fids;
294     tmpobj = calloc(1, ep->size);
295     if ((r = lseek(ep->fd, ep->fids * ep->size, SEEK_SET)) < 0) {
296         logerror("ef_extend: %s +#%d lseek(%d, %d, SEEK_SET) -> %d",
297                  ep->name, count, ep->fd, ep->fids * ep->size, r);
298         free(tmpobj);
299         return 0;
300     }
301     for (cur = ep->fids; cur < max; cur++) {
302         if (ep->init)
303             ep->init(cur, tmpobj);
304         if ((r = write(ep->fd, tmpobj, ep->size)) != ep->size) {
305             logerror("ef_extend: %s +#%d write(%d, %p, %d) -> %d",
306                      ep->name, count, ep->fd, tmpobj, ep->size, r);
307             free(tmpobj);
308             return 0;
309         }
310     }
311     free(tmpobj);
312     if (ep->flags & EFF_MEM) {
313         /* XXX this will cause problems if there are ef_ptrs (to the
314          * old allocated structure) active when we do the re-open */
315         mode = ep->mode;
316         how = ep->flags;
317         ef_close(type);
318         ef_open(type, mode, how);
319     } else {
320         ep->fids += count;
321     }
322     return 1;
323 }
324
325 /*
326  * Mark the cache for the file containing objects of type 'type' as unused.
327  */
328 void
329 ef_zapcache(int type)
330 {
331     struct empfile *ep = &empfile[type];
332     if ((ep->flags & EFF_MEM) == 0) {
333         ep->cids = 0;
334         ep->baseid = -1;
335     }
336 }
337
338 struct castr *
339 ef_cadef(int type)
340 {
341     return empfile[type].cadef;
342 }
343
344 int
345 ef_nelem(int type)
346 {
347     return empfile[type].fids;
348 }
349
350 int
351 ef_flags(int type)
352 {
353     return empfile[type].flags;
354 }
355
356 time_t
357 ef_mtime(int type)
358 {
359     if (empfile[type].fd <= 0)
360         return 0;
361     return fdate(empfile[type].fd);
362 }
363
364 /*
365  * Search empfile[0..EF_MAX-1] for element named NAME.
366  * Return its index in empfile[] if found, else -1.
367  */
368 int
369 ef_byname(char *name)
370 {
371     struct empfile *ef;
372     int i;
373     int len;
374
375     len = strlen(name);
376     for (i = 0; i < EF_MAX; i++) {
377         ef = &empfile[i];
378         if (strncmp(ef->name, name, min(len, strlen(ef->name))) == 0)
379             return i;
380     }
381     return -1;
382 }
383
384 char *
385 ef_nameof(int type)
386 {
387     if (type < 0 || type >= EF_MAX)
388         return "bad item type";
389     return empfile[type].name;
390 }
391
392 int
393 ef_check(int type)
394 {
395     if (type < 0 || type >= EF_MAX) {
396         logerror("ef_ptr: bad EF_type %d\n", type);
397         return -1;
398     }
399     return 0;
400 }
401
402 int
403 ef_ensure_space(int type, int id, int count)
404 {
405     while (id >= empfile[type].fids) {
406         if (!ef_extend(type, count))
407             return 0;
408     }
409     return 1;
410 }