about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTim Chevalier <chevalier@alum.wellesley.edu>2013-04-25 17:18:25 -0700
committerTim Chevalier <chevalier@alum.wellesley.edu>2013-04-26 15:51:22 -0700
commit848641fcb54c282f6bd616ef1d6ac06087d778f6 (patch)
tree964d82b3abb16662ffb56f8e43c995999d4ed6b2
parent64412eca10399d64d25346d5f8220c1b1f88cd29 (diff)
downloadrust-848641fcb54c282f6bd616ef1d6ac06087d778f6.tar.gz
rust-848641fcb54c282f6bd616ef1d6ac06087d778f6.zip
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.
-rw-r--r--src/libcore/os.rs35
-rw-r--r--src/librustpkg/path_util.rs44
2 files changed, 40 insertions, 39 deletions
diff --git a/src/libcore/os.rs b/src/libcore/os.rs
index 62633837207..b81209d35cf 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 a8931e52747..0ebc33cf5bc 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));
-    }
-}