std: add fs::mkdir(), rng.gen_str(), tempfile::mkdtemp()

Signed-off-by: Elly Jones <elly@leptoquark.net>
This commit is contained in:
Elly Jones 2011-11-08 23:35:15 -05:00 committed by Brian Anderson
parent a936f78d98
commit c11c44abc0
7 changed files with 78 additions and 5 deletions

View file

@ -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<os::kernel32::LPSECURITY_ATTRIBUTES>();
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

View file

@ -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 {

View file

@ -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 {

View file

@ -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()));
}

View file

@ -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;

23
src/lib/tempfile.rs Normal file
View file

@ -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<str> {
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;
}

View file

@ -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