1
Fork 0

Use CopyFileEx for fs::copy on Windows

Adds a couple more tests for fs::copy

Signed-off-by: Peter Atashian <retep998@gmail.com>
This commit is contained in:
Peter Atashian 2015-07-10 04:54:00 -04:00
parent e6a9be10bc
commit 1d202692ec
4 changed files with 93 additions and 16 deletions

View file

@ -21,7 +21,7 @@ use core::prelude::*;
use fmt;
use ffi::OsString;
use io::{self, Error, ErrorKind, SeekFrom, Seek, Read, Write};
use io::{self, SeekFrom, Seek, Read, Write};
use path::{Path, PathBuf};
use sys::fs as fs_imp;
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<()>
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> {
let from = from.as_ref();
let to = to.as_ref();
if !from.is_file() {
return Err(Error::new(ErrorKind::InvalidInput,
"the source path is not an existing file"))
}
let mut reader = try!(File::open(from));
let mut writer = try!(File::create(to));
let perm = try!(reader.metadata()).permissions();
let ret = try!(io::copy(&mut reader, &mut writer));
try!(set_permissions(to, perm));
Ok(ret)
fs_imp::copy(from.as_ref(), to.as_ref())
}
/// Creates a new hard link on the filesystem.
@ -1745,6 +1732,19 @@ mod tests {
}
}
#[test]
fn copy_src_does_not_exist() {
let tmpdir = tmpdir();
let from = Path2::new("test/nonexistent-bogus-path");
let to = tmpdir.join("out.txt");
check!(check!(File::create(&to)).write(b"hello"));
assert!(fs::copy(&from, &to).is_err());
assert!(!from.exists());
let mut v = Vec::new();
check!(check!(File::open(&to)).read_to_end(&mut v));
assert_eq!(v, b"hello");
}
#[test]
fn copy_file_ok() {
let tmpdir = tmpdir();
@ -1814,6 +1814,18 @@ mod tests {
check!(fs::set_permissions(&out, attr.permissions()));
}
#[cfg(windows)]
#[test]
fn copy_file_preserves_streams() {
let tmp = tmpdir();
check!(check!(File::create(tmp.join("in.txt:bunny"))).write("carrot".as_bytes()));
assert_eq!(check!(fs::copy(tmp.join("in.txt"), tmp.join("out.txt"))), 6);
assert_eq!(check!(tmp.join("out.txt").metadata()).len(), 0);
let mut v = Vec::new();
check!(check!(File::open(tmp.join("out.txt:bunny"))).read_to_end(&mut v));
assert_eq!(v, b"carrot".to_vec());
}
#[cfg(not(windows))] // FIXME(#10264) operation not permitted?
#[test]
fn symlinks_work() {