|
| 1 | +mod unix; |
| 2 | + |
| 3 | +use std::{io, os::unix::fs::DirBuilderExt, path::Path}; |
| 4 | + |
| 5 | +#[cfg(unix)] |
| 6 | +use unix as sys; |
| 7 | + |
| 8 | +/// A builder used to create directories in various manners. |
| 9 | +/// |
| 10 | +/// This builder also supports platform-specific options. |
| 11 | +pub struct DirBuilder { |
| 12 | + recursive: bool, |
| 13 | + inner: sys::BuilderInner, |
| 14 | +} |
| 15 | + |
| 16 | +impl DirBuilder { |
| 17 | + /// Creates a new set of options with default mode/security settings for all |
| 18 | + /// platforms and also non-recursive. |
| 19 | + /// |
| 20 | + /// This an async version of [`std::fs::DirBuilder::new`] |
| 21 | + /// |
| 22 | + /// # Examples |
| 23 | + /// |
| 24 | + /// ```no_run |
| 25 | + /// use monoio::fs::DirBuilder; |
| 26 | + /// |
| 27 | + /// let builder = DirBuilder::new(); |
| 28 | + /// ``` |
| 29 | + pub fn new() -> Self { |
| 30 | + Self { |
| 31 | + recursive: false, |
| 32 | + inner: sys::BuilderInner::new(), |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /// Indicates that directories should be created recursively, creating all |
| 37 | + /// parent directories. Parents that do not exist are created with the same |
| 38 | + /// security and permissions settings. |
| 39 | + /// |
| 40 | + /// This option defaults to `false`. |
| 41 | + /// |
| 42 | + /// This an async version of [`std::fs::DirBuilder::recursive`] |
| 43 | + /// |
| 44 | + /// # Examples |
| 45 | + /// |
| 46 | + /// ```no_run |
| 47 | + /// use monoio::fs::DirBuilder; |
| 48 | + /// |
| 49 | + /// let mut builder = DirBuilder::new(); |
| 50 | + /// builder.recursive(true); |
| 51 | + /// ``` |
| 52 | + pub fn recursive(&mut self, recursive: bool) -> &mut Self { |
| 53 | + self.recursive = recursive; |
| 54 | + self |
| 55 | + } |
| 56 | + |
| 57 | + /// Creates the specified directory with the options configured in this |
| 58 | + /// builder. |
| 59 | + /// |
| 60 | + /// It is considered an error if the directory already exists unless |
| 61 | + /// recursive mode is enabled. |
| 62 | + /// |
| 63 | + /// This is async version of [`std::fs::DirBuilder::create`] and use io-uring |
| 64 | + /// in support platform. |
| 65 | + /// |
| 66 | + /// # Errors |
| 67 | + /// |
| 68 | + /// An error will be returned under the following circumstances: |
| 69 | + /// |
| 70 | + /// * Path already points to an existing file. |
| 71 | + /// * Path already points to an existing directory and the mode is non-recursive. |
| 72 | + /// * The calling process doesn't have permissions to create the directory or its missing |
| 73 | + /// parents. |
| 74 | + /// * Other I/O error occurred. |
| 75 | + /// |
| 76 | + /// # Examples |
| 77 | + /// |
| 78 | + /// ```no_run |
| 79 | + /// use monoio::fs::DirBuilder; |
| 80 | + /// |
| 81 | + /// #[monoio::main] |
| 82 | + /// async fn main() -> std::io::Result<()> { |
| 83 | + /// DirBuilder::new() |
| 84 | + /// .recursive(true) |
| 85 | + /// .create("/some/dir") |
| 86 | + /// .await?; |
| 87 | + /// |
| 88 | + /// Ok(()) |
| 89 | + /// } |
| 90 | + /// ``` |
| 91 | + pub async fn create<P: AsRef<Path>>(&self, path: P) -> io::Result<()> { |
| 92 | + if self.recursive { |
| 93 | + self.create_dir_all(path.as_ref()).await |
| 94 | + } else { |
| 95 | + self.inner.mkdir(path.as_ref()).await |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + async fn create_dir_all(&self, path: &Path) -> io::Result<()> { |
| 100 | + if path == Path::new("") { |
| 101 | + return Ok(()); |
| 102 | + } |
| 103 | + |
| 104 | + let mut inexist_path = path; |
| 105 | + let mut need_create = vec![]; |
| 106 | + |
| 107 | + while match self.inner.mkdir(inexist_path).await { |
| 108 | + Ok(()) => false, |
| 109 | + Err(ref e) if e.kind() == io::ErrorKind::NotFound => true, |
| 110 | + Err(_) if is_dir(inexist_path).await => false, |
| 111 | + Err(e) => return Err(e), |
| 112 | + } { |
| 113 | + match inexist_path.parent() { |
| 114 | + Some(p) => { |
| 115 | + need_create.push(inexist_path); |
| 116 | + inexist_path = p; |
| 117 | + } |
| 118 | + None => { |
| 119 | + return Err(io::Error::new( |
| 120 | + io::ErrorKind::Other, |
| 121 | + "failed to create whole tree", |
| 122 | + )) |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + for p in need_create.into_iter().rev() { |
| 128 | + self.inner.mkdir(p).await?; |
| 129 | + } |
| 130 | + |
| 131 | + Ok(()) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +impl Default for DirBuilder { |
| 136 | + fn default() -> Self { |
| 137 | + Self::new() |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +impl DirBuilderExt for DirBuilder { |
| 142 | + fn mode(&mut self, mode: u32) -> &mut Self { |
| 143 | + self.inner.set_mode(mode); |
| 144 | + self |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +// currently, will use the std version of metadata, will change to use the io-uring version |
| 149 | +// when the statx is merge |
| 150 | +async fn is_dir(path: &Path) -> bool { |
| 151 | + std::fs::metadata(path).is_ok_and(|metadata| metadata.is_dir()) |
| 152 | +} |
0 commit comments