authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-12-08 15:22:55-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-12-23 22:15:08-08:00
log181ac08459f8d4001c504330ee66037135e56908
tree835526ffd9676ee1a4a591494e50d73b45ddee2d
parent9f4d40b1f9bffc4137055b8a07f042ecfa398124

std.Io.Threaded: add missing fileLength implementation


1 files changed, 51 insertions(+), 4 deletions(-)

lib/std/Io/Threaded.zig+51-4
...@@ -708,6 +708,7 @@ pub fn io(t: *Threaded) Io {...@@ -708,6 +708,7 @@ pub fn io(t: *Threaded) Io {
708 .dirSetTimestampsNow = dirSetTimestampsNow,708 .dirSetTimestampsNow = dirSetTimestampsNow,
709709
710 .fileStat = fileStat,710 .fileStat = fileStat,
711 .fileLength = fileLength,
711 .fileClose = fileClose,712 .fileClose = fileClose,
712 .fileWriteStreaming = fileWriteStreaming,713 .fileWriteStreaming = fileWriteStreaming,
713 .fileWritePositional = fileWritePositional,714 .fileWritePositional = fileWritePositional,
...@@ -832,6 +833,7 @@ pub fn ioBasic(t: *Threaded) Io {...@@ -832,6 +833,7 @@ pub fn ioBasic(t: *Threaded) Io {
832 .dirSetTimestampsNow = dirSetTimestampsNow,833 .dirSetTimestampsNow = dirSetTimestampsNow,
833834
834 .fileStat = fileStat,835 .fileStat = fileStat,
836 .fileLength = fileLength,
835 .fileClose = fileClose,837 .fileClose = fileClose,
836 .fileWriteStreaming = fileWriteStreaming,838 .fileWriteStreaming = fileWriteStreaming,
837 .fileWritePositional = fileWritePositional,839 .fileWritePositional = fileWritePositional,
...@@ -1940,6 +1942,51 @@ fn dirStatPathWasi(...@@ -1940,6 +1942,51 @@ fn dirStatPathWasi(
1940 }1942 }
1941}1943}
19421944
1945fn fileLength(userdata: ?*anyopaque, file: File) File.LengthError!u64 {
1946 const t: *Threaded = @ptrCast(@alignCast(userdata));
1947
1948 if (native_os == .linux) {
1949 const current_thread = Thread.getCurrent(t);
1950 const linux = std.os.linux;
1951
1952 try current_thread.beginSyscall();
1953 while (true) {
1954 var statx = std.mem.zeroes(linux.Statx);
1955 switch (linux.errno(linux.statx(file.handle, "", linux.AT.EMPTY_PATH, linux.STATX_SIZE, &statx))) {
1956 .SUCCESS => {
1957 current_thread.endSyscall();
1958 return statx.size;
1959 },
1960 .INTR => {
1961 try current_thread.checkCancel();
1962 continue;
1963 },
1964 .CANCELED => return current_thread.endSyscallCanceled(),
1965 else => |e| {
1966 current_thread.endSyscall();
1967 switch (e) {
1968 .ACCES => |err| return errnoBug(err),
1969 .BADF => |err| return errnoBug(err), // File descriptor used after closed.
1970 .FAULT => |err| return errnoBug(err),
1971 .INVAL => |err| return errnoBug(err),
1972 .LOOP => |err| return errnoBug(err),
1973 .NAMETOOLONG => |err| return errnoBug(err),
1974 .NOENT => |err| return errnoBug(err),
1975 .NOMEM => return error.SystemResources,
1976 .NOTDIR => |err| return errnoBug(err),
1977 else => |err| return posix.unexpectedErrno(err),
1978 }
1979 },
1980 }
1981 }
1982 } else if (is_windows) {
1983 // TODO call NtQueryInformationFile and ask for only the size instead of "all"
1984 }
1985
1986 const stat = try fileStat(ioBasic(t), file);
1987 return stat.size;
1988}
1989
1943const fileStat = switch (native_os) {1990const fileStat = switch (native_os) {
1944 .linux => fileStatLinux,1991 .linux => fileStatLinux,
1945 .windows => fileStatWindows,1992 .windows => fileStatWindows,
...@@ -5479,7 +5526,7 @@ const fileReadStreaming = switch (native_os) {...@@ -5479,7 +5526,7 @@ const fileReadStreaming = switch (native_os) {
5479 else => fileReadStreamingPosix,5526 else => fileReadStreamingPosix,
5480};5527};
54815528
5482fn fileReadStreamingPosix(userdata: ?*anyopaque, file: File, data: [][]u8) File.Reader.Error!usize {5529fn fileReadStreamingPosix(userdata: ?*anyopaque, file: File, data: []const []u8) File.Reader.Error!usize {
5483 const t: *Threaded = @ptrCast(@alignCast(userdata));5530 const t: *Threaded = @ptrCast(@alignCast(userdata));
5484 const current_thread = Thread.getCurrent(t);5531 const current_thread = Thread.getCurrent(t);
54855532
...@@ -5567,7 +5614,7 @@ fn fileReadStreamingPosix(userdata: ?*anyopaque, file: File, data: [][]u8) File....@@ -5567,7 +5614,7 @@ fn fileReadStreamingPosix(userdata: ?*anyopaque, file: File, data: [][]u8) File.
5567 }5614 }
5568}5615}
55695616
5570fn fileReadStreamingWindows(userdata: ?*anyopaque, file: File, data: [][]u8) File.Reader.Error!usize {5617fn fileReadStreamingWindows(userdata: ?*anyopaque, file: File, data: []const []u8) File.Reader.Error!usize {
5571 const t: *Threaded = @ptrCast(@alignCast(userdata));5618 const t: *Threaded = @ptrCast(@alignCast(userdata));
5572 const current_thread = Thread.getCurrent(t);5619 const current_thread = Thread.getCurrent(t);
55735620
...@@ -5596,7 +5643,7 @@ fn fileReadStreamingWindows(userdata: ?*anyopaque, file: File, data: [][]u8) Fil...@@ -5596,7 +5643,7 @@ fn fileReadStreamingWindows(userdata: ?*anyopaque, file: File, data: [][]u8) Fil
5596 }5643 }
5597}5644}
55985645
5599fn fileReadPositionalPosix(userdata: ?*anyopaque, file: File, data: [][]u8, offset: u64) File.ReadPositionalError!usize {5646fn fileReadPositionalPosix(userdata: ?*anyopaque, file: File, data: []const []u8, offset: u64) File.ReadPositionalError!usize {
5600 const t: *Threaded = @ptrCast(@alignCast(userdata));5647 const t: *Threaded = @ptrCast(@alignCast(userdata));
5601 const current_thread = Thread.getCurrent(t);5648 const current_thread = Thread.getCurrent(t);
56025649
...@@ -5698,7 +5745,7 @@ const fileReadPositional = switch (native_os) {...@@ -5698,7 +5745,7 @@ const fileReadPositional = switch (native_os) {
5698 else => fileReadPositionalPosix,5745 else => fileReadPositionalPosix,
5699};5746};
57005747
5701fn fileReadPositionalWindows(userdata: ?*anyopaque, file: File, data: [][]u8, offset: u64) File.ReadPositionalError!usize {5748fn fileReadPositionalWindows(userdata: ?*anyopaque, file: File, data: []const []u8, offset: u64) File.ReadPositionalError!usize {
5702 const t: *Threaded = @ptrCast(@alignCast(userdata));5749 const t: *Threaded = @ptrCast(@alignCast(userdata));
5703 const current_thread = Thread.getCurrent(t);5750 const current_thread = Thread.getCurrent(t);
57045751