(* What most programs need of the command line and of files: an argument as a string, a whole file read or written, all of a slice written to a descriptor, a path made absolute and plain, and a tree removed. It uses only what Sys has, and leaves out a walk of a tree (a program makes its own with Sys.openDir). *) module Files; import Sys, Buf, Heap, Rt; import SYSTEM; (* The number of arguments, the program's name included; argument i exists for i below it. *) proc argCount*: INT; begin return ARGC(); end; (* Argument i, a C string, as a slice; empty if there is no argument i, as for i below 0 or from ARGC() on. *) proc arg*(i: INT): []const U8; var s: ^U8; n: INT; begin if (i < 0) or (i >= ARGC()) then return ""; end; s := SYSTEM.ARGV()[i]; n := 0; while s[n] # 0 do INC(n); end; return s[0..n]; end; (* Adds the file at path to b: 0, or minus the errno of the open or the read that failed, as Sys gives them: -2 (ENOENT) for no such file, -13 (EACCES) for one not allowed, -21 (EISDIR) for a directory. What was read before a read failed stays in b. *) proc load*(path: []const U8; var b: Buf.Buf): INT; var fd, n: INT; begin fd := Sys.openRead(path); if fd < 0 then return fd; end; repeat n := Sys.read(fd, Buf.spare(b, 65536)); (* straight into b *) if n > 0 then INC(b.len, n); end; until (n <= 0) and (n # Sys.errIntr); (* a signal Sys.handle took; read again *) Sys.close(fd); return n; end; (* Adds the file at path to b; false if it can't be read. *) proc read*(path: []const U8; var b: Buf.Buf): BOOL; begin return load(path, b) = 0; end; (* Writes all of s to fd, again after a short write (a pipe, a socket) or a signal: 0, or minus the errno of the write that failed. *) proc writeAll*(fd: INT; s: []const U8): INT; var n, at: INT; begin at := 0; while at < LEN(s) do n := Sys.write(fd, s[at..LEN(s)]); if n > 0 then at := at + n; elsif n # Sys.errIntr then return n; (* a signal broke in: write again *) end; end; return 0; end; (* Makes the file at path hold data; false if it can't be written, or the close, where a late write error shows, fails. *) proc write*(path, data: []const U8): BOOL; var fd, r: INT; begin fd := Sys.create(path); if fd < 0 then return false; end; r := writeAll(fd, data); if r < 0 then Sys.close(fd); return false; end; return Sys.closeChecked(fd) = 0; (* a write error may come only at the close *) end; (* The most symbolic links realPath follows: Linux's own bound. *) const MaxLinks = 40; (* Takes the next name off rest, from at on, moving at past it; "" if there is none. *) proc nextName(const rest: Buf.Buf; var at: INT): []U8; var s: []U8; from: INT; begin s := Buf.bytes(rest); while (at < LEN(s)) and (s[at] = '/') do INC(at); end; from := at; while (at < LEN(s)) and (s[at] # '/') do INC(at); end; return s[from..at]; end; (* Fails with -20 (ENOTDIR) unless res, the path found so far, is a directory; "" is the root. *) proc mustBeDir(const res: Buf.Buf): INT; var info: Sys.Info; r: INT; begin if res.len = 0 then return 0; end; info, r := Sys.linkInfo(Buf.bytes(res)); if r < 0 then return r; end; if not info.dir then return -20; end; return 0; end; (* The link at res, whose name began at mark: takes its name off res and puts what it names in front of what is left of the path in rest, from at. A relative target is read from the link's directory, which res keeps; an absolute one starts again from the root. *) proc followLink(var res, rest: Buf.Buf; mark: INT; var at, links: INT): INT; var target: []const U8; joined: Buf.Buf; r: INT; begin INC(links); if links > MaxLinks then return -40; end; (* ELOOP *) target, r := Sys.readLink(Buf.bytes(res)); if r < 0 then return r; end; Buf.init(joined, Heap.heap); Buf.str(joined, target); Buf.add(joined, '/'); Buf.str(joined, Buf.bytes(rest)[at..rest.len]); Buf.cut(rest, 0); Buf.str(rest, Buf.bytes(joined)); at := 0; Buf.done(joined); if (LEN(target) > 0) and (target[0] = '/') then Buf.cut(res, 0); else Buf.cut(res, mark); end; return 0; end; (* Adds one name of a path to res: nothing for ".", the parent for "..", the name itself for a file or directory, and for a link what it names, to be read next. *) proc enter(var res, rest: Buf.Buf; var at, links: INT; name: []const U8): INT; var info: Sys.Info; mark, r: INT; begin if (name = ".") or (name = "..") then r := mustBeDir(res); if r < 0 then return r; end; if name = ".." then mark := res.len; while (mark > 0) and (Buf.bytes(res)[mark - 1] # '/') do DEC(mark); end; if mark > 0 then Buf.cut(res, mark - 1); end; (* the root has no parent *) end; return 0; end; mark := res.len; Buf.add(res, '/'); Buf.str(res, name); info, r := Sys.linkInfo(Buf.bytes(res)); if r < 0 then return r; end; if not info.symlink then return 0; end; return followLink(res, rest, mark, at, links); end; (* Adds to b the absolute path of the file at path, with no ".", "..", or symbolic link in it: 0, or minus an errno, -2 (ENOENT) if a part of it isn't there, -40 (ELOOP) after 40 links. The working directory stays as it is; b is left as it was on failure. *) proc realPath*(path: []const U8; var b: Buf.Buf): INT; var res, rest: Buf.Buf; dir, name: []const U8; at, links, r: INT; begin if LEN(path) = 0 then return Sys.errNoEntry; end; Buf.init(res, Heap.heap); Buf.init(rest, Heap.heap); r := 0; if path[0] # '/' then dir, r := Sys.workDir(); if dir # "/" then Buf.str(res, dir); end; end; Buf.str(rest, path); at := 0; links := 0; while r = 0 do name := nextName(rest, at); if LEN(name) = 0 then break; end; r := enter(res, rest, at, links, name); end; if r = 0 then if res.len = 0 then Buf.add(b, '/'); else Buf.str(b, Buf.bytes(res)); end; end; Buf.done(res); Buf.done(rest); return r; end; proc removeTree*(path: []const U8): INT; forward; (* Removes what is in the directory at path: 0, or minus the first errno, going on with the rest after it. *) proc removeContents(path: []const U8): INT; var d: Sys.Dir; child: Buf.Buf; name: []const U8; more: BOOL; mark, r, first: INT; begin r := Sys.openDir(d, path); if r < 0 then return r; end; Buf.init(child, Heap.heap); Buf.str(child, path); if (LEN(path) = 0) or (path[LEN(path) - 1] # '/') then Buf.add(child, '/'); end; mark := child.len; first := 0; name, more := Sys.nextEntry(d); while more do Buf.cut(child, mark); Buf.str(child, name); r := removeTree(Buf.bytes(child)); if (r < 0) and (first = 0) then first := r; end; name, more := Sys.nextEntry(d); end; Sys.closeDir(d); Buf.done(child); return first; end; (* Removes the file at path and, if it is a directory, all within it, without following a symbolic link (the link goes, not what it names): 0, or minus the first errno. *) proc removeTree*(path: []const U8): INT; var info: Sys.Info; r: INT; begin info, r := Sys.linkInfo(path); if r < 0 then return r; end; if not info.dir then return Sys.remove(path); end; r := removeContents(path); if r < 0 then return r; end; return Sys.removeDir(path); end; (* Sets b to base and then rest, and gives it. *) proc join(var b: Buf.Buf; base, rest: []const U8): []U8; begin Buf.cut(b, 0); Buf.str(b, base); Buf.str(b, rest); return Buf.bytes(b); end; (* Stops the test, saying what, unless realPath of path gives err and adds want to a buffer. *) proc wantReal(path, want: []const U8; err: INT; what: []const U8); var b: Buf.Buf; r: INT; begin Buf.init(b, Heap.heap); r := realPath(path, b); if (r # err) or (Buf.bytes(b) # want) then Rt.stop(what); end; Buf.done(b); end; (* A pipe written 300,000 bytes, more than it holds, by a copy of this program while this one reads: every byte arrives. *) proc testPipe; var rd, wr, n, total, pid, status: INT; b: Buf.Buf; begin Buf.init(b, Heap.heap); n := 300000; INC(b.len, LEN(Buf.spare(b, n))); rd, wr := Sys.pipe(); pid := Sys.fork(); if pid = 0 then Sys.close(rd); Sys.exit(-writeAll(wr, Buf.bytes(b))); end; Sys.close(wr); total := 0; repeat n := Sys.read(rd, Buf.spare(b, 4096)); if n > 0 then INC(total, n); end; until (n <= 0) and (n # Sys.errIntr); Sys.close(rd); _, _, status := Sys.wait(pid, true); if (total # 300000) or (status # 0) then Rt.stop("Files.writeAll: a pipe"); end; Buf.done(b); end; (* realPath and removeTree in a scratch tree, with links in it and one out of it. What realPath gives is under /tmp as realPath gives it, which on a Mac is /private/tmp, /tmp being a link to it. *) proc testTree; const T = "/tmp/raz-files-test.d"; Out = "/tmp/raz-files-test.out"; var p, q, rt, ro: Buf.Buf; here, now, realT, realOut: []const U8; r: INT; begin Buf.init(p, Heap.heap); Buf.init(q, Heap.heap); Buf.init(rt, Heap.heap); Buf.init(ro, Heap.heap); if realPath("/tmp", rt) # 0 then Rt.stop("realPath: /tmp"); end; Buf.str(ro, Buf.bytes(rt)); Buf.str(rt, "/raz-files-test.d"); Buf.str(ro, "/raz-files-test.out"); realT := Buf.bytes(rt); realOut := Buf.bytes(ro); _ := removeTree(T); _ := removeTree(Out); if (Sys.makeDir(T) # 0) or (Sys.makeDir(join(p, T, "/a")) # 0) or (Sys.makeDir(join(p, T, "/a/b")) # 0) or (Sys.makeDir(Out) # 0) then Rt.stop("Files: scratch tree"); end; if not write(join(p, T, "/a/b/f"), "x") or not write(join(p, Out, "/keep"), "x") then Rt.stop("Files: scratch files"); end; if (Sys.symlink("a/b", join(p, T, "/rel")) # 0) or (Sys.symlink(join(q, T, "/a"), join(p, T, "/abs")) # 0) or (Sys.symlink("../a/b", join(p, T, "/a/up")) # 0) or (Sys.symlink("loop", join(p, T, "/loop")) # 0) or (Sys.symlink(Out, join(p, T, "/a/ex")) # 0) then Rt.stop("Files: links"); end; wantReal(join(p, T, "/rel/f"), join(q, realT, "/a/b/f"), 0, "realPath: a relative link"); wantReal(join(p, T, "/abs/b/../b/./f"), join(q, realT, "/a/b/f"), 0, "realPath: absolute link, .. and ."); wantReal(join(p, T, "/a/up/f"), join(q, realT, "/a/b/f"), 0, "realPath: a link read from its directory"); wantReal(join(p, T, "/a/"), join(q, realT, "/a"), 0, "realPath: a trailing slash"); wantReal(join(p, T, "/a/b/../../abs/b"), join(q, realT, "/a/b"), 0, "realPath: .. then a link"); wantReal(join(p, T, "/a/ex/keep"), join(q, realOut, "/keep"), 0, "realPath: a link out of the tree"); wantReal(join(p, T, "/a/nope"), "", Sys.errNoEntry, "realPath: a missing part"); wantReal(join(p, T, "/a/b/f/.."), "", -20, "realPath: .. of a file"); wantReal(join(p, T, "/loop"), "", -40, "realPath: a link loop"); wantReal("", "", Sys.errNoEntry, "realPath: empty"); wantReal("/", "/", 0, "realPath: the root"); wantReal("//../..//", "/", 0, "realPath: past the root"); here, r := Sys.workDir(); Buf.cut(q, 0); Buf.cut(p, 0); Buf.str(q, here); (* here lasts only until the next Sys.workDir *) if realPath(".", p) # 0 then Rt.stop("realPath: ."); end; now, r := Sys.workDir(); if (Buf.bytes(p) # Buf.bytes(q)) or (now # Buf.bytes(q)) then Rt.stop("realPath: the working directory"); end; if removeTree(join(p, T, "/a")) # 0 then Rt.stop("removeTree: a tree"); end; _, r := Sys.linkInfo(join(p, Out, "/keep")); if r # 0 then Rt.stop("removeTree: followed a link"); end; if removeTree(T) # 0 then Rt.stop("removeTree: the rest"); end; if removeTree(T) # Sys.errNoEntry then Rt.stop("removeTree: no such file"); end; if removeTree(Out) # 0 then Rt.stop("removeTree: the outside"); end; Buf.done(p); Buf.done(q); Buf.done(rt); Buf.done(ro); end; (* The module's test. *) proc main*; var b: Buf.Buf; begin Buf.init(b, Heap.heap); if not write("/tmp/raz-files-test", "abc") then Rt.stop("Files.write"); end; if (load("/tmp/raz-files-test", b) # 0) or (Buf.bytes(b) # "abc") then Rt.stop("Files.load"); end; if not read("/tmp/raz-files-test", b) or (Buf.bytes(b) # "abcabc") then Rt.stop("Files.read"); end; _ := Sys.remove("/tmp/raz-files-test"); if load("/tmp/raz-files-test", b) # Sys.errNoEntry then Rt.stop("Files.load: no such file"); end; if load("/", b) # -21 then Rt.stop("Files.load: a directory"); end; if read("/", b) or (b.len # 6) then Rt.stop("Files.read: a directory"); end; if (argCount() < 1) or (LEN(arg(0)) = 0) then Rt.stop("Files.argCount"); end; if (LEN(arg(argCount())) # 0) or (LEN(arg(-1)) # 0) then Rt.stop("Files.arg: past the arguments"); end; Buf.done(b); testPipe; testTree; end; end Files.