2012-03-07 20:17:30 -06:00
|
|
|
#[doc = "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-01-31 19:05:20 -06: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)
|
2011-11-08 22:35:15 -06:00
|
|
|
ret some(s);
|
|
|
|
}
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
ret none;
|
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mkdtemp() {
|
|
|
|
let r = mkdtemp("./", "foobar");
|
|
|
|
alt r {
|
|
|
|
some(p) {
|
2012-03-12 22:04:27 -05:00
|
|
|
os::remove_dir(p);
|
2012-01-17 21:05:07 -06:00
|
|
|
assert(str::ends_with(p, "foobar"));
|
|
|
|
}
|
|
|
|
_ { assert(false); }
|
|
|
|
}
|
|
|
|
}
|