about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-05-01 00:57:35 -0700
committerbors <bors@rust-lang.org>2013-05-01 00:57:35 -0700
commitbfccfdc78065752079a3863db19ca7148ade3e6f (patch)
treeb836a4d48a9eb02362cacad524962a3c2467a6b2 /src/libcore
parent17ca13651a2bc6533413d80eb8941e5ddb20820e (diff)
parent782e06e0e379768d03e35acae16e7ee1e1841633 (diff)
downloadrust-bfccfdc78065752079a3863db19ca7148ade3e6f.tar.gz
rust-bfccfdc78065752079a3863db19ca7148ade3e6f.zip
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.
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/os.rs30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/libcore/os.rs b/src/libcore/os.rs
index f1962eeaa23..0455dabb7f0 100644
--- a/src/libcore/os.rs
+++ b/src/libcore/os.rs
@@ -818,6 +818,36 @@ pub fn change_dir(p: &Path) -> bool {
     }
 }
 
+/// Changes the current working directory to the specified
+/// path while acquiring a global lock, then calls `action`.
+/// If the change is successful, releases the lock and restores the
+/// CWD to what it was before, returning true.
+/// Returns false if the directory doesn't exist or if the directory change
+/// is otherwise unsuccessful.
+pub fn change_dir_locked(p: &Path, action: &fn()) -> bool {
+    use unstable::global::global_data_clone_create;
+    use unstable::{Exclusive, exclusive};
+
+    fn key(_: Exclusive<()>) { }
+
+    let result = unsafe {
+        global_data_clone_create(key, || {
+            ~exclusive(())
+        })
+    };
+
+    do result.with_imm() |_| {
+        let old_dir = os::getcwd();
+        if change_dir(p) {
+            action();
+            change_dir(&old_dir)
+        }
+        else {
+            false
+        }
+    }
+}
+
 /// Copies a file from one location to another
 pub fn copy_file(from: &Path, to: &Path) -> bool {
     return do_copy_file(from, to);