Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve fs error open_from unix #83522

Merged
merged 1 commit into from
Mar 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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