async_std/fs/
read_link.rs

1use crate::io;
2use crate::path::{Path, PathBuf};
3use crate::task::spawn_blocking;
4use crate::utils::Context as _;
5
6/// Reads a symbolic link and returns the path it points to.
7///
8/// This function is an async version of [`std::fs::read_link`].
9///
10/// [`std::fs::read_link`]: https://doc.rust-lang.org/std/fs/fn.read_link.html
11///
12/// # Errors
13///
14/// An error will be returned in the following situations:
15///
16/// * `path` does not point to an existing link.
17/// * Some other I/O error occurred.
18///
19/// # Examples
20///
21/// ```no_run
22/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
23/// #
24/// use async_std::fs;
25///
26/// let path = fs::read_link("a.txt").await?;
27/// #
28/// # Ok(()) }) }
29/// ```
30pub async fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
31    let path = path.as_ref().to_owned();
32    spawn_blocking(move || {
33        std::fs::read_link(&path)
34            .map(Into::into)
35            .context(|| format!("could not read link `{}`", path.display()))
36    })
37    .await
38}