Repository navigation
File cloning improvements #5168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tuffnatty
wants to merge
7
commits into
MidnightCommander:master
Choose a base branch
from
tuffnatty:linux-copy-file-range
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+910
−207
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b9dbfd8
(fix) copy_file_file: don't preallocate space in append and reget modes
zyv 7303d00
(fix) file cloning: Require COPY_FILE_RANGE_CLONE support on FreeBSD …
tuffnatty 9365e47
Add support for cross-filesystem file cloning on Linux 5.19+
tuffnatty 6e793fe
ux: Report progress during slow file cloning, e.g. NFS < 4.2
tuffnatty 43c00c5
tests: add regression tests for mc_copy_file_range_*()
tuffnatty 20e17aa
tests: add copy_file_file() regression tests
zyv cacf21c
Enabled file cloning for temporary files created during VFS copy
tuffnatty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -49,8 +49,16 @@ | |||||||||||||||||||||||||||||||||
| #ifdef HAVE_FICLONERANGE | ||||||||||||||||||||||||||||||||||
| #include <linux/fs.h> // FICLONERANGE | ||||||||||||||||||||||||||||||||||
| #include <sys/ioctl.h> // ioctl() | ||||||||||||||||||||||||||||||||||
| #elif defined(HAVE_COPY_FILE_RANGE) | ||||||||||||||||||||||||||||||||||
| #include <unistd.h> // COPY_FILE_RANGE_CLONE | ||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| #if defined(HAVE_COPY_FILE_RANGE) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| #include <unistd.h> // copy_file_range(), COPY_FILE_RANGE_CLONE | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| #ifdef __linux__ | ||||||||||||||||||||||||||||||||||
| #include <sys/utsname.h> // uname() | ||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| #elif defined(HAVE_SYS_CLONEFILE_H) | ||||||||||||||||||||||||||||||||||
| #include <sys/clonefile.h> // CLONE_NOOWNERCOPY | ||||||||||||||||||||||||||||||||||
| #elif defined(HAVE_REFLINK) | ||||||||||||||||||||||||||||||||||
|
|
@@ -210,6 +218,31 @@ vfs_test_current_dir (const vfs_path_t *vpath) | |||||||||||||||||||||||||||||||||
| && my_stat.st_ino == my_stat2.st_ino && my_stat.st_dev == my_stat2.st_dev); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /* --------------------------------------------------------------------------------------------- */ | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| #ifdef HAVE_COPY_FILE_RANGE | ||||||||||||||||||||||||||||||||||
| /* Return true if copy_file_range(2) works well. To stay on the safe side, this is false on Linux | ||||||||||||||||||||||||||||||||||
| * kernels before 5.19 and on FreeBSD builds before 15.0 which has introduced | ||||||||||||||||||||||||||||||||||
| * COPY_FILE_RANGE_CLONE. */ | ||||||||||||||||||||||||||||||||||
| static gboolean | ||||||||||||||||||||||||||||||||||
| vfs_copy_file_range_works (void) | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| #if defined(__FreeBSD__) && defined(COPY_FILE_RANGE_CLONE) | ||||||||||||||||||||||||||||||||||
| return TRUE; | ||||||||||||||||||||||||||||||||||
| #elif defined(__linux__) | ||||||||||||||||||||||||||||||||||
| static int copy_file_range_works_cache = -1; | ||||||||||||||||||||||||||||||||||
| struct utsname name; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if (copy_file_range_works_cache < 0) | ||||||||||||||||||||||||||||||||||
| copy_file_range_works_cache = | ||||||||||||||||||||||||||||||||||
| uname (&name) == 0 && 0 <= str_verscmp (name.release, "5.19.0"); | ||||||||||||||||||||||||||||||||||
| return (gboolean) copy_file_range_works_cache; | ||||||||||||||||||||||||||||||||||
| #else | ||||||||||||||||||||||||||||||||||
| return FALSE; | ||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /* --------------------------------------------------------------------------------------------- */ | ||||||||||||||||||||||||||||||||||
| /*** public functions ****************************************************************************/ | ||||||||||||||||||||||||||||||||||
| /* --------------------------------------------------------------------------------------------- */ | ||||||||||||||||||||||||||||||||||
|
|
@@ -719,78 +752,64 @@ vfs_preallocate (int dest_vfs_fd, off_t src_fsize, off_t dest_fsize) | |||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /* --------------------------------------------------------------------------------------------- */ | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| int | ||||||||||||||||||||||||||||||||||
| vfs_clone_file (int dest_vfs_fd, int src_vfs_fd) | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| #ifdef HAVE_FILE_CLONING_BY_RANGE | ||||||||||||||||||||||||||||||||||
| void *dest_fd = NULL; | ||||||||||||||||||||||||||||||||||
| void *src_fd = NULL; | ||||||||||||||||||||||||||||||||||
| struct vfs_class *dest_class; | ||||||||||||||||||||||||||||||||||
| struct vfs_class *src_class; | ||||||||||||||||||||||||||||||||||
| off_t in_offset, out_offset; | ||||||||||||||||||||||||||||||||||
| #if defined(FICLONERANGE) | ||||||||||||||||||||||||||||||||||
| ssize_t | ||||||||||||||||||||||||||||||||||
| mc_copy_file_range_ficlonerange (int src_fd, off_t *src_offset, int dest_fd, off_t *dest_offset, | ||||||||||||||||||||||||||||||||||
| size_t len) | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| int rc; | ||||||||||||||||||||||||||||||||||
| struct file_clone_range fcr = { | ||||||||||||||||||||||||||||||||||
| .src_fd = src_fd, | ||||||||||||||||||||||||||||||||||
| .src_offset = *src_offset, | ||||||||||||||||||||||||||||||||||
| .src_length = (len == SSIZE_MAX) ? 0 : len, | ||||||||||||||||||||||||||||||||||
| .dest_offset = *dest_offset, | ||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||
| off_t n_copied; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| rc = ioctl (dest_fd, FICLONERANGE, &fcr); | ||||||||||||||||||||||||||||||||||
| if (rc == -1) | ||||||||||||||||||||||||||||||||||
| return rc; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| n_copied = (len == SSIZE_MAX) ? lseek (src_fd, 0, SEEK_END) - *src_offset : (off_t) len; | ||||||||||||||||||||||||||||||||||
| *src_offset += n_copied; | ||||||||||||||||||||||||||||||||||
| *dest_offset += n_copied; | ||||||||||||||||||||||||||||||||||
| return n_copied; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| dest_class = vfs_class_find_by_handle (dest_vfs_fd, &dest_fd); | ||||||||||||||||||||||||||||||||||
| if ((dest_class->flags & VFSF_LOCAL) == 0) | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| errno = ENOTSUP; | ||||||||||||||||||||||||||||||||||
| return (-1); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| if (dest_fd == NULL) | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| errno = EBADF; | ||||||||||||||||||||||||||||||||||
| return (-1); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| /* --------------------------------------------------------------------------------------------- */ | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| src_class = vfs_class_find_by_handle (src_vfs_fd, &src_fd); | ||||||||||||||||||||||||||||||||||
| if ((src_class->flags & VFSF_LOCAL) == 0) | ||||||||||||||||||||||||||||||||||
| #if defined(HAVE_COPY_FILE_RANGE) | ||||||||||||||||||||||||||||||||||
| ssize_t | ||||||||||||||||||||||||||||||||||
| mc_copy_file_range_native (int src_fd, off_t *src_offset, int dest_fd, off_t *dest_offset, | ||||||||||||||||||||||||||||||||||
| size_t len) | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| if (!vfs_copy_file_range_works ()) | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| errno = ENOTSUP; | ||||||||||||||||||||||||||||||||||
| return (-1); | ||||||||||||||||||||||||||||||||||
| return -1; | ||||||||||||||||||||||||||||||||||
|
tuffnatty marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| if (src_fd == NULL) | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| errno = EBADF; | ||||||||||||||||||||||||||||||||||
| return (-1); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| in_offset = mc_lseek (src_vfs_fd, 0, SEEK_CUR); | ||||||||||||||||||||||||||||||||||
| if (in_offset < 0) | ||||||||||||||||||||||||||||||||||
| return (-1); | ||||||||||||||||||||||||||||||||||
| out_offset = mc_lseek (dest_vfs_fd, 0, SEEK_CUR); | ||||||||||||||||||||||||||||||||||
| if (out_offset < 0) | ||||||||||||||||||||||||||||||||||
| return (-1); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| #if defined(FICLONERANGE) | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| struct file_clone_range fcr = { | ||||||||||||||||||||||||||||||||||
| .src_fd = *(int *) src_fd, | ||||||||||||||||||||||||||||||||||
| .src_offset = in_offset, | ||||||||||||||||||||||||||||||||||
| .src_length = 0, | ||||||||||||||||||||||||||||||||||
| .dest_offset = out_offset, | ||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return ioctl (*(int *) dest_fd, FICLONERANGE, &fcr); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| #elif defined(COPY_FILE_RANGE_CLONE) | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| ssize_t result; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| do | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| result = copy_file_range (*(int *) src_fd, &in_offset, *(int *) dest_fd, &out_offset, | ||||||||||||||||||||||||||||||||||
| SSIZE_MAX, COPY_FILE_RANGE_CLONE); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| while (result > 0); | ||||||||||||||||||||||||||||||||||
| return result; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| #if defined(COPY_FILE_RANGE_CLONE) | ||||||||||||||||||||||||||||||||||
| return copy_file_range (src_fd, src_offset, dest_fd, dest_offset, len, COPY_FILE_RANGE_CLONE); | ||||||||||||||||||||||||||||||||||
| #else | ||||||||||||||||||||||||||||||||||
| (void) dest_vfs_fd; | ||||||||||||||||||||||||||||||||||
| (void) src_vfs_fd; | ||||||||||||||||||||||||||||||||||
| errno = ENOTSUP; | ||||||||||||||||||||||||||||||||||
| return (-1); | ||||||||||||||||||||||||||||||||||
| return copy_file_range (src_fd, src_offset, dest_fd, dest_offset, len, 0); | ||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /* --------------------------------------------------------------------------------------------- */ | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| gboolean | ||||||||||||||||||||||||||||||||||
| vfs_cloning_supported (const vfs_path_t *src_vpath, const vfs_path_t *dst_vpath) | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| if (!vfs_file_is_local (src_vpath)) | ||||||||||||||||||||||||||||||||||
| return FALSE; | ||||||||||||||||||||||||||||||||||
| if (vfs_file_is_local (dst_vpath)) | ||||||||||||||||||||||||||||||||||
| return TRUE; | ||||||||||||||||||||||||||||||||||
| if ((vfs_file_class_flags (dst_vpath) & VFSF_USETMP) != 0) | ||||||||||||||||||||||||||||||||||
| return TRUE; | ||||||||||||||||||||||||||||||||||
| return FALSE; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+803
to
+813
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Already added to my corrections batch, see unit test. Only localfs stores an
Suggested change
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /* --------------------------------------------------------------------------------------------- */ | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.