1 | use crate::io; |
2 | use crate::path::{Path, PathBuf}; |
3 | use crate::task::spawn_blocking; |
4 | use 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 | /// ``` |
30 | pub async fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> { |
31 | let path: PathBuf = path.as_ref().to_owned(); |
32 | spawn_blockingJoinHandle>(move || { |
33 | std::fs::read_link(&path) |
34 | .map(Into::into) |
35 | .context(|| format!("could not read link ` {}`" , path.display())) |
36 | }) |
37 | .await |
38 | } |
39 | |