dav/scfg.c

changeset 648
fefe4b6f1048
parent 647
8bf1d9688698
child 649
0f4c59ac8c74
equal deleted inserted replaced
647:8bf1d9688698 648:fefe4b6f1048
162 } 162 }
163 163
164 return 0; 164 return 0;
165 } 165 }
166 166
167 Filter* parse_filter(xmlNode *node) {
168 UcxList *include = NULL;
169 UcxList *exclude = NULL;
170 UcxList *tags = NULL;
171
172 if(scfg_load_filter(node, &include, &exclude, &tags)) {
173 return NULL;
174 }
175
176 Filter *filter = malloc(sizeof(Filter));
177 filter->include = include;
178 filter->exclude = exclude;
179 filter->tags = tags;
180 }
181
182 void init_default_filter(Filter *filter) {
183 if(!filter->include) {
184 regex_t *matchall = malloc(sizeof(regex_t));
185 regcomp(matchall, ".*", REG_NOSUB);
186 filter->include = ucx_list_append(NULL, matchall);
187 }
188 /*
189 if(!filter->exclude) {
190 regex_t *matchnothing = malloc(sizeof(regex_t));
191 regcomp(matchnothing, "///", REG_NOSUB);
192 filter->exclude = ucx_list_append(NULL, matchnothing);
193 }
194 */
195 }
196
167 static TagFormat str2tagformat(const char *str) { 197 static TagFormat str2tagformat(const char *str) {
168 if(!strcmp(str, "text")) { 198 if(!strcmp(str, "text")) {
169 return TAG_FORMAT_TEXT; 199 return TAG_FORMAT_TEXT;
170 } else if(!strcmp(str, "csv")) { 200 } else if(!strcmp(str, "csv")) {
171 return TAG_FORMAT_CSV; 201 return TAG_FORMAT_CSV;
248 *tagconfig = conf; 278 *tagconfig = conf;
249 return tagconfig; 279 return tagconfig;
250 } 280 }
251 281
252 static SplitConfig* parse_split(xmlNode *node) { 282 static SplitConfig* parse_split(xmlNode *node) {
253 char *pattern = NULL; 283 Filter *filter = NULL;
254 char *minsize = NULL; 284 char *minsize = NULL;
255 char *blocksize = NULL; 285 char *blocksize = NULL;
256 286
257 xmlNode *c = node->children; 287 xmlNode *c = node->children;
258 while(c) { 288 while(c) {
259 char *value = util_xml_get_text(c); 289 if(xstreq(c->name, "filter")) {
260 if(xstreq(c->name, "pattern")) { 290 filter = parse_filter(node);
261 pattern = value; 291 if(filter->tags) {
292 fprintf(stderr, "splitconfig: tag filter not supported\n");
293 free_filter(*filter);
294 free(filter);
295 return NULL;
296 }
262 } else if(xstreq(c->name, "minsize")) { 297 } else if(xstreq(c->name, "minsize")) {
263 minsize = value; 298 minsize = util_xml_get_text(c);
264 } else if(xstreq(c->name, "blocksize")) { 299 } else if(xstreq(c->name, "blocksize")) {
265 blocksize = value; 300 blocksize = util_xml_get_text(c);
266 } 301 }
267 c = c->next; 302 c = c->next;
268 } 303 }
269 304
270 uint64_t sz = 0; 305 uint64_t sz = 0;
281 if(!util_szstrtouint(blocksize, &sz)) { 316 if(!util_szstrtouint(blocksize, &sz)) {
282 fprintf(stderr, "splitconfig: blocksize is not a number\n"); 317 fprintf(stderr, "splitconfig: blocksize is not a number\n");
283 return NULL; 318 return NULL;
284 } 319 }
285 320
286 if(!pattern && !minsize) { 321 if(!filter && !minsize) {
287 fprintf(stderr, "splitconfig: pattern or minsize must be specified\n"); 322 fprintf(stderr, "splitconfig: filter or minsize must be specified\n");
288 return NULL; 323 return NULL;
289 } 324 }
290 325
291 int64_t minsz = -1; 326 int64_t minsz = -1;
292 if(minsize) { 327 if(minsize) {
297 } 332 }
298 minsz = (int64_t)m; 333 minsz = (int64_t)m;
299 } 334 }
300 335
301 SplitConfig *sc = calloc(1, sizeof(SplitConfig)); 336 SplitConfig *sc = calloc(1, sizeof(SplitConfig));
302 if(pattern) { 337 if(filter) {
303 regex_t *regex = malloc(sizeof(regex_t)); 338 init_default_filter(filter);
304 if (regcomp(regex, pattern, REG_EXTENDED|REG_NOSUB)) {
305 fprintf(stderr, "Invalid regular expression (%s)\n", pattern);
306 } else {
307 sc->pattern = regex;
308 }
309 } 339 }
310 sc->minsize = minsz; 340 sc->minsize = minsz;
311 sc->blocksize = (size_t)sz; 341 sc->blocksize = (size_t)sz;
312 return sc; 342 return sc;
313 } 343 }
607 } 637 }
608 if((metadata & FINFO_OWNER) == FINFO_OWNER) { 638 if((metadata & FINFO_OWNER) == FINFO_OWNER) {
609 dir->db_settings |= DB_STORE_OWNER; 639 dir->db_settings |= DB_STORE_OWNER;
610 } 640 }
611 641
612 if (include) { 642 dir->filter.include = include;
613 dir->include = include; 643 dir->filter.exclude = exclude;
614 } else { 644 dir->filter.tags = tagfilter;
615 regex_t *matchall = malloc(sizeof(regex_t)); 645 init_default_filter(&dir->filter);
616 regcomp(matchall, ".*", REG_NOSUB);
617 dir->include = ucx_list_append(NULL, matchall);
618 }
619 if (exclude) {
620 dir->exclude = exclude;
621 } else {
622 regex_t *matchnothing = malloc(sizeof(regex_t));
623 regcomp(matchnothing, "///", REG_NOSUB);
624 dir->exclude = ucx_list_append(NULL, matchnothing);
625 }
626 dir->tagfilter = tagfilter;
627 646
628 if (trash && sstrtrim(sstr(trash)).length > 0) { 647 if (trash && sstrtrim(sstr(trash)).length > 0) {
629 if (trash[0] == '/' || trash[0] == '$') { 648 if (trash[0] == '/' || trash[0] == '$') {
630 dir->trash = scfg_create_path(trash); 649 dir->trash = scfg_create_path(trash);
631 } else { 650 } else {
854 } 873 }
855 } 874 }
856 return dbname; 875 return dbname;
857 } 876 }
858 877
878 void free_filter(Filter filter) {
879 UCX_FOREACH(elm, filter.include) {
880 regfree(elm->data);
881 free(elm->data);
882 }
883 ucx_list_free(filter.include);
884 UCX_FOREACH(elm, filter.exclude) {
885 regfree(elm->data);
886 free(elm->data);
887 }
888 ucx_list_free(filter.exclude);
889 }
859 890
860 void free_sync_config() { 891 void free_sync_config() {
861 if(directories) { 892 if(directories) {
862 UcxMapIterator i = ucx_map_iterator(directories); 893 UcxMapIterator i = ucx_map_iterator(directories);
863 SyncDirectory *dir; 894 SyncDirectory *dir;
872 } 903 }
873 if(dir->trash) { 904 if(dir->trash) {
874 free(dir->trash); 905 free(dir->trash);
875 } 906 }
876 907
877 UCX_FOREACH(elm, dir->include) { 908 free_filter(dir->filter);
878 regfree(elm->data);
879 free(elm->data);
880 }
881 ucx_list_free(dir->include);
882 UCX_FOREACH(elm, dir->exclude) {
883 regfree(elm->data);
884 free(elm->data);
885 }
886 ucx_list_free(dir->exclude);
887 909
888 free(dir); 910 free(dir);
889 } 911 }
890 912
891 ucx_map_free(directories); 913 ucx_map_free(directories);

mercurial