]> git.pond.sub.org Git - empserver/commitdiff
Move view open/close into src/lib/common/file.c
authorMarkus Armbruster <armbru@pond.sub.org>
Mon, 1 Sep 2008 14:38:48 +0000 (10:38 -0400)
committerMarkus Armbruster <armbru@pond.sub.org>
Thu, 4 Sep 2008 01:17:46 +0000 (21:17 -0400)
Really belongs there, because it manipulates empfile[].

New ef_open_view() to replace ef_init_view().  Make ef_close() cope
with views, and remove ef_fina_view().  Make ef_extend() and
ef_truncate() oops on views.

include/file.h
src/lib/common/file.c
src/lib/subs/fileinit.c

index 3d8587bd50b4a10afe84f311782f37a3b82f094c..9e9b166e2180282503e96cd2c0b63b734c6eaee3 100644 (file)
@@ -197,6 +197,7 @@ extern void *ef_ptr(int, int);
 extern char *ef_nameof(int);
 extern time_t ef_mtime(int);
 extern int ef_open(int, int, int);
+extern int ef_open_view(int, int);
 extern int ef_close(int);
 extern int ef_flush(int);
 extern void ef_blank(int, int, void *);
index 81b0b97d8e2987f06712b3b148bc259bbee2cf0b..02d6ca193aeff3ac8c05febb66ad117afbd0dee5 100644 (file)
@@ -193,6 +193,32 @@ ef_realloc_cache(struct empfile *ep, int count)
     return 1;
 }
 
+/*
+ * Open the table TYPE as view of table BASE.
+ * Return non-zero on success, zero on failure.
+ * Beware: views work only as long as BASE doesn't change size!
+ * You must call ef_close(TYPE) before closing BASE.
+ */
+int
+ef_open_view(int type, int base)
+{
+    struct empfile *ep;
+
+    if (CANT_HAPPEN(!EF_IS_VIEW(type)))
+       return -1;
+    ep = &empfile[type];
+    if (CANT_HAPPEN(!(ef_flags(base) & EFF_MEM)))
+       return -1;
+
+    ep->cache = empfile[base].cache;
+    ep->csize = empfile[base].csize;
+    ep->flags |= EFF_MEM;
+    ep->baseid = empfile[base].baseid;
+    ep->cids = empfile[base].cids;
+    ep->fids = empfile[base].fids;
+    return 0;
+}
+
 /*
  * Close the file-backed table TYPE (EF_SECTOR, ...).
  * Return non-zero on success, zero on failure.
@@ -201,25 +227,33 @@ int
 ef_close(int type)
 {
     struct empfile *ep;
-    int retval;
+    int retval = 1;
 
-    retval = ef_flush(type);
+    if (ef_check(type) < 0)
+       return 0;
     ep = &empfile[type];
-    ep->flags &= EFF_IMMUTABLE;
-    if (!(ep->flags & EFF_STATIC)) {
-       free(ep->cache);
+
+    if (EF_IS_VIEW(type))
        ep->cache = NULL;
+    else {
+       if (!ef_flush(type))
+           retval = 0;
+       ep->flags &= EFF_IMMUTABLE;
+       if (!(ep->flags & EFF_STATIC)) {
+           free(ep->cache);
+           ep->cache = NULL;
+       }
+       if (close(ep->fd) < 0) {
+           logerror("Error closing %s (%s)", ep->file, strerror(errno));
+           retval = 0;
+       }
+       ep->fd = -1;
     }
-    if (close(ep->fd) < 0) {
-       logerror("Error closing %s (%s)", ep->file, strerror(errno));
-       retval = 0;
-    }
-    ep->fd = -1;
     return retval;
 }
 
 /*
- * Flush table TYPE (EF_SECTOR, ...) to disk.
+ * Flush file-backed table TYPE (EF_SECTOR, ...) to its backing file.
  * Do nothing if the table is privately mapped.
  * Update timestamps of written elements if table is EFF_TYPED.
  * Return non-zero on success, zero on failure.
@@ -557,7 +591,7 @@ ef_extend(int type, int count)
     char *p;
     int need_sentinel, i, id;
 
-    if (ef_check(type) < 0)
+    if (ef_check(type) < 0 || CANT_HAPPEN(EF_IS_VIEW(type)))
        return 0;
     ep = &empfile[type];
     if (CANT_HAPPEN(count < 0))
@@ -652,7 +686,7 @@ ef_truncate(int type, int count)
     struct empfile *ep;
     int need_sentinel;
 
-    if (ef_check(type) < 0)
+    if (ef_check(type) < 0 || CANT_HAPPEN(EF_IS_VIEW(type)))
        return 0;
     ep = &empfile[type];
     if (CANT_HAPPEN(count < 0 || count > ep->fids))
index b6511df03d512e2174d3d286cfe6766cccb7aa27..8876d47c66cef83e76656b78923b13694e84bf26 100644 (file)
@@ -55,8 +55,6 @@ static struct fileinit fileinit[] = {
 
 static void ef_open_srv(void);
 static void ef_close_srv(void);
-static int ef_init_view(int, int);
-static void ef_fina_view(int);
 
 /*
  * Initialize empfile for full server operations.
@@ -107,7 +105,7 @@ ef_open_srv(void)
     failed |= !ef_open(EF_LOST, 0, -1);
     failed |= !ef_open(EF_REALM, EFF_MEM, MAXNOC * MAXNOR);
     if (!failed)
-       failed |= ef_init_view(EF_COUNTRY, EF_NATION);
+       failed |= ef_open_view(EF_COUNTRY, EF_NATION);
     if (failed) {
        logerror("Missing files, giving up");
        exit(EXIT_FAILURE);
@@ -117,7 +115,7 @@ ef_open_srv(void)
 static void
 ef_close_srv(void)
 {
-    ef_fina_view(EF_COUNTRY);
+    ef_close(EF_COUNTRY);
     ef_close(EF_NATION);
     ef_close(EF_SECTOR);
     ef_close(EF_SHIP);
@@ -136,23 +134,3 @@ ef_close_srv(void)
     ef_close(EF_LOST);
     ef_close(EF_REALM);
 }
-
-static int
-ef_init_view(int type, int base)
-{
-    if (CANT_HAPPEN(!(empfile[base].flags & EFF_MEM)))
-       return -1;
-    empfile[type].cache = empfile[base].cache;
-    empfile[type].csize = empfile[base].csize;
-    empfile[type].flags |= EFF_MEM;
-    empfile[type].baseid = empfile[base].baseid;
-    empfile[type].cids = empfile[base].cids;
-    empfile[type].fids = empfile[base].fids;
-    return 0;
-}
-
-static void
-ef_fina_view(int type)
-{
-    empfile[type].cache = NULL;
-}