Skip to main content

tokio/fs/
create_dir_all.rs

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/// If the final component at `path` already exists and is a directory, this
30/// function will also return successfully without error.
31///
32/// Notable exception is made for situations where any of the directories
33/// specified in the `path` could not be created as it was being created concurrently.
34/// Such cases are considered to be successful. That is, calling `create_dir_all`
35/// concurrently from multiple threads or processes is guaranteed not to fail
36/// due to a race condition with itself.
37///
38/// [`fs::create_dir`]: std::fs::create_dir
39///
40/// # Examples
41///
42/// ```no_run
43/// use tokio::fs;
44///
45/// #[tokio::main]
46/// async fn main() -> std::io::Result<()> {
47///     fs::create_dir_all("/some/dir").await?;
48///     Ok(())
49/// }
50/// ```
51pub async fn create_dir_all(path: impl AsRef<Path>) -> io::Result<()> {
52    let path = path.as_ref().to_owned();
53    asyncify(move || std::fs::create_dir_all(path)).await
54}