@@ -780,6 +780,7 @@ pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
780
780
781
781
#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
782
782
pub fn copy ( from : & Path , to : & Path ) -> io:: Result < u64 > {
783
+ use cmp;
783
784
use fs:: { File , set_permissions} ;
784
785
use sync:: atomic:: { AtomicBool , Ordering } ;
785
786
@@ -822,13 +823,7 @@ pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
822
823
let mut written = 0u64 ;
823
824
while written < len {
824
825
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 ;
832
827
let copy_result = unsafe {
833
828
// We actually don't have to adjust the offsets,
834
829
// because copy_file_range adjusts the file offset automatically
@@ -856,6 +851,7 @@ pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
856
851
Some ( os_err) if os_err == libc:: ENOSYS || os_err == libc:: EXDEV => {
857
852
// Either kernel is too old or the files are not mounted on the same fs.
858
853
// Try again with fallback method
854
+ assert_eq ! ( written, 0 ) ;
859
855
let ret = io:: copy ( & mut reader, & mut writer) ?;
860
856
set_permissions ( to, perm) ?;
861
857
return Ok ( ret)
0 commit comments