dav-sync detects remote removed files

Thu, 03 Jul 2014 16:16:02 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 03 Jul 2014 16:16:02 +0200
changeset 50
9c486ea25161
parent 49
c5759ac76c1b
child 51
e94bf8530d56

dav-sync detects remote removed files

dav/sync.c file | annotate | diff | comparison | revisions
dav/sync.h file | annotate | diff | comparison | revisions
--- a/dav/sync.c	Thu Jul 03 15:50:13 2014 +0200
+++ b/dav/sync.c	Thu Jul 03 16:16:02 2014 +0200
@@ -134,17 +134,24 @@
         // TODO: free
         return 0; // empty repository
     }
-      
+    
+    UcxMap *svrres = ucx_map_new(db->resources->count);
+    
     UcxList *stack = ucx_list_prepend(NULL, ls->children);
     while(stack) {
         DavResource *res = stack->data;
         stack = ucx_list_remove(stack, stack);
          
-        while(res) {
+        while(res) { 
+            // download the resource
             if(sync_get_resource(dir, res, db)) {
                 fprintf(stderr, "sync_get_resource failed for resource: %s\n", res->path);
             }
             
+            LocalResource *local = ucx_map_cstr_get(db->resources, res->path);
+            ucx_map_cstr_put(svrres, res->path, local);
+            ucx_map_cstr_remove(db->resources, res->path);
+            
             if(res->children) {
                 stack = ucx_list_prepend(stack, res->children);
             }
@@ -152,6 +159,14 @@
         }
     }
     
+    UcxMapIterator i = ucx_map_iterator(db->resources);
+    LocalResource *local;
+    UCX_MAP_FOREACH(key, local, i) {
+        sync_remove_resource(dir, local);
+    }
+    ucx_map_free(db->resources);
+    db->resources = svrres;
+    
     // TODO: cleanup
     
     // store db
@@ -239,6 +254,26 @@
     return ret;
 }
 
+void sync_remove_resource(SyncDirectory *dir, LocalResource *res) {
+    char *local_path = util_concat_path(dir->path, res->path);
+    struct stat s;
+    if(stat(local_path, &s)) {
+        free(local_path);
+        return;
+    }
+    
+    if(s.st_mtim.tv_sec != res->last_modified) {
+        free(local_path);
+        return;
+    }
+    
+    printf("delete: %s\n", res->path);
+    if(unlink(local_path)) {
+        fprintf(stderr, "Cannot remove file %s\n", local_path);
+    }
+    free(local_path);
+}
+
 int cmd_push(CmdArgs *a) {
     if(a->argc != 1) {
         fprintf(stderr, "Too %s arguments\n", a->argc < 1 ? "few" : "many");
--- a/dav/sync.h	Thu Jul 03 15:50:13 2014 +0200
+++ b/dav/sync.h	Thu Jul 03 16:16:02 2014 +0200
@@ -46,6 +46,7 @@
 int cmd_sync(CmdArgs *args);
 
 int sync_get_resource(SyncDirectory *dir, DavResource *res, SyncDatabase *db);
+void sync_remove_resource(SyncDirectory *dir, LocalResource *res);
 
 UcxList* local_scan(SyncDirectory *dir, SyncDatabase *db);
 

mercurial