2013-01-16 04:28:39 -06:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
//! Temporary files and directories
|
2011-11-08 22:35:15 -06:00
|
|
|
|
2013-03-12 15:00:50 -05:00
|
|
|
use core::rand::RngUtil;
|
2011-11-08 22:35:15 -06:00
|
|
|
|
2012-09-27 20:03:00 -05:00
|
|
|
pub fn mkdtemp(tmpdir: &Path, suffix: &str) -> Option<Path> {
|
2013-04-23 09:00:43 -05:00
|
|
|
let r = rand::rng();
|
2013-01-16 04:28:39 -06:00
|
|
|
for 1000.times {
|
|
|
|
let p = tmpdir.push(r.gen_str(16) + suffix);
|
|
|
|
if os::make_dir(&p, 0x1c0) { // 700
|
2012-08-20 14:23:37 -05:00
|
|
|
return Some(p);
|
2011-11-08 22:35:15 -06:00
|
|
|
}
|
|
|
|
}
|
2013-01-16 04:28:39 -06:00
|
|
|
None
|
2011-11-08 22:35:15 -06:00
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mkdtemp() {
|
2013-01-16 04:28:39 -06:00
|
|
|
let p = mkdtemp(&Path("."), "foobar").unwrap();
|
|
|
|
os::remove_dir(&p);
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(str::ends_with(p.to_str(), "foobar"));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|