|
| 1 | +use nix; |
| 2 | +use std::fs::File; |
| 3 | +use std::io; |
| 4 | + |
| 5 | +/// Helper functions for std::fs::File |
| 6 | +pub trait FileExt { |
| 7 | + /// Copy the entire contents of `self` to `to`. This uses operating system |
| 8 | + /// specific fast paths if available. |
| 9 | + fn copy_to(&mut self, to: &mut File) -> io::Result<u64>; |
| 10 | +} |
| 11 | + |
| 12 | +impl FileExt for File { |
| 13 | + #[cfg(not(any(target_os = "linux", target_os = "android")))] |
| 14 | + fn copy_to(&mut self, to: &mut File) -> io::Result<u64> { |
| 15 | + return io::copy(self, &mut to); |
| 16 | + } |
| 17 | + |
| 18 | + // Derived from src/libstd/sys/unix/fs.rs in Rust |
| 19 | + #[cfg(any(target_os = "linux", target_os = "android"))] |
| 20 | + fn copy_to(&mut self, to: &mut File) -> io::Result<u64> { |
| 21 | + use nix::fcntl::copy_file_range; |
| 22 | + use nix::errno::Errno; |
| 23 | + use std::os::unix::io::AsRawFd; |
| 24 | + use std::sync::atomic::{AtomicBool, Ordering}; |
| 25 | + |
| 26 | + // Kernel prior to 4.5 don't have copy_file_range |
| 27 | + // We store the availability in a global to avoid unnecessary syscalls |
| 28 | + static HAS_COPY_FILE_RANGE: AtomicBool = AtomicBool::new(true); |
| 29 | + |
| 30 | + let len = self.metadata()?.len(); |
| 31 | + |
| 32 | + let has_copy_file_range = HAS_COPY_FILE_RANGE.load(Ordering::Relaxed); |
| 33 | + let mut written = 0u64; |
| 34 | + while written < len { |
| 35 | + let copy_result = if has_copy_file_range { |
| 36 | + let bytes_to_copy = std::cmp::min(len - written, usize::MAX as u64) as usize; |
| 37 | + // We actually don't have to adjust the offsets, |
| 38 | + // because copy_file_range adjusts the file offset automatically |
| 39 | + let copy_result = |
| 40 | + copy_file_range(self.as_raw_fd(), None, to.as_raw_fd(), None, bytes_to_copy); |
| 41 | + if let Err(ref copy_err) = copy_result { |
| 42 | + match copy_err.as_errno() { |
| 43 | + Some(Errno::ENOSYS) | Some(Errno::EPERM) => { |
| 44 | + HAS_COPY_FILE_RANGE.store(false, Ordering::Relaxed); |
| 45 | + } |
| 46 | + _ => {} |
| 47 | + } |
| 48 | + } |
| 49 | + copy_result |
| 50 | + } else { |
| 51 | + Err(nix::Error::from_errno(Errno::ENOSYS)) |
| 52 | + }; |
| 53 | + match copy_result { |
| 54 | + Ok(ret) => written += ret as u64, |
| 55 | + Err(err) => { |
| 56 | + match err.as_errno() { |
| 57 | + Some(os_err) |
| 58 | + if os_err == Errno::ENOSYS |
| 59 | + || os_err == Errno::EXDEV |
| 60 | + || os_err == Errno::EINVAL |
| 61 | + || os_err == Errno::EPERM => |
| 62 | + { |
| 63 | + // Try fallback io::copy if either: |
| 64 | + // - Kernel version is < 4.5 (ENOSYS) |
| 65 | + // - Files are mounted on different fs (EXDEV) |
| 66 | + // - copy_file_range is disallowed, for example by seccomp (EPERM) |
| 67 | + // - copy_file_range cannot be used with pipes or device nodes (EINVAL) |
| 68 | + assert_eq!(written, 0); |
| 69 | + return io::copy(self, to); |
| 70 | + } |
| 71 | + Some(os_err) => return Err(io::Error::from_raw_os_error(os_err as i32)), |
| 72 | + _ => return Err(io::Error::new(io::ErrorKind::Other, err)) |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + Ok(written) |
| 78 | + } |
| 79 | +} |
0 commit comments