aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorMarijn Haverbeke <[email protected]>2011-03-10 15:56:51 +0100
committerGraydon Hoare <[email protected]>2011-03-14 14:57:13 -0700
commitc731d625fe5f7626b41c7241893350b8b27b1dbe (patch)
tree9b5c8f5071d44d601914fa9ea1e44e07fe6f23db /src/lib
parentSplit trans' collection in two passes. This allows us to handle tags (diff)
downloadrust-c731d625fe5f7626b41c7241893350b8b27b1dbe.tar.xz
rust-c731d625fe5f7626b41c7241893350b8b27b1dbe.zip
Add basic file-system functionality
std.fs.list_dir will list the files in a directory, std.fs.file_is_dir will, given a pathname, determine whether it is a directory or not.
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/_str.rs5
-rw-r--r--src/lib/fs.rs37
-rw-r--r--src/lib/io.rs2
-rw-r--r--src/lib/linux_os.rs7
-rw-r--r--src/lib/macos_os.rs10
-rw-r--r--src/lib/path.rs21
-rw-r--r--src/lib/posix_fs.rs19
-rw-r--r--src/lib/std.rc10
-rw-r--r--src/lib/win32_fs.rs10
-rw-r--r--src/lib/win32_os.rs5
10 files changed, 86 insertions, 40 deletions
diff --git a/src/lib/_str.rs b/src/lib/_str.rs
index 3f453349..526dac8f 100644
--- a/src/lib/_str.rs
+++ b/src/lib/_str.rs
@@ -106,6 +106,11 @@ fn from_bytes(vec[u8] v) : is_utf8(v) -> str {
ret rustrt.str_from_vec(v);
}
+// FIXME temp thing
+fn unsafe_from_bytes(vec[u8] v) -> str {
+ ret rustrt.str_from_vec(v);
+}
+
fn refcount(str s) -> uint {
auto r = rustrt.refcount[u8](s);
if (r == dbg.const_refcount) {
diff --git a/src/lib/fs.rs b/src/lib/fs.rs
new file mode 100644
index 00000000..55f4f979
--- /dev/null
+++ b/src/lib/fs.rs
@@ -0,0 +1,37 @@
+native "rust" mod rustrt {
+ fn rust_file_is_dir(str path) -> int;
+}
+
+fn path_sep() -> str {
+ ret _str.unsafe_from_bytes(vec(os_fs.path_sep as u8));
+}
+
+type path = str;
+
+fn dirname(path p) -> path {
+ auto sep = path_sep();
+ check (_str.byte_len(sep) == 1u);
+ let int i = _str.rindex(p, sep.(0));
+ if (i == -1) {
+ ret p;
+ }
+ ret _str.substr(p, 0u, i as uint);
+}
+
+impure fn file_is_dir(path p) -> bool {
+ ret rustrt.rust_file_is_dir(p) != 0;
+}
+
+impure fn list_dir(path p) -> vec[str] {
+ auto pl = _str.byte_len(p);
+ if (pl == 0u || p.(pl - 1u) as char != os_fs.path_sep) {
+ p += path_sep();
+ }
+ let vec[str] full_paths = vec();
+ for (str filename in os_fs.list_dir(p)) {
+ if (!_str.eq(filename, ".")) {if (!_str.eq(filename, "..")) {
+ full_paths = _vec.push[str](full_paths, p + filename);
+ }}
+ }
+ ret full_paths;
+}
diff --git a/src/lib/io.rs b/src/lib/io.rs
index 34c4a98d..219125c5 100644
--- a/src/lib/io.rs
+++ b/src/lib/io.rs
@@ -86,6 +86,7 @@ tag fileflag {
append;
create;
truncate;
+ none;
}
state obj fd_buf_writer(int fd, bool must_close) {
@@ -120,6 +121,7 @@ fn file_buf_writer(str path, vec[fileflag] flags) -> buf_writer {
case (append) { fflags |= os.libc_constants.O_APPEND(); }
case (create) { fflags |= os.libc_constants.O_CREAT(); }
case (truncate) { fflags |= os.libc_constants.O_TRUNC(); }
+ case (none) {}
}
}
diff --git a/src/lib/linux_os.rs b/src/lib/linux_os.rs
index d5ef2a6b..e05a69b3 100644
--- a/src/lib/linux_os.rs
+++ b/src/lib/linux_os.rs
@@ -15,9 +15,10 @@ native mod libc = "libc.so.6" {
fn ungetc(int c, FILE f);
type dir;
- // readdir is a mess; handle via wrapper function in rustrt.
fn opendir(sbuf d) -> dir;
fn closedir(dir d) -> int;
+ type dirent;
+ fn readdir(dir d) -> dirent;
fn getenv(sbuf n) -> sbuf;
fn setenv(sbuf n, sbuf v, int overwrite) -> int;
@@ -39,10 +40,6 @@ mod libc_constants {
fn S_IWUSR() -> uint { ret 0x0080u; }
}
-fn path_sep() -> str {
- ret "/";
-}
-
fn exec_suffix() -> str {
ret "";
}
diff --git a/src/lib/macos_os.rs b/src/lib/macos_os.rs
index 53a66b56..3dc63bad 100644
--- a/src/lib/macos_os.rs
+++ b/src/lib/macos_os.rs
@@ -1,5 +1,4 @@
-import _str.sbuf;
-import _vec.vbuf;
+import libc = posix;
native mod libc = "libc.dylib" {
@@ -15,9 +14,10 @@ native mod libc = "libc.dylib" {
fn ungetc(int c, FILE f);
type dir;
- // readdir is a mess; handle via wrapper function in rustrt.
fn opendir(sbuf d) -> dir;
fn closedir(dir d) -> int;
+ type dirent;
+ fn readdir(dir d) -> dirent;
fn getenv(sbuf n) -> sbuf;
fn setenv(sbuf n, sbuf v, int overwrite) -> int;
@@ -39,10 +39,6 @@ mod libc_constants {
fn S_IWUSR() -> uint { ret 0x0200u; }
}
-fn path_sep() -> str {
- ret "/";
-}
-
fn exec_suffix() -> str {
ret "";
}
diff --git a/src/lib/path.rs b/src/lib/path.rs
deleted file mode 100644
index 9cda93d6..00000000
--- a/src/lib/path.rs
+++ /dev/null
@@ -1,21 +0,0 @@
-
-type path = str;
-
-fn dirname(path p) -> path {
- auto sep = os.path_sep();
- check (_str.byte_len(sep) == 1u);
- let int i = _str.rindex(p, sep.(0));
- if (i == -1) {
- ret p;
- }
- ret _str.substr(p, 0u, i as uint);
-}
-
-// Local Variables:
-// mode: rust;
-// fill-column: 78;
-// indent-tabs-mode: nil
-// c-basic-offset: 4
-// buffer-file-coding-system: utf-8-unix
-// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
-// End:
diff --git a/src/lib/posix_fs.rs b/src/lib/posix_fs.rs
new file mode 100644
index 00000000..05366a15
--- /dev/null
+++ b/src/lib/posix_fs.rs
@@ -0,0 +1,19 @@
+native "rust" mod rustrt {
+ fn rust_dirent_filename(os.libc.dirent ent) -> str;
+}
+
+impure fn list_dir(str path) -> vec[str] {
+ // TODO ensure this is always closed
+ auto dir = os.libc.opendir(_str.buf(path));
+ check (dir as uint != 0u);
+ let vec[str] result = vec();
+ while (true) {
+ auto ent = os.libc.readdir(dir);
+ if (ent as int == 0) {break;}
+ result = _vec.push[str](result, rustrt.rust_dirent_filename(ent));
+ }
+ os.libc.closedir(dir);
+ ret result;
+}
+
+const char path_sep = '/';
diff --git a/src/lib/std.rc b/src/lib/std.rc
index 4ad422a3..8098a4e9 100644
--- a/src/lib/std.rc
+++ b/src/lib/std.rc
@@ -26,6 +26,8 @@ mod util;
// Authorize various rule-bendings.
auth io = unsafe;
+auth fs = unsafe;
+auth os_fs = unsafe;
auth _str = unsafe;
auth _vec = unsafe;
auth _task = unsafe;
@@ -41,12 +43,17 @@ auth rand.mk_rng = unsafe;
alt (target_os) {
case ("win32") {
mod os = "win32_os.rs";
+ mod os_fs = "win32_fs.rs";
} case ("macos") {
mod os = "macos_os.rs";
+ mod os_fs = "posix_fs.rs";
} else {
mod os = "linux_os.rs";
+ mod os_fs = "posix_fs.rs";
}
- }
+}
+mod fs;
+
// FIXME: parametric
mod map;
@@ -56,7 +63,6 @@ mod rand;
mod dbg;
mod bitv;
mod sort;
-mod path;
mod sha1;
// Local Variables:
diff --git a/src/lib/win32_fs.rs b/src/lib/win32_fs.rs
new file mode 100644
index 00000000..641ef33e
--- /dev/null
+++ b/src/lib/win32_fs.rs
@@ -0,0 +1,10 @@
+native "rust" mod rustrt {
+ fn rust_list_files(str path) -> vec[str];
+ fn rust_file_is_dir(str path) -> int;
+}
+
+impure fn list_dir(str path) -> vec[str] {
+ ret rustrt.rust_list_files(path+"*");
+}
+
+const char path_sep = '\\';
diff --git a/src/lib/win32_os.rs b/src/lib/win32_os.rs
index 9f9ec2c2..0c6a23a6 100644
--- a/src/lib/win32_os.rs
+++ b/src/lib/win32_os.rs
@@ -29,11 +29,6 @@ mod libc_constants {
fn S_IWUSR() -> uint { ret 0x0080u; } // really _S_IWRITE in win32
}
-
-fn path_sep() -> str {
- ret "\\";
-}
-
fn exec_suffix() -> str {
ret ".exe";
}