mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-26 14:43:24 +00:00
Move the implementation of Path::exists
to sys_common::fs
so platforms can specialize it
Windows implementation of `fs::try_exists`
This commit is contained in:
parent
3e827cc21e
commit
2c2c1593ac
@ -2208,3 +2208,29 @@ impl AsInnerMut<fs_imp::DirBuilder> for DirBuilder {
|
||||
&mut self.inner
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `Ok(true)` if the path points at an existing entity.
|
||||
///
|
||||
/// This function will traverse symbolic links to query information about the
|
||||
/// destination file. In case of broken symbolic links this will return `Ok(false)`.
|
||||
///
|
||||
/// As opposed to the `exists()` method, this one doesn't silently ignore errors
|
||||
/// unrelated to the path not existing. (E.g. it will return `Err(_)` in case of permission
|
||||
/// denied on some of the parent directories.)
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// #![feature(path_try_exists)]
|
||||
/// use std::fs;
|
||||
///
|
||||
/// assert!(!fs::try_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());
|
||||
/// ```
|
||||
// FIXME: stabilization should modify documentation of `exists()` to recommend this method
|
||||
// instead.
|
||||
#[unstable(feature = "path_try_exists", issue = "83186")]
|
||||
#[inline]
|
||||
pub fn try_exists<P: AsRef<Path>>(path: P) -> io::Result<bool> {
|
||||
fs_imp::try_exists(path.as_ref())
|
||||
}
|
||||
|
@ -2507,11 +2507,7 @@ impl Path {
|
||||
#[unstable(feature = "path_try_exists", issue = "83186")]
|
||||
#[inline]
|
||||
pub fn try_exists(&self) -> io::Result<bool> {
|
||||
match fs::metadata(self) {
|
||||
Ok(_) => Ok(true),
|
||||
Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
|
||||
Err(error) => Err(error),
|
||||
}
|
||||
fs::try_exists(self)
|
||||
}
|
||||
|
||||
/// Returns `true` if the path exists on disk and is pointing at a regular file.
|
||||
|
@ -12,7 +12,7 @@ use crate::sys::time::SystemTime;
|
||||
use crate::sys::unsupported;
|
||||
use crate::sys_common::os_str_bytes::OsStrExt;
|
||||
|
||||
pub use crate::sys_common::fs::copy;
|
||||
pub use crate::sys_common::fs::{copy, try_exists};
|
||||
//pub use crate::sys_common::fs::remove_dir_all;
|
||||
|
||||
fn cstr(path: &Path) -> io::Result<CString> {
|
||||
|
@ -48,7 +48,7 @@ use libc::{
|
||||
dirent64, fstat64, ftruncate64, lseek64, lstat64, off64_t, open64, readdir64_r, stat64,
|
||||
};
|
||||
|
||||
pub use crate::sys_common::fs::remove_dir_all;
|
||||
pub use crate::sys_common::fs::{remove_dir_all, try_exists};
|
||||
|
||||
pub struct File(FileDesc);
|
||||
|
||||
|
@ -275,6 +275,10 @@ pub fn remove_dir_all(_path: &Path) -> io::Result<()> {
|
||||
unsupported()
|
||||
}
|
||||
|
||||
pub fn try_exists(_path: &Path) -> io::Result<bool> {
|
||||
unsupported()
|
||||
}
|
||||
|
||||
pub fn readlink(_p: &Path) -> io::Result<PathBuf> {
|
||||
unsupported()
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ use crate::sys::time::SystemTime;
|
||||
use crate::sys::unsupported;
|
||||
use crate::sys_common::FromInner;
|
||||
|
||||
pub use crate::sys_common::fs::remove_dir_all;
|
||||
pub use crate::sys_common::fs::{remove_dir_all, try_exists};
|
||||
|
||||
pub struct File {
|
||||
fd: WasiFd,
|
||||
|
@ -11,6 +11,7 @@ use crate::sync::Arc;
|
||||
use crate::sys::handle::Handle;
|
||||
use crate::sys::time::SystemTime;
|
||||
use crate::sys::{c, cvt};
|
||||
pub use crate::sys_common::fs::try_exists;
|
||||
use crate::sys_common::FromInner;
|
||||
|
||||
use super::to_u16s;
|
||||
|
@ -41,3 +41,11 @@ fn remove_dir_all_recursive(path: &Path) -> io::Result<()> {
|
||||
}
|
||||
fs::remove_dir(path)
|
||||
}
|
||||
|
||||
pub fn try_exists(path: &Path) -> io::Result<bool> {
|
||||
match fs::metadata(path) {
|
||||
Ok(_) => Ok(true),
|
||||
Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
|
||||
Err(error) => Err(error),
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user