about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-09-06 08:01:33 +0000
committerbors <bors@rust-lang.org>2014-09-06 08:01:33 +0000
commit20c0ba1279efb5d40fcd4a739a50d09e48e0b37f (patch)
treed6ead4586f68946d11ae2daede995262a092f71b
parent4bea7b3ed0856310fc64614e5bb01e348777c99f (diff)
parenta049fb98cd399cb0d15ed8dc3bc5b8d9b327116b (diff)
auto merge of #16907 : SimonSapin/rust/tempdir-result, r=huonw
This allows using `try!()`

[breaking-change]

Fixes #16875
-rw-r--r--src/librustc/back/link.rs4
-rw-r--r--src/librustdoc/test.rs2
-rw-r--r--src/libstd/io/tempfile.rs22
-rw-r--r--src/libtest/lib.rs2
-rw-r--r--src/test/run-pass/rename-directory.rs2
-rw-r--r--src/test/run-pass/tempfile.rs6
6 files changed, 21 insertions, 17 deletions
diff --git a/src/librustc/back/link.rs b/src/librustc/back/link.rs
index 5adf8b65381..158ae22331a 100644
--- a/src/librustc/back/link.rs
+++ b/src/librustc/back/link.rs
@@ -647,7 +647,7 @@ fn link_rlib<'a>(sess: &'a Session,
             // contain the metadata in a separate file. We use a temp directory
             // here so concurrent builds in the same directory don't try to use
             // the same filename for metadata (stomping over one another)
