Skip to content

Commit 20c0ba1

Browse files
committed
auto merge of #16907 : SimonSapin/rust/tempdir-result, r=huonw
This allows using `try!()` [breaking-change] Fixes #16875
2 parents 4bea7b3 + a049fb9 commit 20c0ba1

File tree

6 files changed

+21
-17
lines changed

6 files changed

+21
-17
lines changed

src/librustc/back/link.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ fn link_rlib<'a>(sess: &'a Session,
647647
// contain the metadata in a separate file. We use a temp directory
648648
// here so concurrent builds in the same directory don't try to use
649649
// the same filename for metadata (stomping over one another)
650-
let tmpdir = TempDir::new("rustc").expect("needs a temp dir");
650+
let tmpdir = TempDir::new("rustc").ok().expect("needs a temp dir");
651651
let metadata = tmpdir.path().join(METADATA_FILENAME);
652652
match fs::File::create(&metadata).write(trans.metadata
653653
.as_slice()) {
@@ -812,7 +812,7 @@ fn link_staticlib(sess: &Session, obj_filename: &Path, out_filename: &Path) {
812812
// links to all upstream files as well.
813813
fn link_natively(sess: &Session, trans: &CrateTranslation, dylib: bool,
814814
obj_filename: &Path, out_filename: &Path) {
815-
let tmpdir = TempDir::new("rustc").expect("needs a temp dir");
815+
let tmpdir = TempDir::new("rustc").ok().expect("needs a temp dir");
816816

817817
// The invocations of cc share some flags across platforms
818818
let pname = get_cc_prog(sess);

src/librustdoc/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ fn runtest(test: &str, cratename: &str, libs: HashSet<Path>, externs: core::Exte
170170
None,
171171
span_diagnostic_handler);
172172

173-
let outdir = TempDir::new("rustdoctest").expect("rustdoc needs a tempdir");
173+
let outdir = TempDir::new("rustdoctest").ok().expect("rustdoc needs a tempdir");
174174
let out = Some(outdir.path().clone());
175175
let cfg = config::build_configuration(&sess);
176176
let libdir = sess.target_filesearch().get_lib_path();

src/libstd/io/tempfile.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
1313
use io::{fs, IoResult};
1414
use io;
15-
use iter::range;
1615
use libc;
1716
use ops::Drop;
1817
use option::{Option, None, Some};
@@ -33,35 +32,40 @@ impl TempDir {
3332
/// will have the suffix `suffix`. The directory will be automatically
3433
/// deleted once the returned wrapper is destroyed.
3534
///
36-
/// If no directory can be created, None is returned.
37-
pub fn new_in(tmpdir: &Path, suffix: &str) -> Option<TempDir> {
35+
/// If no directory can be created, `Err` is returned.
36+
pub fn new_in(tmpdir: &Path, suffix: &str) -> IoResult<TempDir> {
3837
if !tmpdir.is_absolute() {
3938
return TempDir::new_in(&os::make_absolute(tmpdir), suffix);
4039
}
4140

4241
static mut CNT: atomic::AtomicUint = atomic::INIT_ATOMIC_UINT;
4342

44-
for _ in range(0u, 1000) {
43+
let mut attempts = 0u;
44+
loop {
4545
let filename =
4646
format!("rs-{}-{}-{}",
4747
unsafe { libc::getpid() },
4848
unsafe { CNT.fetch_add(1, atomic::SeqCst) },
4949
suffix);
5050
let p = tmpdir.join(filename);
5151
match fs::mkdir(&p, io::UserRWX) {
52-
Err(..) => {}
53-
Ok(()) => return Some(TempDir { path: Some(p), disarmed: false })
52+
Err(error) => {
53+
if attempts >= 1000 {
54+
return Err(error)
55+
}
56+
attempts += 1;
57+
}
58+
Ok(()) => return Ok(TempDir { path: Some(p), disarmed: false })
5459
}
5560
}
56-
None
5761
}
5862

5963
/// Attempts to make a temporary directory inside of `os::tmpdir()` whose
6064
/// name will have the suffix `suffix`. The directory will be automatically
6165
/// deleted once the returned wrapper is destroyed.
6266
///
63-
/// If no directory can be created, None is returned.
64-
pub fn new(suffix: &str) -> Option<TempDir> {
67+
/// If no directory can be created, `Err` is returned.
68+
pub fn new(suffix: &str) -> IoResult<TempDir> {
6569
TempDir::new_in(&os::tmpdir(), suffix)
6670
}
6771

src/libtest/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1660,7 +1660,7 @@ mod tests {
16601660
#[test]
16611661
pub fn ratchet_test() {
16621662

1663-
let dpth = TempDir::new("test-ratchet").expect("missing test for ratchet");
1663+
let dpth = TempDir::new("test-ratchet").ok().expect("missing test for ratchet");
16641664
let pth = dpth.path().join("ratchet.json");
16651665

16661666
let mut m1 = MetricMap::new();

src/test/run-pass/rename-directory.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn rename_directory() {
2222
unsafe {
2323
static U_RWX: i32 = (libc::S_IRUSR | libc::S_IWUSR | libc::S_IXUSR) as i32;
2424

25-
let tmpdir = TempDir::new("rename_directory").expect("rename_directory failed");
25+
let tmpdir = TempDir::new("rename_directory").ok().expect("rename_directory failed");
2626
let tmpdir = tmpdir.path();
2727
let old_path = tmpdir.join_many(["foo", "bar", "baz"]);
2828
fs::mkdir_recursive(&old_path, io::UserRWX);

src/test/run-pass/tempfile.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ fn recursive_mkdir_rel_2() {
160160
pub fn test_rmdir_recursive_ok() {
161161
let rwx = io::UserRWX;
162162

163-
let tmpdir = TempDir::new("test").expect("test_rmdir_recursive_ok: \
164-
couldn't create temp dir");
163+
let tmpdir = TempDir::new("test").ok().expect("test_rmdir_recursive_ok: \
164+
couldn't create temp dir");
165165
let tmpdir = tmpdir.path();
166166
let root = tmpdir.join("foo");
167167

@@ -190,7 +190,7 @@ pub fn dont_double_fail() {
190190
}
191191

192192
fn in_tmpdir(f: ||) {
193-
let tmpdir = TempDir::new("test").expect("can't make tmpdir");
193+
let tmpdir = TempDir::new("test").ok().expect("can't make tmpdir");
194194
assert!(os::change_dir(tmpdir.path()));
195195

196196
f();

0 commit comments

Comments
 (0)