feat(install): write UDIF/DMG-style containers
This commit is contained in:
1 parent
ac08887d8f
commit
1caeab70fa
7 files changed
+213
-4
No files matched your search
@@ -598,6 +598,116 @@ write_wix_source(const std::filesystem::path &output, const InstallOptions &opti
|
||||
return Platform::atomic_write_file(output, xml, true);
|
||||
}
|
||||
|
||||
// ── UDIF (DMG) trailer ─────────────────────────────────────────────────────────────────────
|
||||
// A DMG is a data fork (the disk image payload) followed by a 512-byte "koly" trailer. This
|
||||
// writer produces a valid UDIF container whose payload is the deterministic tar of the package;
|
||||
// macOS DiskImageMounter additionally needs a filesystem payload (HFS+/APFS), which is macOS-only
|
||||
// work. Checksums are CRC-32 as in the UDIF specification.
|
||||
|
||||
class Crc32 {
|
||||
public:
|
||||
void update(std::string_view data) {
|
||||
for (const char ch : data) {
|
||||
crc_ ^= static_cast<std::uint32_t>(static_cast<unsigned char>(ch));
|
||||
for (int bit = 0; bit < 8; ++bit) {
|
||||
crc_ = (crc_ >> 1) ^ (0xEDB88320u & (0u - (crc_ & 1u)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] std::uint32_t value() const { return ~crc_; }
|
||||
|
||||
private:
|
||||
std::uint32_t crc_{0xFFFFFFFFu};
|
||||
};
|
||||
|
||||
void put_be32(std::string &out, std::size_t offset, std::uint32_t value) {
|
||||
for (int i = 3; i >= 0; --i) {
|
||||
out[offset + static_cast<std::size_t>(i)] = static_cast<char>(value & 0xFFu);
|
||||
value >>= 8;
|
||||
}
|
||||
}
|
||||
|
||||
void put_be64(std::string &out, std::size_t offset, std::uint64_t value) {
|
||||
for (int i = 7; i >= 0; --i) {
|
||||
out[offset + static_cast<std::size_t>(i)] = static_cast<char>(value & 0xFFu);
|
||||
value >>= 8;
|
||||
}
|
||||
}
|
||||
|
||||
std::string make_koly_trailer(std::uint64_t data_fork_length, std::uint32_t data_crc,
|
||||
std::string_view segment_seed) {
|
||||
std::string koly(512, '\0');
|
||||
koly[0] = 'k';
|
||||
koly[1] = 'o';
|
||||
koly[2] = 'l';
|
||||
koly[3] = 'y';
|
||||
put_be32(koly, 4, 4); // version
|
||||
put_be32(koly, 8, 512); // header size
|
||||
put_be32(koly, 12, 1); // flags
|
||||
put_be64(koly, 16, 0); // running data fork offset
|
||||
put_be64(koly, 24, 0); // data fork offset
|
||||
put_be64(koly, 32, data_fork_length);
|
||||
put_be64(koly, 40, 0); // resource fork offset
|
||||
put_be64(koly, 48, 0); // resource fork length
|
||||
put_be32(koly, 56, 1); // segment number
|
||||
put_be32(koly, 60, 1); // segment count
|
||||
const std::string digest = Util::sha256_hex(segment_seed);
|
||||
for (std::size_t i = 0; i < 16; ++i) {
|
||||
koly[64 + i] = static_cast<char>(std::stoul(digest.substr(i * 2, 2), nullptr, 16));
|
||||
}
|
||||
// Data fork checksum: type 2 (CRC-32), 32-bit.
|
||||
put_be32(koly, 80, 2);
|
||||
put_be32(koly, 84, 32);
|
||||
put_be32(koly, 88, data_crc);
|
||||
put_be64(koly, 216, 0); // no XML plist
|
||||
put_be64(koly, 224, 0);
|
||||
// Master checksum mirrors the data fork checksum.
|
||||
put_be32(koly, 352, 2);
|
||||
put_be32(koly, 356, 32);
|
||||
put_be32(koly, 360, data_crc);
|
||||
put_be32(koly, 488, 1); // image variant
|
||||
put_be64(koly, 492, (data_fork_length + 511u) / 512u); // sector count
|
||||
return koly;
|
||||
}
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error>
|
||||
write_dmg_archive(const std::filesystem::path &output, const std::filesystem::path &payload) {
|
||||
std::ifstream in{payload, std::ios::binary};
|
||||
if (!in) {
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::io_error,
|
||||
std::format("cannot open DMG payload '{}'", payload.string()))};
|
||||
}
|
||||
std::ofstream out{output, std::ios::binary | std::ios::trunc};
|
||||
if (!out) {
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::io_error,
|
||||
std::format("cannot create DMG '{}'", output.string()))};
|
||||
}
|
||||
Crc32 crc;
|
||||
std::uint64_t length = 0;
|
||||
std::array<char, 64 * 1024> buffer{};
|
||||
while (in) {
|
||||
in.read(buffer.data(), static_cast<std::streamsize>(buffer.size()));
|
||||
const auto count = in.gcount();
|
||||
if (count > 0) {
|
||||
out.write(buffer.data(), count);
|
||||
crc.update(std::string_view{buffer.data(), static_cast<std::size_t>(count)});
|
||||
length += static_cast<std::uint64_t>(count);
|
||||
}
|
||||
}
|
||||
const std::string koly = make_koly_trailer(length, crc.value(), payload.filename().string());
|
||||
out.write(koly.data(), static_cast<std::streamsize>(koly.size()));
|
||||
out.flush();
|
||||
if (!out) {
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::io_error,
|
||||
std::format("failed writing DMG '{}'", output.string()))};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
// Debian package: ar container with debian-binary, control.tar and data.tar (uncompressed).
|
||||
Void::Result::Result<void, Void::Result::Error>
|
||||
write_deb_package(const std::filesystem::path &output, const InstallOptions &options,
|
||||
@@ -1042,6 +1152,16 @@ install_project(const Graph::ProjectDescription &description, const Graph::Build
|
||||
!packed) {
|
||||
return std::unexpected{packed.error()};
|
||||
}
|
||||
} else if (options.archive_format == "dmg") {
|
||||
const auto payload = stage_root / "installer-payload.tar";
|
||||
auto archived =
|
||||
Package::create_deterministic_tar(payload, std::move(archive_entries), 0);
|
||||
if (!archived) {
|
||||
return std::unexpected{archived.error()};
|
||||
}
|
||||
if (auto packed = write_dmg_archive(*options.archive_path, payload); !packed) {
|
||||
return std::unexpected{packed.error()};
|
||||
}
|
||||
} else {
|
||||
const bool archive_as_script = options.archive_format == "sh";
|
||||
const auto archive_target = archive_as_script
|
||||
|
||||
Reference in new issue
Block a user