From b7ba0b69b19a20523af751c0935882ddba579c8c Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 6 Mar 2024 18:35:04 -0700 Subject: [PATCH] std.io.Writer: add writeFile method --- lib/std/io/Writer.zig | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/std/io/Writer.zig b/lib/std/io/Writer.zig index 82040efc3a..95a7bd5d6a 100644 --- a/lib/std/io/Writer.zig +++ b/lib/std/io/Writer.zig @@ -58,3 +58,14 @@ pub fn writeStruct(self: Self, value: anytype) anyerror!void { comptime assert(@typeInfo(@TypeOf(value)).Struct.layout != .Auto); return self.writeAll(mem.asBytes(&value)); } + +pub fn writeFile(self: Self, file: std.fs.File) anyerror!void { + // TODO: figure out how to adjust std lib abstractions so that this ends up + // doing sendfile or maybe even copy_file_range under the right conditions. + var buf: [4000]u8 = undefined; + while (true) { + const n = try file.readAll(&buf); + try self.writeAll(buf[0..n]); + if (n < buf.len) return; + } +}