From 6c6ef7317bf87321c5eaefb1322380c8f297ff1b Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Sat, 27 Mar 2021 01:21:35 +0800 Subject: [PATCH] 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. --- library/std/src/sys/unix/fs.rs | 8 +++----- 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 f25a6280cd076..759565bab73f3 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 { 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 92d0cc60850c6..30908824dd663 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 { 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)?;