| 1 | use crate::io; |
| 2 | use crate::path::{Path, PathBuf}; |
| 3 | use crate::task::spawn_blocking; |
| 4 | use crate::utils::Context as _; |
| 5 | |
| 6 | /// Returns the canonical form of a path. |
| 7 | /// |
| 8 | /// The returned path is in absolute form with all intermediate components normalized and symbolic |
| 9 | /// links resolved. |
| 10 | /// |
| 11 | /// This function is an async version of [`std::fs::canonicalize`]. |
| 12 | /// |
| 13 | /// [`std::fs::canonicalize`]: https://doc.rust-lang.org/std/fs/fn.canonicalize.html |
| 14 | /// |
| 15 | /// # Errors |
| 16 | /// |
| 17 | /// An error will be returned in the following situations: |
| 18 | /// |
| 19 | /// * `path` does not point to an existing file or directory. |
| 20 | /// * A non-final component in `path` is not a directory. |
| 21 | /// * Some other I/O error occurred. |
| 22 | /// |
| 23 | /// # Examples |
| 24 | /// |
| 25 | /// ```no_run |
| 26 | /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { |
| 27 | /// # |
| 28 | /// use async_std::fs; |
| 29 | /// |
| 30 | /// let path = fs::canonicalize("." ).await?; |
| 31 | /// # |
| 32 | /// # Ok(()) }) } |
| 33 | /// ``` |
| 34 | pub async fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> { |
| 35 | let path: PathBuf = path.as_ref().to_owned(); |
| 36 | spawn_blockingJoinHandle>(move || { |
| 37 | std::fs::canonicalize(&path) |
| 38 | .map(Into::into) |
| 39 | .context(|| format!("could not canonicalize ` {}`" , path.display())) |
| 40 | }) |
| 41 | .await |
| 42 | } |
| 43 | |