2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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.
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
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.
28 * file.c: Operations on Empire tables (`files' for historical reasons)
30 * Known contributors to this file:
33 * Markus Armbruster, 2005-2006
41 #include <sys/types.h>
47 #include "prototypes.h"
49 static int fillcache(struct empfile *, int);
50 static int do_write(struct empfile *, void *, int, int);
53 * Open the file-backed table TYPE (EF_SECTOR, ...).
54 * HOW are flags to control operation. Naturally, immutable flags are
56 * Return non-zero on success, zero on failure.
57 * You must call ef_close() before the next ef_open().
60 ef_open(int type, int how)
64 int oflags, fd, fsiz, size;
66 if (ef_check(type) < 0)
68 if (CANT_HAPPEN(how & EFF_IMMUTABLE))
69 how &= ~EFF_IMMUTABLE;
73 if (CANT_HAPPEN(ep->fd >= 0))
79 oflags |= O_CREAT | O_TRUNC;
83 if ((fd = open(ep->file, oflags, S_IRWUG)) < 0) {
84 logerror("Can't open %s (%s)", ep->file, strerror(errno));
88 lock.l_type = how & EFF_RDONLY ? F_RDLCK : F_WRLCK;
89 lock.l_whence = SEEK_SET;
90 lock.l_start = lock.l_len = 0;
91 if (fcntl(fd, F_SETLK, &lock) == -1) {
92 logerror("Can't lock %s (%s)", ep->file, strerror(errno));
99 if (fsiz % ep->size) {
100 logerror("Can't open %s (file size not a multiple of record size %d)",
105 ep->fids = fsiz / ep->size;
108 if (ep->flags & EFF_STATIC) {
109 /* ep->cache already points to space for ep->csize elements */
111 if (ep->fids > ep->csize) {
112 logerror("Can't open %s: file larger than %d bytes",
113 ep->file, ep->fids * ep->size);
120 ep->csize = ep->fids;
122 ep->csize = blksize(fd) / ep->size;
123 /* 0 could lead to null cache, which confuses assertions */
126 size = ep->csize * ep->size;
127 if (CANT_HAPPEN(ep->cache))
129 ep->cache = malloc(size);
130 if (ep->cache == NULL) {
131 logerror("Can't open %s: out of memory", ep->file);
138 ep->flags = (ep->flags & EFF_IMMUTABLE) | (how & ~EFF_CREATE);
141 /* map file into cache */
142 if ((how & EFF_MEM) && ep->fids) {
143 if (fillcache(ep, 0) != ep->fids) {
144 ep->cids = 0; /* prevent cache flush */
145 ep->flags &= EFF_IMMUTABLE; /* maintain invariant */
155 * Close the file-backed table TYPE (EF_SECTOR, ...).
156 * Return non-zero on success, zero on failure.
164 retval = ef_flush(type);
166 ep->flags &= EFF_IMMUTABLE;
167 if (!(ep->flags & EFF_STATIC)) {
171 if (close(ep->fd) < 0) {
172 logerror("Error closing %s (%s)", ep->name, strerror(errno));
180 * Flush file-backed table TYPE (EF_SECTOR, ...) to disk.
181 * Return non-zero on success, zero on failure.
188 if (ef_check(type) < 0)
191 if (CANT_HAPPEN(ep->fd < 0))
194 * We don't know which cache entries are dirty. ef_write() writes
195 * through, but direct updates through ef_ptr() don't. They are
196 * allowed only with EFF_MEM. Assume the whole cash is dirty
199 if (!(ep->flags & EFF_RDONLY) && (ep->flags & EFF_MEM))
200 return do_write(ep, ep->cache, ep->baseid, ep->cids) >= 0;
206 * Return pointer to element ID in table TYPE if it exists, else NULL.
207 * The table must be fully cached, i.e. flags & EFF_MEM.
208 * The caller is responsible for flushing changes he makes.
211 ef_ptr(int type, int id)
215 if (ef_check(type) < 0)
218 if (CANT_HAPPEN(!(ep->flags & EFF_MEM) || !ep->cache))
220 if (id < 0 || id >= ep->fids)
222 return ep->cache + ep->size * id;
226 * Read element ID from table TYPE into buffer INTO.
227 * FIXME pass buffer size!
228 * Return non-zero on success, zero on failure.
231 ef_read(int type, int id, void *into)
236 if (ef_check(type) < 0)
239 if (CANT_HAPPEN(!ep->cache))
241 if (id < 0 || id >= ep->fids)
244 if (ep->flags & EFF_MEM) {
245 from = ep->cache + id * ep->size;
247 if (ep->baseid + ep->cids <= id || ep->baseid > id) {
248 if (fillcache(ep, id) < 1)
251 from = ep->cache + (id - ep->baseid) * ep->size;
253 memcpy(into, from, ep->size);
256 ep->postread(id, into);
261 * Fill cache of EP with elements starting at ID.
262 * If any were read, return their number.
263 * Else return -1 and leave the cache unchanged.
266 fillcache(struct empfile *ep, int start)
271 if (CANT_HAPPEN(ep->fd < 0 || !ep->cache))
274 if (lseek(ep->fd, start * ep->size, SEEK_SET) == (off_t)-1) {
275 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
280 n = ep->csize * ep->size;
282 ret = read(ep->fd, p, n);
284 if (errno != EAGAIN) {
285 logerror("Error reading %s (%s)", ep->file, strerror(errno));
288 } else if (ret == 0) {
297 return -1; /* nothing read, old cache still ok */
300 ep->cids = (p - ep->cache) / ep->size;
305 * Write COUNT elements from BUF to EP, starting at ID.
306 * Return 0 on success, -1 on error.
309 do_write(struct empfile *ep, void *buf, int id, int count)
314 if (CANT_HAPPEN(ep->fd < 0 || id < 0 || count < 0))
317 if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
318 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
323 n = count * ep->size;
325 ret = write(ep->fd, p, n);
327 if (errno != EAGAIN) {
328 logerror("Error writing %s (%s)", ep->file, strerror(errno));
329 /* FIXME if this extended file, truncate back to old size */
342 * Write element ID into file-backed table TYPE from buffer FROM.
343 * FIXME pass buffer size!
344 * Write through cache straight to disk.
345 * Cannot write beyond the end of fully cached table (flags & EFF_MEM).
346 * Can write at the end of partially cached table.
347 * Return non-zero on success, zero on failure.
350 ef_write(int type, int id, void *from)
355 if (ef_check(type) < 0)
359 ep->prewrite(id, from);
360 if (CANT_HAPPEN((ep->flags & EFF_MEM) ? id >= ep->fids : id > ep->fids))
361 return 0; /* not implemented */
362 if (do_write(ep, from, id, 1) < 0)
364 if (id >= ep->baseid && id < ep->baseid + ep->cids) {
365 /* update the cache if necessary */
366 to = ep->cache + (id - ep->baseid) * ep->size;
368 memcpy(to, from, ep->size);
370 if (id >= ep->fids) {
371 /* write beyond end of file extends it, take note */
378 * Extend the file-backed table TYPE by COUNT elements.
379 * Return non-zero on success, zero on failure.
382 ef_extend(int type, int count)
388 if (ef_check(type) < 0)
391 if (CANT_HAPPEN(ep->fd < 0 || count < 0))
394 tmpobj = calloc(1, ep->size);
396 for (i = 0; i < count; i++) {
398 ep->init(id + i, tmpobj);
399 if (do_write(ep, tmpobj, id + i, 1) < 0)
404 if (ep->flags & EFF_MEM) {
405 /* FIXME lazy bastards... do this right */
406 /* XXX this will cause problems if there are ef_ptrs (to the
407 * old allocated structure) active when we do the re-open */
408 how = ep->flags & ~EFF_IMMUTABLE;
421 return empfile[type].cadef;
427 return empfile[type].fids;
433 return empfile[type].flags;
439 if (empfile[type].fd <= 0)
441 return fdate(empfile[type].fd);
445 * Search for a table matching NAME, return its table type.
446 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
450 ef_byname(char *name)
452 return stmtch(name, empfile, offsetof(struct empfile, name),
457 * Search CHOICES[] for a table type matching NAME, return it.
458 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
460 * CHOICES[] must be terminated with a negative value.
463 ef_byname_from(char *name, int choices[])
469 for (p = choices; *p >= 0; p++) {
470 if (ef_check(*p) < 0)
472 switch (mineq(name, empfile[*p].name)) {
490 if (ef_check(type) < 0)
491 return "bad ef_type";
492 return empfile[type].name;
498 if (CANT_HAPPEN((unsigned)type >= EF_MAX))
504 * Ensure file-backed table contains ID.
505 * If necessary, extend it in steps of COUNT elements.
506 * Return non-zero on success, zero on failure.
509 ef_ensure_space(int type, int id, int count)
511 if (ef_check(type) < 0)
515 while (id >= empfile[type].fids) {
516 if (!ef_extend(type, count))