63 return ret; |
63 return ret; |
64 } |
64 } |
65 |
65 |
66 static DavContext *context; |
66 static DavContext *context; |
67 |
67 |
|
68 void create_default_config(char *file) { |
|
69 xmlDoc *doc = xmlNewDoc(BAD_CAST "1.0"); |
|
70 xmlNode *root = xmlNewNode(NULL, BAD_CAST "configuration"); |
|
71 xmlDocSetRootElement(doc, root); |
|
72 xmlSaveFormatFileEnc(file, doc, "UTF-8", 1); |
|
73 xmlFreeDoc(doc); |
|
74 } |
|
75 |
68 void load_config(DavContext *ctx) { |
76 void load_config(DavContext *ctx) { |
69 context = ctx; |
77 context = ctx; |
70 // TODO: free the config somewhere |
78 // TODO: free the config somewhere |
71 repos = ucx_map_new(16); |
79 repos = ucx_map_new(16); |
72 keys = ucx_map_new(16); |
80 keys = ucx_map_new(16); |
75 if(check_config_dir()) { |
83 if(check_config_dir()) { |
76 return; |
84 return; |
77 } |
85 } |
78 |
86 |
79 char *file = util_concat_path(ENV_HOME, ".dav/config.xml"); |
87 char *file = util_concat_path(ENV_HOME, ".dav/config.xml"); |
|
88 |
|
89 struct stat s; |
|
90 if(stat(file, &s)) { |
|
91 switch(errno) { |
|
92 case ENOENT: { |
|
93 create_default_config(file); |
|
94 break; |
|
95 } |
|
96 default: { |
|
97 perror("Cannot load config.xml"); |
|
98 } |
|
99 } |
|
100 return; |
|
101 } |
|
102 |
80 xmlDoc *doc = xmlReadFile(file, NULL, 0); |
103 xmlDoc *doc = xmlReadFile(file, NULL, 0); |
|
104 free(file); |
81 if(!doc) { |
105 if(!doc) { |
82 doc = xmlNewDoc(BAD_CAST "1.0"); |
106 fprintf(stderr, "Cannot load config.xml\n"); |
83 xmlNode *root = xmlNewNode(NULL, BAD_CAST "configuration"); |
|
84 xmlDocSetRootElement(doc, root); |
|
85 xmlSaveFormatFileEnc(file, doc, "UTF-8", 1); |
|
86 xmlFreeDoc(doc); |
|
87 free(file); |
|
88 return; |
107 return; |
89 } |
108 } |
90 free(file); |
|
91 |
109 |
92 xmlNode *xml_root = xmlDocGetRootElement(doc); |
110 xmlNode *xml_root = xmlDocGetRootElement(doc); |
93 xmlNode *node = xml_root->children; |
111 xmlNode *node = xml_root->children; |
94 while(node) { |
112 while(node) { |
95 if(node->type == XML_ELEMENT_NODE) { |
113 if(node->type == XML_ELEMENT_NODE) { |