Implemented fs::metadata and Path::exists

yoshuawuyts-patch-1
Wouter Geraedts 5 years ago
parent 930b81868d
commit e690b55b18

@ -1,8 +1,7 @@
use std::path::Path;
use cfg_if::cfg_if; use cfg_if::cfg_if;
use crate::io; use crate::io;
use crate::path::Path;
use crate::task::blocking; use crate::task::blocking;
/// Reads metadata for a path. /// Reads metadata for a path.
@ -36,7 +35,7 @@ use crate::task::blocking;
/// # Ok(()) }) } /// # Ok(()) }) }
/// ``` /// ```
pub async fn metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> { pub async fn metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
let path = path.as_ref().to_owned(); let path: std::path::PathBuf = path.as_ref().to_path_buf().into();
blocking::spawn(async move { std::fs::metadata(path) }).await blocking::spawn(async move { std::fs::metadata(path) }).await
} }

@ -75,6 +75,31 @@ impl Path {
self.inner.components() self.inner.components()
} }
/// Returns `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 `false`.
///
/// If you cannot access the directory containing the file, e.g., because of a
/// permission error, this will return `false`.
///
/// # Examples
///
/// ```no_run
/// use async_std::path::Path;
/// assert_eq!(Path::new("does_not_exist.txt").exists(), false);
/// ```
///
/// # See Also
///
/// This is a convenience function that coerces errors to false. If you want to
/// check errors, call [fs::metadata].
///
/// [fs::metadata]: ../fs/fn.metadata.html
pub async fn exists(&self) -> bool {
fs::metadata(self).await.is_ok()
}
/// Directly wraps a string slice as a `Path` slice. /// Directly wraps a string slice as a `Path` slice.
/// ///
/// This is a cost-free conversion. /// This is a cost-free conversion.

Loading…
Cancel
Save