-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
fs: add support for DirBuilder and DirBuilderExt #2524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0321531
fs: add support for DirBuilder and DirBuilderExt
shkurski e73a9b2
fix documentation errors
shkurski 384d185
remove DirBuilderExt trait implementation and simplify code
shkurski 4d137fa
implement tokio::fs::os::unix::DirBuilderExt trait
shkurski 22bc3e9
poke ci
Darksonn aeb8bcf
Fix DirBuilderExt link
Darksonn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
use crate::fs::asyncify; | ||
|
||
use std::io; | ||
use std::path::Path; | ||
|
||
/// A builder for creating directories in various manners. | ||
/// | ||
/// Additional Unix-specific options are available via importing the | ||
/// [`DirBuilderExt`] trait. | ||
/// | ||
/// This is a specialized version of [`std::fs::DirBuilder`] for usage on | ||
/// the Tokio runtime. | ||
/// | ||
/// [std::fs::DirBuilder]: std::fs::DirBuilder | ||
/// [`DirBuilderExt`]: crate::fs::os::unix::DirBuilderExt | ||
#[derive(Debug, Default)] | ||
pub struct DirBuilder { | ||
/// Indicates whether to create parent directories if they are missing. | ||
recursive: bool, | ||
|
||
/// Set the Unix mode for newly created directories. | ||
#[cfg(unix)] | ||
pub(super) mode: Option<u32>, | ||
} | ||
|
||
impl DirBuilder { | ||
/// Creates a new set of options with default mode/security settings for all | ||
/// platforms and also non-recursive. | ||
/// | ||
/// This is an async version of [`std::fs::DirBuilder::new`][std] | ||
/// | ||
/// [std]: std::fs::DirBuilder::new | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// use tokio::fs::DirBuilder; | ||
/// | ||
/// let builder = DirBuilder::new(); | ||
/// ``` | ||
pub fn new() -> Self { | ||
Default::default() | ||
} | ||
|
||
/// Indicates whether to create directories recursively (including all parent directories). | ||
/// Parents that do not exist are created with the same security and permissions settings. | ||
/// | ||
/// This option defaults to `false`. | ||
/// | ||
/// This is an async version of [`std::fs::DirBuilder::recursive`][std] | ||
/// | ||
/// [std]: std::fs::DirBuilder::recursive | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// use tokio::fs::DirBuilder; | ||
/// | ||
/// let mut builder = DirBuilder::new(); | ||
/// builder.recursive(true); | ||
/// ``` | ||
pub fn recursive(&mut self, recursive: bool) -> &mut Self { | ||
self.recursive = recursive; | ||
self | ||
} | ||
|
||
/// Creates the specified directory with the configured options. | ||
/// | ||
/// It is considered an error if the directory already exists unless | ||
/// recursive mode is enabled. | ||
/// | ||
/// This is an async version of [`std::fs::DirBuilder::create`][std] | ||
/// | ||
/// [std]: std::fs::DirBuilder::create | ||
/// | ||
/// # Errors | ||
/// | ||
/// An error will be returned under the following circumstances: | ||
/// | ||
/// * Path already points to an existing file. | ||
/// * Path already points to an existing directory and the mode is | ||
/// non-recursive. | ||
/// * The calling process doesn't have permissions to create the directory | ||
/// or its missing parents. | ||
/// * Other I/O error occurred. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// use tokio::fs::DirBuilder; | ||
/// use std::io; | ||
/// | ||
/// #[tokio::main] | ||
/// async fn main() -> io::Result<()> { | ||
/// DirBuilder::new() | ||
/// .recursive(true) | ||
/// .create("/tmp/foo/bar/baz") | ||
/// .await?; | ||
/// | ||
/// Ok(()) | ||
/// } | ||
/// ``` | ||
pub async fn create<P: AsRef<Path>>(&self, path: P) -> io::Result<()> { | ||
let path = path.as_ref().to_owned(); | ||
let mut builder = std::fs::DirBuilder::new(); | ||
builder.recursive(self.recursive); | ||
|
||
#[cfg(unix)] | ||
{ | ||
if let Some(mode) = self.mode { | ||
std::os::unix::fs::DirBuilderExt::mode(&mut builder, mode); | ||
} | ||
} | ||
|
||
asyncify(move || builder.create(path)).await | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use crate::fs::dir_builder::DirBuilder; | ||
|
||
/// Unix-specific extensions to [`DirBuilderExt`]. | ||
/// | ||
/// [crate::fs::os::unix::DirBuilderExt]: DirBuilderExt | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This link seems off. Shouldn't it link to |
||
pub trait DirBuilderExt { | ||
/// Sets the mode to create new directories with. | ||
/// | ||
/// This option defaults to 0o777. | ||
/// | ||
/// # Examples | ||
/// | ||
/// | ||
/// ```no_run | ||
/// use tokio::fs::DirBuilder; | ||
/// use tokio::fs::os::unix::DirBuilderExt; | ||
/// | ||
/// let mut builder = DirBuilder::new(); | ||
/// builder.mode(0o775); | ||
/// ``` | ||
fn mode(&mut self, mode: u32) -> &mut Self; | ||
} | ||
|
||
impl DirBuilderExt for DirBuilder { | ||
fn mode(&mut self, mode: u32) -> &mut Self { | ||
self.mode = Some(mode); | ||
self | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,6 @@ | |
|
||
mod symlink; | ||
pub use self::symlink::symlink; | ||
|
||
mod dir_builder_ext; | ||
pub use self::dir_builder_ext::DirBuilderExt; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.