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