raz / commit 2ae3d534

commit
2ae3d534e686667532c31462cdc8d2e03fb1722a
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
4c98ab55
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.

patch · log

7 files changed, +226 -11

doc/raz-lib.md (history) modified +38 -7

@@ -11,7 +11,7 @@
1111 | Module | What |
1212 |---|---|
1313 | `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` |
1515 | `Out` | writing text and numbers to standard output, buffered; safe to call from several threads |
1616 | `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C |
1717 | `Lock` | a lock for short sections, for C's threads and `Thread`'s alike |
@@ -203,6 +203,7 @@
203203 Info* = record
204204 size*: I64; (* in bytes *)
205205 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 *)
206207 mode*: INT; (* its permission bits, as chmod takes them: 1EDH is rwxr-xr-x *)
207208 dir*: BOOL; (* a directory *)
208209 file*: BOOL; (* a regular file: not a directory, device, pipe or socket *)
@@ -210,6 +211,7 @@
210211 ino*: U64; (* its inode, which a rename keeps and a new file doesn't *)
211212 dev*: U64; (* the file system it is on; with ino, it names the file *)
212213 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 *)
213215 end;
214216 Kind* = (unknown, isFile, isDir, isLink, isOther); (* what a directory says a name in it is *)
215217 Dir* = record ... end; (* an open directory; its fields are the target's *)
@@ -241,7 +243,9 @@
241243 proc sync*(fd: INT): INT; (* what was written to it, on the disk *)
242244 proc syncAll*(fd: INT): INT; (* all that was written to its file system, on the disk *)
243245 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 *)
244247 proc close*(fd: INT);
248+proc closeChecked*(fd: INT): INT; (* close, and 0 or minus the errno it failed with *)
245249 proc dup*(fd: INT): INT; (* a new fd for the same file, the lowest free *)
246250 proc dupTo*(fd, into: INT): INT; (* into made the same file as fd, closing what it was *)
247251 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 @@
256260 proc remove*(path: []U8): INT; (* a file, not a directory *)
257261 proc removeDir*(path: []U8): INT; (* an empty directory *)
258262 proc makeDir*(path: []U8): INT; (* readable by all, writable by its owner *)
263+proc makeDirMode*(path: []U8; mode: INT): INT; (* makeDir, given mode *)
259264 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 *)
260266 proc access*(path: []U8; how: Accesses): INT; (* 0 if this user may read, write or execute it *)
261267 proc umask*(mask: INT): INT; (* the bits taken from every file made; the mask it was *)
262268 proc symlink*(target, path: []U8): INT; (* path made a link to target *)
@@ -374,12 +380,21 @@
374380 program needs before it says a file is saved; `syncAll` returns once
375381 everything written to the file system that holds the file is on the
376382 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
380386 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;
383398 `linkInfo` is `info` of a symbolic link itself, not what it names, so
384399 that `symlink` is true, `dir` and `file` false, and `size` the length
385400 of what it names, which need not be there. `Info.mtime` is what a program checks
@@ -392,7 +407,23 @@
392407 has: more than 1 if a hard link shares it, so that writing to it
393408 writes to a file named elsewhere too; 0 on a target that doesn't say.
394409 `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).
396427
397428 **Programs.** `openPty` makes a pseudo-terminal, a terminal that a
398429 program, a terminal emulator, plays the screen and the keyboard of:

doc/report-history.md (history) modified +5 -1

@@ -216,4 +216,8 @@
216216 `ADR(s[0])` when `s` isn't empty, and what the slice holds when it
217217 is, where `ADR(s[0])` stops the program; of an array, that of the
218218 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.

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

@@ -23,6 +23,7 @@
2323 extern proc mkdir*(path: ^U8; mode: U32): I32;
2424 extern proc rmdir*(path: ^U8): I32;
2525 extern proc chmod*(path: ^U8; mode: U32): I32;
26+extern proc utimensat*(dir: I32; path, times: ^U8; flags: I32): I32;
2627 extern proc readlink*(path, buf: ^U8; n: UINT): INT;
2728 extern proc lseek*(fd: I32; off: I64; whence: I32): I64;
2829 extern proc opendir*(path: ^U8): ^U8; (* DIR * *)

