Reuses previous .unrecoverable files to help multistep recovering.

This commit is contained in:
Andrea Mazzoleni 2014-01-26 10:17:13 +01:00
parent f005009f4b
commit d43bd8e928
3 changed files with 45 additions and 32 deletions

View file

@ -45,32 +45,41 @@ int handle_create(struct snapraid_handle* handle, struct snapraid_file* file, in
handle->truncated = 0;
handle->created = 0;
/* open for read write */
/* O_SEQUENTIAL: opening in sequential mode in Windows */
/* flags for opening */
/* O_BINARY: open as binary file (Windows only) */
/* O_NOFOLLOW: do not follow links to ensure to open the real file */
flags = O_RDWR | O_BINARY | O_NOFOLLOW;
/* O_SEQUENTIAL: improve performance for sequential access (Windows only) */
flags = O_BINARY | O_NOFOLLOW;
if (!skip_sequential)
flags |= O_SEQUENTIAL;
handle->f = open(handle->path, flags);
/* if failed for missing permission */
if (handle->f == -1 && errno == EACCES) {
/* retry without requesting write permission */
flags = O_RDONLY | O_BINARY | O_NOFOLLOW;
if (!skip_sequential)
flags |= O_SEQUENTIAL;
handle->f = open(handle->path, flags);
/* open for read write */
handle->f = open(handle->path, flags | O_RDWR);
/* if failed for missing write permission */
if (handle->f == -1 && (errno == EACCES || errno == EROFS)) {
/* open for real-only */
handle->f = open(handle->path, flags | O_RDONLY);
}
/* if failed for missing file */
if (handle->f == -1 && errno == ENOENT) {
/* retry creating it */
flags = O_RDWR | O_CREAT | O_BINARY | O_NOFOLLOW;
if (!skip_sequential)
flags |= O_SEQUENTIAL;
/* mark it as created */
handle->created = 1;
handle->f = open(handle->path, flags, 0600);
char path_from[PATH_MAX];
/* check if exists a .unrecoverable copy, and rename to the real one */
pathprint(path_from , sizeof(path_from), "%s.unrecoverable", handle->path);
if (rename(path_from, handle->path) == 0) {
/* open for read write */
handle->f = open(handle->path, flags | O_RDWR);
} else {
/* create it */
handle->f = open(handle->path, flags | O_RDWR | O_CREAT, 0600);
if (handle->f != -1) {
/* mark it as created if really done */
handle->created = 1;
}
}
}
if (handle->f == -1) {
@ -147,14 +156,16 @@ int handle_open(struct snapraid_handle* handle, struct snapraid_file* file, int
handle->truncated = 0;
handle->created = 0;
/* open for read */
/* O_SEQUENTIAL: opening in sequential mode in Windows */
/* flags for opening */
/* O_BINARY: open as binary file (Windows only) */
/* O_NOFOLLOW: do not follow links to ensure to open the real file */
/* O_NOATIME: do not change access time */
flags = O_RDONLY | O_BINARY | O_NOFOLLOW;
/* O_SEQUENTIAL: improve performance for sequential access (Windows only) */
flags = O_BINARY | O_NOFOLLOW;
if (!skip_sequential)
flags |= O_SEQUENTIAL;
handle->f = open_noatime(handle->path, flags);
/* open for read */
handle->f = open_noatime(handle->path, flags | O_RDONLY);
if (handle->f == -1) {
/* invalidate for error */
handle->file = 0;