Add close_file arg to saxdb_close_context(); allocate all saxdb contexts from heap.
[srvx.git] / src / saxdb.c
index 61c7e69b7eb388368bf153d470ee039ae75aad6d..88c578d08e00aba9e3b226f34faa1c1407b5cdab 100644 (file)
@@ -24,8 +24,7 @@
 #include "saxdb.h"
 #include "timeq.h"
 
-DECLARE_LIST(int_list, int);
-DEFINE_LIST(int_list, int);
+DEFINE_LIST(int_list, int)
 
 struct saxdb {
     char *name;
@@ -34,7 +33,7 @@ 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;
 };
@@ -44,8 +43,6 @@ struct saxdb_context {
     unsigned int indent;
     struct int_list complex;
     jmp_buf jbuf;
-    /* XXX: If jbuf is ever used, places that use saxdb_open_context() and
-     * saxdb_close_context() must be modified to fill it in */
 };
 
 #define COMPLEX(CTX) ((CTX)->complex.used ? ((CTX)->complex.list[(CTX)->complex.used-1]) : 1)
@@ -65,8 +62,10 @@ saxdb_read_db(struct saxdb *db) {
     assert(db);
     assert(db->filename);
     data = parse_database(db->filename);
-    if (!data) return;
+    if (!data)
+        return;
     if (db->writer == saxdb_mondo_writer) {
+        free_database(mondo_db);
         mondo_db = data;
     } else {
         db->reader(data);
@@ -127,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, "Exception %d caught while writing to %s", res, tmp_fname);
+            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));
     }
@@ -188,18 +184,25 @@ saxdb_write_all(void) {
 
     for (it = dict_first(saxdbs); it; it = iter_next(it)) {
         db = iter_data(it);
-        if (!db->mondo_section) saxdb_write_db(db);
+        if (!db->mondo_section)
+            saxdb_write_db(db);
     }
 }
 
-#define saxdb_put_char(DEST, CH)   fputc(CH, (DEST)->output)
-#define saxdb_put_string(DEST, CH) fputs(CH, (DEST)->output)
+#define saxdb_put_char(DEST, CH) do { \
+    if (fputc(CH, (DEST)->output) == EOF) \
+        longjmp((DEST)->jbuf, errno); \
+    } while (0)
+#define saxdb_put_string(DEST, CH) do { \
+    if (fputs(CH, (DEST)->output) == EOF) \
+        longjmp((DEST)->jbuf, errno); \
+    } while (0)
 
 static inline void
 saxdb_put_nchars(struct saxdb_context *dest, const char *name, int len) {
-    while (len--) {
-        fputc(*name++, dest->output);
-    }
+    while (len--)
+        if (fputc(*name++, dest->output) == EOF)
+            longjmp(dest->jbuf, errno);
 }
 
 static void
@@ -209,7 +212,8 @@ saxdb_put_qstring(struct saxdb_context *dest, const char *str) {
     assert(str);
     saxdb_put_char(dest, '"');
     while ((esc = strpbrk(str, "\\\a\b\t\n\v\f\r\""))) {
-        if (esc != str) saxdb_put_nchars(dest, str, esc-str);
+        if (esc != str)
+            saxdb_put_nchars(dest, str, esc-str);
         saxdb_put_char(dest, '\\');
         switch (*esc) {
         case '\a': saxdb_put_char(dest, 'a'); break;
@@ -237,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
@@ -297,12 +301,20 @@ saxdb_write_string(struct saxdb_context *dest, const char *name, const char *val
 
 void
 saxdb_write_int(struct saxdb_context *dest, const char *name, unsigned long value) {
-    unsigned char buf[16];
+    char buf[16];
     /* we could optimize this to take advantage of the fact that buf will never need escapes */
     snprintf(buf, sizeof(buf), "%lu", value);
     saxdb_write_string(dest, name, buf);
 }
 
+void
+saxdb_write_sint(struct saxdb_context *dest, const char *name, long value) {
+    char buf[16];
+    /* we could optimize this to take advantage of the fact that buf will never need escapes */
+    snprintf(buf, sizeof(buf), "%ld", value);
+    saxdb_write_string(dest, name, buf);
+}
+
 static void
 saxdb_free(void *data) {
     struct saxdb *db = data;
@@ -379,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++;
         }
     }
@@ -398,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;
 }
 
@@ -419,19 +431,23 @@ static MODCMD_FUNC(cmd_stats_databases) {
     tbl.contents[0][4] = "Last Duration";
     for (ii=1, it=dict_first(saxdbs); it; it=iter_next(it), ++ii) {
         struct saxdb *db = iter_data(it);
+        if (db->mondo_section) {
+            --ii;
+            continue;
+        }
         char *buf = malloc(INTERVALLEN*3);
         tbl.contents[ii] = calloc(tbl.width, sizeof(tbl.contents[ii][0]));
         tbl.contents[ii][0] = db->name;
         tbl.contents[ii][1] = db->mondo_section ? db->mondo_section : db->filename;
         if (db->write_interval) {
-            intervalString(buf, db->write_interval);
+            intervalString(buf, db->write_interval, user->handle_info);
         } else {
             strcpy(buf, "Never");
         }
         tbl.contents[ii][2] = buf;
         if (db->last_write) {
-            intervalString(buf+INTERVALLEN, now - db->last_write);
-            intervalString(buf+INTERVALLEN*2, db->last_write_duration);
+            intervalString(buf+INTERVALLEN, now - db->last_write, user->handle_info);
+            intervalString(buf+INTERVALLEN*2, db->last_write_duration, user->handle_info);
         } else {
             strcpy(buf+INTERVALLEN, "Never");
             strcpy(buf+INTERVALLEN*2, "Never");
@@ -439,6 +455,7 @@ static MODCMD_FUNC(cmd_stats_databases) {
         tbl.contents[ii][3] = buf+INTERVALLEN;
         tbl.contents[ii][4] = buf+INTERVALLEN*2;
     }
+    tbl.length = ii;
     table_send(cmd->parent->bot, user->nick, 0, 0, tbl);
     free(tbl.contents[0]);
     for (ii=1; ii<tbl.length; ++ii) {
@@ -517,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;
 }
 
@@ -547,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);
 }