@@ -21,7 +21,7 @@ use core::prelude::*;
21
21
22
22
use fmt;
23
23
use ffi:: OsString ;
24
- use io:: { self , Error , ErrorKind , SeekFrom , Seek , Read , Write } ;
24
+ use io:: { self , SeekFrom , Seek , Read , Write } ;
25
25
use path:: { Path , PathBuf } ;
26
26
use sys:: fs as fs_imp;
27
27
use sys_common:: { AsInnerMut , FromInner , AsInner } ;
@@ -858,20 +858,7 @@ pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()>
858
858
/// ```
859
859
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
860
860
pub fn copy < P : AsRef < Path > , Q : AsRef < Path > > ( from : P , to : Q ) -> io:: Result < u64 > {
861
- let from = from. as_ref ( ) ;
862
- let to = to. as_ref ( ) ;
863
- if !from. is_file ( ) {
864
- return Err ( Error :: new ( ErrorKind :: InvalidInput ,
865
- "the source path is not an existing file" ) )
866
- }
867
-
868
- let mut reader = try!( File :: open ( from) ) ;
869
- let mut writer = try!( File :: create ( to) ) ;
870
- let perm = try!( reader. metadata ( ) ) . permissions ( ) ;
871
-
872
- let ret = try!( io:: copy ( & mut reader, & mut writer) ) ;
873
- try!( set_permissions ( to, perm) ) ;
874
- Ok ( ret)
861
+ fs_imp:: copy ( from. as_ref ( ) , to. as_ref ( ) )
875
862
}
876
863
877
864
/// Creates a new hard link on the filesystem.
@@ -1745,6 +1732,19 @@ mod tests {
1745
1732
}
1746
1733
}
1747
1734
1735
+ #[ test]
1736
+ fn copy_src_does_not_exist ( ) {
1737
+ let tmpdir = tmpdir ( ) ;
1738
+ let from = Path2 :: new ( "test/nonexistent-bogus-path" ) ;
1739
+ let to = tmpdir. join ( "out.txt" ) ;
1740
+ check ! ( check!( File :: create( & to) ) . write( b"hello" ) ) ;
1741
+ assert ! ( fs:: copy( & from, & to) . is_err( ) ) ;
1742
+ assert ! ( !from. exists( ) ) ;
1743
+ let mut v = Vec :: new ( ) ;
1744
+ check ! ( check!( File :: open( & to) ) . read_to_end( & mut v) ) ;
1745
+ assert_eq ! ( v, b"hello" ) ;
1746
+ }
1747
+
1748
1748
#[ test]
1749
1749
fn copy_file_ok ( ) {
1750
1750
let tmpdir = tmpdir ( ) ;
@@ -1814,6 +1814,18 @@ mod tests {
1814
1814
check ! ( fs:: set_permissions( & out, attr. permissions( ) ) ) ;
1815
1815
}
1816
1816
1817
+ #[ cfg( windows) ]
1818
+ #[ test]
1819
+ fn copy_file_preserves_streams ( ) {
1820
+ let tmp = tmpdir ( ) ;
1821
+ check ! ( check!( File :: create( tmp. join( "in.txt:bunny" ) ) ) . write( "carrot" . as_bytes( ) ) ) ;
1822
+ assert_eq ! ( check!( fs:: copy( tmp. join( "in.txt" ) , tmp. join( "out.txt" ) ) ) , 6 ) ;
1823
+ assert_eq ! ( check!( tmp. join( "out.txt" ) . metadata( ) ) . len( ) , 0 ) ;
1824
+ let mut v = Vec :: new ( ) ;
1825
+ check ! ( check!( File :: open( tmp. join( "out.txt:bunny" ) ) ) . read_to_end( & mut v) ) ;
1826
+ assert_eq ! ( v, b"carrot" . to_vec( ) ) ;
1827
+ }
1828
+
1817
1829
#[ cfg( not( windows) ) ] // FIXME(#10264) operation not permitted?
1818
1830
#[ test]
1819
1831
fn symlinks_work ( ) {
0 commit comments