startup doesn't override broken config.xml anymore

Fri, 02 Oct 2015 15:13:30 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 02 Oct 2015 15:13:30 +0200
changeset 142
0c0ccb7f71ba
parent 141
b0c44be83276
child 143
d8b01bed3d83

startup doesn't override broken config.xml anymore

dav/config.c file | annotate | diff | comparison | revisions
dav/main.c file | annotate | diff | comparison | revisions
dav/scfg.c file | annotate | diff | comparison | revisions
--- a/dav/config.c	Fri Oct 02 14:42:47 2015 +0200
+++ b/dav/config.c	Fri Oct 02 15:13:30 2015 +0200
@@ -65,6 +65,14 @@
 
 static DavContext *context;
 
+void create_default_config(char *file) {
+    xmlDoc *doc = xmlNewDoc(BAD_CAST "1.0");
+    xmlNode *root = xmlNewNode(NULL, BAD_CAST "configuration");
+    xmlDocSetRootElement(doc, root);
+    xmlSaveFormatFileEnc(file, doc, "UTF-8", 1);
+    xmlFreeDoc(doc);
+}
+
 void load_config(DavContext *ctx) {
     context = ctx;
     // TODO: free the config somewhere
@@ -77,17 +85,27 @@
     }
     
     char *file = util_concat_path(ENV_HOME, ".dav/config.xml");
-    xmlDoc *doc = xmlReadFile(file, NULL, 0);
-    if(!doc) {
-        doc = xmlNewDoc(BAD_CAST "1.0");
-        xmlNode *root = xmlNewNode(NULL, BAD_CAST "configuration");
-        xmlDocSetRootElement(doc, root);
-        xmlSaveFormatFileEnc(file, doc, "UTF-8", 1);
-        xmlFreeDoc(doc);
-        free(file);
+    
+    struct stat s;
+    if(stat(file, &s)) {
+        switch(errno) {
+            case ENOENT: {
+                create_default_config(file);
+                break;
+            }
+            default: {
+                perror("Cannot load config.xml");
+            }
+        }
         return;
     }
+    
+    xmlDoc *doc = xmlReadFile(file, NULL, 0);
     free(file);
+    if(!doc) {
+        fprintf(stderr, "Cannot load config.xml\n");
+        return;
+    }
     
     xmlNode *xml_root = xmlDocGetRootElement(doc);
     xmlNode *node = xml_root->children;
--- a/dav/main.c	Fri Oct 02 14:42:47 2015 +0200
+++ b/dav/main.c	Fri Oct 02 15:13:30 2015 +0200
@@ -46,8 +46,14 @@
 
 static DavContext *ctx;
 
+static int printxmlerror = 1;
 static void xmlerrorfnc(void * c, const char * msg, ... ) {
-    // nothing
+    if(printxmlerror) {
+        va_list ap;
+        va_start(ap, msg);
+        vfprintf(stderr, msg, ap);
+        va_end(ap);
+    }
 }
 
 //define DO_THE_TEST
@@ -62,6 +68,7 @@
     initGenericErrorDefaultFunc(&fnc);
     ctx = dav_context_new();
     load_config(ctx);
+    printxmlerror = 0;
 #ifdef DO_THE_TEST
     test();
     return 0;
--- a/dav/scfg.c	Fri Oct 02 14:42:47 2015 +0200
+++ b/dav/scfg.c	Fri Oct 02 15:13:30 2015 +0200
@@ -49,7 +49,7 @@
     char *file = util_concat_path(ENV_HOME, ".dav/sync.xml");
     xmlDoc *doc = xmlReadFile(file, NULL, 0);
     if(!doc) {
-        fprintf(stderr, "Missing configuration file\n");
+        fprintf(stderr, "Missing or broken configuration file\n");
         scfg_print_example();
         return -1;
     }

mercurial