1
Fork 0

Rollup merge of #83522 - pickfire:patch-6, r=JohnTitor

Improve fs error open_from unix

Consistency for #79399
Suggested by JohnTitor

r? `@JohnTitor`

Not user if the error is too long now, do we handle long errors well?
This commit is contained in:
Dylan DPC 2021-03-27 20:37:11 +01:00 committed by GitHub
commit aee7b9e7d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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)?;