summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUnknown <glenda@cirno>2015-01-03 16:00:45 -0800
committerUnknown <glenda@cirno>2015-01-03 16:00:45 -0800
commit0b66de42e03697a3e0a931a869ee103146af2138 (patch)
tree941d8bdd8210fa6397d79ff55cfecdecb60ed3a5
parentaac52bce831c3ac80febb312ae21343e1a23c936 (diff)
downloadmc-0b66de42e03697a3e0a931a869ee103146af2138.tar.gz
Implement directory entries on Plan 9.
-rw-r--r--libstd/dir+plan9.myr66
-rw-r--r--libstd/syswrap+plan9.myr3
2 files changed, 68 insertions, 1 deletions
diff --git a/libstd/dir+plan9.myr b/libstd/dir+plan9.myr
index e69de29..41a579f 100644
--- a/libstd/dir+plan9.myr
+++ b/libstd/dir+plan9.myr
@@ -0,0 +1,66 @@
+use sys
+
+use "alloc.use"
+use "die.use"
+use "option.use"
+use "result.use"
+use "slcp.use"
+use "sldup.use"
+use "syswrap.use"
+use "types.use"
+
+pkg std =
+ type dir = struct
+ fd : fd
+ buf : byte[65536] /* a big big, but at least it will always hold a directory entry... */
+ len : int64
+ off : int64
+ ;;
+
+ const diropen : (p : byte[:] -> std.result(dir#, byte[:]))
+ const dirread : (d : dir# -> std.option(byte[:]))
+ const dirclose : (d : dir# -> void)
+;;
+
+const diropen = {p
+ var fd
+ var dir
+
+ fd = open(p, Ordonly)
+ if fd < 0
+ -> `Fail "couldn't open directory"
+ ;;
+
+ dir = zalloc()
+ dir.fd = fd
+ -> `Ok dir
+}
+
+
+const dirread = {d
+ var len : int64, name, base, namelen, dirlen
+
+ /* NB: On Plan 9, read(2) will always return whole directory entries */
+ if d.off >= d.len
+ len = read(d.fd, d.buf[:]) castto(int64)
+ if len <= 0
+ -> `None
+ ;;
+ d.len = len
+ d.off = 0
+ ;;
+
+ namelen = (d.buf[d.off + Stringsoff] castto(int64)) | \
+ ((d.buf[d.off + Stringsoff + 1] castto(int64)) << 8)
+ base = d.off + Stringsoff + 2
+ dirlen = (d.buf[d.off] castto(int64)) | \
+ ((d.buf[d.off + 1] castto(int64)) << 8)
+ name = d.buf[base:base + namelen]
+ d.off += dirlen + 2
+ -> `Some std.sldup(name)
+}
+
+const dirclose = {d
+ close(d.fd)
+ free(d)
+}
diff --git a/libstd/syswrap+plan9.myr b/libstd/syswrap+plan9.myr
index 28a1cdd..f6379b4 100644
--- a/libstd/syswrap+plan9.myr
+++ b/libstd/syswrap+plan9.myr
@@ -76,7 +76,8 @@ pkg std =
pkglocal const Qidversoff : int64 = Qidtypeoff + 1
pkglocal const Qidpathoff : int64 = Qidversoff + 4
pkglocal const Modeoff : int64 = Qidpathoff + 8
- pkglocal const Mtimeoff : int64 = Modeoff + 4
+ pkglocal const Atimeoff : int64 = Modeoff + 4
+ pkglocal const Mtimeoff : int64 = Atimeoff + 4
pkglocal const Lengthoff : int64 = Mtimeoff + 4
pkglocal const Stringsoff : int64 = Lengthoff + 8
;;