| 1 | (* What most programs need of the command line and of files: an |
| 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). *) |
| 6 | module Files; |
| 7 | import Sys, Buf, Heap, Rt; |
| 8 | import SYSTEM; |
| 9 | |
| 10 | (* The number of arguments, the program's name included; argument i |
| 11 | exists for i below it. *) |
| 12 | proc argCount*: INT; |
| 13 | begin |
| 14 | return ARGC(); |
| 15 | end; |
| 16 | |
| 17 | (* Argument i, a C string, as a slice; empty if there is no argument i, |
| 18 | as for i below 0 or from ARGC() on. *) |
| 19 | proc arg*(i: INT): []const U8; |
| 20 | var s: ^U8; n: INT; |
| 21 | begin |
| 22 | if (i < 0) or (i >= ARGC()) then return ""; end; |
| 23 | s := SYSTEM.ARGV()[i]; |
| 24 | n := 0; |
| 25 | while s[n] # 0 do INC(n); end; |
| 26 | return s[0..n]; |
| 27 | end; |
| 28 | |
| 29 | (* Adds the file at path to b: 0, or minus the errno of the open or |
| 30 | the read that failed, as Sys gives them: -2 (ENOENT) for no such |
| 31 | file, -13 (EACCES) for one not allowed, -21 (EISDIR) for a |
| 32 | directory. What was read before a read failed stays in b. *) |
| 33 | proc load*(path: []const U8; var b: Buf.Buf): INT; |
| 34 | var fd, n: INT; |
| 35 | begin |
| 36 | fd := Sys.openRead(path); |
| 37 | if fd < 0 then return fd; end; |
| 38 | repeat |
| 39 | n := Sys.read(fd, Buf.spare(b, 65536)); (* straight into b *) |
| 40 | if n > 0 then INC(b.len, n); end; |
| 41 | until (n <= 0) and (n # Sys.errIntr); (* a signal Sys.handle took; read again *) |
| 42 | Sys.close(fd); |
| 43 | return n; |
| 44 | end; |
| 45 | |
| 46 | (* Adds the file at path to b; false if it can't be read. *) |
| 47 | proc read*(path: []const U8; var b: Buf.Buf): BOOL; |
| 48 | begin |
| 49 | return load(path, b) = 0; |
| 50 | end; |
| 51 | |
| 52 | (* Writes all of s to fd, again after a short write (a pipe, a socket) |
| 53 | or a signal: 0, or minus the errno of the write that failed. *) |
| 54 | proc writeAll*(fd: INT; s: []const U8): INT; |
| 55 | var n, at: INT; |
| 56 | begin |
| 57 | at := 0; |
| 58 | while at < LEN(s) do |
| 59 | n := Sys.write(fd, s[at..LEN(s)]); |
| 60 | if n > 0 then at := at + n; |
| 61 | elsif n # Sys.errIntr then return n; (* a signal broke in: write again *) |
| 62 | end; |
| 63 | end; |
| 64 | return 0; |
| 65 | end; |
| 66 | |
| 67 | (* Makes the file at path hold data; false if it can't be written, or |
| 68 | the close, where a late write error shows, fails. *) |
| 69 | proc write*(path, data: []const U8): BOOL; |
| 70 | var fd, r: INT; |
| 71 | begin |
| 72 | fd := Sys.create(path); |
| 73 | if fd < 0 then return false; end; |
| 74 | r := writeAll(fd, data); |
| 75 | if r < 0 then Sys.close(fd); return false; end; |
| 76 | return Sys.closeChecked(fd) = 0; (* a write error may come only at the close *) |
| 77 | end; |
| 78 | |
| 79 | (* The most symbolic links realPath follows: Linux's own bound. *) |
| 80 | const MaxLinks = 40; |
| 81 | |
| 82 | (* Takes the next name off rest, from at on, moving at past it; "" if |
| 83 | there is none. *) |
| 84 | proc nextName(const rest: Buf.Buf; var at: INT): []U8; |
| 85 | var s: []U8; from: INT; |
| 86 | begin |
| 87 | s := Buf.bytes(rest); |
| 88 | while (at < LEN(s)) and (s[at] = '/') do INC(at); end; |
| 89 | from := at; |
| 90 | while (at < LEN(s)) and (s[at] # '/') do INC(at); end; |
| 91 | return s[from..at]; |
| 92 | end; |
| 93 | |
| 94 | (* Fails with -20 (ENOTDIR) unless res, the path found so far, is a |
| 95 | directory; "" is the root. *) |
| 96 | proc mustBeDir(const res: Buf.Buf): INT; |
| 97 | var info: Sys.Info; r: INT; |
| 98 | begin |
| 99 | if res.len = 0 then return 0; end; |
| 100 | info, r := Sys.linkInfo(Buf.bytes(res)); |
| 101 | if r < 0 then return r; end; |
| 102 | if not info.dir then return -20; end; |
| 103 | return 0; |
| 104 | end; |
| 105 | |
| 106 | (* The link at res, whose name began at mark: takes its name off res |
| 107 | and puts what it names in front of what is left of the path in |
| 108 | rest, from at. A relative target is read from the link's directory, |
| 109 | which res keeps; an absolute one starts again from the root. *) |
| 110 | proc followLink(var res, rest: Buf.Buf; mark: INT; var at, links: INT): INT; |
| 111 | var target: []const U8; joined: Buf.Buf; r: INT; |
| 112 | begin |
| 113 | INC(links); |
| 114 | if links > MaxLinks then return -40; end; (* ELOOP *) |
| 115 | target, r := Sys.readLink(Buf.bytes(res)); |
| 116 | if r < 0 then return r; end; |
| 117 | Buf.init(joined, Heap.heap); |
| 118 | Buf.str(joined, target); |
| 119 | Buf.add(joined, '/'); |
| 120 | Buf.str(joined, Buf.bytes(rest)[at..rest.len]); |
| 121 | Buf.cut(rest, 0); |
| 122 | Buf.str(rest, Buf.bytes(joined)); |
| 123 | at := 0; |
| 124 | Buf.done(joined); |
| 125 | if (LEN(target) > 0) and (target[0] = '/') then Buf.cut(res, 0); else Buf.cut(res, mark); end; |
| 126 | return 0; |
| 127 | end; |
| 128 | |
| 129 | (* Adds one name of a path to res: nothing for ".", the parent for |
| 130 | "..", the name itself for a file or directory, and for a link what |
| 131 | it names, to be read next. *) |
| 132 | proc enter(var res, rest: Buf.Buf; var at, links: INT; name: []const U8): INT; |
| 133 | var info: Sys.Info; mark, r: INT; |
| 134 | begin |
| 135 | if (name = ".") or (name = "..") then |
| 136 | r := mustBeDir(res); |
| 137 | if r < 0 then return r; end; |
| 138 | if name = ".." then |
| 139 | mark := res.len; |
| 140 | while (mark > 0) and (Buf.bytes(res)[mark - 1] # '/') do DEC(mark); end; |
| 141 | if mark > 0 then Buf.cut(res, mark - 1); end; (* the root has no parent *) |
| 142 | end; |
| 143 | return 0; |
| 144 | end; |
| 145 | mark := res.len; |
| 146 | Buf.add(res, '/'); |
| 147 | Buf.str(res, name); |
| 148 | info, r := Sys.linkInfo(Buf.bytes(res)); |
| 149 | if r < 0 then return r; end; |
| 150 | if not info.symlink then return 0; end; |
| 151 | return followLink(res, rest, mark, at, links); |
| 152 | end; |
| 153 | |
| 154 | (* Adds to b the absolute path of the file at path, with no ".", "..", |
| 155 | or symbolic link in it: 0, or minus an errno, -2 (ENOENT) if a part |
| 156 | of it isn't there, -40 (ELOOP) after 40 links. The working |
| 157 | directory stays as it is; b is left as it was on failure. *) |
| 158 | proc realPath*(path: []const U8; var b: Buf.Buf): INT; |
| 159 | var res, rest: Buf.Buf; dir, name: []const U8; at, links, r: INT; |
| 160 | begin |
| 161 | if LEN(path) = 0 then return Sys.errNoEntry; end; |
| 162 | Buf.init(res, Heap.heap); |
| 163 | Buf.init(rest, Heap.heap); |
| 164 | r := 0; |
| 165 | if path[0] # '/' then |
| 166 | dir, r := Sys.workDir(); |
| 167 | if dir # "/" then Buf.str(res, dir); end; |
| 168 | end; |
| 169 | Buf.str(rest, path); |
| 170 | at := 0; |
| 171 | links := 0; |
| 172 | while r = 0 do |
| 173 | name := nextName(rest, at); |
| 174 | if LEN(name) = 0 then break; end; |
| 175 | r := enter(res, rest, at, links, name); |
| 176 | end; |
| 177 | if r = 0 then |
| 178 | if res.len = 0 then Buf.add(b, '/'); else Buf.str(b, Buf.bytes(res)); end; |
| 179 | end; |
| 180 | Buf.done(res); |
| 181 | Buf.done(rest); |
| 182 | return r; |
| 183 | end; |
| 184 | |
| 185 | proc removeTree*(path: []const U8): INT; forward; |
| 186 | |
| 187 | (* Removes what is in the directory at path: 0, or minus the first |
| 188 | errno, going on with the rest after it. *) |
| 189 | proc removeContents(path: []const U8): INT; |
| 190 | var d: Sys.Dir; child: Buf.Buf; name: []const U8; more: BOOL; mark, r, first: INT; |
| 191 | begin |
| 192 | r := Sys.openDir(d, path); |
| 193 | if r < 0 then return r; end; |
| 194 | Buf.init(child, Heap.heap); |
| 195 | Buf.str(child, path); |
| 196 | if (LEN(path) = 0) or (path[LEN(path) - 1] # '/') then Buf.add(child, '/'); end; |
| 197 | mark := child.len; |
| 198 | first := 0; |
| 199 | name, more := Sys.nextEntry(d); |
| 200 | while more do |
| 201 | Buf.cut(child, mark); |
| 202 | Buf.str(child, name); |
| 203 | r := removeTree(Buf.bytes(child)); |
| 204 | if (r < 0) and (first = 0) then first := r; end; |
| 205 | name, more := Sys.nextEntry(d); |
| 206 | end; |
| 207 | Sys.closeDir(d); |
| 208 | Buf.done(child); |
| 209 | return first; |
| 210 | end; |
| 211 | |
| 212 | (* Removes the file at path and, if it is a directory, all within it, |
| 213 | without following a symbolic link (the link goes, not what it |
| 214 | names): 0, or minus the first errno. *) |
| 215 | proc removeTree*(path: []const U8): INT; |
| 216 | var info: Sys.Info; r: INT; |
| 217 | begin |
| 218 | info, r := Sys.linkInfo(path); |
| 219 | if r < 0 then return r; end; |
| 220 | if not info.dir then return Sys.remove(path); end; |
| 221 | r := removeContents(path); |
| 222 | if r < 0 then return r; end; |
| 223 | return Sys.removeDir(path); |
| 224 | end; |
| 225 | |
| 226 | (* Sets b to base and then rest, and gives it. *) |
| 227 | proc join(var b: Buf.Buf; base, rest: []const U8): []U8; |
| 228 | begin |
| 229 | Buf.cut(b, 0); |
| 230 | Buf.str(b, base); |
| 231 | Buf.str(b, rest); |
| 232 | return Buf.bytes(b); |
| 233 | end; |
| 234 | |
| 235 | (* Stops the test, saying what, unless realPath of path gives err and |
| 236 | adds want to a buffer. *) |
| 237 | proc wantReal(path, want: []const U8; err: INT; what: []const U8); |
| 238 | var b: Buf.Buf; r: INT; |
| 239 | begin |
| 240 | Buf.init(b, Heap.heap); |
| 241 | r := realPath(path, b); |
| 242 | if (r # err) or (Buf.bytes(b) # want) then Rt.stop(what); end; |
| 243 | Buf.done(b); |
| 244 | end; |
| 245 | |
| 246 | (* A pipe written 300,000 bytes, more than it holds, by a copy of this |
| 247 | program while this one reads: every byte arrives. *) |
| 248 | proc testPipe; |
| 249 | var rd, wr, n, total, pid, status: INT; b: Buf.Buf; |
| 250 | begin |
| 251 | Buf.init(b, Heap.heap); |
| 252 | n := 300000; |
| 253 | INC(b.len, LEN(Buf.spare(b, n))); |
| 254 | rd, wr := Sys.pipe(); |
| 255 | pid := Sys.fork(); |
| 256 | if pid = 0 then |
| 257 | Sys.close(rd); |
| 258 | Sys.exit(-writeAll(wr, Buf.bytes(b))); |
| 259 | end; |
| 260 | Sys.close(wr); |
| 261 | total := 0; |
| 262 | repeat |
| 263 | n := Sys.read(rd, Buf.spare(b, 4096)); |
| 264 | if n > 0 then INC(total, n); end; |
| 265 | until (n <= 0) and (n # Sys.errIntr); |
| 266 | Sys.close(rd); |
| 267 | _, _, status := Sys.wait(pid, true); |
| 268 | if (total # 300000) or (status # 0) then Rt.stop("Files.writeAll: a pipe"); end; |
| 269 | Buf.done(b); |
| 270 | end; |
| 271 | |
| 272 | (* realPath and removeTree in a scratch tree, with links in it and one |
| 273 | out of it. What realPath gives is under /tmp as realPath gives it, |
| 274 | which on a Mac is /private/tmp, /tmp being a link to it. *) |
| 275 | proc testTree; |
| 276 | const T = "/tmp/raz-files-test.d"; Out = "/tmp/raz-files-test.out"; |
| 277 | var p, q, rt, ro: Buf.Buf; here, now, realT, realOut: []const U8; r: INT; |
| 278 | begin |
| 279 | Buf.init(p, Heap.heap); |
| 280 | Buf.init(q, Heap.heap); |
| 281 | Buf.init(rt, Heap.heap); |
| 282 | Buf.init(ro, Heap.heap); |
| 283 | if realPath("/tmp", rt) # 0 then Rt.stop("realPath: /tmp"); end; |
| 284 | Buf.str(ro, Buf.bytes(rt)); |
| 285 | Buf.str(rt, "/raz-files-test.d"); |
| 286 | Buf.str(ro, "/raz-files-test.out"); |
| 287 | realT := Buf.bytes(rt); |
| 288 | realOut := Buf.bytes(ro); |
| 289 | _ := removeTree(T); |
| 290 | _ := removeTree(Out); |
| 291 | if (Sys.makeDir(T) # 0) or (Sys.makeDir(join(p, T, "/a")) # 0) |
| 292 | or (Sys.makeDir(join(p, T, "/a/b")) # 0) or (Sys.makeDir(Out) # 0) then |
| 293 | Rt.stop("Files: scratch tree"); |
| 294 | end; |
| 295 | if not write(join(p, T, "/a/b/f"), "x") or not write(join(p, Out, "/keep"), "x") then |
| 296 | Rt.stop("Files: scratch files"); |
| 297 | end; |
| 298 | if (Sys.symlink("a/b", join(p, T, "/rel")) # 0) |
| 299 | or (Sys.symlink(join(q, T, "/a"), join(p, T, "/abs")) # 0) |
| 300 | or (Sys.symlink("../a/b", join(p, T, "/a/up")) # 0) |
| 301 | or (Sys.symlink("loop", join(p, T, "/loop")) # 0) |
| 302 | or (Sys.symlink(Out, join(p, T, "/a/ex")) # 0) then |
| 303 | Rt.stop("Files: links"); |
| 304 | end; |
| 305 | wantReal(join(p, T, "/rel/f"), join(q, realT, "/a/b/f"), 0, "realPath: a relative link"); |
| 306 | wantReal(join(p, T, "/abs/b/../b/./f"), join(q, realT, "/a/b/f"), 0, "realPath: absolute link, .. and ."); |
| 307 | wantReal(join(p, T, "/a/up/f"), join(q, realT, "/a/b/f"), 0, "realPath: a link read from its directory"); |
| 308 | wantReal(join(p, T, "/a/"), join(q, realT, "/a"), 0, "realPath: a trailing slash"); |
| 309 | wantReal(join(p, T, "/a/b/../../abs/b"), join(q, realT, "/a/b"), 0, "realPath: .. then a link"); |
| 310 | wantReal(join(p, T, "/a/ex/keep"), join(q, realOut, "/keep"), 0, "realPath: a link out of the tree"); |
| 311 | wantReal(join(p, T, "/a/nope"), "", Sys.errNoEntry, "realPath: a missing part"); |
| 312 | wantReal(join(p, T, "/a/b/f/.."), "", -20, "realPath: .. of a file"); |
| 313 | wantReal(join(p, T, "/loop"), "", -40, "realPath: a link loop"); |
| 314 | wantReal("", "", Sys.errNoEntry, "realPath: empty"); |
| 315 | wantReal("/", "/", 0, "realPath: the root"); |
| 316 | wantReal("//../..//", "/", 0, "realPath: past the root"); |
| 317 | here, r := Sys.workDir(); |
| 318 | Buf.cut(q, 0); |
| 319 | Buf.cut(p, 0); |
| 320 | Buf.str(q, here); (* here lasts only until the next Sys.workDir *) |
| 321 | if realPath(".", p) # 0 then Rt.stop("realPath: ."); end; |
| 322 | now, r := Sys.workDir(); |
| 323 | if (Buf.bytes(p) # Buf.bytes(q)) or (now # Buf.bytes(q)) then Rt.stop("realPath: the working directory"); end; |
| 324 | if removeTree(join(p, T, "/a")) # 0 then Rt.stop("removeTree: a tree"); end; |
| 325 | _, r := Sys.linkInfo(join(p, Out, "/keep")); |
| 326 | if r # 0 then Rt.stop("removeTree: followed a link"); end; |
| 327 | if removeTree(T) # 0 then Rt.stop("removeTree: the rest"); end; |
| 328 | if removeTree(T) # Sys.errNoEntry then Rt.stop("removeTree: no such file"); end; |
| 329 | if removeTree(Out) # 0 then Rt.stop("removeTree: the outside"); end; |
| 330 | Buf.done(p); |
| 331 | Buf.done(q); |
| 332 | Buf.done(rt); |
| 333 | Buf.done(ro); |
| 334 | end; |
| 335 | |
| 336 | (* The module's test. *) |
| 337 | proc main*; |
| 338 | var b: Buf.Buf; |
| 339 | begin |
| 340 | Buf.init(b, Heap.heap); |
| 341 | if not write("/tmp/raz-files-test", "abc") then Rt.stop("Files.write"); end; |
| 342 | if (load("/tmp/raz-files-test", b) # 0) or (Buf.bytes(b) # "abc") then Rt.stop("Files.load"); end; |
| 343 | if not read("/tmp/raz-files-test", b) or (Buf.bytes(b) # "abcabc") then Rt.stop("Files.read"); end; |
| 344 | _ := Sys.remove("/tmp/raz-files-test"); |
| 345 | if load("/tmp/raz-files-test", b) # Sys.errNoEntry then Rt.stop("Files.load: no such file"); end; |
| 346 | if load("/", b) # -21 then Rt.stop("Files.load: a directory"); end; |
| 347 | if read("/", b) or (b.len # 6) then Rt.stop("Files.read: a directory"); end; |
| 348 | if (argCount() < 1) or (LEN(arg(0)) = 0) then Rt.stop("Files.argCount"); end; |
| 349 | if (LEN(arg(argCount())) # 0) or (LEN(arg(-1)) # 0) then Rt.stop("Files.arg: past the arguments"); end; |
| 350 | Buf.done(b); |
| 351 | testPipe; |
| 352 | testTree; |
| 353 | end; |
| 354 | |
| 355 | end Files. |