From 3ae3cdf65138f7d3052bdc7450b59dc0e5423c90 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 07:25:17 +0000 Subject: [PATCH] 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. --- diff --git a/PROPOSALS.md b/PROPOSALS.md index ac5db96..92f1e05 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -573,16 +573,3 @@ Met in libro; every program with a screen, rmacs and ravite among them, needs the same. - -## Why `Files.read` failed - -`Files.read(path, b)` is false if the file can't be read, and says no -more: libro can say "can't read the file", but not that it is missing, -a directory, or not allowed. The program that wants to know opens it -itself, with `Sys.openRead`, and reads it as `Files.read` does. - -The proposal: `proc load*(path: []U8; b: ^Buf.Buf): INT`, 0, or minus -the errno of the open or the read that failed, as `Sys` gives them; -`read` stays as it is. Library growth that breaks no program. - -Met in libro. diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 77a3db2..5b3ec21 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -676,6 +676,7 @@ module Files; proc arg*(i: INT): []U8; (* argument i; 0 is the program's name *) proc read*(path: []U8; b: ^Buf.Buf): BOOL; (* the whole file added to b *) +proc load*(path: []U8; b: ^Buf.Buf): INT; (* as read: 0, or minus the errno that stopped it *) proc write*(path, data: []U8): BOOL; (* the file made to hold data *) module Opt; diff --git a/lib/Files.raz b/lib/Files.raz index 0b4ebc4..7b53932 100644 --- a/lib/Files.raz +++ b/lib/Files.raz @@ -1,7 +1,7 @@ (* What most programs need of the command line and of files: an argument as a string, and a whole file read or written. *) module Files; -import Sys, Buf; +import Sys, Buf, Heap, Rt; (* Argument i, a C string, as a slice. *) proc arg*(i: INT): []U8; @@ -13,20 +13,29 @@ return s[0..n]; end; -(* Adds the file at path to b; false if it can't be read. *) -proc read*(path: []U8; b: ^Buf.Buf): BOOL; +(* 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: []U8; b: ^Buf.Buf): INT; var fd, n: INT; begin fd := Sys.openRead(path); - if fd < 0 then return false; end; + 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; Sys.close(fd); - return n = 0; + return n; end; +(* Adds the file at path to b; false if it can't be read. *) +proc read*(path: []U8; b: ^Buf.Buf): BOOL; +begin + return load(path, b) = 0; +end; + (* Makes the file at path hold data; false if it can't be written. *) proc write*(path, data: []U8): BOOL; var fd, n, at: INT; @@ -41,6 +50,21 @@ end; Sys.close(fd); return true; +end; + +(* The module's test. *) +proc main*; +var b: Buf.Buf; +begin + Buf.init(ADR(b), Heap.heap); + if not write("/tmp/raz-files-test", "abc") then Rt.stop("Files.write"); end; + if (load("/tmp/raz-files-test", ADR(b)) # 0) or (Buf.bytes(ADR(b)) # "abc") then Rt.stop("Files.load"); end; + if not read("/tmp/raz-files-test", ADR(b)) or (Buf.bytes(ADR(b)) # "abcabc") then Rt.stop("Files.read"); end; + _ := Sys.remove("/tmp/raz-files-test"); + if load("/tmp/raz-files-test", ADR(b)) # -2 then Rt.stop("Files.load: no such file"); end; + if load("/", ADR(b)) # -21 then Rt.stop("Files.load: a directory"); end; + if read("/", ADR(b)) or (b.len # 6) then Rt.stop("Files.read: a directory"); end; + Buf.done(ADR(b)); end; end Files.