diff -r babdf40dd22c -r 8e1948eebef5 dav/sync.c --- a/dav/sync.c Fri Jun 01 09:23:51 2018 +0200 +++ b/dav/sync.c Fri Jun 01 19:05:08 2018 +0200 @@ -258,11 +258,7 @@ } static int res_matches_tags(DavResource *res, SyncTagFilter *tagfilter) { - if(!tagfilter) { - return 1; - } - - if(tagfilter->mode == DAV_SYNC_TAGFILTER_OFF) { + if(!tagfilter || tagfilter->mode == DAV_SYNC_TAGFILTER_OFF) { return 1; } @@ -277,13 +273,25 @@ return ret; } -static int localres_matches_tags(LocalResource *res, SyncTagFilter *tagfilter) { - if(!tagfilter) { +static int localres_matches_tags( + SyncDirectory *dir, + LocalResource *res, + SyncTagFilter *tagfilter) +{ + if(!tagfilter || tagfilter->mode == DAV_SYNC_TAGFILTER_OFF) { return 1; } + + DavBool changed = 0; + UcxList *res_tags = sync_get_file_tags(dir, res, &changed); - // TODO: implement - return 1; + int ret = matches_tagfilter(res_tags, tagfilter); + UCX_FOREACH(elm, res_tags) { + DavTag *t = elm->data; + free_dav_tag(t); + } + ucx_list_free(res_tags); + return ret; } static DavSession* create_session(DavContext *ctx, Repository *repo, char *url) { @@ -909,6 +917,13 @@ return -1; } + // if there are syntax errors in the command line, fail asap. + SyncTagFilter* tagfilter = parse_tagfilter_string(cmd_getoption(a, "tags")); + if (!tagfilter) { + fprintf(stderr, "Malformed tag filter\n"); + return -1; + } + SyncDirectory *dir = scfg_get_dir(a->argv[0]); if(!dir) { fprintf(stderr, "Unknown sync dir: %s\n", a->argv[0]); @@ -980,6 +995,8 @@ locktokenfile = create_locktoken_file(dir->name, lock->token); } + DavBool remove_file = cmd_getoption(a, "remove") ? 1 : 0; + int sync_success = 0; int sync_delete = 0; int sync_skipped = 0; @@ -994,7 +1011,17 @@ int ret = 0; UCX_FOREACH(elm, resources) { LocalResource *local_res = elm->data; - if (!res_matches_filter(dir, local_res->path+1)) { + int error = 0; + + if ( res_matches_filter(dir, local_res->path+1) + || !localres_matches_tags(dir, local_res, dir->tagfilter)) + { + local_res->keep = TRUE; + } else if (!localres_matches_tags(dir, local_res, tagfilter)) { + if(!remove_file) { + local_res->keep = TRUE; + } + } else { if(sync_shutdown) { LocalResource *lr = ucx_map_cstr_remove(db->resources, local_res->path); if(lr) { @@ -1016,7 +1043,6 @@ } // upload every changed file - int error = 0; int is_changed = local_resource_is_changed(dir, db, local_res); if (is_changed || local_res->tags_updated) { DavResource *res = dav_resource_new(sn, local_res->path); @@ -1078,18 +1104,18 @@ } dav_resource_free(res); } - - // remove every locally available resource from db->resource - // the remaining elements are all deleted files - elm->data = NULL; - if(!error) { - ucx_map_cstr_put(lclres, local_res->path, local_res); - } - - LocalResource *lr = ucx_map_cstr_remove(db->resources, local_res->path); - if(lr) { - local_resource_free(lr); - } + } + + // remove every locally available resource from db->resource + // the remaining elements are all deleted files + elm->data = NULL; + if(!error) { + ucx_map_cstr_put(lclres, local_res->path, local_res); + } + + LocalResource *lr = ucx_map_cstr_remove(db->resources, local_res->path); + if(lr) { + local_resource_free(lr); } } @@ -1098,7 +1124,7 @@ UcxMapIterator i = ucx_map_iterator(db->resources); LocalResource *local; UCX_MAP_FOREACH(key, local, i) { - if (!res_matches_filter(dir, local->path+1)) { + if (!local->keep) { if(sync_shutdown) { ucx_map_cstr_put(lclres, local->path, local_resource_copy(local)); } else if(sync_delete_remote_resource(sn, local, &sync_delete)) {