dav/sync.c

changeset 216
16d6b97fbf33
parent 215
781aee172901
child 217
12bad63cf5a8
--- a/dav/sync.c	Thu Mar 17 15:06:26 2016 +0100
+++ b/dav/sync.c	Thu Mar 17 20:24:37 2016 +0100
@@ -357,7 +357,7 @@
         
         if(cdt && exists && s.st_mtime != local->last_modified) {
             // file modified on the server and on the client
-            rename_local_file(dir, db, local->path);
+            rename_conflict_file(dir, db, local->path);
         }
     } else {
         if(stat(local_path, &s)) {
@@ -368,7 +368,7 @@
             //fprintf(stderr, "Error: file %s is a directory\n", local_path);
         } else if(cdt) {
             // rename file on conflict
-            rename_local_file(dir, db, res->path);
+            rename_conflict_file(dir, db, res->path);
         }
     }
       
@@ -499,7 +499,7 @@
     free(local_path);
 }
 
-void rename_local_file(SyncDirectory *dir, SyncDatabase *db, char *path) {
+void rename_conflict_file(SyncDirectory *dir, SyncDatabase *db, char *path) {
     char *local_path = util_concat_path(dir->path, path);
     char *parent = util_parent_path(local_path);
     
@@ -507,12 +507,19 @@
     struct stat s;
     int loop = 1;
     do {
-        sstr_t new_path = ucx_asprintf(
-        ucx_default_allocator(),
+        char *res_parent = util_parent_path(path);
+        char *res_name = util_resource_name(path);
+        
+        sstr_t new_path = ucx_sprintf(
             "%sorig.%d.%s",
             parent,
             rev,
-            util_resource_name(path));
+            res_name);
+        sstr_t new_res_path = ucx_sprintf(
+            "%sorig.%d.%s",
+            res_parent,
+            rev,
+            res_name);
         
         
         if(stat(new_path.ptr, &s)) {
@@ -526,13 +533,21 @@
                             "Cannot rename file %s to %s\n",
                             local_path,
                             new_path.ptr);
+                } else {
+                    LocalResource *conflict = calloc(1, sizeof(LocalResource));
+                    conflict->path = strdup(new_res_path.ptr);
+                    ucx_map_cstr_put(db->conflict, new_res_path.ptr, conflict);
                 }
             }
         }
         rev++;
+        free(res_parent);
         free(new_path.ptr);
+        free(new_res_path.ptr);
+        
     } while(loop);
     free(parent);
+    free(local_path);
 }
 
 char* create_tmp_download_path(char *path) {
@@ -597,6 +612,10 @@
     free(new_path);
 }
 
+static int res_isconflict(SyncDatabase *db, LocalResource *res) {
+    return ucx_map_cstr_get(db->conflict, res->path) ? 1 : 0;
+}
+
 int cmd_push(CmdArgs *a) {
     if(a->argc != 1) {
         fprintf(stderr, "Too %s arguments\n", a->argc < 1 ? "few" : "many");
@@ -644,6 +663,7 @@
     
     int sync_success = 0;
     int sync_delete = 0;
+    int sync_skipped = 0;
     int sync_error = 0;
     
     // upload all changed files
@@ -655,6 +675,12 @@
     UCX_FOREACH(elm, resources) {
         LocalResource *local_res = elm->data;
         if (!res_matches_filter(dir, local_res->path+1)) {
+            if(res_isconflict(db, local_res)) {
+                printf("skip: %s\n", local_res->path);
+                sync_skipped++;
+                continue;
+            }
+            
             // upload every changed file
             if (local_resource_is_changed(dir, db, local_res)) {
                 DavResource *res = dav_resource_new(sn, local_res->path);
@@ -722,10 +748,12 @@
     // Report
     char *str_success = sync_success == 1 ? "file" : "files";
     char *str_delete = sync_delete == 1 ? "file" : "files";
+    char *str_skipped = sync_delete == 1 ? "file" : "files";
     char *str_error = sync_error == 1 ? "error" : "errors";
-    printf("Result: %d %s pushed, %d %s deleted, %d %s\n",
+    printf("Result: %d %s pushed, %d %s deleted, %d %s skipped, %d %s\n",
             sync_success, str_success,
             sync_delete,str_delete,
+            sync_skipped,str_skipped,
             sync_error, str_error);
     
     return 0;
@@ -815,7 +843,7 @@
                 LocalResource *res = calloc(1, sizeof(LocalResource));
                 res->path = sstrdup(value).ptr;
                 if(res) {
-                    ucx_map_sstr_put(db->remove, value, res);
+                    //ucx_map_sstr_put(db->remove, value, res);
                     ucx_map_sstr_remove(db->resources, value);
                 }
                 

mercurial