diff options
| author | Can Celik <ogulcancelik@gmail.com> | 2026-09-10 21:51:57 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-09-10 21:51:57 +0300 |
| commit | 425c86179791cd32e2d4ce0ae7f269940b4b0663 (patch) | |
| tree | e52c4383a6d69216cbe2a32638bf90841d7e2189 /vendor/libghostty-vt/src/lib/compat/file.zig | |
| parent | 90e947a6e26e02f10ec7f1694c4eb5b78a86406b (diff) | |
chore: upgrade libghostty and preserve terminal behavior (#3906)
Diffstat (limited to 'vendor/libghostty-vt/src/lib/compat/file.zig')
| -rw-r--r-- | vendor/libghostty-vt/src/lib/compat/file.zig | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/vendor/libghostty-vt/src/lib/compat/file.zig b/vendor/libghostty-vt/src/lib/compat/file.zig new file mode 100644 index 00000000..efb31a52 --- /dev/null +++ b/vendor/libghostty-vt/src/lib/compat/file.zig @@ -0,0 +1,56 @@ +//! Code taken from 0.15.2 and `std.fs.file`. See README.md for license and +//! details. +const builtin = @import("builtin"); +const std = @import("std"); +const global = @import("../../global.zig"); + +pub const ReadToEndAllocError = error{FileTooBig} || + std.Io.File.ReadStreamingError || + std.mem.Allocator.Error; + +/// Read the file from its current position through end-of-stream, returning +/// `error.FileTooBig` if the result exceeds `max_bytes`. +/// +/// Caller owns the memory. +pub fn readToEndAlloc(file: std.Io.File, alloc: std.mem.Allocator, max_bytes: usize) ReadToEndAllocError![]u8 { + var read_buf: [4096]u8 = undefined; + var result: std.ArrayList(u8) = .empty; + defer result.deinit(alloc); + + while (true) { + const n = file.readStreaming(global.io(), &.{&read_buf}) catch |err| switch (err) { + error.EndOfStream => break, + else => |e| return e, + }; + if (n == 0) continue; + if (n > max_bytes - result.items.len) return error.FileTooBig; + try result.appendSlice(alloc, read_buf[0..n]); + } + + return result.toOwnedSlice(alloc); +} + +test "readToEndAlloc reads through EOF and permits exact limit" { + const testing = std.testing; + const contents = "hello, world"; + + var tmp_dir = testing.tmpDir(.{}); + defer tmp_dir.cleanup(); + try tmp_dir.dir.writeFile(testing.io, .{ + .sub_path = "data", + .data = contents, + }); + + const file = try tmp_dir.dir.openFile(testing.io, "data", .{}); + defer file.close(testing.io); + const result = try readToEndAlloc(file, testing.allocator, contents.len); + defer testing.allocator.free(result); + try testing.expectEqualStrings(contents, result); + + const too_big_file = try tmp_dir.dir.openFile(testing.io, "data", .{}); + defer too_big_file.close(testing.io); + try testing.expectError( + error.FileTooBig, + readToEndAlloc(too_big_file, testing.allocator, contents.len - 1), + ); +} |
