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