Add close_file arg to saxdb_close_context(); allocate all saxdb contexts from heap.
[srvx.git] / src / saxdb.c
index bfb93ca2e3a046921af89206b067a9e115c308dc..88c578d08e00aba9e3b226f34faa1c1407b5cdab 100644 (file)
@@ -24,7 +24,7 @@
 #include "saxdb.h"
 #include "timeq.h"
 
-DEFINE_LIST(int_list, int);
+DEFINE_LIST(int_list, int)
 
 struct saxdb {
     char *name;
@@ -33,11 +33,18 @@ struct saxdb {
     saxdb_reader_func_t *reader;
     saxdb_writer_func_t *writer;
     unsigned int write_interval;
-    time_t last_write;
+    unsigned long last_write;
     unsigned int last_write_duration;
     struct saxdb *prev;
 };
 
+struct saxdb_context {
+    FILE *output;
+    unsigned int indent;
+    struct int_list complex;
+    jmp_buf jbuf;
+};
+
 #define COMPLEX(CTX) ((CTX)->complex.used ? ((CTX)->complex.list[(CTX)->complex.used-1]) : 1)
 
 static struct saxdb *last_db;
@@ -58,6 +65,7 @@ saxdb_read_db(struct saxdb *db) {
     if (!data)
         return;
     if (db->writer == saxdb_mondo_writer) {
+        free_database(mondo_db);
         mondo_db = data;
     } else {
         db->reader(data);
@@ -118,37 +126,34 @@ saxdb_register(const char *name, saxdb_reader_func_t *reader, saxdb_writer_func_
 
 static int
 saxdb_write_db(struct saxdb *db) {
-    struct saxdb_context ctx;
+    struct saxdb_context *ctx;
+    FILE *output;
     char tmp_fname[MAXLEN];
     int res, res2;
-    time_t start, finish;
+    unsigned long start, finish;
 
     assert(db->filename);
     sprintf(tmp_fname, "%s.new", db->filename);
-    memset(&ctx, 0, sizeof(ctx));
-    ctx.output = fopen(tmp_fname, "w+");
-    int_list_init(&ctx.complex);
-    if (!ctx.output) {
+    output = fopen(tmp_fname, "w+");
+    if (!output) {
         log_module(MAIN_LOG, LOG_ERROR, "Unable to write to %s: %s", tmp_fname, strerror(errno));
-        int_list_clean(&ctx.complex);
         return 1;
     }
+    ctx = saxdb_open_context(output);
     start = time(NULL);
-    if ((res = setjmp(ctx.jbuf)) || (res2 = db->writer(&ctx))) {
+    if ((res = setjmp(*saxdb_jmp_buf(ctx))) || (res2 = db->writer(ctx))) {
         if (res) {
             log_module(MAIN_LOG, LOG_ERROR, "Error writing to %s: %s", tmp_fname, strerror(res));
         } else {
             log_module(MAIN_LOG, LOG_ERROR, "Internal error %d while writing to %s", res2, tmp_fname);
         }
-        int_list_clean(&ctx.complex);
-        fclose(ctx.output);
+        ctx->complex.used = 0; /* Squelch asserts about unbalanced output. */
+        saxdb_close_context(ctx, 1);
         remove(tmp_fname);
         return 2;
     }
     finish = time(NULL);
-    assert(ctx.complex.used == 0);
-    int_list_clean(&ctx.complex);
-    fclose(ctx.output);
+    saxdb_close_context(ctx, 1);
     if (rename(tmp_fname, db->filename) < 0) {
         log_module(MAIN_LOG, LOG_ERROR, "Unable to rename %s to %s: %s", tmp_fname, db->filename, strerror(errno));
     }
@@ -236,7 +241,7 @@ saxdb_pre_object(struct saxdb_context *dest) {
     }
 }
 #else
-#define saxdb_pre_object(DEST) 
+#define saxdb_pre_object(DEST)
 #endif
 
 static inline void
@@ -386,7 +391,7 @@ static MODCMD_FUNC(cmd_write) {
                 stop.tv_sec -= 1;
                 stop.tv_usec += 1000000;
             }
-            reply("MSG_DB_WROTE_DB", db->name, stop.tv_sec, stop.tv_usec);
+            reply("MSG_DB_WROTE_DB", db->name, (unsigned long)stop.tv_sec, (unsigned long)stop.tv_usec);
             written++;
         }
     }
@@ -405,7 +410,7 @@ static MODCMD_FUNC(cmd_writeall) {
         stop.tv_sec -= 1;
         stop.tv_usec += 1000000;
     }
-    reply("MSG_DB_WROTE_ALL", stop.tv_sec, stop.tv_usec);
+    reply("MSG_DB_WROTE_ALL", (unsigned long)stop.tv_sec, (unsigned long)stop.tv_usec);
     return 1;
 }
 
@@ -529,21 +534,19 @@ write_database_helper(struct saxdb_context *ctx, struct dict *db) {
 
 int
 write_database(FILE *out, struct dict *db) {
-    struct saxdb_context ctx;
+    struct saxdb_context *ctx;
     int res;
 
-    ctx.output = out;
-    ctx.indent = 0;
-    int_list_init(&ctx.complex);
-    if (!(res = setjmp(ctx.jbuf))) {
-        write_database_helper(&ctx, db);
+    ctx = saxdb_open_context(out);
+    if (!(res = setjmp(*saxdb_jmp_buf(ctx)))) {
+        write_database_helper(ctx, db);
     } else {
         log_module(MAIN_LOG, LOG_ERROR, "Exception %d caught while writing to stream", res);
-        int_list_clean(&ctx.complex);
+        ctx->complex.used = 0; /* Squelch asserts about unbalanced output. */
+        saxdb_close_context(ctx, 0);
         return 1;
     }
-    assert(ctx.complex.used == 0);
-    int_list_clean(&ctx.complex);
+    saxdb_close_context(ctx, 0);
     return 0;
 }
 
@@ -559,9 +562,19 @@ saxdb_open_context(FILE *file) {
     return ctx;
 }
 
+jmp_buf *
+saxdb_jmp_buf(struct saxdb_context *ctx) {
+    return &ctx->jbuf;
+}
+
+
 void
-saxdb_close_context(struct saxdb_context *ctx) {
+saxdb_close_context(struct saxdb_context *ctx, int close_file) {
     assert(ctx->complex.used == 0);
     int_list_clean(&ctx->complex);
+    if (close_file)
+        fclose(ctx->output);
+    else
+        fflush(ctx->output);
     free(ctx);
 }