From 73d8ebbe2d68aad748feea4de69ead0e2931b324 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 15 Oct 2026 17:00:12 +0100 Subject: [PATCH] Sys: linkInfo, Info.ino, openMode, lock, syncAll What sauce's Sos made by SYSCALL, now in both Sys files, from PROPOSALS.md. Library growth that breaks no program. - linkInfo: lstat, the link itself, dangling or not. - Info.ino: st_ino, for a cache keyed by inode. - openMode: open with a mode for a file it makes; open is it with 644. - lock: flock, LOCK_EX, LOCK_NB unless wait (-11 if held). - syncAll: syncfs; over C, POSIX sync, which flushes all. Sys's test covers each; raz-lib.md says them. --- diff --git a/PROPOSALS.md b/PROPOSALS.md index f5abce0..026df81 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -82,77 +82,6 @@ the budget; and, across modules, the imported module's text kept to be read again, which is to be checked. -## What a version control system needs of `Sys` - -Met in porting sauce (`~/r/sauce`) from Free Pascal, where a module of -its own, `Sos`, makes these calls by `SYSCALL`. Each is library -growth that breaks no program, so it could come with any release, not -only a thaw; and each has a C call behind it, for `Sys.raz` over the C -library. - -**Seeing a link itself.** `info` follows a symbolic link, as it should -for most programs; but a program that tracks or copies a tree must see -the link: a dangling one is -ENOENT to `info`, and the size it gives is -the target's, not the link's. - -``` -proc linkInfo*(path: []U8): (Info, INT); (* the path itself, not what a link at its end names *) -``` - -For a link, `symlink` is true, `dir` and `file` false, and `size` the -length of its target, as `readLink` gives it. The C call is `lstat`. - -**The inode.** A program that keeps a cache of files, to know one -unchanged without reading it, keys it by inode, size and time, as git -and sauce do; a file replaced by a rename has a new inode and may keep -its size and time. `Info` gains a field: - -``` -ino*: U64; (* the file's inode, on its file system *) -``` - -A record literal names its fields, so no program changes. On a target -whose files have no inode, 0. - -**A new file's mode.** `open` with `creating` always asks for 644, so -a program can't make a file executable as it writes it, nor read-only: -it must `chmod` after, and between the two the file is there with the -wrong mode, and the umask is lost (sauce writes 666 or 777 less the -umask for the working tree, 444 for its objects). - -``` -proc openMode*(path: []U8; how: Accesses; mode: INT): INT; (* open, a file it creates given mode, less the umask *) -``` - -`open(path, how)` is `openMode(path, how, 1A4H)`. - -**A lock between programs.** Two programs that change the same files -need to take turns. A lock file made with `exclusive` works until a -program dies holding it, and then it is stale forever; the kernel's -lock on an open file goes when the program ends, however it ends. - -``` -proc lock*(fd: INT; wait: BOOL): INT; (* an exclusive lock on the open file fd, until it is closed; -11 if another program holds one and not wait *) -``` - -The C call is `flock`; it locks the open file, not a range of it, and -is enough for a program that only needs one at a time. - -**Flushing a file system.** `sync` makes one file durable. A program -that writes hundreds of files and then one that names them all (a -commit's objects, then HEAD) wants them all on the disk before the -last, without a `sync` of each, which is slow: - -``` -proc syncAll*(fd: INT): INT; (* everything written to the file system holding fd, on the disk *) -``` - -The Linux call is `syncfs`; where there is none, `sync`, which flushes -every file system and so does no less. - -Not needed: syncing a directory, so that a rename in it is durable, is -already `sync(open(dir, {reading}))`. - ## `Buf` when memory runs out `Buf.add` and `Buf.str` call `room`, and when it fails they drop the diff --git a/doc/raz-lib.md b/doc/raz-lib.md index df4e563..2e2d35f 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`, `openRead`, `create`, `read`, `write`, `seek`, `position`, `truncate`, `sync`, `close`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `chmod`, `symlink`, `readLink`, `info`, `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`, `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 | @@ -171,6 +171,7 @@ dir*: BOOL; (* a directory *) 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 *) end; Dir* = record ... end; (* an open directory; its fields are the target's *) Program* = record @@ -190,12 +191,15 @@ proc write*(fd: INT; s: []U8): INT; (* the bytes written *) proc read*(fd: INT; buf: []U8): INT; (* the bytes read; 0 at the end *) proc open*(path: []U8; how: Accesses): INT; (* its fd *) +proc openMode*(path: []U8; how: Accesses; mode: INT): INT; (* open, a file it makes given mode *) proc openRead*(path: []U8): INT; (* open(path, {reading}) *) proc create*(path: []U8): INT; (* open(path, {writing, creating, truncating}) *) proc seek*(fd: INT; pos: I64; fromEnd: BOOL): I64; (* the new position *) proc position*(fd: INT): I64; (* where the next read or write is *) proc truncate*(fd: INT; size: I64): INT; (* the file made size bytes long *) proc sync*(fd: INT): INT; (* what was written to it, on the disk *) +proc syncAll*(fd: INT): INT; (* all that was written to its file system, on the disk *) +proc lock*(fd: INT; wait: BOOL): INT; (* the file's lock, held until fd is closed *) proc close*(fd: INT); proc isTerminal*(fd: INT): BOOL; proc mmap*(size: INT): ^U8; (* size bytes of zeroed memory, in whole pages; or nil *) @@ -211,6 +215,7 @@ proc symlink*(target, path: []U8): INT; (* path made a link to target *) proc readLink*(path: []U8): ([]U8, INT); (* what the link names, good until the next call *) proc info*(path: []U8): (Info, INT); (* following a symbolic link *) +proc linkInfo*(path: []U8): (Info, INT); (* the link itself *) proc infoOf*(fd: INT): (Info, INT); (* the open file's *) proc space*(path: []U8): (I64, I64, INT); (* the bytes free and in all on path's file system *) @@ -286,15 +291,30 @@ writable by its owner; with `exclusive`, failing with -17 (EEXIST) if it is there, as a program does that makes a lock or a file of its own; `truncating` it to nothing; `appending`, each write going to its -end, however other programs write it too. `seek` moves to `pos` from +end, however other programs write it too. `openMode` is `open` that +gives a file it makes the bits `mode`, less those the umask takes +away: 1EDH (755) for a program, 124H (444) for a file never to be +written; `open` gives 1A4H (644). `seek` moves to `pos` from the start of the file, or from its end if `fromEnd`, where a negative `pos` reads back from the end; `position` says where the file is, without moving. `truncate` makes the file `size` bytes long, cutting it or adding zeros, and leaves the position where it was. `sync` returns once what was written to the file is on the disk, as a -program needs before it says a file is saved. `infoOf` is `info` of -an open file: its size is what it holds now, however it was written. `Info.mtime` is what a program checks -to see whether a file changed under it; `Info.mode` holds the nine +program needs before it says a file is saved; `syncAll` returns once +everything written to the file system that holds the file is on the +disk, at once rather than a file at a time, and may flush other file +systems too. `lock` takes the file's lock, which one open file holds +at a time: another that asks for it waits if `wait`, and otherwise +fails with -11 (EAGAIN). The lock goes when the file is closed or the +program ends, however it ends, so it never goes stale as a file made +with `exclusive` does. `infoOf` is `info` of +an open file: its size is what it holds now, however it was written; +`linkInfo` is `info` of a symbolic link itself, not what it names, so +that `symlink` is true, `dir` and `file` false, and `size` the length +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 bits of `rwxrwxrwx`, and `chmod` sets them, 1EDH (755) for a program, 1A4H (644) for data. diff --git a/lib/C.raz b/lib/C.raz index 49872e8..9755fba 100644 --- a/lib/C.raz +++ b/lib/C.raz @@ -60,6 +60,8 @@ extern proc stat*(path, st: ^U8): I32; extern proc fstat*(fd: I32; st: ^U8): I32; extern proc fsync*(fd: I32): I32; +extern proc sync*; +extern proc flock*(fd, how: I32): I32; extern proc lstat*(path, st: ^U8): I32; extern proc statvfs*(path, st: ^U8): I32; extern proc symlink*(target, path: ^U8): I32; diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index 0a44384..7d656b4 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -29,6 +29,7 @@ dir*: BOOL; (* a directory *) 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 *) end; (* An open directory: getdents64's records, read a block at a time. *) @@ -147,15 +148,22 @@ end; (* The file at path, opened as how asks: its file descriptor, or minus - an errno. A file it creates is readable by all, writable by its - owner. *) -proc open*(path: []U8; how: Accesses): INT; + an errno. A file it creates has the permission bits mode, less + those the umask takes away. *) +proc openMode*(path: []U8; how: Accesses; mode: INT): INT; var buf: [4096]U8; begin if not cstr(path, buf) then return -36; end; (* ENAMETOOLONG *) - return SYSCALL(2, ADR(buf), flags(how), 1A4H); (* mode 644 *) + return SYSCALL(2, ADR(buf), flags(how), mode); end; +(* openMode with 644: a file it creates is readable by all, writable + by its owner. *) +proc open*(path: []U8; how: Accesses): INT; +begin + return openMode(path, how, 1A4H); (* 644 *) +end; + (* The file at path, opened for reading. *) proc openRead*(path: []U8): INT; begin @@ -197,6 +205,24 @@ return SYSCALL(74, fd); (* fsync *) end; +(* Returns once everything written to the file system that holds fd + is on the disk, as a sync of each file would, but at once: 0, or + minus an errno. *) +proc syncAll*(fd: INT): INT; +begin + return SYSCALL(306, fd); (* syncfs *) +end; + +(* Takes the lock on the open file fd, which only one program holds at + a time, and which goes when fd is closed or the program ends, + however it ends. If another program holds it, waits for it if wait, + and otherwise returns -11 (EAGAIN). 0, or minus an errno. *) +proc lock*(fd: INT; wait: BOOL): INT; +begin + if wait then return SYSCALL(73, fd, 2); end; (* flock, LOCK_EX *) + return SYSCALL(73, fd, 2 | 4); (* LOCK_EX | LOCK_NB *) +end; + (* Closes the file descriptor fd. *) proc close*(fd: INT); begin @@ -316,6 +342,7 @@ var i: Info; begin i.size := st[6]; (* st_size *) + i.ino := U64(st[1]); (* st_ino *) i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *) i.mode := st[3] & 1FFH; i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *) @@ -340,6 +367,20 @@ return i, 0; end; +(* info of path itself: if it is a symbolic link, of the link, not of + what it names, so that symlink is true, dir and file false, and the + size the length of what it names. *) +proc linkInfo*(path: []U8): (Info, INT); +var a: [4096]U8; st: [18]INT; i: Info; r: INT; +begin + if not cstr(path, a) then return i, -36; end; + r := SYSCALL(6, ADR(a), ADR(st)); (* lstat *) + if r # 0 then return i, r; end; + i := fromStat(st); + i.symlink := st[3] & 0F000H = 0A000H; (* S_IFLNK *) + return i, 0; +end; + (* info of the file open on fd, which is never a link. *) proc infoOf*(fd: INT): (Info, INT); var st: [18]INT; i: Info; r: INT; @@ -1319,11 +1360,19 @@ if symlink("/tmp/raz-sys-dir", "/tmp/raz-sys-link") # 0 then exit(2); end; i, err := info("/tmp/raz-sys-link"); if (err # 0) or not i.dir or not i.symlink then exit(2); end; + i, err := linkInfo("/tmp/raz-sys-link"); + if (err # 0) or i.dir or i.file or not i.symlink or (i.size # 16) then exit(2); end; + i, err := linkInfo("/tmp/raz-sys-dir"); + if (err # 0) or not i.dir or i.symlink or (i.ino = 0) then exit(2); end; name, err := readLink("/tmp/raz-sys-link"); if (err # 0) or (name # "/tmp/raz-sys-dir") then exit(2); end; name, err := readLink("/tmp/raz-sys-dir"); if err # -22 then exit(2); end; (* EINVAL: not a link *) _ := remove("/tmp/raz-sys-link"); + if symlink("/no/such/file", "/tmp/raz-sys-link") # 0 then exit(2); end; (* dangling *) + i, err := linkInfo("/tmp/raz-sys-link"); + if (err # 0) or not i.symlink or (i.size # 13) then exit(2); end; + _ := remove("/tmp/raz-sys-link"); (* open as asked, seek, the mode and the time *) _ := remove("/tmp/raz-sys-dir/h"); fd := open("/tmp/raz-sys-dir/h", {writing, creating, exclusive}); @@ -1342,7 +1391,16 @@ if (truncate(fd, 4) # 0) or (position(fd) # 2) or (sync(fd) # 0) then exit(2); end; i, err := infoOf(fd); if (err # 0) or (i.size # 4) or not i.file or i.symlink then exit(2); end; + m := INT(i.ino); + i, err := info("/tmp/raz-sys-dir/h"); + if (err # 0) or (INT(i.ino) # m) or (syncAll(fd) # 0) then exit(2); end; + (* the lock: one open file holds it, another can't take it until + the first is closed *) + k := openRead("/tmp/raz-sys-dir/h"); + if (lock(fd, false) # 0) or (lock(k, false) # -11) then exit(2); end; close(fd); + if lock(k, true) # 0 then exit(2); end; + close(k); if (position(fd) # -9) or (truncate(fd, 0) # -9) or (sync(fd) # -9) then exit(2); end; (* EBADF *) i, err := infoOf(fd); if err # -9 then exit(2); end; @@ -1351,6 +1409,11 @@ if chmod("/tmp/raz-sys-dir/h", 1EDH) # 0 then exit(2); end; i, err := info("/tmp/raz-sys-dir/h"); if i.mode # 1EDH then exit(2); end; + _ := remove("/tmp/raz-sys-dir/h"); + fd := openMode("/tmp/raz-sys-dir/h", {writing, creating}, 1C0H); (* rwx------ *) + close(fd); + i, err := info("/tmp/raz-sys-dir/h"); + if (fd < 0) or (err # 0) or (i.mode # 1C0H) then exit(2); end; _ := remove("/tmp/raz-sys-dir/h"); i, err := info("/no/such/file"); if err >= 0 then exit(2); end; diff --git a/lib/Sys.raz b/lib/Sys.raz index 2fd24ba..4f2d023 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -42,6 +42,7 @@ dir*: BOOL; (* a directory *) 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 *) end; (* An open directory: C's DIR. *) @@ -175,17 +176,24 @@ end; (* The file at path, opened as how asks: its file descriptor, or minus - an errno. A file it creates is readable by all, writable by its - owner. *) -proc open*(path: []U8; how: Accesses): INT; + an errno. A file it creates has the permission bits mode, less + those the umask takes away. *) +proc openMode*(path: []U8; how: Accesses; mode: INT): INT; var buf: [4096]U8; r: INT; begin if not cstr(path, buf) then return -36; end; - r := INT(C.open(ADR(buf[0]), flags(how), 1A4H)); (* mode 644 *) + r := INT(C.open(ADR(buf[0]), flags(how), mode)); if r < 0 then return failed(); end; return r; end; +(* openMode with 644: a file it creates is readable by all, writable + by its owner. *) +proc open*(path: []U8; how: Accesses): INT; +begin + return openMode(path, how, 1A4H); (* 644 *) +end; + (* The file at path, opened for reading. *) proc openRead*(path: []U8): INT; begin @@ -235,6 +243,28 @@ return 0; end; +(* Returns once everything written to the file system that holds fd + is on the disk: 0, or minus an errno. POSIX has no call for one file + system, so it is sync's, which flushes them all and does no less. *) +proc syncAll*(fd: INT): INT; +begin + C.sync; + return 0; +end; + +(* Takes the lock on the open file fd, which only one program holds at + a time, and which goes when fd is closed or the program ends, + however it ends. If another program holds it, waits for it if wait, + and otherwise returns -11 (EAGAIN). 0, or minus an errno. *) +proc lock*(fd: INT; wait: BOOL): INT; +var how: I32; +begin + how := 2; (* LOCK_EX *) + if not wait then how := how | 4; end; (* LOCK_NB *) + if C.flock(I32(fd), how) # 0 then return failed(); end; + return 0; +end; + (* Closes the file descriptor fd. *) proc close*(fd: INT); begin @@ -355,6 +385,7 @@ var i: Info; begin i.size := st[6]; (* st_size *) + i.ino := U64(st[1]); (* st_ino *) i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *) i.mode := st[3] & 1FFH; i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *) @@ -377,6 +408,19 @@ return i, 0; end; +(* info of path itself: if it is a symbolic link, of the link, not of + what it names, so that symlink is true, dir and file false, and the + size the length of what it names. *) +proc linkInfo*(path: []U8): (Info, INT); +var a: [4096]U8; st: [18]INT; i: Info; +begin + if not cstr(path, a) then return i, -36; end; + if C.lstat(ADR(a[0]), ^U8(ADR(st))) # 0 then return i, failed(); end; + i := fromStat(st); + i.symlink := st[3] & 0F000H = 0A000H; (* S_IFLNK *) + return i, 0; +end; + (* info of the file open on fd, which is never a link. *) proc infoOf*(fd: INT): (Info, INT); var st: [18]INT; i: Info; @@ -1298,11 +1342,19 @@ if symlink("/tmp/raz-sys-dir", "/tmp/raz-sys-link") # 0 then exit(2); end; i, err := info("/tmp/raz-sys-link"); if (err # 0) or not i.dir or not i.symlink then exit(2); end; + i, err := linkInfo("/tmp/raz-sys-link"); + if (err # 0) or i.dir or i.file or not i.symlink or (i.size # 16) then exit(2); end; + i, err := linkInfo("/tmp/raz-sys-dir"); + if (err # 0) or not i.dir or i.symlink or (i.ino = 0) then exit(2); end; name, err := readLink("/tmp/raz-sys-link"); if (err # 0) or (name # "/tmp/raz-sys-dir") then exit(2); end; name, err := readLink("/tmp/raz-sys-dir"); if err # -22 then exit(2); end; (* EINVAL: not a link *) _ := remove("/tmp/raz-sys-link"); + if symlink("/no/such/file", "/tmp/raz-sys-link") # 0 then exit(2); end; (* dangling *) + i, err := linkInfo("/tmp/raz-sys-link"); + if (err # 0) or not i.symlink or (i.size # 13) then exit(2); end; + _ := remove("/tmp/raz-sys-link"); (* open as asked, seek, the mode and the time *) _ := remove("/tmp/raz-sys-dir/h"); fd := open("/tmp/raz-sys-dir/h", {writing, creating, exclusive}); @@ -1321,7 +1373,16 @@ if (truncate(fd, 4) # 0) or (position(fd) # 2) or (sync(fd) # 0) then exit(2); end; i, err := infoOf(fd); if (err # 0) or (i.size # 4) or not i.file or i.symlink then exit(2); end; + m := INT(i.ino); + i, err := info("/tmp/raz-sys-dir/h"); + if (err # 0) or (INT(i.ino) # m) or (syncAll(fd) # 0) then exit(2); end; + (* the lock: one open file holds it, another can't take it until + the first is closed *) + k := openRead("/tmp/raz-sys-dir/h"); + if (lock(fd, false) # 0) or (lock(k, false) # -11) then exit(2); end; close(fd); + if lock(k, true) # 0 then exit(2); end; + close(k); if (position(fd) # -9) or (truncate(fd, 0) # -9) or (sync(fd) # -9) then exit(2); end; (* EBADF *) i, err := infoOf(fd); if err # -9 then exit(2); end; @@ -1330,6 +1391,11 @@ if chmod("/tmp/raz-sys-dir/h", 1EDH) # 0 then exit(2); end; i, err := info("/tmp/raz-sys-dir/h"); if i.mode # 1EDH then exit(2); end; + _ := remove("/tmp/raz-sys-dir/h"); + fd := openMode("/tmp/raz-sys-dir/h", {writing, creating}, 1C0H); (* rwx------ *) + close(fd); + i, err := info("/tmp/raz-sys-dir/h"); + if (fd < 0) or (err # 0) or (i.mode # 1C0H) then exit(2); end; _ := remove("/tmp/raz-sys-dir/h"); i, err := info("/no/such/file"); if err >= 0 then exit(2); end;