Thu, 31 May 2018 15:24:52 +0200
adds remove-repository command
dav/config.c | file | annotate | diff | comparison | revisions | |
dav/config.h | file | annotate | diff | comparison | revisions | |
dav/main.c | file | annotate | diff | comparison | revisions | |
dav/main.h | file | annotate | diff | comparison | revisions |
--- 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) {
--- 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);
--- 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 <name...>\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; +}