files.bend
bend-kit-files@0.1.1.0 · 295 lines · 0 definitions
1# POSIX paths plus directory and metadata effects. Source: https://github.com/paymog/bend-kit/tree/main/files
2import Base
3import bend-kit-bytes@0.3.1.0/bytes.bend as Bytes
4
5# The path.* defs are pure and lexical: they never touch the disk.
6# The effects are thin OS calls; each failure is (errno, text).
7# import ./files/files.bend as Fs
8
9def path.is_absolute(p: String) -> Bool:
10 String.starts_with(p, "/")
11
12def path.join.rel(bare: Bool, a: String, b: String) -> String:
13 match bare:
14 case True{}:
15 a ++ b
16 case False{}:
17 a ++ "/" ++ b
18
19def path.join.if(abs: Bool, +a: String, b: String) -> String:
20 match abs:
21 case True{}:
22 b
23 case False{}:
24 path.join.rel(String.is_empty(a) || String.ends_with(a, "/"), a, b)
25
26# b when b is absolute; otherwise b under a.
27def path.join(a: String, +b: String) -> String:
28 path.join.if(path.is_absolute(b), a, b)
29
30def path.norm.pop.if(up: Bool, h: String, t: List<&2, String>) ->
31 List<&2, String>:
32 match up:
33 case True{}:
34 ".." <> (h <> t)
35 case False{}:
36 t
37
38# A ".." above the root is dropped; above a relative start it is kept.
39def path.norm.pop(stack: List<&2, String>, abs: Bool) -> List<&2, String>:
40 match stack:
41 case Nil{}:
42 Bool.pick(List<&2, String>, abs, Nil{}, [".."])
43 case Con{+h, t}:
44 path.norm.pop.if(String.eq(h, ".."), h, t)
45
46def path.norm.step(
47 skip: Bool, up: Bool, s: String, stack: List<&2, String>, abs: Bool
48) -> List<&2, String>:
49 match skip:
50 case True{}:
51 stack
52 case False{}:
53 match up:
54 case True{}:
55 path.norm.pop(stack, abs)
56 case False{}:
57 s <> stack
58
59# The kept segments, last first.
60def path.norm.go(segs: List<&2, String>, stack: List<&2, String>, +abs: Bool) ->
61 List<&2, String>:
62 match segs:
63 case Nil{}:
64 stack
65 case Con{+s, t}:
66 path.norm.go(t,
67 path.norm.step(String.eq(s, "") || String.eq(s, "."), String.eq(s, ".."),
68 s, stack, abs),
69 abs)
70
71def path.norm.fin(abs: Bool, body: String) -> String:
72 match abs:
73 case True{}:
74 "/" ++ body
75 case False{}:
76 match body:
77 case SNil{}:
78 "."
79 case SCon{h, t}:
80 SCon{h, t}
81
82# Drops empty and "." segments and resolves "..", as POSIX normpath does
83# (RFC 3986 §5.2.4 for paths), but always to one leading "/".
84def path.normalize(+p: String) -> String:
85 +abs = path.is_absolute(p)
86 path.norm.fin(abs,
87 String.join(List.reverse(&2, String,
88 path.norm.go(String.split(p, '/'), Nil{}, abs)), "/"))
89
90def path.parent(p: String) -> String:
91 +n = path.normalize(p)
92 path.norm.fin(path.is_absolute(n),
93 String.join(List.reverse(&2, String,
94 List.tail(&2, String, List.reverse(&2, String, String.split(n, '/')))), "/"))
95
96def path.file_name.fin(m: Maybe<&2, String>) -> String:
97 match m:
98 case None{}:
99 ""
100 case Some{+s}:
101 Bool.pick(String, String.eq(s, "."), "", s)
102
103# The last segment of the normalized path; "" for "/" and ".".
104def path.file_name(p: String) -> String:
105 path.file_name.fin(List.last(&2, String, String.split(path.normalize(p), '/')))
106
107def path.extension.fin(bare: Bool, ext: String) -> String:
108 match bare:
109 case True{}:
110 ""
111 case False{}:
112 ext
113
114def path.extension.go(rev: List<&2, String>) -> String:
115 match rev:
116 case Nil{}:
117 ""
118 case Con{ext, Nil{}}:
119 ""
120 case Con{ext, Con{prev, rest}}:
121 path.extension.fin(String.is_empty(prev) && List.is_empty(&2, String, rest),
122 ext)
123
124# The text after the file name's last ".", without the dot. A leading dot
125# does not start one: ".bashrc" has none.
126def path.extension(p: String) -> String:
127 path.extension.go(List.reverse(&2, String, String.split(path.file_name(p), '.')))
128
129type FileType is Data:
130 Regular{}
131 Directory{}
132 Other{}
133
134# mtime is whole seconds since the Unix epoch. size and mtime wrap at 2^32.
135type Info is Data:
136 Info{kind: FileType, size: U32, mtime: U32}
137
138# Entry names, NUL-separated, in the order the OS gives them.
139def list_dir.raw(path: String) -> IO(Result<&1, &1, U32 & String, String>):
140 import "./effs/files.c"
141 import "./effs/files.js"
142
143# (kind, size, mtime); kind 0 is a regular file, 1 a directory, 2 other.
144# Symbolic links are followed.
145def stat.raw(path: String) ->
146 IO(Result<&1, &1, U32 & String, U32 & U32 & U32>):
147 import "./effs/files.c"
148 import "./effs/files.js"
149
150def mkdir(path: String) -> IO(Result<&1, &1, U32 & String, Unit>):
151 import "./effs/files.c"
152 import "./effs/files.js"
153
154# Removes a file or an empty directory.
155def remove(path: String) -> IO(Result<&1, &1, U32 & String, Unit>):
156 import "./effs/files.c"
157 import "./effs/files.js"
158
159def rename(from: String, to: String) ->
160 IO(Result<&1, &1, U32 & String, Unit>):
161 import "./effs/files.c"
162 import "./effs/files.js"
163
164# Creates a new, empty, private directory under the system temp dir.
165def temp_dir() -> IO(Result<&1, &1, U32 & String, String>):
166 import "./effs/files.c"
167 import "./effs/files.js"
168
169# A successful zero-length read is EOF (or max = 0). Reads at most 1 MiB
170# per call; a short read is not necessarily EOF. Errors retain the File.
171def read.words(file: File, max: U32) ->
172 IO(File & Result<&1, &1, U32 & String, U32 & Array<U32>>):
173 import "./effs/files.c"
174 import "./effs/files.js"
175
176def read_bytes.wrap(result: Result<&1, &1, U32 & String, U32 & Array<U32>>) ->
177 Result<&1, &1, U32 & String, Bytes.Bytes>:
178 match result:
179 case Fail{err}:
180 Fail{err}
181 case Done{(len, words)}:
182 Done{Bytes.Bytes{len, words}}
183
184def read_bytes.pair(pair: File & Result<&1, &1, U32 & String, U32 & Array<U32>>) ->
185 File & Result<&1, &1, U32 & String, Bytes.Bytes>:
186 match pair:
187 case (handle, result):
188 (handle, read_bytes.wrap(result))
189
190# The existing Bytes layout, without an octet List or String.
191def read_bytes(file: File, max: U32) ->
192 IO(File & Result<&1, &1, U32 & String, Bytes.Bytes>):
193 do IO<File & Result<&1, &1, U32 & String, Bytes.Bytes>>:
194 pair : File & Result<&1, &1, U32 & String, U32 & Array<U32>> <- read.words(file, max)
195 return read_bytes.pair(pair)
196
197# Writes the entire buffer or fails. Errors retain the File; writes before
198# an error are not rolled back. A len beyond the word array fails EINVAL.
199def write.words(file: File, len: U32, words: Array<U32>) ->
200 IO(File & Result<&1, &1, U32 & String, Unit>):
201 import "./effs/files.c"
202 import "./effs/files.js"
203
204def write_bytes(file: File, data: Bytes.Bytes) ->
205 IO(File & Result<&1, &1, U32 & String, Unit>):
206 Bytes.Bytes{len, buf} = data
207 write.words(file, len, buf)
208
209def list_dir.fin(r: Result<&1, &1, U32 & String, String>) ->
210 Result<&1, &1, U32 & String, List<&2, String>>:
211 match r:
212 case Fail{e}:
213 Fail{e}
214 case Done{s}:
215 Done{List.sort(~String, ~String.is_le,
216 List.filter(~String, ~(n => Bool.not(String.is_empty(n))),
217 String.split(s, Chr{0})))}
218
219# Entry names, without "." and "..", sorted.
220def list_dir(path: String) ->
221 IO(Result<&1, &1, U32 & String, List<&2, String>>):
222 do IO<Result<&1, &1, U32 & String, List<&2, String>>>:
223 r : Result<&1, &1, U32 & String, String> <- list_dir.raw(path)
224 return list_dir.fin(r)
225
226def stat.kind(+k: U32) -> FileType:
227 Bool.pick(FileType, U32.is_eq(k, 0), Regular{},
228 Bool.pick(FileType, U32.is_eq(k, 1), Directory{}, Other{}))
229
230def stat.fin(r: Result<&1, &1, U32 & String, U32 & U32 & U32>) ->
231 Result<&1, &1, U32 & String, Info>:
232 match r:
233 case Fail{e}:
234 Fail{e}
235 case Done{(k, size, mtime)}:
236 Done{Info{stat.kind(k), size, mtime}}
237
238def stat(path: String) -> IO(Result<&1, &1, U32 & String, Info>):
239 do IO<Result<&1, &1, U32 & String, Info>>:
240 r : Result<&1, &1, U32 & String, U32 & U32 & U32> <- stat.raw(path)
241 return stat.fin(r)
242
243def exists(path: String) -> IO(Bool):
244 do IO<Bool>:
245 r : Result<&1, &1, U32 & String, U32 & U32 & U32> <- stat.raw(path)
246 return Result.is_done(&1, &1, U32 & String, U32 & U32 & U32, r)
247
248def mkdir_all.dir(r: Result<&1, &1, U32 & String, Info>) ->
249 Result<&1, &1, U32 & String, Unit>:
250 match r:
251 case Fail{e}:
252 Fail{e}
253 case Done{Info{Directory{}, size, mtime}}:
254 Done{Unit{}}
255 case Done{i}:
256 Fail{(17, "File exists")}
257
258def mkdir_all.fail(exists: Bool, e: U32 & String,
259 k: IO(Result<&1, &1, U32 & String, Unit>)) ->
260 IO(Result<&1, &1, U32 & String, Unit>):
261 match exists:
262 case True{}:
263 k
264 case False{}:
265 IO.pure(Result<&1, &1, U32 & String, Unit>, Fail{e})
266
267def mkdir_all.step(r: Result<&1, &1, U32 & String, Unit>,
268 k: IO(Result<&1, &1, U32 & String, Unit>)) ->
269 IO(Result<&1, &1, U32 & String, Unit>):
270 match r:
271 case Fail{(+code, text)}:
272 mkdir_all.fail(U32.is_eq(code, 17), (code, text), k)
273 case Done{u}:
274 k
275
276def mkdir_all.go(segs: List<&2, String>, +at: String) ->
277 IO(Result<&1, &1, U32 & String, Unit>):
278 match segs:
279 case Nil{}:
280 do IO<Result<&1, &1, U32 & String, Unit>>:
281 r : Result<&1, &1, U32 & String, Info> <- stat(at)
282 return mkdir_all.dir(r)
283 case Con{s, t}:
284 +next = path.join(at, s)
285 do IO<Result<&1, &1, U32 & String, Unit>>:
286 r : Result<&1, &1, U32 & String, Unit> <- mkdir(next)
287 mkdir_all.step(r, mkdir_all.go(t, next))
288
289# Creates the directory and any missing parents. Succeeds when it is
290# already a directory; fails if a component is not a directory.
291def mkdir_all(+path: String) -> IO(Result<&1, &1, U32 & String, Unit>):
292 mkdir_all.go(
293 List.filter(~String, ~(n => Bool.not(String.eq(n, ""))),
294 String.split(path, '/')),
295 Bool.pick(String, path.is_absolute(path), "/", "."))