]> git.pond.sub.org Git - empserver/commitdiff
Clean up how a view's base table is defined
authorMarkus Armbruster <armbru@pond.sub.org>
Sun, 1 May 2011 11:59:17 +0000 (13:59 +0200)
committerMarkus Armbruster <armbru@pond.sub.org>
Sat, 25 Jun 2011 14:50:06 +0000 (16:50 +0200)
New struct empfile member base replaces ef_open_view() parameter base.
Cleaner, because the base table is a property of the view, not of how
it's used.

Use it to clean up verify_fail()'s base table access, and for extra
sanity checks in ef_open() and ef_open_view().

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

index 47964a7111ef91ea621f612a99af2ceb26cceca8..5a8dc0ce6d476a28f7c9f8e9465524ec391d964f 100644 (file)
@@ -42,6 +42,7 @@ struct empfile {
     char *file;                        /* file name, relative to gamedir for
                                   game state, to builtindir for config */
     struct castr *cadef;       /* table column selectors (column meta-data) */
     char *file;                        /* file name, relative to gamedir for
                                   game state, to builtindir for config */
     struct castr *cadef;       /* table column selectors (column meta-data) */
+    int base;                  /* view's base table, else EF_BAD */
     int size;                  /* size of a table entry */
     int flags;                 /* only EFF_IMMUTABLE immutable, see below
                                   for use of remaining bits */
     int size;                  /* size of a table entry */
     int flags;                 /* only EFF_IMMUTABLE immutable, see below
                                   for use of remaining bits */
@@ -199,7 +200,7 @@ enum {
 };
 
 #define EF_IS_GAME_STATE(type) (EF_SECTOR <= (type) && (type) <= EF_DYNMAX)
 };
 
 #define EF_IS_GAME_STATE(type) (EF_SECTOR <= (type) && (type) <= EF_DYNMAX)
-#define EF_IS_VIEW(type) (EF_COUNTRY <= (type) && (type) < EF_MAX)
+#define EF_IS_VIEW(type) (empfile[(type)].base != EF_BAD)
 
 extern struct castr *ef_cadef(int);
 extern int ef_read(int, int, void *);
 
 extern struct castr *ef_cadef(int);
 extern int ef_read(int, int, void *);
@@ -209,7 +210,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 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_open_view(int);
 extern int ef_close(int);
 extern int ef_flush(int);
 extern void ef_blank(int, int, void *);
 extern int ef_close(int);
 extern int ef_flush(int);
 extern void ef_blank(int, int, void *);
