about summary refs log tree commit diff
path: root/library/std
diff options
context:
space:
mode:
authorMax Wase <max.vvase@gmail.com>2021-05-27 15:20:36 +0300
committerMax Wase <max.vvase@gmail.com>2021-05-27 15:20:36 +0300
commite1cf38fa8932b8961c03338b585138c610c4cec4 (patch)
tree668797a553e2b5d7108d6af0e2524ee8dee00cfd /library/std
parent9814e830942dcc65e69a0d077cb2e013b5948795 (diff)
downloadrust-e1cf38fa8932b8961c03338b585138c610c4cec4.tar.gz
rust-e1cf38fa8932b8961c03338b585138c610c4cec4.zip
Add `is_symlink()` method for `Path`.
Diffstat (limited to 'library/std')
-rw-r--r--library/std/src/path.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/library/std/src/path.rs b/library/std/src/path.rs
index 9c5615f58c4..efee31f9915 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<Path>`](Box) into a [`PathBuf`] without copying or
     /// allocating.
     #[stable(feature = "into_boxed_path", since = "1.20.0")]