Skip to content

Commit

Permalink
Improve fs error open_from unix
Browse files Browse the repository at this point in the history
Consistency for #79399
Suggested by JohnTitor

Improve fs error invaild input for sys_common

The text was duplicated from unix.
  • Loading branch information
pickfire committed Mar 27, 2021
1 parent b8719c5 commit 6c6ef73
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
8 changes: 3 additions & 5 deletions library/std/src/sys/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
}
Expand Down
10 changes: 6 additions & 4 deletions library/std/src/sys_common/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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)?;
Expand Down

0 comments on commit 6c6ef73

Please sign in to comment.