@@ -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 } ;
@@ -862,20 +862,7 @@ pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()>
862
862
/// ```
863
863
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
864
864
pub fn copy < P : AsRef < Path > , Q : AsRef < Path > > ( from : P , to : Q ) -> io:: Result < u64 > {
865
- let from = from. as_ref ( ) ;
866
- let to = to. as_ref ( ) ;
867
- if !from. is_file ( ) {
868
- return Err ( Error :: new ( ErrorKind :: InvalidInput ,
869
- "the source path is not an existing file" ) )
870
- }
871
-
872
- let mut reader = try!( File :: open ( from) ) ;
873
- let mut writer = try!( File :: create ( to) ) ;
874
- let perm = try!( reader. metadata ( ) ) . permissions ( ) ;
875
-
876
- let ret = try!( io:: copy ( & mut reader, & mut writer) ) ;
877
- try!( set_permissions ( to, perm) ) ;
878
- Ok ( ret)
865
+ fs_imp:: copy ( from. as_ref ( ) , to. as_ref ( ) )
879
866
}
880
867
881
868
/// Creates a new hard link on the filesystem.
@@ -1749,6 +1736,19 @@ mod tests {
1749
1736
}
1750
1737
}
1751
1738
1739
+ #[ test]
1740
+ fn copy_src_does_not_exist ( ) {
1741
+ let tmpdir = tmpdir ( ) ;
1742
+ let from = Path2 :: new ( "test/nonexistent-bogus-path" ) ;
1743
+ let to = tmpdir. join ( "out.txt" ) ;
1744
+ check ! ( check!( File :: create( & to) ) . write( b"hello" ) ) ;
1745
+ assert ! ( fs:: copy( & from, & to) . is_err( ) ) ;
1746
+ assert ! ( !from. exists( ) ) ;
1747
+ let mut v = Vec :: new ( ) ;
1748
+ check ! ( check!( File :: open( & to) ) . read_to_end( & mut v) ) ;
1749
+ assert_eq ! ( v, b"hello" ) ;
1750
+ }
1751
+
1752
1752
#[ test]
1753
1753
fn copy_file_ok ( ) {
1754
1754
let tmpdir = tmpdir ( ) ;
@@ -1818,6 +1818,18 @@ mod tests {
1818
1818
check ! ( fs:: set_permissions( & out, attr. permissions( ) ) ) ;
1819
1819
}
1820
1820
1821
+ #[ cfg( windows) ]
1822
+ #[ test]
1823
+ fn copy_file_preserves_streams ( ) {
1824
+ let tmp = tmpdir ( ) ;
1825
+ check ! ( check!( File :: create( tmp. join( "in.txt:bunny" ) ) ) . write( "carrot" . as_bytes( ) ) ) ;
1826
+ assert_eq ! ( check!( fs:: copy( tmp. join( "in.txt" ) , tmp. join( "out.txt" ) ) ) , 6 ) ;
1827
+ assert_eq ! ( check!( tmp. join( "out.txt" ) . metadata( ) ) . len( ) , 0 ) ;
1828
+ let mut v = Vec :: new ( ) ;
1829
+ check ! ( check!( File :: open( tmp. join( "out.txt:bunny" ) ) ) . read_to_end( & mut v) ) ;
1830
+ assert_eq ! ( v, b"carrot" . to_vec( ) ) ;
1831
+ }
1832
+
1821
1833
#[ cfg( not( windows) ) ] // FIXME(#10264) operation not permitted?
1822
1834
#[ test]
1823
1835
fn symlinks_work ( ) {
0 commit comments