added conflict detection to dav-sync push

Fri, 18 Mar 2016 13:00:00 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 18 Mar 2016 13:00:00 +0100
changeset 220
f1b3873a6525
parent 219
9792cfe63b80
child 221
e22c29b7ee2f

added conflict detection to dav-sync push

dav/sync.c file | annotate | diff | comparison | revisions
dav/sync.h file | annotate | diff | comparison | revisions
--- a/dav/sync.c	Fri Mar 18 12:38:30 2016 +0100
+++ b/dav/sync.c	Fri Mar 18 13:00:00 2016 +0100
@@ -674,6 +674,8 @@
         return -1;
     }
     
+    int cdt = cmd_getoption(a, "conflict") ? 0 : 1; // conflict detection
+    
     int sync_success = 0;
     int sync_delete = 0;
     int sync_skipped = 0;
@@ -712,6 +714,12 @@
                         sync_error++;
                     }
                 } else {
+                    if(cdt && remote_resource_is_changed(sn, dir, db, local_res)) {
+                        printf("conflict: %s\n", local_res->path);
+                        sync_skipped++;
+                        continue;
+                    }
+                    
                     printf("put: %s\n", local_res->path);
                     if(sync_put_resource(dir, res, local_res, &sync_success)) {
                         sync_error++;
@@ -914,6 +922,30 @@
     return 1;
 }
 
+int remote_resource_is_changed(
+        DavSession *sn,
+        SyncDirectory *dir,
+        SyncDatabase *db,
+        LocalResource *res)
+{
+    DavResource *remote = dav_get(sn, res->path, "D:getetag");
+    int ret = 0;
+    if(remote) {
+        char *etag = dav_get_property(remote, "D:getetag");
+        if(etag && res->etag) {
+            sstr_t e = sstr(etag);
+            if(sstrprefix(e, S("W/"))) {
+                e = sstrsubs(e, 2);
+            }
+            if(strcmp(e.ptr, res->etag)) {
+                ret = 1;
+            }
+        }
+        dav_resource_free(remote);
+    }
+    return ret;
+}
+
 
 int sync_set_status(DavResource *res, char *status) {
     DavResource *resource = dav_resource_new(res->session, res->path);
--- a/dav/sync.h	Fri Mar 18 12:38:30 2016 +0100
+++ b/dav/sync.h	Fri Mar 18 13:00:00 2016 +0100
@@ -63,6 +63,11 @@
 UcxList* read_changes(SyncDirectory *dir, SyncDatabase *db);
 LocalResource* local_resource_new(SyncDirectory *dir, SyncDatabase *db, char *path, int *isdir);
 int local_resource_is_changed(SyncDirectory *dir, SyncDatabase *db, LocalResource *res);
+int remote_resource_is_changed(
+        DavSession *sn,
+        SyncDirectory *dir,
+        SyncDatabase *db,
+        LocalResource *res);
 
 int sync_set_status(DavResource *res, char *status);
 int sync_remove_status(DavResource *res);

mercurial