mirror of
https://github.com/amadvance/snapraid.git
synced 2026-10-25 18:40:19 -04:00
Avoid recreating missing files and directories during fix with syncedonly (-e/-b)
This commit is contained in:
parent
6b8d3e23a0
commit
0138b48023
3 changed files with 96 additions and 62 deletions
|
|
@ -208,7 +208,7 @@
|
|||
*
|
||||
* A quarantined file may be reopened later during the same fix run. In that case
|
||||
* FILE_IS_QUARANTINED identifies it as belonging to the current failed recovery and
|
||||
* handle_create() continues operating on its .unrecoverable file.
|
||||
* handle_open_create() continues operating on its .unrecoverable file.
|
||||
*
|
||||
* A partial -S/-B fix operates only at block level and therefore never performs
|
||||
* file-level finalization.
|
||||
|
|
@ -1523,7 +1523,7 @@ static int file_post(struct snapraid_state* state, int fix, int partial, block_o
|
|||
}
|
||||
|
||||
/* reopen the file for writing, as required to set the mtime on Windows */
|
||||
ret = handle_create(&handle[j], file, state->file_mode);
|
||||
ret = handle_open_create(&handle[j], file, state->file_mode, 0);
|
||||
if (ret != 0) {
|
||||
/* LCOV_EXCL_START */
|
||||
log_tag("%s:%" PRIu64 ":%s:%s: Open error. %s.\n", es(errno), i, disk->name, esc_tag(file->sub), strerror(errno));
|
||||
|
|
@ -2072,8 +2072,9 @@ static int state_check_process(struct snapraid_state* state, int fix, struct sna
|
|||
|
||||
/* if fixing, and the file is not excluded, we must open for writing */
|
||||
if (fix && !file_flag_has(file, FILE_IS_EXCLUDED)) {
|
||||
/* if fixing, create the file, open for writing and resize if required */
|
||||
ret = handle_create(&handle[j], file, state->file_mode);
|
||||
if (!state->opt.syncedonly) {
|
||||
/* ordinary fix: create the file, open for writing and resize if required */
|
||||
ret = handle_open_create(&handle[j], file, state->file_mode, 1);
|
||||
if (ret == -1) {
|
||||
/* LCOV_EXCL_START */
|
||||
log_tag("%s:%" PRIu64 ":%s:%s: Create error. %s.\n", es(errno), i, disk->name, esc_tag(file->sub), strerror(errno));
|
||||
|
|
@ -2093,6 +2094,29 @@ static int state_check_process(struct snapraid_state* state, int fix, struct sna
|
|||
*/
|
||||
file_flag_set(file, FILE_IS_CREATED);
|
||||
}
|
||||
} else {
|
||||
/* in syncedonly mode, open for writing but do not create missing files */
|
||||
if (!file_flag_has(file, FILE_IS_MISSING)) {
|
||||
ret = handle_open_create(&handle[j], file, state->file_mode, 0);
|
||||
} else {
|
||||
errno = ENOENT;
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
/* if missing, mark as unsynced and let common error handler record the missing block */
|
||||
if (ret == -1 && errno == ENOENT) {
|
||||
file_flag_set(file, FILE_IS_UNSYNCED);
|
||||
} else if (ret == -1) {
|
||||
/* LCOV_EXCL_START */
|
||||
log_tag("%s:%" PRIu64 ":%s:%s: Open error. %s.\n", es(errno), i, disk->name, esc_tag(file->sub), strerror(errno));
|
||||
log_fatal_errno(errno, disk->name);
|
||||
log_fatal(errno, "Stopping at block %" PRIu64 "\n", i);
|
||||
|
||||
++unrecoverable_error;
|
||||
goto bail;
|
||||
/* LCOV_EXCL_STOP */
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/* open the file only for reading */
|
||||
if (!file_flag_has(file, FILE_IS_MISSING)) {
|
||||
|
|
@ -2101,6 +2125,8 @@ static int state_check_process(struct snapraid_state* state, int fix, struct sna
|
|||
errno = ENOENT;
|
||||
ret = -1; /* if the file is missing, we cannot open it */
|
||||
}
|
||||
}
|
||||
|
||||
if (ret == -1) {
|
||||
/* save the failed block for the check/fix */
|
||||
failed[failed_count].is_bad = 1;
|
||||
|
|
@ -2124,12 +2150,11 @@ static int state_check_process(struct snapraid_state* state, int fix, struct sna
|
|||
/*
|
||||
* Mark the file as missing, to avoid to retry to open it again
|
||||
* note that this can be done only if we are not fixing it
|
||||
* otherwise, it could be recreated
|
||||
* or if syncedonly is active so it won't be recreated
|
||||
*/
|
||||
file_flag_set(file, FILE_IS_MISSING);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
/* if it's the first open, and not excluded */
|
||||
if (!file_flag_has(file, FILE_IS_OPENED)
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
/****************************************************************************/
|
||||
/* handle */
|
||||
|
||||
int handle_create(struct snapraid_handle* handle, struct snapraid_file* file, int mode)
|
||||
int handle_open_create(struct snapraid_handle* handle, struct snapraid_file* file, int mode, int create)
|
||||
{
|
||||
int ret;
|
||||
int flags;
|
||||
|
|
@ -27,13 +27,6 @@ int handle_create(struct snapraid_handle* handle, struct snapraid_file* file, in
|
|||
pathprint(path, sizeof(path), "%s%s", handle->disk->dir, file->sub);
|
||||
pathprint(path_unrecoverable, sizeof(path_unrecoverable), "%s.unrecoverable", path);
|
||||
|
||||
ret = mkancestor(path);
|
||||
if (ret != 0) {
|
||||
/* LCOV_EXCL_START */
|
||||
return -1;
|
||||
/* LCOV_EXCL_STOP */
|
||||
}
|
||||
|
||||
/* initial values, changed later if required */
|
||||
handle->created = 0;
|
||||
handle->readonly_errno = 0;
|
||||
|
|
@ -94,8 +87,15 @@ int handle_create(struct snapraid_handle* handle, struct snapraid_file* file, in
|
|||
}
|
||||
}
|
||||
|
||||
/* if failed for missing file */
|
||||
if (handle->f == -1 && errno == ENOENT) {
|
||||
/* if failed for missing file and creation allowed */
|
||||
if (handle->f == -1 && errno == ENOENT && create) {
|
||||
ret = mkancestor(path);
|
||||
if (ret != 0) {
|
||||
/* LCOV_EXCL_START */
|
||||
return -1;
|
||||
/* LCOV_EXCL_STOP */
|
||||
}
|
||||
|
||||
handle->f = open(path, flags | O_RDWR | O_CREAT | O_EXCL, 0600);
|
||||
if (handle->f != -1) {
|
||||
handle->created = 1;
|
||||
|
|
@ -109,12 +109,20 @@ int handle_create(struct snapraid_handle* handle, struct snapraid_file* file, in
|
|||
}
|
||||
|
||||
if (handle->f == -1) {
|
||||
/*
|
||||
* If creation was not requested, a missing file is an expected condition,
|
||||
* not a fatal error.
|
||||
*/
|
||||
if (create || errno != ENOENT) {
|
||||
/* LCOV_EXCL_START */
|
||||
log_fatal(errno, "Error opening file '%s'. %s.\n", handle->path, strerror(errno));
|
||||
handle_close(handle);
|
||||
return -1;
|
||||
/* LCOV_EXCL_STOP */
|
||||
}
|
||||
int saved_errno = errno;
|
||||
handle_close(handle);
|
||||
errno = saved_errno;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* just opened */
|
||||
handle->file = file;
|
||||
|
|
@ -182,10 +190,10 @@ int handle_quarantine(struct snapraid_handle* handle, struct snapraid_file* file
|
|||
file_flag_set(file, FILE_IS_QUARANTINED);
|
||||
|
||||
/*
|
||||
* FILE_IS_QUARANTINED is set, so handle_create() reopens the
|
||||
* FILE_IS_QUARANTINED is set, so handle_open_create() reopens the
|
||||
* .unrecoverable file belonging to the current recovery attempt.
|
||||
*/
|
||||
ret = handle_create(handle, file, mode);
|
||||
ret = handle_open_create(handle, file, mode, 0);
|
||||
if (ret != 0)
|
||||
return -1;
|
||||
#else
|
||||
|
|
|
|||
|
|
@ -24,15 +24,16 @@ struct snapraid_handle {
|
|||
};
|
||||
|
||||
/**
|
||||
* Create or reopen a recovery file.
|
||||
* Open or create a recovery file.
|
||||
* A new recovery attempt always uses the normal filename, even if a previous .unrecoverable file exists.
|
||||
* FILE_IS_QUARANTINED determines whether the .unrecoverable filename is opened in the current run.
|
||||
* If the normal file is created, the handle->created is set.
|
||||
* If create is non-zero, a missing file is created and handle->created is set.
|
||||
* If create is zero, a missing file is not created, returning -1 with errno == ENOENT.
|
||||
* The initial size of the file is stored in the file->st struct.
|
||||
* If the file cannot be opened for write access, it's opened with read-only access.
|
||||
* The read-only access works only if the file has already the correct size and doesn't need to be modified.
|
||||
*/
|
||||
int handle_create(struct snapraid_handle* handle, struct snapraid_file* file, int mode);
|
||||
int handle_open_create(struct snapraid_handle* handle, struct snapraid_file* file, int mode, int create);
|
||||
|
||||
/**
|
||||
* Move an opened file to its persistent .unrecoverable name.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue