Auto merge of #26751 - retep998:copy-that-floppy, r=alexcrichton
Using the OS mechanism for copying files allows the OS to optimize the transfer using stuff such as [Offloaded Data Transfers (ODX)](https://msdn.microsoft.com/en-us/library/windows/desktop/hh848056%28v=vs.85%29.aspx). Also preserves a lot more information, including NTFS [File Streams](https://msdn.microsoft.com/en-us/library/windows/desktop/aa364404%28v=vs.85%29.aspx), which the manual implementation threw away. In addition, it is an atomic operation, unlike the manual implementation which has extra calls for copying over permissions. r? @alexcrichton
This commit is contained in:
commit
d0d37075a5
4 changed files with 93 additions and 16 deletions
|
@ -14,7 +14,7 @@ use os::unix::prelude::*;
|
|||
|
||||
use ffi::{CString, CStr, OsString, OsStr};
|
||||
use fmt;
|
||||
use io::{self, Error, SeekFrom};
|
||||
use io::{self, Error, ErrorKind, SeekFrom};
|
||||
use libc::{self, c_int, size_t, off_t, c_char, mode_t};
|
||||
use mem;
|
||||
use path::{Path, PathBuf};
|
||||
|
@ -516,3 +516,19 @@ pub fn canonicalize(p: &Path) -> io::Result<PathBuf> {
|
|||
buf.truncate(p);
|
||||
Ok(PathBuf::from(OsString::from_vec(buf)))
|
||||
}
|
||||
|
||||
pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
|
||||
use fs::{File, PathExt, set_permissions};
|
||||
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)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue