Skip to content

Commit c058784

Browse files
authored
fix(docs): create_dir_all doesn't error if path already exists
1 parent 5fda2aa commit c058784

File tree

1 file changed

+55
-19
lines changed

1 file changed

+55
-19
lines changed

src/lib.rs

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -114,48 +114,84 @@ pub async fn copy<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<
114114
unblock(move || std::fs::copy(&src, &dst)).await
115115
}
116116

117-
/// Creates a directory.
117+
/// Creates a new, empty directory at the provided path
118118
///
119-
/// Note that this function will only create the final directory in `path`. If you want to create
120-
/// all of its missing parent directories too, use [`create_dir_all()`] instead.
119+
/// # Platform-specific behavior
120+
///
121+
/// This function currently corresponds to the `mkdir` function on Unix
122+
/// and the `CreateDirectory` function on Windows.
123+
/// Note that, this [may change in the future][changes].
124+
///
125+
/// [changes]: io#platform-specific-behavior
126+
///
127+
/// **NOTE**: If a parent of the given path doesn't exist, this function will
128+
/// return an error. To create a directory and all its missing parents at the
129+
/// same time, use the [`create_dir_all`] function.
121130
///
122131
/// # Errors
123132
///
124-
/// An error will be returned in the following situations:
133+
/// This function will return an error in the following situations, but is not
134+
/// limited to just these cases:
125135
///
126-
/// * `path` already points to an existing file or directory.
127-
/// * A parent directory in `path` does not exist.
128-
/// * The current process lacks permissions to create the directory.
129-
/// * Some other I/O error occurred.
136+
/// * User lacks permissions to create directory at `path`.
137+
/// * A parent of the given path doesn't exist. (To create a directory and all
138+
/// its missing parents at the same time, use the [`create_dir_all`]
139+
/// function.)
140+
/// * `path` already exists.
130141
///
131142
/// # Examples
132143
///
133144
/// ```no_run
134-
/// # futures_lite::future::block_on(async {
135-
/// async_fs::create_dir("./some/directory").await?;
136-
/// # std::io::Result::Ok(()) });
145+
/// use std::fs;
146+
///
147+
/// fn main() -> std::io::Result<()> {
148+
/// fs::create_dir("/some/dir")?;
149+
/// Ok(())
150+
/// }
137151
/// ```
138152
pub async fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
139153
let path = path.as_ref().to_owned();
140154
unblock(move || std::fs::create_dir(path)).await
141155
}
142156

143-
/// Creates a directory and its parent directories if they are missing.
157+
/// Recursively create a directory and all of its parent components if they
158+
/// are missing.
159+
///
160+
/// # Platform-specific behavior
161+
///
162+
/// This function currently corresponds to the `mkdir` function on Unix
163+
/// and the `CreateDirectory` function on Windows.
164+
/// Note that, this [may change in the future][changes].
165+
///
166+
/// [changes]: io#platform-specific-behavior
144167
///
145168
/// # Errors
146169
///
147-
/// An error will be returned in the following situations:
170+
/// This function will return an error in the following situations, but is not
171+
/// limited to just these cases:
148172
///
149-
/// * `path` already points to an existing file or directory.
150-
/// * The current process lacks permissions to create the directory or its missing parents.
151-
/// * Some other I/O error occurred.
173+
/// * If any directory in the path specified by `path`
174+
/// does not already exist and it could not be created otherwise. The specific
175+
/// error conditions for when a directory is being created (after it is
176+
/// determined to not exist) are outlined by [`fs::create_dir`].
177+
///
178+
/// Notable exception is made for situations where any of the directories
179+
/// specified in the `path` could not be created as it was being created concurrently.
180+
/// Such cases are considered to be successful. That is, calling `create_dir_all`
181+
/// concurrently from multiple threads or processes is guaranteed not to fail
182+
/// due to a race condition with itself.
183+
///
184+
/// [`fs::create_dir`]: create_dir
152185
///
153186
/// # Examples
154187
///
155188
/// ```no_run
156-
/// # futures_lite::future::block_on(async {
157-
/// async_fs::create_dir_all("./some/directory").await?;
158-
/// # std::io::Result::Ok(()) });
189+
/// use std::fs;
190+
///
191+
/// fn main() -> std::io::Result<()> {
192+
/// fs::create_dir_all("/some/dir")?;
193+
/// Ok(())
194+
/// }
159195
/// ```
160196
pub async fn create_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
161197
let path = path.as_ref().to_owned();

0 commit comments

Comments
 (0)