Skip to content

Commit

Permalink
Have std::io::TempDir::new and new_in return IoResult
Browse files Browse the repository at this point in the history
This allows using `try!()`

[breaking-change]

Fixes rust-lang#16875
  • Loading branch information
SimonSapin committed Aug 31, 2014
1 parent 27e8d5b commit a049fb9
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/librustc/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,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()) {
Expand Down Expand Up @@ -1280,7 +1280,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);
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
22 changes: 13 additions & 9 deletions src/libstd/io/tempfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

use io::{fs, IoResult};
use io;
use iter::range;
use libc;
use ops::Drop;
use option::{Option, None, Some};
Expand All @@ -33,35 +32,40 @@ 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() },
unsafe { CNT.fetch_add(1, atomic::SeqCst) },
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)
}

Expand Down
2 changes: 1 addition & 1 deletion src/libtest/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass/rename-directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/test/run-pass/tempfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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();
Expand Down

4 comments on commit a049fb9

@huonw
Copy link

@huonw huonw commented on a049fb9 Sep 5, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r+

@alexcrichton
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors: retry

@alexcrichton
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors: retry

@alexcrichton
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors: retry

Please sign in to comment.