pub fn ensure_dir(dir: &OsStr, dir_type: &str, mode: Mode) {
    let mut d = dir.as_bytes();
    // @TODO Better than doing this manually is there a method that will clean the dir up?
    // There might be other things that need to be done like removing repeated slashes (`/`) etc.
    //
    // Remove any trailing slashes
    while d.len() > 0 && d[d.len() - 1] == b'/' {
        d = &d[0..d.len() - 1];
    }

    let st: FileStat = match stat(d) {
        Err(e) => {
            if errno() != ENOENT {
                fatal!("Error accessing {} {:?}: {:?}", dir_type, dir, e);
            }

            let last_slash = d.iter().enumerate().rfind(|c| *c.1 == b'/');
            match last_slash {
                Some(pos) if pos.0 > 0 => {
                    ensure_dir(OsStr::from_bytes(&d[0..pos.0]), dir_type, mode);
                }
                _ => {
                    fatal!("Can't find directory {:?}", dir);
                }
            }

            // Allow for a race condition where someone else creates the directory
            match mkdir(d, mode) {
                Err(e) if errno() != EEXIST => {
                    fatal!("Can't create {} {:?}: {:?}", dir_type, dir, e)
                }
                _ => (),
            }

            match stat(d) {
                Err(e) => {
                    fatal!("Can't stat {} {:?}: {:?}", dir_type, dir, e);
                }
                Ok(st) => st,
            }
        }
        Ok(st) => st,
    };

    if !(S_IFDIR & st.st_mode == S_IFDIR) {
        fatal!("{:?} exists but isn't a directory.", dir);
    }

    match access(d, AccessFlags::W_OK) {
        Err(e) => fatal!("Can't write to {} {:?}: {:?}", dir_type, dir, e),
        Ok(_) => (),
    }
}
/// Create directory `dir`, creating parent directories as needed.
/// `dir_type` is printed in error messages. Fails if the resulting directory
/// is not writeable.
Create directory `dir`, creating parent directories as needed.
`dir_type` is printed in error messages. Fails if the resulting directory
is not writeable.
Create directory ` dir ` creating parent directories as needed . ` dir_type ` is printed in error messages . Fails if the resulting directory is not writeable .
pub fn ensure_dir(dir: &OsStr, dir_type: &str, mode: Mode) {
    let mut d = dir.as_bytes();
    // @TODO Better than doing this manually is there a method that will clean the dir up?
    // There might be other things that need to be done like removing repeated slashes (`/`) etc.
    //
    // Remove any trailing slashes
    while d.len() > 0 && d[d.len() - 1] == b'/' {
        d = &d[0..d.len() - 1];
    }
    let st: FileStat = match stat(d) {
        Err(e) => {
            if errno() != ENOENT {
                fatal!("Error accessing {} {:?}: {:?}", dir_type, dir, e);
            }
            let last_slash = d.iter().enumerate().rfind(|c| *c.1 == b'/');
            match last_slash {
                Some(pos) if pos.0 > 0 => {
                    ensure_dir(OsStr::from_bytes(&d[0..pos.0]), dir_type, mode);
                }
                _ => {
                    fatal!("Can't find directory {:?}", dir);
                }
            }
            // Allow for a race condition where someone else creates the directory
            match mkdir(d, mode) {
                Err(e) if errno() != EEXIST => {
                    fatal!("Can't create {} {:?}: {:?}", dir_type, dir, e)
                }
                _ => (),
            }
            match stat(d) {
                Err(e) => {
                    fatal!("Can't stat {} {:?}: {:?}", dir_type, dir, e);
                }
                Ok(st) => st,
            }
        }
        Ok(st) => st,
    };
    if !(S_IFDIR & st.st_mode == S_IFDIR) {
        fatal!("{:?} exists but isn't a directory.", dir);
    }
    match access(d, AccessFlags::W_OK) {
        Err(e) => fatal!("Can't write to {} {:?}: {:?}", dir_type, dir, e),
        Ok(_) => (),
    }
}
pub fn ensure_dir ( dir : & OsStr , dir_type : & str , mode : Mode ) { let mut d = dir . as_bytes ( ) ; // @TODO Better than doing this manually is there a method that will clean the dir up? // There might be other things that need to be done like removing repeated slashes (`/`) etc. // // Remove any trailing slashes while d . len ( ) > 0 && d [ d . len ( ) - 1 ] == b'/' { d = & d [ 0 .. d . len ( ) - 1 ] ; } let st : FileStat = match stat ( d ) { Err ( e ) => { if errno ( ) != ENOENT { fatal ! ( " " dir_type dir e ) ; } let last_slash = d . iter ( ) . enumerate ( ) . rfind ( | c | * c . 1 == b'/' ) ; match last_slash { Some ( pos ) if pos . 0 > 0 => { ensure_dir ( OsStr :: from_bytes ( & d [ 0 .. pos . 0 ] ) , dir_type , mode ) ; } _ => { fatal ! ( " " dir ) ; } } // Allow for a race condition where someone else creates the directory match mkdir ( d , mode ) { Err ( e ) if errno ( ) != EEXIST => { fatal ! ( " " dir_type dir e ) } _ => ( ) , } match stat ( d ) { Err ( e ) => { fatal ! ( " " dir_type dir e ) ; } Ok ( st ) => st , } } Ok ( st ) => st , } ; if ! ( S_IFDIR & st . st_mode == S_IFDIR ) { fatal ! ( " " dir ) ; } match access ( d , AccessFlags :: W_OK ) { Err ( e ) => fatal ! ( " " dir_type dir e ) , Ok ( _ ) => ( ) , } }
Create directory `dir`, creating parent directories as needed.
Create directory ` dir ` creating parent directories as needed .
dir
&OsStr
dir_type
&str
mode
Mode