diff options
author | Nicolas Koch <nioko1337@gmail.com> | 2018-05-29 23:42:42 +0200 |
---|---|---|
committer | Nicolas Koch <nioko1337@gmail.com> | 2018-05-29 23:42:42 +0200 |
commit | c7d6a0130b6b76b65982916198e7de2b348f9718 (patch) | |
tree | 807d3b51d50bd8f99e65bc768bb416a5ec16fd92 | |
parent | Use FIXME instead of TODO; Move bytes_to_copy calculation inside if (diff) | |
download | grust-c7d6a0130b6b76b65982916198e7de2b348f9718.tar.gz grust-c7d6a0130b6b76b65982916198e7de2b348f9718.tar.bz2 grust-c7d6a0130b6b76b65982916198e7de2b348f9718.tar.xz |
Fix additional nits:
- compute bytes_to_copy more elegantly
- add assert that written is 0 in fallback case
-rw-r--r-- | src/libstd/sys/unix/fs.rs | 10 |
1 files changed, 3 insertions, 7 deletions
diff --git a/src/libstd/sys/unix/fs.rs b/src/libstd/sys/unix/fs.rs index 56075c5e8d..38f1ac472f 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<u64> { | |||
780 | 780 | ||
781 | #[cfg(any(target_os = "linux", target_os = "android"))] | 781 | #[cfg(any(target_os = "linux", target_os = "android"))] |
782 | pub fn copy(from: &Path, to: &Path) -> io::Result<u64> { | 782 | pub fn copy(from: &Path, to: &Path) -> io::Result<u64> { |
783 | use cmp; | ||
783 | use fs::{File, set_permissions}; | 784 | use fs::{File, set_permissions}; |
784 | use sync::atomic::{AtomicBool, Ordering}; | 785 | use sync::atomic::{AtomicBool, Ordering}; |
785 | 786 | ||
@@ -822,13 +823,7 @@ pub fn copy(from: &Path, to: &Path) -> io::Result<u64> { | |||
822 | let mut written = 0u64; | 823 | let mut written = 0u64; |
823 | while written < len { | 824 | while written < len { |
824 | let copy_result = if has_copy_file_range { | 825 | let copy_result = if has_copy_file_range { |
825 | // FIXME: should ideally use TryFrom | 826 | let bytes_to_copy = cmp::min(len - written, usize::max_value() as u64) as usize; |
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 | |||
832 | let copy_result = unsafe { | 827 | let copy_result = unsafe { |
833 | // We actually don't have to adjust the offsets, | 828 | // We actually don't have to adjust the offsets, |
834 | // because copy_file_range adjusts the file offset automatically | 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 | Some(os_err) if os_err == libc::ENOSYS || os_err == libc::EXDEV => { | 851 | Some(os_err) if os_err == libc::ENOSYS || os_err == libc::EXDEV => { |
857 | // Either kernel is too old or the files are not mounted on the same fs. | 852 | // Either kernel is too old or the files are not mounted on the same fs. |
858 | // Try again with fallback method | 853 | // Try again with fallback method |
854 | assert_eq!(written, 0); | ||
859 | let ret = io::copy(&mut reader, &mut writer)?; | 855 | let ret = io::copy(&mut reader, &mut writer)?; |
860 | set_permissions(to, perm)?; | 856 | set_permissions(to, perm)?; |
861 | return Ok(ret) | 857 | return Ok(ret) |