Skip to content

Commit dd5b1de

Browse files
committed
auto merge of #6082 : catamorphism/rust/mkdir_recursive, r=brson
r? @brson 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. (Or r? anyone who gets to it first.)
2 parents 88dd53a + 848641f commit dd5b1de

File tree

2 files changed

+40
-39
lines changed

2 files changed

+40
-39
lines changed

src/libcore/os.rs

+35
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,27 @@ pub fn make_dir(p: &Path, mode: c_int) -> bool {
639639
}
640640
}
641641

642+
/// Creates a directory with a given mode.
643+
/// Returns true iff creation
644+
/// succeeded. Also creates all intermediate subdirectories
645+
/// if they don't already exist, giving all of them the same mode.
646+
pub fn mkdir_recursive(p: &Path, mode: c_int) -> bool {
647+
if path_is_dir(p) {
648+
return true;
649+
}
650+
let parent = p.dir_path();
651+
debug!("mkdir_recursive: parent = %s",
652+
parent.to_str());
653+
if parent.to_str() == ~"."
654+
|| parent.to_str() == ~"/" { // !!!
655+
// No parent directories to create
656+
path_is_dir(&parent) && make_dir(p, mode)
657+
}
658+
else {
659+
mkdir_recursive(&parent, mode) && make_dir(p, mode)
660+
}
661+
}
662+
642663
/// Lists the contents of a directory
643664
#[allow(non_implicitly_copyable_typarams)]
644665
pub fn list_dir(p: &Path) -> ~[~str] {
@@ -1467,4 +1488,18 @@ mod tests {
14671488
assert!((remove_file(&out)));
14681489
}
14691490
}
1491+
1492+
#[test]
1493+
fn recursive_mkdir_ok() {
1494+
use libc::consts::os::posix88::{S_IRUSR, S_IWUSR, S_IXUSR};
1495+
1496+
let root = os::tmpdir();
1497+
let path = "xy/z/zy";
1498+
let nested = root.push(path);
1499+
assert!(os::mkdir_recursive(&nested, (S_IRUSR | S_IWUSR | S_IXUSR) as i32));
1500+
assert!(os::path_is_dir(&root.push("xy")));
1501+
assert!(os::path_is_dir(&root.push("xy/z")));
1502+
assert!(os::path_is_dir(&nested));
1503+
}
1504+
14701505
}

src/librustpkg/path_util.rs

+5-39
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use core::path::*;
1414
use core::{os, str};
1515
use core::option::*;
1616
use util::PkgId;
17+
use core::libc::consts::os::posix88::{S_IRUSR, S_IWUSR, S_IXUSR};
1718

1819
#[deriving(Eq)]
1920
pub enum OutputType { Main, Lib, Bench, Test }
@@ -25,34 +26,15 @@ pub fn rust_path() -> ~[Path] {
2526
~[Path(".")]
2627
}
2728

29+
static u_rwx: i32 = (S_IRUSR | S_IWUSR | S_IXUSR) as i32;
30+
2831
/// Creates a directory that is readable, writeable,
2932
/// and executable by the user. Returns true iff creation
3033
/// succeeded.
3134
pub fn make_dir_rwx(p: &Path) -> bool {
3235
use core::libc::consts::os::posix88::{S_IRUSR, S_IWUSR, S_IXUSR};
3336

34-
os::make_dir(p, (S_IRUSR | S_IWUSR | S_IXUSR) as i32)
35-
}
36-
37-
/// Creates a directory that is readable, writeable,
38-
/// and executable by the user. Returns true iff creation
39-
/// succeeded. Also creates all intermediate subdirectories
40-
/// if they don't already exist.
41-
pub fn mkdir_recursive(p: &Path) -> bool {
42-
if os::path_is_dir(p) {
43-
return true;
44-
}
45-
let parent = p.dir_path();
46-
debug!("mkdir_recursive: parent = %s",
47-
parent.to_str());
48-
if parent.to_str() == ~"."
49-
|| parent.to_str() == ~"/" { // !!!
50-
// No parent directories to create
51-
os::path_is_dir(&parent) && make_dir_rwx(p)
52-
}
53-
else {
54-
mkdir_recursive(&parent) && make_dir_rwx(p)
55-
}
37+
os::make_dir(p, u_rwx)
5638
}
5739

5840
/// 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 {
130112
// n.b. Should actually use a target-specific
131113
// subdirectory of build/
132114
result = result.push(normalize(~pkgid.path).to_str());
133-
if os::path_exists(&result) || mkdir_recursive(&result) {
115+
if os::path_exists(&result) || os::mkdir_recursive(&result, u_rwx) {
134116
result
135117
}
136118
else {
@@ -148,19 +130,3 @@ pub fn mk_output_path(what: OutputType, short_name: ~str, dir: Path) -> Path {
148130
os::EXE_SUFFIX))
149131
}
150132
}
151-
152-
#[cfg(test)]
153-
mod test {
154-
use core::os;
155-
156-
#[test]
157-
fn recursive_mkdir_ok() {
158-
let root = os::tmpdir();
159-
let path = "xy/z/zy";
160-
let nested = root.push(path);
161-
assert!(super::mkdir_recursive(&nested));
162-
assert!(os::path_is_dir(&root.push("xy")));
163-
assert!(os::path_is_dir(&root.push("xy/z")));
164-
assert!(os::path_is_dir(&nested));
165-
}
166-
}

0 commit comments

Comments
 (0)