about summary refs log tree commit diff
path: root/library/std/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-06-18 17:13:19 +0000
committerbors <bors@rust-lang.org>2021-06-18 17:13:19 +0000
commit88ba8ad730a124f7e1d4f1bd17f2b14cb18eed3c (patch)
tree12e8bb7dd2bc367ce12555a0e4405e4654312d2d /library/std/src
parent312b894cc12240a3fcc645474c3daa14f7d568ea (diff)
parent01435fc83a5f3ed827d7ce618f4e3068a6ff964f (diff)
downloadrust-88ba8ad730a124f7e1d4f1bd17f2b14cb18eed3c.tar.gz
rust-88ba8ad730a124f7e1d4f1bd17f2b14cb18eed3c.zip
Auto merge of #85747 - maxwase:path-symlinks-methods, r=m-ou-se
Path methods — symlinks improvement

This PR adds symlink method for the `Path`.

Tracking issue: #85748
For the discussion you can see [internals topic](https://internals.rust-lang.org/t/path-methods-symlinks-improvement/14776)

P.S.
I'm not fully sure about `stable` attribute, correct me if I'm wrong.
Diffstat (limited to 'library/std/src')
-rw-r--r--library/std/src/fs.rs26
-rw-r--r--library/std/src/path.rs26
2 files changed, 52 insertions, 0 deletions
diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs
index a1636e2f604..9076656f64e 100644
--- a/library/std/src/fs.rs
+++ b/library/std/src/fs.rs
@@ -1007,6 +1007,32 @@ impl Metadata {
         self.file_type().is_file()
     }
 
+    /// Returns `true` if this metadata is for a symbolic link.
+    ///
+    /// # Examples
+    ///
+    #[cfg_attr(unix, doc = "```no_run")]
+    #[cfg_attr(not(unix), doc = "```ignore")]
+    /// #![feature(is_symlink)]
+    /// use std::fs;
+    /// use std::path::Path;
+    /// use std::os::unix::fs::symlink;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let link_path = Path::new("link");
+    ///     symlink("/origin_does_not_exists/", link_path)?;
+    ///
+    ///     let metadata = fs::symlink_metadata(link_path)?;
+    ///
+    ///     assert!(metadata.is_symlink());
+    ///     Ok(())
+    /// }
+    /// ```
+    #[unstable(feature = "is_symlink", issue = "85748")]
+    pub fn is_symlink(&self) -> bool {
+        self.file_type().is_symlink()
+    }
+
     /// Returns the size of the file, in bytes, this metadata is for.
     ///
     /// # Examples
diff --git a/library/std/src/path.rs b/library/std/src/path.rs
index 6ccf4c9656e..c71751efb9f 100644
--- a/library/std/src/path.rs
+++ b/library/std/src/path.rs
@@ -2592,6 +2592,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.
+    ///
+    /// This function will not traverse symbolic links.
+    /// In case of a broken symbolic link 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
+    ///
+    #[cfg_attr(unix, doc = "```no_run")]
+    #[cfg_attr(not(unix), doc = "```ignore")]
+    /// #![feature(is_symlink)]
+    /// use std::path::Path;
+    /// use std::os::unix::fs::symlink;
+    ///
+    /// let link_path = Path::new("link");
+    /// symlink("/origin_does_not_exists/", link_path).unwrap();
+    /// assert_eq!(link_path.is_symlink(), true);
+    /// assert_eq!(link_path.exists(), false);
+    /// ```
+    #[unstable(feature = "is_symlink", issue = "85748")]
+    pub fn is_symlink(&self) -> bool {
+        fs::symlink_metadata(self).map(|m| m.is_symlink()).unwrap_or(false)
+    }
+
     /// Converts a [`Box<Path>`](Box) into a [`PathBuf`] without copying or
     /// allocating.
     #[stable(feature = "into_boxed_path", since = "1.20.0")]