-            let tmpdir = TempDir::new("rustc").expect("needs a temp dir");
+            let tmpdir = TempDir::new("rustc").ok().expect("needs a temp dir");
             let metadata = tmpdir.path().join(METADATA_FILENAME);
             match fs::File::create(&metadata).write(trans.metadata
                                                          .as_slice()) {
@@ -812,7 +812,7 @@ fn link_staticlib(sess: &Session, obj_filename: &Path, out_filename: &Path) {
 // links to all upstream files as well.
 fn link_natively(sess: &Session, trans: &CrateTranslation, dylib: bool,
                  obj_filename: &Path, out_filename: &Path) {
-    let tmpdir = TempDir::new("rustc").expect("needs a temp dir");
+    let tmpdir = TempDir::new("rustc").ok().expect("needs a temp dir");
 
     // The invocations of cc share some flags across platforms
     let pname = get_cc_prog(sess);
diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs
index d2345614b25..73250934d8e 100644
--- a/src/librustdoc/test.rs
+++ b/src/librustdoc/test.rs
@@ -170,7 +170,7 @@ fn runtest(test: &str, cratename: &str, libs: HashSet<Path>, externs: core::Exte
                                       None,
                                       span_diagnostic_handler);
 
-    let outdir = TempDir::new("rustdoctest").expect("rustdoc needs a tempdir");
+    let outdir = TempDir::new("rustdoctest").ok().expect("rustdoc needs a tempdir");
     let out = Some(outdir.path().clone());
     let cfg = config::build_configuration(&sess);
     let libdir = sess.target_filesearch().get_lib_path();
diff --git a/src/libstd/io/tempfile.rs b/src/libstd/io/tempfile.rs
index 8def5d5c997..6c9f10e19ae 100644
--- a/src/libstd/io/tempfile.rs
+++ b/src/libstd/io/tempfile.rs
@@ -12,7 +12,6 @@
 
 use io::{fs, IoResult};
 use io;
-use iter::range;
 use libc;
 use ops::Drop;
 use option::{Option, None, Some};
@@ -33,15 +32,16 @@ impl TempDir {
     /// will have the suffix `suffix`. The directory will be automatically
     /// deleted once the returned wrapper is destroyed.
     ///
-    /// If no directory can be created, None is returned.
-    pub fn new_in(tmpdir: &Path, suffix: &str) -> Option<TempDir> {
+    /// If no directory can be created, `Err` is returned.
+    pub fn new_in(tmpdir: &Path, suffix: &str) -> IoResult<TempDir> {
         if !tmpdir.is_absolute() {
             return TempDir::new_in(&os::make_absolute(tmpdir), suffix);
         }
 
         static mut CNT: atomic::AtomicUint = atomic::INIT_ATOMIC_UINT;
 
-        for _ in range(0u, 1000) {
+        let mut attempts = 0u;
+        loop {
             let filename =
                 format!("rs-{}-{}-{}",
                         unsafe { libc::getpid() },
@@ -49,19 +49,23 @@ impl TempDir {
                         suffix);
             let p = tmpdir.join(filename);
             match fs::mkdir(&p, io::UserRWX) {
-                Err(..) => {}
-                Ok(()) => return Some(TempDir { path: Some(p), disarmed: false })
+                Err(error) => {
+                    if attempts >= 1000 {
+                        return Err(error)
+                    }
+                    attempts += 1;
+                }
+                Ok(()) => return Ok(TempDir { path: Some(p), disarmed: false })
             }
         }
-        None
     }
 
     /// Attempts to make a temporary directory inside of `os::tmpdir()` whose
     /// name will have the suffix `suffix`. The directory will be automatically
     /// deleted once the returned wrapper is destroyed.
     ///
-    /// If no directory can be created, None is returned.
-    pub fn new(suffix: &str) -> Option<TempDir> {
+    /// If no directory can be created, `Err` is returned.
+    pub fn new(suffix: &str) -> IoResult<TempDir> {
         TempDir::new_in(&os::tmpdir(), suffix)
     }
 
diff --git a/src/libtest/lib.rs b/src/libtest/lib.rs
index 4790e3833b7..df69443ed28 100644
--- a/src/libtest/lib.rs
+++ b/src/libtest/lib.rs
@@ -1660,7 +1660,7 @@ mod tests {
     #[test]
     pub fn ratchet_test() {
 
-        let dpth = TempDir::new("test-ratchet").expect("missing test for ratchet");
+        let dpth = TempDir::new("test-ratchet").ok().expect("missing test for ratchet");
         let pth = dpth.path().join("ratchet.json");
 
         let mut m1 = MetricMap::new();
diff --git a/src/test/run-pass/rename-directory.rs b/src/test/run-pass/rename-directory.rs
index e0609782a0a..c1b542e0315 100644
--- a/src/test/run-pass/rename-directory.rs
+++ b/src/test/run-pass/rename-directory.rs
@@ -22,7 +22,7 @@ fn rename_directory() {
     unsafe {
         static U_RWX: i32 = (libc::S_IRUSR | libc::S_IWUSR | libc::S_IXUSR) as i32;
 
-        let tmpdir = TempDir::new("rename_directory").expect("rename_directory failed");
+        let tmpdir = TempDir::new("rename_directory").ok().expect("rename_directory failed");
         let tmpdir = tmpdir.path();
         let old_path = tmpdir.join_many(["foo", "bar", "baz"]);
         fs::mkdir_recursive(&old_path, io::UserRWX);
diff --git a/src/test/run-pass/tempfile.rs b/src/test/run-pass/tempfile.rs
index 9ecf5c3940a..3a41cac1fa3 100644
--- a/src/test/run-pass/tempfile.rs
+++ b/src/test/run-pass/tempfile.rs
@@ -160,8 +160,8 @@ fn recursive_mkdir_rel_2() {
 pub fn test_rmdir_recursive_ok() {
     let rwx = io::UserRWX;
 
-    let tmpdir = TempDir::new("test").expect("test_rmdir_recursive_ok: \
-                                              couldn't create temp dir");
+    let tmpdir = TempDir::new("test").ok().expect("test_rmdir_recursive_ok: \
+                                                   couldn't create temp dir");
     let tmpdir = tmpdir.path();
     let root = tmpdir.join("foo");
 
@@ -190,7 +190,7 @@ pub fn dont_double_fail() {
 }
 
 fn in_tmpdir(f: ||) {
-    let tmpdir = TempDir::new("test").expect("can't make tmpdir");
+    let tmpdir = TempDir::new("test").ok().expect("can't make tmpdir");
     assert!(os::change_dir(tmpdir.path()));
 
     f();