From 2ae3d534e686667532c31462cdc8d2e03fb1722a Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Tue, 20 Oct 2026 07:40:59 +0100 Subject: [PATCH] Sys: setTime, lockShared, makeDirMode, closeChecked; Info.seconds and special What a program that copies files whole needs, found porting fdn, the backup, from Free Pascal, where it must keep every bit it did: - setTime(path, sec, nsec): a file's time set, utimensat; nsec outside 0..999999999 is -22. - Info.seconds: st_mtime itself. Info.mtime, in ns, wraps past 2262 and before 1677, so a time copied through it came out centuries off. - Info.special: the set-user-id, set-group-id and sticky bits, which Info.mode leaves out; chmod takes mode | special. - makeDirMode(path, mode): makeDir given a mode, as openMode is open. - lockShared(fd, wait): flock's LOCK_SH, for a reader that shares a file with other readers while lock keeps a writer out. - closeChecked(fd): close giving the error a full disk or an NFS quota shows only there; never tried again. Over C through C.utimensat and CAbi's new atFdcwd and lockSh, which tools/cabi.c writes. Library growth that breaks no program: new procedures, and fields a record literal leaves zero. Sys's tests, over both and over musl, try each: times in 1970, before it and in 2300 to the ns, both locks against each other, a set-user-id file, a sticky directory made 700, a close twice. --- diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 41a00bd..9f4b01a 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`, `dup`, `dupTo`, `dupAbove`, `keepOnExec`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `chmod`, `access`, `umask`, `symlink`, `readLink`, `info`, `linkInfo`, `infoOf`, `openDir`, `nextEntry`, `entryKind`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `getMode`, `setMode`, `termSize`, `ready`; the environment, `env`, `envAt`, `setEnv`, `workDir`, `setWorkDir`; programs, `spawn`, `exec` and `execve`, pipes and signals; the user, the machine, time used and limits; time, `clock`, `now`, `sleep`; `random`; errors, by name, and what one means, `errorText`; 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, which takes what each C library fixes from `CAbi` | +| `Sys` | system calls: `open`, `openMode`, `openRead`, `create`, `read`, `write`, `seek`, `position`, `truncate`, `sync`, `syncAll`, `lock`, `lockShared`, `close`, `closeChecked`, `dup`, `dupTo`, `dupAbove`, `keepOnExec`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `makeDirMode`, `chmod`, `setTime`, `access`, `umask`, `symlink`, `readLink`, `info`, `linkInfo`, `infoOf`, `openDir`, `nextEntry`, `entryKind`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `getMode`, `setMode`, `termSize`, `ready`; the environment, `env`, `envAt`, `setEnv`, `workDir`, `setWorkDir`; programs, `spawn`, `exec` and `execve`, pipes and signals; the user, the machine, time used and limits; time, `clock`, `now`, `sleep`; `random`; errors, by name, and what one means, `errorText`; 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, which takes what each C library fixes from `CAbi` | | `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 | @@ -203,6 +203,7 @@ Info* = record size*: I64; (* in bytes *) mtime*: I64; (* when its contents last changed, in nanoseconds since 1970 *) + seconds*: I64; (* mtime in whole seconds, rounded down: exact past 2262 and before 1677, where mtime wraps *) mode*: INT; (* its permission bits, as chmod takes them: 1EDH is rwxr-xr-x *) dir*: BOOL; (* a directory *) file*: BOOL; (* a regular file: not a directory, device, pipe or socket *) @@ -210,6 +211,7 @@ 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 *) links*: U64; (* how many names it has: 1 for a file no hard link shares *) + special*: INT; (* its set-user-id, set-group-id and sticky bits, as chmod takes them: 800H, 400H, 200H *) 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 *) @@ -241,7 +243,9 @@ 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 lockShared*(fd: INT; wait: BOOL): INT; (* lock, shared with others that ask it so *) proc close*(fd: INT); +proc closeChecked*(fd: INT): INT; (* close, and 0 or minus the errno it failed with *) proc dup*(fd: INT): INT; (* a new fd for the same file, the lowest free *) proc dupTo*(fd, into: INT): INT; (* into made the same file as fd, closing what it was *) proc dupAbove*(fd, least: INT): INT; (* a new fd for the same file, the lowest free from least on, closed on exec *) @@ -256,7 +260,9 @@ proc remove*(path: []U8): INT; (* a file, not a directory *) proc removeDir*(path: []U8): INT; (* an empty directory *) proc makeDir*(path: []U8): INT; (* readable by all, writable by its owner *) +proc makeDirMode*(path: []U8; mode: INT): INT; (* makeDir, given mode *) proc chmod*(path: []U8; mode: INT): INT; +proc setTime*(path: []U8; sec: I64; nsec: INT): INT; (* its mtime, and the time it was read, made sec and nsec *) proc access*(path: []U8; how: Accesses): INT; (* 0 if this user may read, write or execute it *) proc umask*(mask: INT): INT; (* the bits taken from every file made; the mask it was *) proc symlink*(target, path: []U8): INT; (* path made a link to target *) @@ -374,12 +380,21 @@ 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 +systems too. `lock` takes the file's lock for one open file alone: +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; +with `exclusive` does. `lockShared` takes it shared, as a program +that only reads asks it: any number may hold it so at once, and `lock` +waits, or fails, until the last has let it go, as a shared one waits +for the one that `lock` took. Both are `flock`'s, so they agree with +any other program that takes the file's lock so. `closeChecked` closes +as `close` does, and says if that failed: a file system may say only +then that what was written never reached it, a full disk or a quota +over NFS, so a program that must know its writes landed, as one that +copies a backup, asks it. The fd is closed either way, so a failed +`closeChecked`, -4 (EINTR) too, is never tried again. `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 @@ -392,7 +407,23 @@ has: more than 1 if a hard link shares it, so that writing to it writes to a file named elsewhere too; 0 on a target that doesn't say. `Info.mode` holds the nine bits of `rwxrwxrwx`, and `chmod` sets them, -1EDH (755) for a program, 1A4H (644) for data. +1EDH (755) for a program, 1A4H (644) for data; `Info.special` holds +the three above them, set-user-id 800H, set-group-id 400H and sticky +200H, which `chmod` sets too, with `mode | special`, as a program that +copies a file keeps them. `makeDirMode` is `makeDir` that gives the +directory the bits `mode`, less those the umask takes away, as +`openMode` gives a file: 1C0H (700) for one only its owner may enter. +Of the three above them, Linux gives a new directory only sticky; it +takes set-group-id from the directory it is made in. `setTime` sets +the file's time, following a symbolic link, and the time it was last +read with it, as a program that copies or restores a file keeps its +time, so that a later run sees it unchanged: `sec` seconds and `nsec`, +from 0 to 999999999, nanoseconds since 1970. `Info.mtime` holds the +time in nanoseconds, which an `I64` holds from 1677 to 2262 only; +`Info.seconds` holds it in whole seconds, rounded down, whatever it +is, so a program that copies times exactly takes `Info.seconds`, and +the nanoseconds as `mtime - seconds * 1000000000`, which is right in +any year, since the wrapping cancels out (section 6.1). **Programs.** `openPty` makes a pseudo-terminal, a terminal that a program, a terminal emulator, plays the screen and the keyboard of: diff --git a/doc/report-history.md b/doc/report-history.md index 8f179ec..400ffa1 100644 --- a/doc/report-history.md +++ b/doc/report-history.md @@ -216,4 +216,8 @@ `ADR(s[0])` when `s` isn't empty, and what the slice holds when it is, where `ADR(s[0])` stops the program; of an array, that of the slice of all of it (sections 5.3 and 6.8). A predeclared name may be - hidden (section 2), so it breaks no program. + hidden (section 2), so it breaks no program. Then `Sys` gains + `setTime`, `lockShared`, `makeDirMode` and `closeChecked`, and + `Info` gains `seconds` and `special`, for a program that copies files + whole: their times in any year, their set-user-id, set-group-id and + sticky bits, and a close that fails (volume 2). It breaks no program. diff --git a/lib/C.raz b/lib/C.raz index 7e4b986..c3825af 100644 --- a/lib/C.raz +++ b/lib/C.raz @@ -23,6 +23,7 @@ extern proc mkdir*(path: ^U8; mode: U32): I32; extern proc rmdir*(path: ^U8): I32; extern proc chmod*(path: ^U8; mode: U32): I32; +extern proc utimensat*(dir: I32; path, times: ^U8; flags: I32): I32; extern proc readlink*(path, buf: ^U8; n: UINT): INT; extern proc lseek*(fd: I32; off: I64; whence: I32): I64; extern proc opendir*(path: ^U8): ^U8; (* DIR * *) diff --git a/lib/CAbi.linux-x86_64.raz b/lib/CAbi.linux-x86_64.raz index d5df9c8..e974473 100644 --- a/lib/CAbi.linux-x86_64.raz +++ b/lib/CAbi.linux-x86_64.raz @@ -29,6 +29,7 @@ seekCur* = 1; seekEnd* = 2; lockEx* = 2; + lockSh* = 1; lockNb* = 4; protRead* = 1; protWrite* = 2; @@ -36,6 +37,7 @@ mapPrivate* = 2; mapAnonymous* = 32; mfdCloexec* = 1; + atFdcwd* = -100; fOk* = 0; rOk* = 4; wOk* = 2; diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index 61c529e..00f1333 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -45,6 +45,7 @@ Info* = record size*: I64; (* in bytes *) mtime*: I64; (* its contents' last change, in ns since 1970 *) + seconds*: I64; (* mtime in whole seconds, rounded down: exact past 2262 and before 1677, where mtime wraps *) mode*: INT; (* its permission bits, rwxrwxrwx *) dir*: BOOL; (* a directory *) file*: BOOL; (* not a directory, device, pipe or socket *) @@ -52,6 +53,7 @@ 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 *) links*: U64; (* how many names it has: 1 for a file no hard link shares *) + special*: INT; (* its set-user-id, set-group-id and sticky bits, as chmod takes them: 800H, 400H, 200H *) end; (* What a directory says a name in it is, without asking the file: @@ -272,12 +274,32 @@ begin if wait then return SYSCALL(73, fd, 2); end; (* flock, LOCK_EX *) return SYSCALL(73, fd, 2 | 4); (* LOCK_EX | LOCK_NB *) +end; + +(* Takes the lock on the open file fd as lock does, but shared: any + number of programs may hold it so at once, as readers do, while none + holds it as lock takes it. 0, or minus an errno. *) +proc lockShared*(fd: INT; wait: BOOL): INT; +begin + if wait then return SYSCALL(73, fd, 1); end; (* flock, LOCK_SH *) + return SYSCALL(73, fd, 1 | 4); (* LOCK_SH | LOCK_NB *) end; (* Closes the file descriptor fd. *) proc close*(fd: INT); begin _ := SYSCALL(3, fd); +end; + +(* Closes the file descriptor fd, as close does, and says whether that + failed: 0, or minus an errno. A file system may report only at the + close that what was written to the file never reached it, -28 + (ENOSPC) or -122 (EDQUOT) say, as NFS does; a program that must know + its writes landed asks so. The fd is closed either way, so it is never + closed again, after -4 (EINTR) too. *) +proc closeChecked*(fd: INT): INT; +begin + return SYSCALL(3, fd); end; (* A new file descriptor for the file fd is open on, the lowest free: @@ -387,6 +409,16 @@ return SYSCALL(83, ADR(a), 1EDH); (* mkdir, mode 755 *) end; +(* Makes a directory at path with the permission bits mode, less those + the umask takes away: 1C0H (700) for one only its owner may enter. Of + the bits above them Linux keeps only sticky, 200H. *) +proc makeDirMode*(path: []U8; mode: INT): INT; +var a: [4096]U8; +begin + if not cstr(path, a) then return -36; end; + return SYSCALL(83, ADR(a), mode); (* mkdir *) +end; + (* Removes the directory at path, which must be empty. *) proc removeDir*(path: []U8): INT; var a: [4096]U8; @@ -403,6 +435,21 @@ return SYSCALL(90, ADR(a), mode); end; +(* Sets when the contents of what path names last changed, and when it + was last read, to sec seconds and nsec nanoseconds since 1970, as a + program copying a file keeps its time: Info.seconds gives sec back, + whatever the time. *) +proc setTime*(path: []U8; sec: I64; nsec: INT): INT; +var a: [4096]U8; t: [2]Timespec; +begin + if not cstr(path, a) then return -36; end; + if (nsec < 0) or (nsec >= 1000000000) then return -22; end; (* EINVAL *) + t[0].sec := sec; + t[0].nsec := nsec; + t[1] := t[0]; + return SYSCALL(280, -100, ADR(a), ADR(t), 0); (* utimensat, from the working directory *) +end; + (* 0 if this user, by its real ids, may do with path what how asks: reading, writing and executing; minus an errno, -13 (EACCES) if not. An empty how asks only whether path is there. *) @@ -453,7 +500,9 @@ i.ino := U64(st[1]); (* st_ino *) i.links := U64(st[2]); (* st_nlink *) i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *) + i.seconds := st[11]; i.mode := st[3] & 1FFH; + i.special := st[3] & 0E00H; i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *) i.file := st[3] & 0F000H = 8000H; (* S_IFREG *) return i; @@ -2143,7 +2192,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, j: Info; d: Dir; name: []U8; ok: BOOL; +var free, total: I64; fd, n, err, w, h, l, c, a, m, k, o: 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; @@ -2225,7 +2274,23 @@ close(fd); if lock(k, true) # 0 then exit(2); end; close(k); + (* the shared lock: two open files hold it at once, lock is refused + until both have gone, and a shared one while lock holds it *) + fd := openRead("/tmp/raz-sys-dir/h"); + k := openRead("/tmp/raz-sys-dir/h"); + if (lockShared(fd, false) # 0) or (lockShared(k, true) # 0) then exit(2); end; + o := openRead("/tmp/raz-sys-dir/h"); + if lock(o, false) # -11 then exit(2); end; + close(fd); + close(k); + if lock(o, false) # 0 then exit(2); end; + fd := openRead("/tmp/raz-sys-dir/h"); + if lockShared(fd, false) # -11 then exit(2); end; + close(fd); + close(o); if (position(fd) # -9) or (truncate(fd, 0) # -9) or (sync(fd) # -9) then exit(2); end; (* EBADF *) + k := openRead("/tmp/raz-sys-dir/h"); + if (closeChecked(k) # 0) or (closeChecked(k) # -9) then exit(2); end; i, err := infoOf(fd); if err # -9 then exit(2); end; i, err := info("/tmp/raz-sys-dir/h"); @@ -2233,6 +2298,26 @@ 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; + (* setTime, after 1970 and before it, to the nanosecond *) + if setTime("/tmp/raz-sys-dir/h", 1234567890, 123456789) # 0 then exit(2); end; + i, err := info("/tmp/raz-sys-dir/h"); + if (i.mtime # 1234567890123456789) or (i.seconds # 1234567890) then exit(2); end; + if setTime("/tmp/raz-sys-dir/h", -2, 500000000) # 0 then exit(2); end; + i, err := info("/tmp/raz-sys-dir/h"); + if (i.mtime # -1500000000) or (i.seconds # -2) then exit(2); end; + if setTime("/tmp/raz-sys-dir/h", 10413792000, 0) # 0 then exit(2); end; (* 2300, past mtime's reach *) + i, err := info("/tmp/raz-sys-dir/h"); + if i.seconds # 10413792000 then exit(2); end; + if setTime("/tmp/raz-sys-dir/h", 0, 1000000000) # -22 then exit(2); end; + if setTime("/no/such/file", 0, 0) # -2 then exit(2); end; + (* the special bits: set-user-id by chmod, and sticky by makeDirMode *) + if chmod("/tmp/raz-sys-dir/h", 9EDH) # 0 then exit(2); end; + i, err := info("/tmp/raz-sys-dir/h"); + if (i.mode # 1EDH) or (i.special # 800H) then exit(2); end; + if makeDirMode("/tmp/raz-sys-dir/m", 3C0H) # 0 then exit(2); end; + i, err := info("/tmp/raz-sys-dir/m"); + if (err # 0) or not i.dir or (i.mode # 1C0H) or (i.special # 200H) then exit(2); end; + if removeDir("/tmp/raz-sys-dir/m") # 0 then exit(2); end; _ := remove("/tmp/raz-sys-dir/h"); fd := openMode("/tmp/raz-sys-dir/h", {writing, creating}, 1C0H); (* rwx------ *) close(fd); diff --git a/lib/Sys.raz b/lib/Sys.raz index b37506f..b658221 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -62,6 +62,7 @@ Info* = record size*: I64; (* in bytes *) mtime*: I64; (* its contents' last change, in ns since 1970 *) + seconds*: I64; (* mtime in whole seconds, rounded down: exact past 2262 and before 1677, where mtime wraps *) mode*: INT; (* its permission bits, rwxrwxrwx *) dir*: BOOL; (* a directory *) file*: BOOL; (* not a directory, device, pipe or socket *) @@ -69,6 +70,7 @@ 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 *) links*: U64; (* how many names it has: 1 for a file no hard link shares *) + special*: INT; (* its set-user-id, set-group-id and sticky bits, as chmod takes them: 800H, 400H, 200H *) end; (* What a directory says a name in it is, without asking the file: @@ -332,12 +334,36 @@ return 0; end; +(* Takes the lock on the open file fd as lock does, but shared: any + number of programs may hold it so at once, as readers do, while none + holds it as lock takes it. 0, or minus an errno. *) +proc lockShared*(fd: INT; wait: BOOL): INT; +var how: I32; +begin + how := CAbi.lockSh; + if not wait then how := how | CAbi.lockNb; end; + if C.flock(I32(fd), how) # 0 then return failed(); end; + return 0; +end; + (* Closes the file descriptor fd. *) proc close*(fd: INT); begin _ := C.close(I32(fd)); end; +(* Closes the file descriptor fd, as close does, and says whether that + failed: 0, or minus an errno. A file system may report only at the + close that what was written to the file never reached it, -28 + (ENOSPC) or -122 (EDQUOT) say, as NFS does; a program that must know + its writes landed asks so. The fd is closed either way, so it is never + closed again, after -4 (EINTR) too. *) +proc closeChecked*(fd: INT): INT; +begin + if C.close(I32(fd)) # 0 then return failed(); end; + return 0; +end; + (* A new file descriptor for the file fd is open on, the lowest free: it, or minus an errno. *) proc dup*(fd: INT): INT; @@ -453,6 +479,17 @@ return 0; end; +(* Makes a directory at path with the permission bits mode, less those + the umask takes away: 1C0H (700) for one only its owner may enter. Of + the bits above them Linux keeps only sticky, 200H. *) +proc makeDirMode*(path: []U8; mode: INT): INT; +var a: [4096]U8; +begin + if not cstr(path, a) then return -36; end; + if C.mkdir(ADR(a[0]), U32(mode)) # 0 then return failed(); end; + return 0; +end; + (* Removes the directory at path, which must be empty. *) proc removeDir*(path: []U8): INT; var a: [4096]U8; @@ -471,6 +508,22 @@ return 0; end; +(* Sets when the contents of what path names last changed, and when it + was last read, to sec seconds and nsec nanoseconds since 1970, as a + program copying a file keeps its time: Info.seconds gives sec back, + whatever the time. *) +proc setTime*(path: []U8; sec: I64; nsec: INT): INT; +var a: [4096]U8; t: [2]Timespec; +begin + if not cstr(path, a) then return -36; end; + if (nsec < 0) or (nsec >= 1000000000) then return -22; end; (* EINVAL *) + t[0].sec := sec; + t[0].nsec := nsec; + t[1] := t[0]; + if C.utimensat(CAbi.atFdcwd, ADR(a[0]), ^U8(ADR(t)), 0) # 0 then return failed(); end; + return 0; +end; + (* 0 if this user, by its real ids, may do with path what how asks: reading, writing and executing; minus an errno, -13 (EACCES) if not. An empty how asks only whether path is there. *) @@ -532,7 +585,9 @@ i.ino := U64(field(p, CAbi.stInoAt, CAbi.stInoLen)); i.links := U64(field(p, CAbi.stNlinkAt, CAbi.stNlinkLen)); i.mtime := field(p, CAbi.stMtimeAt, CAbi.stMtimeLen) * 1000000000 + field(p, CAbi.stMtimeNsAt, CAbi.stMtimeNsLen); + i.seconds := field(p, CAbi.stMtimeAt, CAbi.stMtimeLen); i.mode := INT(field(p, CAbi.stModeAt, CAbi.stModeLen)) & 1FFH; + i.special := INT(field(p, CAbi.stModeAt, CAbi.stModeLen)) & 0E00H; i.dir := fileType(st) = CAbi.sIfdir; i.file := fileType(st) = CAbi.sIfreg; return i; @@ -2068,7 +2123,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, j: Info; d: Dir; name: []U8; ok: BOOL; +var free, total: I64; fd, n, err, w, h, l, c, a, m, k, o: 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; @@ -2149,7 +2204,23 @@ close(fd); if lock(k, true) # 0 then exit(2); end; close(k); + (* the shared lock: two open files hold it at once, lock is refused + until both have gone, and a shared one while lock holds it *) + fd := openRead("/tmp/raz-sys-dir/h"); + k := openRead("/tmp/raz-sys-dir/h"); + if (lockShared(fd, false) # 0) or (lockShared(k, true) # 0) then exit(2); end; + o := openRead("/tmp/raz-sys-dir/h"); + if lock(o, false) # -11 then exit(2); end; + close(fd); + close(k); + if lock(o, false) # 0 then exit(2); end; + fd := openRead("/tmp/raz-sys-dir/h"); + if lockShared(fd, false) # -11 then exit(2); end; + close(fd); + close(o); if (position(fd) # -9) or (truncate(fd, 0) # -9) or (sync(fd) # -9) then exit(2); end; (* EBADF *) + k := openRead("/tmp/raz-sys-dir/h"); + if (closeChecked(k) # 0) or (closeChecked(k) # -9) then exit(2); end; i, err := infoOf(fd); if err # -9 then exit(2); end; i, err := info("/tmp/raz-sys-dir/h"); @@ -2157,6 +2228,26 @@ 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; + (* setTime, after 1970 and before it, to the nanosecond *) + if setTime("/tmp/raz-sys-dir/h", 1234567890, 123456789) # 0 then exit(2); end; + i, err := info("/tmp/raz-sys-dir/h"); + if (i.mtime # 1234567890123456789) or (i.seconds # 1234567890) then exit(2); end; + if setTime("/tmp/raz-sys-dir/h", -2, 500000000) # 0 then exit(2); end; + i, err := info("/tmp/raz-sys-dir/h"); + if (i.mtime # -1500000000) or (i.seconds # -2) then exit(2); end; + if setTime("/tmp/raz-sys-dir/h", 10413792000, 0) # 0 then exit(2); end; (* 2300, past mtime's reach *) + i, err := info("/tmp/raz-sys-dir/h"); + if i.seconds # 10413792000 then exit(2); end; + if setTime("/tmp/raz-sys-dir/h", 0, 1000000000) # -22 then exit(2); end; + if setTime("/no/such/file", 0, 0) # -2 then exit(2); end; + (* the special bits: set-user-id by chmod, and sticky by makeDirMode *) + if chmod("/tmp/raz-sys-dir/h", 9EDH) # 0 then exit(2); end; + i, err := info("/tmp/raz-sys-dir/h"); + if (i.mode # 1EDH) or (i.special # 800H) then exit(2); end; + if makeDirMode("/tmp/raz-sys-dir/m", 3C0H) # 0 then exit(2); end; + i, err := info("/tmp/raz-sys-dir/m"); + if (err # 0) or not i.dir or (i.mode # 1C0H) or (i.special # 200H) then exit(2); end; + if removeDir("/tmp/raz-sys-dir/m") # 0 then exit(2); end; _ := remove("/tmp/raz-sys-dir/h"); fd := openMode("/tmp/raz-sys-dir/h", {writing, creating}, 1C0H); (* rwx------ *) close(fd); diff --git a/tools/cabi.c b/tools/cabi.c index 9423dc0..3d1ae34 100644 --- a/tools/cabi.c +++ b/tools/cabi.c @@ -114,10 +114,11 @@ K("fSetfl", F_SETFL); K("fDupfdCloexec", F_DUPFD_CLOEXEC); K("fdCloexec", FD_CLOEXEC); K("seekSet", SEEK_SET); K("seekCur", SEEK_CUR); K("seekEnd", SEEK_END); - K("lockEx", LOCK_EX); K("lockNb", LOCK_NB); + K("lockEx", LOCK_EX); K("lockSh", LOCK_SH); K("lockNb", LOCK_NB); K("protRead", PROT_READ); K("protWrite", PROT_WRITE); K("mapShared", MAP_SHARED); K("mapPrivate", MAP_PRIVATE); K("mapAnonymous", MAP_ANONYMOUS); K("mfdCloexec", MFD_CLOEXEC); + K("atFdcwd", AT_FDCWD); K("fOk", F_OK); K("rOk", R_OK); K("wOk", W_OK); K("xOk", X_OK); SAY("struct stat, statvfs and dirent");