diff options
| author | Ivan Tham <pickfire@riseup.net> | 2021-03-27 01:21:35 +0800 |
|---|---|---|
| committer | Ivan Tham <pickfire@riseup.net> | 2021-03-27 21:23:48 +0800 |
| commit | 6c6ef7317bf87321c5eaefb1322380c8f297ff1b (patch) | |
| tree | 4f2ebcb1f9bbe90429aa9b09974ddf96c3c530bf | |
| parent | b8719c51e0e44483cff9b6975a830f6e51812a48 (diff) | |
| download | rust-6c6ef7317bf87321c5eaefb1322380c8f297ff1b.tar.gz rust-6c6ef7317bf87321c5eaefb1322380c8f297ff1b.zip | |
Improve fs error open_from unix
Consistency for #79399 Suggested by JohnTitor Improve fs error invaild input for sys_common The text was duplicated from unix.
| -rw-r--r-- | library/std/src/sys/unix/fs.rs | 8 | ||||
| -rw-r--r-- | library/std/src/sys_common/fs.rs | 10 |
2 files changed, 9 insertions, 9 deletions
diff --git a/library/std/src/sys/unix/fs.rs b/library/std/src/sys/unix/fs.rs index f25a6280cd0..759565bab73 100644 --- a/library/std/src/sys/unix/fs.rs +++ b/library/std/src/sys/unix/fs.rs @@ -2,7 +2,7 @@ use crate::os::unix::prelude::*; use crate::ffi::{CStr, CString, OsStr, OsString}; use crate::fmt; -use crate::io::{self, Error, ErrorKind, IoSlice, IoSliceMut, SeekFrom}; +use crate::io::{self, Error, IoSlice, IoSliceMut, SeekFrom}; use crate::mem; use crate::path::{Path, PathBuf}; use crate::ptr; @@ -1152,14 +1152,12 @@ pub fn canonicalize(p: &Path) -> io::Result<PathBuf> { fn open_from(from: &Path) -> io::Result<(crate::fs::File, crate::fs::Metadata)> { use crate::fs::File; + use crate::sys_common::fs::NOT_FILE_ERROR; let reader = File::open(from)?; let metadata = reader.metadata()?; if !metadata.is_file() { - return Err(Error::new_const( - ErrorKind::InvalidInput, - &"the source path is not an existing regular file", - )); + return Err(NOT_FILE_ERROR); } Ok((reader, metadata)) } diff --git a/library/std/src/sys_common/fs.rs b/library/std/src/sys_common/fs.rs index 92d0cc60850..30908824dd6 100644 --- a/library/std/src/sys_common/fs.rs +++ b/library/std/src/sys_common/fs.rs @@ -4,15 +4,17 @@ use crate::fs; use crate::io::{self, Error, ErrorKind}; use crate::path::Path; +pub(crate) const NOT_FILE_ERROR: Error = Error::new_const( + ErrorKind::InvalidInput, + &"the source path is neither a regular file nor a symlink to a regular file", +); + pub fn copy(from: &Path, to: &Path) -> io::Result<u64> { let mut reader = fs::File::open(from)?; let metadata = reader.metadata()?; if !metadata.is_file() { - return Err(Error::new_const( - ErrorKind::InvalidInput, - &"the source path is not an existing regular file", - )); + return Err(NOT_FILE_ERROR); } let mut writer = fs::File::create(to)?; |
