]> git.pond.sub.org Git - empserver/blob - src/lib/common/file.c
(main,mobility_check,upda,turn,rea,mobupdate,ef_open,logerror,
[empserver] / src / lib / common / file.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2004, 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     static int block;
61     int size;
62
63     if (ef_check(type) < 0)
64         return 0;
65     ep = &empfile[type];
66     if ((ep->fd = open(ep->file, mode, 0660)) < 0) {
67         logerror("%s: open failed", ep->file);
68         return 0;
69     }
70     if (block == 0)
71         block = blksize(ep->fd);
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 = block / ep->size;
81     size = ep->csize * ep->size;
82     ep->cache = malloc(size);
83     if ((ep->cache == NULL) && (size != 0)) {
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     if (id > ep->fids) {
262         logerror("WARNING ef_write: expanded %s by more than one id",
263                  ep->name);
264         log_last_commands();
265     }
266     if (id >= ep->fids) {
267         if (ep->flags & EFF_MEM) {
268             logerror("file %s went beyond %d items; won't be able toread item w/o restart",
269                      ep->name, ep->fids);
270         } else {
271             /* write expanded file; ep->fids = last id + 1 */
272             ep->fids = id + 1;
273         }
274     }
275     return 1;
276 }
277
278 /*
279  * Grow the file containing objects of the type 'type' by 'count' objects.
280  */
281 int
282 ef_extend(int type, int count)
283 {
284     struct empfile *ep;
285     char *tmpobj;
286     int cur, max;
287     int mode, how;
288     int r;
289
290     if (ef_check(type) < 0)
291         return 0;
292     ep = &empfile[type];
293     max = ep->fids + count;
294     cur = ep->fids;
295     tmpobj = calloc(1, ep->size);
296     if ((r = lseek(ep->fd, ep->fids * ep->size, SEEK_SET)) < 0) {
297         logerror("ef_extend: %s +#%d lseek(%d, %d, SEEK_SET) -> %d",
298                  ep->name, count, ep->fd, ep->fids * ep->size, r);
299         free(tmpobj);
300         return 0;
301     }
302     for (cur = ep->fids; cur < max; cur++) {
303         if (ep->init)
304             ep->init(cur, tmpobj);
305         if ((r = write(ep->fd, tmpobj, ep->size)) != ep->size) {
306             logerror("ef_extend: %s +#%d write(%d, %p, %d) -> %d",
307                      ep->name, count, ep->fd, tmpobj, ep->size, r);
308             free(tmpobj);
309             return 0;
310         }
311     }
312     free(tmpobj);
313     if (ep->flags & EFF_MEM) {
314         /* XXX this will cause problems if there are ef_ptrs (to the
315          * old allocated structure) active when we do the re-open */
316         mode = ep->mode;
317         how = ep->flags;
318         ef_close(type);
319         ef_open(type, mode, how);
320     } else {
321         ep->fids += count;
322     }
323     return 1;
324 }
325
326 /*
327  * Mark the cache for the file containing objects of type 'type' as unused.
328  */
329 void
330 ef_zapcache(int type)
331 {
332     struct empfile *ep = &empfile[type];
333     if ((ep->flags & EFF_MEM) == 0) {
334         ep->cids = 0;
335         ep->baseid = -1;
336     }
337 }
338
339 struct castr *
340 ef_cadef(int type)
341 {
342     return empfile[type].cadef;
343 }
344
345 int
346 ef_nelem(int type)
347 {
348     return empfile[type].fids;
349 }
350
351 int
352 ef_flags(int type)
353 {
354     return empfile[type].flags;
355 }
356
357 time_t
358 ef_mtime(int type)
359 {
360     if (empfile[type].fd <= 0)
361         return 0;
362     return fdate(empfile[type].fd);
363 }
364
365 /*
366  * Return the filedescriptor used for the file containing objects of type
367  * 'type'.
368  */
369 int
370 ef_byname(char *name)
371 {
372     struct empfile *ef;
373     int i;
374     int len;
375
376     len = strlen(name);
377     for (i = 0; i < EF_MAX; i++) {
378         ef = &empfile[i];
379         if (strncmp(ef->name, name, min(len, strlen(ef->name))) == 0)
380             return i;
381     }
382     return -1;
383 }
384
385 char *
386 ef_nameof(int type)
387 {
388     if (type < 0 || type >= EF_MAX)
389         return "bad item type";
390     return empfile[type].name;
391 }
392
393 int
394 ef_check(int type)
395 {
396     if (type < 0 || type >= EF_MAX) {
397         logerror("ef_ptr: bad EF_type %d\n", type);
398         return -1;
399     }
400     return 0;
401 }
402
403 int
404 ef_ensure_space(int type, int id, int count)
405 {
406     while (id >= empfile[type].fids) {
407         if (!ef_extend(type, count))
408             return 0;
409     }
410     return 1;
411 }