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