dav/sync.c

changeset 621
06f9fddc82a1
parent 619
1d20a6cab2e5
child 626
2f735b0e87fa
--- a/dav/sync.c	Sun Aug 11 08:58:58 2019 +0200
+++ b/dav/sync.c	Sun Aug 11 10:57:25 2019 +0200
@@ -1274,9 +1274,8 @@
         fprintf(stderr, "Cannot store metadata: %s\n", res->path);
     }
     
-    if(local->etag) {
-        free(local->etag);
-    }
+    // dont free local->etag, because local_resource_set_etag will do that
+    
     if(local->hash) {
         free(local->hash);
     }
@@ -1288,7 +1287,7 @@
     }
     
     // set metadata from stat
-    local->etag = nullstrdup(etag);
+    local_resource_set_etag(local, etag);
     local->hash = nullstrdup(content_hash);
     
     sync_set_metadata_from_stat(local, &s);
@@ -1464,7 +1463,7 @@
         }
         
         // set metadata from stat
-        local->etag = strdup(etag);
+        local_resource_set_etag(local, etag);
         if(content_hash) {
             local->hash = content_hash;
         }
@@ -2844,6 +2843,24 @@
     return 0;
 }
 
+void local_resource_set_etag(LocalResource *local, const char *etag) {
+    // free old etag
+    if(local->etag) {
+        free(local->etag);
+    }
+    
+    if(!etag) {
+        local->etag = NULL;
+        return;
+    }
+    
+    scstr_t e = scstr(etag);
+    if(sstrprefix(e, S("W/"))) {
+        e = scstrsubs(e, 2);
+    }
+    local->etag = sstrdup(e).ptr;
+}
+
 char* resource_local_path(DavResource *res) {
 #ifdef SYS_LINK_EXT
     // on Windows, add .lnk extension to links
@@ -3816,21 +3833,7 @@
             } else {
                 // everything seems fine, we can update the local resource
                 char *etag = dav_get_string_property(up_res, "D:getetag");
-                if(etag) {
-                    if(strlen(etag) > 2 && etag[0] == 'W' && etag[1] == '/') {
-                        etag = etag + 2;
-                    } 
-                }
-
-                if(local->etag) {
-                    free(local->etag);
-                }
-
-                if(etag) {
-                    local->etag = strdup(etag);
-                } else {
-                    local->etag = NULL;
-                }
+                local_resource_set_etag(local, etag);
                 
                 if(!issplit && dir->hashing) {
                     if(local->hash) {
@@ -3928,21 +3931,7 @@
         
         // everything seems fine, we can update the local resource
         char *etag = dav_get_string_property(up_res, "D:getetag");
-        if(etag) {
-            if(strlen(etag) > 2 && etag[0] == 'W' && etag[1] == '/') {
-                etag = etag + 2;
-            } 
-        }
-
-        if(local->etag) {
-            free(local->etag);
-        }
-
-        if(etag) {
-            local->etag = strdup(etag);
-        } else {
-            local->etag = NULL;
-        }
+        local_resource_set_etag(local, etag);
         
         local->last_modified = s.st_mtime;
         

mercurial