From 848641fcb54c282f6bd616ef1d6ac06087d778f6 Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Thu, 25 Apr 2013 17:18:25 -0700 Subject: [PATCH] core: Move mkdir_recursive from rustpkg into core::os mkdir_recursive creates a directory as well as any of its parent directories that don't exist already. Seems like a useful thing to have in core. --- src/libcore/os.rs | 35 +++++++++++++++++++++++++++++ src/librustpkg/path_util.rs | 44 +++++-------------------------------- 2 files changed, 40 insertions(+), 39 deletions(-) diff --git a/src/libcore/os.rs b/src/libcore/os.rs index 62633837207f9..b81209d35cf1d 100644 --- a/src/libcore/os.rs +++ b/src/libcore/os.rs @@ -639,6 +639,27 @@ pub fn make_dir(p: &Path, mode: c_int) -> bool { } } +/// Creates a directory with a given mode. +/// Returns true iff creation +/// succeeded. Also creates all intermediate subdirectories +/// if they don't already exist, giving all of them the same mode. +pub fn mkdir_recursive(p: &Path, mode: c_int) -> bool { + if path_is_dir(p) { + return true; + } + let parent = p.dir_path(); + debug!("mkdir_recursive: parent = %s", + parent.to_str()); + if parent.to_str() == ~"." + || parent.to_str() == ~"/" { // !!! + // No parent directories to create + path_is_dir(&parent) && make_dir(p, mode) + } + else { + mkdir_recursive(&parent, mode) && make_dir(p, mode) + } +} + /// Lists the contents of a directory #[allow(non_implicitly_copyable_typarams)] pub fn list_dir(p: &Path) -> ~[~str] { @@ -1467,4 +1488,18 @@ mod tests { assert!((remove_file(&out))); } } + + #[test] + fn recursive_mkdir_ok() { + use libc::consts::os::posix88::{S_IRUSR, S_IWUSR, S_IXUSR}; + + let root = os::tmpdir(); + let path = "xy/z/zy"; + let nested = root.push(path); + assert!(os::mkdir_recursive(&nested, (S_IRUSR | S_IWUSR | S_IXUSR) as i32)); + assert!(os::path_is_dir(&root.push("xy"))); + assert!(os::path_is_dir(&root.push("xy/z"))); + assert!(os::path_is_dir(&nested)); + } + } diff --git a/src/librustpkg/path_util.rs b/src/librustpkg/path_util.rs index a8931e52747d9..0ebc33cf5bc3e 100644 --- a/src/librustpkg/path_util.rs +++ b/src/librustpkg/path_util.rs @@ -14,6 +14,7 @@ use core::path::*; use core::{os, str}; use core::option::*; use util::PkgId; +use core::libc::consts::os::posix88::{S_IRUSR, S_IWUSR, S_IXUSR}; #[deriving(Eq)] pub enum OutputType { Main, Lib, Bench, Test } @@ -25,34 +26,15 @@ pub fn rust_path() -> ~[Path] { ~[Path(".")] } +static u_rwx: i32 = (S_IRUSR | S_IWUSR | S_IXUSR) as i32; + /// Creates a directory that is readable, writeable, /// and executable by the user. Returns true iff creation /// succeeded. pub fn make_dir_rwx(p: &Path) -> bool { use core::libc::consts::os::posix88::{S_IRUSR, S_IWUSR, S_IXUSR}; - os::make_dir(p, (S_IRUSR | S_IWUSR | S_IXUSR) as i32) -} - -/// Creates a directory that is readable, writeable, -/// and executable by the user. Returns true iff creation -/// succeeded. Also creates all intermediate subdirectories -/// if they don't already exist. -pub fn mkdir_recursive(p: &Path) -> bool { - if os::path_is_dir(p) { - return true; - } - let parent = p.dir_path(); - debug!("mkdir_recursive: parent = %s", - parent.to_str()); - if parent.to_str() == ~"." - || parent.to_str() == ~"/" { // !!! - // No parent directories to create - os::path_is_dir(&parent) && make_dir_rwx(p) - } - else { - mkdir_recursive(&parent) && make_dir_rwx(p) - } + os::make_dir(p, u_rwx) } /// Replace all occurrences of '-' in the stem part of path with '_' @@ -130,7 +112,7 @@ pub fn build_pkg_id_in_workspace(pkgid: PkgId, workspace: &Path) -> Path { // n.b. Should actually use a target-specific // subdirectory of build/ result = result.push(normalize(~pkgid.path).to_str()); - if os::path_exists(&result) || mkdir_recursive(&result) { + if os::path_exists(&result) || os::mkdir_recursive(&result, u_rwx) { result } else { @@ -148,19 +130,3 @@ pub fn mk_output_path(what: OutputType, short_name: ~str, dir: Path) -> Path { os::EXE_SUFFIX)) } } - -#[cfg(test)] -mod test { - use core::os; - - #[test] - fn recursive_mkdir_ok() { - let root = os::tmpdir(); - let path = "xy/z/zy"; - let nested = root.push(path); - assert!(super::mkdir_recursive(&nested)); - assert!(os::path_is_dir(&root.push("xy"))); - assert!(os::path_is_dir(&root.push("xy/z"))); - assert!(os::path_is_dir(&nested)); - } -}