diff --git a/src/lib/fs.rs b/src/lib/fs.rs index 09e36ea7945..b15ec23c5b7 100644 --- a/src/lib/fs.rs +++ b/src/lib/fs.rs @@ -4,6 +4,7 @@ Module: fs File system manipulation */ +import os; import os::getcwd; import os_fs; @@ -115,6 +116,28 @@ fn file_is_dir(p: path) -> bool { ret str::as_buf(p, {|buf| rustrt::rust_file_is_dir(buf) != 0 }); } +/* +Function: make_dir + +Creates a directory at the specific path. +*/ +fn make_dir(p: path, mode: int) -> bool { + ret mkdir(p, mode); + + #[cfg(target_os = "win32")] + fn mkdir(_p: path, _mode: int) -> bool { + // FIXME: turn mode into something useful? + let noctx = ptr::null(); + ret str::as_buf(_p, {|buf| os::kernel32::CreateDirectory(buf, noctx) }); + } + + #[cfg(target_os = "linux")] + #[cfg(target_os = "macos")] + fn mkdir(_p: path, _mode: int) -> bool { + ret str::as_buf(_p, {|buf| os::libc::mkdir(buf, _mode) == 0 }); + } +} + /* Function: list_dir diff --git a/src/lib/linux_os.rs b/src/lib/linux_os.rs index 61acc6a4899..093fe542ca9 100644 --- a/src/lib/linux_os.rs +++ b/src/lib/linux_os.rs @@ -51,6 +51,7 @@ native mod libc { fn pipe(buf: *mutable fd_t) -> c_int; fn waitpid(pid: pid_t, &status: c_int, options: c_int) -> pid_t; fn readlink(path: str::sbuf, buf: str::sbuf, bufsize: size_t) -> ssize_t; + fn mkdir(path: str::sbuf, mode: int) -> int; } mod libc_constants { diff --git a/src/lib/macos_os.rs b/src/lib/macos_os.rs index dbea5bcb7fe..e2209f15d2e 100644 --- a/src/lib/macos_os.rs +++ b/src/lib/macos_os.rs @@ -40,10 +40,11 @@ native mod libc { type dirent; fn readdir(d: dir) -> dirent; fn getenv(n: str::sbuf) -> str::sbuf; - fn setenv(n: str::sbuf, v: str::sbuf, overwrite: c_int) -> c_int; - fn unsetenv(n: str::sbuf) -> c_int; - fn pipe(buf: *mutable fd_t) -> c_int; - fn waitpid(pid: pid_t, &status: c_int, options: c_int) -> pid_t; + fn setenv(n: str::sbuf, v: str::sbuf, overwrite: int) -> int; + fn unsetenv(n: str::sbuf) -> int; + fn pipe(buf: *mutable int) -> int; + fn waitpid(pid: int, &status: int, options: int) -> int; + fn mkdir(s: str::sbuf, mode: int) -> int; } mod libc_constants { diff --git a/src/lib/rand.rs b/src/lib/rand.rs index 37074a6a609..0f8117e901a 100644 --- a/src/lib/rand.rs +++ b/src/lib/rand.rs @@ -32,6 +32,13 @@ type rng = obj { Return the next random float */ fn next_float() -> float; + + /* + Method: gen_str + + Return a random string composed of A-Z, a-z, 0-9. + */ + fn gen_str(len: uint) -> str; }; resource rand_res(c: rustrt::rctx) { rustrt::rand_free(c); } @@ -53,6 +60,19 @@ fn mk_rng() -> rng { let scale = u32::max_value as float; ret ((u1 / scale + u2) / scale + u3) / scale; } + fn gen_str(len: uint) -> str { + let charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + + "abcdefghijklmnopqrstuvwxyz" + + "0123456789"; + let s = ""; + let i = 0u; + while (i < len) { + let n = rustrt::rand_next(**c) as uint % str::char_len(charset); + s = s + str::from_char(str::char_at(charset, n)); + i += 1u; + } + s + } } ret rt_rng(@rand_res(rustrt::rand_new())); } diff --git a/src/lib/std.rc b/src/lib/std.rc index 87184d5e905..ac15964c6fa 100644 --- a/src/lib/std.rc +++ b/src/lib/std.rc @@ -13,7 +13,7 @@ export ctypes, either, option, result, four, tri, util; export bitv, deque, fun_treemap, list, map, smallintmap, sort, treemap, ufind; export rope; export ebml, dbg, getopts, json, math, rand, sha1, term, time, unsafe; -export extfmt, test; +export extfmt, test, tempfile; // FIXME: generic_os and os_fs shouldn't be exported export generic_os, os, os_fs; @@ -79,6 +79,7 @@ mod json; mod math; mod rand; mod sha1; +mod tempfile; mod term; mod time; mod unsafe; diff --git a/src/lib/tempfile.rs b/src/lib/tempfile.rs new file mode 100644 index 00000000000..1d29169ecc4 --- /dev/null +++ b/src/lib/tempfile.rs @@ -0,0 +1,23 @@ +/* +Module: tempfile + +Temporary files and directories +*/ + +import fs; +import option; +import option::{none, some}; +import rand; + +fn mkdtemp(prefix: str, suffix: str) -> option::t { + let r = rand::mk_rng(); + let i = 0u; + while (i < 1000u) { + let s = prefix + r.gen_str(16u) + suffix; + if fs::make_dir(s, 0x1c0) { // FIXME: u+rwx + ret some(s); + } + i += 1u; + } + ret none; +} diff --git a/src/lib/win32_os.rs b/src/lib/win32_os.rs index d85f8995d99..6befe626817 100644 --- a/src/lib/win32_os.rs +++ b/src/lib/win32_os.rs @@ -41,15 +41,19 @@ mod libc_constants { type DWORD = u32; type HMODULE = uint; type LPTSTR = str::sbuf; +type LPCTSTR = str::sbuf; #[abi = "stdcall"] native mod kernel32 { + type LPSECURITY_ATTRIBUTES; fn GetEnvironmentVariableA(n: str::sbuf, v: str::sbuf, nsize: uint) -> uint; fn SetEnvironmentVariableA(n: str::sbuf, v: str::sbuf) -> int; fn GetModuleFileNameA(hModule: HMODULE, lpFilename: LPTSTR, nSize: DWORD) -> DWORD; + fn CreateDirectory(lpPathName: LPCTSTR, + lpSecurityAttributes: LPSECURITY_ATTRIBUTES) -> bool; } // FIXME turn into constants