diff options
author | Magnus Ahltorp <map@kth.se> | 2016-11-22 14:31:37 +0100 |
---|---|---|
committer | Magnus Ahltorp <map@kth.se> | 2016-11-22 14:31:37 +0100 |
commit | 8826eb502c73df3a512a2d257f4264d68a10e1c8 (patch) | |
tree | 73363b5b45a832f2764bdff40d29857824b0cc7c /c_src/permdb.c | |
parent | d58b568521780d6ba8e2b89ffbdda1e3571ce6e5 (diff) |
filebuffer: continue reading after short readrobust-distributionexperimental-new-dist
permdb: only read small chunks when verifying commit
Diffstat (limited to 'c_src/permdb.c')
-rw-r--r-- | c_src/permdb.c | 45 |
1 files changed, 34 insertions, 11 deletions
diff --git a/c_src/permdb.c b/c_src/permdb.c index 96dc405..a33c43a 100644 --- a/c_src/permdb.c +++ b/c_src/permdb.c @@ -15,6 +15,7 @@ #include <fcntl.h> #include <err.h> #include <nettle/sha2.h> +#include <sys/param.h> #include "erlport.h" #include "permdb.h" #include "filebuffer.h" @@ -207,24 +208,40 @@ validate_checksum(struct commit_info *commit, buffered_file *file) (long long unsigned) commit->length, (long long unsigned) commit->start)); - unsigned char *checksumdata = - bf_read(file, commit->start, commit->length, NULL); - if (checksumdata == NULL) { - return -1; - } + char *error = NULL; uint8_t checksum[SHA256_DIGEST_SIZE]; struct sha256_ctx commit_checksum_context; sha256_init(&commit_checksum_context); - sha256_update(&commit_checksum_context, commit->length, checksumdata); - sha256_digest(&commit_checksum_context, SHA256_DIGEST_SIZE, checksum); - if (memcmp(checksum, commit->checksum, SHA256_DIGEST_SIZE) == 0) { + node_offset offset = commit->start; + node_offset bytesleft = commit->length; + node_offset chunksize = 1024*1024; + + while (bytesleft > 0) { + node_offset length = MIN(chunksize, bytesleft); + unsigned char *checksumdata = + bf_read(file, offset, length, &error); + if (checksumdata == NULL) { + dprintf(READ, + (stderr, + "validate_checksum: could not read file: %s\n", error)); + return -1; + } + + sha256_update(&commit_checksum_context, length, checksumdata); + + offset += length; + bytesleft -= length; free(checksumdata); + } + + sha256_digest(&commit_checksum_context, SHA256_DIGEST_SIZE, checksum); + + if (memcmp(checksum, commit->checksum, SHA256_DIGEST_SIZE) == 0) { return 0; } - free(checksumdata); return -1; } @@ -294,6 +311,12 @@ datafile_verify_file(buffered_file *file) struct commit_info *data_commit = read_data_commit_backward(file, &offset); + if (data_commit == NULL) { + dprintf(READ, (stderr, "did not find a commit\n")); + } else { + dprintf(READ, (stderr, "last commit: start %llu length %llu\n", (long long unsigned) data_commit->start, (long long unsigned) data_commit->length)); + } + if (data_commit == NULL || validate_checksum(data_commit, file) < 0) { fprintf(stderr, "commit broken: %llu\n", (long long unsigned) offset); @@ -589,8 +612,8 @@ unsigned char * read_internal_data(permdb_object *state, node_offset offset, size_t length) { buffered_file *file = state->datafile; - dprintf(READ, (stderr, "reading data: offset %llu\n", - (unsigned long long) offset)); + dprintf(READ, (stderr, "reading data: offset %llu length %llu\n", + (unsigned long long) offset, (unsigned long long) length)); return bf_read(file, offset, length, &state->error); } |