# HG changeset patch # User Olaf Wintermann # Date 1553793802 -3600 # Node ID 268157722a0e1adc71588a35272119def633dd4d # Parent 9e85e1ec1155a51955ed60fd8e25805e5410bfc2 store content hash for splitted resources in the db diff -r 9e85e1ec1155 -r 268157722a0e dav/sync.c --- 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) { diff -r 9e85e1ec1155 -r 268157722a0e libidav/utils.c --- 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 diff -r 9e85e1ec1155 -r 268157722a0e libidav/utils.h --- 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