From e1cf38fa8932b8961c03338b585138c610c4cec4 Mon Sep 17 00:00:00 2001 From: Max Wase Date: Thu, 27 May 2021 15:20:36 +0300 Subject: [PATCH] Add `is_symlink()` method for `Path`. --- library/std/src/path.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/library/std/src/path.rs b/library/std/src/path.rs index 9c5615f58c43..efee31f99158 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -2568,6 +2568,32 @@ impl Path { fs::metadata(self).map(|m| m.is_dir()).unwrap_or(false) } + /// Returns true if the path exists on disk and is pointing at a symbolic link file. + /// This method can alse be used to check whether symlink exists. + /// + /// This function will not traverse symbolic links. + /// In case of broken symbolic links this will also return true. + /// + /// If you cannot access the directory containing the file, e.g., because of a + /// permission error, this will return false. + /// + /// # Examples + /// + /// ```no_run + /// use std::path::Path; + /// use std::os::unix::fs::symlink; + /// + /// let link_path = Path::new("/link"); + /// symlink("/origin_does_not_exists/", link_path)?; + /// assert_eq!(link_path.is_symlink(), true); + /// assert_eq!(link_path.exists(), false); + /// ``` + #[unstable(feature = "path_ext", issue = "none")] + #[inline] + pub fn is_symlink(&self) -> bool { + fs::symlink_metadata(self).is_ok() + } + /// Converts a [`Box`](Box) into a [`PathBuf`] without copying or /// allocating. #[stable(feature = "into_boxed_path", since = "1.20.0")]