link.MappedFile: update statx usage

This commit is contained in:
Andrew Kelley
2025-12-14 16:46:08 -08:00
parent 2a40c1b556
commit 4458e423bf
6 changed files with 69 additions and 31 deletions
+14 -4
View File
@@ -1822,6 +1822,11 @@ fn dirStatFileLinux(
const t: *Threaded = @ptrCast(@alignCast(userdata));
const current_thread = Thread.getCurrent(t);
const linux = std.os.linux;
const use_c = std.c.versionCheck(if (builtin.abi.isAndroid())
.{ .major = 30, .minor = 0, .patch = 0 }
else
.{ .major = 2, .minor = 28, .patch = 0 });
const sys = if (use_c) std.c else std.os.linux;
var path_buffer: [posix.PATH_MAX]u8 = undefined;
const sub_path_posix = try pathToPosix(sub_path, &path_buffer);
@@ -1832,14 +1837,14 @@ fn dirStatFileLinux(
try current_thread.beginSyscall();
while (true) {
var statx = std.mem.zeroes(linux.Statx);
const rc = linux.statx(
const rc = sys.statx(
dir.handle,
sub_path_posix,
flags,
.{ .TYPE = true, .MODE = true, .ATIME = true, .MTIME = true, .CTIME = true, .INO = true, .SIZE = true },
&statx,
);
switch (linux.errno(rc)) {
switch (sys.errno(rc)) {
.SUCCESS => {
current_thread.endSyscall();
assert(statx.mask.TYPE);
@@ -2076,18 +2081,23 @@ fn fileStatLinux(userdata: ?*anyopaque, file: File) File.StatError!File.Stat {
const t: *Threaded = @ptrCast(@alignCast(userdata));
const current_thread = Thread.getCurrent(t);
const linux = std.os.linux;
const use_c = std.c.versionCheck(if (builtin.abi.isAndroid())
.{ .major = 30, .minor = 0, .patch = 0 }
else
.{ .major = 2, .minor = 28, .patch = 0 });
const sys = if (use_c) std.c else std.os.linux;
try current_thread.beginSyscall();
while (true) {
var statx = std.mem.zeroes(linux.Statx);
const rc = linux.statx(
const rc = sys.statx(
file.handle,
"",
linux.AT.EMPTY_PATH,
.{ .TYPE = true, .MODE = true, .ATIME = true, .MTIME = true, .CTIME = true, .INO = true, .SIZE = true },
&statx,
);
switch (linux.errno(rc)) {
switch (sys.errno(rc)) {
.SUCCESS => {
current_thread.endSyscall();
assert(statx.mask.TYPE);
+4 -3
View File
@@ -1148,9 +1148,10 @@ pub fn checkAllAllocationFailures(backing_allocator: std.mem.Allocator, comptime
break :x failing_allocator_inst.alloc_index;
};
var fail_index: usize = 0;
while (fail_index < needed_alloc_count) : (fail_index += 1) {
var failing_allocator_inst = std.testing.FailingAllocator.init(backing_allocator, .{ .fail_index = fail_index });
for (0..needed_alloc_count) |fail_index| {
var failing_allocator_inst = std.testing.FailingAllocator.init(backing_allocator, .{
.fail_index = fail_index,
});
args.@"0" = failing_allocator_inst.allocator();
if (@call(.auto, test_fn, args)) |_| {
+4 -1
View File
@@ -168,7 +168,10 @@ pub fn renderToStderr(eb: ErrorBundle, io: Io, options: RenderOptions, color: st
var buffer: [256]u8 = undefined;
const stderr = try io.lockStderrWriter(&buffer);
defer io.unlockStderrWriter();
try renderToWriter(eb, options, &stderr.interface, color.getTtyConf(stderr.mode));
renderToWriter(eb, options, &stderr.interface, color.getTtyConf(stderr.mode)) catch |err| switch (err) {
error.WriteFailed => return stderr.interface.err.?,
else => |e| return e,
};
}
pub fn renderToWriter(
+2 -2
View File
@@ -6362,9 +6362,9 @@ fn testParse(io: Io, source: [:0]const u8, allocator: Allocator, anything_change
return formatted;
}
fn testTransformImpl(
io: Io,
allocator: Allocator,
fba: *std.heap.FixedBufferAllocator,
io: Io,
source: [:0]const u8,
expected_source: []const u8,
) !void {
@@ -6386,7 +6386,7 @@ fn testTransform(source: [:0]const u8, expected_source: []const u8) !void {
const io = std.testing.io;
var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);
return std.testing.checkAllAllocationFailures(fixed_allocator.allocator(), testTransformImpl, .{
io, &fixed_allocator, source, expected_source,
&fixed_allocator, io, source, expected_source,
});
}
fn testCanonical(source: [:0]const u8) !void {
+36 -12
View File
@@ -1,3 +1,4 @@
/// TODO add a mapped file abstraction to std.Io
const MappedFile = @This();
const builtin = @import("builtin");
@@ -74,18 +75,41 @@ pub fn init(file: std.Io.File, gpa: std.mem.Allocator, io: Io) !MappedFile {
};
}
if (is_linux) {
const statx = try linux.wrapped.statx(
mf.file.handle,
"",
std.posix.AT.EMPTY_PATH,
.{ .TYPE = true, .SIZE = true, .BLOCKS = true },
);
assert(statx.mask.TYPE);
assert(statx.mask.SIZE);
assert(statx.mask.BLOCKS);
if (!std.posix.S.ISREG(statx.mode)) return error.PathAlreadyExists;
break :stat .{ statx.size, @max(std.heap.pageSize(), statx.blksize) };
const use_c = std.c.versionCheck(if (builtin.abi.isAndroid())
.{ .major = 30, .minor = 0, .patch = 0 }
else
.{ .major = 2, .minor = 28, .patch = 0 });
const sys = if (use_c) std.c else std.os.linux;
while (true) {
var statx = std.mem.zeroes(linux.Statx);
const rc = sys.statx(
mf.file.handle,
"",
std.posix.AT.EMPTY_PATH,
.{ .TYPE = true, .SIZE = true, .BLOCKS = true },
&statx,
);
switch (sys.errno(rc)) {
.SUCCESS => {
assert(statx.mask.TYPE);
assert(statx.mask.SIZE);
assert(statx.mask.BLOCKS);
if (!std.posix.S.ISREG(statx.mode)) return error.PathAlreadyExists;
break :stat .{ statx.size, @max(std.heap.pageSize(), statx.blksize) };
},
.INTR => continue,
.ACCES => return error.AccessDenied,
.BADF => if (std.debug.runtime_safety) unreachable else return error.Unexpected,
.FAULT => if (std.debug.runtime_safety) unreachable else return error.Unexpected,
.INVAL => if (std.debug.runtime_safety) unreachable else return error.Unexpected,
.LOOP => return error.SymLinkLoop,
.NAMETOOLONG => return error.NameTooLong,
.NOENT => return error.FileNotFound,
.NOTDIR => return error.FileNotFound,
.NOMEM => return error.SystemResources,
else => |err| return std.posix.unexpectedErrno(err),
}
}
}
const stat = try std.posix.fstat(mf.file.handle);
if (!std.posix.S.ISREG(stat.mode)) return error.PathAlreadyExists;
+9 -9
View File
@@ -4607,7 +4607,7 @@ fn updateModule(comp: *Compilation, color: Color, prog_node: std.Progress.Node)
if (errors.errorMessageCount() > 0) {
const io = comp.io;
errors.renderToStderr(io, .{}, color);
try errors.renderToStderr(io, .{}, color);
return error.CompileErrorsReported;
}
}
@@ -4660,7 +4660,7 @@ fn cmdTranslateC(
return;
} else {
const color: Color = .auto;
result.errors.renderToStderr(io, .{}, color);
result.errors.renderToStderr(io, .{}, color) catch {};
process.exit(1);
}
}
@@ -5281,7 +5281,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8)
if (fetch.error_bundle.root_list.items.len > 0) {
var errors = try fetch.error_bundle.toOwnedBundle("");
errors.renderToStderr(io, .{}, color);
errors.renderToStderr(io, .{}, color) catch {};
process.exit(1);
}
@@ -6213,7 +6213,7 @@ fn cmdAstCheck(arena: Allocator, io: Io, args: []const []const u8) !void {
try wip_errors.init(arena);
try wip_errors.addZirErrorMessages(zir, tree, source, display_path);
var error_bundle = try wip_errors.toOwnedBundle("");
error_bundle.renderToStderr(io, .{}, color);
try error_bundle.renderToStderr(io, .{}, color);
if (zir.loweringFailed()) {
process.exit(1);
}
@@ -6284,7 +6284,7 @@ fn cmdAstCheck(arena: Allocator, io: Io, args: []const []const u8) !void {
try wip_errors.init(arena);
try wip_errors.addZoirErrorMessages(zoir, tree, source, display_path);
var error_bundle = try wip_errors.toOwnedBundle("");
error_bundle.renderToStderr(io, .{}, color);
error_bundle.renderToStderr(io, .{}, color) catch {};
process.exit(1);
}
@@ -6558,7 +6558,7 @@ fn cmdChangelist(arena: Allocator, io: Io, args: []const []const u8) !void {
try wip_errors.init(arena);
try wip_errors.addZirErrorMessages(old_zir, old_tree, old_source, old_source_path);
var error_bundle = try wip_errors.toOwnedBundle("");
error_bundle.renderToStderr(io, .{}, color);
error_bundle.renderToStderr(io, .{}, color) catch {};
process.exit(1);
}
@@ -6570,7 +6570,7 @@ fn cmdChangelist(arena: Allocator, io: Io, args: []const []const u8) !void {
try wip_errors.init(arena);
try wip_errors.addZirErrorMessages(new_zir, new_tree, new_source, new_source_path);
var error_bundle = try wip_errors.toOwnedBundle("");
error_bundle.renderToStderr(io, .{}, color);
error_bundle.renderToStderr(io, .{}, color) catch {};
process.exit(1);
}
@@ -7006,7 +7006,7 @@ fn cmdFetch(
if (fetch.error_bundle.root_list.items.len > 0) {
var errors = try fetch.error_bundle.toOwnedBundle("");
errors.renderToStderr(io, .{}, color);
errors.renderToStderr(io, .{}, color) catch {};
process.exit(1);
}
@@ -7363,7 +7363,7 @@ fn loadManifest(
var error_bundle = try wip_errors.toOwnedBundle("");
defer error_bundle.deinit(gpa);
error_bundle.renderToStderr(io, .{}, options.color);
error_bundle.renderToStderr(io, .{}, options.color) catch {};
process.exit(2);
}