Sys: linkInfo, Info.ino, openMode, lock, syncAll What sauce's Sos made by SYSCALL, now in both Sys files, from PROPOSALS.md. Library growth that breaks no program. - linkInfo: lstat, the link itself, dangling or not. - Info.ino: st_ino, for a cache keyed by inode. - openMode: open with a mode for a file it makes; open is it with 644. - lock: flock, LOCK_EX, LOCK_NB unless wait (-11 if held). - syncAll: syncfs; over C, POSIX sync, which flushes all. Sys's test covers each; raz-lib.md says them.
| @@ -82,77 +82,6 @@ | ||
| 82 | 82 | the budget; and, across modules, the imported module's |
| 83 | 83 | text kept to be read again, which is to be checked. |
| 84 | 84 | |
| 85 | -## What a version control system needs of `Sys` | |
| 86 | - | |
| 87 | -Met in porting sauce (`~/r/sauce`) from Free Pascal, where a module of | |
| 88 | -its own, `Sos`, makes these calls by `SYSCALL`. Each is library | |
| 89 | -growth that breaks no program, so it could come with any release, not | |
| 90 | -only a thaw; and each has a C call behind it, for `Sys.raz` over the C | |
| 91 | -library. | |
| 92 | - | |
| 93 | -**Seeing a link itself.** `info` follows a symbolic link, as it should | |
| 94 | -for most programs; but a program that tracks or copies a tree must see | |
| 95 | -the link: a dangling one is -ENOENT to `info`, and the size it gives is | |
| 96 | -the target's, not the link's. | |
| 97 | - | |
| 98 | -``` | |
| 99 | -proc linkInfo*(path: []U8): (Info, INT); (* the path itself, not what a link at its end names *) | |
| 100 | -``` | |
| 101 | - | |
| 102 | -For a link, `symlink` is true, `dir` and `file` false, and `size` the | |
| 103 | -length of its target, as `readLink` gives it. The C call is `lstat`. | |
| 104 | - | |
| 105 | -**The inode.** A program that keeps a cache of files, to know one | |
| 106 | -unchanged without reading it, keys it by inode, size and time, as git | |
| 107 | -and sauce do; a file replaced by a rename has a new inode and may keep | |
| 108 | -its size and time. `Info` gains a field: | |
| 109 | - | |
| 110 | -``` | |
| 111 | -ino*: U64; (* the file's inode, on its file system *) | |
| 112 | -``` | |
| 113 | - | |
| 114 | -A record literal names its fields, so no program changes. On a target | |
| 115 | -whose files have no inode, 0. | |
| 116 | - | |
| 117 | -**A new file's mode.** `open` with `creating` always asks for 644, so | |
| 118 | -a program can't make a file executable as it writes it, nor read-only: | |
| 119 | -it must `chmod` after, and between the two the file is there with the | |
| 120 | -wrong mode, and the umask is lost (sauce writes 666 or 777 less the | |
| 121 | -umask for the working tree, 444 for its objects). | |
| 122 | - | |
| 123 | -``` | |
| 124 | -proc openMode*(path: []U8; how: Accesses; mode: INT): INT; (* open, a file it creates given mode, less the umask *) | |
| 125 | -``` | |
| 126 | - | |
| 127 | -`open(path, how)` is `openMode(path, how, 1A4H)`. | |
| 128 | - | |
| 129 | -**A lock between programs.** Two programs that change the same files | |
| 130 | -need to take turns. A lock file made with `exclusive` works until a | |
| 131 | -program dies holding it, and then it is stale forever; the kernel's | |
| 132 | -lock on an open file goes when the program ends, however it ends. | |
| 133 | - | |
| 134 | -``` | |
| 135 | -proc lock*(fd: INT; wait: BOOL): INT; (* an exclusive lock on the open file fd, until it is closed; -11 if another program holds one and not wait *) | |
| 136 | -``` | |
| 137 | - | |
| 138 | -The C call is `flock`; it locks the open file, not a range of it, and | |
| 139 | -is enough for a program that only needs one at a time. | |
| 140 | - | |
| 141 | -**Flushing a file system.** `sync` makes one file durable. A program | |
| 142 | -that writes hundreds of files and then one that names them all (a | |
| 143 | -commit's objects, then HEAD) wants them all on the disk before the | |
| 144 | -last, without a `sync` of each, which is slow: | |
| 145 | - | |
| 146 | -``` | |
| 147 | -proc syncAll*(fd: INT): INT; (* everything written to the file system holding fd, on the disk *) | |
| 148 | -``` | |
| 149 | - | |
| 150 | -The Linux call is `syncfs`; where there is none, `sync`, which flushes | |
| 151 | -every file system and so does no less. | |
| 152 | - | |
| 153 | -Not needed: syncing a directory, so that a rename in it is durable, is | |
| 154 | -already `sync(open(dir, {reading}))`. | |
| 155 | - | |
| 156 | 85 | ## `Buf` when memory runs out |
| 157 | 86 | |
| 158 | 87 | `Buf.add` and `Buf.str` call `room`, and when it fails they drop the |
| @@ -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`, `openRead`, `create`, `read`, `write`, `seek`, `position`, `truncate`, `sync`, `close`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `chmod`, `symlink`, `readLink`, `info`, `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`, `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 | |
| @@ -171,6 +171,7 @@ | ||
| 171 | 171 | dir*: BOOL; (* a directory *) |
| 172 | 172 | file*: BOOL; (* a regular file: not a directory, device, pipe or socket *) |
| 173 | 173 | symlink*: BOOL; (* the path itself is a symbolic link *) |
| 174 | + ino*: U64; (* its inode, which a rename keeps and a new file doesn't *) | |
| 174 | 175 | end; |
| 175 | 176 | Dir* = record ... end; (* an open directory; its fields are the target's *) |
| 176 | 177 | Program* = record |
| @@ -190,12 +191,15 @@ | ||
| 190 | 191 | proc write*(fd: INT; s: []U8): INT; (* the bytes written *) |
| 191 | 192 | proc read*(fd: INT; buf: []U8): INT; (* the bytes read; 0 at the end *) |
| 192 | 193 | proc open*(path: []U8; how: Accesses): INT; (* its fd *) |
| 194 | +proc openMode*(path: []U8; how: Accesses; mode: INT): INT; (* open, a file it makes given mode *) | |
| 193 | 195 | proc openRead*(path: []U8): INT; (* open(path, {reading}) *) |
| 194 | 196 | proc create*(path: []U8): INT; (* open(path, {writing, creating, truncating}) *) |
| 195 | 197 | proc seek*(fd: INT; pos: I64; fromEnd: BOOL): I64; (* the new position *) |
| 196 | 198 | proc position*(fd: INT): I64; (* where the next read or write is *) |
| 197 | 199 | proc truncate*(fd: INT; size: I64): INT; (* the file made size bytes long *) |
| 198 | 200 | proc sync*(fd: INT): INT; (* what was written to it, on the disk *) |
| 201 | +proc syncAll*(fd: INT): INT; (* all that was written to its file system, on the disk *) | |
| 202 | +proc lock*(fd: INT; wait: BOOL): INT; (* the file's lock, held until fd is closed *) | |
| 199 | 203 | proc close*(fd: INT); |
| 200 | 204 | proc isTerminal*(fd: INT): BOOL; |
| 201 | 205 | proc mmap*(size: INT): ^U8; (* size bytes of zeroed memory, in whole pages; or nil *) |
| @@ -211,6 +215,7 @@ | ||
| 211 | 215 | proc symlink*(target, path: []U8): INT; (* path made a link to target *) |
| 212 | 216 | proc readLink*(path: []U8): ([]U8, INT); (* what the link names, good until the next call *) |
| 213 | 217 | proc info*(path: []U8): (Info, INT); (* following a symbolic link *) |
| 218 | +proc linkInfo*(path: []U8): (Info, INT); (* the link itself *) | |
| 214 | 219 | proc infoOf*(fd: INT): (Info, INT); (* the open file's *) |
| 215 | 220 | proc space*(path: []U8): (I64, I64, INT); (* the bytes free and in all on path's file system *) |
| 216 | 221 | |
| @@ -286,15 +291,30 @@ | ||
| 286 | 291 | writable by its owner; with `exclusive`, failing with -17 (EEXIST) if |
| 287 | 292 | it is there, as a program does that makes a lock or a file of its |
| 288 | 293 | own; `truncating` it to nothing; `appending`, each write going to its |
| 289 | -end, however other programs write it too. `seek` moves to `pos` from | |
| 294 | +end, however other programs write it too. `openMode` is `open` that | |
| 295 | +gives a file it makes the bits `mode`, less those the umask takes | |
| 296 | +away: 1EDH (755) for a program, 124H (444) for a file never to be | |
| 297 | +written; `open` gives 1A4H (644). `seek` moves to `pos` from | |
| 290 | 298 | the start of the file, or from its end if `fromEnd`, where a negative |
| 291 | 299 | `pos` reads back from the end; `position` says where the file is, |
| 292 | 300 | without moving. `truncate` makes the file `size` bytes long, cutting |
| 293 | 301 | it or adding zeros, and leaves the position where it was. `sync` |
| 294 | 302 | returns once what was written to the file is on the disk, as a |
| 295 | -program needs before it says a file is saved. `infoOf` is `info` of | |
| 296 | -an open file: its size is what it holds now, however it was written. `Info.mtime` is what a program checks | |
| 297 | -to see whether a file changed under it; `Info.mode` holds the nine | |
| 303 | +program needs before it says a file is saved; `syncAll` returns once | |
| 304 | +everything written to the file system that holds the file is on the | |
| 305 | +disk, at once rather than a file at a time, and may flush other file | |
| 306 | +systems too. `lock` takes the file's lock, which one open file holds | |
| 307 | +at a time: another that asks for it waits if `wait`, and otherwise | |
| 308 | +fails with -11 (EAGAIN). The lock goes when the file is closed or the | |
| 309 | +program ends, however it ends, so it never goes stale as a file made | |
| 310 | +with `exclusive` does. `infoOf` is `info` of | |
| 311 | +an open file: its size is what it holds now, however it was written; | |
| 312 | +`linkInfo` is `info` of a symbolic link itself, not what it names, so | |
| 313 | +that `symlink` is true, `dir` and `file` false, and `size` the length | |
| 314 | +of what it names, which need not be there. `Info.mtime` is what a program checks | |
| 315 | +to see whether a file changed under it, and `Info.ino` whether it is | |
| 316 | +still the same file, not one renamed over it; 0 on a target whose | |
| 317 | +files have none. `Info.mode` holds the nine | |
| 298 | 318 | bits of `rwxrwxrwx`, and `chmod` sets them, 1EDH (755) for a program, |
| 299 | 319 | 1A4H (644) for data. |
| 300 | 320 |
| @@ -60,6 +60,8 @@ | ||
| 60 | 60 | extern proc stat*(path, st: ^U8): I32; |
| 61 | 61 | extern proc fstat*(fd: I32; st: ^U8): I32; |
| 62 | 62 | extern proc fsync*(fd: I32): I32; |
| 63 | +extern proc sync*; | |
| 64 | +extern proc flock*(fd, how: I32): I32; | |
| 63 | 65 | extern proc lstat*(path, st: ^U8): I32; |
| 64 | 66 | extern proc statvfs*(path, st: ^U8): I32; |
| 65 | 67 | extern proc symlink*(target, path: ^U8): I32; |
| @@ -29,6 +29,7 @@ | ||
| 29 | 29 | dir*: BOOL; (* a directory *) |
| 30 | 30 | file*: BOOL; (* not a directory, device, pipe or socket *) |
| 31 | 31 | symlink*: BOOL; (* the path itself is a symbolic link *) |
| 32 | + ino*: U64; (* its inode, which a rename keeps and a new file doesn't *) | |
| 32 | 33 | end; |
| 33 | 34 | |
| 34 | 35 | (* An open directory: getdents64's records, read a block at a time. *) |
| @@ -147,15 +148,22 @@ | ||
| 147 | 148 | end; |
| 148 | 149 | |
| 149 | 150 | (* The file at path, opened as how asks: its file descriptor, or minus |
| 150 | - an errno. A file it creates is readable by all, writable by its | |
| 151 | - owner. *) | |
| 152 | -proc open*(path: []U8; how: Accesses): INT; | |
| 151 | + an errno. A file it creates has the permission bits mode, less | |
| 152 | + those the umask takes away. *) | |
| 153 | +proc openMode*(path: []U8; how: Accesses; mode: INT): INT; | |
| 153 | 154 | var buf: [4096]U8; |
| 154 | 155 | begin |
| 155 | 156 | if not cstr(path, buf) then return -36; end; (* ENAMETOOLONG *) |
| 156 | - return SYSCALL(2, ADR(buf), flags(how), 1A4H); (* mode 644 *) | |
| 157 | + return SYSCALL(2, ADR(buf), flags(how), mode); | |
| 157 | 158 | end; |
| 158 | 159 | |
| 160 | +(* openMode with 644: a file it creates is readable by all, writable | |
| 161 | + by its owner. *) | |
| 162 | +proc open*(path: []U8; how: Accesses): INT; | |
| 163 | +begin | |
| 164 | + return openMode(path, how, 1A4H); (* 644 *) | |
| 165 | +end; | |
| 166 | + | |
| 159 | 167 | (* The file at path, opened for reading. *) |
| 160 | 168 | proc openRead*(path: []U8): INT; |
| 161 | 169 | begin |
| @@ -197,6 +205,24 @@ | ||
| 197 | 205 | return SYSCALL(74, fd); (* fsync *) |
| 198 | 206 | end; |
| 199 | 207 | |
| 208 | +(* Returns once everything written to the file system that holds fd | |
| 209 | + is on the disk, as a sync of each file would, but at once: 0, or | |
| 210 | + minus an errno. *) | |
| 211 | +proc syncAll*(fd: INT): INT; | |
| 212 | +begin | |
| 213 | + return SYSCALL(306, fd); (* syncfs *) | |
| 214 | +end; | |
| 215 | + | |
| 216 | +(* Takes the lock on the open file fd, which only one program holds at | |
| 217 | + a time, and which goes when fd is closed or the program ends, | |
| 218 | + however it ends. If another program holds it, waits for it if wait, | |
| 219 | + and otherwise returns -11 (EAGAIN). 0, or minus an errno. *) | |
| 220 | +proc lock*(fd: INT; wait: BOOL): INT; | |
| 221 | +begin | |
| 222 | + if wait then return SYSCALL(73, fd, 2); end; (* flock, LOCK_EX *) | |
| 223 | + return SYSCALL(73, fd, 2 | 4); (* LOCK_EX | LOCK_NB *) | |
| 224 | +end; | |
| 225 | + | |
| 200 | 226 | (* Closes the file descriptor fd. *) |
| 201 | 227 | proc close*(fd: INT); |
| 202 | 228 | begin |
| @@ -316,6 +342,7 @@ | ||
| 316 | 342 | var i: Info; |
| 317 | 343 | begin |
| 318 | 344 | i.size := st[6]; (* st_size *) |
| 345 | + i.ino := U64(st[1]); (* st_ino *) | |
| 319 | 346 | i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *) |
| 320 | 347 | i.mode := st[3] & 1FFH; |
| 321 | 348 | i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *) |
| @@ -340,6 +367,20 @@ | ||
| 340 | 367 | return i, 0; |
| 341 | 368 | end; |
| 342 | 369 | |
| 370 | +(* info of path itself: if it is a symbolic link, of the link, not of | |
| 371 | + what it names, so that symlink is true, dir and file false, and the | |
| 372 | + size the length of what it names. *) | |
| 373 | +proc linkInfo*(path: []U8): (Info, INT); | |
| 374 | +var a: [4096]U8; st: [18]INT; i: Info; r: INT; | |
| 375 | +begin | |
| 376 | + if not cstr(path, a) then return i, -36; end; | |
| 377 | + r := SYSCALL(6, ADR(a), ADR(st)); (* lstat *) | |
| 378 | + if r # 0 then return i, r; end; | |
| 379 | + i := fromStat(st); | |
| 380 | + i.symlink := st[3] & 0F000H = 0A000H; (* S_IFLNK *) | |
| 381 | + return i, 0; | |
| 382 | +end; | |
| 383 | + | |
| 343 | 384 | (* info of the file open on fd, which is never a link. *) |
| 344 | 385 | proc infoOf*(fd: INT): (Info, INT); |
| 345 | 386 | var st: [18]INT; i: Info; r: INT; |
| @@ -1319,11 +1360,19 @@ | ||
| 1319 | 1360 | if symlink("/tmp/raz-sys-dir", "/tmp/raz-sys-link") # 0 then exit(2); end; |
| 1320 | 1361 | i, err := info("/tmp/raz-sys-link"); |
| 1321 | 1362 | if (err # 0) or not i.dir or not i.symlink then exit(2); end; |
| 1363 | + i, err := linkInfo("/tmp/raz-sys-link"); | |
| 1364 | + if (err # 0) or i.dir or i.file or not i.symlink or (i.size # 16) then exit(2); end; | |
| 1365 | + i, err := linkInfo("/tmp/raz-sys-dir"); | |
| 1366 | + if (err # 0) or not i.dir or i.symlink or (i.ino = 0) then exit(2); end; | |
| 1322 | 1367 | name, err := readLink("/tmp/raz-sys-link"); |
| 1323 | 1368 | if (err # 0) or (name # "/tmp/raz-sys-dir") then exit(2); end; |
| 1324 | 1369 | name, err := readLink("/tmp/raz-sys-dir"); |
| 1325 | 1370 | if err # -22 then exit(2); end; (* EINVAL: not a link *) |
| 1326 | 1371 | _ := remove("/tmp/raz-sys-link"); |
| 1372 | + if symlink("/no/such/file", "/tmp/raz-sys-link") # 0 then exit(2); end; (* dangling *) | |
| 1373 | + i, err := linkInfo("/tmp/raz-sys-link"); | |
| 1374 | + if (err # 0) or not i.symlink or (i.size # 13) then exit(2); end; | |
| 1375 | + _ := remove("/tmp/raz-sys-link"); | |
| 1327 | 1376 | (* open as asked, seek, the mode and the time *) |
| 1328 | 1377 | _ := remove("/tmp/raz-sys-dir/h"); |
| 1329 | 1378 | fd := open("/tmp/raz-sys-dir/h", {writing, creating, exclusive}); |
| @@ -1342,7 +1391,16 @@ | ||
| 1342 | 1391 | if (truncate(fd, 4) # 0) or (position(fd) # 2) or (sync(fd) # 0) then exit(2); end; |
| 1343 | 1392 | i, err := infoOf(fd); |
| 1344 | 1393 | if (err # 0) or (i.size # 4) or not i.file or i.symlink then exit(2); end; |
| 1394 | + m := INT(i.ino); | |
| 1395 | + i, err := info("/tmp/raz-sys-dir/h"); | |
| 1396 | + if (err # 0) or (INT(i.ino) # m) or (syncAll(fd) # 0) then exit(2); end; | |
| 1397 | + (* the lock: one open file holds it, another can't take it until | |
| 1398 | + the first is closed *) | |
| 1399 | + k := openRead("/tmp/raz-sys-dir/h"); | |
| 1400 | + if (lock(fd, false) # 0) or (lock(k, false) # -11) then exit(2); end; | |
| 1345 | 1401 | close(fd); |
| 1402 | + if lock(k, true) # 0 then exit(2); end; | |
| 1403 | + close(k); | |
| 1346 | 1404 | if (position(fd) # -9) or (truncate(fd, 0) # -9) or (sync(fd) # -9) then exit(2); end; (* EBADF *) |
| 1347 | 1405 | i, err := infoOf(fd); |
| 1348 | 1406 | if err # -9 then exit(2); end; |
| @@ -1351,6 +1409,11 @@ | ||
| 1351 | 1409 | if chmod("/tmp/raz-sys-dir/h", 1EDH) # 0 then exit(2); end; |
| 1352 | 1410 | i, err := info("/tmp/raz-sys-dir/h"); |
| 1353 | 1411 | if i.mode # 1EDH then exit(2); end; |
| 1412 | + _ := remove("/tmp/raz-sys-dir/h"); | |
| 1413 | + fd := openMode("/tmp/raz-sys-dir/h", {writing, creating}, 1C0H); (* rwx------ *) | |
| 1414 | + close(fd); | |
| 1415 | + i, err := info("/tmp/raz-sys-dir/h"); | |
| 1416 | + if (fd < 0) or (err # 0) or (i.mode # 1C0H) then exit(2); end; | |
| 1354 | 1417 | _ := remove("/tmp/raz-sys-dir/h"); |
| 1355 | 1418 | i, err := info("/no/such/file"); |
| 1356 | 1419 | if err >= 0 then exit(2); end; |
| @@ -42,6 +42,7 @@ | ||
| 42 | 42 | dir*: BOOL; (* a directory *) |
| 43 | 43 | file*: BOOL; (* not a directory, device, pipe or socket *) |
| 44 | 44 | symlink*: BOOL; (* the path itself is a symbolic link *) |
| 45 | + ino*: U64; (* its inode, which a rename keeps and a new file doesn't *) | |
| 45 | 46 | end; |
| 46 | 47 | |
| 47 | 48 | (* An open directory: C's DIR. *) |
| @@ -175,17 +176,24 @@ | ||
| 175 | 176 | end; |
| 176 | 177 | |
| 177 | 178 | (* The file at path, opened as how asks: its file descriptor, or minus |
| 178 | - an errno. A file it creates is readable by all, writable by its | |
| 179 | - owner. *) | |
| 180 | -proc open*(path: []U8; how: Accesses): INT; | |
| 179 | + an errno. A file it creates has the permission bits mode, less | |
| 180 | + those the umask takes away. *) | |
| 181 | +proc openMode*(path: []U8; how: Accesses; mode: INT): INT; | |
| 181 | 182 | var buf: [4096]U8; r: INT; |
| 182 | 183 | begin |
| 183 | 184 | if not cstr(path, buf) then return -36; end; |
| 184 | - r := INT(C.open(ADR(buf[0]), flags(how), 1A4H)); (* mode 644 *) | |
| 185 | + r := INT(C.open(ADR(buf[0]), flags(how), mode)); | |
| 185 | 186 | if r < 0 then return failed(); end; |
| 186 | 187 | return r; |
| 187 | 188 | end; |
| 188 | 189 | |
| 190 | +(* openMode with 644: a file it creates is readable by all, writable | |
| 191 | + by its owner. *) | |
| 192 | +proc open*(path: []U8; how: Accesses): INT; | |
| 193 | +begin | |
| 194 | + return openMode(path, how, 1A4H); (* 644 *) | |
| 195 | +end; | |
| 196 | + | |
| 189 | 197 | (* The file at path, opened for reading. *) |
| 190 | 198 | proc openRead*(path: []U8): INT; |
| 191 | 199 | begin |
| @@ -235,6 +243,28 @@ | ||
| 235 | 243 | return 0; |
| 236 | 244 | end; |
| 237 | 245 | |
| 246 | +(* Returns once everything written to the file system that holds fd | |
| 247 | + is on the disk: 0, or minus an errno. POSIX has no call for one file | |
| 248 | + system, so it is sync's, which flushes them all and does no less. *) | |
| 249 | +proc syncAll*(fd: INT): INT; | |
| 250 | +begin | |
| 251 | + C.sync; | |
| 252 | + return 0; | |
| 253 | +end; | |
| 254 | + | |
| 255 | +(* Takes the lock on the open file fd, which only one program holds at | |
| 256 | + a time, and which goes when fd is closed or the program ends, | |
| 257 | + however it ends. If another program holds it, waits for it if wait, | |
| 258 | + and otherwise returns -11 (EAGAIN). 0, or minus an errno. *) | |
| 259 | +proc lock*(fd: INT; wait: BOOL): INT; | |
| 260 | +var how: I32; | |
| 261 | +begin | |
| 262 | + how := 2; (* LOCK_EX *) | |
| 263 | + if not wait then how := how | 4; end; (* LOCK_NB *) | |
| 264 | + if C.flock(I32(fd), how) # 0 then return failed(); end; | |
| 265 | + return 0; | |
| 266 | +end; | |
| 267 | + | |
| 238 | 268 | (* Closes the file descriptor fd. *) |
| 239 | 269 | proc close*(fd: INT); |
| 240 | 270 | begin |
| @@ -355,6 +385,7 @@ | ||
| 355 | 385 | var i: Info; |
| 356 | 386 | begin |
| 357 | 387 | i.size := st[6]; (* st_size *) |
| 388 | + i.ino := U64(st[1]); (* st_ino *) | |
| 358 | 389 | i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *) |
| 359 | 390 | i.mode := st[3] & 1FFH; |
| 360 | 391 | i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *) |
| @@ -377,6 +408,19 @@ | ||
| 377 | 408 | return i, 0; |
| 378 | 409 | end; |
| 379 | 410 | |
| 411 | +(* info of path itself: if it is a symbolic link, of the link, not of | |
| 412 | + what it names, so that symlink is true, dir and file false, and the | |
| 413 | + size the length of what it names. *) | |
| 414 | +proc linkInfo*(path: []U8): (Info, INT); | |
| 415 | +var a: [4096]U8; st: [18]INT; i: Info; | |
| 416 | +begin | |
| 417 | + if not cstr(path, a) then return i, -36; end; | |
| 418 | + if C.lstat(ADR(a[0]), ^U8(ADR(st))) # 0 then return i, failed(); end; | |
| 419 | + i := fromStat(st); | |
| 420 | + i.symlink := st[3] & 0F000H = 0A000H; (* S_IFLNK *) | |
| 421 | + return i, 0; | |
| 422 | +end; | |
| 423 | + | |
| 380 | 424 | (* info of the file open on fd, which is never a link. *) |
| 381 | 425 | proc infoOf*(fd: INT): (Info, INT); |
| 382 | 426 | var st: [18]INT; i: Info; |
| @@ -1298,11 +1342,19 @@ | ||
| 1298 | 1342 | if symlink("/tmp/raz-sys-dir", "/tmp/raz-sys-link") # 0 then exit(2); end; |
| 1299 | 1343 | i, err := info("/tmp/raz-sys-link"); |
| 1300 | 1344 | if (err # 0) or not i.dir or not i.symlink then exit(2); end; |
| 1345 | + i, err := linkInfo("/tmp/raz-sys-link"); | |
| 1346 | + if (err # 0) or i.dir or i.file or not i.symlink or (i.size # 16) then exit(2); end; | |
| 1347 | + i, err := linkInfo("/tmp/raz-sys-dir"); | |
| 1348 | + if (err # 0) or not i.dir or i.symlink or (i.ino = 0) then exit(2); end; | |
| 1301 | 1349 | name, err := readLink("/tmp/raz-sys-link"); |
| 1302 | 1350 | if (err # 0) or (name # "/tmp/raz-sys-dir") then exit(2); end; |
| 1303 | 1351 | name, err := readLink("/tmp/raz-sys-dir"); |
| 1304 | 1352 | if err # -22 then exit(2); end; (* EINVAL: not a link *) |
| 1305 | 1353 | _ := remove("/tmp/raz-sys-link"); |
| 1354 | + if symlink("/no/such/file", "/tmp/raz-sys-link") # 0 then exit(2); end; (* dangling *) | |
| 1355 | + i, err := linkInfo("/tmp/raz-sys-link"); | |
| 1356 | + if (err # 0) or not i.symlink or (i.size # 13) then exit(2); end; | |
| 1357 | + _ := remove("/tmp/raz-sys-link"); | |
| 1306 | 1358 | (* open as asked, seek, the mode and the time *) |
| 1307 | 1359 | _ := remove("/tmp/raz-sys-dir/h"); |
| 1308 | 1360 | fd := open("/tmp/raz-sys-dir/h", {writing, creating, exclusive}); |
| @@ -1321,7 +1373,16 @@ | ||
| 1321 | 1373 | if (truncate(fd, 4) # 0) or (position(fd) # 2) or (sync(fd) # 0) then exit(2); end; |
| 1322 | 1374 | i, err := infoOf(fd); |
| 1323 | 1375 | if (err # 0) or (i.size # 4) or not i.file or i.symlink then exit(2); end; |
| 1376 | + m := INT(i.ino); | |
| 1377 | + i, err := info("/tmp/raz-sys-dir/h"); | |
| 1378 | + if (err # 0) or (INT(i.ino) # m) or (syncAll(fd) # 0) then exit(2); end; | |
| 1379 | + (* the lock: one open file holds it, another can't take it until | |
| 1380 | + the first is closed *) | |
| 1381 | + k := openRead("/tmp/raz-sys-dir/h"); | |
| 1382 | + if (lock(fd, false) # 0) or (lock(k, false) # -11) then exit(2); end; | |
| 1324 | 1383 | close(fd); |
| 1384 | + if lock(k, true) # 0 then exit(2); end; | |
| 1385 | + close(k); | |
| 1325 | 1386 | if (position(fd) # -9) or (truncate(fd, 0) # -9) or (sync(fd) # -9) then exit(2); end; (* EBADF *) |
| 1326 | 1387 | i, err := infoOf(fd); |
| 1327 | 1388 | if err # -9 then exit(2); end; |
| @@ -1330,6 +1391,11 @@ | ||
| 1330 | 1391 | if chmod("/tmp/raz-sys-dir/h", 1EDH) # 0 then exit(2); end; |
| 1331 | 1392 | i, err := info("/tmp/raz-sys-dir/h"); |
| 1332 | 1393 | if i.mode # 1EDH then exit(2); end; |
| 1394 | + _ := remove("/tmp/raz-sys-dir/h"); | |
| 1395 | + fd := openMode("/tmp/raz-sys-dir/h", {writing, creating}, 1C0H); (* rwx------ *) | |
| 1396 | + close(fd); | |
| 1397 | + i, err := info("/tmp/raz-sys-dir/h"); | |
| 1398 | + if (fd < 0) or (err # 0) or (i.mode # 1C0H) then exit(2); end; | |
| 1333 | 1399 | _ := remove("/tmp/raz-sys-dir/h"); |
| 1334 | 1400 | i, err := info("/no/such/file"); |
| 1335 | 1401 | if err >= 0 then exit(2); end; |