diff options
| author | Igor Anić <igor.anic@gmail.com> | 2024-08-16 07:05:53 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-15 22:05:53 -0700 |
| commit | 72bcc3b7a52a8ce7a582ed205325f761da72a6a5 (patch) | |
| tree | c28796fb8db1093c10f3b27ca8b0980c6103308b /src | |
| parent | 708414aaf4d88157153cf413137d2cb3c106c3b4 (diff) | |
| download | zig-72bcc3b7a52a8ce7a582ed205325f761da72a6a5.tar.gz zig-72bcc3b7a52a8ce7a582ed205325f761da72a6a5.zip | |
std.tar: add writer (#19603)
Simplifies code in docs creation where we used `std.tar.output.Header`.
Writer uses that Header internally and provides higher level interface.
Updates checksum on write, handles long file names, allows setting mtime and file permission mode. Provides handy interface for passing `Dir.WalkerEntry`.
Diffstat (limited to 'src')
| -rw-r--r-- | src/Compilation.zig | 33 |
1 files changed, 4 insertions, 29 deletions
diff --git a/src/Compilation.zig b/src/Compilation.zig index d7dabd5164..d25534ceb8 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -4071,7 +4071,8 @@ fn docsCopyModule(comp: *Compilation, module: *Package.Module, name: []const u8, var walker = try mod_dir.walk(comp.gpa); defer walker.deinit(); - const padding_buffer = [1]u8{0} ** 512; + var archiver = std.tar.writer(tar_file.writer().any()); + archiver.prefix = name; while (try walker.next()) |entry| { switch (entry.kind) { @@ -4082,43 +4083,17 @@ fn docsCopyModule(comp: *Compilation, module: *Package.Module, name: []const u8, }, else => continue, } - var file = mod_dir.openFile(entry.path, .{}) catch |err| { return comp.lockAndSetMiscFailure(.docs_copy, "unable to open '{}{s}': {s}", .{ root, entry.path, @errorName(err), }); }; defer file.close(); - - const stat = file.stat() catch |err| { - return comp.lockAndSetMiscFailure(.docs_copy, "unable to stat '{}{s}': {s}", .{ + archiver.writeFile(entry.path, file) catch |err| { + return comp.lockAndSetMiscFailure(.docs_copy, "unable to archive '{}{s}': {s}", .{ root, entry.path, @errorName(err), }); }; - - var file_header = std.tar.output.Header.init(); - file_header.typeflag = .regular; - try file_header.setPath(name, entry.path); - try file_header.setSize(stat.size); - try file_header.updateChecksum(); - - const header_bytes = std.mem.asBytes(&file_header); - const padding = p: { - const remainder: u16 = @intCast(stat.size % 512); - const n = if (remainder > 0) 512 - remainder else 0; - break :p padding_buffer[0..n]; - }; - - var header_and_trailer: [2]std.posix.iovec_const = .{ - .{ .base = header_bytes.ptr, .len = header_bytes.len }, - .{ .base = padding.ptr, .len = padding.len }, - }; - - try tar_file.writeFileAll(file, .{ - .in_len = stat.size, - .headers_and_trailers = &header_and_trailer, - .header_count = 1, - }); } } |
