dav-sync push ignores conflict files

Thu, 17 Mar 2016 20:24:37 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 17 Mar 2016 20:24:37 +0100
changeset 216
16d6b97fbf33
parent 215
781aee172901
child 217
12bad63cf5a8

dav-sync push ignores conflict files

dav/db.c file | annotate | diff | comparison | revisions
dav/db.h file | annotate | diff | comparison | revisions
dav/sync.c file | annotate | diff | comparison | revisions
dav/sync.h file | annotate | diff | comparison | revisions
--- a/dav/db.c	Thu Mar 17 15:06:26 2016 +0100
+++ b/dav/db.c	Thu Mar 17 20:24:37 2016 +0100
@@ -59,7 +59,7 @@
         xmlDocSetRootElement(doc, root);
         if(xmlSaveFormatFileEnc(db_file, doc, "UTF-8", 1) != -1) {
             db->resources = ucx_map_new(2048);
-            db->remove = ucx_map_new(8);
+            //db->remove = ucx_map_new(8);
         } else {
             free(db);
             db = NULL;
@@ -71,7 +71,8 @@
     free(db_file);
     
     db->resources = ucx_map_new(2048);
-    db->remove = ucx_map_new(16);
+    //db->remove = ucx_map_new(16);
+    db->conflict = ucx_map_new(16);
     int error = 0;
     while(xmlTextReaderRead(reader)) {
         int type = xmlTextReaderNodeType(reader);
@@ -86,10 +87,10 @@
                     error = 1;
                     break;
                 }
-            } else if(xstreq(name, "remove")) {
-                LocalResource *res = process_remove(reader);
+            } else if(xstreq(name, "conflict")) {
+                LocalResource *res = process_conflict(reader);
                 if(res) {
-                    ucx_map_cstr_put(db->remove, res->path, res);
+                    ucx_map_cstr_put(db->conflict, res->path, res);
                 } else {
                     error = 1;
                     break;
@@ -173,7 +174,7 @@
     }
 }
 
-LocalResource* process_remove(xmlTextReaderPtr reader) {
+LocalResource* process_conflict(xmlTextReaderPtr reader) {
     LocalResource *res = calloc(1, sizeof(LocalResource));
     
     int path = 0;
@@ -189,7 +190,7 @@
             const xmlChar *value = xmlTextReaderConstValue(reader);
             res->path = strdup((char*)value);
         } else if(XML_READER_TYPE_END_ELEMENT) {
-            if(xstreq(name, "remove")) {
+            if(xstreq(name, "conflict")) {
                 break;
             } else {
                 path = 0;
@@ -283,6 +284,7 @@
     }
     
     // write all remove entries
+/*
     i = ucx_map_iterator(db->remove);
     UCX_MAP_FOREACH(key, res, i) {
         // <remove>
@@ -296,6 +298,22 @@
         // </remove>
         xmlTextWriterEndElement(writer);
     }
+*/
+    
+    // write all conflict entries
+    i = ucx_map_iterator(db->conflict);
+    UCX_MAP_FOREACH(key, res, i) {
+        // <conflict>
+        xmlTextWriterStartElement(writer, BAD_CAST "conflict");
+        
+        xmlTextWriterWriteElement(
+                    writer,
+                    BAD_CAST "path",
+                    BAD_CAST res->path);
+        
+        // </conflict>
+        xmlTextWriterEndElement(writer);
+    }
     
     // end
     xmlTextWriterEndElement(writer);
--- a/dav/db.h	Thu Mar 17 15:06:26 2016 +0100
+++ b/dav/db.h	Thu Mar 17 20:24:37 2016 +0100
@@ -53,14 +53,15 @@
 
 struct SyncDatabase {
     UcxMap *resources;
-    UcxMap *remove;
+    //UcxMap *remove;
+    UcxMap *conflict;
 };
 
 SyncDatabase* load_db(char *name);
 int store_db(SyncDatabase *db, char *name);
 
 LocalResource* process_resource(xmlTextReaderPtr reader);
-LocalResource* process_remove(xmlTextReaderPtr reader);
+LocalResource* process_conflict(xmlTextReaderPtr reader);
 
 
 #ifdef	__cplusplus
--- 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);
                 }
                 
--- a/dav/sync.h	Thu Mar 17 15:06:26 2016 +0100
+++ b/dav/sync.h	Thu Mar 17 20:24:37 2016 +0100
@@ -56,7 +56,7 @@
         int *counter);
 int sync_remove_local_resource(SyncDirectory *dir, LocalResource *res);
 void sync_remove_local_directory(SyncDirectory *dir, LocalResource *res);
-void rename_local_file(SyncDirectory *dir, SyncDatabase *db, char *path);
+void rename_conflict_file(SyncDirectory *dir, SyncDatabase *db, char *path);
 char* create_tmp_download_path(char *path);
 void move_to_trash(SyncDirectory *dir, char *path);
 UcxList* local_scan(SyncDirectory *dir, SyncDatabase *db);

mercurial