dav/sync.c

changeset 58
1708cba82ca3
parent 57
3c1ce5f203d7
child 61
e9b102d5a6f6
--- a/dav/sync.c	Tue Aug 05 13:05:03 2014 +0200
+++ b/dav/sync.c	Tue Aug 05 14:00:35 2014 +0200
@@ -91,6 +91,8 @@
         ret = cmd_sync(args);
     }
     
+    // TODO: cleanup sync config (don't forget to call regfree for regex)
+    
     return ret;
 }
 
@@ -106,6 +108,25 @@
     fprintf(stderr, "        -r         Read changes from stdin\n\n");
 }
 
+static int res_matches_filter(SyncDirectory *dir, char *res_path) {
+    
+    UCX_FOREACH(inc, dir->include) {
+        regex_t* pattern = (regex_t*) inc->data;
+        if (regexec(pattern, res_path, 0, NULL, 0)) {
+            return 1;
+        }
+    }
+    
+    UCX_FOREACH(exc, dir->exclude) {
+        regex_t* pattern = (regex_t*) exc->data;
+        if (!regexec(pattern, res_path, 0, NULL, 0)) {
+            return 1;
+        }
+    }
+    
+    return 0;
+}
+
 int cmd_pull(CmdArgs *a) {
     if(a->argc != 1) {
         fprintf(stderr, "Too %s arguments\n", a->argc < 1 ? "few" : "many");
@@ -164,7 +185,12 @@
         DavResource *res = stack->data;
         stack = ucx_list_remove(stack, stack);
          
-        while(res) { 
+        while(res) {
+            if (res_matches_filter(dir, res->path)) {
+                res = res->next;
+                continue;
+            }
+            
             // download the resource
             if(sync_get_resource(a, dir, res, db)) {
                 fprintf(stderr, "sync_get_resource failed for resource: %s\n", res->path);
@@ -184,12 +210,15 @@
     UcxMapIterator i = ucx_map_iterator(db->resources);
     LocalResource *local;
     UCX_MAP_FOREACH(key, local, i) {
+        if (res_matches_filter(dir, local->path)) {
+            continue;
+        }
         sync_remove_resource(dir, local);
     }
     ucx_map_free(db->resources);
     db->resources = svrres;
     
-    // TODO: cleanup
+    // TODO: cleanup - BUT DONT CLEANUP SYNC CONFIG (do this in main!)
     
     // store db
     if(store_db(db, dir->database)) {
@@ -256,7 +285,6 @@
     int ret = 0;
     if(res->iscollection) {
         mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
-        //printf("mkdir %s\n", local_path);
         if(util_mkdir(local_path, mode) && errno != EEXIST) {
             ret = -1;
         }
@@ -395,19 +423,20 @@
     sn->key = dav_context_get_key(ctx, repo->default_key);
     
     // upload all changed files
-    //UcxList *resources = local_scan(dir, db);
-    //UcxList *resources = read_changes(dir, db);
     UcxList *resources = cmd_getoption(a, "read") ?
             read_changes(dir, db) : local_scan(dir, db);
     
     UCX_FOREACH(elm, resources) {
         LocalResource *local_res = elm->data;
-        printf("put: %s\n", local_res->path);
-        DavResource *res = dav_resource_new(sn, local_res->path);
-        if(!sync_put_resource(dir, res, db)) {
+        if (!res_matches_filter(dir, local_res->path+1)) {
+            printf("put: %s\n", local_res->path);
             ucx_map_cstr_put(db->resources, local_res->path, local_res);
+            DavResource *res = dav_resource_new(sn, local_res->path);
+            if(sync_put_resource(dir, res, db)) {
+                ucx_map_cstr_remove(db->resources, local_res->path);
+            }
+            dav_resource_free(res);
         }
-        dav_resource_free(res);
     }
     ucx_list_free(resources);
     
@@ -519,8 +548,6 @@
                     resources = ucx_list_append(resources, res);
                 }
             } else if(!sstrcmp(name, S("remove"))) {
-                int isdir;
-                //LocalResource *res = path_to_local_resource(dir, db, value.ptr, &isdir);
                 LocalResource *res = calloc(1, sizeof(LocalResource));
                 res->path = sstrdup(value).ptr;
                 if(res) {
@@ -539,6 +566,7 @@
 }
 
 LocalResource* path_to_local_resource(SyncDirectory *dir, SyncDatabase *db, char *path, int *isdir) {
+    
     char *file_path = util_concat_path(dir->path, path);
     struct stat s;
     if(stat(file_path, &s)) {
@@ -566,14 +594,13 @@
                 res->last_modified = s.st_mtime;
                 
                 return res;
-            }                       
+            }
         } else {
             LocalResource *res = calloc(1, sizeof(LocalResource));
             res->path = strdup(path);
             res->etag = NULL;
             res->last_modified = s.st_mtime;
             res->size = s.st_size;
-            //ucx_map_cstr_put(db->resources, res->path, res);
             return res;
         }
     } else {

mercurial