]> git.pond.sub.org Git - empserver/blob - src/lib/common/file.c
(do_write): 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 <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, 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 ((ep->fd = open(ep->file, oflags, 0660)) < 0) {
82         logerror("%s: open failed", ep->file);
83         return 0;
84     }
85     ep->baseid = 0;
86     ep->cids = 0;
87     ep->flags = (ep->flags & ~EFF_OPEN) | (how ^ ~EFF_CREATE);
88     fsiz = fsize(ep->fd);
89     if (fsiz % ep->size) {
90         logerror("Can't open %s (file size not a multiple of record size %d)",
91                  ep->file, ep->size);
92         close(ep->fd);
93         return 0;
94     }
95     ep->fids = fsiz / ep->size;
96     if (ep->flags & EFF_MEM)
97         ep->csize = ep->fids;
98     else
99         ep->csize = max(1, blksize(ep->fd) / ep->size);
100     size = ep->csize * ep->size;
101     ep->cache = malloc(size);
102     if (ep->cache == NULL) {
103         logerror("ef_open: %s malloc(%d) failed\n", ep->file, size);
104         return 0;
105     }
106     if (ep->flags & EFF_MEM) {
107         if (fillcache(ep, 0) != ep->fids) {
108             return 0;
109         }
110     }
111     return 1;
112 }
113
114 /*
115  * Close the file containing objects of the type 'type', flushing the cache
116  * if applicable.
117  */
118 int
119 ef_close(int type)
120 {
121     struct empfile *ep;
122     int r;
123
124     if (ef_check(type) < 0)
125         return 0;
126     ep = &empfile[type];
127     if (ep->cache == NULL) {
128         /* no cache implies never opened */
129         return 0;
130     }
131     ef_flush(type);
132     ep->flags &= ~EFF_MEM;
133     free(ep->cache);
134     ep->cache = NULL;
135     if ((r = close(ep->fd)) < 0) {
136         logerror("ef_close: %s close(%d) -> %d", ep->name, ep->fd, r);
137     }
138     ep->fd = -1;
139     return 1;
140 }
141
142 /*
143  * Flush the cache of the file containing objects of type 'type' to disk.
144  */
145 int
146 ef_flush(int type)
147 {
148     struct empfile *ep;
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         return do_write(ep, ep->cache, ep->baseid, ep->cids) >= 0;
165
166     return 1;
167 }
168
169 /*
170  * Return a pointer the id 'id' of object of type 'type' in the cache.
171  */
172 char *
173 ef_ptr(int type, int id)
174 {
175     struct empfile *ep;
176
177     if (ef_check(type) < 0)
178         return NULL;
179     ep = &empfile[type];
180     if (id < 0 || id >= ep->fids)
181         return NULL;
182     if ((ep->flags & EFF_MEM) == 0) {
183         logerror("ef_ptr: (%s) only valid for EFF_MEM entries", ep->file);
184         return NULL;
185     }
186     return ep->cache + ep->size * id;
187 }
188
189 /*
190  * buffered read.  Tries to read a large number of items.
191  * This system won't work if item size is > sizeof buffer area.
192  */
193 int
194 ef_read(int type, int id, void *into)
195 {
196     struct empfile *ep;
197     void *from;
198
199     if (ef_check(type) < 0)
200         return 0;
201     ep = &empfile[type];
202     if (id < 0)
203         return 0;
204     if (ep->flags & EFF_MEM) {
205         if (id >= ep->fids)
206             return 0;
207         from = ep->cache + (id * ep->size);
208     } else {
209         if (id >= ep->fids) {
210             ep->fids = fsize(ep->fd) / ep->size;
211             if (id >= ep->fids)
212                 return 0;
213         }
214         if (ep->baseid + ep->cids <= id || ep->baseid > id)
215             if (fillcache(ep, id) < 1)
216                 return 0;
217         from = ep->cache + (id - ep->baseid) * ep->size;
218     }
219     memcpy(into, from, ep->size);
220
221     if (ep->postread)
222         ep->postread(id, into);
223     return 1;
224 }
225
226 /*
227  * Fill cache of EP with elements starting at ID.
228  * If any were read, return their number.
229  * Else return -1 and leave the cache unchanged.
230  */
231 static int
232 fillcache(struct empfile *ep, int start)
233 {
234     int n, ret;
235     char *p;
236
237     if (CANT_HAPPEN(ep->fd < 0 || !ep->cache))
238         return -1;
239
240     if (lseek(ep->fd, start * ep->size, SEEK_SET) == (off_t)-1) {
241         logerror("Error seeking %s (%s)", ep->file, strerror(errno));
242         return -1;
243     }
244
245     p = ep->cache;
246     n = ep->csize * ep->size;
247     while (n > 0) {
248         ret = read(ep->fd, p, n);
249         if (ret < 0) {
250             if (errno != EAGAIN) {
251                 logerror("Error reading %s (%s)", ep->file, strerror(errno));
252                 break;
253             }
254         } else if (ret == 0) {
255             break;
256         } else {
257             p += ret;
258             n -= ret;
259         }
260     }
261
262     if (p == ep->cache)
263         return -1;              /* nothing read, old cache still ok */
264
265     ep->baseid = start;
266     ep->cids = (p - ep->cache) / ep->size;
267     return ep->cids;
268 }
269
270 /*
271  * Write COUNT elements from BUF to EP, starting at ID.
272  * Return 0 on success, -1 on error.
273  */
274 static int
275 do_write(struct empfile *ep, void *buf, int id, int count)
276 {
277     int n, ret;
278     char *p;
279
280     if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
281         return -1;
282
283     if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
284         logerror("Error seeking %s (%s)", ep->file, strerror(errno));
285         return -1;
286     }
287
288     p = buf;
289     n = count * ep->size;
290     while (n > 0) {
291         ret = write(ep->fd, p, n);
292         if (ret < 0) {
293             if (errno != EAGAIN) {
294                 logerror("Error writing %s (%s)", ep->file, strerror(errno));
295                 return -1;
296             }
297         } else {
298             p += ret;
299             n -= ret;
300         }
301     }
302
303     return 0;
304 }
305
306 /*
307  * buffered write.  Modifies read cache (if applicable)
308  * and writes through to disk.
309  */
310 int
311 ef_write(int type, int id, void *from)
312 {
313     struct empfile *ep;
314     char *to;
315
316     if (ef_check(type) < 0)
317         return 0;
318     ep = &empfile[type];
319     if (id > 65536) {
320         /* largest unit id; this may bite us in large games */
321         logerror("ef_write: type %d id %d is too large!\n", type, id);
322         return 0;
323     }
324     if (ep->prewrite)
325         ep->prewrite(id, from);
326     if (CANT_HAPPEN((ep->flags & EFF_MEM) ? id >= ep->fids : id > ep->fids))
327         return 0;               /* not implemented */
328     if (do_write(ep, from, id, 1) < 0)
329         return 0;
330     if (id >= ep->baseid && id < ep->baseid + ep->cids) {
331         /* update the cache if necessary */
332         to = ep->cache + (id - ep->baseid) * ep->size;
333         memcpy(to, from, ep->size);
334     }
335     CANT_HAPPEN(id > ep->fids);
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 }