dav/sync.c

changeset 679
0d352b79363a
parent 675
a8117c4feaad
child 680
e66f2645be65
--- a/dav/sync.c	Sat Nov 09 10:18:58 2019 +0100
+++ b/dav/sync.c	Fri Nov 15 18:07:11 2019 +0100
@@ -839,7 +839,7 @@
         }
         
         // download the resource
-        if(sync_get_resource(a, dir, res->path, res, db, &sync_success)) {
+        if(sync_get_resource(a, dir, res->path, res, db, TRUE, &sync_success)) {
             fprintf(stderr, "resource download failed: %s\n", res->path);
             sync_error++;
         }
@@ -1368,6 +1368,7 @@
         const char *path,
         DavResource *res,
         SyncDatabase *db,
+        DavBool update_db,
         int *counter)
 { 
     char *link = SYNC_SYMLINK(dir) ?
@@ -1488,7 +1489,14 @@
                 return -1;
             }
         }
-
+        
+    } else if(tmp_path) {
+        if(sys_unlink(tmp_path)) {
+            fprintf(stderr, "Cannot remove tmp file: %s\n", tmp_path);
+        }
+    }
+    
+    if(update_db && ret == 0) {
         if(!local) {
             // new local resource
             local = calloc(1, sizeof(LocalResource));
@@ -1535,10 +1543,6 @@
         }
         sync_set_metadata_from_stat(local, &s);
         local->skipped = FALSE;
-    } else if(tmp_path) {
-        if(sys_unlink(tmp_path)) {
-            fprintf(stderr, "Cannot remove tmp file: %s\n", tmp_path);
-        }
     }
     
     if(tmp_path) {
@@ -2506,6 +2510,7 @@
         }
         
         DavResource *vres = NULL;
+        DavBool update_local_entry = TRUE;
         if(version) {
             if(dir->versioning->type == VERSIONING_SIMPLE) {
                 vres = versioning_simple_find(res, version);
@@ -2517,6 +2522,12 @@
                 ret = 1;
                 break;
             }
+            
+            // By restoring an old version of a file, the local dir is not
+            // in sync with the server anymore. Mark this file to change
+            // the metadata later, to make sure, the file will be detected
+            // as locally modified, on the next push/pull
+            update_local_entry = FALSE;
         } else {
             vres = res;
         }
@@ -2532,9 +2543,16 @@
                 }
                 free(local_path);
             } else {
-                if(sync_get_resource(a, dir, res->path, vres, db, &sync_success)) {
+                if(sync_get_resource(a, dir, res->path, vres, db, update_local_entry, &sync_success)) {
                     fprintf(stderr, "sync_get_resource failed for resource: %s\n", res->path);
                     sync_error++;
+                } else if(!update_local_entry) {
+                    LocalResource *lr = ucx_map_cstr_get(db->resources, res->path);
+                    if(lr) {
+                        lr->last_modified = 0;
+                        nullfree(lr->hash);
+                        lr->hash = NULL;
+                    } // else should not happen
                 }
             }
         }
@@ -3673,10 +3691,17 @@
     }
 }
 
-int versioning_delete(SyncDirectory *dir, DavResource *res) {
+int versioning_delete_begin(SyncDirectory *dir, DavResource *res, int *exists) {
     if(dir->versioning->type == VERSIONING_SIMPLE) {
-        // TODO
-    }
+        versioning_begin(dir, res, exists);
+    } else {
+        // versioning delete with DeltaV currently not supported in dav-sync
+        *exists = 1;
+    }
+    return 0;
+}
+
+int versioning_delete_end(SyncDirectory *dir, DavResource *res) {
     return 0;
 }
 
@@ -4213,17 +4238,20 @@
             // local resource metadata == remote resource metadata
             // resource can be deleted
             printf("delete: %s\n", res->path);
-            
+            int exists = 1;
+            int vend_required = 0;
             if(dir->versioning && dir->versioning->always) {
-                if(versioning_delete(dir, res)) {
+                if(versioning_delete_begin(dir, res, &exists)) {
                     fprintf(
                             stderr,
                             "Cannot save resource version before deletion\n");
                     ret = 1;
+                } else {
+                    vend_required = 1;
                 }
             }
             
-            if(!ret && dav_delete(res)) {
+            if(!ret && dav_delete(res) && exists) {
                 if(sn->error != DAV_NOT_FOUND) {
                     fprintf(stderr, "Cannot delete resource %s\n", res->path);
                     ret = 1;
@@ -4231,6 +4259,10 @@
             } else {
                 (*counter)++;
             }
+            
+            if(vend_required) {
+                versioning_delete_end(dir, res);
+            }
         }
         // else TODO: should we inform the user that the file was modified on
         // the server and delete was skipped?
@@ -5362,7 +5394,10 @@
             return hex_hash;
         }
     } else {
-        return dav_get_string_property_ns(res, DAV_NS, "content-hash");
+        char *hash = dav_get_string_property_ns(res, DAV_NS, "content-hash");
+        if(hash) {
+            return strdup(hash);
+        }
     }
     return NULL;
 }

mercurial