raz / commit 19f3570a

commit
19f3570a74c0b31fd8f60f184e5feca7ac4a2b64
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
2560ff23
Sys: files opened as asked, seek, links, permissions and times

open takes a set of Access; openRead and create are two of its uses.
seek, removeDir, chmod, symlink, readLink and setWorkDir; Info gains
mtime and mode. Both Sys files, each with its test.

patch · log

4 files changed, +249 -30

PLAN.md (history) modified +2 -2

@@ -27,10 +27,10 @@
2727 Every `Sys` export is written twice, in `Sys.linux-x86_64.raz` and in
2828 `Sys.raz` over the C library, and tested through both (`test/libc`).
2929
30-- [ ] 1. The report, version 2026-10.9: every new and changed export of
30+- [x] 1. The report, version 2026-10.9: every new and changed export of
3131 `Sys`, `Str`, `Map`, `Sort`, `Math` and `Fmt` (section 11), for
3232 review before any code.
33-- [ ] 2. `Sys`, files: `open` with a set of `Access`, `seek`, `Info`'s
33+- [x] 2. `Sys`, files: `open` with a set of `Access`, `seek`, `Info`'s
3434 `mtime` and `mode`, `chmod`, `readLink`, `symlink`, `removeDir`,
3535 `setWorkDir`.
3636 - [ ] 3. `Sys`, programs: `pipe`, `spawn` of a `Program`, `wait` of any

lib/C.raz (history) modified +4 -0

@@ -20,6 +20,9 @@
2020 extern proc rename*(old, new: ^U8): I32;
2121 extern proc unlink*(path: ^U8): I32;
2222 extern proc mkdir*(path: ^U8; mode: U32): I32;
23+extern proc rmdir*(path: ^U8): I32;
24+extern proc chmod*(path: ^U8; mode: U32): I32;
25+extern proc readlink*(path, buf: ^U8; n: U64): I64;
2326 extern proc lseek*(fd: I32; off: I64; whence: I32): I64;
2427 extern proc opendir*(path: ^U8): ^U8; (* DIR * *)
2528 extern proc readdir*(d: ^U8): ^U8; (* struct dirent * *)
@@ -37,6 +40,7 @@
3740 extern proc accept*(fd: I32; addr: ^U8; len: ^U32): I32;
3841 extern proc getenv*(name: ^U8): ^U8;
3942 extern proc getcwd*(buf: ^U8; n: U64): ^U8;
43+extern proc chdir*(path: ^U8): I32;
4044 extern proc stat*(path, st: ^U8): I32;
4145 extern proc lstat*(path, st: ^U8): I32;
4246 extern proc symlink*(target, path: ^U8): I32;

lib/Sys.linux-x86_64.raz (history) modified +118 -13

