broker_tokio/fs/
canonicalize.rs

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