Skip to content

Commit 88ba8ad

Browse files
committed
Auto merge of rust-lang#85747 - maxwase:path-symlinks-methods, r=m-ou-se
Path methods — symlinks improvement This PR adds symlink method for the `Path`. Tracking issue: rust-lang#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.
2 parents 312b894 + 01435fc commit 88ba8ad

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

library/std/src/fs.rs

+26
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,32 @@ impl Metadata {
10071007
self.file_type().is_file()
10081008
}
10091009

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

library/std/src/path.rs

+26
Original file line numberDiff line numberDiff line change
@@ -2592,6 +2592,32 @@ impl Path {
25922592
fs::metadata(self).map(|m| m.is_dir()).unwrap_or(false)
25932593
}
25942594

2595+
/// Returns true if the path exists on disk and is pointing at a symbolic link.
2596+
///
2597+
/// This function will not traverse symbolic links.
2598+
/// In case of a broken symbolic link this will also return true.
2599+
///
2600+
/// If you cannot access the directory containing the file, e.g., because of a
2601+
/// permission error, this will return false.
2602+
///
2603+
/// # Examples
2604+
///
2605+
#[cfg_attr(unix, doc = "```no_run")]
2606+
#[cfg_attr(not(unix), doc = "```ignore")]
2607+
/// #![feature(is_symlink)]
2608+
/// use std::path::Path;
2609+
/// use std::os::unix::fs::symlink;
2610+
///
2611+
/// let link_path = Path::new("link");
2612+
/// symlink("/origin_does_not_exists/", link_path).unwrap();
2613+
/// assert_eq!(link_path.is_symlink(), true);
2614+
/// assert_eq!(link_path.exists(), false);
2615+
/// ```
2616+
#[unstable(feature = "is_symlink", issue = "85748")]
2617+
pub fn is_symlink(&self) -> bool {
2618+
fs::symlink_metadata(self).map(|m| m.is_symlink()).unwrap_or(false)
2619+
}
2620+
25952621
/// Converts a [`Box<Path>`](Box) into a [`PathBuf`] without copying or
25962622
/// allocating.
25972623
#[stable(feature = "into_boxed_path", since = "1.20.0")]

0 commit comments

Comments
 (0)