raz / commit 3ae3cdf6

commit
3ae3cdf65138f7d3052bdc7450b59dc0e5423c90
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
7235b7bd
Files.load: why a file couldn't be read

Met in libro, which could say "can't read the file" but not that it
was missing, a directory or not allowed. load is read's loop, giving 0
or minus the errno of the open or the read that failed, as Sys does;
read is now load(path, b) = 0, as it was. Library growth that breaks no
program; Files gains a test, its main: written, loaded, read, missing,
and a directory.

patch · log

3 files changed, +30 -18

PROPOSALS.md modified +0 -13

@@ -573,16 +573,3 @@
573573
574574 Met in libro; every program with a screen, rmacs and ravite among
575575 them, needs the same.
576-
577-## Why `Files.read` failed
578-
579-`Files.read(path, b)` is false if the file can't be read, and says no
580-more: libro can say "can't read the file", but not that it is missing,
581-a directory, or not allowed. The program that wants to know opens it
582-itself, with `Sys.openRead`, and reads it as `Files.read` does.
583-
584-The proposal: `proc load*(path: []U8; b: ^Buf.Buf): INT`, 0, or minus
585-the errno of the open or the read that failed, as `Sys` gives them;
586-`read` stays as it is. Library growth that breaks no program.
587-
588-Met in libro.

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

@@ -676,6 +676,7 @@
676676 module Files;
677677 proc arg*(i: INT): []U8; (* argument i; 0 is the program's name *)
678678 proc read*(path: []U8; b: ^Buf.Buf): BOOL; (* the whole file added to b *)
679+proc load*(path: []U8; b: ^Buf.Buf): INT; (* as read: 0, or minus the errno that stopped it *)
679680 proc write*(path, data: []U8): BOOL; (* the file made to hold data *)
680681
681682 module Opt;

lib/Files.raz (history) modified +29 -5

@@ -1,7 +1,7 @@
11 (* What most programs need of the command line and of files: an
22 argument as a string, and a whole file read or written. *)
33 module Files;
4-import Sys, Buf;
4+import Sys, Buf, Heap, Rt;
55
66 (* Argument i, a C string, as a slice. *)
77 proc arg*(i: INT): []U8;
@@ -13,20 +13,29 @@
1313 return s[0..n];
1414 end;
1515
16-(* Adds the file at path to b; false if it can't be read. *)
17-proc read*(path: []U8; b: ^Buf.Buf): BOOL;
16+(* Adds the file at path to b: 0, or minus the errno of the open or
17+ the read that failed, as Sys gives them: -2 (ENOENT) for no such
18+ file, -13 (EACCES) for one not allowed, -21 (EISDIR) for a
19+ directory. What was read before a read failed stays in b. *)
20+proc load*(path: []U8; b: ^Buf.Buf): INT;
1821 var fd, n: INT;
1922 begin
2023 fd := Sys.openRead(path);
21- if fd < 0 then return false; end;
24+ if fd < 0 then return fd; end;
2225 repeat
2326 n := Sys.read(fd, Buf.spare(b, 65536)); (* straight into b *)
2427 if n > 0 then INC(b^.len, n); end;
2528 until n <= 0;
2629 Sys.close(fd);
27- return n = 0;
30+ return n;
2831 end;
2932
33+(* Adds the file at path to b; false if it can't be read. *)
34+proc read*(path: []U8; b: ^Buf.Buf): BOOL;
35+begin
36+ return load(path, b) = 0;
37+end;
38+
3039 (* Makes the file at path hold data; false if it can't be written. *)
3140 proc write*(path, data: []U8): BOOL;
3241 var fd, n, at: INT;
@@ -41,6 +50,21 @@
4150 end;
4251 Sys.close(fd);
4352 return true;
53+end;
54+
55+(* The module's test. *)
56+proc main*;
57+var b: Buf.Buf;
58+begin
59+ Buf.init(ADR(b), Heap.heap);
60+ if not write("/tmp/raz-files-test", "abc") then Rt.stop("Files.write"); end;
61+ if (load("/tmp/raz-files-test", ADR(b)) # 0) or (Buf.bytes(ADR(b)) # "abc") then Rt.stop("Files.load"); end;
62+ if not read("/tmp/raz-files-test", ADR(b)) or (Buf.bytes(ADR(b)) # "abcabc") then Rt.stop("Files.read"); end;
63+ _ := Sys.remove("/tmp/raz-files-test");
64+ if load("/tmp/raz-files-test", ADR(b)) # -2 then Rt.stop("Files.load: no such file"); end;
65+ if load("/", ADR(b)) # -21 then Rt.stop("Files.load: a directory"); end;
66+ if read("/", ADR(b)) or (b.len # 6) then Rt.stop("Files.read: a directory"); end;
67+ Buf.done(ADR(b));
4468 end;
4569
4670 end Files.