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