Skip to content

Commit bfccfdc

Browse files
committed
auto merge of #6144 : catamorphism/rust/mkdir_recursive-breakage, r=thestinger
r? @brson or @thestinger : Added a change_dir_locked function to os, and use it in the mkdir_recursive tests so that the tests don't clobber each other's directory changes.
2 parents 17ca136 + 782e06e commit bfccfdc

File tree

2 files changed

+59
-21
lines changed

2 files changed

+59
-21
lines changed

src/libcore/os.rs

+30
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,36 @@ pub fn change_dir(p: &Path) -> bool {
818818
}
819819
}
820820
821+
/// Changes the current working directory to the specified
822+
/// path while acquiring a global lock, then calls `action`.
823+
/// If the change is successful, releases the lock and restores the
824+
/// CWD to what it was before, returning true.
825+
/// Returns false if the directory doesn't exist or if the directory change
826+
/// is otherwise unsuccessful.
827+
pub fn change_dir_locked(p: &Path, action: &fn()) -> bool {
828+
use unstable::global::global_data_clone_create;
829+
use unstable::{Exclusive, exclusive};
830+
831+
fn key(_: Exclusive<()>) { }
832+
833+
let result = unsafe {
834+
global_data_clone_create(key, || {
835+
~exclusive(())
836+
})
837+
};
838+
839+
do result.with_imm() |_| {
840+
let old_dir = os::getcwd();
841+
if change_dir(p) {
842+
action();
843+
change_dir(&old_dir)
844+
}
845+
else {
846+
false
847+
}
848+
}
849+
}
850+
821851
/// Copies a file from one location to another
822852
pub fn copy_file(from: &Path, to: &Path) -> bool {
823853
return do_copy_file(from, to);

src/libstd/tempfile.rs

+29-21
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,18 @@ mod tests {
4242
use core::libc::consts::os::posix88::{S_IRUSR, S_IWUSR, S_IXUSR};
4343
use core::os;
4444

45-
let root = mkdtemp(&os::tmpdir(), "temp").expect("recursive_mkdir_rel");
46-
os::change_dir(&root);
47-
let path = Path("frob");
48-
assert!(os::mkdir_recursive(&path, (S_IRUSR | S_IWUSR | S_IXUSR) as i32));
49-
assert!(os::path_is_dir(&path));
50-
assert!(os::mkdir_recursive(&path, (S_IRUSR | S_IWUSR | S_IXUSR) as i32));
51-
assert!(os::path_is_dir(&path));
45+
let root = mkdtemp(&os::tmpdir(), "recursive_mkdir_rel").
46+
expect("recursive_mkdir_rel");
47+
assert!(do os::change_dir_locked(&root) {
48+
let path = Path("frob");
49+
debug!("recursive_mkdir_rel: Making: %s in cwd %s [%?]", path.to_str(),
50+
os::getcwd().to_str(),
51+
os::path_exists(&path));
52+
assert!(os::mkdir_recursive(&path, (S_IRUSR | S_IWUSR | S_IXUSR) as i32));
53+
assert!(os::path_is_dir(&path));
54+
assert!(os::mkdir_recursive(&path, (S_IRUSR | S_IWUSR | S_IXUSR) as i32));
55+
assert!(os::path_is_dir(&path));
56+
});
5257
}
5358

5459
#[test]
@@ -67,18 +72,21 @@ mod tests {
6772
use core::libc::consts::os::posix88::{S_IRUSR, S_IWUSR, S_IXUSR};
6873
use core::os;
6974

70-
let root = mkdtemp(&os::tmpdir(), "temp").expect("recursive_mkdir_rel_2");
71-
os::change_dir(&root);
72-
let path = Path("./frob/baz");
73-
debug!("...Making: %s in cwd %s", path.to_str(), os::getcwd().to_str());
74-
assert!(os::mkdir_recursive(&path, (S_IRUSR | S_IWUSR | S_IXUSR) as i32));
75-
assert!(os::path_is_dir(&path));
76-
assert!(os::path_is_dir(&path.pop()));
77-
let path2 = Path("quux/blat");
78-
debug!("Making: %s in cwd %s", path2.to_str(), os::getcwd().to_str());
79-
assert!(os::mkdir_recursive(&path2, (S_IRUSR | S_IWUSR | S_IXUSR) as i32));
80-
assert!(os::path_is_dir(&path2));
81-
assert!(os::path_is_dir(&path2.pop()));
75+
let root = mkdtemp(&os::tmpdir(), "recursive_mkdir_rel_2").
76+
expect("recursive_mkdir_rel_2");
77+
assert!(do os::change_dir_locked(&root) {
78+
let path = Path("./frob/baz");
79+
debug!("recursive_mkdir_rel_2: Making: %s in cwd %s [%?]", path.to_str(),
80+
os::getcwd().to_str(), os::path_exists(&path));
81+
assert!(os::mkdir_recursive(&path, (S_IRUSR | S_IWUSR | S_IXUSR) as i32));
82+
assert!(os::path_is_dir(&path));
83+
assert!(os::path_is_dir(&path.pop()));
84+
let path2 = Path("quux/blat");
85+
debug!("recursive_mkdir_rel_2: Making: %s in cwd %s", path2.to_str(),
86+
os::getcwd().to_str());
87+
assert!(os::mkdir_recursive(&path2, (S_IRUSR | S_IWUSR | S_IXUSR) as i32));
88+
assert!(os::path_is_dir(&path2));
89+
assert!(os::path_is_dir(&path2.pop()));
90+
});
8291
}
83-
84-
}
92+
}

0 commit comments

Comments
 (0)