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.
| @@ -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`, `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 | | |
| 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 | |
| @@ -173,7 +173,9 @@ | ||
| 173 | 173 | file*: BOOL; (* a regular file: not a directory, device, pipe or socket *) |
| 174 | 174 | symlink*: BOOL; (* the path itself is a symbolic link *) |
| 175 | 175 | 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 *) | |
| 176 | 177 | end; |
| 178 | + Kind* = (unknown, isFile, isDir, isLink, isOther); (* what a directory says a name in it is *) | |
| 177 | 179 | Dir* = record ... end; (* an open directory; its fields are the target's *) |
| 178 | 180 | Program* = record |
| 179 | 181 | path*: []U8; |
| @@ -222,6 +224,7 @@ | ||
| 222 | 224 | |
| 223 | 225 | proc openDir*(d: ^Dir; path: []U8): INT; |
| 224 | 226 | 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 *) | |
| 225 | 228 | proc closeDir*(d: ^Dir); |
| 226 | 229 | |
| 227 | 230 | proc rawMode*(fd: INT): BOOL; (* the terminal on fd: no echo, no lines, no signals *) |
| @@ -282,7 +285,12 @@ | ||
| 282 | 285 | holds data: opening a pipe would wait for a program to write to it. |
| 283 | 286 | |
| 284 | 287 | `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 | |
| 286 | 294 | keys before the program reads them (echo, line editing, signals, flow |
| 287 | 295 | control), and NL to CR NL on output. A program that turns it on |
| 288 | 296 | restores it before it ends: `Rt.atExit` is the place. |
| @@ -315,7 +323,10 @@ | ||
| 315 | 323 | of what it names, which need not be there. `Info.mtime` is what a program checks |
| 316 | 324 | to see whether a file changed under it, and `Info.ino` whether it is |
| 317 | 325 | 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 | |
| 319 | 330 | bits of `rwxrwxrwx`, and `chmod` sets them, 1EDH (755) for a program, |
| 320 | 331 | 1A4H (644) for data. |
| 321 | 332 |
| @@ -30,11 +30,19 @@ | ||
| 30 | 30 | file*: BOOL; (* not a directory, device, pipe or socket *) |
| 31 | 31 | symlink*: BOOL; (* the path itself is a symbolic link *) |
| 32 | 32 | 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 *) | |
| 33 | 34 | end; |
| 34 | 35 | |
| 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 | + | |
| 35 | 42 | (* An open directory: getdents64's records, read a block at a time. *) |
| 36 | 43 | Dir* = record |
| 37 | 44 | fd, pos, len: INT; |
| 45 | + kind: Kind; (* of the name nextEntry gave last *) | |
| 38 | 46 | buf: [8192]U8; |
| 39 | 47 | end; |
| 40 | 48 | |
| @@ -342,6 +350,7 @@ | ||
| 342 | 350 | var i: Info; |
| 343 | 351 | begin |
| 344 | 352 | i.size := st[6]; (* st_size *) |
| 353 | + i.dev := U64(st[0]); (* st_dev *) | |
| 345 | 354 | i.ino := U64(st[1]); (* st_ino *) |
| 346 | 355 | i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *) |
| 347 | 356 | i.mode := st[3] & 1FFH; |
| @@ -403,10 +412,24 @@ | ||
| 403 | 412 | return st[4] * unit, st[2] * unit, 0; (* f_bavail, f_blocks *) |
| 404 | 413 | end; |
| 405 | 414 | |
| 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 | + | |
| 406 | 428 | (* Opens the directory at path, for nextEntry. *) |
| 407 | 429 | proc openDir*(d: ^Dir; path: []U8): INT; |
| 408 | 430 | var a: [4096]U8; |
| 409 | 431 | begin |
| 432 | + d^.kind := unknown; | |
| 410 | 433 | d^.pos := 0; |
| 411 | 434 | d^.len := 0; |
| 412 | 435 | if not cstr(path, a) then return -36; end; |
| @@ -431,6 +454,7 @@ | ||
| 431 | 454 | at := d^.pos + 19; |
| 432 | 455 | n := 0; |
| 433 | 456 | while d^.buf[at + n] # 0 do INC(n); end; |
| 457 | + d^.kind := kindOf(d^.buf[d^.pos + 18]); | |
| 434 | 458 | INC(d^.pos, INT(d^.buf[d^.pos + 16]) + INT(d^.buf[d^.pos + 17]) * 256); |
| 435 | 459 | if (d^.buf[at..at + n] # ".") and (d^.buf[at..at + n] # "..") then |
| 436 | 460 | return d^.buf[at..at + n], true; |
| @@ -439,6 +463,14 @@ | ||
| 439 | 463 | return "", false; |
| 440 | 464 | end; |
| 441 | 465 | |
| 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 | + | |
| 442 | 474 | (* Closes the directory d that openDir opened. *) |
| 443 | 475 | proc closeDir*(d: ^Dir); |
| 444 | 476 | begin |
| @@ -1322,7 +1354,7 @@ | ||
| 1322 | 1354 | directory made, listed and emptied; no terminal; a socket, sent to |
| 1323 | 1355 | after its end; programs; time and the network. *) |
| 1324 | 1356 | 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; | |
| 1326 | 1358 | begin |
| 1327 | 1359 | fd := create("/tmp/raz-sys-test"); |
| 1328 | 1360 | if fd < 0 then exit(2); end; |
| @@ -1420,6 +1452,10 @@ | ||
| 1420 | 1452 | if openDir(ADR(d), "/tmp/raz-sys-dir") # 0 then exit(2); end; |
| 1421 | 1453 | name, ok := nextEntry(ADR(d)); |
| 1422 | 1454 | 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; | |
| 1423 | 1459 | name, ok := nextEntry(ADR(d)); |
| 1424 | 1460 | if ok then exit(2); end; |
| 1425 | 1461 | closeDir(ADR(d)); |
| @@ -23,6 +23,7 @@ | ||
| 23 | 23 | sigTerm* = 15; sigChld* = 17; sigCont* = 18; sigStop* = 19; |
| 24 | 24 | sigTstp* = 20; sigTtin* = 21; sigTtou* = 22; sigWinch* = 28; |
| 25 | 25 | DirentName = 19; (* the offset of d_name in glibc's struct dirent *) |
| 26 | + DirentType = 18; (* and of d_type *) | |
| 26 | 27 | TIOCGWINSZ = 5413H; (* glibc's, as the two below *) |
| 27 | 28 | TIOCSWINSZ = 5414H; |
| 28 | 29 | TIOCSCTTY = 540EH; |
| @@ -43,11 +44,19 @@ | ||
| 43 | 44 | file*: BOOL; (* not a directory, device, pipe or socket *) |
| 44 | 45 | symlink*: BOOL; (* the path itself is a symbolic link *) |
| 45 | 46 | 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 *) | |
| 46 | 48 | end; |
| 47 | 49 | |
| 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 | + | |
| 48 | 56 | (* An open directory: C's DIR. *) |
| 49 | 57 | Dir* = record |
| 50 | 58 | d: ^U8; |
| 59 | + kind: Kind; (* of the name nextEntry gave last *) | |
| 51 | 60 | end; |
| 52 | 61 | |
| 53 | 62 | (* A program for spawn to start. *) |
| @@ -385,6 +394,7 @@ | ||
| 385 | 394 | var i: Info; |
| 386 | 395 | begin |
| 387 | 396 | i.size := st[6]; (* st_size *) |
| 397 | + i.dev := U64(st[0]); (* st_dev *) | |
| 388 | 398 | i.ino := U64(st[1]); (* st_ino *) |
| 389 | 399 | i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *) |
| 390 | 400 | i.mode := st[3] & 1FFH; |
| @@ -439,10 +449,24 @@ | ||
| 439 | 449 | return st[4] * st[1], st[2] * st[1], 0; (* f_bavail, f_blocks, in f_frsize *) |
| 440 | 450 | end; |
| 441 | 451 | |
| 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 | + | |
| 442 | 465 | (* Opens the directory at path, for nextEntry. *) |
| 443 | 466 | proc openDir*(d: ^Dir; path: []U8): INT; |
| 444 | 467 | var a: [4096]U8; |
| 445 | 468 | begin |
| 469 | + d^.kind := unknown; | |
| 446 | 470 | if not cstr(path, a) then return -36; end; |
| 447 | 471 | d^.d := C.opendir(ADR(a[0])); |
| 448 | 472 | if d^.d = nil then return failed(); end; |
| @@ -457,6 +481,7 @@ | ||
| 457 | 481 | while true do |
| 458 | 482 | e := C.readdir(d^.d); |
| 459 | 483 | if e = nil then return "", false; end; |
| 484 | + d^.kind := kindOf(e[DirentType]); | |
| 460 | 485 | e := e + DirentName; |
| 461 | 486 | n := 0; |
| 462 | 487 | while e[n] # 0 do INC(n); end; |
| @@ -465,6 +490,14 @@ | ||
| 465 | 490 | return "", false; |
| 466 | 491 | end; |
| 467 | 492 | |
| 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 | + | |
| 468 | 501 | (* Closes the directory d that openDir opened. *) |
| 469 | 502 | proc closeDir*(d: ^Dir); |
| 470 | 503 | begin |
| @@ -1304,7 +1337,7 @@ | ||
| 1304 | 1337 | and closed; a directory made, listed and emptied; no terminal; a |
| 1305 | 1338 | socket, sent to after its end; programs; time and the network. *) |
| 1306 | 1339 | 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; | |
| 1308 | 1341 | begin |
| 1309 | 1342 | fd := create("/tmp/raz-sys-test"); |
| 1310 | 1343 | if fd < 0 then exit(2); end; |
| @@ -1402,6 +1435,10 @@ | ||
| 1402 | 1435 | if openDir(ADR(d), "/tmp/raz-sys-dir") # 0 then exit(2); end; |
| 1403 | 1436 | name, ok := nextEntry(ADR(d)); |
| 1404 | 1437 | 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; | |
| 1405 | 1442 | name, ok := nextEntry(ADR(d)); |
| 1406 | 1443 | if ok then exit(2); end; |
| 1407 | 1444 | closeDir(ADR(d)); |