From e1cf38fa8932b8961c03338b585138c610c4cec4 Mon Sep 17 00:00:00 2001 From: Max Wase Date: Thu, 27 May 2021 15:20:36 +0300 Subject: [PATCH 1/6] 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 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`](Box) into a [`PathBuf`] without copying or /// allocating. #[stable(feature = "into_boxed_path", since = "1.20.0")] From 89c0f50b09e1f37b605f262aa8ffd3d2f4ee1c18 Mon Sep 17 00:00:00 2001 From: Max Wase Date: Thu, 27 May 2021 16:04:08 +0300 Subject: [PATCH 2/6] Fix `is_symlink()` method for `Path` using added `is_symlink()` method for `Metadata` --- library/std/src/fs.rs | 24 ++++++++++++++++++++++++ library/std/src/path.rs | 5 ++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs index a1636e2f604..97b5e26bd0b 100644 --- a/library/std/src/fs.rs +++ b/library/std/src/fs.rs @@ -1007,6 +1007,30 @@ impl Metadata { self.file_type().is_file() } + /// Returns `true` if this metadata is for a symbolic link file. + /// + /// # Examples + /// + /// ```no_run + /// 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 = "none")] + 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 efee31f9915..2023a8448aa 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -2588,10 +2588,9 @@ impl Path { /// assert_eq!(link_path.is_symlink(), true); /// assert_eq!(link_path.exists(), false); /// ``` - #[unstable(feature = "path_ext", issue = "none")] - #[inline] + #[unstable(feature = "is_symlink", issue = "none")] pub fn is_symlink(&self) -> bool { - fs::symlink_metadata(self).is_ok() + fs::symlink_metadata(self).map(|m| m.is_symlink()).unwrap_or(false) } /// Converts a [`Box`](Box) into a [`PathBuf`] without copying or From 2d88c52ab7173f82db4e309661fb06ebdf55b3a6 Mon Sep 17 00:00:00 2001 From: Max Wase Date: Thu, 27 May 2021 16:11:54 +0300 Subject: [PATCH 3/6] Tracking issue add. --- library/std/src/fs.rs | 2 +- library/std/src/path.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs index 97b5e26bd0b..8e89f5a1891 100644 --- a/library/std/src/fs.rs +++ b/library/std/src/fs.rs @@ -1026,7 +1026,7 @@ impl Metadata { /// Ok(()) /// } /// ``` - #[unstable(feature = "is_symlink", issue = "none")] + #[unstable(feature = "is_symlink", issue = "85748")] pub fn is_symlink(&self) -> bool { self.file_type().is_symlink() } diff --git a/library/std/src/path.rs b/library/std/src/path.rs index 2023a8448aa..719f28dd604 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -2588,7 +2588,7 @@ impl Path { /// assert_eq!(link_path.is_symlink(), true); /// assert_eq!(link_path.exists(), false); /// ``` - #[unstable(feature = "is_symlink", issue = "none")] + #[unstable(feature = "is_symlink", issue = "85748")] pub fn is_symlink(&self) -> bool { fs::symlink_metadata(self).map(|m| m.is_symlink()).unwrap_or(false) } From a0958df56f9f6714aa31c9d8812434a0f2f8f992 Mon Sep 17 00:00:00 2001 From: Max Wase Date: Thu, 27 May 2021 18:02:21 +0300 Subject: [PATCH 4/6] Review fixes + doc-features --- library/std/src/fs.rs | 5 +++-- library/std/src/path.rs | 9 +++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs index 8e89f5a1891..521693d7f2c 100644 --- a/library/std/src/fs.rs +++ b/library/std/src/fs.rs @@ -1007,17 +1007,18 @@ impl Metadata { self.file_type().is_file() } - /// Returns `true` if this metadata is for a symbolic link file. + /// Returns `true` if this metadata is for a symbolic link. /// /// # Examples /// /// ```no_run + /// #![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"); + /// let link_path = Path::new("link"); /// symlink("/origin_does_not_exists/", link_path)?; /// /// let metadata = fs::symlink_metadata(link_path)?; diff --git a/library/std/src/path.rs b/library/std/src/path.rs index 719f28dd604..9afeb173f20 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -2568,11 +2568,11 @@ 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. + /// Returns true if the path exists on disk and is pointing at a symbolic link. /// 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. + /// 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. @@ -2580,11 +2580,12 @@ impl Path { /// # Examples /// /// ```no_run + /// #![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)?; + /// 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); /// ``` From f3c1db311c760770cccf5242bf383ab5bf4783a8 Mon Sep 17 00:00:00 2001 From: Max Wase Date: Sun, 6 Jun 2021 22:42:29 +0300 Subject: [PATCH 5/6] Update doc library/std/src/path.rs Co-authored-by: Mara Bos --- library/std/src/path.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/library/std/src/path.rs b/library/std/src/path.rs index 9afeb173f20..055c9514445 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -2569,7 +2569,6 @@ impl Path { } /// Returns true if the path exists on disk and is pointing at a symbolic link. - /// This method can alse be used to check whether symlink exists. /// /// This function will not traverse symbolic links. /// In case of a broken symbolic link this will also return true. From 01435fc83a5f3ed827d7ce618f4e3068a6ff964f Mon Sep 17 00:00:00 2001 From: Max Wase Date: Fri, 18 Jun 2021 14:17:21 +0300 Subject: [PATCH 6/6] `no_run` and `ignore` doc attributes --- library/std/src/fs.rs | 3 ++- library/std/src/path.rs | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs index 521693d7f2c..9076656f64e 100644 --- a/library/std/src/fs.rs +++ b/library/std/src/fs.rs @@ -1011,7 +1011,8 @@ impl Metadata { /// /// # Examples /// - /// ```no_run + #[cfg_attr(unix, doc = "```no_run")] + #[cfg_attr(not(unix), doc = "```ignore")] /// #![feature(is_symlink)] /// use std::fs; /// use std::path::Path; diff --git a/library/std/src/path.rs b/library/std/src/path.rs index 055c9514445..272ea5a3686 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -2578,7 +2578,8 @@ impl Path { /// /// # Examples /// - /// ```no_run + #[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;