lib/CAbi.linux-x86_64.raz (history) modified +2 -0

@@ -29,6 +29,7 @@
2929 seekCur* = 1;
3030 seekEnd* = 2;
3131 lockEx* = 2;
32+ lockSh* = 1;
3233 lockNb* = 4;
3334 protRead* = 1;
3435 protWrite* = 2;
@@ -36,6 +37,7 @@
3637 mapPrivate* = 2;
3738 mapAnonymous* = 32;
3839 mfdCloexec* = 1;
40+ atFdcwd* = -100;
3941 fOk* = 0;
4042 rOk* = 4;
4143 wOk* = 2;

lib/Sys.linux-x86_64.raz (history) modified +86 -1

@@ -45,6 +45,7 @@
4545 Info* = record
4646 size*: I64; (* in bytes *)
4747 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 *)
4849 mode*: INT; (* its permission bits, rwxrwxrwx *)
4950 dir*: BOOL; (* a directory *)
5051 file*: BOOL; (* not a directory, device, pipe or socket *)
@@ -52,6 +53,7 @@
5253 ino*: U64; (* its inode, which a rename keeps and a new file doesn't *)
5354 dev*: U64; (* the file system it is on; with ino, it names the file *)
5455 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 *)
5557 end;
5658
5759 (* What a directory says a name in it is, without asking the file:
@@ -272,12 +274,32 @@
272274 begin
273275 if wait then return SYSCALL(73, fd, 2); end; (* flock, LOCK_EX *)
274276 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 *)
275286 end;
276287
277288 (* Closes the file descriptor fd. *)
278289 proc close*(fd: INT);
279290 begin
280291 _ := 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);
281303 end;
282304
283305 (* A new file descriptor for the file fd is open on, the lowest free:
@@ -387,6 +409,16 @@
387409 return SYSCALL(83, ADR(a), 1EDH); (* mkdir, mode 755 *)
388410 end;
389411
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+
390422 (* Removes the directory at path, which must be empty. *)
391423 proc removeDir*(path: []U8): INT;
392424 var a: [4096]U8;
@@ -403,6 +435,21 @@
403435 return SYSCALL(90, ADR(a), mode);
404436 end;
405437
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+
406453 (* 0 if this user, by its real ids, may do with path what how asks:
407454 reading, writing and executing; minus an errno, -13 (EACCES) if not.
408455 An empty how asks only whether path is there. *)
@@ -453,7 +500,9 @@
453500 i.ino := U64(st[1]); (* st_ino *)
454501 i.links := U64(st[2]); (* st_nlink *)
455502 i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *)
503+ i.seconds := st[11];
456504 i.mode := st[3] & 1FFH;
505+ i.special := st[3] & 0E00H;
457506 i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *)
458507 i.file := st[3] & 0F000H = 8000H; (* S_IFREG *)
459508 return i;
@@ -2143,7 +2192,7 @@
21432192 directory made, listed and emptied; no terminal; a socket, sent to
21442193 after its end; programs; time and the network. *)
21452194 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;
21472196 begin
21482197 fd := create("/tmp/raz-sys-test");
21492198 if fd < 0 then exit(2); end;
@@ -2225,7 +2274,23 @@
22252274 close(fd);
22262275 if lock(k, true) # 0 then exit(2); end;
22272276 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);
22282291 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;
22292294 i, err := infoOf(fd);
22302295 if err # -9 then exit(2); end;
22312296 i, err := info("/tmp/raz-sys-dir/h");
@@ -2233,6 +2298,26 @@
22332298 if chmod("/tmp/raz-sys-dir/h", 1EDH) # 0 then exit(2); end;
22342299 i, err := info("/tmp/raz-sys-dir/h");
22352300 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;
22362321 _ := remove("/tmp/raz-sys-dir/h");
22372322 fd := openMode("/tmp/raz-sys-dir/h", {writing, creating}, 1C0H); (* rwx------ *)
22382323 close(fd);

lib/Sys.raz (history) modified +92 -1

