Rollup merge of #126140 - eduardosm:stabilize-fs_try_exists, r=Amanieu
Rename `std::fs::try_exists` to `std::fs::exists` and stabilize fs_try_exists FCP completed in tracking issue. Tracking issue: https://github.com/rust-lang/rust/issues/83186 Closes https://github.com/rust-lang/rust/issues/83186 Stabilized API: ```rust mod fs { pub fn exists<P: AsRef<Path>>(path: P) -> io::Result<bool>; } ```
This commit is contained in:
commit
f3ced9d540
@ -2742,18 +2742,15 @@ impl AsInnerMut<fs_imp::DirBuilder> for DirBuilder {
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// #![feature(fs_try_exists)]
|
|
||||||
/// use std::fs;
|
/// use std::fs;
|
||||||
///
|
///
|
||||||
/// assert!(!fs::try_exists("does_not_exist.txt").expect("Can't check existence of file does_not_exist.txt"));
|
/// assert!(!fs::exists("does_not_exist.txt").expect("Can't check existence of file does_not_exist.txt"));
|
||||||
/// assert!(fs::try_exists("/root/secret_file.txt").is_err());
|
/// assert!(fs::exists("/root/secret_file.txt").is_err());
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// [`Path::exists`]: crate::path::Path::exists
|
/// [`Path::exists`]: crate::path::Path::exists
|
||||||
// FIXME: stabilization should modify documentation of `exists()` to recommend this method
|
#[stable(feature = "fs_try_exists", since = "CURRENT_RUSTC_VERSION")]
|
||||||
// instead.
|
|
||||||
#[unstable(feature = "fs_try_exists", issue = "83186")]
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn try_exists<P: AsRef<Path>>(path: P) -> io::Result<bool> {
|
pub fn exists<P: AsRef<Path>>(path: P) -> io::Result<bool> {
|
||||||
fs_imp::try_exists(path.as_ref())
|
fs_imp::exists(path.as_ref())
|
||||||
}
|
}
|
||||||
|
@ -2907,6 +2907,8 @@ impl Path {
|
|||||||
/// prevent time-of-check to time-of-use (TOCTOU) bugs. You should only use it in scenarios
|
/// prevent time-of-check to time-of-use (TOCTOU) bugs. You should only use it in scenarios
|
||||||
/// where those bugs are not an issue.
|
/// where those bugs are not an issue.
|
||||||
///
|
///
|
||||||
|
/// This is an alias for [`std::fs::exists`](crate::fs::exists).
|
||||||
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
@ -2919,7 +2921,7 @@ impl Path {
|
|||||||
#[stable(feature = "path_try_exists", since = "1.63.0")]
|
#[stable(feature = "path_try_exists", since = "1.63.0")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn try_exists(&self) -> io::Result<bool> {
|
pub fn try_exists(&self) -> io::Result<bool> {
|
||||||
fs::try_exists(self)
|
fs::exists(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if the path exists on disk and is pointing at a regular file.
|
/// Returns `true` if the path exists on disk and is pointing at a regular file.
|
||||||
|
@ -18,7 +18,7 @@ use crate::sys::time::SystemTime;
|
|||||||
use crate::sys::unsupported;
|
use crate::sys::unsupported;
|
||||||
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
|
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
|
||||||
|
|
||||||
pub use crate::sys_common::fs::{copy, try_exists};
|
pub use crate::sys_common::fs::{copy, exists};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct File(FileDesc);
|
pub struct File(FileDesc);
|
||||||
|
@ -12,7 +12,7 @@ use crate::{
|
|||||||
sys::unsupported,
|
sys::unsupported,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use crate::sys_common::fs::try_exists;
|
pub use crate::sys_common::fs::exists;
|
||||||
|
|
||||||
/// A file descriptor.
|
/// A file descriptor.
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
|
@ -97,7 +97,7 @@ use libc::{
|
|||||||
))]
|
))]
|
||||||
use libc::{dirent64, fstat64, ftruncate64, lseek64, lstat64, off64_t, open64, stat64};
|
use libc::{dirent64, fstat64, ftruncate64, lseek64, lstat64, off64_t, open64, stat64};
|
||||||
|
|
||||||
pub use crate::sys_common::fs::try_exists;
|
pub use crate::sys_common::fs::exists;
|
||||||
|
|
||||||
pub struct File(FileDesc);
|
pub struct File(FileDesc);
|
||||||
|
|
||||||
|
@ -478,7 +478,7 @@ mod cgroups {
|
|||||||
|
|
||||||
use crate::borrow::Cow;
|
use crate::borrow::Cow;
|
||||||
use crate::ffi::OsString;
|
use crate::ffi::OsString;
|
||||||
use crate::fs::{try_exists, File};
|
use crate::fs::{exists, File};
|
||||||
use crate::io::Read;
|
use crate::io::Read;
|
||||||
use crate::io::{BufRead, BufReader};
|
use crate::io::{BufRead, BufReader};
|
||||||
use crate::os::unix::ffi::OsStringExt;
|
use crate::os::unix::ffi::OsStringExt;
|
||||||
@ -556,7 +556,7 @@ mod cgroups {
|
|||||||
path.push("cgroup.controllers");
|
path.push("cgroup.controllers");
|
||||||
|
|
||||||
// skip if we're not looking at cgroup2
|
// skip if we're not looking at cgroup2
|
||||||
if matches!(try_exists(&path), Err(_) | Ok(false)) {
|
if matches!(exists(&path), Err(_) | Ok(false)) {
|
||||||
return usize::MAX;
|
return usize::MAX;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -613,7 +613,7 @@ mod cgroups {
|
|||||||
path.push(&group_path);
|
path.push(&group_path);
|
||||||
|
|
||||||
// skip if we guessed the mount incorrectly
|
// skip if we guessed the mount incorrectly
|
||||||
if matches!(try_exists(&path), Err(_) | Ok(false)) {
|
if matches!(exists(&path), Err(_) | Ok(false)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -291,7 +291,7 @@ pub fn remove_dir_all(_path: &Path) -> io::Result<()> {
|
|||||||
unsupported()
|
unsupported()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn try_exists(_path: &Path) -> io::Result<bool> {
|
pub fn exists(_path: &Path) -> io::Result<bool> {
|
||||||
unsupported()
|
unsupported()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ use crate::sys::time::SystemTime;
|
|||||||
use crate::sys::unsupported;
|
use crate::sys::unsupported;
|
||||||
use crate::sys_common::{AsInner, FromInner, IntoInner};
|
use crate::sys_common::{AsInner, FromInner, IntoInner};
|
||||||
|
|
||||||
pub use crate::sys_common::fs::try_exists;
|
pub use crate::sys_common::fs::exists;
|
||||||
|
|
||||||
pub struct File {
|
pub struct File {
|
||||||
fd: WasiFd,
|
fd: WasiFd,
|
||||||
|
@ -1531,7 +1531,7 @@ pub fn junction_point(original: &Path, link: &Path) -> io::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Try to see if a file exists but, unlike `exists`, report I/O errors.
|
// Try to see if a file exists but, unlike `exists`, report I/O errors.
|
||||||
pub fn try_exists(path: &Path) -> io::Result<bool> {
|
pub fn exists(path: &Path) -> io::Result<bool> {
|
||||||
// Open the file to ensure any symlinks are followed to their target.
|
// Open the file to ensure any symlinks are followed to their target.
|
||||||
let mut opts = OpenOptions::new();
|
let mut opts = OpenOptions::new();
|
||||||
// No read, write, etc access rights are needed.
|
// No read, write, etc access rights are needed.
|
||||||
|
@ -42,7 +42,7 @@ fn remove_dir_all_recursive(path: &Path) -> io::Result<()> {
|
|||||||
fs::remove_dir(path)
|
fs::remove_dir(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn try_exists(path: &Path) -> io::Result<bool> {
|
pub fn exists(path: &Path) -> io::Result<bool> {
|
||||||
match fs::metadata(path) {
|
match fs::metadata(path) {
|
||||||
Ok(_) => Ok(true),
|
Ok(_) => Ok(true),
|
||||||
Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
|
Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user