dav/config.c

changeset 398
26fdeed98cd7
parent 364
3769ba002fd1
child 405
6b85d745e402
equal deleted inserted replaced
397:ddda42712f39 398:26fdeed98cd7
612 fprintf(stderr, "Cannot load config.xml\n"); 612 fprintf(stderr, "Cannot load config.xml\n");
613 return 1; 613 return 1;
614 } 614 }
615 615
616 xmlNode *root = xmlDocGetRootElement(doc); 616 xmlNode *root = xmlDocGetRootElement(doc);
617 if(!root) {
618 fprintf(stderr, "Missing root node in config.xml\n");
619 xmlFreeDoc(doc);
620 free(file);
621 return 1;
622 }
617 623
618 xmlNode *repoNode = xmlNewNode(NULL, BAD_CAST "repository"); 624 xmlNode *repoNode = xmlNewNode(NULL, BAD_CAST "repository");
619 xmlNodeAddContent(repoNode, BAD_CAST "\n\t\t"); 625 xmlNodeAddContent(repoNode, BAD_CAST "\n\t\t");
620 xmlNewTextChild(repoNode, NULL, BAD_CAST "name", BAD_CAST repo->name); 626 xmlNewTextChild(repoNode, NULL, BAD_CAST "name", BAD_CAST repo->name);
621 xmlNodeAddContent(repoNode, BAD_CAST "\n\t\t"); 627 xmlNodeAddContent(repoNode, BAD_CAST "\n\t\t");
639 645
640 xmlNodeAddContent(root, BAD_CAST "\n\t"); 646 xmlNodeAddContent(root, BAD_CAST "\n\t");
641 xmlAddChild(root, repoNode); 647 xmlAddChild(root, repoNode);
642 xmlNodeAddContent(root, BAD_CAST "\n"); 648 xmlNodeAddContent(root, BAD_CAST "\n");
643 649
644 int ret = 0; 650 int ret = (xmlSaveFormatFileEnc(file, doc, "UTF-8", 1) == -1) ? 1 : 0;
645 if(xmlSaveFormatFileEnc(file, doc, "UTF-8", 1) == -1) {
646 ret = 1;
647 }
648 xmlFreeDoc(doc); 651 xmlFreeDoc(doc);
649 free(file); 652 free(file);
650 653
651 return ret; 654 return ret;
652 } 655 }
653 656
654 int list_repositories() { 657 int remove_repository(Repository *repo) {
658 char *file = util_concat_path(ENV_HOME, ".dav/config.xml");
659 struct stat s;
660 if(stat(file, &s)) {
661 perror("Cannot access config.xml");
662 free(file);
663 return 1;
664 }
665
666 xmlDoc *doc = xmlReadFile(file, NULL, 0);
667 if(!doc) {
668 free(file);
669 fprintf(stderr, "Cannot load config.xml\n");
670 return 1;
671 }
672
673 xmlNodePtr root = xmlDocGetRootElement(doc);
674 if(!root) {
675 fprintf(stderr, "Missing root node in config.xml\n");
676 xmlFreeDoc(doc);
677 free(file);
678 return 1;
679 }
680
681 xmlNodePtr repoNode = root->children;
682 xmlNodePtr matchedRepoNode = NULL;
683 while(!matchedRepoNode && repoNode) {
684 if(repoNode->type == XML_ELEMENT_NODE
685 && xstreq(repoNode->name, "repository")) {
686 xmlNodePtr nameNode = repoNode->children;
687 while(!matchedRepoNode && nameNode) {
688 if (nameNode->type == XML_ELEMENT_NODE
689 && xstreq(nameNode->name, "name")) {
690 char *reponame = util_xml_get_text(nameNode);
691 if(!strcmp(repo->name, reponame)) {
692 matchedRepoNode = repoNode;
693 }
694 }
695 nameNode = nameNode->next;
696 }
697 }
698 repoNode = repoNode->next;
699 }
700
701 if(matchedRepoNode) {
702 xmlNodePtr prev = matchedRepoNode->prev;
703 xmlNodePtr next = matchedRepoNode->next;
704 if(prev && prev->type == XML_TEXT_NODE) {
705 sstr_t content = sstr(prev->content);
706 sstr_t lf = sstrrchr(content, '\n');
707 if(lf.length > 0) {
708 *lf.ptr = '\0';
709 char* newcontent = sstrdup(content).ptr;
710 xmlNodeSetContent(prev, newcontent);
711 free(newcontent);
712 }
713 }
714 if(next && next->type == XML_TEXT_NODE) {
715 sstr_t lf = sstrchr(sstr(next->content), '\n');
716 if(lf.length > 0) {
717 char* newcontent = malloc(lf.length);
718 memcpy(newcontent, lf.ptr+1, lf.length-1);
719 newcontent[lf.length-1] = '\0';
720 xmlNodeSetContent(next, newcontent);
721 free(newcontent);
722 }
723 }
724
725 xmlUnlinkNode(matchedRepoNode);
726 xmlFreeNode(matchedRepoNode);
727 }
728
729 int ret = (xmlSaveFormatFileEnc(file, doc, "UTF-8", 1) == -1) ? 1 : 0;
730 xmlFreeDoc(doc);
731 free(file);
732
733 return ret;
734 }
735
736 int list_repositories(void) {
655 UcxMapIterator i = ucx_map_iterator(repos); 737 UcxMapIterator i = ucx_map_iterator(repos);
656 Repository *repo; 738 Repository *repo;
657 UCX_MAP_FOREACH(key, repo, i) { 739 UCX_MAP_FOREACH(key, repo, i) {
658 printf("%s\n", repo->name); 740 printf("%s\n", repo->name);
659 } 741 }

mercurial