Skip to content

Commit 89c0f50

Browse files
committed
Fix is_symlink() method for Path using added is_symlink() method for Metadata
1 parent e1cf38f commit 89c0f50

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

library/std/src/fs.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,30 @@ impl Metadata {
10071007
self.file_type().is_file()
10081008
}
10091009

1010+
/// Returns `true` if this metadata is for a symbolic link file.
1011+
///
1012+
/// # Examples
1013+
///
1014+
/// ```no_run
1015+
/// use std::fs;
1016+
/// use std::path::Path;
1017+
/// use std::os::unix::fs::symlink;
1018+
///
1019+
/// fn main() -> std::io::Result<()> {
1020+
/// let link_path = Path::new("/link");
1021+
/// symlink("/origin_does_not_exists/", link_path)?;
1022+
///
1023+
/// let metadata = fs::symlink_metadata(link_path)?;
1024+
///
1025+
/// assert!(metadata.is_symlink());
1026+
/// Ok(())
1027+
/// }
1028+
/// ```
1029+
#[unstable(feature = "is_symlink", issue = "none")]
1030+
pub fn is_symlink(&self) -> bool {
1031+
self.file_type().is_symlink()
1032+
}
1033+
10101034
/// Returns the size of the file, in bytes, this metadata is for.
10111035
///
10121036
/// # Examples

library/std/src/path.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2588,10 +2588,9 @@ impl Path {
25882588
/// assert_eq!(link_path.is_symlink(), true);
25892589
/// assert_eq!(link_path.exists(), false);
25902590
/// ```
2591-
#[unstable(feature = "path_ext", issue = "none")]
2592-
#[inline]
2591+
#[unstable(feature = "is_symlink", issue = "none")]
25932592
pub fn is_symlink(&self) -> bool {
2594-
fs::symlink_metadata(self).is_ok()
2593+
fs::symlink_metadata(self).map(|m| m.is_symlink()).unwrap_or(false)
25952594
}
25962595

25972596
/// Converts a [`Box<Path>`](Box) into a [`PathBuf`] without copying or

0 commit comments

Comments
 (0)