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.
This commit is contained in:
Ivan Tham 2021-03-27 01:21:35 +08:00
parent b8719c51e0
commit 6c6ef7317b
2 changed files with 9 additions and 9 deletions

View file

@ -2,7 +2,7 @@ use crate::os::unix::prelude::*;
use crate::ffi::{CStr, CString, OsStr, OsString}; use crate::ffi::{CStr, CString, OsStr, OsString};
use crate::fmt; 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::mem;
use crate::path::{Path, PathBuf}; use crate::path::{Path, PathBuf};
use crate::ptr; 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)> { fn open_from(from: &Path) -> io::Result<(crate::fs::File, crate::fs::Metadata)> {
use crate::fs::File; use crate::fs::File;
use crate::sys_common::fs::NOT_FILE_ERROR;
let reader = File::open(from)?; let reader = File::open(from)?;
let metadata = reader.metadata()?; let metadata = reader.metadata()?;
if !metadata.is_file() { if !metadata.is_file() {
return Err(Error::new_const( return Err(NOT_FILE_ERROR);
ErrorKind::InvalidInput,
&"the source path is not an existing regular file",
));
} }
Ok((reader, metadata)) Ok((reader, metadata))
} }

View file

@ -4,15 +4,17 @@ use crate::fs;
use crate::io::{self, Error, ErrorKind}; use crate::io::{self, Error, ErrorKind};
use crate::path::Path; 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> { pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
let mut reader = fs::File::open(from)?; let mut reader = fs::File::open(from)?;
let metadata = reader.metadata()?; let metadata = reader.metadata()?;
if !metadata.is_file() { if !metadata.is_file() {
return Err(Error::new_const( return Err(NOT_FILE_ERROR);
ErrorKind::InvalidInput,
&"the source path is not an existing regular file",
));
} }
let mut writer = fs::File::create(to)?; let mut writer = fs::File::create(to)?;