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.
| @@ -11,7 +11,7 @@ | ||
| 11 | 11 | | Module | What | |
| 12 | 12 | |---|---| |
| 13 | 13 | | `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 | |
| 14 | -| `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` | | |
| 14 | +| `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` | | |
| 15 | 15 | | `Out` | writing text and numbers to standard output, buffered; safe to call from several threads | |
| 16 | 16 | | `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C | |
| 17 | 17 | | `Lock` | a lock for short sections, for C's threads and `Thread`'s alike | |
| @@ -203,6 +203,7 @@ | ||
| 203 | 203 | Info* = record |
| 204 | 204 | size*: I64; (* in bytes *) |
| 205 | 205 | mtime*: I64; (* when its contents last changed, in nanoseconds since 1970 *) |
| 206 | + seconds*: I64; (* mtime in whole seconds, rounded down: exact past 2262 and before 1677, where mtime wraps *) | |
| 206 | 207 | mode*: INT; (* its permission bits, as chmod takes them: 1EDH is rwxr-xr-x *) |
| 207 | 208 | dir*: BOOL; (* a directory *) |
| 208 | 209 | file*: BOOL; (* a regular file: not a directory, device, pipe or socket *) |
| @@ -210,6 +211,7 @@ | ||
| 210 | 211 | ino*: U64; (* its inode, which a rename keeps and a new file doesn't *) |
| 211 | 212 | dev*: U64; (* the file system it is on; with ino, it names the file *) |
| 212 | 213 | links*: U64; (* how many names it has: 1 for a file no hard link shares *) |
| 214 | + special*: INT; (* its set-user-id, set-group-id and sticky bits, as chmod takes them: 800H, 400H, 200H *) | |
| 213 | 215 | end; |
| 214 | 216 | Kind* = (unknown, isFile, isDir, isLink, isOther); (* what a directory says a name in it is *) |
| 215 | 217 | Dir* = record ... end; (* an open directory; its fields are the target's *) |
| @@ -241,7 +243,9 @@ | ||
| 241 | 243 | proc sync*(fd: INT): INT; (* what was written to it, on the disk *) |
| 242 | 244 | proc syncAll*(fd: INT): INT; (* all that was written to its file system, on the disk *) |
| 243 | 245 | proc lock*(fd: INT; wait: BOOL): INT; (* the file's lock, held until fd is closed *) |
| 246 | +proc lockShared*(fd: INT; wait: BOOL): INT; (* lock, shared with others that ask it so *) | |
| 244 | 247 | proc close*(fd: INT); |
| 248 | +proc closeChecked*(fd: INT): INT; (* close, and 0 or minus the errno it failed with *) | |
| 245 | 249 | proc dup*(fd: INT): INT; (* a new fd for the same file, the lowest free *) |
| 246 | 250 | proc dupTo*(fd, into: INT): INT; (* into made the same file as fd, closing what it was *) |
| 247 | 251 | 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 @@ | ||
| 256 | 260 | proc remove*(path: []U8): INT; (* a file, not a directory *) |
| 257 | 261 | proc removeDir*(path: []U8): INT; (* an empty directory *) |
| 258 | 262 | proc makeDir*(path: []U8): INT; (* readable by all, writable by its owner *) |
| 263 | +proc makeDirMode*(path: []U8; mode: INT): INT; (* makeDir, given mode *) | |
| 259 | 264 | proc chmod*(path: []U8; mode: INT): INT; |
| 265 | +proc setTime*(path: []U8; sec: I64; nsec: INT): INT; (* its mtime, and the time it was read, made sec and nsec *) | |
| 260 | 266 | proc access*(path: []U8; how: Accesses): INT; (* 0 if this user may read, write or execute it *) |
| 261 | 267 | proc umask*(mask: INT): INT; (* the bits taken from every file made; the mask it was *) |
| 262 | 268 | proc symlink*(target, path: []U8): INT; (* path made a link to target *) |
| @@ -374,12 +380,21 @@ | ||
| 374 | 380 | program needs before it says a file is saved; `syncAll` returns once |
| 375 | 381 | everything written to the file system that holds the file is on the |
| 376 | 382 | disk, at once rather than a file at a time, and may flush other file |
| 377 | -systems too. `lock` takes the file's lock, which one open file holds | |
| 378 | -at a time: another that asks for it waits if `wait`, and otherwise | |
| 379 | -fails with -11 (EAGAIN). The lock goes when the file is closed or the | |
| 383 | +systems too. `lock` takes the file's lock for one open file alone: | |
| 384 | +another that asks for it waits if `wait`, and otherwise fails with -11 | |
| 385 | +(EAGAIN). The lock goes when the file is closed or the | |
| 380 | 386 | program ends, however it ends, so it never goes stale as a file made |
| 381 | -with `exclusive` does. `infoOf` is `info` of | |
| 382 | -an open file: its size is what it holds now, however it was written; | |
| 387 | +with `exclusive` does. `lockShared` takes it shared, as a program | |
| 388 | +that only reads asks it: any number may hold it so at once, and `lock` | |
| 389 | +waits, or fails, until the last has let it go, as a shared one waits | |
| 390 | +for the one that `lock` took. Both are `flock`'s, so they agree with | |
| 391 | +any other program that takes the file's lock so. `closeChecked` closes | |
| 392 | +as `close` does, and says if that failed: a file system may say only | |
| 393 | +then that what was written never reached it, a full disk or a quota | |
| 394 | +over NFS, so a program that must know its writes landed, as one that | |
| 395 | +copies a backup, asks it. The fd is closed either way, so a failed | |
| 396 | +`closeChecked`, -4 (EINTR) too, is never tried again. `infoOf` is | |
| 397 | +`info` of an open file: its size is what it holds now, however it was written; | |
| 383 | 398 | `linkInfo` is `info` of a symbolic link itself, not what it names, so |
| 384 | 399 | that `symlink` is true, `dir` and `file` false, and `size` the length |
| 385 | 400 | of what it names, which need not be there. `Info.mtime` is what a program checks |
| @@ -392,7 +407,23 @@ | ||
| 392 | 407 | has: more than 1 if a hard link shares it, so that writing to it |
| 393 | 408 | writes to a file named elsewhere too; 0 on a target that doesn't say. |
| 394 | 409 | `Info.mode` holds the nine bits of `rwxrwxrwx`, and `chmod` sets them, |
| 395 | -1EDH (755) for a program, 1A4H (644) for data. | |
| 410 | +1EDH (755) for a program, 1A4H (644) for data; `Info.special` holds | |
| 411 | +the three above them, set-user-id 800H, set-group-id 400H and sticky | |
| 412 | +200H, which `chmod` sets too, with `mode | special`, as a program that | |
| 413 | +copies a file keeps them. `makeDirMode` is `makeDir` that gives the | |
| 414 | +directory the bits `mode`, less those the umask takes away, as | |
| 415 | +`openMode` gives a file: 1C0H (700) for one only its owner may enter. | |
| 416 | +Of the three above them, Linux gives a new directory only sticky; it | |
| 417 | +takes set-group-id from the directory it is made in. `setTime` sets | |
| 418 | +the file's time, following a symbolic link, and the time it was last | |
| 419 | +read with it, as a program that copies or restores a file keeps its | |
| 420 | +time, so that a later run sees it unchanged: `sec` seconds and `nsec`, | |
| 421 | +from 0 to 999999999, nanoseconds since 1970. `Info.mtime` holds the | |
| 422 | +time in nanoseconds, which an `I64` holds from 1677 to 2262 only; | |
| 423 | +`Info.seconds` holds it in whole seconds, rounded down, whatever it | |
| 424 | +is, so a program that copies times exactly takes `Info.seconds`, and | |
| 425 | +the nanoseconds as `mtime - seconds * 1000000000`, which is right in | |
| 426 | +any year, since the wrapping cancels out (section 6.1). | |
| 396 | 427 | |
| 397 | 428 | **Programs.** `openPty` makes a pseudo-terminal, a terminal that a |
| 398 | 429 | program, a terminal emulator, plays the screen and the keyboard of: |
| @@ -216,4 +216,8 @@ | ||
| 216 | 216 | `ADR(s[0])` when `s` isn't empty, and what the slice holds when it |
| 217 | 217 | is, where `ADR(s[0])` stops the program; of an array, that of the |
| 218 | 218 | slice of all of it (sections 5.3 and 6.8). A predeclared name may be |
| 219 | - hidden (section 2), so it breaks no program. | |
| 219 | + hidden (section 2), so it breaks no program. Then `Sys` gains | |
| 220 | + `setTime`, `lockShared`, `makeDirMode` and `closeChecked`, and | |
| 221 | + `Info` gains `seconds` and `special`, for a program that copies files | |
| 222 | + whole: their times in any year, their set-user-id, set-group-id and | |
| 223 | + sticky bits, and a close that fails (volume 2). It breaks no program. |
| @@ -23,6 +23,7 @@ | ||
| 23 | 23 | extern proc mkdir*(path: ^U8; mode: U32): I32; |
| 24 | 24 | extern proc rmdir*(path: ^U8): I32; |
| 25 | 25 | extern proc chmod*(path: ^U8; mode: U32): I32; |
| 26 | +extern proc utimensat*(dir: I32; path, times: ^U8; flags: I32): I32; | |
| 26 | 27 | extern proc readlink*(path, buf: ^U8; n: UINT): INT; |
| 27 | 28 | extern proc lseek*(fd: I32; off: I64; whence: I32): I64; |
| 28 | 29 | extern proc opendir*(path: ^U8): ^U8; (* DIR * *) |
| @@ -29,6 +29,7 @@ | ||
| 29 | 29 | seekCur* = 1; |
| 30 | 30 | seekEnd* = 2; |
| 31 | 31 | lockEx* = 2; |
| 32 | + lockSh* = 1; | |
| 32 | 33 | lockNb* = 4; |
| 33 | 34 | protRead* = 1; |
| 34 | 35 | protWrite* = 2; |
| @@ -36,6 +37,7 @@ | ||
| 36 | 37 | mapPrivate* = 2; |
| 37 | 38 | mapAnonymous* = 32; |
| 38 | 39 | mfdCloexec* = 1; |
| 40 | + atFdcwd* = -100; | |
| 39 | 41 | fOk* = 0; |
| 40 | 42 | rOk* = 4; |
| 41 | 43 | wOk* = 2; |
| @@ -45,6 +45,7 @@ | ||
| 45 | 45 | Info* = record |
| 46 | 46 | size*: I64; (* in bytes *) |
| 47 | 47 | mtime*: I64; (* its contents' last change, in ns since 1970 *) |
| 48 | + seconds*: I64; (* mtime in whole seconds, rounded down: exact past 2262 and before 1677, where mtime wraps *) | |
| 48 | 49 | mode*: INT; (* its permission bits, rwxrwxrwx *) |
| 49 | 50 | dir*: BOOL; (* a directory *) |
| 50 | 51 | file*: BOOL; (* not a directory, device, pipe or socket *) |
| @@ -52,6 +53,7 @@ | ||
| 52 | 53 | ino*: U64; (* its inode, which a rename keeps and a new file doesn't *) |
| 53 | 54 | dev*: U64; (* the file system it is on; with ino, it names the file *) |
| 54 | 55 | links*: U64; (* how many names it has: 1 for a file no hard link shares *) |
| 56 | + special*: INT; (* its set-user-id, set-group-id and sticky bits, as chmod takes them: 800H, 400H, 200H *) | |
| 55 | 57 | end; |
| 56 | 58 | |
| 57 | 59 | (* What a directory says a name in it is, without asking the file: |
| @@ -272,12 +274,32 @@ | ||
| 272 | 274 | begin |
| 273 | 275 | if wait then return SYSCALL(73, fd, 2); end; (* flock, LOCK_EX *) |
| 274 | 276 | return SYSCALL(73, fd, 2 | 4); (* LOCK_EX | LOCK_NB *) |
| 277 | +end; | |
| 278 | + | |
| 279 | +(* Takes the lock on the open file fd as lock does, but shared: any | |
| 280 | + number of programs may hold it so at once, as readers do, while none | |
| 281 | + holds it as lock takes it. 0, or minus an errno. *) | |
| 282 | +proc lockShared*(fd: INT; wait: BOOL): INT; | |
| 283 | +begin | |
| 284 | + if wait then return SYSCALL(73, fd, 1); end; (* flock, LOCK_SH *) | |
| 285 | + return SYSCALL(73, fd, 1 | 4); (* LOCK_SH | LOCK_NB *) | |
| 275 | 286 | end; |
| 276 | 287 | |
| 277 | 288 | (* Closes the file descriptor fd. *) |
| 278 | 289 | proc close*(fd: INT); |
| 279 | 290 | begin |
| 280 | 291 | _ := SYSCALL(3, fd); |
| 292 | +end; | |
| 293 | + | |
| 294 | +(* Closes the file descriptor fd, as close does, and says whether that | |
| 295 | + failed: 0, or minus an errno. A file system may report only at the | |
| 296 | + close that what was written to the file never reached it, -28 | |
| 297 | + (ENOSPC) or -122 (EDQUOT) say, as NFS does; a program that must know | |
| 298 | + its writes landed asks so. The fd is closed either way, so it is never | |
| 299 | + closed again, after -4 (EINTR) too. *) | |
| 300 | +proc closeChecked*(fd: INT): INT; | |
| 301 | +begin | |
| 302 | + return SYSCALL(3, fd); | |
| 281 | 303 | end; |
| 282 | 304 | |
| 283 | 305 | (* A new file descriptor for the file fd is open on, the lowest free: |
| @@ -387,6 +409,16 @@ | ||
| 387 | 409 | return SYSCALL(83, ADR(a), 1EDH); (* mkdir, mode 755 *) |
| 388 | 410 | end; |
| 389 | 411 | |
| 412 | +(* Makes a directory at path with the permission bits mode, less those | |
| 413 | + the umask takes away: 1C0H (700) for one only its owner may enter. Of | |
| 414 | + the bits above them Linux keeps only sticky, 200H. *) | |
| 415 | +proc makeDirMode*(path: []U8; mode: INT): INT; | |
| 416 | +var a: [4096]U8; | |
| 417 | +begin | |
| 418 | + if not cstr(path, a) then return -36; end; | |
| 419 | + return SYSCALL(83, ADR(a), mode); (* mkdir *) | |
| 420 | +end; | |
| 421 | + | |
| 390 | 422 | (* Removes the directory at path, which must be empty. *) |
| 391 | 423 | proc removeDir*(path: []U8): INT; |
| 392 | 424 | var a: [4096]U8; |
| @@ -403,6 +435,21 @@ | ||
| 403 | 435 | return SYSCALL(90, ADR(a), mode); |
| 404 | 436 | end; |
| 405 | 437 | |
| 438 | +(* Sets when the contents of what path names last changed, and when it | |
| 439 | + was last read, to sec seconds and nsec nanoseconds since 1970, as a | |
| 440 | + program copying a file keeps its time: Info.seconds gives sec back, | |
| 441 | + whatever the time. *) | |
| 442 | +proc setTime*(path: []U8; sec: I64; nsec: INT): INT; | |
| 443 | +var a: [4096]U8; t: [2]Timespec; | |
| 444 | +begin | |
| 445 | + if not cstr(path, a) then return -36; end; | |
| 446 | + if (nsec < 0) or (nsec >= 1000000000) then return -22; end; (* EINVAL *) | |
| 447 | + t[0].sec := sec; | |
| 448 | + t[0].nsec := nsec; | |
| 449 | + t[1] := t[0]; | |
| 450 | + return SYSCALL(280, -100, ADR(a), ADR(t), 0); (* utimensat, from the working directory *) | |
| 451 | +end; | |
| 452 | + | |
| 406 | 453 | (* 0 if this user, by its real ids, may do with path what how asks: |
| 407 | 454 | reading, writing and executing; minus an errno, -13 (EACCES) if not. |
| 408 | 455 | An empty how asks only whether path is there. *) |
| @@ -453,7 +500,9 @@ | ||
| 453 | 500 | i.ino := U64(st[1]); (* st_ino *) |
| 454 | 501 | i.links := U64(st[2]); (* st_nlink *) |
| 455 | 502 | i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *) |
| 503 | + i.seconds := st[11]; | |
| 456 | 504 | i.mode := st[3] & 1FFH; |
| 505 | + i.special := st[3] & 0E00H; | |
| 457 | 506 | i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *) |
| 458 | 507 | i.file := st[3] & 0F000H = 8000H; (* S_IFREG *) |
| 459 | 508 | return i; |
| @@ -2143,7 +2192,7 @@ | ||
| 2143 | 2192 | directory made, listed and emptied; no terminal; a socket, sent to |
| 2144 | 2193 | after its end; programs; time and the network. *) |
| 2145 | 2194 | proc main*; |
| 2146 | -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; | |
| 2195 | +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; | |
| 2147 | 2196 | begin |
| 2148 | 2197 | fd := create("/tmp/raz-sys-test"); |
| 2149 | 2198 | if fd < 0 then exit(2); end; |
| @@ -2225,7 +2274,23 @@ | ||
| 2225 | 2274 | close(fd); |
| 2226 | 2275 | if lock(k, true) # 0 then exit(2); end; |
| 2227 | 2276 | close(k); |
| 2277 | + (* the shared lock: two open files hold it at once, lock is refused | |
| 2278 | + until both have gone, and a shared one while lock holds it *) | |
| 2279 | + fd := openRead("/tmp/raz-sys-dir/h"); | |
| 2280 | + k := openRead("/tmp/raz-sys-dir/h"); | |
| 2281 | + if (lockShared(fd, false) # 0) or (lockShared(k, true) # 0) then exit(2); end; | |
| 2282 | + o := openRead("/tmp/raz-sys-dir/h"); | |
| 2283 | + if lock(o, false) # -11 then exit(2); end; | |
| 2284 | + close(fd); | |
| 2285 | + close(k); | |
| 2286 | + if lock(o, false) # 0 then exit(2); end; | |
| 2287 | + fd := openRead("/tmp/raz-sys-dir/h"); | |
| 2288 | + if lockShared(fd, false) # -11 then exit(2); end; | |
| 2289 | + close(fd); | |
| 2290 | + close(o); | |
| 2228 | 2291 | if (position(fd) # -9) or (truncate(fd, 0) # -9) or (sync(fd) # -9) then exit(2); end; (* EBADF *) |
| 2292 | + k := openRead("/tmp/raz-sys-dir/h"); | |
| 2293 | + if (closeChecked(k) # 0) or (closeChecked(k) # -9) then exit(2); end; | |
| 2229 | 2294 | i, err := infoOf(fd); |
| 2230 | 2295 | if err # -9 then exit(2); end; |
| 2231 | 2296 | i, err := info("/tmp/raz-sys-dir/h"); |
| @@ -2233,6 +2298,26 @@ | ||
| 2233 | 2298 | if chmod("/tmp/raz-sys-dir/h", 1EDH) # 0 then exit(2); end; |
| 2234 | 2299 | i, err := info("/tmp/raz-sys-dir/h"); |
| 2235 | 2300 | if i.mode # 1EDH then exit(2); end; |
| 2301 | + (* setTime, after 1970 and before it, to the nanosecond *) | |
| 2302 | + if setTime("/tmp/raz-sys-dir/h", 1234567890, 123456789) # 0 then exit(2); end; | |
| 2303 | + i, err := info("/tmp/raz-sys-dir/h"); | |
| 2304 | + if (i.mtime # 1234567890123456789) or (i.seconds # 1234567890) then exit(2); end; | |
| 2305 | + if setTime("/tmp/raz-sys-dir/h", -2, 500000000) # 0 then exit(2); end; | |
| 2306 | + i, err := info("/tmp/raz-sys-dir/h"); | |
| 2307 | + if (i.mtime # -1500000000) or (i.seconds # -2) then exit(2); end; | |
| 2308 | + if setTime("/tmp/raz-sys-dir/h", 10413792000, 0) # 0 then exit(2); end; (* 2300, past mtime's reach *) | |
| 2309 | + i, err := info("/tmp/raz-sys-dir/h"); | |
| 2310 | + if i.seconds # 10413792000 then exit(2); end; | |
| 2311 | + if setTime("/tmp/raz-sys-dir/h", 0, 1000000000) # -22 then exit(2); end; | |
| 2312 | + if setTime("/no/such/file", 0, 0) # -2 then exit(2); end; | |
| 2313 | + (* the special bits: set-user-id by chmod, and sticky by makeDirMode *) | |
| 2314 | + if chmod("/tmp/raz-sys-dir/h", 9EDH) # 0 then exit(2); end; | |
| 2315 | + i, err := info("/tmp/raz-sys-dir/h"); | |
| 2316 | + if (i.mode # 1EDH) or (i.special # 800H) then exit(2); end; | |
| 2317 | + if makeDirMode("/tmp/raz-sys-dir/m", 3C0H) # 0 then exit(2); end; | |
| 2318 | + i, err := info("/tmp/raz-sys-dir/m"); | |
| 2319 | + if (err # 0) or not i.dir or (i.mode # 1C0H) or (i.special # 200H) then exit(2); end; | |
| 2320 | + if removeDir("/tmp/raz-sys-dir/m") # 0 then exit(2); end; | |
| 2236 | 2321 | _ := remove("/tmp/raz-sys-dir/h"); |
| 2237 | 2322 | fd := openMode("/tmp/raz-sys-dir/h", {writing, creating}, 1C0H); (* rwx------ *) |
| 2238 | 2323 | close(fd); |
| @@ -62,6 +62,7 @@ | ||
| 62 | 62 | Info* = record |
| 63 | 63 | size*: I64; (* in bytes *) |
| 64 | 64 | mtime*: I64; (* its contents' last change, in ns since 1970 *) |
| 65 | + seconds*: I64; (* mtime in whole seconds, rounded down: exact past 2262 and before 1677, where mtime wraps *) | |
| 65 | 66 | mode*: INT; (* its permission bits, rwxrwxrwx *) |
| 66 | 67 | dir*: BOOL; (* a directory *) |
| 67 | 68 | file*: BOOL; (* not a directory, device, pipe or socket *) |
| @@ -69,6 +70,7 @@ | ||
| 69 | 70 | ino*: U64; (* its inode, which a rename keeps and a new file doesn't *) |
| 70 | 71 | dev*: U64; (* the file system it is on; with ino, it names the file *) |
| 71 | 72 | links*: U64; (* how many names it has: 1 for a file no hard link shares *) |
| 73 | + special*: INT; (* its set-user-id, set-group-id and sticky bits, as chmod takes them: 800H, 400H, 200H *) | |
| 72 | 74 | end; |
| 73 | 75 | |
| 74 | 76 | (* What a directory says a name in it is, without asking the file: |
| @@ -332,12 +334,36 @@ | ||
| 332 | 334 | return 0; |
| 333 | 335 | end; |
| 334 | 336 | |
| 337 | +(* Takes the lock on the open file fd as lock does, but shared: any | |
| 338 | + number of programs may hold it so at once, as readers do, while none | |
| 339 | + holds it as lock takes it. 0, or minus an errno. *) | |
| 340 | +proc lockShared*(fd: INT; wait: BOOL): INT; | |
| 341 | +var how: I32; | |
| 342 | +begin | |
| 343 | + how := CAbi.lockSh; | |
| 344 | + if not wait then how := how | CAbi.lockNb; end; | |
| 345 | + if C.flock(I32(fd), how) # 0 then return failed(); end; | |
| 346 | + return 0; | |
| 347 | +end; | |
| 348 | + | |
| 335 | 349 | (* Closes the file descriptor fd. *) |
| 336 | 350 | proc close*(fd: INT); |
| 337 | 351 | begin |
| 338 | 352 | _ := C.close(I32(fd)); |
| 339 | 353 | end; |
| 340 | 354 | |
| 355 | +(* Closes the file descriptor fd, as close does, and says whether that | |
| 356 | + failed: 0, or minus an errno. A file system may report only at the | |
| 357 | + close that what was written to the file never reached it, -28 | |
| 358 | + (ENOSPC) or -122 (EDQUOT) say, as NFS does; a program that must know | |
| 359 | + its writes landed asks so. The fd is closed either way, so it is never | |
| 360 | + closed again, after -4 (EINTR) too. *) | |
| 361 | +proc closeChecked*(fd: INT): INT; | |
| 362 | +begin | |
| 363 | + if C.close(I32(fd)) # 0 then return failed(); end; | |
| 364 | + return 0; | |
| 365 | +end; | |
| 366 | + | |
| 341 | 367 | (* A new file descriptor for the file fd is open on, the lowest free: |
| 342 | 368 | it, or minus an errno. *) |
| 343 | 369 | proc dup*(fd: INT): INT; |
| @@ -453,6 +479,17 @@ | ||
| 453 | 479 | return 0; |
| 454 | 480 | end; |
| 455 | 481 | |
| 482 | +(* Makes a directory at path with the permission bits mode, less those | |
| 483 | + the umask takes away: 1C0H (700) for one only its owner may enter. Of | |
| 484 | + the bits above them Linux keeps only sticky, 200H. *) | |
| 485 | +proc makeDirMode*(path: []U8; mode: INT): INT; | |
| 486 | +var a: [4096]U8; | |
| 487 | +begin | |
| 488 | + if not cstr(path, a) then return -36; end; | |
| 489 | + if C.mkdir(ADR(a[0]), U32(mode)) # 0 then return failed(); end; | |
| 490 | + return 0; | |
| 491 | +end; | |
| 492 | + | |
| 456 | 493 | (* Removes the directory at path, which must be empty. *) |
| 457 | 494 | proc removeDir*(path: []U8): INT; |
| 458 | 495 | var a: [4096]U8; |
| @@ -471,6 +508,22 @@ | ||
| 471 | 508 | return 0; |
| 472 | 509 | end; |
| 473 | 510 | |
| 511 | +(* Sets when the contents of what path names last changed, and when it | |
| 512 | + was last read, to sec seconds and nsec nanoseconds since 1970, as a | |
| 513 | + program copying a file keeps its time: Info.seconds gives sec back, | |
| 514 | + whatever the time. *) | |
| 515 | +proc setTime*(path: []U8; sec: I64; nsec: INT): INT; | |
| 516 | +var a: [4096]U8; t: [2]Timespec; | |
| 517 | +begin | |
| 518 | + if not cstr(path, a) then return -36; end; | |
| 519 | + if (nsec < 0) or (nsec >= 1000000000) then return -22; end; (* EINVAL *) | |
| 520 | + t[0].sec := sec; | |
| 521 | + t[0].nsec := nsec; | |
| 522 | + t[1] := t[0]; | |
| 523 | + if C.utimensat(CAbi.atFdcwd, ADR(a[0]), ^U8(ADR(t)), 0) # 0 then return failed(); end; | |
| 524 | + return 0; | |
| 525 | +end; | |
| 526 | + | |
| 474 | 527 | (* 0 if this user, by its real ids, may do with path what how asks: |
| 475 | 528 | reading, writing and executing; minus an errno, -13 (EACCES) if not. |
| 476 | 529 | An empty how asks only whether path is there. *) |
| @@ -532,7 +585,9 @@ | ||
| 532 | 585 | i.ino := U64(field(p, CAbi.stInoAt, CAbi.stInoLen)); |
| 533 | 586 | i.links := U64(field(p, CAbi.stNlinkAt, CAbi.stNlinkLen)); |
| 534 | 587 | i.mtime := field(p, CAbi.stMtimeAt, CAbi.stMtimeLen) * 1000000000 + field(p, CAbi.stMtimeNsAt, CAbi.stMtimeNsLen); |
| 588 | + i.seconds := field(p, CAbi.stMtimeAt, CAbi.stMtimeLen); | |
| 535 | 589 | i.mode := INT(field(p, CAbi.stModeAt, CAbi.stModeLen)) & 1FFH; |
| 590 | + i.special := INT(field(p, CAbi.stModeAt, CAbi.stModeLen)) & 0E00H; | |
| 536 | 591 | i.dir := fileType(st) = CAbi.sIfdir; |
| 537 | 592 | i.file := fileType(st) = CAbi.sIfreg; |
| 538 | 593 | return i; |
| @@ -2068,7 +2123,7 @@ | ||
| 2068 | 2123 | and closed; a directory made, listed and emptied; no terminal; a |
| 2069 | 2124 | socket, sent to after its end; programs; time and the network. *) |
| 2070 | 2125 | proc main*; |
| 2071 | -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; | |
| 2126 | +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; | |
| 2072 | 2127 | begin |
| 2073 | 2128 | fd := create("/tmp/raz-sys-test"); |
| 2074 | 2129 | if fd < 0 then exit(2); end; |
| @@ -2149,7 +2204,23 @@ | ||
| 2149 | 2204 | close(fd); |
| 2150 | 2205 | if lock(k, true) # 0 then exit(2); end; |
| 2151 | 2206 | close(k); |
| 2207 | + (* the shared lock: two open files hold it at once, lock is refused | |
| 2208 | + until both have gone, and a shared one while lock holds it *) | |
| 2209 | + fd := openRead("/tmp/raz-sys-dir/h"); | |
| 2210 | + k := openRead("/tmp/raz-sys-dir/h"); | |
| 2211 | + if (lockShared(fd, false) # 0) or (lockShared(k, true) # 0) then exit(2); end; | |
| 2212 | + o := openRead("/tmp/raz-sys-dir/h"); | |
| 2213 | + if lock(o, false) # -11 then exit(2); end; | |
| 2214 | + close(fd); | |
| 2215 | + close(k); | |
| 2216 | + if lock(o, false) # 0 then exit(2); end; | |
| 2217 | + fd := openRead("/tmp/raz-sys-dir/h"); | |
| 2218 | + if lockShared(fd, false) # -11 then exit(2); end; | |
| 2219 | + close(fd); | |
| 2220 | + close(o); | |
| 2152 | 2221 | if (position(fd) # -9) or (truncate(fd, 0) # -9) or (sync(fd) # -9) then exit(2); end; (* EBADF *) |
| 2222 | + k := openRead("/tmp/raz-sys-dir/h"); | |
| 2223 | + if (closeChecked(k) # 0) or (closeChecked(k) # -9) then exit(2); end; | |
| 2153 | 2224 | i, err := infoOf(fd); |
| 2154 | 2225 | if err # -9 then exit(2); end; |
| 2155 | 2226 | i, err := info("/tmp/raz-sys-dir/h"); |
| @@ -2157,6 +2228,26 @@ | ||
| 2157 | 2228 | if chmod("/tmp/raz-sys-dir/h", 1EDH) # 0 then exit(2); end; |
| 2158 | 2229 | i, err := info("/tmp/raz-sys-dir/h"); |
| 2159 | 2230 | if i.mode # 1EDH then exit(2); end; |
| 2231 | + (* setTime, after 1970 and before it, to the nanosecond *) | |
| 2232 | + if setTime("/tmp/raz-sys-dir/h", 1234567890, 123456789) # 0 then exit(2); end; | |
| 2233 | + i, err := info("/tmp/raz-sys-dir/h"); | |
| 2234 | + if (i.mtime # 1234567890123456789) or (i.seconds # 1234567890) then exit(2); end; | |
| 2235 | + if setTime("/tmp/raz-sys-dir/h", -2, 500000000) # 0 then exit(2); end; | |
| 2236 | + i, err := info("/tmp/raz-sys-dir/h"); | |
| 2237 | + if (i.mtime # -1500000000) or (i.seconds # -2) then exit(2); end; | |
| 2238 | + if setTime("/tmp/raz-sys-dir/h", 10413792000, 0) # 0 then exit(2); end; (* 2300, past mtime's reach *) | |
| 2239 | + i, err := info("/tmp/raz-sys-dir/h"); | |
| 2240 | + if i.seconds # 10413792000 then exit(2); end; | |
| 2241 | + if setTime("/tmp/raz-sys-dir/h", 0, 1000000000) # -22 then exit(2); end; | |
| 2242 | + if setTime("/no/such/file", 0, 0) # -2 then exit(2); end; | |
| 2243 | + (* the special bits: set-user-id by chmod, and sticky by makeDirMode *) | |
| 2244 | + if chmod("/tmp/raz-sys-dir/h", 9EDH) # 0 then exit(2); end; | |
| 2245 | + i, err := info("/tmp/raz-sys-dir/h"); | |
| 2246 | + if (i.mode # 1EDH) or (i.special # 800H) then exit(2); end; | |
| 2247 | + if makeDirMode("/tmp/raz-sys-dir/m", 3C0H) # 0 then exit(2); end; | |
| 2248 | + i, err := info("/tmp/raz-sys-dir/m"); | |
| 2249 | + if (err # 0) or not i.dir or (i.mode # 1C0H) or (i.special # 200H) then exit(2); end; | |
| 2250 | + if removeDir("/tmp/raz-sys-dir/m") # 0 then exit(2); end; | |
| 2160 | 2251 | _ := remove("/tmp/raz-sys-dir/h"); |
| 2161 | 2252 | fd := openMode("/tmp/raz-sys-dir/h", {writing, creating}, 1C0H); (* rwx------ *) |
| 2162 | 2253 | close(fd); |
| @@ -114,10 +114,11 @@ | ||
| 114 | 114 | K("fSetfl", F_SETFL); K("fDupfdCloexec", F_DUPFD_CLOEXEC); |
| 115 | 115 | K("fdCloexec", FD_CLOEXEC); |
| 116 | 116 | K("seekSet", SEEK_SET); K("seekCur", SEEK_CUR); K("seekEnd", SEEK_END); |
| 117 | - K("lockEx", LOCK_EX); K("lockNb", LOCK_NB); | |
| 117 | + K("lockEx", LOCK_EX); K("lockSh", LOCK_SH); K("lockNb", LOCK_NB); | |
| 118 | 118 | K("protRead", PROT_READ); K("protWrite", PROT_WRITE); |
| 119 | 119 | K("mapShared", MAP_SHARED); K("mapPrivate", MAP_PRIVATE); |
| 120 | 120 | K("mapAnonymous", MAP_ANONYMOUS); K("mfdCloexec", MFD_CLOEXEC); |
| 121 | + K("atFdcwd", AT_FDCWD); | |
| 121 | 122 | K("fOk", F_OK); K("rOk", R_OK); K("wOk", W_OK); K("xOk", X_OK); |
| 122 | 123 | |
| 123 | 124 | SAY("struct stat, statvfs and dirent"); |