Make .unrecoverable a persistent one-way recovery quarantine
Some checks are pending
Build / build-mac (push) Waiting to run
Build / build-linux (push) Waiting to run
Build / build-arm32 (push) Waiting to run
Build / build-win (push) Waiting to run
Build / build-freebsd (push) Waiting to run
Build / build-musl (push) Waiting to run
CodeQL Advanced / Analyze (c-cpp) (push) Waiting to run
CodeQL Advanced / Analyze (actions) (push) Waiting to run

This commit is contained in:
Andrea Mazzoleni 2026-08-29 21:25:49 +02:00
parent f3bed11cef
commit 37bd4b0f4a
3 changed files with 213 additions and 126 deletions

View file

@ -14,6 +14,8 @@ int handle_create(struct snapraid_handle* handle, struct snapraid_file* file, in
{
int ret;
int flags;
char path[PATH_MAX];
char path_unrecoverable[PATH_MAX];
/* if it's the same file, and already opened, nothing to do */
if (handle->file == file && handle->f != -1) {
@ -21,9 +23,11 @@ int handle_create(struct snapraid_handle* handle, struct snapraid_file* file, in
}
advise_init(&handle->advise, mode);
pathprint(handle->path, sizeof(handle->path), "%s%s", handle->disk->dir, file->sub);
ret = mkancestor(handle->path);
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;
@ -42,63 +46,61 @@ int handle_create(struct snapraid_handle* handle, struct snapraid_file* file, in
*/
flags = O_BINARY | O_NOFOLLOW | advise_flags(&handle->advise);
/* open for read write */
handle->f = open(handle->path, flags | O_RDWR);
/*
* A previous .unrecoverable file is a persistent one-way quarantine.
* Always give it precedence over the normal filename, even if both exist.
*/
handle->f = open(path_unrecoverable, flags | O_RDWR);
/* if failed for missing write permission */
if (handle->f == -1 && (errno == EACCES || errno == EROFS)) {
int saved_errno = errno;
/* open for read-only */
handle->f = open(handle->path, flags | O_RDONLY);
handle->f = open(path_unrecoverable, flags | O_RDONLY);
if (handle->f != -1) {
handle->readonly_errno = saved_errno;
}
}
/* if failed for missing file */
if (handle->f == -1 && errno == ENOENT) {
char path_unrecoverable[PATH_MAX];
if (handle->f != -1) {
handle->is_unrecoverable = 1;
pathcpy(handle->path, sizeof(handle->path), path_unrecoverable);
} else if (errno == ENOENT) {
/* no previous quarantine exists, try the normal filename */
handle->f = open(path, flags | O_RDWR);
/* check if exists a .unrecoverable copy */
pathprint(path_unrecoverable, sizeof(path_unrecoverable), "%s.unrecoverable", handle->path);
/*
* Open a previous recovery directly as .unrecoverable, keeping it
* quarantined until the whole file is processed successfully. Promoting
* it now could expose an incomplete or unverified file if this fix aborts
* or fails again. Reuse it for a multistep fix, preserving readable CHG
* blocks because their current hash is not available.
*/
handle->f = open(path_unrecoverable, flags | O_RDWR);
if (handle->f != -1) {
handle->is_unrecoverable = 1;
pathcpy(handle->path, sizeof(handle->path), path_unrecoverable);
} else if (errno == ENOENT) {
/*
* Create missing file as .unrecoverable so that an interruption or
* crash (e.g. SIGKILL) during recovery never leaves a partially recovered
* file visible under its final name.
*/
handle->f = open(path_unrecoverable, flags | O_RDWR | O_CREAT | O_EXCL, 0600);
/* if failed for missing write permission */
if (handle->f == -1 && (errno == EACCES || errno == EROFS)) {
int saved_errno = errno;
/* open for read-only */
handle->f = open(path, flags | O_RDONLY);
if (handle->f != -1) {
handle->is_unrecoverable = 1;
pathcpy(handle->path, sizeof(handle->path), path_unrecoverable);
handle->readonly_errno = saved_errno;
}
}
/* if failed for missing file */
if (handle->f == -1 && errno == ENOENT) {
/*
* The file is genuinely missing and there is no previous
* quarantine. Create it using the normal filename.
*
* It will be moved to .unrecoverable only if recovery later
* encounters data that cannot be certified CURRENT.
*/
handle->f = open(path, flags | O_RDWR | O_CREAT | O_EXCL, 0600);
if (handle->f != -1) {
handle->created = 1;
} else if (errno == EEXIST) {
/* if created concurrently, reopen safely */
handle->f = open(path_unrecoverable, flags | O_RDWR);
if (handle->f != -1) {
handle->is_unrecoverable = 1;
pathcpy(handle->path, sizeof(handle->path), path_unrecoverable);
} else {
pathcpy(handle->path, sizeof(handle->path), path_unrecoverable);
}
} else {
pathcpy(handle->path, sizeof(handle->path), path_unrecoverable);
handle->f = open(path, flags | O_RDWR);
}
} else {
/* report the path that failed to open */
pathcpy(handle->path, sizeof(handle->path), path_unrecoverable);
}
pathcpy(handle->path, sizeof(handle->path), path);
} else {
/* report the .unrecoverable path that failed to open */
pathcpy(handle->path, sizeof(handle->path), path_unrecoverable);
}
if (handle->f == -1) {
@ -137,6 +139,93 @@ int handle_create(struct snapraid_handle* handle, struct snapraid_file* file, in
return 0;
}
/**
* Move an opened file to its persistent .unrecoverable name.
*/
int handle_quarantine(struct snapraid_handle* handle, struct snapraid_file* file, int mode)
{
char path[PATH_MAX];
char path_to[PATH_MAX];
struct stat st;
int ret;
(void)mode;
/* already persistently quarantined */
if (handle->is_unrecoverable)
return 0;
pathprint(path, sizeof(path), "%s%s", handle->disk->dir, file->sub);
pathprint(path_to, sizeof(path_to), "%s%s.unrecoverable", handle->disk->dir, file->sub);
/*
* Never overwrite a previous quarantine.
*/
ret = lstat(path_to, &st);
if (ret == 0) {
errno = EEXIST;
log_error(errno, "Unrecoverable file '%s' already exists.\n", path_to);
return -1;
}
if (errno != ENOENT) {
/* LCOV_EXCL_START */
log_error(errno, "Error accessing file '%s'. %s.\n", path_to, strerror(errno));
return -1;
/* LCOV_EXCL_STOP */
}
#ifdef _WIN32
/*
* Windows may not allow renaming an opened file. Close it before
* the rename and reopen the quarantined file afterward.
*/
ret = handle_close(handle);
if (ret != 0)
return -1;
ret = rename(path, path_to);
if (ret != 0) {
/* LCOV_EXCL_START */
log_error(errno, "Error renaming file '%s' to '%s'. %s.\n", path, path_to, strerror(errno));
return -1;
/* LCOV_EXCL_STOP */
}
/*
* handle_create() gives precedence to the existing .unrecoverable
* file, so it reopens path_to and sets is_unrecoverable.
*/
ret = handle_create(handle, file, mode);
if (ret != 0)
return -1;
if (!handle->is_unrecoverable) {
/* LCOV_EXCL_START */
errno = ENXIO;
log_error(errno, "Error renaming file '%s' to '%s'. %s.\n", path, path_to, strerror(errno));
return -1;
/* LCOV_EXCL_STOP */
}
#else
/*
* POSIX allows renaming an opened file. The descriptor continues
* to reference the same inode, so no close/reopen is required.
*/
ret = rename(path, path_to);
if (ret != 0) {
/* LCOV_EXCL_START */
log_error(errno, "Error renaming file '%s' to '%s'. %s.\n", path, path_to, strerror(errno));
return -1;
/* LCOV_EXCL_STOP */
}
handle->is_unrecoverable = 1;
pathcpy(handle->path, sizeof(handle->path), path_to);
#endif
return 0;
}
int handle_truncate(struct snapraid_handle* handle, struct snapraid_file* file)
{
int ret;