dav/scfg.c

changeset 58
1708cba82ca3
parent 56
99f7816fcf01
child 65
d4077e8175f3
equal deleted inserted replaced
57:3c1ce5f203d7 58:1708cba82ca3
27 */ 27 */
28 28
29 #include <stdio.h> 29 #include <stdio.h>
30 #include <stdlib.h> 30 #include <stdlib.h>
31 #include <string.h> 31 #include <string.h>
32
33 #include <libidav/utils.h> 32 #include <libidav/utils.h>
34 #include <ucx/map.h> 33 #include <ucx/map.h>
35 34
36 #include "scfg.h" 35 #include "scfg.h"
37 36
74 xmlFreeDoc(doc); 73 xmlFreeDoc(doc);
75 74
76 return ret; 75 return ret;
77 } 76 }
78 77
78 static UcxList* add_regex_pattern(UcxList *list, char *value) {
79 regex_t *regex = malloc(sizeof(regex_t));
80 if (regcomp(regex, value, REG_EXTENDED|REG_NOSUB)) {
81 fprintf(stderr, "Invalid regular expression (%s) ... skipped\n", value);
82 return list;
83 } else {
84 return ucx_list_append(list, regex);
85 }
86 }
87
79 int scfg_load_directory(xmlNode *node) { 88 int scfg_load_directory(xmlNode *node) {
80 char *name = NULL; 89 char *name = NULL;
81 char *path = NULL; 90 char *path = NULL;
82 char *collection = NULL; 91 char *collection = NULL;
83 char *repository = NULL; 92 char *repository = NULL;
84 char *database = NULL; 93 char *database = NULL;
94 UcxList *include = NULL;
95 UcxList *exclude = NULL;
85 96
86 node = node->children; 97 node = node->children;
87 while(node) { 98 while(node) {
88 if(node->type == XML_ELEMENT_NODE) { 99 if(node->type == XML_ELEMENT_NODE) {
89 char *value = util_xml_get_text(node); 100 char *value = util_xml_get_text(node);
97 collection = value; 108 collection = value;
98 } else if(xstreq(node->name, "repository")) { 109 } else if(xstreq(node->name, "repository")) {
99 repository = value; 110 repository = value;
100 } else if(xstreq(node->name, "database")) { 111 } else if(xstreq(node->name, "database")) {
101 database = value; 112 database = value;
113 } else if(xstreq(node->name, "include")) {
114 include = add_regex_pattern(include, value);
115 } else if(xstreq(node->name, "exclude")) {
116 exclude = add_regex_pattern(exclude, value);
102 } 117 }
103 } 118 }
104 node = node->next; 119 node = node->next;
105 } 120 }
106 121
125 dir->name = strdup(name); 140 dir->name = strdup(name);
126 dir->path = strdup(path); 141 dir->path = strdup(path);
127 dir->collection = collection ? strdup(collection) : NULL; 142 dir->collection = collection ? strdup(collection) : NULL;
128 dir->repository = strdup(repository); 143 dir->repository = strdup(repository);
129 dir->database = strdup(database); 144 dir->database = strdup(database);
145 if (include) {
146 dir->include = include;
147 } else {
148 regex_t *matchall = malloc(sizeof(regex_t));
149 regcomp(matchall, ".*", REG_NOSUB);
150 dir->include = ucx_list_append(NULL, matchall);
151 }
152 if (exclude) {
153 dir->exclude = exclude;
154 } else {
155 regex_t *matchnothing = malloc(sizeof(regex_t));
156 regcomp(matchnothing, "///", REG_NOSUB);
157 dir->exclude = ucx_list_append(NULL, matchnothing);
158 }
130 159
131 ucx_map_cstr_put(directories, name, dir); 160 ucx_map_cstr_put(directories, name, dir);
132 161
133 return 0; 162 return 0;
134 } 163 }
135 164
136 void scfg_print_example() { 165 void scfg_print_example() {
137 fprintf(stderr, "example sync.xml\n\n"); 166 fprintf(stderr,
138 fprintf(stderr, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"); 167 "example sync.xml\n\n"
139 fprintf(stderr, "<configuration>\n"); 168 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
140 fprintf(stderr, " <directory>\n"); 169 "<configuration>\n"
141 fprintf(stderr, " <!-- identifier -->\n"); 170 " <directory>\n"
142 fprintf(stderr, " <name>documents</name>\n\n"); 171 " <!-- identifier -->\n"
143 fprintf(stderr, " <!-- local path to the directory -->\n"); 172 " <name>documents</name>\n\n"
144 fprintf(stderr, " <path>/home/user/Documents</path>\n\n"); 173 " <!-- local path to the directory -->\n"
145 fprintf(stderr, " <!-- repository name specified in config.xml -->\n"); 174 " <path>/home/user/Documents</path>\n\n"
146 fprintf(stderr, " <repository>server</repository>\n\n"); 175 " <!-- repository name specified in config.xml -->\n"
147 fprintf(stderr, " <!-- database file name -->\n"); 176 " <repository>server</repository>\n\n"
148 fprintf(stderr, " <database>documents-db.xml</database>\n"); 177 " <!-- collection to synchronize (optional) -->\n"
149 fprintf(stderr, " </directory>\n"); 178 " <collection>somecol</collection>\n\n"
150 fprintf(stderr, "</configuration>\n"); 179 " <!-- database file name -->\n"
180 " <database>documents-db.xml</database>\n"
181 " </directory>\n"
182 "</configuration>\n");
151 } 183 }
152 184
153 SyncDirectory* scfg_get_dir(char *name) { 185 SyncDirectory* scfg_get_dir(char *name) {
154 return ucx_map_cstr_get(directories, name); 186 return ucx_map_cstr_get(directories, name);
155 } 187 }

mercurial