-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
fs: add support for DirBuilder and DirBuilderExt #2524
Changes from 1 commit
0321531
e73a9b2
384d185
4d137fa
22bc3e9
aeb8bcf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,137 @@ | ||||||||||
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 | ||||||||||
/// [os::unix::fs::DirBuilderExt] trait. | ||||||||||
/// | ||||||||||
/// This is a specialized version of [`std::fs::DirBuilder`] for usage from | ||||||||||
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.
Suggested change
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. Thanks! Fixed |
||||||||||
/// the Tokio runtime. | ||||||||||
/// | ||||||||||
/// [os::unix::fs::DirBuilderExt]: ../os/unix/fs/trait.DirBuilderExt.html | ||||||||||
/// [std::fs::DirBuilder]: https://doc.rust-lang.org/std/fs/struct.DirBuilder.html | ||||||||||
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 should use intra-rustdoc links, so that broken links are automatically detected.
Suggested change
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. |
||||||||||
#[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)] | ||||||||||
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() -> DirBuilder { | ||||||||||
#[cfg(not(unix))] | ||||||||||
let builder = DirBuilder { recursive: false }; | ||||||||||
|
||||||||||
#[cfg(unix)] | ||||||||||
let builder = DirBuilder { | ||||||||||
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. We can just use the pub fn new() -> Self {
Default::default()
} 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. Thank you, switched to the |
||||||||||
recursive: false, | ||||||||||
mode: None, | ||||||||||
}; | ||||||||||
|
||||||||||
builder | ||||||||||
} | ||||||||||
|
||||||||||
/// 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 { | ||||||||||
Darksonn marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
std::os::unix::fs::DirBuilderExt::mode(&mut builder, mode); | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
asyncify(move || builder.create(path)).await | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
cfg_unix! { | ||||||||||
use std::os::unix::fs::DirBuilderExt; | ||||||||||
|
||||||||||
impl DirBuilderExt for DirBuilder { | ||||||||||
fn mode(&mut self, mode: u32) -> &mut Self { | ||||||||||
self.mode = Some(mode); | ||||||||||
self | ||||||||||
} | ||||||||||
} | ||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -354,6 +354,15 @@ macro_rules! cfg_uds { | |
} | ||
} | ||
|
||
macro_rules! cfg_unix { | ||
($($item:item)*) => { | ||
$( | ||
#[cfg(unix)] | ||
$item | ||
)* | ||
} | ||
} | ||
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. We use 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. |
||
|
||
macro_rules! cfg_unstable { | ||
($($item:item)*) => { | ||
$( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we change the link text to [`DirBuilderExt`]? Note that the link is broken.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.