Skip to content

Deprecate std::path::Path's aliases of std::fs functions. #80741

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

Closed
4 changes: 2 additions & 2 deletions library/std/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2179,7 +2179,7 @@ impl DirBuilder {
match self.inner.mkdir(path) {
Ok(()) => return Ok(()),
Err(ref e) if e.kind() == io::ErrorKind::NotFound => {}
Err(_) if path.is_dir() => return Ok(()),
Err(_) if metadata(path).map(|m| m.is_dir()).unwrap_or(false) => return Ok(()),
Err(e) => return Err(e),
}
match path.parent() {
Expand All @@ -2190,7 +2190,7 @@ impl DirBuilder {
}
match self.inner.mkdir(path) {
Ok(()) => Ok(()),
Err(_) if path.is_dir() => Ok(()),
Err(_) if metadata(path).map(|m| m.is_dir()).unwrap_or(false) => Ok(()),
Err(e) => Err(e),
}
}
Expand Down
44 changes: 44 additions & 0 deletions library/std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2305,6 +2305,11 @@ impl Path {
/// println!("{:?}", metadata.file_type());
/// ```
#[stable(feature = "path_ext", since = "1.5.0")]
#[rustc_deprecated(
since = "1.51.0",
reason = "the `std::fs::metadata` function is now preferred",
suggestion = "std::fs::metadata"
)]
pub fn metadata(&self) -> io::Result<fs::Metadata> {
fs::metadata(self)
}
Expand All @@ -2323,6 +2328,11 @@ impl Path {
/// println!("{:?}", metadata.file_type());
/// ```
#[stable(feature = "path_ext", since = "1.5.0")]
#[rustc_deprecated(
since = "1.51.0",
reason = "the `std::fs::symlink_metadata` function is now preferred",
suggestion = "std::fs::symlink_metadata"
)]
pub fn symlink_metadata(&self) -> io::Result<fs::Metadata> {
fs::symlink_metadata(self)
}
Expand All @@ -2341,6 +2351,11 @@ impl Path {
/// assert_eq!(path.canonicalize().unwrap(), PathBuf::from("/foo/test/bar.rs"));
/// ```
#[stable(feature = "path_ext", since = "1.5.0")]
#[rustc_deprecated(
since = "1.51.0",
reason = "the `std::fs::canonicalize` function is now preferred",
suggestion = "std::fs::canonicalize"
)]
pub fn canonicalize(&self) -> io::Result<PathBuf> {
fs::canonicalize(self)
}
Expand All @@ -2358,6 +2373,11 @@ impl Path {
/// let path_link = path.read_link().expect("read_link call failed");
/// ```
#[stable(feature = "path_ext", since = "1.5.0")]
#[rustc_deprecated(
since = "1.51.0",
reason = "the `std::fs::read_link` function is now preferred",
suggestion = "std::fs::read_link"
)]
pub fn read_link(&self) -> io::Result<PathBuf> {
fs::read_link(self)
}
Expand All @@ -2382,6 +2402,11 @@ impl Path {
/// }
/// ```
#[stable(feature = "path_ext", since = "1.5.0")]
#[rustc_deprecated(
since = "1.51.0",
reason = "the `std::fs::read_dir` function is now preferred",
suggestion = "std::fs::read_dir"
)]
pub fn read_dir(&self) -> io::Result<fs::ReadDir> {
fs::read_dir(self)
}
Expand All @@ -2406,6 +2431,11 @@ impl Path {
/// This is a convenience function that coerces errors to false. If you want to
/// check errors, call [`fs::metadata`].
#[stable(feature = "path_ext", since = "1.5.0")]
#[rustc_deprecated(
since = "1.51.0",
reason = "other processes may remove or rename files at any time",
suggestion = "use `std::fs::metadata`"
)]
pub fn exists(&self) -> bool {
fs::metadata(self).is_ok()
}
Expand Down Expand Up @@ -2438,6 +2468,11 @@ impl Path {
/// a Unix-like system for example. See [`fs::File::open`] or
/// [`fs::OpenOptions::open`] for more information.
#[stable(feature = "path_ext", since = "1.5.0")]
#[rustc_deprecated(
since = "1.51.0",
reason = "other processes may remove, rename, or replace files at any time",
suggestion = "use `std::fs::File::open` or `std::fs::metadata`"
)]
pub fn is_file(&self) -> bool {
fs::metadata(self).map(|m| m.is_file()).unwrap_or(false)
}
Expand All @@ -2463,7 +2498,16 @@ impl Path {
/// This is a convenience function that coerces errors to false. If you want to
/// check errors, call [`fs::metadata`] and handle its [`Result`]. Then call
/// [`fs::Metadata::is_dir`] if it was [`Ok`].
///
/// When the goal is simply to read from the source, the most reliable way to
/// test the source can be read is to open it. See [`fs::read_dir`] for more
/// information.
Comment on lines +2507 to +2510
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to update the deprecated docs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this because is_file already has a similar comment, and these comments relate to one of the reasons for deprecating these functions, so it seems like useful context.

#[stable(feature = "path_ext", since = "1.5.0")]
#[rustc_deprecated(
since = "1.51.0",
reason = "other processes may remove, rename, or replace directories at any time",
suggestion = "use `std::fs::read_dir` or `std::fs::metadata`"
)]
pub fn is_dir(&self) -> bool {
fs::metadata(self).map(|m| m.is_dir()).unwrap_or(false)
}
Expand Down
6 changes: 4 additions & 2 deletions library/std/src/sys_common/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ use crate::io::{self, Error, ErrorKind};
use crate::path::Path;

pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
if !from.is_file() {
let mut reader = fs::File::open(from)?;
let metadata = reader.metadata()?;

if !metadata.is_file() {
return Err(Error::new(
ErrorKind::InvalidInput,
"the source path is not an existing regular file",
));
}

let mut reader = fs::File::open(from)?;
let mut writer = fs::File::create(to)?;
let perm = reader.metadata()?.permissions();

Expand Down