From 9d27fd7e5a140a8f5f029e9512e1aa4633723123 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 15 Oct 2026 20:03:47 +0000 Subject: [PATCH] Sys: entryKind and Info.dev, for walking trees What rwalk needs, by the owner's word. Library growth that breaks no program; in both Sys files. - entryKind: what the name nextEntry gave last is, from d_type, so a walk needn't call linkInfo for each name; unknown where the file system doesn't say. - Info.dev: st_dev; with ino, a sure test that a link has led back into a directory already being walked. Sys's test covers each; raz-lib.md says them. --- diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 7b2acb4..0351b69 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -11,7 +11,7 @@ | Module | What | |---|---| | `Rt` | ending the program, `exit` and `atExit`; `stop`, which ends it with a message; `trace`, the calls under way; what the compiler's checks call when they fail | -| `Sys` | system calls: `open`, `openMode`, `openRead`, `create`, `read`, `write`, `seek`, `position`, `truncate`, `sync`, `syncAll`, `lock`, `close`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `chmod`, `symlink`, `readLink`, `info`, `linkInfo`, `infoOf`, `openDir`, `nextEntry`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `termSize`, `ready`; the environment, `env`, `workDir`, `setWorkDir`; programs, pipes and signals; time, `clock`, `now`, `sleep`; `random`; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and `Sys.raz` over the C library for the others | +| `Sys` | system calls: `open`, `openMode`, `openRead`, `create`, `read`, `write`, `seek`, `position`, `truncate`, `sync`, `syncAll`, `lock`, `close`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `chmod`, `symlink`, `readLink`, `info`, `linkInfo`, `infoOf`, `openDir`, `nextEntry`, `entryKind`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `termSize`, `ready`; the environment, `env`, `workDir`, `setWorkDir`; programs, pipes and signals; time, `clock`, `now`, `sleep`; `random`; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and `Sys.raz` over the C library for the others | | `Out` | writing text and numbers to standard output, buffered; safe to call from several threads | | `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C | | `Lock` | a lock for short sections, for C's threads and `Thread`'s alike | @@ -173,7 +173,9 @@ file*: BOOL; (* a regular file: not a directory, device, pipe or socket *) symlink*: BOOL; (* the path itself is a symbolic link *) ino*: U64; (* its inode, which a rename keeps and a new file doesn't *) + dev*: U64; (* the file system it is on; with ino, it names the file *) end; + Kind* = (unknown, isFile, isDir, isLink, isOther); (* what a directory says a name in it is *) Dir* = record ... end; (* an open directory; its fields are the target's *) Program* = record path*: []U8; @@ -222,6 +224,7 @@ proc openDir*(d: ^Dir; path: []U8): INT; proc nextEntry*(d: ^Dir): ([]U8, BOOL); (* a name, good until the next call; false at the end *) +proc entryKind*(d: ^Dir): Kind; (* what the name nextEntry gave last is *) proc closeDir*(d: ^Dir); proc rawMode*(fd: INT): BOOL; (* the terminal on fd: no echo, no lines, no signals *) @@ -282,7 +285,12 @@ holds data: opening a pipe would wait for a program to write to it. `nextEntry` gives every name in the directory except `.` and `..`, in -no particular order. `rawMode` turns off what the terminal does with +no particular order. `entryKind` says what the name it gave last is, a +file, a directory, a symbolic link (whatever it names) or something +else, as the directory itself records it, so that a program walking a +tree needn't ask `linkInfo` of every name, a system call each; where +the file system doesn't record it, it is `unknown`, and `linkInfo` +must be asked. `rawMode` turns off what the terminal does with keys before the program reads them (echo, line editing, signals, flow control), and NL to CR NL on output. A program that turns it on restores it before it ends: `Rt.atExit` is the place. @@ -315,7 +323,10 @@ of what it names, which need not be there. `Info.mtime` is what a program checks to see whether a file changed under it, and `Info.ino` whether it is still the same file, not one renamed over it; 0 on a target whose -files have none. `Info.mode` holds the nine +files have none. `Info.dev` is the file system the file is on: two +files are one only if both `dev` and `ino` agree, which is how a +program following links finds that one has led it back into a +directory it is already in. `Info.mode` holds the nine bits of `rwxrwxrwx`, and `chmod` sets them, 1EDH (755) for a program, 1A4H (644) for data. diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index 7d656b4..32580ea 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -30,11 +30,19 @@ file*: BOOL; (* not a directory, device, pipe or socket *) symlink*: BOOL; (* the path itself is a symbolic link *) ino*: U64; (* its inode, which a rename keeps and a new file doesn't *) + dev*: U64; (* the file system it is on; with ino, it names the file *) end; + (* What a directory says a name in it is, without asking the file: + a regular file, a directory, a symbolic link, or something else + (a device, pipe or socket); unknown where the file system + doesn't say, and linkInfo must be asked. *) + Kind* = (unknown, isFile, isDir, isLink, isOther); + (* An open directory: getdents64's records, read a block at a time. *) Dir* = record fd, pos, len: INT; + kind: Kind; (* of the name nextEntry gave last *) buf: [8192]U8; end; @@ -342,6 +350,7 @@ var i: Info; begin i.size := st[6]; (* st_size *) + i.dev := U64(st[0]); (* st_dev *) i.ino := U64(st[1]); (* st_ino *) i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *) i.mode := st[3] & 1FFH; @@ -403,10 +412,24 @@ return st[4] * unit, st[2] * unit, 0; (* f_bavail, f_blocks *) end; +(* The Kind of a d_type, as getdents64 and readdir give it: DT_REG is + 8, DT_DIR 4, DT_LNK 10, DT_UNKNOWN 0, and the rest (pipes, devices, + sockets) other things. *) +proc kindOf(t: U8): Kind; +begin + if t = 8 then return isFile; + elsif t = 4 then return isDir; + elsif t = 10 then return isLink; + elsif t = 0 then return unknown; + end; + return isOther; +end; + (* Opens the directory at path, for nextEntry. *) proc openDir*(d: ^Dir; path: []U8): INT; var a: [4096]U8; begin + d^.kind := unknown; d^.pos := 0; d^.len := 0; if not cstr(path, a) then return -36; end; @@ -431,6 +454,7 @@ at := d^.pos + 19; n := 0; while d^.buf[at + n] # 0 do INC(n); end; + d^.kind := kindOf(d^.buf[d^.pos + 18]); INC(d^.pos, INT(d^.buf[d^.pos + 16]) + INT(d^.buf[d^.pos + 17]) * 256); if (d^.buf[at..at + n] # ".") and (d^.buf[at..at + n] # "..") then return d^.buf[at..at + n], true; @@ -439,6 +463,14 @@ return "", false; end; +(* What the name nextEntry gave last is, as the directory says, so that + a walk needn't ask each file with linkInfo; unknown where the file + system doesn't say. A link is isLink, whatever it names. *) +proc entryKind*(d: ^Dir): Kind; +begin + return d^.kind; +end; + (* Closes the directory d that openDir opened. *) proc closeDir*(d: ^Dir); begin @@ -1322,7 +1354,7 @@ directory made, listed and emptied; no terminal; a socket, sent to after its end; programs; time and the network. *) proc main*; -var free, total: I64; fd, n, err, w, h, l, c, a, m, k: INT; got: [1]INT; buf: [16]U8; p, q: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL; +var free, total: I64; fd, n, err, w, h, l, c, a, m, k: INT; got: [1]INT; buf: [16]U8; p, q: ^U8; i, j: Info; d: Dir; name: []U8; ok: BOOL; begin fd := create("/tmp/raz-sys-test"); if fd < 0 then exit(2); end; @@ -1420,6 +1452,10 @@ if openDir(ADR(d), "/tmp/raz-sys-dir") # 0 then exit(2); end; name, ok := nextEntry(ADR(d)); if not ok or (name # "g") then exit(2); end; + if (entryKind(ADR(d)) # isFile) and (entryKind(ADR(d)) # unknown) then exit(2); end; + i, err := info("/tmp/raz-sys-dir/g"); + j, err := info("/tmp/raz-sys-dir"); + if (err # 0) or (i.dev # j.dev) or (i.ino = j.ino) then exit(2); end; name, ok := nextEntry(ADR(d)); if ok then exit(2); end; closeDir(ADR(d)); diff --git a/lib/Sys.raz b/lib/Sys.raz index 4f2d023..c778f34 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -23,6 +23,7 @@ sigTerm* = 15; sigChld* = 17; sigCont* = 18; sigStop* = 19; sigTstp* = 20; sigTtin* = 21; sigTtou* = 22; sigWinch* = 28; DirentName = 19; (* the offset of d_name in glibc's struct dirent *) + DirentType = 18; (* and of d_type *) TIOCGWINSZ = 5413H; (* glibc's, as the two below *) TIOCSWINSZ = 5414H; TIOCSCTTY = 540EH; @@ -43,11 +44,19 @@ file*: BOOL; (* not a directory, device, pipe or socket *) symlink*: BOOL; (* the path itself is a symbolic link *) ino*: U64; (* its inode, which a rename keeps and a new file doesn't *) + dev*: U64; (* the file system it is on; with ino, it names the file *) end; + (* What a directory says a name in it is, without asking the file: + a regular file, a directory, a symbolic link, or something else + (a device, pipe or socket); unknown where the file system + doesn't say, and linkInfo must be asked. *) + Kind* = (unknown, isFile, isDir, isLink, isOther); + (* An open directory: C's DIR. *) Dir* = record d: ^U8; + kind: Kind; (* of the name nextEntry gave last *) end; (* A program for spawn to start. *) @@ -385,6 +394,7 @@ var i: Info; begin i.size := st[6]; (* st_size *) + i.dev := U64(st[0]); (* st_dev *) i.ino := U64(st[1]); (* st_ino *) i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *) i.mode := st[3] & 1FFH; @@ -439,10 +449,24 @@ return st[4] * st[1], st[2] * st[1], 0; (* f_bavail, f_blocks, in f_frsize *) end; +(* The Kind of a d_type, as getdents64 and readdir give it: DT_REG is + 8, DT_DIR 4, DT_LNK 10, DT_UNKNOWN 0, and the rest (pipes, devices, + sockets) other things. *) +proc kindOf(t: U8): Kind; +begin + if t = 8 then return isFile; + elsif t = 4 then return isDir; + elsif t = 10 then return isLink; + elsif t = 0 then return unknown; + end; + return isOther; +end; + (* Opens the directory at path, for nextEntry. *) proc openDir*(d: ^Dir; path: []U8): INT; var a: [4096]U8; begin + d^.kind := unknown; if not cstr(path, a) then return -36; end; d^.d := C.opendir(ADR(a[0])); if d^.d = nil then return failed(); end; @@ -457,6 +481,7 @@ while true do e := C.readdir(d^.d); if e = nil then return "", false; end; + d^.kind := kindOf(e[DirentType]); e := e + DirentName; n := 0; while e[n] # 0 do INC(n); end; @@ -465,6 +490,14 @@ return "", false; end; +(* What the name nextEntry gave last is, as the directory says, so that + a walk needn't ask each file with linkInfo; unknown where the file + system doesn't say. A link is isLink, whatever it names. *) +proc entryKind*(d: ^Dir): Kind; +begin + return d^.kind; +end; + (* Closes the directory d that openDir opened. *) proc closeDir*(d: ^Dir); begin @@ -1304,7 +1337,7 @@ and closed; a directory made, listed and emptied; no terminal; a socket, sent to after its end; programs; time and the network. *) proc main*; -var free, total: I64; fd, n, err, w, h, l, c, a, m, k: INT; got: [1]INT; buf: [16]U8; p, q: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL; +var free, total: I64; fd, n, err, w, h, l, c, a, m, k: INT; got: [1]INT; buf: [16]U8; p, q: ^U8; i, j: Info; d: Dir; name: []U8; ok: BOOL; begin fd := create("/tmp/raz-sys-test"); if fd < 0 then exit(2); end; @@ -1402,6 +1435,10 @@ if openDir(ADR(d), "/tmp/raz-sys-dir") # 0 then exit(2); end; name, ok := nextEntry(ADR(d)); if not ok or (name # "g") then exit(2); end; + if (entryKind(ADR(d)) # isFile) and (entryKind(ADR(d)) # unknown) then exit(2); end; + i, err := info("/tmp/raz-sys-dir/g"); + j, err := info("/tmp/raz-sys-dir"); + if (err # 0) or (i.dev # j.dev) or (i.ino = j.ino) then exit(2); end; name, ok := nextEntry(ADR(d)); if ok then exit(2); end; closeDir(ADR(d));