]> git.pond.sub.org Git - empserver/blob - src/lib/common/file.c
(ef_nameof, ef_ensure_space): Oops on bad argument.
[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: Operations on Empire tables (`files' for historical reasons)
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  *     Steve McClure, 2000
33  *     Markus Armbruster, 2005
34  */
35
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <signal.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #if !defined(_WIN32)
42 #include <unistd.h>
43 #endif
44 #include "misc.h"
45 #include "nsc.h"
46 #include "file.h"
47 #include "common.h"
48 #include "gen.h"
49
50
51 static int fillcache(struct empfile *, int);
52 static int do_write(struct empfile *, void *, int, int);
53
54 /*
55  * Open the file-backed 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
71     /* open file */
72     ep = &empfile[type];
73     if (CANT_HAPPEN(ep->fd >= 0))
74         return 0;
75     oflags = O_RDWR;
76     if (how & EFF_RDONLY)
77         oflags = O_RDONLY;
78     if (how & EFF_CREATE)
79         oflags |= O_CREAT | O_TRUNC;
80 #if defined(_WIN32)
81     oflags |= O_BINARY;
82 #endif
83     if ((fd = open(ep->file, oflags, 0660)) < 0) {
84         logerror("Can't open %s (%s)", ep->file, strerror(errno));
85         return 0;
86     }
87
88     /* get file size */
89     fsiz = fsize(fd);
90     if (fsiz % ep->size) {
91         logerror("Can't open %s (file size not a multiple of record size %d)",
92                  ep->file, ep->size);
93         close(fd);
94         return 0;
95     }
96     ep->fids = fsiz / ep->size;
97     if (how & EFF_MEM)
98         ep->csize = ep->fids;
99     else
100         ep->csize = max(1, blksize(fd) / ep->size);
101     size = ep->csize * ep->size;
102     if (size) {
103         ep->cache = malloc(size);
104         if (ep->cache == NULL) {
105             logerror("Can't open %s: out of memory", ep->file);
106             close(fd);
107             return 0;
108         }
109     } else {
110         ep->cache = NULL;
111     }
112     ep->baseid = 0;
113     ep->cids = 0;
114     ep->flags = (ep->flags & ~EFF_OPEN) | (how & ~EFF_CREATE);
115     ep->fd = fd;
116
117     /* map file into cache */
118     if ((how & EFF_MEM) && ep->fids) {
119         if (fillcache(ep, 0) != ep->fids) {
120             ep->cids = 0;       /* prevent cache flush */
121             ep->flags &= ~EFF_OPEN; /* maintain invariant */
122             ef_close(type);
123             return 0;
124         }
125     }
126
127     return 1;
128 }
129
130 /*
131  * Close the file-backed table TYPE (EF_SECTOR, ...).
132  * Return non-zero on success, zero on failure.
133  */
134 int
135 ef_close(int type)
136 {
137     struct empfile *ep;
138     int retval;
139
140     retval = ef_flush(type);
141     ep = &empfile[type];
142     ep->flags &= ~EFF_OPEN;
143     free(ep->cache);
144     ep->cache = NULL;
145     if (close(ep->fd) < 0) {
146         logerror("Error closing %s (%s)", ep->name, strerror(errno));
147         retval = 0;
148     }
149     ep->fd = -1;
150     return retval;
151 }
152
153 /*
154  * Flush file-backed table TYPE (EF_SECTOR, ...) to disk.
155  * Return non-zero on success, zero on failure.
156  */
157 int
158 ef_flush(int type)
159 {
160     struct empfile *ep;
161
162     if (ef_check(type) < 0)
163         return 0;
164     ep = &empfile[type];
165     if (CANT_HAPPEN(ep->fd < 0))
166         return 0;
167     /*
168      * We don't know which cache entries are dirty.  ef_write() writes
169      * through, but direct updates through ef_ptr() don't.  They are
170      * allowed only with EFF_MEM.  Assume the whole cash is dirty
171      * then.
172      */
173     if (!(ep->flags & EFF_RDONLY) && (ep->flags & EFF_MEM))
174         return do_write(ep, ep->cache, ep->baseid, ep->cids) >= 0;
175
176     return 1;
177 }
178
179 /*
180  * Return pointer to element ID in table TYPE if it exists, else NULL.
181  * The table must be fully cached, i.e. flags & EFF_MEM.
182  * The caller is responsible for flushing changes he makes.
183  */
184 void *
185 ef_ptr(int type, int id)
186 {
187     struct empfile *ep;
188
189     if (ef_check(type) < 0)
190         return NULL;
191     ep = &empfile[type];
192     if (CANT_HAPPEN(!(ep->flags & EFF_MEM)))
193         return NULL;
194     if (id < 0 || id >= ep->fids)
195         return NULL;            /* FIXME can this happen? */
196     return ep->cache + ep->size * id;
197 }
198
199 /*
200  * Read element ID from table TYPE into buffer INTO.
201  * FIXME pass buffer size!
202  * Return non-zero on success, zero on failure.
203  */
204 int
205 ef_read(int type, int id, void *into)
206 {
207     struct empfile *ep;
208     void *from;
209
210     if (ef_check(type) < 0)
211         return 0;
212     ep = &empfile[type];
213     if (CANT_HAPPEN(!ep->cache))
214         return 0;
215     if (id < 0)
216         return 0;               /* FIXME can this happen? */
217     if (id >= ep->fids)
218         return 0;
219
220     if (ep->flags & EFF_MEM) {
221         from = ep->cache + id * ep->size;
222     } else {
223         if (ep->baseid + ep->cids <= id || ep->baseid > id) {
224             if (fillcache(ep, id) < 1)
225                 return 0;
226         }
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  * Write COUNT elements from BUF to EP, starting at ID.
282  * Return 0 on success, -1 on error.
283  */
284 static int
285 do_write(struct empfile *ep, void *buf, int id, int count)
286 {
287     int n, ret;
288     char *p;
289
290     if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
291         return -1;
292
293     if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
294         logerror("Error seeking %s (%s)", ep->file, strerror(errno));
295         return -1;
296     }
297
298     p = buf;
299     n = count * ep->size;
300     while (n > 0) {
301         ret = write(ep->fd, p, n);
302         if (ret < 0) {
303             if (errno != EAGAIN) {
304                 logerror("Error writing %s (%s)", ep->file, strerror(errno));
305                 /* FIXME if this extended file, truncate back to old size */
306                 return -1;
307             }
308         } else {
309             p += ret;
310             n -= ret;
311         }
312     }
313
314     return 0;
315 }
316
317 /*
318  * Write element ID into file-backed table TYPE from buffer FROM.
319  * FIXME pass buffer size!
320  * Write through cache straight to disk.
321  * Cannot write beyond the end of fully cached table (flags & EFF_MEM).
322  * Can write at the end of partially cached table.
323  * Return non-zero on success, zero on failure.
324  */
325 int
326 ef_write(int type, int id, void *from)
327 {
328     struct empfile *ep;
329     char *to;
330
331     if (ef_check(type) < 0)
332         return 0;
333     ep = &empfile[type];
334     if (CANT_HAPPEN(ep->fd < 0))
335         return 0;
336     if (ep->prewrite)
337         ep->prewrite(id, from);
338     if (CANT_HAPPEN((ep->flags & EFF_MEM) ? id >= ep->fids : id > ep->fids))
339         return 0;               /* not implemented */
340     if (do_write(ep, from, id, 1) < 0)
341         return 0;
342     if (id >= ep->baseid && id < ep->baseid + ep->cids) {
343         /* update the cache if necessary */
344         to = ep->cache + (id - ep->baseid) * ep->size;
345         memcpy(to, from, ep->size);
346     }
347     if (id >= ep->fids) {
348         /* write beyond end of file extends it, take note */
349         ep->fids = id + 1;
350     }
351     return 1;
352 }
353
354 /*
355  * Extend the file-backed table TYPE by COUNT elements.
356  * Return the ID of the first new element, or -1 on failure.
357  */
358 int
359 ef_extend(int type, int count)
360 {
361     struct empfile *ep;
362     char *tmpobj;
363     int id, i, how;
364
365     if (ef_check(type) < 0)
366         return 0;
367     ep = &empfile[type];
368     if (CANT_HAPPEN(ep->fd < 0 || count < 0))
369         return 0;
370
371     tmpobj = calloc(1, ep->size);
372     id = ep->fids;
373     for (i = 0; i < count; i++) {
374         if (ep->init)
375             ep->init(id + i, tmpobj);
376         if (do_write(ep, tmpobj, id + i, 1) < 0)
377             break;
378     }
379     free(tmpobj);
380
381     if (ep->flags & EFF_MEM) {
382         /* FIXME lazy bastards...  do this right */
383         /* XXX this will cause problems if there are ef_ptrs (to the
384          * old allocated structure) active when we do the re-open */
385         how = ep->flags & EFF_OPEN;
386         ef_close(type);
387         ef_open(type, how);
388     } else {
389         ep->fids += i;
390     }
391
392     return i == count;
393 }
394
395 struct castr *
396 ef_cadef(int type)
397 {
398     return empfile[type].cadef;
399 }
400
401 int
402 ef_nelem(int type)
403 {
404     return empfile[type].fids;
405 }
406
407 int
408 ef_flags(int type)
409 {
410     return empfile[type].flags;
411 }
412
413 time_t
414 ef_mtime(int type)
415 {
416     if (empfile[type].fd <= 0)
417         return 0;
418     return fdate(empfile[type].fd);
419 }
420
421 /*
422  * Search empfile[0..EF_MAX-1] for element named NAME.
423  * Return its index in empfile[] if found, else -1.
424  */
425 int
426 ef_byname(char *name)
427 {
428     struct empfile *ef;
429     int i;
430     int len;
431
432     len = strlen(name);
433     for (i = 0; i < EF_MAX; i++) {
434         ef = &empfile[i];
435         if (strncmp(ef->name, name, min(len, strlen(ef->name))) == 0)
436             return i;
437     }
438     return -1;
439 }
440
441 char *
442 ef_nameof(int type)
443 {
444     if (ef_check(type) < 0)
445         return "bad ef_type";
446     return empfile[type].name;
447 }
448
449 int
450 ef_check(int type)
451 {
452     if (CANT_HAPPEN((unsigned)type >= EF_MAX))
453         return -1;
454     return 0;
455 }
456
457 /*
458  * Ensure file-backed table contains ID.
459  * If necessary, extend it in steps of COUNT elements.
460  * Return non-zero on success, zero on failure.
461  */
462 int
463 ef_ensure_space(int type, int id, int count)
464 {
465     if (ef_check(type) < 0)
466         return 0;
467
468     while (id >= empfile[type].fids) {
469         if (!ef_extend(type, count))
470             return 0;
471     }
472     return 1;
473 }