@@ -152,6 +152,15 @@ pub struct Permissions(fs_imp::FilePermissions);
152
152
#[ derive( Copy , Clone , PartialEq , Eq , Hash ) ]
153
153
pub struct FileType ( fs_imp:: FileType ) ;
154
154
155
+ /// A builder used to create directories in various manners.
156
+ ///
157
+ /// This builder also supports platform-specific options.
158
+ #[ unstable( feature = "dir_builder" , reason = "recently added API" ) ]
159
+ pub struct DirBuilder {
160
+ inner : fs_imp:: DirBuilder ,
161
+ recursive : bool ,
162
+ }
163
+
155
164
impl File {
156
165
/// Attempts to open a file in read-only mode.
157
166
///
@@ -997,7 +1006,7 @@ pub fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
997
1006
/// ```
998
1007
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
999
1008
pub fn create_dir < P : AsRef < Path > > ( path : P ) -> io:: Result < ( ) > {
1000
- fs_imp :: mkdir ( path. as_ref ( ) )
1009
+ DirBuilder :: new ( ) . create ( path. as_ref ( ) )
1001
1010
}
1002
1011
1003
1012
/// Recursively create a directory and all of its parent components if they
@@ -1022,10 +1031,7 @@ pub fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
1022
1031
/// ```
1023
1032
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1024
1033
pub fn create_dir_all < P : AsRef < Path > > ( path : P ) -> io:: Result < ( ) > {
1025
- let path = path. as_ref ( ) ;
1026
- if path == Path :: new ( "" ) || path. is_dir ( ) { return Ok ( ( ) ) }
1027
- if let Some ( p) = path. parent ( ) { try!( create_dir_all ( p) ) }
1028
- create_dir ( path)
1034
+ DirBuilder :: new ( ) . recursive ( true ) . create ( path. as_ref ( ) )
1029
1035
}
1030
1036
1031
1037
/// Removes an existing, empty directory.
@@ -1286,6 +1292,52 @@ pub fn set_permissions<P: AsRef<Path>>(path: P, perm: Permissions) -> io::Result
1286
1292
fs_imp:: set_perm ( path. as_ref ( ) , perm. 0 )
1287
1293
}
1288
1294
1295
+ impl DirBuilder {
1296
+ /// Creates a new set of options with default mode/security settings for all
1297
+ /// platforms and also non-recursive.
1298
+ pub fn new ( ) -> DirBuilder {
1299
+ DirBuilder {
1300
+ inner : fs_imp:: DirBuilder :: new ( ) ,
1301
+ recursive : false ,
1302
+ }
1303
+ }
1304
+
1305
+ /// Indicate that directories create should be created recursively, creating
1306
+ /// all parent directories if they do not exist with the same security and
1307
+ /// permissions settings.
1308
+ ///
1309
+ /// This option defaults to `false`
1310
+ pub fn recursive ( & mut self , recursive : bool ) -> & mut Self {
1311
+ self . recursive = recursive;
1312
+ self
1313
+ }
1314
+
1315
+ /// Create the specified directory with the options configured in this
1316
+ /// builder.
1317
+ pub fn create < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < ( ) > {
1318
+ let path = path. as_ref ( ) ;
1319
+ if self . recursive {
1320
+ self . create_dir_all ( path)
1321
+ } else {
1322
+ self . inner . mkdir ( path)
1323
+ }
1324
+ }
1325
+
1326
+ fn create_dir_all ( & self , path : & Path ) -> io:: Result < ( ) > {
1327
+ if path == Path :: new ( "" ) || path. is_dir ( ) { return Ok ( ( ) ) }
1328
+ if let Some ( p) = path. parent ( ) {
1329
+ try!( self . create_dir_all ( p) )
1330
+ }
1331
+ self . inner . mkdir ( path)
1332
+ }
1333
+ }
1334
+
1335
+ impl AsInnerMut < fs_imp:: DirBuilder > for DirBuilder {
1336
+ fn as_inner_mut ( & mut self ) -> & mut fs_imp:: DirBuilder {
1337
+ & mut self . inner
1338
+ }
1339
+ }
1340
+
1289
1341
#[ cfg( test) ]
1290
1342
mod tests {
1291
1343
#![ allow( deprecated) ] //rand
0 commit comments