store content hash for splitted resources in the db

Thu, 28 Mar 2019 18:23:22 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 28 Mar 2019 18:23:22 +0100
changeset 545
268157722a0e
parent 544
9e85e1ec1155
child 546
33e312dd851d

store content hash for splitted resources in the db

dav/sync.c file | annotate | diff | comparison | revisions
libidav/utils.c file | annotate | diff | comparison | revisions
libidav/utils.h file | annotate | diff | comparison | revisions
--- a/dav/sync.c	Thu Mar 28 18:09:31 2019 +0100
+++ b/dav/sync.c	Thu Mar 28 18:23:22 2019 +0100
@@ -1073,6 +1073,7 @@
     }
     UcxList *part_updates = NULL;
     uint64_t blockcount = 0;
+    char *content_hash = NULL;
     
     if(res->iscollection && !issplit) {
         // why are we here?
@@ -1114,8 +1115,18 @@
         ret = dav_get_content(res, out, (dav_write_func)fwrite);
     }
     fclose(out);
-    if(truncate_file >= 0) {
-        truncate(tmp_path, truncate_file);
+    
+    if(issplit) {
+        if(truncate_file >= 0) {
+            truncate(tmp_path, truncate_file);
+        }
+        
+        char *res_hash = sync_get_content_hash(res);
+        if(res_hash) {
+            content_hash = res_hash;
+        } else {
+            content_hash = util_file_hash(local_path);
+        }
     }
 
     if(ret == 0) {
@@ -1157,11 +1168,17 @@
         if(local->etag) {
             free(local->etag);
         }
+        if(local->hash) {
+            free(local->hash);
+        }
         
         update_parts(local, part_updates, blockcount);
         
         // set metadata from stat
         local->etag = strdup(etag);
+        if(content_hash) {
+            local->hash = content_hash;
+        }
         sync_set_metadata_from_stat(local, &s);
         local->skipped = FALSE;
     } else if(tmp_path) {
--- a/libidav/utils.c	Thu Mar 28 18:09:31 2019 +0100
+++ b/libidav/utils.c	Thu Mar 28 18:23:22 2019 +0100
@@ -874,3 +874,25 @@
         path[len-1] = '\0';
     }
 }
+
+char* util_file_hash(const char *path) {
+    FILE *in = fopen(path, "r");
+    if(!in) {
+        return NULL;
+    }
+    
+    DAV_SHA_CTX *sha = dav_hash_init();
+    char *buf = malloc(16384);
+    
+    size_t r;
+    while((r = fread(buf, 1, 16384, in)) > 0) {
+        dav_hash_update(sha, buf, r);
+    }
+    
+    char hash[DAV_SHA256_DIGEST_LENGTH];
+    dav_hash_final(sha, hash);
+    free(buf);
+    fclose(in);
+    
+    return util_hexstr(hash, DAV_SHA256_DIGEST_LENGTH);    
+}
\ No newline at end of file
--- a/libidav/utils.h	Thu Mar 28 18:09:31 2019 +0100
+++ b/libidav/utils.h	Thu Mar 28 18:23:22 2019 +0100
@@ -111,6 +111,8 @@
 
 void util_remove_trailing_pathseparator(char *path);
 
+char* util_file_hash(const char *path);
+
 #ifdef	__cplusplus
 }
 #endif

mercurial