1use crate::fs::asyncify;
2
3use std::io;
4use std::path::Path;
5
6/// Recursively creates a directory and all of its parent components if they
7/// are missing.
8///
9/// This is an async version of [`std::fs::create_dir_all`].
10///
11/// # Platform-specific behavior
12///
13/// This function currently corresponds to the `mkdir` function on Unix
14/// and the `CreateDirectory` function on Windows.
15/// Note that, this [may change in the future][changes].
16///
17/// [changes]: https://doc.rust-lang.org/std/io/index.html#platform-specific-behavior
18///
19/// # Errors
20///
21/// This function will return an error in the following situations, but is not
22/// limited to just these cases:
23///
24/// * If any directory in the path specified by `path` does not already exist
25/// and it could not be created otherwise. The specific error conditions for
26/// when a directory is being created (after it is determined to not exist) are
27/// outlined by [`fs::create_dir`].
28///
29/// Notable exception is made for situations where any of the directories
30/// specified in the `path` could not be created as it was being created concurrently.
31/// Such cases are considered to be successful. That is, calling `create_dir_all`
32/// concurrently from multiple threads or processes is guaranteed not to fail
33/// due to a race condition with itself.
34///
35/// [`fs::create_dir`]: std::fs::create_dir
36///
37/// # Examples
38///
39/// ```no_run
40/// use tokio::fs;
41///
42/// #[tokio::main]
43/// async fn main() -> std::io::Result<()> {
44/// fs::create_dir_all("/some/dir").await?;
45/// Ok(())
46/// }
47/// ```
48pub async fn create_dir_all(path: impl AsRef<Path>) -> io::Result<()> {
49 let path = path.as_ref().to_owned();
50 asyncify(move || std::fs::create_dir_all(path)).await
51}
52