Skip to content

Commit ec6bc2c

Browse files
committed
Update to latest code head
1 parent 7643a4d commit ec6bc2c

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

tokio/src/fs/try_exists.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
use crate::fs::asyncify;
2+
3+
use std::io;
24
use std::path::Path;
35

46
/// Returns `Ok(true)` if the path points at an existing entity.
57
///
68
/// This function will traverse symbolic links to query information about the
79
/// destination file. In case of broken symbolic links this will return `Ok(false)`.
810
///
9-
/// This is the async equivalent of [`std::fs::try_exists`][std].
11+
/// This is the async equivalent of [`std::path::Path::try_exists`][std].
1012
///
11-
/// [std]: fn@std::fs::try_exists
13+
/// [std]: fn@std::path::Path::try_exists
1214
///
1315
/// # Examples
1416
///
@@ -21,11 +23,7 @@ use std::path::Path;
2123
/// # }
2224
/// ```
2325
24-
pub async fn try_exists(path: impl AsRef<Path>) -> Result<bool, std::io::Error> {
26+
pub async fn try_exists(path: impl AsRef<Path>) -> io::Result<bool> {
2527
let path = path.as_ref().to_owned();
26-
match asyncify(move || std::fs::metadata(path)).await {
27-
Ok(_) => Ok(true),
28-
Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(false),
29-
Err(error) => Err(error),
30-
}
28+
asyncify(move || path.as_path().try_exists()).await
3129
}

tokio/tests/fs_try_exists.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#![warn(rust_2018_idioms)]
2+
#![cfg(all(feature = "full", not(tokio_wasi)))] // Wasi does not support file operations
3+
4+
use std::os::unix::prelude::PermissionsExt;
5+
6+
use tempfile::tempdir;
7+
use tokio::fs;
8+
9+
#[tokio::test]
10+
async fn try_exists() {
11+
let dir = tempdir().unwrap();
12+
13+
let existing_path = dir.path().join("foo.txt");
14+
fs::write(&existing_path, b"Hello File!").await.unwrap();
15+
let nonexisting_path = dir.path().join("bar.txt");
16+
17+
assert_eq!(fs::try_exists(existing_path).await.unwrap(), true);
18+
assert_eq!(fs::try_exists(nonexisting_path).await.unwrap(), false);
19+
20+
let permission_denied_directory_path = dir.path().join("baz");
21+
fs::create_dir(&permission_denied_directory_path)
22+
.await
23+
.unwrap();
24+
let permission_denied_file_path = permission_denied_directory_path.join("baz.txt");
25+
fs::write(&permission_denied_file_path, b"Hello File!")
26+
.await
27+
.unwrap();
28+
let mut perms = tokio::fs::metadata(&permission_denied_directory_path)
29+
.await
30+
.unwrap()
31+
.permissions();
32+
perms.set_mode(0o244);
33+
fs::set_permissions(&permission_denied_directory_path, perms)
34+
.await
35+
.unwrap();
36+
let permission_denied_result = fs::try_exists(permission_denied_file_path).await;
37+
assert_eq!(
38+
permission_denied_result.err().unwrap().kind(),
39+
std::io::ErrorKind::PermissionDenied
40+
);
41+
}

0 commit comments

Comments
 (0)