bitwise shrub/kav / lib / fs.kav
 1use <prelude.kav>
 2use <rt.kav> as rt
 3use <result.kav> as res
 4
 5pub abstract linear datatype File {
 6    | File(rt::Fd)
 7}
 8
 9pub fn fd(f: !File) -> int {
10    val File(fd) = f
11    rt::num(fd)
12}
13
14fn open(w: !World, path: cstr, flags: int, mode: int) -> res::Result[File, int] {
15    val fd = rt::open(w, path, flags, mode) 
16    if fd < 0 then res::Err(0 - fd) else res::Ok(File(rt::fd(w, fd)))
17}
18
19pub fn openread(w: !World, path: cstr) -> res::Result[File, int] {
20    open(w, path, 0, 0)
21}
22
23pub fn opentrunc(w: !World, path: cstr) -> res::Result[File, int] {
24    open(w, path, 1 + 64 + 512, 420)
25}
26
27pub fn openappend(w: !World, path: cstr) -> res::Result[File, int] {
28    open(w, path, 1 + 64 + 1024, 420)
29}
30
31/% Release the File, returning its Fd. Now you have to close it. %/
32pub fn intofd(f: File) -> rt::Fd {
33    val ~File(fd) = f
34    fd
35}
36
37pub fn close(f: File) -> int {
38    val ~File(fd) = f
39    rt::close(fd)
40}
41
42pub fn read(f: !File, p: ptr, n: int) -> int {
43    val File(fd) = f 
44    rt::read(fd, p, n)
45}
46
47
48pub fn write(f: !File, p: ptr, n: int) -> int {
49    val File(fd) = f 
50    rt::write(fd, p, n)
51}
52
53pub fn mkdir(w: !World, path: cstr, mode: int) -> res::Result[one, int] {
54    val r = rt::mkdir(w, path, mode)
55    if r < 0 then res::Err(0 - r) else res::Ok(one)
56}
57
58pub fn rmdir(w: !World, path: cstr) -> res::Result[one, int] {
59    val r = rt::rmdir(w, path)
60    if r < 0 then res::Err(0 - r) else res::Ok(one)
61}