Sys: position, truncate, sync and infoOf
| @@ -58,6 +58,8 @@ | ||
| 58 | 58 | extern proc getcwd*(buf: ^U8; n: UINT): ^U8; |
| 59 | 59 | extern proc chdir*(path: ^U8): I32; |
| 60 | 60 | extern proc stat*(path, st: ^U8): I32; |
| 61 | +extern proc fstat*(fd: I32; st: ^U8): I32; | |
| 62 | +extern proc fsync*(fd: I32): I32; | |
| 61 | 63 | extern proc lstat*(path, st: ^U8): I32; |
| 62 | 64 | extern proc statvfs*(path, st: ^U8): I32; |
| 63 | 65 | extern proc symlink*(target, path: ^U8): I32; |
| @@ -176,6 +176,27 @@ | ||
| 176 | 176 | return SYSCALL(8, fd, pos, 0); (* SEEK_SET *) |
| 177 | 177 | end; |
| 178 | 178 | |
| 179 | +(* Where in its file fd is, without moving: the next read or write's | |
| 180 | + position, or minus an errno. *) | |
| 181 | +proc position*(fd: INT): I64; | |
| 182 | +begin | |
| 183 | + return SYSCALL(8, fd, 0, 1); (* lseek, SEEK_CUR *) | |
| 184 | +end; | |
| 185 | + | |
| 186 | +(* The file fd makes size bytes long, cut or filled with zeros; its | |
| 187 | + position stays. 0, or minus an errno. *) | |
| 188 | +proc truncate*(fd: INT; size: I64): INT; | |
| 189 | +begin | |
| 190 | + return SYSCALL(77, fd, size); (* ftruncate *) | |
| 191 | +end; | |
| 192 | + | |
| 193 | +(* Returns once what was written to fd is on the disk: 0, or minus an | |
| 194 | + errno. *) | |
| 195 | +proc sync*(fd: INT): INT; | |
| 196 | +begin | |
| 197 | + return SYSCALL(74, fd); (* fsync *) | |
| 198 | +end; | |
| 199 | + | |
| 179 | 200 | (* Closes the file descriptor fd. *) |
| 180 | 201 | proc close*(fd: INT); |
| 181 | 202 | begin |
| @@ -290,26 +311,44 @@ | ||
| 290 | 311 | return linked[0..n], 0; |
| 291 | 312 | end; |
| 292 | 313 | |
| 314 | +(* An Info from a struct stat, all but symlink. *) | |
| 315 | +proc fromStat(st: [18]INT): Info; | |
| 316 | +var i: Info; | |
| 317 | +begin | |
| 318 | + i.size := st[6]; (* st_size *) | |
| 319 | + i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *) | |
| 320 | + i.mode := st[3] & 1FFH; | |
| 321 | + i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *) | |
| 322 | + i.file := st[3] & 0F000H = 8000H; (* S_IFREG *) | |
| 323 | + return i; | |
| 324 | +end; | |
| 325 | + | |
| 293 | 326 | (* The size of what path names, whether it is a directory or a regular |
| 294 | 327 | file, following a symbolic link, and whether path is one; and 0, or |
| 295 | 328 | minus an errno. *) |
| 296 | 329 | proc info*(path: []U8): (Info, INT); |
| 297 | -var a: [4096]U8; st: [18]INT; i: Info; r: INT; (* struct stat: 144 bytes *) | |
| 330 | +var a: [4096]U8; st: [18]INT; i: Info; r: INT; isLink: BOOL; (* struct stat: 144 bytes *) | |
| 298 | 331 | begin |
| 299 | 332 | if not cstr(path, a) then return i, -36; end; |
| 300 | 333 | r := SYSCALL(6, ADR(a), ADR(st)); (* lstat *) |
| 301 | 334 | if r # 0 then return i, r; end; |
| 302 | - i.symlink := st[3] & 0F000H = 0A000H; (* st_mode is S_IFLNK *) | |
| 335 | + isLink := st[3] & 0F000H = 0A000H; (* st_mode is S_IFLNK *) | |
| 303 | 336 | r := SYSCALL(4, ADR(a), ADR(st)); (* stat *) |
| 304 | 337 | if r # 0 then return i, r; end; |
| 305 | - i.size := st[6]; (* st_size *) | |
| 306 | - i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *) | |
| 307 | - i.mode := st[3] & 1FFH; | |
| 308 | - i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *) | |
| 309 | - i.file := st[3] & 0F000H = 8000H; (* S_IFREG *) | |
| 338 | + i := fromStat(st); | |
| 339 | + i.symlink := isLink; | |
| 310 | 340 | return i, 0; |
| 311 | 341 | end; |
| 312 | 342 | |
| 343 | +(* info of the file open on fd, which is never a link. *) | |
| 344 | +proc infoOf*(fd: INT): (Info, INT); | |
| 345 | +var st: [18]INT; i: Info; r: INT; | |
| 346 | +begin | |
| 347 | + r := SYSCALL(5, fd, ADR(st)); (* fstat *) | |
| 348 | + if r # 0 then return i, r; end; | |
| 349 | + return fromStat(st), 0; | |
| 350 | +end; | |
| 351 | + | |
| 313 | 352 | (* The bytes free to an ordinary user on path's file system, and the |
| 314 | 353 | bytes in all. *) |
| 315 | 354 | proc space*(path: []U8): (I64, I64, INT); |
| @@ -1298,7 +1337,15 @@ | ||
| 1298 | 1337 | if (seek(fd, -2, true) # 3) or (read(fd, buf) # 2) or (buf[0..2] # "de") then exit(2); end; |
| 1299 | 1338 | if (seek(fd, 1, false) # 1) or (write(fd, "B") # 1) or (seek(fd, 0, false) # 0) then exit(2); end; |
| 1300 | 1339 | if (read(fd, buf) # 5) or (buf[0..5] # "aBcde") then exit(2); end; |
| 1340 | + (* where it is, its size open, cut short, on the disk *) | |
| 1341 | + if (position(fd) # 5) or (seek(fd, 2, false) # 2) or (position(fd) # 2) then exit(2); end; | |
| 1342 | + if (truncate(fd, 4) # 0) or (position(fd) # 2) or (sync(fd) # 0) then exit(2); end; | |
| 1343 | + i, err := infoOf(fd); | |
| 1344 | + if (err # 0) or (i.size # 4) or not i.file or i.symlink then exit(2); end; | |
| 1301 | 1345 | close(fd); |
| 1346 | + if (position(fd) # -9) or (truncate(fd, 0) # -9) or (sync(fd) # -9) then exit(2); end; (* EBADF *) | |
| 1347 | + i, err := infoOf(fd); | |
| 1348 | + if err # -9 then exit(2); end; | |
| 1302 | 1349 | i, err := info("/tmp/raz-sys-dir/h"); |
| 1303 | 1350 | if (err # 0) or (i.mode & 1C0H # 180H) or (i.mtime < 1700000000 * 1000000000) then exit(2); end; |
| 1304 | 1351 | if chmod("/tmp/raz-sys-dir/h", 1EDH) # 0 then exit(2); end; |
| @@ -209,6 +209,32 @@ | ||
| 209 | 209 | return r; |
| 210 | 210 | end; |
| 211 | 211 | |
| 212 | +(* Where in its file fd is, without moving: the next read or write's | |
| 213 | + position, or minus an errno. *) | |
| 214 | +proc position*(fd: INT): I64; | |
| 215 | +var r: I64; | |
| 216 | +begin | |
| 217 | + r := C.lseek(I32(fd), 0, 1); (* SEEK_CUR *) | |
| 218 | + if r < 0 then return failed(); end; | |
| 219 | + return r; | |
| 220 | +end; | |
| 221 | + | |
| 222 | +(* The file fd makes size bytes long, cut or filled with zeros; its | |
| 223 | + position stays. 0, or minus an errno. *) | |
| 224 | +proc truncate*(fd: INT; size: I64): INT; | |
| 225 | +begin | |
| 226 | + if C.ftruncate(I32(fd), size) # 0 then return failed(); end; | |
| 227 | + return 0; | |
| 228 | +end; | |
| 229 | + | |
| 230 | +(* Returns once what was written to fd is on the disk: 0, or minus an | |
| 231 | + errno. *) | |
| 232 | +proc sync*(fd: INT): INT; | |
| 233 | +begin | |
| 234 | + if C.fsync(I32(fd)) # 0 then return failed(); end; | |
| 235 | + return 0; | |
| 236 | +end; | |
| 237 | + | |
| 212 | 238 | (* Closes the file descriptor fd. *) |
| 213 | 239 | proc close*(fd: INT); |
| 214 | 240 | begin |
| @@ -324,24 +350,41 @@ | ||
| 324 | 350 | return linked[0..n], 0; |
| 325 | 351 | end; |
| 326 | 352 | |
| 353 | +(* An Info from a struct stat, all but symlink. *) | |
| 354 | +proc fromStat(st: [18]INT): Info; | |
| 355 | +var i: Info; | |
| 356 | +begin | |
| 357 | + i.size := st[6]; (* st_size *) | |
| 358 | + i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *) | |
| 359 | + i.mode := st[3] & 1FFH; | |
| 360 | + i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *) | |
| 361 | + i.file := st[3] & 0F000H = 8000H; (* S_IFREG *) | |
| 362 | + return i; | |
| 363 | +end; | |
| 364 | + | |
| 327 | 365 | (* The size of what path names, whether it is a directory or a regular |
| 328 | 366 | file, following a symbolic link, and whether path is one; and 0, or |
| 329 | 367 | minus an errno. *) |
| 330 | 368 | proc info*(path: []U8): (Info, INT); |
| 331 | -var a: [4096]U8; st: [18]INT; i: Info; (* glibc's struct stat: 144 bytes *) | |
| 369 | +var a: [4096]U8; st: [18]INT; i: Info; isLink: BOOL; (* glibc's struct stat: 144 bytes *) | |
| 332 | 370 | begin |
| 333 | 371 | if not cstr(path, a) then return i, -36; end; |
| 334 | 372 | if C.lstat(ADR(a[0]), ^U8(ADR(st))) # 0 then return i, failed(); end; |
| 335 | - i.symlink := st[3] & 0F000H = 0A000H; (* st_mode is S_IFLNK *) | |
| 373 | + isLink := st[3] & 0F000H = 0A000H; (* st_mode is S_IFLNK *) | |
| 336 | 374 | if C.stat(ADR(a[0]), ^U8(ADR(st))) # 0 then return i, failed(); end; |
| 337 | - i.size := st[6]; (* st_size *) | |
| 338 | - i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *) | |
| 339 | - i.mode := st[3] & 1FFH; | |
| 340 | - i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *) | |
| 341 | - i.file := st[3] & 0F000H = 8000H; (* S_IFREG *) | |
| 375 | + i := fromStat(st); | |
| 376 | + i.symlink := isLink; | |
| 342 | 377 | return i, 0; |
| 343 | 378 | end; |
| 344 | 379 | |
| 380 | +(* info of the file open on fd, which is never a link. *) | |
| 381 | +proc infoOf*(fd: INT): (Info, INT); | |
| 382 | +var st: [18]INT; i: Info; | |
| 383 | +begin | |
| 384 | + if C.fstat(I32(fd), ^U8(ADR(st))) # 0 then return i, failed(); end; | |
| 385 | + return fromStat(st), 0; | |
| 386 | +end; | |
| 387 | + | |
| 345 | 388 | (* The bytes free to an ordinary user on path's file system, and the |
| 346 | 389 | bytes in all. *) |
| 347 | 390 | proc space*(path: []U8): (I64, I64, INT); |
| @@ -1273,7 +1316,15 @@ | ||
| 1273 | 1316 | if (seek(fd, -2, true) # 3) or (read(fd, buf) # 2) or (buf[0..2] # "de") then exit(2); end; |
| 1274 | 1317 | if (seek(fd, 1, false) # 1) or (write(fd, "B") # 1) or (seek(fd, 0, false) # 0) then exit(2); end; |
| 1275 | 1318 | if (read(fd, buf) # 5) or (buf[0..5] # "aBcde") then exit(2); end; |
| 1319 | + (* where it is, its size open, cut short, on the disk *) | |
| 1320 | + if (position(fd) # 5) or (seek(fd, 2, false) # 2) or (position(fd) # 2) then exit(2); end; | |
| 1321 | + if (truncate(fd, 4) # 0) or (position(fd) # 2) or (sync(fd) # 0) then exit(2); end; | |
| 1322 | + i, err := infoOf(fd); | |
| 1323 | + if (err # 0) or (i.size # 4) or not i.file or i.symlink then exit(2); end; | |
| 1276 | 1324 | close(fd); |
| 1325 | + if (position(fd) # -9) or (truncate(fd, 0) # -9) or (sync(fd) # -9) then exit(2); end; (* EBADF *) | |
| 1326 | + i, err := infoOf(fd); | |
| 1327 | + if err # -9 then exit(2); end; | |
| 1277 | 1328 | i, err := info("/tmp/raz-sys-dir/h"); |
| 1278 | 1329 | if (err # 0) or (i.mode & 1C0H # 180H) or (i.mtime < 1700000000 * 1000000000) then exit(2); end; |
| 1279 | 1330 | if chmod("/tmp/raz-sys-dir/h", 1EDH) # 0 then exit(2); end; |
| @@ -29,7 +29,7 @@ | ||
| 29 | 29 | import Sys, Out, Version, Files, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Parse, Link, Elf; |
| 30 | 30 | |
| 31 | 31 | const |
| 32 | - Report = "2026-10.25"; (* the version of doc/raz.md it follows *) | |
| 32 | + Report = "2026-10.26"; (* the version of doc/raz.md it follows *) | |
| 33 | 33 | MemfdCreate = 319; (* the system call *) |
| 34 | 34 | MfdCloexec = 1; (* an ELF still executes: the kernel opens it first *) |
| 35 | 35 | Execve = 59; |