Skip to content

Commit e1cf38f

Browse files
committed
Add is_symlink() method for Path.
1 parent 9814e83 commit e1cf38f

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

library/std/src/path.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2568,6 +2568,32 @@ impl Path {
25682568
fs::metadata(self).map(|m| m.is_dir()).unwrap_or(false)
25692569
}
25702570

2571+
/// Returns true if the path exists on disk and is pointing at a symbolic link file.
2572+
/// This method can alse be used to check whether symlink exists.
2573+
///
2574+
/// This function will not traverse symbolic links.
2575+
/// In case of broken symbolic links this will also return true.
2576+
///
2577+
/// If you cannot access the directory containing the file, e.g., because of a
2578+
/// permission error, this will return false.
2579+
///
2580+
/// # Examples
2581+
///
2582+
/// ```no_run
2583+
/// use std::path::Path;
2584+
/// use std::os::unix::fs::symlink;
2585+
///
2586+
/// let link_path = Path::new("/link");
2587+
/// symlink("/origin_does_not_exists/", link_path)?;
2588+
/// assert_eq!(link_path.is_symlink(), true);
2589+
/// assert_eq!(link_path.exists(), false);
2590+
/// ```
2591+
#[unstable(feature = "path_ext", issue = "none")]
2592+
#[inline]
2593+
pub fn is_symlink(&self) -> bool {
2594+
fs::symlink_metadata(self).is_ok()
2595+
}
2596+
25712597
/// Converts a [`Box<Path>`](Box) into a [`PathBuf`] without copying or
25722598
/// allocating.
25732599
#[stable(feature = "into_boxed_path", since = "1.20.0")]

0 commit comments

Comments
 (0)