1111extern crate uucore;
1212
1313use clap:: { crate_version, App , AppSettings , Arg , ArgMatches , OsValues } ;
14- use std:: fs;
1514use std:: path:: Path ;
1615use uucore:: display:: Quotable ;
17- use uucore:: error:: { FromIo , UResult , USimpleError } ;
16+ #[ cfg( not( windows) ) ]
17+ use uucore:: error:: FromIo ;
18+ use uucore:: error:: { UResult , USimpleError } ;
1819#[ cfg( not( windows) ) ]
1920use uucore:: mode;
2021use uucore:: { format_usage, InvalidEncodingHandling } ;
@@ -149,22 +150,7 @@ fn exec(dirs: OsValues, recursive: bool, mode: u32, verbose: bool) -> UResult<()
149150}
150151
151152fn mkdir ( path : & Path , recursive : bool , mode : u32 , verbose : bool ) -> UResult < ( ) > {
152- let create_dir = if recursive {
153- fs:: create_dir_all
154- } else {
155- fs:: create_dir
156- } ;
157-
158- create_dir ( path) . map_err_context ( || format ! ( "cannot create directory {}" , path. quote( ) ) ) ?;
159-
160- if verbose {
161- println ! (
162- "{}: created directory {}" ,
163- uucore:: util_name( ) ,
164- path. quote( )
165- ) ;
166- }
167-
153+ create_dir ( path, recursive, verbose) ?;
168154 chmod ( path, mode)
169155}
170156
@@ -184,3 +170,39 @@ fn chmod(_path: &Path, _mode: u32) -> UResult<()> {
184170 // chmod on Windows only sets the readonly flag, which isn't even honored on directories
185171 Ok ( ( ) )
186172}
173+
174+ fn create_dir ( path : & Path , recursive : bool , verbose : bool ) -> UResult < ( ) > {
175+ if path. exists ( ) && !recursive {
176+ return Err ( USimpleError :: new (
177+ 1 ,
178+ format ! ( "{}: File exists" , path. display( ) ) ,
179+ ) ) ;
180+ }
181+ if path == Path :: new ( "" ) {
182+ return Ok ( ( ) ) ;
183+ }
184+
185+ if recursive {
186+ match path. parent ( ) {
187+ Some ( p) => create_dir ( p, recursive, verbose) ?,
188+ None => {
189+ USimpleError :: new ( 1 , "failed to create whole tree" ) ;
190+ }
191+ }
192+ }
193+
194+ match std:: fs:: create_dir ( path) {
195+ Ok ( ( ) ) => {
196+ if verbose {
197+ println ! (
198+ "{}: created directory {}" ,
199+ uucore:: util_name( ) ,
200+ path. quote( )
201+ ) ;
202+ }
203+ Ok ( ( ) )
204+ }
205+ Err ( _) if path. is_dir ( ) => Ok ( ( ) ) ,
206+ Err ( e) => Err ( e. into ( ) ) ,
207+ }
208+ }
0 commit comments