From c7d6a0130b6b76b65982916198e7de2b348f9718 Mon Sep 17 00:00:00 2001 From: Nicolas Koch Date: Tue, 29 May 2018 23:42:42 +0200 Subject: [PATCH] Fix additional nits: - compute bytes_to_copy more elegantly - add assert that written is 0 in fallback case --- src/libstd/sys/unix/fs.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/libstd/sys/unix/fs.rs b/src/libstd/sys/unix/fs.rs index 56075c5e8d0..38f1ac472fa 100644 --- a/src/libstd/sys/unix/fs.rs +++ b/src/libstd/sys/unix/fs.rs @@ -780,6 +780,7 @@ pub fn copy(from: &Path, to: &Path) -> io::Result { #[cfg(any(target_os = "linux", target_os = "android"))] pub fn copy(from: &Path, to: &Path) -> io::Result { + use cmp; use fs::{File, set_permissions}; use sync::atomic::{AtomicBool, Ordering}; @@ -822,13 +823,7 @@ unsafe fn copy_file_range( let mut written = 0u64; while written < len { let copy_result = if has_copy_file_range { - // FIXME: should ideally use TryFrom - let bytes_to_copy = if len - written > usize::max_value() as u64 { - usize::max_value() - } else { - (len - written) as usize - }; - + let bytes_to_copy = cmp::min(len - written, usize::max_value() as u64) as usize; let copy_result = unsafe { // We actually don't have to adjust the offsets, // because copy_file_range adjusts the file offset automatically @@ -856,6 +851,7 @@ unsafe fn copy_file_range( Some(os_err) if os_err == libc::ENOSYS || os_err == libc::EXDEV => { // Either kernel is too old or the files are not mounted on the same fs. // Try again with fallback method + assert_eq!(written, 0); let ret = io::copy(&mut reader, &mut writer)?; set_permissions(to, perm)?; return Ok(ret)