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

Add try_exists function as wrapper over std::path::Path::try_exists #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Version 2.1.3

- Add `async_fs::try_exists` function, which is an async wrapper for `std::path::Path::try_exists`.

# Version 2.1.2

- Ensure that the docs for `create_dir_all` are close to the equivalent function
Expand Down
6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "async-fs"
# When publishing a new version:
# - Update CHANGELOG.md
# - Create "v2.x.y" git tag
version = "2.1.2"
version = "2.1.3"
authors = ["Stjepan Glavina <stjepang@gmail.com>"]
edition = "2021"
rust-version = "1.63"
Expand All @@ -26,6 +26,4 @@ libc = "0.2.78"

[target.'cfg(windows)'.dev-dependencies.windows-sys]
version = "0.59"
features = [
"Win32_Storage_FileSystem",
]
features = ["Win32_Storage_FileSystem"]
27 changes: 27 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,33 @@ pub async fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
unblock(move || std::fs::canonicalize(path)).await
}

/// Returns `Ok(true)` if the path points at an existing entity.
///
/// This function will traverse symbolic links to query information about the
/// destination file. In case of broken symbolic links this will return `Ok(false)`.
///
/// [`try_exists`] only checks whether or not a path was both found and readable.
///
/// Note that while this avoids some pitfalls of the `exists()` method, it still can not
/// prevent time-of-check to time-of-use (TOCTOU) bugs. You should only use it in scenarios
/// where those bugs are not an issue.
///
/// This is the async equivalent of [std::path::Path::try_exists].
///
/// # Examples
///
/// ```no_run
/// use std::path::Path;
/// # futures_lite::future::block_on(async {
/// assert!(!async_fs::try_exists(Path::new("does_not_exist.txt")).await.unwrap());
/// assert!(!async_fs::try_exists(Path::new("exists.txt")).await.unwrap());
/// # std::io::Result::Ok(()) });
/// ```
pub async fn try_exists<P: AsRef<Path>>(path: P) -> io::Result<bool> {
let path = path.as_ref().to_owned();
unblock(move || path.try_exists()).await
}

/// Copies a file to a new location.
///
/// On success, the total number of bytes copied is returned and equals the length of the `dst`
Expand Down
Loading