From 8ed26889b60aa5fd2ac2b0c2ce665108c1304ce3 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 15 Oct 2026 07:12:56 +0000 Subject: [PATCH] Sys: position, truncate, sync and infoOf --- diff --git a/lib/C.raz b/lib/C.raz index 0f1fa6f..49872e8 100644 --- a/lib/C.raz +++ b/lib/C.raz @@ -58,6 +58,8 @@ extern proc getcwd*(buf: ^U8; n: UINT): ^U8; extern proc chdir*(path: ^U8): I32; extern proc stat*(path, st: ^U8): I32; +extern proc fstat*(fd: I32; st: ^U8): I32; +extern proc fsync*(fd: I32): I32; extern proc lstat*(path, st: ^U8): I32; extern proc statvfs*(path, st: ^U8): I32; extern proc symlink*(target, path: ^U8): I32; diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index 8b11488..0a44384 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -176,6 +176,27 @@ return SYSCALL(8, fd, pos, 0); (* SEEK_SET *) end; +(* Where in its file fd is, without moving: the next read or write's + position, or minus an errno. *) +proc position*(fd: INT): I64; +begin + return SYSCALL(8, fd, 0, 1); (* lseek, SEEK_CUR *) +end; + +(* The file fd makes size bytes long, cut or filled with zeros; its + position stays. 0, or minus an errno. *) +proc truncate*(fd: INT; size: I64): INT; +begin + return SYSCALL(77, fd, size); (* ftruncate *) +end; + +(* Returns once what was written to fd is on the disk: 0, or minus an + errno. *) +proc sync*(fd: INT): INT; +begin + return SYSCALL(74, fd); (* fsync *) +end; + (* Closes the file descriptor fd. *) proc close*(fd: INT); begin @@ -290,26 +311,44 @@ return linked[0..n], 0; end; +(* An Info from a struct stat, all but symlink. *) +proc fromStat(st: [18]INT): Info; +var i: Info; +begin + i.size := st[6]; (* st_size *) + i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *) + i.mode := st[3] & 1FFH; + i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *) + i.file := st[3] & 0F000H = 8000H; (* S_IFREG *) + return i; +end; + (* The size of what path names, whether it is a directory or a regular file, following a symbolic link, and whether path is one; and 0, or minus an errno. *) proc info*(path: []U8): (Info, INT); -var a: [4096]U8; st: [18]INT; i: Info; r: INT; (* struct stat: 144 bytes *) +var a: [4096]U8; st: [18]INT; i: Info; r: INT; isLink: BOOL; (* struct stat: 144 bytes *) begin if not cstr(path, a) then return i, -36; end; r := SYSCALL(6, ADR(a), ADR(st)); (* lstat *) if r # 0 then return i, r; end; - i.symlink := st[3] & 0F000H = 0A000H; (* st_mode is S_IFLNK *) + isLink := st[3] & 0F000H = 0A000H; (* st_mode is S_IFLNK *) r := SYSCALL(4, ADR(a), ADR(st)); (* stat *) if r # 0 then return i, r; end; - i.size := st[6]; (* st_size *) - i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *) - i.mode := st[3] & 1FFH; - i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *) - i.file := st[3] & 0F000H = 8000H; (* S_IFREG *) + i := fromStat(st); + i.symlink := isLink; return i, 0; end; +(* info of the file open on fd, which is never a link. *) +proc infoOf*(fd: INT): (Info, INT); +var st: [18]INT; i: Info; r: INT; +begin + r := SYSCALL(5, fd, ADR(st)); (* fstat *) + if r # 0 then return i, r; end; + return fromStat(st), 0; +end; + (* The bytes free to an ordinary user on path's file system, and the bytes in all. *) proc space*(path: []U8): (I64, I64, INT); @@ -1298,7 +1337,15 @@ if (seek(fd, -2, true) # 3) or (read(fd, buf) # 2) or (buf[0..2] # "de") then exit(2); end; if (seek(fd, 1, false) # 1) or (write(fd, "B") # 1) or (seek(fd, 0, false) # 0) then exit(2); end; if (read(fd, buf) # 5) or (buf[0..5] # "aBcde") then exit(2); end; + (* where it is, its size open, cut short, on the disk *) + if (position(fd) # 5) or (seek(fd, 2, false) # 2) or (position(fd) # 2) then exit(2); end; + if (truncate(fd, 4) # 0) or (position(fd) # 2) or (sync(fd) # 0) then exit(2); end; + i, err := infoOf(fd); + if (err # 0) or (i.size # 4) or not i.file or i.symlink then exit(2); end; close(fd); + if (position(fd) # -9) or (truncate(fd, 0) # -9) or (sync(fd) # -9) then exit(2); end; (* EBADF *) + i, err := infoOf(fd); + if err # -9 then exit(2); end; i, err := info("/tmp/raz-sys-dir/h"); if (err # 0) or (i.mode & 1C0H # 180H) or (i.mtime < 1700000000 * 1000000000) then exit(2); end; if chmod("/tmp/raz-sys-dir/h", 1EDH) # 0 then exit(2); end; diff --git a/lib/Sys.raz b/lib/Sys.raz index 67d2dee..2fd24ba 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -209,6 +209,32 @@ return r; end; +(* Where in its file fd is, without moving: the next read or write's + position, or minus an errno. *) +proc position*(fd: INT): I64; +var r: I64; +begin + r := C.lseek(I32(fd), 0, 1); (* SEEK_CUR *) + if r < 0 then return failed(); end; + return r; +end; + +(* The file fd makes size bytes long, cut or filled with zeros; its + position stays. 0, or minus an errno. *) +proc truncate*(fd: INT; size: I64): INT; +begin + if C.ftruncate(I32(fd), size) # 0 then return failed(); end; + return 0; +end; + +(* Returns once what was written to fd is on the disk: 0, or minus an + errno. *) +proc sync*(fd: INT): INT; +begin + if C.fsync(I32(fd)) # 0 then return failed(); end; + return 0; +end; + (* Closes the file descriptor fd. *) proc close*(fd: INT); begin @@ -324,24 +350,41 @@ return linked[0..n], 0; end; +(* An Info from a struct stat, all but symlink. *) +proc fromStat(st: [18]INT): Info; +var i: Info; +begin + i.size := st[6]; (* st_size *) + i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *) + i.mode := st[3] & 1FFH; + i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *) + i.file := st[3] & 0F000H = 8000H; (* S_IFREG *) + return i; +end; + (* The size of what path names, whether it is a directory or a regular file, following a symbolic link, and whether path is one; and 0, or minus an errno. *) proc info*(path: []U8): (Info, INT); -var a: [4096]U8; st: [18]INT; i: Info; (* glibc's struct stat: 144 bytes *) +var a: [4096]U8; st: [18]INT; i: Info; isLink: BOOL; (* glibc's struct stat: 144 bytes *) begin if not cstr(path, a) then return i, -36; end; if C.lstat(ADR(a[0]), ^U8(ADR(st))) # 0 then return i, failed(); end; - i.symlink := st[3] & 0F000H = 0A000H; (* st_mode is S_IFLNK *) + isLink := st[3] & 0F000H = 0A000H; (* st_mode is S_IFLNK *) if C.stat(ADR(a[0]), ^U8(ADR(st))) # 0 then return i, failed(); end; - i.size := st[6]; (* st_size *) - i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *) - i.mode := st[3] & 1FFH; - i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *) - i.file := st[3] & 0F000H = 8000H; (* S_IFREG *) + i := fromStat(st); + i.symlink := isLink; return i, 0; end; +(* info of the file open on fd, which is never a link. *) +proc infoOf*(fd: INT): (Info, INT); +var st: [18]INT; i: Info; +begin + if C.fstat(I32(fd), ^U8(ADR(st))) # 0 then return i, failed(); end; + return fromStat(st), 0; +end; + (* The bytes free to an ordinary user on path's file system, and the bytes in all. *) proc space*(path: []U8): (I64, I64, INT); @@ -1273,7 +1316,15 @@ if (seek(fd, -2, true) # 3) or (read(fd, buf) # 2) or (buf[0..2] # "de") then exit(2); end; if (seek(fd, 1, false) # 1) or (write(fd, "B") # 1) or (seek(fd, 0, false) # 0) then exit(2); end; if (read(fd, buf) # 5) or (buf[0..5] # "aBcde") then exit(2); end; + (* where it is, its size open, cut short, on the disk *) + if (position(fd) # 5) or (seek(fd, 2, false) # 2) or (position(fd) # 2) then exit(2); end; + if (truncate(fd, 4) # 0) or (position(fd) # 2) or (sync(fd) # 0) then exit(2); end; + i, err := infoOf(fd); + if (err # 0) or (i.size # 4) or not i.file or i.symlink then exit(2); end; close(fd); + if (position(fd) # -9) or (truncate(fd, 0) # -9) or (sync(fd) # -9) then exit(2); end; (* EBADF *) + i, err := infoOf(fd); + if err # -9 then exit(2); end; i, err := info("/tmp/raz-sys-dir/h"); if (err # 0) or (i.mode & 1C0H # 180H) or (i.mtime < 1700000000 * 1000000000) then exit(2); end; if chmod("/tmp/raz-sys-dir/h", 1EDH) # 0 then exit(2); end; diff --git a/src/Raz.raz b/src/Raz.raz index 49654a1..0babe5e 100644 --- a/src/Raz.raz +++ b/src/Raz.raz @@ -29,7 +29,7 @@ import Sys, Out, Version, Files, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Parse, Link, Elf; const - Report = "2026-10.25"; (* the version of doc/raz.md it follows *) + Report = "2026-10.26"; (* the version of doc/raz.md it follows *) MemfdCreate = 319; (* the system call *) MfdCloexec = 1; (* an ELF still executes: the kernel opens it first *) Execve = 59;