未验证 提交 aee7b9e7 编写于 作者: D Dylan DPC 提交者: GitHub

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?
......@@ -2,7 +2,7 @@
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))
}
......
......@@ -4,15 +4,17 @@
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)?;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册