Skip to content

Commit 49a64e5

Browse files
Elly Jonesbrson
authored andcommitted
---
yaml --- r: 6436 b: refs/heads/master c: c11c44a h: refs/heads/master v: v3
1 parent 906ca38 commit 49a64e5

File tree

8 files changed

+79
-6
lines changed

8 files changed

+79
-6
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: a936f78d987debe7cb46a9ec13377cef76645cda
2+
refs/heads/master: c11c44abc03c52a0fd8dd58fcc80d571cb69e70e

trunk/src/lib/fs.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Module: fs
44
File system manipulation
55
*/
66

7+
import os;
78
import os::getcwd;
89
import os_fs;
910

@@ -115,6 +116,28 @@ fn file_is_dir(p: path) -> bool {
115116
ret str::as_buf(p, {|buf| rustrt::rust_file_is_dir(buf) != 0 });
116117
}
117118

119+
/*
120+
Function: make_dir
121+
122+
Creates a directory at the specific path.
123+
*/
124+
fn make_dir(p: path, mode: int) -> bool {
125+
ret mkdir(p, mode);
126+
127+
#[cfg(target_os = "win32")]
128+
fn mkdir(_p: path, _mode: int) -> bool {
129+
// FIXME: turn mode into something useful?
130+
let noctx = ptr::null<os::kernel32::LPSECURITY_ATTRIBUTES>();
131+
ret str::as_buf(_p, {|buf| os::kernel32::CreateDirectory(buf, noctx) });
132+
}
133+
134+
#[cfg(target_os = "linux")]
135+
#[cfg(target_os = "macos")]
136+
fn mkdir(_p: path, _mode: int) -> bool {
137+
ret str::as_buf(_p, {|buf| os::libc::mkdir(buf, _mode) == 0 });
138+
}
139+
}
140+
118141
/*
119142
Function: list_dir
120143

trunk/src/lib/linux_os.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ native mod libc {
5151
fn pipe(buf: *mutable fd_t) -> c_int;
5252
fn waitpid(pid: pid_t, &status: c_int, options: c_int) -> pid_t;
5353
fn readlink(path: str::sbuf, buf: str::sbuf, bufsize: size_t) -> ssize_t;
54+
fn mkdir(path: str::sbuf, mode: int) -> int;
5455
}
5556

5657
mod libc_constants {

trunk/src/lib/macos_os.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ native mod libc {
4040
type dirent;
4141
fn readdir(d: dir) -> dirent;
4242
fn getenv(n: str::sbuf) -> str::sbuf;
43-
fn setenv(n: str::sbuf, v: str::sbuf, overwrite: c_int) -> c_int;
44-
fn unsetenv(n: str::sbuf) -> c_int;
45-
fn pipe(buf: *mutable fd_t) -> c_int;
46-
fn waitpid(pid: pid_t, &status: c_int, options: c_int) -> pid_t;
43+
fn setenv(n: str::sbuf, v: str::sbuf, overwrite: int) -> int;
44+
fn unsetenv(n: str::sbuf) -> int;
45+
fn pipe(buf: *mutable int) -> int;
46+
fn waitpid(pid: int, &status: int, options: int) -> int;
47+
fn mkdir(s: str::sbuf, mode: int) -> int;
4748
}
4849

4950
mod libc_constants {

trunk/src/lib/rand.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ type rng = obj {
3232
Return the next random float
3333
*/
3434
fn next_float() -> float;
35+
36+
/*
37+
Method: gen_str
38+
39+
Return a random string composed of A-Z, a-z, 0-9.
40+
*/
41+
fn gen_str(len: uint) -> str;
3542
};
3643

3744
resource rand_res(c: rustrt::rctx) { rustrt::rand_free(c); }
@@ -53,6 +60,19 @@ fn mk_rng() -> rng {
5360
let scale = u32::max_value as float;
5461
ret ((u1 / scale + u2) / scale + u3) / scale;
5562
}
63+
fn gen_str(len: uint) -> str {
64+
let charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
65+
"abcdefghijklmnopqrstuvwxyz" +
66+
"0123456789";
67+
let s = "";
68+
let i = 0u;
69+
while (i < len) {
70+
let n = rustrt::rand_next(**c) as uint % str::char_len(charset);
71+
s = s + str::from_char(str::char_at(charset, n));
72+
i += 1u;
73+
}
74+
s
75+
}
5676
}
5777
ret rt_rng(@rand_res(rustrt::rand_new()));
5878
}

trunk/src/lib/std.rc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export ctypes, either, option, result, four, tri, util;
1313
export bitv, deque, fun_treemap, list, map, smallintmap, sort, treemap, ufind;
1414
export rope;
1515
export ebml, dbg, getopts, json, math, rand, sha1, term, time, unsafe;
16-
export extfmt, test;
16+
export extfmt, test, tempfile;
1717
// FIXME: generic_os and os_fs shouldn't be exported
1818
export generic_os, os, os_fs;
1919

@@ -79,6 +79,7 @@ mod json;
7979
mod math;
8080
mod rand;
8181
mod sha1;
82+
mod tempfile;
8283
mod term;
8384
mod time;
8485
mod unsafe;

trunk/src/lib/tempfile.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Module: tempfile
3+
4+
Temporary files and directories
5+
*/
6+
7+
import fs;
8+
import option;
9+
import option::{none, some};
10+
import rand;
11+
12+
fn mkdtemp(prefix: str, suffix: str) -> option::t<str> {
13+
let r = rand::mk_rng();
14+
let i = 0u;
15+
while (i < 1000u) {
16+
let s = prefix + r.gen_str(16u) + suffix;
17+
if fs::make_dir(s, 0x1c0) { // FIXME: u+rwx
18+
ret some(s);
19+
}
20+
i += 1u;
21+
}
22+
ret none;
23+
}

trunk/src/lib/win32_os.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,19 @@ mod libc_constants {
4141
type DWORD = u32;
4242
type HMODULE = uint;
4343
type LPTSTR = str::sbuf;
44+
type LPCTSTR = str::sbuf;
4445

4546
#[abi = "stdcall"]
4647
native mod kernel32 {
48+
type LPSECURITY_ATTRIBUTES;
4749
fn GetEnvironmentVariableA(n: str::sbuf, v: str::sbuf, nsize: uint) ->
4850
uint;
4951
fn SetEnvironmentVariableA(n: str::sbuf, v: str::sbuf) -> int;
5052
fn GetModuleFileNameA(hModule: HMODULE,
5153
lpFilename: LPTSTR,
5254
nSize: DWORD) -> DWORD;
55+
fn CreateDirectory(lpPathName: LPCTSTR,
56+
lpSecurityAttributes: LPSECURITY_ATTRIBUTES) -> bool;
5357
}
5458

5559
// FIXME turn into constants

0 commit comments

Comments
 (0)