Skip to content

core: Move mkdir_recursive from rustpkg into core::os #6082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/libcore/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down Expand Up @@ -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));
}

}
44 changes: 5 additions & 39 deletions src/librustpkg/path_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 '_'
Expand Down Expand Up @@ -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 {
Expand All @@ -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));
}
}