raz / commit 8ed26889

commit
8ed26889b60aa5fd2ac2b0c2ce665108c1304ce3
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
80585985
Sys: position, truncate, sync and infoOf

patch · log

4 files changed, +115 -15

lib/C.raz (history) modified +2 -0

@@ -58,6 +58,8 @@
5858 extern proc getcwd*(buf: ^U8; n: UINT): ^U8;
5959 extern proc chdir*(path: ^U8): I32;
6060 extern proc stat*(path, st: ^U8): I32;
61+extern proc fstat*(fd: I32; st: ^U8): I32;
62+extern proc fsync*(fd: I32): I32;
6163 extern proc lstat*(path, st: ^U8): I32;
6264 extern proc statvfs*(path, st: ^U8): I32;
6365 extern proc symlink*(target, path: ^U8): I32;

lib/Sys.linux-x86_64.raz (history) modified +54 -7

@@ -176,6 +176,27 @@
176176 return SYSCALL(8, fd, pos, 0); (* SEEK_SET *)
177177 end;
178178
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+
179200 (* Closes the file descriptor fd. *)
180201 proc close*(fd: INT);
181202 begin
@@ -290,26 +311,44 @@
290311 return linked[0..n], 0;
291312 end;
292313
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+
293326 (* The size of what path names, whether it is a directory or a regular
294327 file, following a symbolic link, and whether path is one; and 0, or
295328 minus an errno. *)
296329 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 *)
298331 begin
299332 if not cstr(path, a) then return i, -36; end;
300333 r := SYSCALL(6, ADR(a), ADR(st)); (* lstat *)
301334 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 *)
303336 r := SYSCALL(4, ADR(a), ADR(st)); (* stat *)
304337 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;
310340 return i, 0;
311341 end;
312342
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+
313352 (* The bytes free to an ordinary user on path's file system, and the
314353 bytes in all. *)
315354 proc space*(path: []U8): (I64, I64, INT);
@@ -1298,7 +1337,15 @@
12981337 if (seek(fd, -2, true) # 3) or (read(fd, buf) # 2) or (buf[0..2] # "de") then exit(2); end;
12991338 if (seek(fd, 1, false) # 1) or (write(fd, "B") # 1) or (seek(fd, 0, false) # 0) then exit(2); end;
13001339 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;
13011345 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;
13021349 i, err := info("/tmp/raz-sys-dir/h");
13031350 if (err # 0) or (i.mode & 1C0H # 180H) or (i.mtime < 1700000000 * 1000000000) then exit(2); end;
13041351 if chmod("/tmp/raz-sys-dir/h", 1EDH) # 0 then exit(2); end;

lib/Sys.raz (history) modified +58 -7

@@ -209,6 +209,32 @@
209209 return r;
210210 end;
211211
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+
212238 (* Closes the file descriptor fd. *)
213239 proc close*(fd: INT);
214240 begin
@@ -324,24 +350,41 @@
324350 return linked[0..n], 0;
325351 end;
326352
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+
327365 (* The size of what path names, whether it is a directory or a regular
328366 file, following a symbolic link, and whether path is one; and 0, or
329367 minus an errno. *)
330368 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 *)
332370 begin
333371 if not cstr(path, a) then return i, -36; end;
334372 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 *)
336374 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;
342377 return i, 0;
343378 end;
344379
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+
345388 (* The bytes free to an ordinary user on path's file system, and the
346389 bytes in all. *)
347390 proc space*(path: []U8): (I64, I64, INT);
@@ -1273,7 +1316,15 @@
12731316 if (seek(fd, -2, true) # 3) or (read(fd, buf) # 2) or (buf[0..2] # "de") then exit(2); end;
12741317 if (seek(fd, 1, false) # 1) or (write(fd, "B") # 1) or (seek(fd, 0, false) # 0) then exit(2); end;
12751318 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;
12761324 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;
12771328 i, err := info("/tmp/raz-sys-dir/h");
12781329 if (err # 0) or (i.mode & 1C0H # 180H) or (i.mtime < 1700000000 * 1000000000) then exit(2); end;
12791330 if chmod("/tmp/raz-sys-dir/h", 1EDH) # 0 then exit(2); end;

src/Raz.raz (history) modified +1 -1

@@ -29,7 +29,7 @@
2929 import Sys, Out, Version, Files, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Parse, Link, Elf;
3030
3131 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 *)
3333 MemfdCreate = 319; (* the system call *)
3434 MfdCloexec = 1; (* an ELF still executes: the kernel opens it first *)
3535 Execve = 59;