Thu, 28 Mar 2019 18:23:22 +0100
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