]> git.pond.sub.org Git - empserver/blobdiff - src/lib/common/ef_verify.c
Fix empdump not to touch plane file when import fails
[empserver] / src / lib / common / ef_verify.c
index fb18e663479669865d61e4baf1f234598534a94d..139d04315a9d411f117ac25c4d6f77e5bca45e47 100644 (file)
@@ -28,7 +28,7 @@
  *
  *  Known contributors to this file:
  *     Ron Koenderink, 2005
- *     Markus Armbruster, 2006-2010
+ *     Markus Armbruster, 2006-2011
  */
 
 #include <config.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include "empobj.h"
 #include "file.h"
 #include "misc.h"
 #include "nsc.h"
-#include "plane.h"
 #include "product.h"
 
 static void verify_fail(int, int, struct castr *, int, char *, ...)
@@ -122,6 +122,13 @@ verify_tabref(int type, int row, struct castr *ca, int idx, long val)
                        val, ef_nameof(tabno), ef_nelem(tabno));
            return -1;
        }
+       /* laziness: assumes TABNO is EFF_MEM */
+       if (val >= 0 && !empobj_in_use(tabno, ef_ptr(tabno, val))) {
+           verify_fail(type, row, ca, idx,
+                       "value %ld refers to missing element of table %s",
+                       val, ef_nameof(tabno));
+           return -1;
+       }
     }
     return 0;
 }
@@ -130,7 +137,7 @@ static int
 verify_row(int type, int row)
 {
     struct castr *ca = ef_cadef(type);
-    struct emptypedstr *row_ref;
+    struct empobj *row_ref;
     int i, j, n;
     struct valstr val;
     int ret_val = 0;
@@ -150,6 +157,9 @@ verify_row(int type, int row)
        }
     }
 
+    if (!empobj_in_use(type, row_ref))
+       return ret_val;
+
     for (i = 0; ca[i].ca_name; ++i) {
        if (ca[i].ca_get)
            continue;           /* virtual */
@@ -166,10 +176,6 @@ verify_row(int type, int row)
            }
            if (ca[i].ca_table == type && i == 0) {
                /* uid */
-               /* Some files contain zeroed records, cope */
-               /* TODO tighten this check */
-               if (val.val_as.lng == 0)
-                   continue;
                if (val.val_as.lng != row) {
                    verify_fail(type, row, &ca[i], j,
                                "value is %ld instead of %d",
@@ -188,7 +194,7 @@ verify_row(int type, int row)
 }
 
 static void
-pln_zap_transient_flags(void)
+pln_zap_transient_flags(int may_put)
 {
     int i;
     struct plnstr *pp;
@@ -210,12 +216,18 @@ pln_zap_transient_flags(void)
             */
        }
     }
-    if (dirty)
+    if (dirty && may_put)
        ef_flush(EF_PLANE);     /* pretty wasteful */
 }
 
+/*
+ * Verify game state and configuration are sane.
+ * Correct minor problems, but write corrections to backing files only
+ * if MAY_PUT is non-zero.
+ * Return -1 if uncorrected problems remain, else 0.
+ */
 int
-ef_verify(void)
+ef_verify(int may_put)
 {
     struct empfile *ep;
     int retval = 0;
@@ -242,6 +254,6 @@ ef_verify(void)
        }
     }
 
-    pln_zap_transient_flags();
+    pln_zap_transient_flags(may_put);
     return retval;
 }