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