From d5c1843ba6085c14559661d21b7a960169ce1f17 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Sun, 28 Apr 2013 19:01:36 +0200 Subject: [PATCH] Deletes create file if the fix process is aborted. --- HISTORY | 2 + TODO | 9 +-- check.c | 177 +++++++++++++++++++++++++++++++++++-------------------- elem.h | 40 +++++++++++-- handle.c | 15 ++++- handle.h | 3 +- 6 files changed, 167 insertions(+), 79 deletions(-) diff --git a/HISTORY b/HISTORY index 4b84a454..fbf53d17 100644 --- a/HISTORY +++ b/HISTORY @@ -4,6 +4,8 @@ SnapRAID HISTORY 3.1 2013/4 ========== * Direct use of Windows API for disk access to improve error reporting. + * If the 'fix' process is aborted, renames to .unrecoverable all the new + files created, to allow to reuse again the '-m, --filter-missing' flag. * In Windows don't exclude anymore system files. Only system directories are excluded. diff --git a/TODO b/TODO index 10950f48..105bbdaf 100644 --- a/TODO +++ b/TODO @@ -5,12 +5,9 @@ This is the list of TODO items for SnapRAID. - v3.1 -* If the fix process is interrupted by pressing Ctrl+C, we should rename files partially -recovered to .unrecoverable, if there were created originally missing from the array. -This could be detect checking the size just after creation. -If it's 0, the file should be renamed. -We cannot rename all files, because existing one may contain valid data -that we should not lose. +* Allow to specify the shared path to use to access remote symlink. + +- v3.2 * 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 diff --git a/check.c b/check.c index 21ae660b..5d7b0582 100644 --- a/check.c +++ b/check.c @@ -591,8 +591,9 @@ static int state_check_process(struct snapraid_state* state, int check, int fix, continue; } - /* if the file is different than the current one, close it */ - if (handle[j].file != block_file_get(block)) { + /* if the file is closed or different than the current one */ + if (handle[j].file == 0 || handle[j].file != block_file_get(block)) { + /* close the old one, if any */ ret = handle_close(&handle[j]); if (ret == -1) { fprintf(stderr, "DANGER! Unexpected close error in a data disk, it isn't possible to sync.\n"); @@ -600,61 +601,68 @@ static int state_check_process(struct snapraid_state* state, int check, int fix, ++unrecoverable_error; goto bail; } - } - /* if fixing, and the file is not excluded, we must open for writing */ - if (fix && !file_flag_has(block_file_get(block), FILE_IS_EXCLUDED)) { - /* if fixing, create the file, open for writing and resize if required */ - ret = handle_create(&handle[j], block_file_get(block), state->skip_sequential); - if (ret == -1) { - if (errno == EACCES) { - fprintf(stderr, "WARNING! Please give write permission to the file.\n"); - } else { - fprintf(stderr, "DANGER! Without a working data disk, it isn't possible to fix errors on it.\n"); + /* if fixing, and the file is not excluded, we must open for writing */ + if (fix && !file_flag_has(block_file_get(block), FILE_IS_EXCLUDED)) { + /* if fixing, create the file, open for writing and resize if required */ + ret = handle_create(&handle[j], block_file_get(block), state->skip_sequential); + if (ret == -1) { + if (errno == EACCES) { + fprintf(stderr, "WARNING! Please give write permission to the file.\n"); + } else { + fprintf(stderr, "DANGER! Without a working data disk, it isn't possible to fix errors on it.\n"); + } + printf("Stopping at block %u\n", i); + ++unrecoverable_error; + goto bail; } - printf("Stopping at block %u\n", i); - ++unrecoverable_error; - goto bail; - } - /* check if the file was larger and now truncated */ - if (ret == 1) { - fprintf(stdlog, "File '%s' is larger than expected.\n", handle[j].path); - fprintf(stdlog, "error:%u:%s:%s: Size error\n", i, handle[j].disk->name, block_file_get(block)->sub); - ++error; + /* check if the file was larger and now truncated */ + if (ret == 1) { + fprintf(stdlog, "File '%s' is larger than expected.\n", handle[j].path); + fprintf(stdlog, "error:%u:%s:%s: Size error\n", i, handle[j].disk->name, block_file_get(block)->sub); + ++error; - /* this is already a recovered error */ - fprintf(stdlog, "fixed:%u:%s:%s: Fixed size\n", i, handle[j].disk->name, block_file_get(block)->sub); - ++recovered_error; - } - } else { - /* if checking or hashing, open the file only for reading */ - ret = handle_open(&handle[j], block_file_get(block), stdlog, state->skip_sequential); - if (ret == -1) { - /* save the failed block for the check/fix */ - failed[failed_count].is_bad = 1; - failed[failed_count].is_outofdate = 0; - failed[failed_count].index = j; - failed[failed_count].block = block; - failed[failed_count].handle = &handle[j]; - ++failed_count; + /* this is already a recovered error */ + fprintf(stdlog, "fixed:%u:%s:%s: Fixed size\n", i, handle[j].disk->name, block_file_get(block)->sub); + ++recovered_error; + } - fprintf(stdlog, "error:%u:%s:%s: Open error at position %u\n", i, handle[j].disk->name, block_file_get(block)->sub, block_file_pos(block)); - ++error; - continue; - } + /* check if the file was just created */ + if (handle[j].created != 0) { + /* if fragmented, it may be reopened, so remember that the file */ + /* was originally missing */ + file_flag_set(block_file_get(block), FILE_IS_CREATED); + } + } else { + /* if checking or hashing, open the file only for reading */ + ret = handle_open(&handle[j], block_file_get(block), stdlog, state->skip_sequential); + if (ret == -1) { + /* save the failed block for the check/fix */ + failed[failed_count].is_bad = 1; + failed[failed_count].is_outofdate = 0; + failed[failed_count].index = j; + failed[failed_count].block = block; + failed[failed_count].handle = &handle[j]; + ++failed_count; - /* check if it's a larger file, but not if already notified */ - if (!file_flag_has(block_file_get(block), FILE_IS_LARGER) - && handle[j].st.st_size > block_file_get(block)->size - ) { - fprintf(stdlog, "File '%s' is larger than expected.\n", handle[j].path); - fprintf(stdlog, "error:%u:%s:%s: Size error\n", i, handle[j].disk->name, block_file_get(block)->sub); - ++error; + fprintf(stdlog, "error:%u:%s:%s: Open error at position %u\n", i, handle[j].disk->name, block_file_get(block)->sub, block_file_pos(block)); + ++error; + continue; + } - /* if fragmented, it may be reopened, so store the notification */ - /* to prevent to signal and count the error more than one time */ - file_flag_set(block_file_get(block), FILE_IS_LARGER); + /* check if it's a larger file, but not if already notified */ + if (!file_flag_has(block_file_get(block), FILE_IS_LARGER) + && handle[j].st.st_size > block_file_get(block)->size + ) { + fprintf(stdlog, "File '%s' is larger than expected.\n", handle[j].path); + fprintf(stdlog, "error:%u:%s:%s: Size error\n", i, handle[j].disk->name, block_file_get(block)->sub); + ++error; + + /* if fragmented, it may be reopened, so store the notification */ + /* to prevent to signal and count the error more than one time */ + file_flag_set(block_file_get(block), FILE_IS_LARGER); + } } } @@ -911,7 +919,8 @@ static int state_check_process(struct snapraid_state* state, int check, int fix, } } - /* for all the files prints the final status, does the final time fix */ + /* for all the files prints the final status, and does the final time fix */ + /* we also ensure to close files after processing the last block */ for(j=0;jdir, file->sub); - /* ensure to operate on a closed file */ + /* ensure to close the file before renaming */ ret = handle_close(&handle[j]); if (ret != 0) { fprintf(stderr, "Error closing '%s'. %s.\n", path, strerror(errno)); @@ -992,8 +1001,8 @@ static int state_check_process(struct snapraid_state* state, int check, int fix, /* if the file is not fixed, meaning that it is untouched */ if (!file_flag_has(file, FILE_IS_FIXED)) { - /* nothing to do */ - continue; + /* nothing to do, but close the file */ + goto close_and_continue; } if (state->gui) { @@ -1065,6 +1074,17 @@ static int state_check_process(struct snapraid_state* state, int check, int fix, } } } + +close_and_continue: + /* ensure to close the file just after finishing with it */ + ret = handle_close(&handle[j]); + if (ret != 0) { + fprintf(stderr, "Error closing '%s'. %s.\n", path, strerror(errno)); + fprintf(stderr, "WARNING! Without a working data disk, it isn't possible to fix errors on it.\n"); + printf("Stopping at block %u\n", i); + ++unrecoverable_error; + goto bail; + } } /* count the number of processed block */ @@ -1401,11 +1421,38 @@ static int state_check_process(struct snapraid_state* state, int check, int fix, bail: for(j=0;jfile != 0) { + struct snapraid_file* file = handle[j].file; + + /* now close it */ + ret = handle_close(&handle[j]); + if (ret == -1) { + fprintf(stderr, "DANGER! Unexpected close error in a data disk.\n"); + ++unrecoverable_error; + /* continue, as we are already exiting */ + } + + /* if we closed the file now, it means that we are aborting the execution */ + /* and if the file was originally missing, we have to throw it away */ + /* to ensure that at the next run we will retry to fix it */ + /* in case we select to undelete missing files */ + if (fix && file_flag_has(file, FILE_IS_CREATED)) { + /* rename it to .unrecoverable */ + char path[PATH_MAX]; + char path_to[PATH_MAX]; + + pathprint(path, sizeof(path), "%s%s", handle[j].disk->dir, file->sub); + pathprint(path_to, sizeof(path_to), "%s%s.unrecoverable", handle[j].disk->dir, file->sub); + + ret = rename(path, path_to); + if (ret != 0) { + fprintf(stderr, "Error renaming '%s' to '%s'. %s.\n", path, path_to, strerror(errno)); + fprintf(stderr, "WARNING! Without a working data disk, it isn't possible to fix errors on it.\n"); + ++unrecoverable_error; + /* continue, as we are already exiting */ + } + } } } diff --git a/elem.h b/elem.h index 594c146d..86831c9b 100644 --- a/elem.h +++ b/elem.h @@ -135,11 +135,41 @@ struct snapraid_block { */ #define BLOCK_EMPTY 0 -#define FILE_IS_PRESENT 0x01 /**< If it's seen as present. */ -#define FILE_IS_EXCLUDED 0x02 /**< If it's an excluded file from the processing. */ -#define FILE_IS_LARGER 0x04 /**< If a larger file was already detected. Just to avoid to report it more times. */ -#define FILE_IS_DAMAGED 0x08 /**< If a fix was attempted but it failed. */ -#define FILE_IS_FIXED 0x10 /**< If a fix was done. */ +/** + * If it's seen as present. + * It's used only in sync. + */ +#define FILE_IS_PRESENT 0x01 + +/** + * If it's an excluded file from the processing. + * It's used in sync/check/fix. + */ +#define FILE_IS_EXCLUDED 0x02 + +/** + * If the file was originally larger, and then truncated to the expected suze. + * It's used in check/fix to avoid to report this condition more than one time. + */ +#define FILE_IS_LARGER 0x04 + +/** + * If a fix was attempted but it failed. + * It's used only in fix. + */ +#define FILE_IS_DAMAGED 0x08 + +/** + * If a fix was done. + * It's used only in fix. + */ +#define FILE_IS_FIXED 0x10 + +/** + * If the file was originally missing, and it's created in the fix process. + * It's used only in fix. + */ +#define FILE_IS_CREATED 0x20 #define FILE_IS_HARDLINK 0x100 /**< If it's an hardlink. */ #define FILE_IS_SYMLINK 0x200 /**< If it's a file symlink. */ diff --git a/handle.c b/handle.c index be319294..fe9d195f 100644 --- a/handle.c +++ b/handle.c @@ -44,10 +44,19 @@ int handle_create(struct snapraid_handle* handle, struct snapraid_file* file, in /* open for read write */ /* O_SEQUENTIAL: opening in sequential mode in Windows */ /* O_NOFOLLOW: do not follow links to ensure to open the real file */ - flags = O_RDWR | O_CREAT | O_BINARY | O_NOFOLLOW; + flags = O_RDWR | O_BINARY | O_NOFOLLOW; if (!skip_sequential) flags |= O_SEQUENTIAL; - handle->f = open(handle->path, flags, 0600); + handle->created = 0; + handle->f = open(handle->path, flags); + + /* if failed for missing file */ + if (handle->f == -1 && errno == ENOENT) { + /* retry creating it */ + flags |= O_CREAT; + handle->created = 1; + handle->f = open(handle->path, flags, 0600); + } /* if failed for missing permission */ if (handle->f == -1 && errno == EACCES) { @@ -55,6 +64,7 @@ int handle_create(struct snapraid_handle* handle, struct snapraid_file* file, in flags = O_RDONLY | O_BINARY | O_NOFOLLOW; if (!skip_sequential) flags |= O_SEQUENTIAL; + handle->created = 0; handle->f = open(handle->path, flags); } @@ -137,6 +147,7 @@ int handle_open(struct snapraid_handle* handle, struct snapraid_file* file, FILE flags = O_RDONLY | O_BINARY | O_NOFOLLOW; if (!skip_sequential) flags |= O_SEQUENTIAL; + handle->created = 0; handle->f = open_noatime(handle->path, flags); if (handle->f == -1) { /* invalidate for error */ diff --git a/handle.h b/handle.h index d39120d3..f92c55cb 100644 --- a/handle.h +++ b/handle.h @@ -26,10 +26,11 @@ struct snapraid_handle { char path[PATH_MAX]; /**< Path of the file. */ struct snapraid_disk* disk; /**< Disk of the file. */ - struct snapraid_file* file; /**< File opened. */ + struct snapraid_file* file; /**< File opened. When the file is closed, it's set to 0. */ int f; /**< Handle of the file. */ struct stat st; /**< Stat info of the opened file. */ data_off_t valid_size; /**< Size of the valid data. */ + int created; /**< If the file was created, or it was already existing. */ }; /**