index 60003e9f97762e7d00f634a9d3e33d923303156c..c32b4403ed2bffb1efc7c9a66c92d88d59617e11 100644 (file)
@@ -48,14 +48,11 @@ static void verify_fail(int, int, struct castr *, int, char *, ...)
 static void
 verify_fail(int type, int row, struct castr *ca, int idx, char *fmt, ...)
 {
 static void
 verify_fail(int type, int row, struct castr *ca, int idx, char *fmt, ...)
 {
-    int i;
+    int base = empfile[type].base < 0 ? type : empfile[type].base;
     va_list ap;
 
     va_list ap;
 
-    /* Find base table of view, if any */
-    for (i = 0; empfile[i].cache == empfile[type].cache; i++) ;
-
     fprintf(stderr, "%s %s uid %d",
     fprintf(stderr, "%s %s uid %d",
-           EF_IS_GAME_STATE(i) ? "File" : "Config",
+           EF_IS_GAME_STATE(base) ? "File" : "Config",
            ef_nameof(type), row);
     if (ca) {
        fprintf(stderr, " field %s", ca->ca_name);
            ef_nameof(type), row);
     if (ca) {
        fprintf(stderr, " field %s", ca->ca_name);
index a200fca27bbc2e464e3920f5e2f59342b62c7f30..d6db60fd9e00e2bcc88b1c178c78d94abe4cf0fb 100644 (file)
@@ -83,7 +83,7 @@ ef_open(int type, int how, int nelt)
 
     /* open file */
     ep = &empfile[type];
 
     /* open file */
     ep = &empfile[type];
-    if (CANT_HAPPEN(ep->fd >= 0))
+    if (CANT_HAPPEN(!ep->file || ep->base != EF_BAD || ep->fd >= 0))
        return 0;
     oflags = O_RDWR;
     if (how & EFF_PRIVATE)
        return 0;
     oflags = O_RDWR;
     if (how & EFF_PRIVATE)
@@ -220,20 +220,28 @@ ef_realloc_cache(struct empfile *ep, int count)
 }
 
 /*
 }
 
 /*
- * Open the table TYPE as view of table BASE.
+ * Open the table TYPE, which is a view of a base table
+ * The table must not be already open.
  * Return non-zero on success, zero on failure.
  * 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.
+ * Beware: views work only as long as the base table doesn't change size!
+ * You must close the view before closing its base table.
  */
 int
  */
 int
-ef_open_view(int type, int base)
+ef_open_view(int type)
 {
     struct empfile *ep;
 {
     struct empfile *ep;
+    int base;
 
 
-    if (CANT_HAPPEN(!EF_IS_VIEW(type)))
-       return -1;
+    if (ef_check(type) < 0)
+       return 0;
     ep = &empfile[type];
     ep = &empfile[type];
-    if (CANT_HAPPEN(!(ef_flags(base) & EFF_MEM)))
+    base = ep->base;
+    if (ef_check(base) < 0)
+       return 0;
+    if (CANT_HAPPEN(!(ef_flags(base) & EFF_MEM)
+                   || ep->file || ep->size != empfile[base].size
+                   || ep->cache || ep->oninit || ep->postread
+                   || ep->prewrite || ep->onresize))
        return -1;
 
     ep->cache = empfile[base].cache;
        return -1;
 
     ep->cache = empfile[base].cache;
index 70a049f529b7ac586d5f1004e2e17e33be470c86..a4f876202732a08221e5233c1f6fd6465f9cae64 100644 (file)
@@ -118,42 +118,42 @@ struct empfile empfile[] = {
      * All caches unmapped.  EF_MAP and EF_BMAP get a bogus size here.
      * Fixed up by empfile_fixup().
      */
      * All caches unmapped.  EF_MAP and EF_BMAP get a bogus size here.
      * Fixed up by empfile_fixup().
      */
-    {EF_SECTOR, "sect", "sector", sect_ca,
+    {EF_SECTOR, "sect", "sector", sect_ca, EF_BAD,
      UNMAPPED_CACHE(struct sctstr, EFF_TYPED | EFF_XY | EFF_OWNER)},
      UNMAPPED_CACHE(struct sctstr, EFF_TYPED | EFF_XY | EFF_OWNER)},
-    {EF_SHIP, "ship", "ship", ship_ca,
+    {EF_SHIP, "ship", "ship", ship_ca, EF_BAD,
      UNMAPPED_CACHE(struct shpstr,
                    EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP)},
      UNMAPPED_CACHE(struct shpstr,
                    EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP)},
-    {EF_PLANE, "plane", "plane", plane_ca,
+    {EF_PLANE, "plane", "plane", plane_ca, EF_BAD,
      UNMAPPED_CACHE(struct plnstr,
                    EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP)},
      UNMAPPED_CACHE(struct plnstr,
                    EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP)},
-    {EF_LAND, "land", "land", land_ca,
+    {EF_LAND, "land", "land", land_ca, EF_BAD,
      UNMAPPED_CACHE(struct lndstr,
                    EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP)},
      UNMAPPED_CACHE(struct lndstr,
                    EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP)},
-    {EF_NUKE, "nuke", "nuke", nuke_ca,
+    {EF_NUKE, "nuke", "nuke", nuke_ca, EF_BAD,
      UNMAPPED_CACHE(struct nukstr, EFF_TYPED | EFF_XY | EFF_OWNER)},
      UNMAPPED_CACHE(struct nukstr, EFF_TYPED | EFF_XY | EFF_OWNER)},
-    {EF_NEWS, "news", "news", news_ca,
+    {EF_NEWS, "news", "news", news_ca, EF_BAD,
      UNMAPPED_CACHE(struct nwsstr, 0)},
      UNMAPPED_CACHE(struct nwsstr, 0)},
-    {EF_TREATY, "treaty", "treaty", treaty_ca,
+    {EF_TREATY, "treaty", "treaty", treaty_ca, EF_BAD,
      UNMAPPED_CACHE(struct trtstr, EFF_TYPED)},
      UNMAPPED_CACHE(struct trtstr, EFF_TYPED)},
-    {EF_TRADE, "trade", "trade", trade_ca,
+    {EF_TRADE, "trade", "trade", trade_ca, EF_BAD,
      UNMAPPED_CACHE(struct trdstr, EFF_TYPED | EFF_OWNER)},
      UNMAPPED_CACHE(struct trdstr, EFF_TYPED | EFF_OWNER)},
-    {EF_POWER, "pow", "power", NULL,
+    {EF_POWER, "pow", "power", NULL, EF_BAD,
      UNMAPPED_CACHE(struct powstr, 0)},
      UNMAPPED_CACHE(struct powstr, 0)},
-    {EF_NATION, "nat", "nation", nat_ca,
+    {EF_NATION, "nat", "nation", nat_ca, EF_BAD,
      UNMAPPED_CACHE(struct natstr, EFF_TYPED | EFF_OWNER)},
      UNMAPPED_CACHE(struct natstr, EFF_TYPED | EFF_OWNER)},
-    {EF_LOAN, "loan", "loan", loan_ca,
+    {EF_LOAN, "loan", "loan", loan_ca, EF_BAD,
      UNMAPPED_CACHE(struct lonstr, EFF_TYPED)},
      UNMAPPED_CACHE(struct lonstr, EFF_TYPED)},
-    {EF_MAP, "map", "map", NULL,
+    {EF_MAP, "map", "map", NULL, EF_BAD,
      0, 0, NULL, 0, 0, 0, 0, -1, NULL, NULL, NULL, NULL},
      0, 0, NULL, 0, 0, 0, 0, -1, NULL, NULL, NULL, NULL},
-    {EF_BMAP, "bmap", "bmap", NULL,
+    {EF_BMAP, "bmap", "bmap", NULL, EF_BAD,
      0, 0, NULL, 0, 0, 0, 0, -1, NULL, NULL, NULL, NULL},
      0, 0, NULL, 0, 0, 0, 0, -1, NULL, NULL, NULL, NULL},
-    {EF_COMM, "commodity", "commodity", commodity_ca,
+    {EF_COMM, "commodity", "commodity", commodity_ca, EF_BAD,
      UNMAPPED_CACHE(struct comstr, EFF_TYPED | EFF_OWNER)},
      UNMAPPED_CACHE(struct comstr, EFF_TYPED | EFF_OWNER)},
-    {EF_LOST, "lost", "lostitems", lost_ca,
+    {EF_LOST, "lost", "lostitems", lost_ca, EF_BAD,
      UNMAPPED_CACHE(struct loststr, EFF_TYPED | EFF_OWNER)},
      UNMAPPED_CACHE(struct loststr, EFF_TYPED | EFF_OWNER)},
-    {EF_REALM, "realm", "realms", realm_ca,
+    {EF_REALM, "realm", "realms", realm_ca, EF_BAD,
      UNMAPPED_CACHE(struct realmstr, EFF_TYPED | EFF_OWNER)},
      UNMAPPED_CACHE(struct realmstr, EFF_TYPED | EFF_OWNER)},
-    {EF_GAME, "game", "game", game_ca,
+    {EF_GAME, "game", "game", game_ca, EF_BAD,
      UNMAPPED_CACHE(struct gamestr, EFF_TYPED)},
 
     /*
      UNMAPPED_CACHE(struct gamestr, EFF_TYPED)},
 
     /*
@@ -164,40 +164,40 @@ struct empfile empfile[] = {
      * member are compiled in.  The others are empty; use
      * read_builtin_tables() to fill them.
      */
      * member are compiled in.  The others are empty; use
      * read_builtin_tables() to fill them.
      */
-    {EF_ITEM, "item", "item.config", ichr_ca,
+    {EF_ITEM, "item", "item.config", ichr_ca, EF_BAD,
      ARRAY_CACHE(ichr, EFF_CFG)},
      ARRAY_CACHE(ichr, EFF_CFG)},
-    {EF_PRODUCT, "product", "product.config", pchr_ca,
+    {EF_PRODUCT, "product", "product.config", pchr_ca, EF_BAD,
      ARRAY_CACHE(pchr, EFF_CFG)},
      ARRAY_CACHE(pchr, EFF_CFG)},
-    {EF_SECTOR_CHR, "sect-chr", "sect.config", dchr_ca,
+    {EF_SECTOR_CHR, "sect-chr", "sect.config", dchr_ca, EF_BAD,
      ARRAY_CACHE(dchr, EFF_CFG)},
      ARRAY_CACHE(dchr, EFF_CFG)},
-    {EF_SHIP_CHR, "ship-chr", "ship.config", mchr_ca,
+    {EF_SHIP_CHR, "ship-chr", "ship.config", mchr_ca, EF_BAD,
      ARRAY_CACHE(mchr, EFF_CFG)},
      ARRAY_CACHE(mchr, EFF_CFG)},
-    {EF_PLANE_CHR, "plane-chr", "plane.config", plchr_ca,
+    {EF_PLANE_CHR, "plane-chr", "plane.config", plchr_ca, EF_BAD,
      ARRAY_CACHE(plchr, EFF_CFG)},
      ARRAY_CACHE(plchr, EFF_CFG)},
-    {EF_LAND_CHR, "land-chr", "land.config", lchr_ca,
+    {EF_LAND_CHR, "land-chr", "land.config", lchr_ca, EF_BAD,
      ARRAY_CACHE(lchr, EFF_CFG)},
      ARRAY_CACHE(lchr, EFF_CFG)},
-    {EF_NUKE_CHR, "nuke-chr", "nuke.config", nchr_ca,
+    {EF_NUKE_CHR, "nuke-chr", "nuke.config", nchr_ca, EF_BAD,
      ARRAY_CACHE(nchr, EFF_CFG)},
      ARRAY_CACHE(nchr, EFF_CFG)},
-    {EF_NEWS_CHR, "news-chr", NULL, rpt_ca,
+    {EF_NEWS_CHR, "news-chr", NULL, rpt_ca, EF_BAD,
      ARRAY_TABLE(rpt, EFF_CFG)},
      ARRAY_TABLE(rpt, EFF_CFG)},
-    {EF_INFRASTRUCTURE, "infrastructure", "infra.config", intrchr_ca,
+    {EF_INFRASTRUCTURE, "infrastructure", "infra.config", intrchr_ca, EF_BAD,
      ARRAY_CACHE(intrchr, EFF_CFG)},
     /*
      * Update schedule table.  Use read_schedule() to fill.
      */
      ARRAY_CACHE(intrchr, EFF_CFG)},
     /*
      * Update schedule table.  Use read_schedule() to fill.
      */
-    {EF_UPDATES, "updates", NULL, update_ca,
+    {EF_UPDATES, "updates", NULL, update_ca, EF_BAD,
      ARRAY_CACHE(update_time, EFF_CFG)},
     /*
      * Special tables.  EF_META gets bogus size, cids and fids here.
      * Fixed up by empfile_init().  EF_VERSION's cadef is set by
      * nsc_init().
      */
      ARRAY_CACHE(update_time, EFF_CFG)},
     /*
      * Special tables.  EF_META gets bogus size, cids and fids here.
      * Fixed up by empfile_init().  EF_VERSION's cadef is set by
      * nsc_init().
      */
-    {EF_TABLE, "table", NULL, empfile_ca,
+    {EF_TABLE, "table", NULL, empfile_ca, EF_BAD,
      ARRAY_TABLE(empfile, EFF_CFG)},
      ARRAY_TABLE(empfile, EFF_CFG)},
-    {EF_VERSION, "version", NULL, NULL,
+    {EF_VERSION, "version", NULL, NULL, EF_BAD,
      sizeof(PACKAGE_STRING), EFF_STATIC, version, 1, 0, 1, 1, -1,
      NULL, NULL, NULL, NULL},
      sizeof(PACKAGE_STRING), EFF_STATIC, version, 1, 0, 1, 1, -1,
      NULL, NULL, NULL, NULL},
-    {EF_META, "meta", NULL, mdchr_ca,
+    {EF_META, "meta", NULL, mdchr_ca, EF_BAD,
      PTR_CACHE(mdchr_ca, EFF_CFG)},
 
     /*
      PTR_CACHE(mdchr_ca, EFF_CFG)},
 
     /*
@@ -206,8 +206,8 @@ struct empfile empfile[] = {
      * These get bogus csize, cids and fids here.  Fixed up by
      * empfile_init().
      */
      * These get bogus csize, cids and fids here.  Fixed up by
      * empfile_init().
      */
-#define SYMTAB(type, name, tab) \
-       {(type), (name), NULL, symbol_ca, PTR_CACHE((tab), EFF_CFG)}
+#define SYMTAB(type, name, tab) {(type), (name), NULL, symbol_ca, EF_BAD, \
+                                PTR_CACHE((tab), EFF_CFG)}
     SYMTAB(EF_AGREEMENT_STATUS, "agreement-status", agreement_statuses),
     SYMTAB(EF_LAND_CHR_FLAGS, "land-chr-flags", land_chr_flags),
     SYMTAB(EF_LEVEL, "level", level),
     SYMTAB(EF_AGREEMENT_STATUS, "agreement-status", agreement_statuses),
     SYMTAB(EF_LAND_CHR_FLAGS, "land-chr-flags", land_chr_flags),
     SYMTAB(EF_LEVEL, "level", level),
@@ -231,11 +231,11 @@ struct empfile empfile[] = {
     SYMTAB(EF_TREATY_FLAGS, "treaty-flags", treaty_flags),
 
     /* Views */
     SYMTAB(EF_TREATY_FLAGS, "treaty-flags", treaty_flags),
 
     /* Views */
-    {EF_COUNTRY, "country", NULL, cou_ca,
+    {EF_COUNTRY, "country", NULL, cou_ca, EF_NATION,
      UNMAPPED_CACHE(struct natstr, EFF_TYPED | EFF_OWNER)},
 
     /* Sentinel */
      UNMAPPED_CACHE(struct natstr, EFF_TYPED | EFF_OWNER)},
 
     /* Sentinel */
-    {EF_BAD, NULL, NULL, NULL,
+    {EF_BAD, NULL, NULL, NULL, EF_BAD,
      0, 0, NULL, 0, 0, 0, 0, -1, NULL, NULL, NULL, NULL},
 };
 
      0, 0, NULL, 0, 0, 0, 0, -1, NULL, NULL, NULL, NULL},
 };
 
index b670de43574c373a105c0ecd264ff2edd18e3196..913f507c68d2a70cc0a415dc7830fc3f912c5c99 100644 (file)
@@ -110,7 +110,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_open(EF_LOST, 0, -1);
     failed |= !ef_open(EF_REALM, EFF_MEM, MAXNOC * MAXNOR);
     if (!failed)
-       failed |= ef_open_view(EF_COUNTRY, EF_NATION);
+       failed |= ef_open_view(EF_COUNTRY);
     if (failed) {
        logerror("Missing files, giving up");
        exit(EXIT_FAILURE);
     if (failed) {
        logerror("Missing files, giving up");
        exit(EXIT_FAILURE);