From 8c7332cfdaf32581ad641519f607b04653b75769 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Tue, 19 Mar 2013 21:59:13 +0100 Subject: [PATCH] Rechecks the hash of imported files before using them. --- TODO | 4 ++++ check.c | 4 ++-- import.c | 10 ++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/TODO b/TODO index 7418cd47..bb461691 100644 --- a/TODO +++ b/TODO @@ -5,6 +5,10 @@ This is the list of TODO items for SnapRAID. - v3.1 +* In the import list, uses also all the blocks in the array. +But we must cover the case of bad blocks. Likely we can just check the +hash after reading, and in case, skip it, and retry with another copy. + * If a content file is missing, sync should rewrite it, even if no change was done at the content. * A new 'init' command to differentiate the first 'sync' operation. diff --git a/check.c b/check.c index b5aa6248..7a93f269 100644 --- a/check.c +++ b/check.c @@ -275,11 +275,11 @@ static int repair(struct snapraid_state* state, unsigned pos, unsigned diskmax, } } else { assert(block_state == BLOCK_STATE_CHG || block_state == BLOCK_STATE_NEW); - + /* otherwise it's CHG or NEW and we try to recover it */ failed_map[n] = j; ++n; - + /* we have something to try to recover */ something_to_recover = 1; } diff --git a/import.c b/import.c index b54626f6..644d418f 100644 --- a/import.c +++ b/import.c @@ -125,6 +125,7 @@ int state_import_fetch(struct snapraid_state* state, const unsigned char* hash, int f; unsigned block_size = state->block_size; unsigned read_size; + unsigned char buffer_hash[HASH_SIZE]; const char* path; block = tommy_hashdyn_search(&state->importset, import_block_compare, hash, import_block_hash(hash)); @@ -162,6 +163,15 @@ int state_import_fetch(struct snapraid_state* state, const unsigned char* hash, memset(buffer + read_size, 0, block_size - read_size); } + /* recheck the hash */ + memhash(state->hash, buffer_hash, buffer, read_size); + + if (memcmp(buffer_hash, hash, HASH_SIZE) != 0) { + fprintf(stderr, "Error in data reading file '%s'.\n", path); + fprintf(stderr, "Please don't change imported files while running.\n"); + exit(EXIT_FAILURE); + } + return 0; }