raz / commit 55f203f3

commit
55f203f3d73b6c286b460f94a0f633b110233ee4
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
4fe15c82
Files: writeAll, realPath and removeTree

patch · log

2 files changed, +279 -7

doc/raz-lib.md (history) modified +11 -0

@@ -874,6 +874,14 @@
874874 writes ` 3:07 U+00E9`.
875875
876876 **Files and Opt** are what most programs need of their command line.
877+`Files.writeAll` writes again after a short write or a signal, as a
878+pipe or a socket needs, and `write` is made over it. `realPath` adds
879+to `b` the absolute path of what `path` names, with no `.`, `..` or
880+symbolic link in it, and leaves the working directory alone: -2
881+(`ENOENT`) if a part isn't there, -20 (`ENOTDIR`) if a file is used as
882+a directory, and -40 (`ELOOP`) after 40 links. `b` is left as it was
883+on an error. `removeTree` removes a file, or a directory with all it
884+holds; a symbolic link is removed itself and never followed.
877885
878886 ```
879887 module Files;
@@ -881,6 +889,9 @@
881889 proc read*(path: []U8; var b: Buf.Buf): BOOL; (* the whole file added to b *)
882890 proc load*(path: []U8; var b: Buf.Buf): INT; (* as read: 0, or minus the errno that stopped it *)
883891 proc write*(path, data: []U8): BOOL; (* the file made to hold data; false if it or its close failed *)
892+proc writeAll*(fd: INT; s: []U8): INT; (* all of s written to fd; 0, or minus the errno *)
893+proc realPath*(path: []U8; var b: Buf.Buf): INT; (* the absolute path of path, plain, added to b; 0, or minus an errno *)
894+proc removeTree*(path: []U8): INT; (* the file, or the directory and all in it, removed; 0, or minus the first errno *)
884895
885896 module Opt;
886897 type

lib/Files.raz (history) modified +268 -7

@@ -1,5 +1,8 @@
11 (* What most programs need of the command line and of files: an
2- argument as a string, and a whole file read or written. *)
2+ argument as a string, a whole file read or written, all of a slice
3+ written to a descriptor, a path made absolute and plain, and a tree
4+ removed. It uses only what Sys has, and leaves out a walk of a tree
5+ (a program makes its own with Sys.openDir). *)
36 module Files;
47 import Sys, Buf, Heap, Rt;
58
@@ -38,23 +41,279 @@
3841 return load(path, b) = 0;
3942 end;
4043
44+(* Writes all of s to fd, again after a short write (a pipe, a socket)
45+ or a signal: 0, or minus the errno of the write that failed. *)
46+proc writeAll*(fd: INT; s: []U8): INT;
47+var n, at: INT;
48+begin
49+ at := 0;
50+ while at < LEN(s) do
51+ n := Sys.write(fd, s[at..LEN(s)]);
52+ if n > 0 then at := at + n;
53+ elsif n # Sys.errIntr then return n; (* a signal broke in: write again *)
54+ end;
55+ end;
56+ return 0;
57+end;
58+
4159 (* Makes the file at path hold data; false if it can't be written, or
4260 the close, where a late write error shows, fails. *)
4361 proc write*(path, data: []U8): BOOL;
44-var fd, n, at: INT;
62+var fd, r: INT;
4563 begin
4664 fd := Sys.create(path);
4765 if fd < 0 then return false; end;
66+ r := writeAll(fd, data);
67+ if r < 0 then Sys.close(fd); return false; end;
68+ return Sys.closeChecked(fd) = 0; (* a write error may come only at the close *)
69+end;
70+
71+(* The most symbolic links realPath follows: Linux's own bound. *)
72+const MaxLinks = 40;
73+
74+(* Takes the next name off rest, from at on, moving at past it; "" if
75+ there is none. *)
76+proc nextName(const rest: Buf.Buf; var at: INT): []U8;
77+var s: []U8; from: INT;
78+begin
79+ s := Buf.bytes(rest);
80+ while (at < LEN(s)) and (s[at] = '/') do INC(at); end;
81+ from := at;
82+ while (at < LEN(s)) and (s[at] # '/') do INC(at); end;
83+ return s[from..at];
84+end;
85+
86+(* Fails with -20 (ENOTDIR) unless res, the path found so far, is a
87+ directory; "" is the root. *)
88+proc mustBeDir(const res: Buf.Buf): INT;
89+var info: Sys.Info; r: INT;
90+begin
91+ if res.len = 0 then return 0; end;
92+ info, r := Sys.linkInfo(Buf.bytes(res));
93+ if r < 0 then return r; end;
94+ if not info.dir then return -20; end;
95+ return 0;
96+end;
97+
98+(* The link at res, whose name began at mark: takes its name off res
99+ and puts what it names in front of what is left of the path in
100+ rest, from at. A relative target is read from the link's directory,
101+ which res keeps; an absolute one starts again from the root. *)
102+proc followLink(var res, rest: Buf.Buf; mark: INT; var at, links: INT): INT;
103+var target: []U8; joined: Buf.Buf; r: INT;
104+begin
105+ INC(links);
106+ if links > MaxLinks then return -40; end; (* ELOOP *)
107+ target, r := Sys.readLink(Buf.bytes(res));
108+ if r < 0 then return r; end;
109+ Buf.init(joined, Heap.heap);
110+ Buf.str(joined, target);
111+ Buf.add(joined, '/');
112+ Buf.str(joined, Buf.bytes(rest)[at..rest.len]);
113+ Buf.cut(rest, 0);
114+ Buf.str(rest, Buf.bytes(joined));
48115 at := 0;
49- while at < LEN(data) do
50- n := Sys.write(fd, data[at..LEN(data)]);
51- if n > 0 then at := at + n;
52- elsif n # Sys.errIntr then Sys.close(fd); return false; (* a signal broke in: write again *)
116+ Buf.done(joined);
117+ if (LEN(target) > 0) and (target[0] = '/') then Buf.cut(res, 0); else Buf.cut(res, mark); end;
118+ return 0;
119+end;
120+
121+(* Adds one name of a path to res: nothing for ".", the parent for
122+ "..", the name itself for a file or directory, and for a link what
123+ it names, to be read next. *)
124+proc enter(var res, rest: Buf.Buf; var at, links: INT; name: []U8): INT;
125+var info: Sys.Info; mark, r: INT;
126+begin
127+ if (name = ".") or (name = "..") then
128+ r := mustBeDir(res);
129+ if r < 0 then return r; end;
130+ if name = ".." then
131+ mark := res.len;
132+ while (mark > 0) and (Buf.bytes(res)[mark - 1] # '/') do DEC(mark); end;
133+ if mark > 0 then Buf.cut(res, mark - 1); end; (* the root has no parent *)
53134 end;
135+ return 0;
54136 end;
55- return Sys.closeChecked(fd) = 0; (* a write error may come only at the close *)
137+ mark := res.len;
138+ Buf.add(res, '/');
139+ Buf.str(res, name);
140+ info, r := Sys.linkInfo(Buf.bytes(res));
141+ if r < 0 then return r; end;
142+ if not info.symlink then return 0; end;
143+ return followLink(res, rest, mark, at, links);
144+end;
145+
146+(* Adds to b the absolute path of the file at path, with no ".", "..",
147+ or symbolic link in it: 0, or minus an errno, -2 (ENOENT) if a part
148+ of it isn't there, -40 (ELOOP) after 40 links. The working
149+ directory stays as it is; b is left as it was on failure. *)
150+proc realPath*(path: []U8; var b: Buf.Buf): INT;
151+var res, rest: Buf.Buf; dir, name: []U8; at, links, r: INT;
152+begin
153+ if LEN(path) = 0 then return Sys.errNoEntry; end;
154+ Buf.init(res, Heap.heap);
155+ Buf.init(rest, Heap.heap);
156+ r := 0;
157+ if path[0] # '/' then
158+ dir, r := Sys.workDir();
159+ if dir # "/" then Buf.str(res, dir); end;
160+ end;
161+ Buf.str(rest, path);
162+ at := 0;
163+ links := 0;
164+ while r = 0 do
165+ name := nextName(rest, at);
166+ if LEN(name) = 0 then break; end;
167+ r := enter(res, rest, at, links, name);
168+ end;
169+ if r = 0 then
170+ if res.len = 0 then Buf.add(b, '/'); else Buf.str(b, Buf.bytes(res)); end;
171+ end;
172+ Buf.done(res);
173+ Buf.done(rest);
174+ return r;
175+end;
176+
177+proc removeTree*(path: []U8): INT; forward;
178+
179+(* Removes what is in the directory at path: 0, or minus the first
180+ errno, going on with the rest after it. *)
181+proc removeContents(path: []U8): INT;
182+var d: Sys.Dir; child: Buf.Buf; name: []U8; more: BOOL; mark, r, first: INT;
183+begin
184+ r := Sys.openDir(d, path);
185+ if r < 0 then return r; end;
186+ Buf.init(child, Heap.heap);
187+ Buf.str(child, path);
188+ if (LEN(path) = 0) or (path[LEN(path) - 1] # '/') then Buf.add(child, '/'); end;
189+ mark := child.len;
190+ first := 0;
191+ name, more := Sys.nextEntry(d);
192+ while more do
193+ Buf.cut(child, mark);
194+ Buf.str(child, name);
195+ r := removeTree(Buf.bytes(child));
196+ if (r < 0) and (first = 0) then first := r; end;
197+ name, more := Sys.nextEntry(d);
198+ end;
199+ Sys.closeDir(d);
200+ Buf.done(child);
201+ return first;
202+end;
203+
204+(* Removes the file at path and, if it is a directory, all within it,
205+ without following a symbolic link (the link goes, not what it
206+ names): 0, or minus the first errno. *)
207+proc removeTree*(path: []U8): INT;
208+var info: Sys.Info; r: INT;
209+begin
210+ info, r := Sys.linkInfo(path);
211+ if r < 0 then return r; end;
212+ if not info.dir then return Sys.remove(path); end;
213+ r := removeContents(path);
214+ if r < 0 then return r; end;
215+ return Sys.removeDir(path);
216+end;
217+
218+(* Sets b to base and then rest, and gives it. *)
219+proc join(var b: Buf.Buf; base, rest: []U8): []U8;
220+begin
221+ Buf.cut(b, 0);
222+ Buf.str(b, base);
223+ Buf.str(b, rest);
224+ return Buf.bytes(b);
225+end;
226+
227+(* Stops the test, saying what, unless realPath of path gives err and
228+ adds want to a buffer. *)
229+proc wantReal(path, want: []U8; err: INT; what: []U8);
230+var b: Buf.Buf; r: INT;
231+begin
232+ Buf.init(b, Heap.heap);
233+ r := realPath(path, b);
234+ if (r # err) or (Buf.bytes(b) # want) then Rt.stop(what); end;
235+ Buf.done(b);
236+end;
237+
238+(* A pipe written 300,000 bytes, more than it holds, by a copy of this
239+ program while this one reads: every byte arrives. *)
240+proc testPipe;
241+var rd, wr, n, total, pid, status: INT; b: Buf.Buf;
242+begin
243+ Buf.init(b, Heap.heap);
244+ n := 300000;
245+ INC(b.len, LEN(Buf.spare(b, n)));
246+ rd, wr := Sys.pipe();
247+ pid := Sys.fork();
248+ if pid = 0 then
249+ Sys.close(rd);
250+ Sys.exit(-writeAll(wr, Buf.bytes(b)));
251+ end;
252+ Sys.close(wr);
253+ total := 0;
254+ repeat
255+ n := Sys.read(rd, Buf.spare(b, 4096));
256+ if n > 0 then INC(total, n); end;
257+ until (n <= 0) and (n # Sys.errIntr);
258+ Sys.close(rd);
259+ _, _, status := Sys.wait(pid, true);
260+ if (total # 300000) or (status # 0) then Rt.stop("Files.writeAll: a pipe"); end;
261+ Buf.done(b);
56262 end;
57263
264+(* realPath and removeTree in a scratch tree, with links in it and one
265+ out of it. *)
266+proc testTree;
267+const T = "/tmp/raz-files-test.d"; Out = "/tmp/raz-files-test.out";
268+var p, q: Buf.Buf; here, now: []U8; r: INT;
269+begin
270+ Buf.init(p, Heap.heap);
271+ Buf.init(q, Heap.heap);
272+ _ := removeTree(T);
273+ _ := removeTree(Out);
274+ if (Sys.makeDir(T) # 0) or (Sys.makeDir(join(p, T, "/a")) # 0)
275+ or (Sys.makeDir(join(p, T, "/a/b")) # 0) or (Sys.makeDir(Out) # 0) then
276+ Rt.stop("Files: scratch tree");
277+ end;
278+ if not write(join(p, T, "/a/b/f"), "x") or not write(join(p, Out, "/keep"), "x") then
279+ Rt.stop("Files: scratch files");
280+ end;
281+ if (Sys.symlink("a/b", join(p, T, "/rel")) # 0)
282+ or (Sys.symlink(join(q, T, "/a"), join(p, T, "/abs")) # 0)
283+ or (Sys.symlink("../a/b", join(p, T, "/a/up")) # 0)
284+ or (Sys.symlink("loop", join(p, T, "/loop")) # 0)
285+ or (Sys.symlink(Out, join(p, T, "/a/ex")) # 0) then
286+ Rt.stop("Files: links");
287+ end;
288+ wantReal(join(p, T, "/rel/f"), join(q, T, "/a/b/f"), 0, "realPath: a relative link");
289+ wantReal(join(p, T, "/abs/b/../b/./f"), join(q, T, "/a/b/f"), 0, "realPath: absolute link, .. and .");
290+ wantReal(join(p, T, "/a/up/f"), join(q, T, "/a/b/f"), 0, "realPath: a link read from its directory");
291+ wantReal(join(p, T, "/a/"), join(q, T, "/a"), 0, "realPath: a trailing slash");
292+ wantReal(join(p, T, "/a/b/../../abs/b"), join(q, T, "/a/b"), 0, "realPath: .. then a link");
293+ wantReal(join(p, T, "/a/ex/keep"), join(q, Out, "/keep"), 0, "realPath: a link out of the tree");
294+ wantReal(join(p, T, "/a/nope"), "", Sys.errNoEntry, "realPath: a missing part");
295+ wantReal(join(p, T, "/a/b/f/.."), "", -20, "realPath: .. of a file");
296+ wantReal(join(p, T, "/loop"), "", -40, "realPath: a link loop");
297+ wantReal("", "", Sys.errNoEntry, "realPath: empty");
298+ wantReal("/", "/", 0, "realPath: the root");
299+ wantReal("//../..//", "/", 0, "realPath: past the root");
300+ here, r := Sys.workDir();
301+ Buf.cut(q, 0);
302+ Buf.cut(p, 0);
303+ Buf.str(q, here); (* here lasts only until the next Sys.workDir *)
304+ if realPath(".", p) # 0 then Rt.stop("realPath: ."); end;
305+ now, r := Sys.workDir();
306+ if (Buf.bytes(p) # Buf.bytes(q)) or (now # Buf.bytes(q)) then Rt.stop("realPath: the working directory"); end;
307+ if removeTree(join(p, T, "/a")) # 0 then Rt.stop("removeTree: a tree"); end;
308+ _, r := Sys.linkInfo(join(p, Out, "/keep"));
309+ if r # 0 then Rt.stop("removeTree: followed a link"); end;
310+ if removeTree(T) # 0 then Rt.stop("removeTree: the rest"); end;
311+ if removeTree(T) # Sys.errNoEntry then Rt.stop("removeTree: no such file"); end;
312+ if removeTree(Out) # 0 then Rt.stop("removeTree: the outside"); end;
313+ Buf.done(p);
314+ Buf.done(q);
315+end;
316+
58317 (* The module's test. *)
59318 proc main*;
60319 var b: Buf.Buf;
@@ -69,6 +328,8 @@
69328 if read("/", b) or (b.len # 6) then Rt.stop("Files.read: a directory"); end;
70329 if (LEN(arg(ARGC())) # 0) or (LEN(arg(-1)) # 0) then Rt.stop("Files.arg: past the arguments"); end;
71330 Buf.done(b);
331+ testPipe;
332+ testTree;
72333 end;
73334
74335 end Files.