// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Archives written by other implementations, read by this one, and the //! other way round. //! //! The fixtures in `tests/data` are one small tree written by GNU tar and by //! bsdtar in every format each of them has; `tests/data/make-fixtures.sh` //! makes them. The standard library's own `std.tar` is the other party for //! the round trips, since it needs nothing outside the test. const std = @import("std"); const Io = std.Io; const testing = std.testing; const tar = @import("tar"); const long_dir = "d" ** 60; const long_file = "f" ** 80; const unsplittable = "u" ** 120; const long_target = "t" ** 110; const Expected = struct { name: []const u8, kind: tar.Kind = .file, content: []const u8 = "", link_name: []const u8 = "", mode: u32 = 0o644, }; const Fixture = struct { file: []const u8, bytes: []const u8, /// GNU tar archives `.` itself as `./`; bsdtar was given a list without it. has_root: bool, /// The members only a format with long names can hold. has_long: bool, /// v7 cannot hold the 141-byte name, split or not. has_split: bool, /// v7 has no FIFO type. has_fifo: bool, }; const fixtures = [_]Fixture{ .{ .file = "gnu-gnu.tar", .bytes = @embedFile("data/gnu-gnu.tar"), .has_root = true, .has_long = true, .has_split = true, .has_fifo = true }, .{ .file = "gnu-posix.tar", .bytes = @embedFile("data/gnu-posix.tar"), .has_root = true, .has_long = true, .has_split = true, .has_fifo = true }, .{ .file = "gnu-ustar.tar", .bytes = @embedFile("data/gnu-ustar.tar"), .has_root = true, .has_long = false, .has_split = true, .has_fifo = true }, .{ .file = "gnu-v7.tar", .bytes = @embedFile("data/gnu-v7.tar"), .has_root = true, .has_long = false, .has_split = false, .has_fifo = false }, .{ .file = "bsd-pax.tar", .bytes = @embedFile("data/bsd-pax.tar"), .has_root = false, .has_long = true, .has_split = true, .has_fifo = true }, .{ .file = "bsd-ustar.tar", .bytes = @embedFile("data/bsd-ustar.tar"), .has_root = false, .has_long = false, .has_split = true, .has_fifo = true }, .{ .file = "bsd-gnutar.tar", .bytes = @embedFile("data/bsd-gnutar.tar"), .has_root = false, .has_long = true, .has_split = true, .has_fifo = true }, }; /// What every fixture should read as, in archive order. Both tools were /// asked to sort by name, so the order is the same for all of them. fn expected(f: Fixture, buf: []Expected) []Expected { var n: usize = 0; const add = struct { fn add(b: []Expected, i: *usize, e: Expected) void { b[i.*] = e; i.* += 1; } }.add; if (f.has_root) add(buf, &n, .{ .name = "./", .kind = .directory, .mode = 0o755 }); add(buf, &n, .{ .name = "./" ++ long_dir ++ "/", .kind = .directory, .mode = 0o755 }); if (f.has_split) add(buf, &n, .{ .name = "./" ++ long_dir ++ "/" ++ long_file, .content = "split\n" }); add(buf, &n, .{ .name = "./dir/", .kind = .directory, .mode = 0o755 }); add(buf, &n, .{ .name = "./dir/empty" }); // ./dir/fifo is reported, not returned. add(buf, &n, .{ .name = "./dir/hard", .content = "Hello, world!\n" }); add(buf, &n, .{ .name = "./dir/link", .kind = .symbolic_link, .link_name = "../hello.txt", .mode = 0o777 }); if (f.has_long) add(buf, &n, .{ .name = "./dir/longlink", .kind = .symbolic_link, .link_name = long_target, .mode = 0o777 }); add(buf, &n, .{ .name = "./dir/thousand", .content = "x" ** 1000 }); // ./hello.txt is a hard link to ./dir/hard, and reported. if (f.has_long) add(buf, &n, .{ .name = "./" ++ unsplittable, .content = "long\n" }); return buf[0..n]; } const Log = struct { unsupported: [4]u8 = undefined, unsupported_len: usize = 0, other: usize = 0, fn report(ctx: ?*anyopaque, d: tar.Diagnostic) void { const self: *Log = @ptrCast(@alignCast(ctx.?)); switch (d) { .unsupported_entry => |u| { self.unsupported[self.unsupported_len] = u.typeflag; self.unsupported_len += 1; }, // bsdtar's PAX headers carry records this library has no use for; // they are allowed, and nothing else is. .ignored_pax_record => {}, else => self.other += 1, } } }; test "fixtures written by GNU tar and bsdtar" { for (fixtures) |f| { checkFixture(f) catch |err| { std.debug.print("while reading {s}\n", .{f.file}); return err; }; } } fn checkFixture(f: Fixture) !void { var buf: [16]Expected = undefined; const want = expected(f, &buf); var scratch: [4096]u8 = undefined; var r: Io.Reader = .fixed(f.bytes); var log: Log = .{}; var it = tar.iterator(&r, .{ .context = &log, .report = Log.report }, &scratch); for (want) |e| { const got = (try it.next()) orelse { std.debug.print("ended early; expected {s}\n", .{e.name}); return error.TestUnexpectedResult; }; try testing.expectEqualStrings(e.name, got.name); try testing.expectEqual(e.kind, got.kind); try testing.expectEqualStrings(e.link_name, got.link_name); try testing.expectEqual(e.content.len, got.size); try testing.expectEqual(e.mode, got.mode); try testing.expectEqual(1700000000, got.mtime); var content: [1024]u8 = undefined; var sink: Io.Writer = .fixed(&content); const n = try got.reader.streamRemaining(&sink); try testing.expectEqualStrings(e.content, content[0..n]); } try testing.expectEqual(null, try it.next()); const unsupported: []const u8 = if (f.has_fifo) "61" else "1"; try testing.expectEqualStrings(unsupported, log.unsupported[0..log.unsupported_len]); try testing.expectEqual(0, log.other); } test "every fixture, cut anywhere, is an error or a missing end marker" { for (fixtures) |f| { for (0..f.bytes.len) |cut| { var scratch: [4096]u8 = undefined; var r: Io.Reader = .fixed(f.bytes[0..cut]); var it = tar.iterator(&r, .ignore, &scratch); while (it.next() catch null) |e| { var sink: Io.Writer.Discarding = .init(&.{}); _ = e.reader.streamRemaining(&sink.writer) catch { try testing.expectEqual(error.TruncatedArchive, it.err.?); break; }; } } } } test "written here, read by std.tar" { var buf: [16 * 1024]u8 = undefined; var out: Io.Writer = .fixed(&buf); var scratch: [4096]u8 = undefined; var w: tar.Writer = .init(&out, &scratch); var hello: Io.Reader = .fixed("Hello, world!\n"); try w.add("hello.txt", &hello, .{ .size = 14, .mtime = 1700000000 }); try w.add("dir/", null, .{ .kind = .directory }); try w.add("dir/link", null, .{ .kind = .symbolic_link, .link_name = "../hello.txt" }); var long: Io.Reader = .fixed("long\n"); try w.add(long_dir ++ "/" ++ unsplittable, &long, .{ .size = 5 }); try w.finish(); var file_name: [std.fs.max_path_bytes]u8 = undefined; var link_name: [std.fs.max_path_bytes]u8 = undefined; var r: Io.Reader = .fixed(out.buffered()); var it: std.tar.Iterator = .init(&r, .{ .file_name_buffer = &file_name, .link_name_buffer = &link_name }); var f = (try it.next()).?; try testing.expectEqualStrings("hello.txt", f.name); try testing.expectEqual(.file, f.kind); try testing.expectEqual(14, f.size); try testing.expectEqual(0o644, f.mode); f = (try it.next()).?; try testing.expectEqualStrings("dir/", f.name); try testing.expectEqual(.directory, f.kind); f = (try it.next()).?; try testing.expectEqualStrings("dir/link", f.name); try testing.expectEqual(.sym_link, f.kind); try testing.expectEqualStrings("../hello.txt", f.link_name); f = (try it.next()).?; try testing.expectEqualStrings(long_dir ++ "/" ++ unsplittable, f.name); var content: [16]u8 = undefined; var sink: Io.Writer = .fixed(&content); try it.streamRemaining(f, &sink); try testing.expectEqualStrings("long\n", sink.buffered()); try testing.expectEqual(null, try it.next()); } test "written by std.tar, read here" { var buf: [16 * 1024]u8 = undefined; var out: Io.Writer = .fixed(&buf); var w: std.tar.Writer = .{ .underlying_writer = &out }; try w.writeFileBytes("hello.txt", "Hello, world!\n", .{ .mode = 0o600, .mtime = 1700000000 }); try w.writeDir("dir", .{ .mode = 0o755 }); try w.writeLink("dir/link", "../hello.txt", .{ .mode = 0o777 }); // Long enough that std.tar has to split it into prefix and name. try w.writeFileBytes(long_dir ++ "/" ++ long_file, "split\n", .{ .mode = 0o644 }); try out.splatByteAll(0, 1024); var scratch: [4096]u8 = undefined; var r: Io.Reader = .fixed(out.buffered()); var it = tar.iterator(&r, .ignore, &scratch); var e = (try it.next()).?; try testing.expectEqualStrings("hello.txt", e.name); try testing.expectEqual(0o600, e.mode); try testing.expectEqual(1700000000, e.mtime); try testing.expectEqualStrings("Hello, world!\n", try e.reader.take(14)); e = (try it.next()).?; try testing.expectEqual(.directory, e.kind); try testing.expectEqualStrings("dir", e.name); e = (try it.next()).?; try testing.expectEqual(.symbolic_link, e.kind); try testing.expectEqualStrings("../hello.txt", e.link_name); e = (try it.next()).?; try testing.expectEqualStrings(long_dir ++ "/" ++ long_file, e.name); try testing.expectEqualStrings("split\n", try e.reader.take(6)); try testing.expectEqual(null, try it.next()); }