From 66f7f7d8a9e6e524468f5d84e73b438274c20dbb Mon Sep 17 00:00:00 2001 From: Martin Habovstiak Date: Sat, 6 Feb 2021 13:16:37 +0100 Subject: [PATCH 1/2] Added `try_exists()` method to `std::path::Path` This method is similar to the existing `exists()` method, except it doesn't silently ignore the errors, leading to less error-prone code. This change intentionally does NOT touch the documentation of `exists()` nor recommend people to use this method while it's unstable. Such changes are reserved for stabilization to prevent confusing people. Apart from that it avoids conflicts with #80979. --- library/std/src/path.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/library/std/src/path.rs b/library/std/src/path.rs index 1889e54933867..4dd37f76efcb1 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -2468,6 +2468,36 @@ impl Path { fs::metadata(self).is_ok() } + /// 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::path::Path; + /// assert!(!Path::new("does_not_exist.txt").try_exists().expect("Can't check existence of file does_not_exist.txt")); + /// assert!(Path::new("/root/secret_file.txt").try_exists().is_err()); + /// ``` + // FIXME: stabilization should modify documentation of `exists()` to recommend this method + // instead. + #[unstable(feature = "path_try_exists", issue = "none")] + #[inline] + pub fn try_exists(&self) -> io::Result { + match fs::metadata(self) { + Ok(_) => Ok(true), + Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false), + Err(error) => Err(error), + } + } + /// Returns `true` if the path exists on disk and is pointing at a regular file. /// /// This function will traverse symbolic links to query information about the From 4330268181c3de234457138f23bb821afd0c181e Mon Sep 17 00:00:00 2001 From: Martin Habovstiak Date: Tue, 16 Mar 2021 08:41:14 +0100 Subject: [PATCH 2/2] Filled tracking issue for path_try_exists This adds the ID of the tracking issue to the feature. --- library/std/src/path.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/path.rs b/library/std/src/path.rs index 4dd37f76efcb1..e5e4ccac921b2 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -2488,7 +2488,7 @@ impl Path { /// ``` // FIXME: stabilization should modify documentation of `exists()` to recommend this method // instead. - #[unstable(feature = "path_try_exists", issue = "none")] + #[unstable(feature = "path_try_exists", issue = "83186")] #[inline] pub fn try_exists(&self) -> io::Result { match fs::metadata(self) {