raz / commit 9d27fd7e

commit
9d27fd7e5a140a8f5f029e9512e1aa4633723123
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
78988285
Sys: entryKind and Info.dev, for walking trees

What rwalk needs, by the owner's word. Library growth that breaks no
program; in both Sys files.

- entryKind: what the name nextEntry gave last is, from d_type, so a
  walk needn't call linkInfo for each name; unknown where the file
  system doesn't say.
- Info.dev: st_dev; with ino, a sure test that a link has led back
  into a directory already being walked.

Sys's test covers each; raz-lib.md says them.

patch · log

3 files changed, +89 -5

doc/raz-lib.md (history) modified +14 -3

@@ -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`, `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 |
14+| `Sys` | system calls: `open`, `openMode`, `openRead`, `create`, `read`, `write`, `seek`, `position`, `truncate`, `sync`, `syncAll`, `lock`, `close`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `chmod`, `symlink`, `readLink`, `info`, `linkInfo`, `infoOf`, `openDir`, `nextEntry`, `entryKind`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `termSize`, `ready`; the environment, `env`, `workDir`, `setWorkDir`; programs, pipes and signals; time, `clock`, `now`, `sleep`; `random`; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and `Sys.raz` over the C library for the others |
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 |
@@ -173,7 +173,9 @@
173173 file*: BOOL; (* a regular file: not a directory, device, pipe or socket *)
174174 symlink*: BOOL; (* the path itself is a symbolic link *)
175175 ino*: U64; (* its inode, which a rename keeps and a new file doesn't *)
176+ dev*: U64; (* the file system it is on; with ino, it names the file *)
176177 end;
178+ Kind* = (unknown, isFile, isDir, isLink, isOther); (* what a directory says a name in it is *)
177179 Dir* = record ... end; (* an open directory; its fields are the target's *)
178180 Program* = record
179181 path*: []U8;
@@ -222,6 +224,7 @@
222224
223225 proc openDir*(d: ^Dir; path: []U8): INT;
224226 proc nextEntry*(d: ^Dir): ([]U8, BOOL); (* a name, good until the next call; false at the end *)
227+proc entryKind*(d: ^Dir): Kind; (* what the name nextEntry gave last is *)
225228 proc closeDir*(d: ^Dir);
226229
227230 proc rawMode*(fd: INT): BOOL; (* the terminal on fd: no echo, no lines, no signals *)
@@ -282,7 +285,12 @@
282285 holds data: opening a pipe would wait for a program to write to it.
283286
284287 `nextEntry` gives every name in the directory except `.` and `..`, in
285-no particular order. `rawMode` turns off what the terminal does with
288+no particular order. `entryKind` says what the name it gave last is, a
289+file, a directory, a symbolic link (whatever it names) or something
290+else, as the directory itself records it, so that a program walking a
291+tree needn't ask `linkInfo` of every name, a system call each; where
292+the file system doesn't record it, it is `unknown`, and `linkInfo`
293+must be asked. `rawMode` turns off what the terminal does with
286294 keys before the program reads them (echo, line editing, signals, flow
287295 control), and NL to CR NL on output. A program that turns it on
288296 restores it before it ends: `Rt.atExit` is the place.
@@ -315,7 +323,10 @@
315323 of what it names, which need not be there. `Info.mtime` is what a program checks
316324 to see whether a file changed under it, and `Info.ino` whether it is
317325 still the same file, not one renamed over it; 0 on a target whose
318-files have none. `Info.mode` holds the nine
326+files have none. `Info.dev` is the file system the file is on: two
327+files are one only if both `dev` and `ino` agree, which is how a
328+program following links finds that one has led it back into a
329+directory it is already in. `Info.mode` holds the nine
319330 bits of `rwxrwxrwx`, and `chmod` sets them, 1EDH (755) for a program,
320331 1A4H (644) for data.
321332

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

@@ -30,11 +30,19 @@
3030 file*: BOOL; (* not a directory, device, pipe or socket *)
3131 symlink*: BOOL; (* the path itself is a symbolic link *)
3232 ino*: U64; (* its inode, which a rename keeps and a new file doesn't *)
33+ dev*: U64; (* the file system it is on; with ino, it names the file *)
3334 end;
3435
36+ (* What a directory says a name in it is, without asking the file:
37+ a regular file, a directory, a symbolic link, or something else
38+ (a device, pipe or socket); unknown where the file system
39+ doesn't say, and linkInfo must be asked. *)
40+ Kind* = (unknown, isFile, isDir, isLink, isOther);
41+
3542 (* An open directory: getdents64's records, read a block at a time. *)
3643 Dir* = record
3744 fd, pos, len: INT;
45+ kind: Kind; (* of the name nextEntry gave last *)
3846 buf: [8192]U8;
3947 end;
4048
@@ -342,6 +350,7 @@
342350 var i: Info;
343351 begin
344352 i.size := st[6]; (* st_size *)
353+ i.dev := U64(st[0]); (* st_dev *)
345354 i.ino := U64(st[1]); (* st_ino *)
346355 i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *)
347356 i.mode := st[3] & 1FFH;
@@ -403,10 +412,24 @@
403412 return st[4] * unit, st[2] * unit, 0; (* f_bavail, f_blocks *)
404413 end;
405414
415+(* The Kind of a d_type, as getdents64 and readdir give it: DT_REG is
416+ 8, DT_DIR 4, DT_LNK 10, DT_UNKNOWN 0, and the rest (pipes, devices,
417+ sockets) other things. *)
418+proc kindOf(t: U8): Kind;
419+begin
420+ if t = 8 then return isFile;
421+ elsif t = 4 then return isDir;
422+ elsif t = 10 then return isLink;
423+ elsif t = 0 then return unknown;
424+ end;
425+ return isOther;
426+end;
427+
406428 (* Opens the directory at path, for nextEntry. *)
407429 proc openDir*(d: ^Dir; path: []U8): INT;
408430 var a: [4096]U8;
409431 begin
432+ d^.kind := unknown;
410433 d^.pos := 0;
411434 d^.len := 0;
412435 if not cstr(path, a) then return -36; end;
@@ -431,6 +454,7 @@
431454 at := d^.pos + 19;
432455 n := 0;
433456 while d^.buf[at + n] # 0 do INC(n); end;
457+ d^.kind := kindOf(d^.buf[d^.pos + 18]);
434458 INC(d^.pos, INT(d^.buf[d^.pos + 16]) + INT(d^.buf[d^.pos + 17]) * 256);
435459 if (d^.buf[at..at + n] # ".") and (d^.buf[at..at + n] # "..") then
436460 return d^.buf[at..at + n], true;
@@ -439,6 +463,14 @@
439463 return "", false;
440464 end;
441465
466+(* What the name nextEntry gave last is, as the directory says, so that
467+ a walk needn't ask each file with linkInfo; unknown where the file
468+ system doesn't say. A link is isLink, whatever it names. *)
469+proc entryKind*(d: ^Dir): Kind;
470+begin
471+ return d^.kind;
472+end;
473+
442474 (* Closes the directory d that openDir opened. *)
443475 proc closeDir*(d: ^Dir);
444476 begin
@@ -1322,7 +1354,7 @@
13221354 directory made, listed and emptied; no terminal; a socket, sent to
13231355 after its end; programs; time and the network. *)
13241356 proc main*;
1325-var free, total: I64; fd, n, err, w, h, l, c, a, m, k: INT; got: [1]INT; buf: [16]U8; p, q: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL;
1357+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;
13261358 begin
13271359 fd := create("/tmp/raz-sys-test");
13281360 if fd < 0 then exit(2); end;
@@ -1420,6 +1452,10 @@
14201452 if openDir(ADR(d), "/tmp/raz-sys-dir") # 0 then exit(2); end;
14211453 name, ok := nextEntry(ADR(d));
14221454 if not ok or (name # "g") then exit(2); end;
1455+ if (entryKind(ADR(d)) # isFile) and (entryKind(ADR(d)) # unknown) then exit(2); end;
1456+ i, err := info("/tmp/raz-sys-dir/g");
1457+ j, err := info("/tmp/raz-sys-dir");
1458+ if (err # 0) or (i.dev # j.dev) or (i.ino = j.ino) then exit(2); end;
14231459 name, ok := nextEntry(ADR(d));
14241460 if ok then exit(2); end;
14251461 closeDir(ADR(d));

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

@@ -23,6 +23,7 @@
2323 sigTerm* = 15; sigChld* = 17; sigCont* = 18; sigStop* = 19;
2424 sigTstp* = 20; sigTtin* = 21; sigTtou* = 22; sigWinch* = 28;
2525 DirentName = 19; (* the offset of d_name in glibc's struct dirent *)
26+ DirentType = 18; (* and of d_type *)
2627 TIOCGWINSZ = 5413H; (* glibc's, as the two below *)
2728 TIOCSWINSZ = 5414H;
2829 TIOCSCTTY = 540EH;
@@ -43,11 +44,19 @@
4344 file*: BOOL; (* not a directory, device, pipe or socket *)
4445 symlink*: BOOL; (* the path itself is a symbolic link *)
4546 ino*: U64; (* its inode, which a rename keeps and a new file doesn't *)
47+ dev*: U64; (* the file system it is on; with ino, it names the file *)
4648 end;
4749
50+ (* What a directory says a name in it is, without asking the file:
51+ a regular file, a directory, a symbolic link, or something else
52+ (a device, pipe or socket); unknown where the file system
53+ doesn't say, and linkInfo must be asked. *)
54+ Kind* = (unknown, isFile, isDir, isLink, isOther);
55+
4856 (* An open directory: C's DIR. *)
4957 Dir* = record
5058 d: ^U8;
59+ kind: Kind; (* of the name nextEntry gave last *)
5160 end;
5261
5362 (* A program for spawn to start. *)
@@ -385,6 +394,7 @@
385394 var i: Info;
386395 begin
387396 i.size := st[6]; (* st_size *)
397+ i.dev := U64(st[0]); (* st_dev *)
388398 i.ino := U64(st[1]); (* st_ino *)
389399 i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *)
390400 i.mode := st[3] & 1FFH;
@@ -439,10 +449,24 @@
439449 return st[4] * st[1], st[2] * st[1], 0; (* f_bavail, f_blocks, in f_frsize *)
440450 end;
441451
452+(* The Kind of a d_type, as getdents64 and readdir give it: DT_REG is
453+ 8, DT_DIR 4, DT_LNK 10, DT_UNKNOWN 0, and the rest (pipes, devices,
454+ sockets) other things. *)
455+proc kindOf(t: U8): Kind;
456+begin
457+ if t = 8 then return isFile;
458+ elsif t = 4 then return isDir;
459+ elsif t = 10 then return isLink;
460+ elsif t = 0 then return unknown;
461+ end;
462+ return isOther;
463+end;
464+
442465 (* Opens the directory at path, for nextEntry. *)
443466 proc openDir*(d: ^Dir; path: []U8): INT;
444467 var a: [4096]U8;
445468 begin
469+ d^.kind := unknown;
446470 if not cstr(path, a) then return -36; end;
447471 d^.d := C.opendir(ADR(a[0]));
448472 if d^.d = nil then return failed(); end;
@@ -457,6 +481,7 @@
457481 while true do
458482 e := C.readdir(d^.d);
459483 if e = nil then return "", false; end;
484+ d^.kind := kindOf(e[DirentType]);
460485 e := e + DirentName;
461486 n := 0;
462487 while e[n] # 0 do INC(n); end;
@@ -465,6 +490,14 @@
465490 return "", false;
466491 end;
467492
493+(* What the name nextEntry gave last is, as the directory says, so that
494+ a walk needn't ask each file with linkInfo; unknown where the file
495+ system doesn't say. A link is isLink, whatever it names. *)
496+proc entryKind*(d: ^Dir): Kind;
497+begin
498+ return d^.kind;
499+end;
500+
468501 (* Closes the directory d that openDir opened. *)
469502 proc closeDir*(d: ^Dir);
470503 begin
@@ -1304,7 +1337,7 @@
13041337 and closed; a directory made, listed and emptied; no terminal; a
13051338 socket, sent to after its end; programs; time and the network. *)
13061339 proc main*;
1307-var free, total: I64; fd, n, err, w, h, l, c, a, m, k: INT; got: [1]INT; buf: [16]U8; p, q: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL;
1340+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;
13081341 begin
13091342 fd := create("/tmp/raz-sys-test");
13101343 if fd < 0 then exit(2); end;
@@ -1402,6 +1435,10 @@
14021435 if openDir(ADR(d), "/tmp/raz-sys-dir") # 0 then exit(2); end;
14031436 name, ok := nextEntry(ADR(d));
14041437 if not ok or (name # "g") then exit(2); end;
1438+ if (entryKind(ADR(d)) # isFile) and (entryKind(ADR(d)) # unknown) then exit(2); end;
1439+ i, err := info("/tmp/raz-sys-dir/g");
1440+ j, err := info("/tmp/raz-sys-dir");
1441+ if (err # 0) or (i.dev # j.dev) or (i.ino = j.ino) then exit(2); end;
14051442 name, ok := nextEntry(ADR(d));
14061443 if ok then exit(2); end;
14071444 closeDir(ADR(d));