From adbc74f862df3d79c0411beaab82aae63195972e Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Sun, 19 Apr 2015 21:21:49 +0200 Subject: [PATCH] Suppress missing file warnings if -m is specified --- cmdline/check.c | 6 ++++-- cmdline/dry.c | 4 ++-- cmdline/handle.c | 21 +++++++++++++++++---- cmdline/handle.h | 4 ++-- cmdline/scrub.c | 4 ++-- cmdline/snapraid.c | 1 + cmdline/state.h | 1 + cmdline/support.c | 17 +++++++++++++++++ cmdline/support.h | 7 +++++++ cmdline/sync.c | 8 ++++---- 10 files changed, 57 insertions(+), 16 deletions(-) diff --git a/cmdline/check.c b/cmdline/check.c index 95d080a4..94ba37e1 100644 --- a/cmdline/check.c +++ b/cmdline/check.c @@ -999,7 +999,8 @@ static int state_check_process(struct snapraid_state* state, int fix, struct sna } } else { /* otherwise, open the file only for reading */ - ret = handle_open(&handle[j], file, state->file_mode, msg_warning); + ret = handle_open(&handle[j], file, state->file_mode, + msg_warning, state->opt.expected_missing ? msg_expected : 0); if (ret == -1) { /* save the failed block for the check/fix */ failed[failed_count].is_bad = 1; @@ -1063,7 +1064,8 @@ static int state_check_process(struct snapraid_state* state, int fix, struct sna } /* read from the file */ - read_size = handle_read(&handle[j], block, buffer[j], state->block_size, msg_warning); + read_size = handle_read(&handle[j], block, buffer[j], state->block_size, + msg_warning, state->opt.expected_missing ? msg_expected : 0); if (read_size == -1) { /* save the failed block for the check/fix */ failed[failed_count].is_bad = 1; /* it's bad because we cannot read it */ diff --git a/cmdline/dry.c b/cmdline/dry.c index a6fbc109..96318b43 100644 --- a/cmdline/dry.c +++ b/cmdline/dry.c @@ -91,7 +91,7 @@ static int state_dry_process(struct snapraid_state* state, struct snapraid_parit } /* open the file only for reading */ - ret = handle_open(&handle[j], block_file_get(block), state->file_mode, msg_error); + ret = handle_open(&handle[j], block_file_get(block), state->file_mode, msg_error, 0); if (ret == -1) { /* LCOV_EXCL_START */ msg_error("DANGER! Unexpected open error in a data disk, it isn't possible to dry.\n"); @@ -103,7 +103,7 @@ static int state_dry_process(struct snapraid_state* state, struct snapraid_parit } /* read from the file */ - read_size = handle_read(&handle[j], block, buffer_aligned, state->block_size, msg_warning); + read_size = handle_read(&handle[j], block, buffer_aligned, state->block_size, msg_warning, 0); if (read_size == -1) { msg_tag("error:%u:%s:%s: Read error at position %u\n", i, disk->name, esc(block_file_get(block)->sub), block_file_pos(block)); ++error; diff --git a/cmdline/handle.c b/cmdline/handle.c index 79a16278..887f0f99 100644 --- a/cmdline/handle.c +++ b/cmdline/handle.c @@ -148,11 +148,14 @@ int handle_truncate(struct snapraid_handle* handle, struct snapraid_file* file) return 0; } -int handle_open(struct snapraid_handle* handle, struct snapraid_file* file, int mode, fptr* out) +int handle_open(struct snapraid_handle* handle, struct snapraid_file* file, int mode, fptr* out, fptr* out_missing) { int ret; int flags; + if (!out_missing) + out_missing = out; + /* if already opened, nothing to do */ if (handle->file == file && handle->f != -1) { return 0; @@ -179,7 +182,10 @@ int handle_open(struct snapraid_handle* handle, struct snapraid_file* file, int handle->f = -1; handle->valid_size = 0; - out("Error opening file '%s'. %s.\n", handle->path, strerror(errno)); + if (errno == ENOENT) + out_missing("Missing file '%s'.\n", handle->path); + else + out("Error opening file '%s'. %s.\n", handle->path, strerror(errno)); return -1; } @@ -242,7 +248,7 @@ int handle_close(struct snapraid_handle* handle) return 0; } -int handle_read(struct snapraid_handle* handle, struct snapraid_block* block, unsigned char* block_buffer, unsigned block_size, fptr* out) +int handle_read(struct snapraid_handle* handle, struct snapraid_block* block, unsigned char* block_buffer, unsigned block_size, fptr* out, fptr* out_missing) { ssize_t read_ret; data_off_t offset; @@ -251,9 +257,16 @@ int handle_read(struct snapraid_handle* handle, struct snapraid_block* block, un offset = block_file_pos(block) * (data_off_t)block_size; + if (!out_missing) + out_missing = out; + /* check if we are going to read only not initialized data */ if (offset >= handle->valid_size) { - out("Reading missing data from file '%s' at offset %" PRIu64 ".\n", handle->path, offset); + /* if the file is missing, it's at 0 size, or it's rebuilt while reading */ + if (offset == handle->valid_size || handle->valid_size == 0) + out_missing("Reading data from missing file '%s' at offset %" PRIu64 ".\n", handle->path, offset); + else + out("Reading missing data from file '%s' at offset %" PRIu64 ".\n", handle->path, offset); return -1; } diff --git a/cmdline/handle.h b/cmdline/handle.h index 75f724cc..7ef2432a 100644 --- a/cmdline/handle.h +++ b/cmdline/handle.h @@ -53,7 +53,7 @@ int handle_truncate(struct snapraid_handle* handle, struct snapraid_file* file); * Opens a file. * The file is opened for reading. */ -int handle_open(struct snapraid_handle* handle, struct snapraid_file* file, int mode, fptr* out); +int handle_open(struct snapraid_handle* handle, struct snapraid_file* file, int mode, fptr* out, fptr* out_missing); /** * Closes a file. @@ -64,7 +64,7 @@ int handle_close(struct snapraid_handle* handle); * Read a block from a file. * If the read block is shorter, it's padded with 0. */ -int handle_read(struct snapraid_handle* handle, struct snapraid_block* block, unsigned char* block_buffer, unsigned block_size, fptr* out); +int handle_read(struct snapraid_handle* handle, struct snapraid_block* block, unsigned char* block_buffer, unsigned block_size, fptr* out, fptr* out_missing); /** * Writes a block to a file. diff --git a/cmdline/scrub.c b/cmdline/scrub.c index 78d5f909..bc977f58 100644 --- a/cmdline/scrub.c +++ b/cmdline/scrub.c @@ -246,7 +246,7 @@ static int state_scrub_process(struct snapraid_state* state, struct snapraid_par } } - ret = handle_open(&handle[j], block_file_get(block), state->file_mode, msg_warning); + ret = handle_open(&handle[j], block_file_get(block), state->file_mode, msg_warning, 0); if (ret == -1) { /* file we have tried to open for error reporting */ struct snapraid_file* file = block_file_get(block); @@ -283,7 +283,7 @@ static int state_scrub_process(struct snapraid_state* state, struct snapraid_par /* from the last sync, as we are expected to return errors if running */ /* in an unsynced array. This is just like the check command. */ - read_size = handle_read(&handle[j], block, buffer[j], state->block_size, msg_warning); + read_size = handle_read(&handle[j], block, buffer[j], state->block_size, msg_warning, 0); if (read_size == -1) { /* file we are processing for error reporting */ struct snapraid_file* file = block_file_get(block); diff --git a/cmdline/snapraid.c b/cmdline/snapraid.c index 4787011c..a9093d5c 100644 --- a/cmdline/snapraid.c +++ b/cmdline/snapraid.c @@ -481,6 +481,7 @@ int main(int argc, char* argv[]) } break; case 'm' : filter_missing = 1; + opt.expected_missing = 1; break; case 'e' : /* when processing only error, we filter both files and blocks */ diff --git a/cmdline/state.h b/cmdline/state.h index e8e82afb..6d189c91 100644 --- a/cmdline/state.h +++ b/cmdline/state.h @@ -103,6 +103,7 @@ struct snapraid_option { int force_progress; /**< Force the use of the progress status. */ unsigned force_autosave_at; /**< Force autosave at the specified block. */ int fake_device; /**< Fake device data. */ + int expected_missing; /**< If missing files are expected and should not be reported. */ }; /** diff --git a/cmdline/support.c b/cmdline/support.c index 406d78e4..ba1aef91 100644 --- a/cmdline/support.c +++ b/cmdline/support.c @@ -98,6 +98,23 @@ void msg_warning(const char* format, ...) pthread_mutex_unlock(&msg_lock); } +void msg_expected(const char* format, ...) +{ + va_list ap; + + pthread_mutex_lock(&msg_lock); + + if (stdlog) { + va_start(ap, format); + fprintf(stdlog, "msg:warning: "); + vfprintf(stdlog, format, ap); + fflush(stdlog); + va_end(ap); + } + + pthread_mutex_unlock(&msg_lock); +} + void msg_tag(const char* format, ...) { va_list ap; diff --git a/cmdline/support.h b/cmdline/support.h index 67a7d683..58a14a78 100644 --- a/cmdline/support.h +++ b/cmdline/support.h @@ -42,6 +42,13 @@ void msg_error(const char* format, ...) __attribute__((format(attribute_printf, */ void msg_warning(const char* format, ...) __attribute__((format(attribute_printf, 1, 2))); +/** + * Warning messages, without fallback to stderr. + * + * These messages go in the log file if specified, otherwise they are lost. + */ +void msg_expected(const char* format, ...) __attribute__((format(attribute_printf, 1, 2))); + /** * Tag messages. * diff --git a/cmdline/sync.c b/cmdline/sync.c index 70e72d2b..2476b5bb 100644 --- a/cmdline/sync.c +++ b/cmdline/sync.c @@ -150,7 +150,7 @@ static int state_hash_process(struct snapraid_state* state, block_off_t blocksta } } - ret = handle_open(&handle[j], block_file_get(block), state->file_mode, msg_warning); + ret = handle_open(&handle[j], block_file_get(block), state->file_mode, msg_warning, 0); if (ret == -1) { /* file we have tried to open for error reporting */ struct snapraid_file* file = block_file_get(block); @@ -221,7 +221,7 @@ static int state_hash_process(struct snapraid_state* state, block_off_t blocksta continue; } - read_size = handle_read(&handle[j], block, buffer, state->block_size, msg_error); + read_size = handle_read(&handle[j], block, buffer, state->block_size, msg_error, 0); if (read_size == -1) { /* LCOV_EXCL_START */ /* file we are processing for error reporting */ @@ -624,7 +624,7 @@ static int state_sync_process(struct snapraid_state* state, struct snapraid_pari } } - ret = handle_open(&handle[j], block_file_get(block), state->file_mode, msg_warning); + ret = handle_open(&handle[j], block_file_get(block), state->file_mode, msg_warning, 0); if (ret == -1) { /* file we have tried to open for error reporting */ struct snapraid_file* file = block_file_get(block); @@ -698,7 +698,7 @@ static int state_sync_process(struct snapraid_state* state, struct snapraid_pari continue; } - read_size = handle_read(&handle[j], block, buffer[j], state->block_size, msg_warning); + read_size = handle_read(&handle[j], block, buffer[j], state->block_size, msg_warning, 0); if (read_size == -1) { /* LCOV_EXCL_START */ /* file we are processing for error reporting */