Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Path methods — symlinks improvement #85747

Merged
merged 6 commits into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions library/std/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,31 @@ impl Metadata {
self.file_type().is_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");
/// 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
Expand Down
26 changes: 26 additions & 0 deletions library/std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// This method can alse be used to check whether symlink exists.
maxwase marked this conversation as resolved.
Show resolved Hide resolved
///
/// 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
///
/// ```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).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")]
Expand Down