@@ -62,6 +62,7 @@
6262 Info* = record
6363 size*: I64; (* in bytes *)
6464 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 *)
6566 mode*: INT; (* its permission bits, rwxrwxrwx *)
6667 dir*: BOOL; (* a directory *)
6768 file*: BOOL; (* not a directory, device, pipe or socket *)
@@ -69,6 +70,7 @@
6970 ino*: U64; (* its inode, which a rename keeps and a new file doesn't *)
7071 dev*: U64; (* the file system it is on; with ino, it names the file *)
7172 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 *)
7274 end;
7375
7476 (* What a directory says a name in it is, without asking the file:
@@ -332,12 +334,36 @@
332334 return 0;
333335 end;
334336
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+
335349 (* Closes the file descriptor fd. *)
336350 proc close*(fd: INT);
337351 begin
338352 _ := C.close(I32(fd));
339353 end;
340354
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+
341367 (* A new file descriptor for the file fd is open on, the lowest free:
342368 it, or minus an errno. *)
343369 proc dup*(fd: INT): INT;
@@ -453,6 +479,17 @@
453479 return 0;
454480 end;
455481
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+
456493 (* Removes the directory at path, which must be empty. *)
457494 proc removeDir*(path: []U8): INT;
458495 var a: [4096]U8;
@@ -471,6 +508,22 @@
471508 return 0;
472509 end;
473510
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+
474527 (* 0 if this user, by its real ids, may do with path what how asks:
475528 reading, writing and executing; minus an errno, -13 (EACCES) if not.
476529 An empty how asks only whether path is there. *)
@@ -532,7 +585,9 @@
532585 i.ino := U64(field(p, CAbi.stInoAt, CAbi.stInoLen));
533586 i.links := U64(field(p, CAbi.stNlinkAt, CAbi.stNlinkLen));
534587 i.mtime := field(p, CAbi.stMtimeAt, CAbi.stMtimeLen) * 1000000000 + field(p, CAbi.stMtimeNsAt, CAbi.stMtimeNsLen);
588+ i.seconds := field(p, CAbi.stMtimeAt, CAbi.stMtimeLen);
535589 i.mode := INT(field(p, CAbi.stModeAt, CAbi.stModeLen)) & 1FFH;
590+ i.special := INT(field(p, CAbi.stModeAt, CAbi.stModeLen)) & 0E00H;
536591 i.dir := fileType(st) = CAbi.sIfdir;
537592 i.file := fileType(st) = CAbi.sIfreg;
538593 return i;
@@ -2068,7 +2123,7 @@
20682123 and closed; a directory made, listed and emptied; no terminal; a
20692124 socket, sent to after its end; programs; time and the network. *)
20702125 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;
20722127 begin
20732128 fd := create("/tmp/raz-sys-test");
20742129 if fd < 0 then exit(2); end;
@@ -2149,7 +2204,23 @@
21492204 close(fd);
21502205 if lock(k, true) # 0 then exit(2); end;
21512206 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);
21522221 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;
21532224 i, err := infoOf(fd);
21542225 if err # -9 then exit(2); end;
21552226 i, err := info("/tmp/raz-sys-dir/h");
@@ -2157,6 +2228,26 @@
21572228 if chmod("/tmp/raz-sys-dir/h", 1EDH) # 0 then exit(2); end;
21582229 i, err := info("/tmp/raz-sys-dir/h");
21592230 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;
21602251 _ := remove("/tmp/raz-sys-dir/h");
21612252 fd := openMode("/tmp/raz-sys-dir/h", {writing, creating}, 1C0H); (* rwx------ *)
21622253 close(fd);

tools/cabi.c (history) modified +2 -1

@@ -114,10 +114,11 @@
114114 K("fSetfl", F_SETFL); K("fDupfdCloexec", F_DUPFD_CLOEXEC);
115115 K("fdCloexec", FD_CLOEXEC);
116116 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);
118118 K("protRead", PROT_READ); K("protWrite", PROT_WRITE);
119119 K("mapShared", MAP_SHARED); K("mapPrivate", MAP_PRIVATE);
120120 K("mapAnonymous", MAP_ANONYMOUS); K("mfdCloexec", MFD_CLOEXEC);
121+ K("atFdcwd", AT_FDCWD);
121122 K("fOk", F_OK); K("rOk", R_OK); K("wOk", W_OK); K("xOk", X_OK);
122123
123124 SAY("struct stat, statvfs and dirent");