Skip to content

Commit 6c6ef73

Browse files
committed
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.
1 parent b8719c5 commit 6c6ef73

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

library/std/src/sys/unix/fs.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::os::unix::prelude::*;
22

33
use crate::ffi::{CStr, CString, OsStr, OsString};
44
use crate::fmt;
5-
use crate::io::{self, Error, ErrorKind, IoSlice, IoSliceMut, SeekFrom};
5+
use crate::io::{self, Error, IoSlice, IoSliceMut, SeekFrom};
66
use crate::mem;
77
use crate::path::{Path, PathBuf};
88
use crate::ptr;
@@ -1152,14 +1152,12 @@ pub fn canonicalize(p: &Path) -> io::Result<PathBuf> {
11521152

11531153
fn open_from(from: &Path) -> io::Result<(crate::fs::File, crate::fs::Metadata)> {
11541154
use crate::fs::File;
1155+
use crate::sys_common::fs::NOT_FILE_ERROR;
11551156

11561157
let reader = File::open(from)?;
11571158
let metadata = reader.metadata()?;
11581159
if !metadata.is_file() {
1159-
return Err(Error::new_const(
1160-
ErrorKind::InvalidInput,
1161-
&"the source path is not an existing regular file",
1162-
));
1160+
return Err(NOT_FILE_ERROR);
11631161
}
11641162
Ok((reader, metadata))
11651163
}

library/std/src/sys_common/fs.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ use crate::fs;
44
use crate::io::{self, Error, ErrorKind};
55
use crate::path::Path;
66

7+
pub(crate) const NOT_FILE_ERROR: Error = Error::new_const(
8+
ErrorKind::InvalidInput,
9+
&"the source path is neither a regular file nor a symlink to a regular file",
10+
);
11+
712
pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
813
let mut reader = fs::File::open(from)?;
914
let metadata = reader.metadata()?;
1015

1116
if !metadata.is_file() {
12-
return Err(Error::new_const(
13-
ErrorKind::InvalidInput,
14-
&"the source path is not an existing regular file",
15-
));
17+
return Err(NOT_FILE_ERROR);
1618
}
1719

1820
let mut writer = fs::File::create(to)?;

0 commit comments

Comments
 (0)