const std = @import("std");
const repeat = @import("zit").repeat;
const support = @import("support.zig");
const addManaged = support.addManaged;
const chmodFile = support.chmodFile;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const freeResult = support.freeResult;
const initRepo = support.initRepo;
const readFile = support.readFile;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const writeFile = support.writeFile;
/// One zip central-directory entry, read back out of the archive. Only the
/// central directory is parsed: it is the record a reader trusts for metadata,
/// and the only place a member's mode appears.
const ZipEntry = struct {
name: []const u8,
method: u16,
crc: u32,
compressed_size: u32,
uncompressed_size: u32,
/// The Unix `st_mode` from the high 16 bits of the external attributes.
unix_mode: u32,
};
/// Walk a zip's central directory. Hand rolled for the same reason the tar
/// reader is: the point is to assert the bytes this repository writes, and a
/// library agreeing with the writer in the same wrong direction would hide the
/// bug worth catching.
fn readZipCentral(gpa: std.mem.Allocator, bytes: []const u8) ![]ZipEntry {
// Find the end-of-central-directory record by scanning back for its
// signature. No archive comment is written, so it is the last 22 bytes;
// scanning anyway keeps the reader honest if that ever changes.
if (bytes.len < 22) return error.TooShort;
var eocd: usize = bytes.len - 22;
while (true) {
if (std.mem.readInt(u32, bytes[eocd..][0..4], .little) == 0x06054b50) break;
if (eocd == 0) return error.NoEndOfCentralDirectory;
eocd -= 1;
}
const count = std.mem.readInt(u16, bytes[eocd + 10 ..][0..2], .little);
var off: usize = std.mem.readInt(u32, bytes[eocd + 16 ..][0..4], .little);
var out: std.ArrayListUnmanaged(ZipEntry) = .empty;
errdefer out.deinit(gpa);
var i: u16 = 0;
while (i < count) : (i += 1) {
if (std.mem.readInt(u32, bytes[off..][0..4], .little) != 0x02014b50)
return error.BadCentralHeader;
const name_len = std.mem.readInt(u16, bytes[off + 28 ..][0..2], .little);
const extra_len = std.mem.readInt(u16, bytes[off + 30 ..][0..2], .little);
const comment_len = std.mem.readInt(u16, bytes[off + 32 ..][0..2], .little);
try out.append(gpa, .{
.name = bytes[off + 46 ..][0..name_len],
.method = std.mem.readInt(u16, bytes[off + 10 ..][0..2], .little),
.crc = std.mem.readInt(u32, bytes[off + 16 ..][0..4], .little),
.compressed_size = std.mem.readInt(u32, bytes[off + 20 ..][0..4], .little),
.uncompressed_size = std.mem.readInt(u32, bytes[off + 24 ..][0..4], .little),
.unix_mode = std.mem.readInt(u32, bytes[off + 38 ..][0..4], .little) >> 16,
});
off += 46 + @as(usize, name_len) + extra_len + comment_len;
}
return out.toOwnedSlice(gpa);
}
test "cli zip: modes ride the external attributes, and the CRCs are real" {
// Goal: the zip half of the archive verbs. Same member set and selection as tarball, but
// the mode lives one field deeper — in the central directory's external
// attributes, honoured only when `version made by` says Unix — so a writer
// can produce a perfectly readable archive in which every file is 0644.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try tmp.dir.createDir(io, "src", .default_dir);
try writeFile(io, tmp.dir, "README", "readme\n");
try writeFile(io, tmp.dir, "src/main.c", "int main(){}\n");
try writeFile(io, tmp.dir, "configure", "#!/bin/sh\n");
// Long and highly repetitive, so deflate is a real win and the writer's
// "compress only when it helps" rule has both branches exercised here.
const compressible = repeat("the same line over and over again\n", 400);
try writeFile(io, tmp.dir, "big.txt", compressible);
try chmodFile(io, tmp.dir, "configure", 0o755);
try addManaged(gpa, io, tmp.dir, &.{ "README", "src/main.c", "configure", "big.txt" });
var c1 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "base" });
defer freeResult(gpa, &c1);
try expectExit(c1.term, 0);
var made = try runZit(gpa, io, tmp.dir, &.{ "zip", "tip", "out.zip", "--name", "rel-1" });
defer freeResult(gpa, &made);
try expectExitDiag(made.term, 0, made);
const archive_bytes = try readFile(gpa, io, tmp.dir, "out.zip");
defer gpa.free(archive_bytes);
const entries = try readZipCentral(gpa, archive_bytes);
defer gpa.free(entries);
try std.testing.expectEqual(@as(usize, 4), entries.len);
try std.testing.expectEqualStrings("rel-1/README", entries[0].name);
try std.testing.expectEqualStrings("rel-1/big.txt", entries[1].name);
try std.testing.expectEqualStrings("rel-1/configure", entries[2].name);
try std.testing.expectEqualStrings("rel-1/src/main.c", entries[3].name);
// S_IFREG with the F card's permission. The executable is the case that
// ships a release whose ./configure will not run.
try std.testing.expectEqual(@as(u32, 0o100644), entries[0].unix_mode);
try std.testing.expectEqual(@as(u32, 0o100755), entries[2].unix_mode);
// The CRC is the archive's own integrity claim; a wrong one makes every
// reader reject the member, so it is checked against an independent
// computation rather than assumed.
try std.testing.expectEqual(@import("zit").crc32.Crc32.hash("readme\n"), entries[0].crc);
try std.testing.expectEqual(@as(u32, 7), entries[0].uncompressed_size);
// Compression is applied only when it helps: the repetitive file deflates,
// the seven-byte one would GROW and is therefore stored.
try std.testing.expectEqual(@as(u16, 8), entries[1].method); // deflate
try std.testing.expectEqual(@as(u32, compressible.len), entries[1].uncompressed_size);
try std.testing.expect(entries[1].compressed_size < entries[1].uncompressed_size / 4);
try std.testing.expectEqual(@as(u16, 0), entries[0].method); // stored
// Reproducible, exactly as the tarball is.
var again = try runZit(gpa, io, tmp.dir, &.{ "zip", "tip", "again.zip", "--name", "rel-1" });
defer freeResult(gpa, &again);
try expectExitDiag(again.term, 0, again);
const again_bytes = try readFile(gpa, io, tmp.dir, "again.zip");
defer gpa.free(again_bytes);
try std.testing.expectEqualSlices(u8, archive_bytes, again_bytes);
// The shared surface is genuinely shared: selection, --list, and the
// top-directory rule come from one place, so one probe per verb is enough.
var listed = try runZit(gpa, io, tmp.dir, &.{ "zip", "tip", "--list", "--exclude", "src/*" });
defer freeResult(gpa, &listed);
try expectExitDiag(listed.term, 0, listed);
try std.testing.expect(std.mem.indexOf(u8, listed.stdout, "src/main.c") == null);
try std.testing.expect(std.mem.indexOf(u8, listed.stdout, "/README") != null);
}