aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2025-12-08 18:00:55 -0800
committerAndrew Kelley <andrew@ziglang.org>2025-12-23 22:15:08 -0800
commit1dcfc8787e86ed94d216976e621a49fc488e8214 (patch)
treed5225fa3a0abee6f6ff8f2793fe1ccea951d20a8 /lib/std
parent4be8be1d2bd6959efae7df95e3f5713adf953a42 (diff)
downloadzig-1dcfc8787e86ed94d216976e621a49fc488e8214.tar.gz
zig-1dcfc8787e86ed94d216976e621a49fc488e8214.zip
update all readFileAlloc() to accept Io instance
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/Build/Cache.zig3
-rw-r--r--lib/std/Build/Step/CheckFile.zig3
-rw-r--r--lib/std/Build/Step/ConfigHeader.zig4
-rw-r--r--lib/std/Build/WebServer.zig5
-rw-r--r--lib/std/Io/Dir.zig6
-rw-r--r--lib/std/crypto/Certificate/Bundle/macos.zig3
-rw-r--r--lib/std/fs/test.zig27
-rw-r--r--lib/std/zig/LibCInstallation.zig8
-rw-r--r--lib/std/zig/WindowsSdk.zig2
9 files changed, 30 insertions, 31 deletions
diff --git a/lib/std/Build/Cache.zig b/lib/std/Build/Cache.zig
index 2d6dbc02fa..d2ba33c74d 100644
--- a/lib/std/Build/Cache.zig
+++ b/lib/std/Build/Cache.zig
@@ -1075,7 +1075,8 @@ pub const Manifest = struct {
fn addDepFileMaybePost(self: *Manifest, dir: Io.Dir, dep_file_sub_path: []const u8) !void {
const gpa = self.cache.gpa;
- const dep_file_contents = try dir.readFileAlloc(dep_file_sub_path, gpa, .limited(manifest_file_size_max));
+ const io = self.cache.io;
+ const dep_file_contents = try dir.readFileAlloc(io, dep_file_sub_path, gpa, .limited(manifest_file_size_max));
defer gpa.free(dep_file_contents);
var error_buf: std.ArrayList(u8) = .empty;
diff --git a/lib/std/Build/Step/CheckFile.zig b/lib/std/Build/Step/CheckFile.zig
index 560b6ad050..1c3813ca82 100644
--- a/lib/std/Build/Step/CheckFile.zig
+++ b/lib/std/Build/Step/CheckFile.zig
@@ -51,11 +51,12 @@ pub fn setName(check_file: *CheckFile, name: []const u8) void {
fn make(step: *Step, options: Step.MakeOptions) !void {
_ = options;
const b = step.owner;
+ const io = b.graph.io;
const check_file: *CheckFile = @fieldParentPtr("step", step);
try step.singleUnchangingWatchInput(check_file.source);
const src_path = check_file.source.getPath2(b, step);
- const contents = Io.Dir.cwd().readFileAlloc(src_path, b.allocator, .limited(check_file.max_bytes)) catch |err| {
+ const contents = Io.Dir.cwd().readFileAlloc(io, src_path, b.allocator, .limited(check_file.max_bytes)) catch |err| {
return step.fail("unable to read '{s}': {s}", .{
src_path, @errorName(err),
});
diff --git a/lib/std/Build/Step/ConfigHeader.zig b/lib/std/Build/Step/ConfigHeader.zig
index 589110d4c4..250bae5009 100644
--- a/lib/std/Build/Step/ConfigHeader.zig
+++ b/lib/std/Build/Step/ConfigHeader.zig
@@ -208,7 +208,7 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
.autoconf_undef, .autoconf_at => |file_source| {
try bw.writeAll(c_generated_line);
const src_path = file_source.getPath2(b, step);
- const contents = Io.Dir.cwd().readFileAlloc(src_path, arena, .limited(config_header.max_bytes)) catch |err| {
+ const contents = Io.Dir.cwd().readFileAlloc(io, src_path, arena, .limited(config_header.max_bytes)) catch |err| {
return step.fail("unable to read autoconf input file '{s}': {s}", .{
src_path, @errorName(err),
});
@@ -222,7 +222,7 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
.cmake => |file_source| {
try bw.writeAll(c_generated_line);
const src_path = file_source.getPath2(b, step);
- const contents = Io.Dir.cwd().readFileAlloc(src_path, arena, .limited(config_header.max_bytes)) catch |err| {
+ const contents = Io.Dir.cwd().readFileAlloc(io, src_path, arena, .limited(config_header.max_bytes)) catch |err| {
return step.fail("unable to read cmake input file '{s}': {s}", .{
src_path, @errorName(err),
});
diff --git a/lib/std/Build/WebServer.zig b/lib/std/Build/WebServer.zig
index 38a7a73588..72306cbab9 100644
--- a/lib/std/Build/WebServer.zig
+++ b/lib/std/Build/WebServer.zig
@@ -469,11 +469,12 @@ pub fn serveFile(
content_type: []const u8,
) !void {
const gpa = ws.gpa;
+ const io = ws.graph.io;
// The desired API is actually sendfile, which will require enhancing http.Server.
// We load the file with every request so that the user can make changes to the file
// and refresh the HTML page without restarting this server.
- const file_contents = path.root_dir.handle.readFileAlloc(path.sub_path, gpa, .limited(10 * 1024 * 1024)) catch |err| {
- log.err("failed to read '{f}': {s}", .{ path, @errorName(err) });
+ const file_contents = path.root_dir.handle.readFileAlloc(io, path.sub_path, gpa, .limited(10 * 1024 * 1024)) catch |err| {
+ log.err("failed to read '{f}': {t}", .{ path, err });
return error.AlreadyReported;
};
defer gpa.free(file_contents);
diff --git a/lib/std/Io/Dir.zig b/lib/std/Io/Dir.zig
index 7d1f6212dc..cf5e2b4c72 100644
--- a/lib/std/Io/Dir.zig
+++ b/lib/std/Io/Dir.zig
@@ -1117,10 +1117,10 @@ pub fn readLink(dir: Dir, io: Io, sub_path: []const u8, buffer: []u8) ReadLinkEr
/// On other platforms, `path` is an opaque sequence of bytes with no particular encoding.
pub fn readLinkAbsolute(io: Io, absolute_path: []const u8, buffer: []u8) ReadLinkError!usize {
assert(path.isAbsolute(absolute_path));
- return io.vtable.dirReadLink(io.userdata, .cwd(), path, buffer);
+ return io.vtable.dirReadLink(io.userdata, .cwd(), absolute_path, buffer);
}
-pub const ReadFileAllocError = File.OpenError || File.ReadError || Allocator.Error || error{
+pub const ReadFileAllocError = File.OpenError || File.Reader.Error || Allocator.Error || error{
/// File size reached or exceeded the provided limit.
StreamTooLong,
};
@@ -1603,7 +1603,7 @@ pub const CopyFileOptions = struct {
pub const CopyFileError = File.OpenError || File.StatError ||
File.Atomic.InitError || File.Atomic.FinishError ||
- File.ReadError || File.WriteError || error{InvalidFileName};
+ File.Reader.Error || File.WriteError || error{InvalidFileName};
/// Atomically creates a new file at `dest_path` within `dest_dir` with the
/// same contents as `source_path` within `source_dir`, overwriting any already
diff --git a/lib/std/crypto/Certificate/Bundle/macos.zig b/lib/std/crypto/Certificate/Bundle/macos.zig
index 444d8da675..086c8feb3f 100644
--- a/lib/std/crypto/Certificate/Bundle/macos.zig
+++ b/lib/std/crypto/Certificate/Bundle/macos.zig
@@ -17,9 +17,8 @@ pub fn rescanMac(cb: *Bundle, gpa: Allocator, io: Io, now: Io.Timestamp) RescanM
"/Library/Keychains/System.keychain",
};
- _ = io; // TODO migrate file system to use std.Io
for (keychain_paths) |keychain_path| {
- const bytes = Io.Dir.cwd().readFileAlloc(keychain_path, gpa, .limited(std.math.maxInt(u32))) catch |err| switch (err) {
+ const bytes = Io.Dir.cwd().readFileAlloc(io, keychain_path, gpa, .limited(std.math.maxInt(u32))) catch |err| switch (err) {
error.StreamTooLong => return error.FileTooBig,
else => |e| return e,
};
diff --git a/lib/std/fs/test.zig b/lib/std/fs/test.zig
index 92992f91b4..bb3a9da0f5 100644
--- a/lib/std/fs/test.zig
+++ b/lib/std/fs/test.zig
@@ -767,7 +767,7 @@ test "readFileAlloc" {
var file = try tmp_dir.dir.createFile(io, "test_file", .{ .read = true });
defer file.close(io);
- const buf1 = try tmp_dir.dir.readFileAlloc("test_file", testing.allocator, .limited(1024));
+ const buf1 = try tmp_dir.dir.readFileAlloc(io, "test_file", testing.allocator, .limited(1024));
defer testing.allocator.free(buf1);
try testing.expectEqualStrings("", buf1);
@@ -776,7 +776,7 @@ test "readFileAlloc" {
{
// max_bytes > file_size
- const buf2 = try tmp_dir.dir.readFileAlloc("test_file", testing.allocator, .limited(1024));
+ const buf2 = try tmp_dir.dir.readFileAlloc(io, "test_file", testing.allocator, .limited(1024));
defer testing.allocator.free(buf2);
try testing.expectEqualStrings(write_buf, buf2);
}
@@ -785,13 +785,13 @@ test "readFileAlloc" {
// max_bytes == file_size
try testing.expectError(
error.StreamTooLong,
- tmp_dir.dir.readFileAlloc("test_file", testing.allocator, .limited(write_buf.len)),
+ tmp_dir.dir.readFileAlloc(io, "test_file", testing.allocator, .limited(write_buf.len)),
);
}
{
// max_bytes == file_size + 1
- const buf2 = try tmp_dir.dir.readFileAlloc("test_file", testing.allocator, .limited(write_buf.len + 1));
+ const buf2 = try tmp_dir.dir.readFileAlloc(io, "test_file", testing.allocator, .limited(write_buf.len + 1));
defer testing.allocator.free(buf2);
try testing.expectEqualStrings(write_buf, buf2);
}
@@ -799,7 +799,7 @@ test "readFileAlloc" {
// max_bytes < file_size
try testing.expectError(
error.StreamTooLong,
- tmp_dir.dir.readFileAlloc("test_file", testing.allocator, .limited(write_buf.len - 1)),
+ tmp_dir.dir.readFileAlloc(io, "test_file", testing.allocator, .limited(write_buf.len - 1)),
);
}
@@ -877,16 +877,16 @@ test "file operations on directories" {
switch (native_os) {
.dragonfly, .netbsd => {
// no error when reading a directory. See https://github.com/ziglang/zig/issues/5732
- const buf = try ctx.dir.readFileAlloc(test_dir_name, testing.allocator, .unlimited);
+ const buf = try ctx.dir.readFileAlloc(io, test_dir_name, testing.allocator, .unlimited);
testing.allocator.free(buf);
},
.wasi => {
// WASI return EBADF, which gets mapped to NotOpenForReading.
// See https://github.com/bytecodealliance/wasmtime/issues/1935
- try testing.expectError(error.NotOpenForReading, ctx.dir.readFileAlloc(test_dir_name, testing.allocator, .unlimited));
+ try testing.expectError(error.NotOpenForReading, ctx.dir.readFileAlloc(io, test_dir_name, testing.allocator, .unlimited));
},
else => {
- try testing.expectError(error.IsDir, ctx.dir.readFileAlloc(test_dir_name, testing.allocator, .unlimited));
+ try testing.expectError(error.IsDir, ctx.dir.readFileAlloc(io, test_dir_name, testing.allocator, .unlimited));
},
}
@@ -1679,14 +1679,14 @@ test "copyFile" {
try ctx.dir.copyFile(src_file, ctx.dir, dest_file2, .{ .override_mode = File.default_mode });
defer ctx.dir.deleteFile(dest_file2) catch {};
- try expectFileContents(ctx.dir, dest_file, data);
- try expectFileContents(ctx.dir, dest_file2, data);
+ try expectFileContents(io, ctx.dir, dest_file, data);
+ try expectFileContents(io, ctx.dir, dest_file2, data);
}
}.impl);
}
-fn expectFileContents(dir: Dir, file_path: []const u8, data: []const u8) !void {
- const contents = try dir.readFileAlloc(file_path, testing.allocator, .limited(1000));
+fn expectFileContents(io: Io, dir: Dir, file_path: []const u8, data: []const u8) !void {
+ const contents = try dir.readFileAlloc(io, file_path, testing.allocator, .limited(1000));
defer testing.allocator.free(contents);
try testing.expectEqualSlices(u8, data, contents);
@@ -1695,6 +1695,7 @@ fn expectFileContents(dir: Dir, file_path: []const u8, data: []const u8) !void {
test "AtomicFile" {
try testWithAllSupportedPathTypes(struct {
fn impl(ctx: *TestContext) !void {
+ const io = ctx.io;
const allocator = ctx.arena.allocator();
const test_out_file = try ctx.transformPath("tmp_atomic_file_test_dest.txt");
const test_content =
@@ -1709,7 +1710,7 @@ test "AtomicFile" {
try af.file_writer.interface.writeAll(test_content);
try af.finish();
}
- const content = try ctx.dir.readFileAlloc(test_out_file, allocator, .limited(9999));
+ const content = try ctx.dir.readFileAlloc(io, test_out_file, allocator, .limited(9999));
try testing.expectEqualStrings(test_content, content);
try ctx.dir.deleteFile(test_out_file);
diff --git a/lib/std/zig/LibCInstallation.zig b/lib/std/zig/LibCInstallation.zig
index f3dc73838f..1463ff4a40 100644
--- a/lib/std/zig/LibCInstallation.zig
+++ b/lib/std/zig/LibCInstallation.zig
@@ -37,11 +37,7 @@ pub const FindError = error{
ZigIsTheCCompiler,
};
-pub fn parse(
- allocator: Allocator,
- libc_file: []const u8,
- target: *const std.Target,
-) !LibCInstallation {
+pub fn parse(allocator: Allocator, io: Io, libc_file: []const u8, target: *const std.Target) !LibCInstallation {
var self: LibCInstallation = .{};
const fields = std.meta.fields(LibCInstallation);
@@ -57,7 +53,7 @@ pub fn parse(
}
}
- const contents = try Io.Dir.cwd().readFileAlloc(libc_file, allocator, .limited(std.math.maxInt(usize)));
+ const contents = try Io.Dir.cwd().readFileAlloc(io, libc_file, allocator, .limited(std.math.maxInt(usize)));
defer allocator.free(contents);
var it = std.mem.tokenizeScalar(u8, contents, '\n');
diff --git a/lib/std/zig/WindowsSdk.zig b/lib/std/zig/WindowsSdk.zig
index dca474020a..3f6d58b6ba 100644
--- a/lib/std/zig/WindowsSdk.zig
+++ b/lib/std/zig/WindowsSdk.zig
@@ -775,7 +775,7 @@ const MsvcLibDir = struct {
writer.writeByte(std.fs.path.sep) catch unreachable;
writer.writeAll("state.json") catch unreachable;
- const json_contents = instances_dir.readFileAlloc(writer.buffered(), allocator, .limited(std.math.maxInt(usize))) catch continue;
+ const json_contents = instances_dir.readFileAlloc(io, writer.buffered(), allocator, .limited(std.math.maxInt(usize))) catch continue;
defer allocator.free(json_contents);
var parsed = std.json.parseFromSlice(std.json.Value, allocator, json_contents, .{}) catch continue;