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