]> git.pond.sub.org Git - empserver/blob - src/lib/common/file.c
License upgrade to GPL version 3 or later
[empserver] / src / lib / common / file.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  file.c: Operations on Empire tables (`files' for historical reasons)
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1989
31  *     Steve McClure, 2000
32  *     Markus Armbruster, 2005-2009
33  */
34
35 #include <config.h>
36
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <sys/stat.h>
40 #include <sys/types.h>
41 #include <unistd.h>
42 #ifdef _WIN32
43 #include <io.h>
44 #include <share.h>
45 #endif
46 #include "file.h"
47 #include "match.h"
48 #include "misc.h"
49 #include "nsc.h"
50 #include "prototypes.h"
51
52 static int open_locked(char *, int, mode_t);
53 static int ef_realloc_cache(struct empfile *, int);
54 static int fillcache(struct empfile *, int);
55 static int do_read(struct empfile *, void *, int, int);
56 static int do_write(struct empfile *, void *, int, int);
57 static unsigned get_seqno(struct empfile *, int);
58 static void new_seqno(struct empfile *, void *);
59 static void must_be_fresh(struct empfile *, void *);
60 static void do_blank(struct empfile *, void *, int, int);
61 static int ef_check(int);
62
63 static unsigned ef_generation;
64
65 /*
66  * Open the file-backed table TYPE (EF_SECTOR, ...).
67  * HOW are flags to control operation.  Naturally, immutable flags are
68  * not permitted.
69  * If NELT is non-negative, the table must have that many elements.
70  * Return non-zero on success, zero on failure.
71  * You must call ef_close() before the next ef_open().
72  */
73 int
74 ef_open(int type, int how, int nelt)
75 {
76     struct empfile *ep;
77     int oflags, fd, fsiz, nslots;
78
79     if (ef_check(type) < 0)
80         return 0;
81     if (CANT_HAPPEN(how & EFF_IMMUTABLE))
82         how &= ~EFF_IMMUTABLE;
83
84     /* open file */
85     ep = &empfile[type];
86     if (CANT_HAPPEN(ep->fd >= 0))
87         return 0;
88     oflags = O_RDWR;
89     if (how & EFF_PRIVATE)
90         oflags = O_RDONLY;
91     if (how & EFF_CREATE)
92         oflags |= O_CREAT | O_TRUNC;
93     fd = open_locked(ep->file, oflags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
94     if (fd < 0) {
95         logerror("Can't open %s (%s)", ep->file, strerror(errno));
96         return 0;
97     }
98
99     /* get file size */
100     fsiz = fsize(fd);
101     if (fsiz % ep->size) {
102         logerror("Can't open %s (file size not a multiple of record size %d)",
103                  ep->file, ep->size);
104         close(fd);
105         return 0;
106     }
107     ep->fids = fsiz / ep->size;
108     if (nelt >= 0 && nelt != ep->fids) {
109         logerror("Can't open %s (got %d records instead of %d)",
110                  ep->file, ep->fids, nelt);
111         close(fd);
112         return 0;
113     }
114
115     /* allocate cache */
116     if (ep->flags & EFF_STATIC) {
117         /* ep->cache already points to space for ep->csize elements */
118         if (how & EFF_MEM) {
119             if (ep->fids > ep->csize) {
120                 logerror("Can't open %s: file larger than %d bytes",
121                          ep->file, ep->fids * ep->size);
122                 close(fd);
123                 return 0;
124             }
125         }
126     } else {
127         if (CANT_HAPPEN(ep->cache))
128             free(ep->cache);
129         if (how & EFF_MEM)
130             nslots = ep->fids;
131         else
132             nslots = blksize(fd) / ep->size;
133         if (!ef_realloc_cache(ep, nslots)) {
134             logerror("Can't map %s (%s)", ep->file, strerror(errno));
135             close(fd);
136             return 0;
137         }
138     }
139     ep->baseid = 0;
140     ep->cids = 0;
141     ep->flags = (ep->flags & EFF_IMMUTABLE) | (how & ~EFF_CREATE);
142     ep->fd = fd;
143
144     /* map file into cache */
145     if ((how & EFF_MEM) && ep->fids) {
146         if (fillcache(ep, 0) != ep->fids) {
147             ep->cids = 0;       /* prevent cache flush */
148             ep->flags &= EFF_IMMUTABLE; /* maintain invariant */
149             ef_close(type);
150             return 0;
151         }
152     }
153
154     if (ep->onresize && ep->onresize(type) < 0)
155         return 0;
156     return 1;
157 }
158
159 static int
160 open_locked(char *name, int oflags, mode_t mode)
161 {
162     int rdlonly = (oflags & O_ACCMODE) == O_RDONLY;
163     int fd;
164
165 #ifdef _WIN32
166     fd = _sopen(name, oflags | O_BINARY, rdlonly ? SH_DENYNO : SH_DENYWR,
167                 mode);
168     if (fd < 0)
169         return -1;
170 #else  /* !_WIN32 */
171     struct flock lock;
172
173     fd = open(name, oflags, mode);
174     if (fd < 0)
175         return -1;
176
177     lock.l_type = rdlonly ? F_RDLCK : F_WRLCK;
178     lock.l_whence = SEEK_SET;
179     lock.l_start = lock.l_len = 0;
180     if (fcntl(fd, F_SETLK, &lock) == -1) {
181         close(fd);
182         return -1;
183     }
184 #endif  /* !_WIN32 */
185     return fd;
186 }
187
188 /*
189  * Reallocate cache for table EP to hold COUNT slots.
190  * The table must not be allocated statically.
191  * The cache may still be unmapped.
192  * If reallocation succeeds, any pointers obtained from ef_ptr()
193  * become invalid.
194  * If it fails, the cache is unchanged, and errno is set.
195  * Return non-zero on success, zero on failure.
196  */
197 static int
198 ef_realloc_cache(struct empfile *ep, int count)
199 {
200     void *cache;
201
202     if (CANT_HAPPEN(ep->flags & EFF_STATIC))
203         return 0;
204     if (CANT_HAPPEN(count < 0))
205         count = 0;
206
207     /*
208      * Avoid zero slots, because that can lead to null cache, which
209      * would be interpreted as unmapped cache.
210      */
211     if (count == 0)
212         count++;
213     cache = realloc(ep->cache, count * ep->size);
214     if (!cache)
215         return 0;
216
217     ep->cache = cache;
218     ep->csize = count;
219     return 1;
220 }
221
222 /*
223  * Open the table TYPE as view of table BASE.
224  * Return non-zero on success, zero on failure.
225  * Beware: views work only as long as BASE doesn't change size!
226  * You must call ef_close(TYPE) before closing BASE.
227  */
228 int
229 ef_open_view(int type, int base)
230 {
231     struct empfile *ep;
232
233     if (CANT_HAPPEN(!EF_IS_VIEW(type)))
234         return -1;
235     ep = &empfile[type];
236     if (CANT_HAPPEN(!(ef_flags(base) & EFF_MEM)))
237         return -1;
238
239     ep->cache = empfile[base].cache;
240     ep->csize = empfile[base].csize;
241     ep->flags |= EFF_MEM;
242     ep->baseid = empfile[base].baseid;
243     ep->cids = empfile[base].cids;
244     ep->fids = empfile[base].fids;
245     return 0;
246 }
247
248 /*
249  * Close the file-backed table TYPE (EF_SECTOR, ...).
250  * Return non-zero on success, zero on failure.
251  */
252 int
253 ef_close(int type)
254 {
255     struct empfile *ep;
256     int retval = 1;
257
258     if (ef_check(type) < 0)
259         return 0;
260     ep = &empfile[type];
261
262     if (EF_IS_VIEW(type))
263         ep->cache = NULL;
264     else {
265         if (!ef_flush(type))
266             retval = 0;
267         ep->flags &= EFF_IMMUTABLE;
268         if (!(ep->flags & EFF_STATIC)) {
269             free(ep->cache);
270             ep->cache = NULL;
271         }
272         if (close(ep->fd) < 0) {
273             logerror("Error closing %s (%s)", ep->file, strerror(errno));
274             retval = 0;
275         }
276         ep->fd = -1;
277     }
278     ep->baseid = ep->cids = ep->fids = 0;
279     if (ep->onresize && ep->onresize(type) < 0)
280         retval = 0;
281     return retval;
282 }
283
284 /*
285  * Flush file-backed table TYPE (EF_SECTOR, ...) to its backing file.
286  * Do nothing if the table is privately mapped.
287  * Update timestamps of written elements if table is EFF_TYPED.
288  * Return non-zero on success, zero on failure.
289  */
290 int
291 ef_flush(int type)
292 {
293     struct empfile *ep;
294
295     if (ef_check(type) < 0)
296         return 0;
297     ep = &empfile[type];
298     if (ep->flags & EFF_PRIVATE)
299         return 1;               /* nothing to do */
300     if (CANT_HAPPEN(ep->fd < 0))
301         return 0;
302     /*
303      * We don't know which cache entries are dirty.  ef_write() writes
304      * through, but direct updates through ef_ptr() don't.  They are
305      * allowed only with EFF_MEM.  Assume the whole cash is dirty
306      * then.
307      */
308     if (ep->flags & EFF_MEM) {
309         if (do_write(ep, ep->cache, ep->baseid, ep->cids) < 0)
310             return 0;
311     }
312
313     return 1;
314 }
315
316 /*
317  * Return pointer to element ID in table TYPE if it exists, else NULL.
318  * The table must be fully cached, i.e. flags & EFF_MEM.
319  * The caller is responsible for flushing changes he makes.
320  */
321 void *
322 ef_ptr(int type, int id)
323 {
324     struct empfile *ep;
325
326     if (ef_check(type) < 0)
327         return NULL;
328     ep = &empfile[type];
329     if (CANT_HAPPEN(!(ep->flags & EFF_MEM) || !ep->cache))
330         return NULL;
331     if (id < 0 || id >= ep->fids)
332         return NULL;
333     return ep->cache + ep->size * id;
334 }
335
336 /*
337  * Read element ID from table TYPE into buffer INTO.
338  * FIXME pass buffer size!
339  * Return non-zero on success, zero on failure.
340  */
341 int
342 ef_read(int type, int id, void *into)
343 {
344     struct empfile *ep;
345     void *cachep;
346
347     if (ef_check(type) < 0)
348         return 0;
349     ep = &empfile[type];
350     if (CANT_HAPPEN(!ep->cache))
351         return 0;
352     if (id < 0 || id >= ep->fids)
353         return 0;
354
355     if (ep->flags & EFF_MEM) {
356         cachep = ep->cache + id * ep->size;
357     } else {
358         if (ep->baseid + ep->cids <= id || ep->baseid > id) {
359             if (fillcache(ep, id) < 1)
360                 return 0;
361         }
362         cachep = ep->cache + (id - ep->baseid) * ep->size;
363     }
364     memcpy(into, cachep, ep->size);
365     ef_mark_fresh(type, into);
366
367     if (ep->postread)
368         ep->postread(id, into);
369     return 1;
370 }
371
372 /*
373  * Fill cache of file-backed EP with elements starting at ID.
374  * If any were read, return their number.
375  * Else return -1 and leave the cache unchanged.
376  */
377 static int
378 fillcache(struct empfile *ep, int id)
379 {
380     int ret;
381
382     if (CANT_HAPPEN(!ep->cache))
383         return -1;
384
385     ret = do_read(ep, ep->cache, id, MIN(ep->csize, ep->fids - id));
386     if (ret >= 0) {
387         /* cache changed */
388         ep->baseid = id;
389         ep->cids = ret;
390     }
391     return ret;
392 }
393
394 static int
395 do_read(struct empfile *ep, void *buf, int id, int count)
396 {
397     int n, ret;
398     char *p;
399
400     if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
401         return -1;
402
403     if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
404         logerror("Error seeking %s to elt %d (%s)",
405                  ep->file, id, strerror(errno));
406         return -1;
407     }
408
409     p = buf;
410     n = count * ep->size;
411     while (n > 0) {
412         ret = read(ep->fd, p, n);
413         if (ret < 0) {
414             if (errno != EINTR) {
415                 logerror("Error reading %s elt %d (%s)",
416                          ep->file,
417                          id + (int)((p - (char *)buf) / ep->size),
418                          strerror(errno));
419                 break;
420             }
421         } else if (ret == 0) {
422             logerror("Unexpected EOF reading %s elt %d",
423                      ep->file, id + (int)((p - (char *)buf) / ep->size));
424             break;
425         } else {
426             p += ret;
427             n -= ret;
428         }
429     }
430
431     return (p - (char *)buf) / ep->size;
432 }
433
434 /*
435  * Write COUNT elements starting at ID from BUF to file-backed EP.
436  * Update the timestamp if the table is EFF_TYPED.
437  * Don't actually write if table is privately mapped.
438  * Return 0 on success, -1 on error (file may be corrupt then).
439  */
440 static int
441 do_write(struct empfile *ep, void *buf, int id, int count)
442 {
443     int i, n, ret;
444     char *p;
445     struct emptypedstr *elt;
446     time_t now;
447
448     if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
449         return -1;
450
451     if (ep->flags & EFF_TYPED) {
452         now = ep->flags & EFF_NOTIME ? (time_t)-1 : time(NULL);
453         for (i = 0; i < count; i++) {
454             /*
455              * TODO Oopses here could be due to bad data corruption.
456              * Fail instead of attempting to recover?
457              */
458             elt = (struct emptypedstr *)((char *)buf + i * ep->size);
459             if (CANT_HAPPEN(elt->ef_type != ep->uid))
460                 elt->ef_type = ep->uid;
461             if (CANT_HAPPEN(elt->uid != id + i))
462                 elt->uid = id + i;
463             if (now != (time_t)-1)
464                 elt->timestamp = now;
465         }
466     }
467
468     if (ep->flags & EFF_PRIVATE)
469         return 0;
470
471     if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
472         logerror("Error seeking %s to elt %d (%s)",
473                  ep->file, id, strerror(errno));
474         return -1;
475     }
476
477     p = buf;
478     n = count * ep->size;
479     while (n > 0) {
480         ret = write(ep->fd, p, n);
481         if (ret < 0) {
482             if (errno != EINTR) {
483                 logerror("Error writing %s elt %d (%s)",
484                          ep->file,
485                          id + (int)((p - (char *)buf) / ep->size),
486                          strerror(errno));
487                 return -1;
488             }
489         } else {
490             p += ret;
491             n -= ret;
492         }
493     }
494
495     return 0;
496 }
497
498 /*
499  * Write element ID into table TYPE from buffer FROM.
500  * FIXME pass buffer size!
501  * Update timestamp in FROM if table is EFF_TYPED.
502  * If table is file-backed and not privately mapped, write through
503  * cache straight to disk.
504  * Cannot write beyond the end of fully cached table (flags & EFF_MEM).
505  * Can write at the end of partially cached table.
506  * Return non-zero on success, zero on failure.
507  */
508 int
509 ef_write(int type, int id, void *from)
510 {
511     struct empfile *ep;
512     char *cachep;
513
514     if (ef_check(type) < 0)
515         return 0;
516     ep = &empfile[type];
517     if (CANT_HAPPEN((ep->flags & (EFF_MEM | EFF_PRIVATE)) == EFF_PRIVATE))
518         return 0;
519     if (CANT_HAPPEN((ep->flags & EFF_MEM) ? id >= ep->fids : id > ep->fids))
520         return 0;               /* not implemented */
521     new_seqno(ep, from);
522     if (id >= ep->fids) {
523         /* write beyond end of file extends it, take note */
524         ep->fids = id + 1;
525         if (ep->onresize && ep->onresize(type) < 0)
526             return 0;
527     }
528     if (id >= ep->baseid && id < ep->baseid + ep->cids) {
529         cachep = ep->cache + (id - ep->baseid) * ep->size;
530         if (cachep != from)
531             must_be_fresh(ep, from);
532     } else
533         cachep = NULL;
534     if (ep->prewrite)
535         ep->prewrite(id, cachep, from);
536     if (ep->fd >= 0) {
537         if (do_write(ep, from, id, 1) < 0)
538             return 0;
539     }
540     if (cachep && cachep != from)       /* update the cache if necessary */
541         memcpy(cachep, from, ep->size);
542     return 1;
543 }
544
545 /*
546  * Change element id.
547  * BUF is an element of table TYPE.
548  * ID is its new element ID.
549  * If table is EFF_TYPED, change id and sequence number stored in BUF.
550  * Else do nothing.
551  */
552 void
553 ef_set_uid(int type, void *buf, int uid)
554 {
555     struct emptypedstr *elt;
556     struct empfile *ep;
557
558     if (ef_check(type) < 0)
559         return;
560     ep = &empfile[type];
561     if (!(ep->flags & EFF_TYPED))
562         return;
563     elt = buf;
564     if (elt->uid == uid)
565         return;
566     elt->uid = uid;
567     elt->seqno = get_seqno(ep, uid);
568 }
569
570 /*
571  * Return sequence number of element ID in table EP.
572  * Return zero if table is not EFF_TYPED (it has no sequence number
573  * then).
574  */
575 static unsigned
576 get_seqno(struct empfile *ep, int id)
577 {
578     struct emptypedstr *elt;
579
580     if (!(ep->flags & EFF_TYPED))
581         return 0;
582     if (id < 0 || id >= ep->fids)
583         return 0;
584     if (id >= ep->baseid && id < ep->baseid + ep->cids)
585         elt = (void *)(ep->cache + (id - ep->baseid) * ep->size);
586     else {
587         /* need a buffer, steal last cache slot */
588         if (ep->cids == ep->csize)
589             ep->cids--;
590         elt = (void *)(ep->cache + ep->cids * ep->size);
591         if (do_read(ep, elt, id, 1) < 0)
592             return 0;           /* deep trouble */
593     }
594     return elt->seqno;
595 }
596
597 /*
598  * Increment sequence number in BUF, which is about to be written to EP.
599  * Do nothing if table is not EFF_TYPED (it has no sequence number
600  * then).
601  */
602 static void
603 new_seqno(struct empfile *ep, void *buf)
604 {
605     struct emptypedstr *elt = buf;
606     unsigned old_seqno;
607
608     if (!(ep->flags & EFF_TYPED))
609         return;
610     old_seqno = get_seqno(ep, elt->uid);
611     CANT_HAPPEN(old_seqno != elt->seqno);
612     elt->seqno = old_seqno + 1;
613 }
614
615 void
616 ef_make_stale(void)
617 {
618     ef_generation++;
619 }
620
621 void
622 ef_mark_fresh(int type, void *buf)
623 {
624     struct empfile *ep;
625
626     if (ef_check(type) < 0)
627         return;
628     ep = &empfile[type];
629     if (!(ep->flags & EFF_TYPED))
630         return;
631     ((struct emptypedstr *)buf)->generation = ef_generation;
632 }
633
634 static void
635 must_be_fresh(struct empfile *ep, void *buf)
636 {
637     struct emptypedstr *elt = buf;
638
639     if (!(ep->flags & EFF_TYPED))
640         return;
641     CANT_HAPPEN(elt->generation != (ef_generation & 0xfff));
642 }
643
644 /*
645  * Extend table TYPE by COUNT elements.
646  * Any pointers obtained from ef_ptr() become invalid.
647  * Return non-zero on success, zero on failure.
648  */
649 int
650 ef_extend(int type, int count)
651 {
652     struct empfile *ep;
653     char *p;
654     int need_sentinel, i, id;
655
656     if (ef_check(type) < 0 || CANT_HAPPEN(EF_IS_VIEW(type)))
657         return 0;
658     ep = &empfile[type];
659     if (CANT_HAPPEN(count < 0))
660         return 0;
661
662     id = ep->fids;
663     if (ep->flags & EFF_MEM) {
664         need_sentinel = (ep->flags & EFF_SENTINEL) != 0;
665         if (id + count + need_sentinel > ep->csize) {
666             if (ep->flags & EFF_STATIC) {
667                 logerror("Can't extend %s beyond %d elements",
668                          ep->name, ep->csize - need_sentinel);
669                 return 0;
670             }
671             if (!ef_realloc_cache(ep, id + count + need_sentinel)) {
672                 logerror("Can't extend %s to %d elements (%s)",
673                          ep->name, id + count, strerror(errno));
674                 return 0;
675             }
676         }
677         p = ep->cache + id * ep->size;
678         do_blank(ep, p, id, count);
679         if (ep->fd >= 0) {
680             if (do_write(ep, p, id, count) < 0)
681                 return 0;
682         }
683         if (need_sentinel)
684             memset(ep->cache + (id + count) * ep->size, 0, ep->size);
685         ep->cids = id + count;
686     } else {
687         /* need a buffer, steal last cache slot */
688         if (ep->cids == ep->csize)
689             ep->cids--;
690         p = ep->cache + ep->cids * ep->size;
691         for (i = 0; i < count; i++) {
692             do_blank(ep, p, id + i, 1);
693             if (do_write(ep, p, id + i, 1) < 0)
694                 return 0;
695         }
696     }
697     ep->fids = id + count;
698     if (ep->onresize && ep->onresize(type) < 0)
699         return 0;
700     return 1;
701 }
702
703 /*
704  * Initialize element ID for EP in BUF.
705  * FIXME pass buffer size!
706  */
707 void
708 ef_blank(int type, int id, void *buf)
709 {
710     struct empfile *ep;
711     struct emptypedstr *elt;
712
713     if (ef_check(type) < 0)
714         return;
715     ep = &empfile[type];
716     do_blank(ep, buf, id, 1);
717     if (ep->flags & EFF_TYPED) {
718         elt = buf;
719         elt->seqno = get_seqno(ep, elt->uid);
720     }
721     ef_mark_fresh(type, buf);
722 }
723
724 /*
725  * Initialize COUNT elements of EP in BUF, starting with element ID.
726  */
727 static void
728 do_blank(struct empfile *ep, void *buf, int id, int count)
729 {
730     int i;
731     struct emptypedstr *elt;
732
733     memset(buf, 0, count * ep->size);
734     for (i = 0; i < count; i++) {
735         elt = (struct emptypedstr *)((char *)buf + i * ep->size);
736         if (ep->flags & EFF_TYPED) {
737             elt->ef_type = ep->uid;
738             elt->uid = id + i;
739         }
740         if (ep->oninit)
741             ep->oninit(elt);
742     }
743 }
744
745 /*
746  * Truncate table TYPE to COUNT elements.
747  * Any pointers obtained from ef_ptr() become invalid.
748  * Return non-zero on success, zero on failure.
749  */
750 int
751 ef_truncate(int type, int count)
752 {
753     struct empfile *ep;
754     int need_sentinel;
755
756     if (ef_check(type) < 0 || CANT_HAPPEN(EF_IS_VIEW(type)))
757         return 0;
758     ep = &empfile[type];
759     if (CANT_HAPPEN(count < 0 || count > ep->fids))
760         return 0;
761
762     if (ep->fd >= 0 && !(ep->flags & EFF_PRIVATE)) {
763         if (ftruncate(ep->fd, count * ep->size) < 0) {
764             logerror("Can't truncate %s to %d elements (%s)",
765                      ep->file, count, strerror(errno));
766             return 0;
767         }
768     }
769     ep->fids = count;
770
771     if (ep->flags & EFF_MEM) {
772         need_sentinel = (ep->flags & EFF_SENTINEL) != 0;
773         if (!(ep->flags & EFF_STATIC)) {
774             if (!ef_realloc_cache(ep, count + need_sentinel)) {
775                 logerror("Can't shrink %s cache after truncate (%s)",
776                          ep->name, strerror(errno));
777                 /* continue with unshrunk cache */
778             }
779         }
780         if (need_sentinel)
781             memset(ep->cache + count * ep->size, 0, ep->size);
782         ep->cids = count;
783     } else {
784         if (ep->baseid >= count)
785             ep->cids = 0;
786         else if (ep->cids > count - ep->baseid)
787             ep->cids = count - ep->baseid;
788     }
789
790     if (ep->onresize && ep->onresize(type) < 0)
791         return 0;
792     return 1;
793 }
794
795 struct castr *
796 ef_cadef(int type)
797 {
798     if (ef_check(type) < 0)
799         return NULL;
800     return empfile[type].cadef;
801 }
802
803 int
804 ef_nelem(int type)
805 {
806     if (ef_check(type) < 0)
807         return 0;
808     return empfile[type].fids;
809 }
810
811 int
812 ef_flags(int type)
813 {
814     if (ef_check(type) < 0)
815         return 0;
816     return empfile[type].flags;
817 }
818
819 time_t
820 ef_mtime(int type)
821 {
822     if (ef_check(type) < 0)
823         return 0;
824     if (empfile[type].fd <= 0)
825         return 0;
826     return fdate(empfile[type].fd);
827 }
828
829 /*
830  * Search for a table matching NAME, return its table type.
831  * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
832  * several.
833  */
834 int
835 ef_byname(char *name)
836 {
837     return stmtch(name, empfile, offsetof(struct empfile, name),
838                   sizeof(empfile[0]));
839 }
840
841 /*
842  * Search CHOICES[] for a table type matching NAME, return it.
843  * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
844  * several.
845  * CHOICES[] must be terminated with a negative value.
846  */
847 int
848 ef_byname_from(char *name, int choices[])
849 {
850     int res;
851     int *p;
852
853     res = M_NOTFOUND;
854     for (p = choices; *p >= 0; p++) {
855         if (ef_check(*p) < 0)
856             continue;
857         switch (mineq(name, empfile[*p].name)) {
858         case ME_MISMATCH:
859             break;
860         case ME_PARTIAL:
861             if (res >= 0)
862                 return M_NOTUNIQUE;
863             res = *p;
864             break;
865         case ME_EXACT:
866             return *p;
867         }
868     }
869     return res;
870 }
871
872 char *
873 ef_nameof(int type)
874 {
875     if (ef_check(type) < 0)
876         return "bad ef_type";
877     return empfile[type].name;
878 }
879
880 static int
881 ef_check(int type)
882 {
883     if (CANT_HAPPEN((unsigned)type >= EF_MAX))
884         return -1;
885     return 0;
886 }
887
888 /*
889  * Ensure table contains element ID.
890  * If necessary, extend it in steps of COUNT elements.
891  * Return non-zero on success, zero on failure.
892  */
893 int
894 ef_ensure_space(int type, int id, int count)
895 {
896     if (ef_check(type) < 0)
897         return 0;
898     CANT_HAPPEN(id < 0);
899
900     while (id >= empfile[type].fids) {
901         if (!ef_extend(type, count))
902             return 0;
903     }
904     return 1;
905 }