# HG changeset patch # User Mike Becker # Date 1527773092 -7200 # Node ID 26fdeed98cd7a18bab11c57eef3b700a9f79a735 # Parent ddda42712f3937b21e08300428cc8e025f1a30f3 adds remove-repository command diff -r ddda42712f39 -r 26fdeed98cd7 dav/config.c --- 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) { diff -r ddda42712f39 -r 26fdeed98cd7 dav/config.h --- a/dav/config.h Wed May 30 20:07:19 2018 +0200 +++ b/dav/config.h Thu May 31 15:24:52 2018 +0200 @@ -87,6 +87,7 @@ Key* get_key(char *name); int add_repository(Repository *repo); +int remove_repository(Repository *repo); int list_repositories(void); UcxList* get_repositories(void); diff -r ddda42712f39 -r 26fdeed98cd7 dav/main.c --- a/dav/main.c Wed May 30 20:07:19 2018 +0200 +++ b/dav/main.c Thu May 31 15:24:52 2018 +0200 @@ -141,6 +141,10 @@ } else if(!strcasecmp(cmd, "add-repository") || !strcasecmp(cmd, "add-repo")) { ret = cmd_add_repository(args); + } else if(!strcasecmp(cmd, "remove-repository") + || !strcasecmp(cmd, "remove-repo") + || !strcasecmp(cmd, "rm-repo")) { + ret = cmd_remove_repository(args); } else if(!strcasecmp(cmd, "list-repositories") || !strcasecmp(cmd, "list-repos")) { ret = list_repositories(); @@ -240,6 +244,7 @@ fprintf(stderr, "\n"); fprintf(stderr, "Config commands:\n"); fprintf(stderr, " add-repository\n"); + fprintf(stderr, " remove-repository\n"); fprintf(stderr, " list-repositories\n"); fprintf(stderr, " check-config\n"); fprintf(stderr, "\n"); @@ -1926,3 +1931,28 @@ return ret; } + +int cmd_remove_repository(CmdArgs *args) { + if(args->argc < 1) { + fprintf(stderr, "Too few arguments\n"); + fprintf(stderr, "Usage: dav remove-repository \n"); + return -1; + } + + for(int i = 0 ; i < args->argc ; i++) { + sstr_t reponame = sstr(args->argv[i]); + Repository* repo = get_repository(reponame); + if(repo) { + if(remove_repository(repo)) { + fprintf(stderr, "Cannot write config.xml\n"); + return -1; + } + } else { + fprintf(stderr, "Repository %s does not exist - skipped.\n", + reponame.ptr); + return -1; + } + } + + return -1; +} diff -r ddda42712f39 -r 26fdeed98cd7 dav/main.h --- a/dav/main.h Wed May 30 20:07:19 2018 +0200 +++ b/dav/main.h Thu May 31 15:24:52 2018 +0200 @@ -100,6 +100,7 @@ void printxmldoc(FILE *out, char *root, char *rootns, DavXmlNode *content); int cmd_add_repository(CmdArgs *args); +int cmd_remove_repository(CmdArgs *args); #ifdef __cplusplus }