dav/config.c

changeset 398
26fdeed98cd7
parent 364
3769ba002fd1
child 405
6b85d745e402
--- a/dav/config.c	Wed May 30 20:07:19 2018 +0200
+++ b/dav/config.c	Thu May 31 15:24:52 2018 +0200
@@ -614,6 +614,12 @@
     }
     
     xmlNode *root = xmlDocGetRootElement(doc);
+    if(!root) {
+        fprintf(stderr, "Missing root node in config.xml\n");
+        xmlFreeDoc(doc);
+        free(file);
+        return 1;
+    }
     
     xmlNode *repoNode = xmlNewNode(NULL, BAD_CAST "repository");
     xmlNodeAddContent(repoNode, BAD_CAST "\n\t\t");
@@ -641,17 +647,93 @@
     xmlAddChild(root, repoNode);
     xmlNodeAddContent(root, BAD_CAST "\n");
     
-    int ret = 0;
-    if(xmlSaveFormatFileEnc(file, doc, "UTF-8", 1) == -1) {
-        ret = 1;
-    }
+    int ret = (xmlSaveFormatFileEnc(file, doc, "UTF-8", 1) == -1) ? 1 : 0;
     xmlFreeDoc(doc);
     free(file);
     
     return ret;
 }
 
-int list_repositories() {
+int remove_repository(Repository *repo) {
+    char *file = util_concat_path(ENV_HOME, ".dav/config.xml");
+    struct stat s;
+    if(stat(file, &s)) {
+        perror("Cannot access config.xml");
+        free(file);
+        return 1;
+    }
+    
+    xmlDoc *doc = xmlReadFile(file, NULL, 0);
+    if(!doc) {
+        free(file);
+        fprintf(stderr, "Cannot load config.xml\n");
+        return 1;
+    }
+    
+    xmlNodePtr root = xmlDocGetRootElement(doc);
+    if(!root) {
+        fprintf(stderr, "Missing root node in config.xml\n");
+        xmlFreeDoc(doc);
+        free(file);
+        return 1;
+    }
+    
+    xmlNodePtr repoNode = root->children;
+    xmlNodePtr matchedRepoNode = NULL;
+    while(!matchedRepoNode && repoNode) {
+        if(repoNode->type == XML_ELEMENT_NODE
+                && xstreq(repoNode->name, "repository")) {
+            xmlNodePtr nameNode = repoNode->children;
+            while(!matchedRepoNode && nameNode) {
+                if (nameNode->type == XML_ELEMENT_NODE
+                        && xstreq(nameNode->name, "name")) {
+                    char *reponame = util_xml_get_text(nameNode);
+                    if(!strcmp(repo->name, reponame)) {
+                        matchedRepoNode = repoNode;
+                    }
+                }
+                nameNode = nameNode->next;
+            }
+        }
+        repoNode = repoNode->next;
+    }
+    
+    if(matchedRepoNode) {
+        xmlNodePtr prev = matchedRepoNode->prev;
+        xmlNodePtr next = matchedRepoNode->next;
+        if(prev && prev->type == XML_TEXT_NODE) {
+            sstr_t content = sstr(prev->content);
+            sstr_t lf = sstrrchr(content, '\n');
+            if(lf.length > 0) {
+                *lf.ptr = '\0';
+                char* newcontent = sstrdup(content).ptr;
+                xmlNodeSetContent(prev, newcontent);
+                free(newcontent);
+            }
+        }
+        if(next && next->type == XML_TEXT_NODE) {
+            sstr_t lf = sstrchr(sstr(next->content), '\n');
+            if(lf.length > 0) {
+                char* newcontent = malloc(lf.length);
+                memcpy(newcontent, lf.ptr+1, lf.length-1);
+                newcontent[lf.length-1] = '\0';
+                xmlNodeSetContent(next, newcontent);
+                free(newcontent);
+            }
+        }
+        
+        xmlUnlinkNode(matchedRepoNode);
+        xmlFreeNode(matchedRepoNode);
+    }
+    
+    int ret = (xmlSaveFormatFileEnc(file, doc, "UTF-8", 1) == -1) ? 1 : 0;
+    xmlFreeDoc(doc);
+    free(file);
+    
+    return ret;
+}
+
+int list_repositories(void) {
     UcxMapIterator i = ucx_map_iterator(repos);
     Repository *repo;
     UCX_MAP_FOREACH(key, repo, i) {

mercurial