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))
76 if (how & EFF_PRIVATE)
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_PRIVATE ? 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 table TYPE (EF_SECTOR, ...) to disk.
181 * Does nothing if the table is privately mapped.
182 * Return non-zero on success, zero on failure.
189 if (ef_check(type) < 0)
192 if (ep->flags & EFF_PRIVATE)
193 return 1; /* nothing to do */
194 if (CANT_HAPPEN(ep->fd < 0))
197 * We don't know which cache entries are dirty. ef_write() writes
198 * through, but direct updates through ef_ptr() don't. They are
199 * allowed only with EFF_MEM. Assume the whole cash is dirty
202 if (ep->flags & EFF_MEM)
203 return do_write(ep, ep->cache, ep->baseid, ep->cids) >= 0;
209 * Return pointer to element ID in table TYPE if it exists, else NULL.
210 * The table must be fully cached, i.e. flags & EFF_MEM.
211 * The caller is responsible for flushing changes he makes.
214 ef_ptr(int type, int id)
218 if (ef_check(type) < 0)
221 if (CANT_HAPPEN(!(ep->flags & EFF_MEM) || !ep->cache))
223 if (id < 0 || id >= ep->fids)
225 return ep->cache + ep->size * id;
229 * Read element ID from table TYPE into buffer INTO.
230 * FIXME pass buffer size!
231 * Return non-zero on success, zero on failure.
234 ef_read(int type, int id, void *into)
239 if (ef_check(type) < 0)
242 if (CANT_HAPPEN(!ep->cache))
244 if (id < 0 || id >= ep->fids)
247 if (ep->flags & EFF_MEM) {
248 from = ep->cache + id * ep->size;
250 if (ep->baseid + ep->cids <= id || ep->baseid > id) {
251 if (fillcache(ep, id) < 1)
254 from = ep->cache + (id - ep->baseid) * ep->size;
256 memcpy(into, from, ep->size);
259 ep->postread(id, into);
264 * Fill cache of EP with elements starting at ID.
265 * If any were read, return their number.
266 * Else return -1 and leave the cache unchanged.
269 fillcache(struct empfile *ep, int start)
274 if (CANT_HAPPEN(ep->fd < 0 || !ep->cache))
277 if (lseek(ep->fd, start * ep->size, SEEK_SET) == (off_t)-1) {
278 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
283 n = ep->csize * ep->size;
285 ret = read(ep->fd, p, n);
287 if (errno != EAGAIN) {
288 logerror("Error reading %s (%s)", ep->file, strerror(errno));
291 } else if (ret == 0) {
300 return -1; /* nothing read, old cache still ok */
303 ep->cids = (p - ep->cache) / ep->size;
308 * Write COUNT elements from BUF to EP, starting at ID.
309 * Return 0 on success, -1 on error.
312 do_write(struct empfile *ep, void *buf, int id, int count)
317 if (CANT_HAPPEN(ep->fd < 0 || (ep->flags & EFF_PRIVATE)
318 || id < 0 || count < 0))
321 if (lseek(ep->fd, id * ep->size, SEEK_SET) == (off_t)-1) {
322 logerror("Error seeking %s (%s)", ep->file, strerror(errno));
327 n = count * ep->size;
329 ret = write(ep->fd, p, n);
331 if (errno != EAGAIN) {
332 logerror("Error writing %s (%s)", ep->file, strerror(errno));
333 /* FIXME if this extended file, truncate back to old size */
346 * Write element ID into table TYPE from buffer FROM.
347 * FIXME pass buffer size!
348 * If table is file-backed and not privately mapped, write through
349 * cache straight to disk.
350 * Cannot write beyond the end of fully cached table (flags & EFF_MEM).
351 * Can write at the end of partially cached table.
352 * Return non-zero on success, zero on failure.
355 ef_write(int type, int id, void *from)
360 if (ef_check(type) < 0)
363 if (CANT_HAPPEN((ep->flags & (EFF_MEM | EFF_PRIVATE)) == EFF_PRIVATE))
366 ep->prewrite(id, from);
367 if (CANT_HAPPEN((ep->flags & EFF_MEM) ? id >= ep->fids : id > ep->fids))
368 return 0; /* not implemented */
369 if (!(ep->flags & EFF_PRIVATE)) {
370 if (do_write(ep, from, id, 1) < 0)
373 if (id >= ep->baseid && id < ep->baseid + ep->cids) {
374 /* update the cache if necessary */
375 to = ep->cache + (id - ep->baseid) * ep->size;
377 memcpy(to, from, ep->size);
379 if (id >= ep->fids) {
380 /* write beyond end of file extends it, take note */
387 * Extend the file-backed table TYPE by COUNT elements.
388 * Can't extend privately mapped tables.
389 * Return non-zero on success, zero on failure.
392 ef_extend(int type, int count)
398 if (ef_check(type) < 0)
401 if (CANT_HAPPEN(ep->fd < 0 || count < 0))
403 if (CANT_HAPPEN(ep->flags & EFF_PRIVATE))
404 return 0; /* not implemented */
406 tmpobj = calloc(1, ep->size);
408 for (i = 0; i < count; i++) {
410 ep->init(id + i, tmpobj);
411 if (do_write(ep, tmpobj, id + i, 1) < 0)
416 if (ep->flags & EFF_MEM) {
417 /* FIXME lazy bastards... do this right */
418 /* XXX this will cause problems if there are ef_ptrs (to the
419 * old allocated structure) active when we do the re-open */
420 how = ep->flags & ~EFF_IMMUTABLE;
433 return empfile[type].cadef;
439 return empfile[type].fids;
445 return empfile[type].flags;
451 if (empfile[type].fd <= 0)
453 return fdate(empfile[type].fd);
457 * Search for a table matching NAME, return its table type.
458 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
462 ef_byname(char *name)
464 return stmtch(name, empfile, offsetof(struct empfile, name),
469 * Search CHOICES[] for a table type matching NAME, return it.
470 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
472 * CHOICES[] must be terminated with a negative value.
475 ef_byname_from(char *name, int choices[])
481 for (p = choices; *p >= 0; p++) {
482 if (ef_check(*p) < 0)
484 switch (mineq(name, empfile[*p].name)) {
502 if (ef_check(type) < 0)
503 return "bad ef_type";
504 return empfile[type].name;
510 if (CANT_HAPPEN((unsigned)type >= EF_MAX))
516 * Ensure file-backed table contains ID.
517 * If necessary, extend it in steps of COUNT elements.
518 * Return non-zero on success, zero on failure.
521 ef_ensure_space(int type, int id, int count)
523 if (ef_check(type) < 0)
527 while (id >= empfile[type].fids) {
528 if (!ef_extend(type, count))