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.)
This commit is contained in:
commit
dd5b1de181
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user