@@ -8,9 +8,16 @@
88 stderr* = 2;
99
1010 type
11+ (* What open is asked for: reading, writing or both, and what else
12+ it does to the file. *)
13+ Access* = (reading, writing, creating, exclusive, truncating, appending);
14+ Accesses* = set of Access;
15+
1116 (* What info says of a file. *)
1217 Info* = record
1318 size*: I64; (* in bytes *)
19+ mtime*: I64; (* when its contents last changed, in nanoseconds since 1970 *)
20+ mode*: I64; (* its permission bits, rwxrwxrwx *)
1421 dir*: BOOL; (* a directory *)
1522 file*: BOOL; (* a regular file: not a directory, device, pipe or socket *)
1623 symlink*: BOOL; (* the path itself is a symbolic link *)
@@ -39,6 +46,7 @@
3946
4047 var
4148 cwd: [4096]U8; (* what workDir gives *)
49+ linked: [4096]U8; (* what readLink gives *)
4250 saved: Termios; (* the terminal as rawMode found it *)
4351
4452 (* Writes s to the file descriptor fd; the number of bytes written, or
@@ -67,22 +75,50 @@
6775 return true;
6876 end;
6977
70-(* The file at path, opened for reading: its file descriptor, or minus
71- an errno. *)
72-proc openRead*(path: []U8): I64;
78+(* open's flags for how; Linux's numbers *)
79+proc flags(how: Accesses): I32;
80+var f: I32;
81+begin
82+ f := 0; (* O_RDONLY *)
83+ if writing in how then
84+ f := 1; (* O_WRONLY *)
85+ if reading in how then f := 2; end; (* O_RDWR *)
86+ end;
87+ if creating in how then f := f | 40H; end;
88+ if exclusive in how then f := f | 80H; end;
89+ if truncating in how then f := f | 200H; end;
90+ if appending in how then f := f | 400H; end;
91+ return f;
92+end;
93+
94+(* The file at path, opened as how asks: its file descriptor, or minus
95+ an errno. A file it creates is readable by all, writable by its
96+ owner. *)
97+proc open*(path: []U8; how: Accesses): I64;
7398 var buf: [4096]U8;
7499 begin
75100 if not cstr(path, buf) then return -36; end; (* ENAMETOOLONG *)
76- return SYSCALL(2, ADR(buf), 0); (* open, O_RDONLY *)
101+ return SYSCALL(2, ADR(buf), flags(how), 1A4H); (* mode 644 *)
77102 end;
78103
79-(* The file at path, made empty or new, opened for writing, readable by
80- all: its file descriptor, or minus an errno. *)
104+(* The file at path, opened for reading. *)
105+proc openRead*(path: []U8): I64;
106+begin
107+ return open(path, {reading});
108+end;
109+
110+(* The file at path, made empty or new, opened for writing. *)
81111 proc create*(path: []U8): I64;
82-var buf: [4096]U8;
112+begin
113+ return open(path, {writing, creating, truncating});
114+end;
115+
116+(* Moves fd to pos from the start of its file, or from the end if
117+ fromEnd: the new position, or minus an errno. *)
118+proc seek*(fd, pos: I64; fromEnd: BOOL): I64;
83119 begin
84- if not cstr(path, buf) then return -36; end;
85- return SYSCALL(85, ADR(buf), 420); (* creat, mode 644 *)
120+ if fromEnd then return SYSCALL(8, fd, pos, 2); end; (* lseek, SEEK_END *)
121+ return SYSCALL(8, fd, pos, 0); (* SEEK_SET *)
86122 end;
87123
88124 proc close*(fd: I64);
@@ -137,6 +173,42 @@
137173 return SYSCALL(83, ADR(a), 1EDH); (* mkdir, mode 755 *)
138174 end;
139175
176+(* Removes the directory at path, which must be empty. *)
177+proc removeDir*(path: []U8): I64;
178+var a: [4096]U8;
179+begin
180+ if not cstr(path, a) then return -36; end;
181+ return SYSCALL(84, ADR(a)); (* rmdir *)
182+end;
183+
184+(* Sets the permission bits of what path names. *)
185+proc chmod*(path: []U8; mode: I64): I64;
186+var a: [4096]U8;
187+begin
188+ if not cstr(path, a) then return -36; end;
189+ return SYSCALL(90, ADR(a), mode);
190+end;
191+
192+(* Makes path a symbolic link to target. *)
193+proc symlink*(target, path: []U8): I64;
194+var a, b: [4096]U8;
195+begin
196+ if not cstr(target, a) or not cstr(path, b) then return -36; end;
197+ return SYSCALL(88, ADR(a), ADR(b));
198+end;
199+
200+(* What the symbolic link at path names, good until the next call; and
201+ 0, or minus an errno. *)
202+proc readLink*(path: []U8): ([]U8, I64);
203+var a: [4096]U8; n: I64;
204+begin
205+ if not cstr(path, a) then return "", -36; end;
206+ n := SYSCALL(89, ADR(a), ADR(linked), LEN(linked)); (* readlink *)
207+ if n < 0 then return "", n; end;
208+ if n = LEN(linked) then return "", -36; end; (* it may be cut short *)
209+ return linked[0..n], 0;
210+end;
211+
140212 (* The size of what path names, whether it is a directory or a regular
141213 file, following a symbolic link, and whether path is one; and 0, or
142214 minus an errno. *)
@@ -150,6 +222,8 @@
150222 r := SYSCALL(4, ADR(a), ADR(st)); (* stat *)
151223 if r # 0 then return i, r; end;
152224 i.size := st[6]; (* st_size *)
225+ i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *)
226+ i.mode := st[3] & 1FFH;
153227 i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *)
154228 i.file := st[3] & 0F000H = 8000H; (* S_IFREG *)
155229 return i, 0;
@@ -311,6 +385,14 @@
311385 return cwd[0..r - 1], 0;
312386 end;
313387
388+(* Makes path the working directory. *)
389+proc setWorkDir*(path: []U8): I64;
390+var a: [4096]U8;
391+begin
392+ if not cstr(path, a) then return -36; end;
393+ return SYSCALL(80, ADR(a)); (* chdir *)
394+end;
395+
314396 (* A pseudo-terminal: the file descriptor of its controlling side, or
315397 minus an errno. Its other side, the terminal its programs run on, is
316398 unlocked, for spawn to open. *)
@@ -512,7 +594,6 @@
512594 after its end. *)
513595 proc main*;
514596 var fd, n, err, w, h, l, c, a: I64; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL;
515- dir, name2: [4096]U8;
516597 begin
517598 fd := create("/tmp/raz-sys-test");
518599 if fd < 0 then exit(2); end;
@@ -543,12 +624,34 @@
543624 i, err := info("/dev/null");
544625 if (err # 0) or i.dir or i.file then exit(2); end;
545626 remove("/tmp/raz-sys-link");
546- cstr("/tmp/raz-sys-dir", dir);
547- cstr("/tmp/raz-sys-link", name2);
548- if SYSCALL(88, ADR(dir), ADR(name2)) # 0 then exit(2); end; (* symlink *)
627+ if symlink("/tmp/raz-sys-dir", "/tmp/raz-sys-link") # 0 then exit(2); end;
549628 i, err := info("/tmp/raz-sys-link");
550629 if (err # 0) or not i.dir or not i.symlink then exit(2); end;
630+ name, err := readLink("/tmp/raz-sys-link");
631+ if (err # 0) or (name # "/tmp/raz-sys-dir") then exit(2); end;
632+ name, err := readLink("/tmp/raz-sys-dir");
633+ if err # -22 then exit(2); end; (* EINVAL: not a link *)
551634 remove("/tmp/raz-sys-link");
635+ (* open as asked, seek, the mode and the time *)
636+ remove("/tmp/raz-sys-dir/h");
637+ fd := open("/tmp/raz-sys-dir/h", {writing, creating, exclusive});
638+ if (fd < 0) or (write(fd, "abc") # 3) then exit(2); end;
639+ close(fd);
640+ if open("/tmp/raz-sys-dir/h", {writing, creating, exclusive}) # -17 then exit(2); end;
641+ fd := open("/tmp/raz-sys-dir/h", {writing, appending});
642+ if (fd < 0) or (seek(fd, 0, false) # 0) or (write(fd, "de") # 2) then exit(2); end;
643+ close(fd);
644+ fd := open("/tmp/raz-sys-dir/h", {reading, writing});
645+ if (seek(fd, -2, true) # 3) or (read(fd, buf) # 2) or (buf[0..2] # "de") then exit(2); end;
646+ if (seek(fd, 1, false) # 1) or (write(fd, "B") # 1) or (seek(fd, 0, false) # 0) then exit(2); end;
647+ if (read(fd, buf) # 5) or (buf[0..5] # "aBcde") then exit(2); end;
648+ close(fd);
649+ i, err := info("/tmp/raz-sys-dir/h");
650+ if (err # 0) or (i.mode & 1C0H # 180H) or (i.mtime < 1700000000 * 1000000000) then exit(2); end;
651+ if chmod("/tmp/raz-sys-dir/h", 1EDH) # 0 then exit(2); end;
652+ i, err := info("/tmp/raz-sys-dir/h");
653+ if i.mode # 1EDH then exit(2); end;
654+ remove("/tmp/raz-sys-dir/h");
552655 i, err := info("/no/such/file");
553656 if err >= 0 then exit(2); end;
554657 if openDir(ADR(d), "/tmp/raz-sys-dir") # 0 then exit(2); end;
@@ -567,6 +670,8 @@
567670 if (LEN(env("PATH")) = 0) or (LEN(env("RAZ_NO_SUCH_VARIABLE")) # 0) then exit(2); end;
568671 name, err := workDir();
569672 if (err # 0) or (LEN(name) = 0) or (name[0] # '/') then exit(2); end;
673+ if (setWorkDir("/tmp/raz-sys-dir") # 0) or (removeDir("/tmp/raz-sys-dir") # 0) then exit(2); end;
674+ if (setWorkDir("/") # 0) or (removeDir("/tmp/raz-sys-dir") # -2) then exit(2); end;
570675 (* a socket: nothing at a path, then a listener that a connection
571676 makes ready, and a byte from it to the connection *)
572677 if connect("/tmp/raz-no-such-socket") # -2 then exit(2); end; (* ENOENT *)

lib/Sys.raz (history) modified +125 -15

@@ -4,8 +4,8 @@
44
55 It keeps to POSIX's names, and raw mode is cfmakeraw's. What is
66 glibc's, on x86-64: where a name lies in struct dirent; struct stat;
7- the numbers of the requests TIOCGWINSZ, TIOCSWINSZ and TIOCSCTTY;
8- close_range; and struct sockaddr_un, which on the BSDs starts with a
7+ open's flags; the numbers of the requests TIOCGWINSZ, TIOCSWINSZ and
8+ TIOCSCTTY; close_range; and struct sockaddr_un, which on the BSDs starts with a
99 byte of length. A C library that differs needs a Sys of its own. *)
1010 module Sys;
1111 import C;
@@ -20,9 +20,16 @@
2020 TIOCSCTTY = 540EH;
2121
2222 type
23+ (* What open is asked for: reading, writing or both, and what else
24+ it does to the file. *)
25+ Access* = (reading, writing, creating, exclusive, truncating, appending);
26+ Accesses* = set of Access;
27+
2328 (* What info says of a file. *)
2429 Info* = record
2530 size*: I64; (* in bytes *)
31+ mtime*: I64; (* when its contents last changed, in nanoseconds since 1970 *)
32+ mode*: I64; (* its permission bits, rwxrwxrwx *)
2633 dir*: BOOL; (* a directory *)
2734 file*: BOOL; (* a regular file: not a directory, device, pipe or socket *)
2835 symlink*: BOOL; (* the path itself is a symbolic link *)
@@ -44,6 +51,7 @@
4451
4552 var
4653 cwd: [4096]U8; (* what workDir gives *)
54+ linked: [4096]U8; (* what readLink gives *)
4755 saved: [256]U8; (* the terminal as rawMode found it: a struct termios, of the C library's size *)
4856
4957 (* minus errno, as a system call returns it *)
@@ -86,24 +94,53 @@
8694 return true;
8795 end;
8896
89-(* The file at path, opened for reading: its file descriptor, or minus
90- an errno. *)
91-proc openRead*(path: []U8): I64;
97+(* open's flags for how; glibc's numbers *)
98+proc flags(how: Accesses): I32;
99+var f: I32;
100+begin
101+ f := 0; (* O_RDONLY *)
102+ if writing in how then
103+ f := 1; (* O_WRONLY *)
104+ if reading in how then f := 2; end; (* O_RDWR *)
105+ end;
106+ if creating in how then f := f | 40H; end;
107+ if exclusive in how then f := f | 80H; end;
108+ if truncating in how then f := f | 200H; end;
109+ if appending in how then f := f | 400H; end;
110+ return f;
111+end;
112+
113+(* The file at path, opened as how asks: its file descriptor, or minus
114+ an errno. A file it creates is readable by all, writable by its
115+ owner. *)
116+proc open*(path: []U8; how: Accesses): I64;
92117 var buf: [4096]U8; r: I64;
93118 begin
94119 if not cstr(path, buf) then return -36; end;
95- r := I64(C.open(ADR(buf[0]), 0)); (* O_RDONLY is 0 everywhere *)
120+ r := I64(C.open(ADR(buf[0]), flags(how), 1A4H)); (* mode 644 *)
96121 if r < 0 then return failed(); end;
97122 return r;
98123 end;
99124
100-(* The file at path, made empty or new, opened for writing, readable by
101- all: its file descriptor, or minus an errno. *)
125+(* The file at path, opened for reading. *)
126+proc openRead*(path: []U8): I64;
127+begin
128+ return open(path, {reading});
129+end;
130+
131+(* The file at path, made empty or new, opened for writing. *)
102132 proc create*(path: []U8): I64;
103-var buf: [4096]U8; r: I64;
133+begin
134+ return open(path, {writing, creating, truncating});
135+end;
136+
137+(* Moves fd to pos from the start of its file, or from the end if
138+ fromEnd: the new position, or minus an errno. *)
139+proc seek*(fd, pos: I64; fromEnd: BOOL): I64;
140+var r: I64;
104141 begin
105- if not cstr(path, buf) then return -36; end;
106- r := I64(C.creat(ADR(buf[0]), 420)); (* mode 644 *)
142+ if fromEnd then r := C.lseek(I32(fd), pos, 2); (* SEEK_END *)
143+ else r := C.lseek(I32(fd), pos, 0); end; (* SEEK_SET *)
107144 if r < 0 then return failed(); end;
108145 return r;
109146 end;
@@ -155,6 +192,45 @@
155192 return 0;
156193 end;
157194
195+(* Removes the directory at path, which must be empty. *)
196+proc removeDir*(path: []U8): I64;
197+var a: [4096]U8;
198+begin
199+ if not cstr(path, a) then return -36; end;
200+ if C.rmdir(ADR(a[0])) # 0 then return failed(); end;
201+ return 0;
202+end;
203+
204+(* Sets the permission bits of what path names. *)
205+proc chmod*(path: []U8; mode: I64): I64;
206+var a: [4096]U8;
207+begin
208+ if not cstr(path, a) then return -36; end;
209+ if C.chmod(ADR(a[0]), U32(mode)) # 0 then return failed(); end;
210+ return 0;
211+end;
212+
213+(* Makes path a symbolic link to target. *)
214+proc symlink*(target, path: []U8): I64;
215+var a, b: [4096]U8;
216+begin
217+ if not cstr(target, a) or not cstr(path, b) then return -36; end;
218+ if C.symlink(ADR(a[0]), ADR(b[0])) # 0 then return failed(); end;
219+ return 0;
220+end;
221+
222+(* What the symbolic link at path names, good until the next call; and
223+ 0, or minus an errno. *)
224+proc readLink*(path: []U8): ([]U8, I64);
225+var a: [4096]U8; n: I64;
226+begin
227+ if not cstr(path, a) then return "", -36; end;
228+ n := C.readlink(ADR(a[0]), ADR(linked[0]), U64(LEN(linked)));
229+ if n < 0 then return "", failed(); end;
230+ if n = LEN(linked) then return "", -36; end; (* it may be cut short *)
231+ return linked[0..n], 0;
232+end;
233+
158234 (* The size of what path names, whether it is a directory or a regular
159235 file, following a symbolic link, and whether path is one; and 0, or
160236 minus an errno. *)
@@ -166,6 +242,8 @@
166242 i.symlink := st[3] & 0F000H = 0A000H; (* st_mode is S_IFLNK *)
167243 if C.stat(ADR(a[0]), ^U8(ADR(st))) # 0 then return i, failed(); end;
168244 i.size := st[6]; (* st_size *)
245+ i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *)
246+ i.mode := st[3] & 1FFH;
169247 i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *)
170248 i.file := st[3] & 0F000H = 8000H; (* S_IFREG *)
171249 return i, 0;
@@ -309,6 +387,15 @@
309387 return cwd[0..n], 0;
310388 end;
311389
390+(* Makes path the working directory. *)
391+proc setWorkDir*(path: []U8): I64;
392+var a: [4096]U8;
393+begin
394+ if not cstr(path, a) then return -36; end;
395+ if C.chdir(ADR(a[0])) # 0 then return failed(); end;
396+ return 0;
397+end;
398+
312399 (* A pseudo-terminal: the file descriptor of its controlling side, or
313400 minus an errno. Its other side, the terminal its programs run on, is
314401 unlocked, for spawn to open. *)
@@ -516,7 +603,6 @@
516603 socket, sent to after its end. *)
517604 proc main*;
518605 var fd, n, err, w, h, l, c, a: I64; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL;
519- dir, name2: [4096]U8;
520606 begin
521607 fd := create("/tmp/raz-sys-test");
522608 if fd < 0 then exit(2); end;
@@ -547,12 +633,34 @@
547633 i, err := info("/dev/null");
548634 if (err # 0) or i.dir or i.file then exit(2); end;
549635 remove("/tmp/raz-sys-link");
550- cstr("/tmp/raz-sys-dir", dir);
551- cstr("/tmp/raz-sys-link", name2);
552- if C.symlink(ADR(dir[0]), ADR(name2[0])) # 0 then exit(2); end;
636+ if symlink("/tmp/raz-sys-dir", "/tmp/raz-sys-link") # 0 then exit(2); end;
553637 i, err := info("/tmp/raz-sys-link");
554638 if (err # 0) or not i.dir or not i.symlink then exit(2); end;
639+ name, err := readLink("/tmp/raz-sys-link");
640+ if (err # 0) or (name # "/tmp/raz-sys-dir") then exit(2); end;
641+ name, err := readLink("/tmp/raz-sys-dir");
642+ if err # -22 then exit(2); end; (* EINVAL: not a link *)
555643 remove("/tmp/raz-sys-link");
644+ (* open as asked, seek, the mode and the time *)
645+ remove("/tmp/raz-sys-dir/h");
646+ fd := open("/tmp/raz-sys-dir/h", {writing, creating, exclusive});
647+ if (fd < 0) or (write(fd, "abc") # 3) then exit(2); end;
648+ close(fd);
649+ if open("/tmp/raz-sys-dir/h", {writing, creating, exclusive}) # -17 then exit(2); end;
650+ fd := open("/tmp/raz-sys-dir/h", {writing, appending});
651+ if (fd < 0) or (seek(fd, 0, false) # 0) or (write(fd, "de") # 2) then exit(2); end;
652+ close(fd);
653+ fd := open("/tmp/raz-sys-dir/h", {reading, writing});
654+ if (seek(fd, -2, true) # 3) or (read(fd, buf) # 2) or (buf[0..2] # "de") then exit(2); end;
655+ if (seek(fd, 1, false) # 1) or (write(fd, "B") # 1) or (seek(fd, 0, false) # 0) then exit(2); end;
656+ if (read(fd, buf) # 5) or (buf[0..5] # "aBcde") then exit(2); end;
657+ close(fd);
658+ i, err := info("/tmp/raz-sys-dir/h");
659+ if (err # 0) or (i.mode & 1C0H # 180H) or (i.mtime < 1700000000 * 1000000000) then exit(2); end;
660+ if chmod("/tmp/raz-sys-dir/h", 1EDH) # 0 then exit(2); end;
661+ i, err := info("/tmp/raz-sys-dir/h");
662+ if i.mode # 1EDH then exit(2); end;
663+ remove("/tmp/raz-sys-dir/h");
556664 i, err := info("/no/such/file");
557665 if err >= 0 then exit(2); end;
558666 if openDir(ADR(d), "/tmp/raz-sys-dir") # 0 then exit(2); end;
@@ -571,6 +679,8 @@
571679 if (LEN(env("PATH")) = 0) or (LEN(env("RAZ_NO_SUCH_VARIABLE")) # 0) then exit(2); end;
572680 name, err := workDir();
573681 if (err # 0) or (LEN(name) = 0) or (name[0] # '/') then exit(2); end;
682+ if (setWorkDir("/tmp/raz-sys-dir") # 0) or (removeDir("/tmp/raz-sys-dir") # 0) then exit(2); end;
683+ if (setWorkDir("/") # 0) or (removeDir("/tmp/raz-sys-dir") # -2) then exit(2); end;
574684 (* a socket: nothing at a path, then a listener that a connection
575685 makes ready, and a byte from it to the connection *)
576686 if connect("/tmp/raz-no-such-socket") # -2 then exit(2); end; (* ENOENT *)