Skip to content

Commit c7d6a01

Browse files
committed
Fix additional nits:
- compute bytes_to_copy more elegantly - add assert that written is 0 in fallback case
1 parent 3b271eb commit c7d6a01

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

src/libstd/sys/unix/fs.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,7 @@ pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
780780

781781
#[cfg(any(target_os = "linux", target_os = "android"))]
782782
pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
783+
use cmp;
783784
use fs::{File, set_permissions};
784785
use sync::atomic::{AtomicBool, Ordering};
785786

@@ -822,13 +823,7 @@ pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
822823
let mut written = 0u64;
823824
while written < len {
824825
let copy_result = if has_copy_file_range {
825-
// FIXME: should ideally use TryFrom
826-
let bytes_to_copy = if len - written > usize::max_value() as u64 {
827-
usize::max_value()
828-
} else {
829-
(len - written) as usize
830-
};
831-
826+
let bytes_to_copy = cmp::min(len - written, usize::max_value() as u64) as usize;
832827
let copy_result = unsafe {
833828
// We actually don't have to adjust the offsets,
834829
// because copy_file_range adjusts the file offset automatically
@@ -856,6 +851,7 @@ pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
856851
Some(os_err) if os_err == libc::ENOSYS || os_err == libc::EXDEV => {
857852
// Either kernel is too old or the files are not mounted on the same fs.
858853
// Try again with fallback method
854+
assert_eq!(written, 0);
859855
let ret = io::copy(&mut reader, &mut writer)?;
860856
set_permissions(to, perm)?;
861857
return Ok(ret)

0 commit comments

Comments
 (0)