57 #define ENV_HOME getenv("HOME") |
57 #define ENV_HOME getenv("HOME") |
58 #endif /* _WIN32 */ |
58 #endif /* _WIN32 */ |
59 |
59 |
60 static UcxMap *directories; |
60 static UcxMap *directories; |
61 |
61 |
|
62 UcxMapIterator scfg_directory_iterator() { |
|
63 return ucx_map_iterator(directories); |
|
64 } |
|
65 |
62 static int create_default_sync_config(char *file) { |
66 static int create_default_sync_config(char *file) { |
63 FILE *out = fopen(file, "w"); |
67 FILE *out = fopen(file, "w"); |
64 if(!out) { |
68 if(!out) { |
65 perror("Cannot create config file"); |
69 perror("Cannot create config file"); |
66 return -1; |
70 return -1; |
70 fputs("<configuration>\n", out); |
74 fputs("<configuration>\n", out); |
71 fputs("</configuration>\n", out); |
75 fputs("</configuration>\n", out); |
72 fclose(out); |
76 fclose(out); |
73 |
77 |
74 return 0; |
78 return 0; |
75 } |
|
76 |
|
77 int load_sync_config() { |
|
78 directories = ucx_map_new(8); |
|
79 |
|
80 char *file = util_concat_path(ENV_HOME, ".dav/sync.xml"); |
|
81 |
|
82 struct stat s; |
|
83 if(stat(file, &s)) { |
|
84 switch(errno) { |
|
85 case ENOENT: { |
|
86 if(create_default_sync_config(file)) { |
|
87 return 1; |
|
88 } |
|
89 break; |
|
90 } |
|
91 default: { |
|
92 perror("Cannot load sync.xml"); |
|
93 } |
|
94 } |
|
95 free(file); |
|
96 return 0; |
|
97 } |
|
98 |
|
99 xmlDoc *doc = xmlReadFile(file, NULL, 0); |
|
100 if(!doc) { |
|
101 fprintf(stderr, "Cannot load sync.xml\n"); |
|
102 free(file); |
|
103 return -1; |
|
104 } |
|
105 |
|
106 int ret = 0; |
|
107 xmlNode *node = xmlDocGetRootElement(doc)->children; |
|
108 while(node && !ret) { |
|
109 if(node->type == XML_ELEMENT_NODE) { |
|
110 if(xstreq(node->name, "directory")) { |
|
111 ret = scfg_load_directory(node); |
|
112 } else { |
|
113 print_error(node->line, |
|
114 "unknown config element: %s\n", node->name); |
|
115 ret = 1; |
|
116 } |
|
117 } |
|
118 node = node->next; |
|
119 } |
|
120 |
|
121 xmlFreeDoc(doc); |
|
122 free(file); |
|
123 return ret; |
|
124 } |
79 } |
125 |
80 |
126 static UcxList* add_regex_pattern(UcxList *list, char *value, |
81 static UcxList* add_regex_pattern(UcxList *list, char *value, |
127 unsigned short xmlline) { |
82 unsigned short xmlline) { |
128 regex_t *regex = malloc(sizeof(regex_t)); |
83 regex_t *regex = malloc(sizeof(regex_t)); |
302 ucx_map_cstr_put(directories, name, dir); |
257 ucx_map_cstr_put(directories, name, dir); |
303 |
258 |
304 return 0; |
259 return 0; |
305 } |
260 } |
306 |
261 |
|
262 int load_sync_config() { |
|
263 directories = ucx_map_new(8); |
|
264 |
|
265 char *file = util_concat_path(ENV_HOME, ".dav/sync.xml"); |
|
266 |
|
267 struct stat s; |
|
268 if(stat(file, &s)) { |
|
269 switch(errno) { |
|
270 case ENOENT: { |
|
271 if(create_default_sync_config(file)) { |
|
272 return 1; |
|
273 } |
|
274 break; |
|
275 } |
|
276 default: { |
|
277 perror("Cannot load sync.xml"); |
|
278 } |
|
279 } |
|
280 free(file); |
|
281 return 0; |
|
282 } |
|
283 |
|
284 xmlDoc *doc = xmlReadFile(file, NULL, 0); |
|
285 if(!doc) { |
|
286 fprintf(stderr, "Cannot load sync.xml\n"); |
|
287 free(file); |
|
288 return -1; |
|
289 } |
|
290 |
|
291 int ret = 0; |
|
292 xmlNode *node = xmlDocGetRootElement(doc)->children; |
|
293 while(node && !ret) { |
|
294 if(node->type == XML_ELEMENT_NODE) { |
|
295 if(xstreq(node->name, "directory")) { |
|
296 ret = scfg_load_directory(node); |
|
297 } else { |
|
298 print_error(node->line, |
|
299 "unknown config element: %s\n", node->name); |
|
300 ret = 1; |
|
301 } |
|
302 } |
|
303 node = node->next; |
|
304 } |
|
305 |
|
306 xmlFreeDoc(doc); |
|
307 free(file); |
|
308 return ret; |
|
309 } |
|
310 |
307 SyncDirectory* scfg_get_dir(char *name) { |
311 SyncDirectory* scfg_get_dir(char *name) { |
308 return ucx_map_cstr_get(directories, name); |
312 return ucx_map_cstr_get(directories, name); |
309 } |
313 } |
310 |
314 |
311 int scfg_check_dir(SyncDirectory *dir) { |
315 int scfg_check_dir(SyncDirectory *dir) { |