2012-07-04 16:53:12 -05:00
|
|
|
//! Temporary files and directories
|
2011-11-08 22:35:15 -06:00
|
|
|
|
2011-12-13 18:25:51 -06:00
|
|
|
import core::option;
|
2011-11-08 22:35:15 -06:00
|
|
|
import option::{none, some};
|
|
|
|
import rand;
|
2012-05-22 12:54:12 -05:00
|
|
|
import core::rand::extensions;
|
2011-11-08 22:35:15 -06:00
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn mkdtemp(prefix: ~str, suffix: ~str) -> option<~str> {
|
2012-03-12 17:52:30 -05:00
|
|
|
let r = rand::rng();
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut i = 0u;
|
2011-11-08 22:35:15 -06:00
|
|
|
while (i < 1000u) {
|
|
|
|
let s = prefix + r.gen_str(16u) + suffix;
|
2012-05-03 18:56:55 -05:00
|
|
|
if os::make_dir(s, 0x1c0i32) { // FIXME: u+rwx (#2349)
|
2012-08-01 19:30:05 -05:00
|
|
|
return some(s);
|
2011-11-08 22:35:15 -06:00
|
|
|
}
|
|
|
|
i += 1u;
|
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return none;
|
2011-11-08 22:35:15 -06:00
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mkdtemp() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let r = mkdtemp(~"./", ~"foobar");
|
2012-01-17 21:05:07 -06:00
|
|
|
alt r {
|
2012-08-03 21:59:04 -05:00
|
|
|
some(p) => {
|
2012-03-12 22:04:27 -05:00
|
|
|
os::remove_dir(p);
|
2012-07-14 00:57:48 -05:00
|
|
|
assert(str::ends_with(p, ~"foobar"));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => assert(false)
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|