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

Improved documentation of Path::exists #80979

Closed
wants to merge 6 commits into from
Closed
Changes from 3 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
34 changes: 31 additions & 3 deletions library/std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2386,13 +2386,27 @@ impl Path {
fs::read_dir(self)
}

/// Returns `true` if the path points at an existing entity.
/// Returns `true` if the path points at an existing entity. False if it doesn't
/// **or** we can't tell.
///
/// This function will traverse symbolic links to query information about the
/// destination file. In case of broken symbolic links this will return `false`.
///
/// If you cannot access the directory containing the file, e.g., because of a
/// permission error, this will return `false`.
/// # Caveats
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't generally call out the caveats as a separate section elsewhere in the documentation of the standard library, as it is very often implied (and similarly, the paragraph about traversing the symbolic links could also be considered a caveat, but is outside of this section).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So just simple bold?

the paragraph about traversing the symbolic links could also be considered a caveat, but is outside of this section

I actually don't find this behavior surprising at all as all other applications behave exactly same. Willing to reconsider if there's a bunch of people who find it surprising.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am on board with a Caveats heading here, it seems appropriate.

///
/// Checking if a file exists and then attempting to use it is vulnerable to race
/// conditions. It is better to attempt the operation and handle the error when it
/// fails.
///
/// If you cannot access the metadata of the file, e.g., because of a permission
/// error on the containing directory, this will return `false`. In many cases this
/// is not desirable as it silently ignores error that might be serious.
///
/// Sometimes existence of a file is not what's actually interesting for an
/// application but its accessibility. This function does **not** check if the file
/// is accessible by the calling user. Calling `access` on Unix (or a similar
/// syscall) would be more appropriate. Rust `std` currently does not provide such
/// method.
///
/// # Examples
///
Expand All @@ -2405,6 +2419,20 @@ impl Path {
///
/// This is a convenience function that coerces errors to false. If you want to
/// check errors, call [`fs::metadata`].
///
/// An example of proper error handling.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it perhaps make more sense to just link readers to metadata which will have its own examples?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. My intention was to provide a simple snippet people could copy-paste to do the job properly. So I'd agree to moving the example to metadata. I'm not entirely sure people will find it when linked from exists so maybe add a simple note but maybe that's excessive.

/// ```no_run
/// use std::path::Path;
/// use std::io::ErrorKind;
/// use std::fs;
/// let exists = match fs::metadata(Path::new("does_not_exist.txt")) {
/// Ok(_) => true,
/// Err(err) if err.kind() == ErrorKind::NotFound => false,
/// Err(err) => return Err(err),
/// };
///
/// assert!(!exists);
/// ```
#[stable(feature = "path_ext", since = "1.5.0")]
pub fn exists(&self) -> bool {
fs::metadata(self).is_ok()
Expand Down