]> git.pond.sub.org Git - empserver/blobdiff - src/lib/common/file.c
Remove obsolete comment
[empserver] / src / lib / common / file.c
index 46ae9e30003868dc891d0bdcdc077f651ce323c9..ebf5ab259c8aec45c196fe17c083ce5f02c89dc8 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -19,9 +19,9 @@
  *
  *  ---
  *
- *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
- *  related information and legal notices. It is expected that any future
- *  projects/authors will amend these files as needed.
+ *  See files README, COPYING and CREDITS in the root of the source
+ *  tree for related information and legal notices.  It is expected
+ *  that future projects/authors will amend these files as needed.
  *
  *  ---
  *
  *  Known contributors to this file:
  *     Dave Pare, 1989
  *     Steve McClure, 2000
- *     Markus Armbruster, 2005
+ *     Markus Armbruster, 2005-2006
  */
 
 #include <config.h>
 
 #include <errno.h>
 #include <fcntl.h>
-#include <signal.h>
-#include <stdlib.h>
-#include <string.h>
-#if !defined(_WIN32)
+#include <sys/stat.h>
+#include <sys/types.h>
 #include <unistd.h>
-#endif
-#include "common.h"
 #include "file.h"
-#include "gen.h"
 #include "match.h"
 #include "misc.h"
 #include "nsc.h"
-#include "optlist.h"
+#include "prototypes.h"
 
 static int fillcache(struct empfile *, int);
 static int do_write(struct empfile *, void *, int, int);
@@ -65,6 +60,7 @@ int
 ef_open(int type, int how)
 {
     struct empfile *ep;
+    struct flock lock;
     int oflags, fd, fsiz, size;
 
     if (ef_check(type) < 0)
@@ -84,11 +80,20 @@ ef_open(int type, int how)
 #if defined(_WIN32)
     oflags |= O_BINARY;
 #endif
-    if ((fd = open(ep->file, oflags, 0660)) < 0) {
+    if ((fd = open(ep->file, oflags, S_IRWUG)) < 0) {
        logerror("Can't open %s (%s)", ep->file, strerror(errno));
        return 0;
     }
 
+    lock.l_type = how & EFF_RDONLY ? F_RDLCK : F_WRLCK;
+    lock.l_whence = SEEK_SET;
+    lock.l_start = lock.l_len = 0;
+    if (fcntl(fd, F_SETLK, &lock) == -1) {
+       logerror("Can't lock %s (%s)", ep->file, strerror(errno));
+       close(fd);
+       return 0;
+    }
+
     /* get file size */
     fsiz = fsize(fd);
     if (fsiz % ep->size) {
@@ -101,7 +106,7 @@ ef_open(int type, int how)
 
     /* allocate cache */
     if (ep->flags & EFF_STATIC) {
-       /* ep->cache already points to space for e->csize elements */
+       /* ep->cache already points to space for ep->csize elements */
        if (how & EFF_MEM) {
            if (ep->fids > ep->csize) {
                logerror("Can't open %s: file larger than %d bytes",
@@ -114,12 +119,15 @@ ef_open(int type, int how)
        if (how & EFF_MEM)
            ep->csize = ep->fids;
        else
-           ep->csize = max(1, blksize(fd) / ep->size);
+           ep->csize = blksize(fd) / ep->size;
+       /* 0 could lead to null cache, which confuses assertions */
+       if (!ep->csize)
+           ep->csize++;
        size = ep->csize * ep->size;
        if (CANT_HAPPEN(ep->cache))
            free(ep->cache);
        ep->cache = malloc(size);
-       if (ep->cache == NULL && size) {
+       if (ep->cache == NULL) {
            logerror("Can't open %s: out of memory", ep->file);
            close(fd);
            return 0;
@@ -140,11 +148,6 @@ ef_open(int type, int how)
        }
     }
 
-    /*
-     * Could close fd if both EFF_RDONLY and EFF_MEM, but that doesn't
-     * happen, so don't bother.
-     */
-
     return 1;
 }
 
@@ -379,7 +382,7 @@ int
 ef_extend(int type, int count)
 {
     struct empfile *ep;
-    char *tmpobj;
+    void *tmpobj;
     int id, i, how;
 
     if (ef_check(type) < 0)
@@ -515,56 +518,3 @@ ef_ensure_space(int type, int id, int count)
     }
     return 1;
 }
-
-static void
-ef_fix_size(struct empfile *ep, int n)
-{
-    ep->cids = ep->fids = n;
-    ep->csize = n + 1;
-}
-
-static void
-ef_init_chr(int type, size_t size, ptrdiff_t name_offs)
-{
-    struct empfile *ep = &empfile[type];
-    char *p;
-
-    for (p = ep->cache; *((char **)(p + name_offs)); p += size) ;
-    ep->cids = ep->fids = (p - ep->cache) / size;
-}
-
-/*
- * Initialize Empire tables.
- * Must be called once, before using anything else from this module.
- */
-void
-ef_init(void)
-{
-    struct castr *ca;
-    struct empfile *ep;
-    struct symbol *lup;
-    int i;
-
-    empfile[EF_MAP].size = empfile[EF_BMAP].size = (WORLD_X * WORLD_Y) / 2;
-
-    ef_init_chr(EF_SHIP_CHR,
-               sizeof(struct mchrstr), offsetof(struct mchrstr, m_name));
-    ef_init_chr(EF_PLANE_CHR,
-               sizeof(struct plchrstr), offsetof(struct plchrstr, pl_name));
-    ef_init_chr(EF_LAND_CHR,
-               sizeof(struct lchrstr), offsetof(struct lchrstr, l_name));
-    ef_init_chr(EF_NUKE_CHR,
-               sizeof(struct nchrstr), offsetof(struct nchrstr, n_name));
-
-    ca = (struct castr *)empfile[EF_META].cache;
-    for (i = 0; ca[i].ca_name; i++) ;
-    ef_fix_size(&empfile[EF_META], i);
-
-    for (ep = empfile; ep->uid >= 0; ep++) {
-       if (ep->cadef == symbol_ca) {
-           lup = (struct symbol *)ep->cache;
-           for (i = 0; lup[i].name; i++) ;
-           ef_fix_size(ep, i);
-       }
-    }
-}