Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lib/std/Build/Step/Run.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1821,17 +1821,15 @@ fn evalGeneric(run: *Run, child: *std.process.Child) !StdIoResult {
stdout_bytes = try poller.toOwnedSlice(.stdout);
stderr_bytes = try poller.toOwnedSlice(.stderr);
} else {
var small_buffer: [1]u8 = undefined;
var stdout_reader = stdout.readerStreaming(&small_buffer);
var stdout_reader = stdout.readerStreaming(&.{});
stdout_bytes = stdout_reader.interface.allocRemaining(arena, run.stdio_limit) catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
error.ReadFailed => return stdout_reader.err.?,
error.StreamTooLong => return error.StdoutStreamTooLong,
};
}
} else if (child.stderr) |stderr| {
var small_buffer: [1]u8 = undefined;
var stderr_reader = stderr.readerStreaming(&small_buffer);
var stderr_reader = stderr.readerStreaming(&.{});
stderr_bytes = stderr_reader.interface.allocRemaining(arena, run.stdio_limit) catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
error.ReadFailed => return stderr_reader.err.?,
Expand Down
2 changes: 0 additions & 2 deletions lib/std/Io/Reader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,6 @@ pub const LimitedAllocError = Allocator.Error || ShortError || error{StreamTooLo
/// such case, the next byte that would be read will be the first one to exceed
/// `limit`, and all preceeding bytes have been discarded.
///
/// Asserts `buffer` has nonzero capacity.
///
/// See also:
/// * `appendRemaining`
pub fn allocRemaining(r: *Reader, gpa: Allocator, limit: Limit) LimitedAllocError![]u8 {
Expand Down
41 changes: 24 additions & 17 deletions lib/std/fs/File.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1194,11 +1194,16 @@ pub const Reader = struct {
};
}

pub fn initMode(file: File, buffer: []u8, init_mode: Reader.Mode) Reader {
/// Positional is more threadsafe, since the global seek position is not
/// affected, but when such syscalls are not available, preemptively
/// initializing in streaming mode skips a failed syscall.
pub fn initStreaming(file: File, buffer: []u8) Reader {
return .{
.file = file,
.interface = initInterface(buffer),
.mode = init_mode,
.interface = Reader.initInterface(buffer),
.mode = .streaming,
.seek_err = error.Unseekable,
.size_err = error.Streaming,
};
}

Expand Down Expand Up @@ -1578,14 +1583,21 @@ pub const Writer = struct {
const max_buffers_len = 16;

pub fn init(file: File, buffer: []u8) Writer {
return initMode(file, buffer, .positional);
return .{
.file = file,
.interface = initInterface(buffer),
.mode = .positional,
};
}

pub fn initMode(file: File, buffer: []u8, init_mode: Writer.Mode) Writer {
/// Positional is more threadsafe, since the global seek position is not
/// affected, but when such syscalls are not available, preemptively
/// initializing in streaming mode will skip a failed syscall.
pub fn initStreaming(file: File, buffer: []u8) Writer {
return .{
.file = file,
.interface = initInterface(buffer),
.mode = init_mode,
.mode = .streaming,
};
}

Expand Down Expand Up @@ -2092,15 +2104,10 @@ pub fn reader(file: File, buffer: []u8) Reader {
}

/// Positional is more threadsafe, since the global seek position is not
/// affected, but when such syscalls are not available, preemptively choosing
/// `Reader.Mode.streaming` will skip a failed syscall.
/// affected, but when such syscalls are not available, preemptively
/// initializing in streaming mode skips a failed syscall.
pub fn readerStreaming(file: File, buffer: []u8) Reader {
return .{
.file = file,
.interface = Reader.initInterface(buffer),
.mode = .streaming,
.seek_err = error.Unseekable,
};
return .initStreaming(file, buffer);
}

/// Defaults to positional reading; falls back to streaming.
Expand All @@ -2112,10 +2119,10 @@ pub fn writer(file: File, buffer: []u8) Writer {
}

/// Positional is more threadsafe, since the global seek position is not
/// affected, but when such syscalls are not available, preemptively choosing
/// `Writer.Mode.streaming` will skip a failed syscall.
/// affected, but when such syscalls are not available, preemptively
/// initializing in streaming mode will skip a failed syscall.
pub fn writerStreaming(file: File, buffer: []u8) Writer {
return .initMode(file, buffer, .streaming);
return .initStreaming(file, buffer);
}

const range_off: windows.LARGE_INTEGER = 0;
Expand Down
2 changes: 1 addition & 1 deletion lib/std/net.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2233,7 +2233,7 @@ pub const Stream = struct {
},
.buffer = buffer,
},
.file_writer = .initMode(.{ .handle = stream.handle }, &.{}, .streaming),
.file_writer = .initStreaming(.{ .handle = stream.handle }, &.{}),
};
}

Expand Down
4 changes: 2 additions & 2 deletions lib/std/process/Child.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1003,14 +1003,14 @@ fn forkChildErrReport(fd: i32, err: ChildProcess.SpawnError) noreturn {

fn writeIntFd(fd: i32, value: ErrInt) !void {
var buffer: [8]u8 = undefined;
var fw: std.fs.File.Writer = .initMode(.{ .handle = fd }, &buffer, .streaming);
var fw: std.fs.File.Writer = .initStreaming(.{ .handle = fd }, &buffer);
fw.interface.writeInt(u64, value, .little) catch unreachable;
fw.interface.flush() catch return error.SystemResources;
}

fn readIntFd(fd: i32) !ErrInt {
var buffer: [8]u8 = undefined;
var fr: std.fs.File.Reader = .initMode(.{ .handle = fd }, &buffer, .streaming);
var fr: std.fs.File.Reader = .initStreaming(.{ .handle = fd }, &buffer);
return @intCast(fr.interface.takeInt(u64, .little) catch return error.SystemResources);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6266,7 +6266,8 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: std.Pr

try child.spawn();

const stderr = try child.stderr.?.deprecatedReader().readAllAlloc(arena, std.math.maxInt(usize));
var stderr_reader = child.stderr.?.readerStreaming(&.{});
const stderr = try stderr_reader.interface.allocRemaining(arena, .limited(std.math.maxInt(u32)));

const term = child.wait() catch |err| {
return comp.failCObj(c_object, "failed to spawn zig clang {s}: {s}", .{ argv.items[0], @errorName(err) });
Expand Down
2 changes: 1 addition & 1 deletion src/Package/Fetch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1632,7 +1632,7 @@ fn computeHash(f: *Fetch, pkg_path: Cache.Path, filter: Filter) RunError!Compute

fn dumpHashInfo(all_files: []const *const HashedFile) !void {
var stdout_buffer: [1024]u8 = undefined;
var stdout_writer: fs.File.Writer = .initMode(.stdout(), &stdout_buffer, .streaming);
var stdout_writer: fs.File.Writer = .initStreaming(.stdout(), &stdout_buffer);
const w = &stdout_writer.interface;
for (all_files) |hashed_file| {
try w.print("{t}: {x}: {s}\n", .{ hashed_file.kind, &hashed_file.hash, hashed_file.normalized_path });
Expand Down
5 changes: 1 addition & 4 deletions test/standalone/test_obj_link_run/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ pub fn build(b: *std.Build) void {
b.default_step = test_step;

const test_run = b.addRunArtifact(test_exe);
if (!is_windows) {
// https://github.com/ziglang/zig/issues/24867
test_run.addCheck(.{ .expect_stderr_match = "All 3 tests passed." });
}
test_run.addCheck(.{ .expect_stderr_match = "All 3 tests passed." });
test_step.dependOn(&test_run.step);
}

Expand Down
Loading