don't store weak etag in LocalResource

Sun, 11 Aug 2019 10:57:25 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 11 Aug 2019 10:57:25 +0200
changeset 621
06f9fddc82a1
parent 620
a93374f1849a
child 622
6524f45f304e

don't store weak etag in LocalResource

dav/sync.c file | annotate | diff | comparison | revisions
dav/sync.h file | annotate | diff | comparison | revisions
--- 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;
         
--- a/dav/sync.h	Sun Aug 11 08:58:58 2019 +0200
+++ b/dav/sync.h	Sun Aug 11 10:57:25 2019 +0200
@@ -152,6 +152,9 @@
         DavResource *remote,
         LocalResource *res);
 int local_resource_load_metadata(SyncDirectory *dir, LocalResource *res);
+
+void local_resource_set_etag(LocalResource *local, const char *etag);
+
 char* resource_local_path(DavResource *res);
 
 size_t resource_get_blocksize(SyncDirectory *dir, LocalResource *local, DavResource *res, off_t filesize);

mercurial