1 | use crate::fs::asyncify; |
2 | |
3 | use std::io; |
4 | use std::path::{Path, PathBuf}; |
5 | |
6 | /// Returns the canonical, absolute form of a path with all intermediate |
7 | /// components normalized and symbolic links resolved. |
8 | /// |
9 | /// This is an async version of [`std::fs::canonicalize`]. |
10 | /// |
11 | /// # Platform-specific behavior |
12 | /// |
13 | /// This function currently corresponds to the `realpath` function on Unix |
14 | /// and the `CreateFile` and `GetFinalPathNameByHandle` functions on Windows. |
15 | /// Note that, this [may change in the future][changes]. |
16 | /// |
17 | /// On Windows, this converts the path to use [extended length path][path] |
18 | /// syntax, which allows your program to use longer path names, but means you |
19 | /// can only join backslash-delimited paths to it, and it may be incompatible |
20 | /// with other applications (if passed to the application on the command-line, |
21 | /// or written to a file another application may read). |
22 | /// |
23 | /// [changes]: https://doc.rust-lang.org/std/io/index.html#platform-specific-behavior |
24 | /// [path]: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#maxpath |
25 | /// |
26 | /// # Errors |
27 | /// |
28 | /// This function will return an error in the following situations, but is not |
29 | /// limited to just these cases: |
30 | /// |
31 | /// * `path` does not exist. |
32 | /// * A non-final component in path is not a directory. |
33 | /// |
34 | /// # Examples |
35 | /// |
36 | /// ```no_run |
37 | /// use tokio::fs; |
38 | /// use std::io; |
39 | /// |
40 | /// #[tokio::main] |
41 | /// async fn main() -> io::Result<()> { |
42 | /// let path = fs::canonicalize("../a/../foo.txt" ).await?; |
43 | /// Ok(()) |
44 | /// } |
45 | /// ``` |
46 | pub async fn canonicalize(path: impl AsRef<Path>) -> io::Result<PathBuf> { |
47 | let path = path.as_ref().to_owned(); |
48 | asyncify(move || std::fs::canonicalize(path)).await |
49 | } |
50 | |