Files
2026-11-14 15:31:15 -06:00

352 lines
11 KiB
Plaintext

#! @file archives.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains what packages are written as: a tar archive, a zip file holding
#! its files stored rather than compressed, and an `ar` archive, which a
#! Debian package is. Each is written whole into memory, every entry owned by
#! root and dated alike, so the same stage writes the same bytes. Compressing
#! them is the system's `gzip`, `xz`, and `zstd`.
import std::io;
import system;
module blossom::pipeline;
#! A file or folder of a package.
pub struct entry {
pub var name: string; #! Its path within the package, with `/`, without one at the end.
pub var source: path; #! The file it's read from, or empty for a folder.
pub var folder: bool;
pub var mode: u32; #! Who may read, write, and run it.
}
#! Lists what a stage holds, as entries under a root: the root's own folders
#! first, then each folder before what it holds, in order by name.
#! @param stage The stage.
#! @param root Where its files go in the package, as `usr` or
#! `hello-1.0.0-linux-x86_64`, or empty for its top.
#! @returns The entries.
pub def staged(stage: &path, root: strview): vector<entry> {
var made := vector::<entry>::new();
var above := string::new(nothing {});
for part in root.split('/') {
continue if part.length == 0;
if above.length != 0 {
above.push('/');
}
above.append(part);
made.append(entry { name = string::copy_of(&above), folder = true, mode = 0o755 });
}
walked(stage, above.span, &mut made);
return made;
}
# Adds what a folder holds, in order by name, each folder before what it holds.
def walked(folder: &path, under: strview, into: &mut vector<entry>): void {
var names := vector::<string>::new();
var listing := io::directory::open(folder) else (_) { return; };
while listing.next() is some(each) {
names.append(string::from(each.name, nothing {}));
}
names.sort_by(def(a: &string, b: &string): bool { return a.span.compare(b.span) < 0; });
for &name in names {
val at := folder.join(name.span);
val text := under.length != 0 ? $"{under}/{name.span}" : string::copy_of(name);
val found := io::info(&at) else (_) { continue; };
if found.kind == file_kind::directory {
into.append(entry { name = string::copy_of(&text), folder = true, mode = 0o755 });
walked(&at, text.span, into);
} else {
into.append(entry {
name = string::copy_of(&text),
source = path::copy_of(&at),
mode = (found.permissions & 0o111) != 0 ? 0o755 : 0o644
});
}
}
}
#! A tar archive, as POSIX's ustar writes one, with GNU's long names for a path
#! too long to split between a header's name and prefix.
pub struct tar {
pub var bytes: string;
pub var mtime: i64; #! When every entry was written, in seconds since 1970.
}
#! Adds a folder.
#! @param this The archive.
#! @param name Its path, which `/` is added to.
pub def tar::folder(&mut this, name: strview): void {
val named := $"{name}/";
this.header(named.span, 0o755, 0, '5');
}
#! Adds a file.
#! @param this The archive.
#! @param name Its path.
#! @param data What it holds.
#! @param mode Who may read, write, and run it.
pub def tar::file(&mut this, name: strview, data: span<u8>, mode: u32): void {
this.header(name, mode, data.length, '0');
this.bytes.append(data);
this.pad();
}
#! Ends the archive with its two empty blocks.
#! @param this The archive.
#! @returns Its bytes.
pub def tar::finish(&mut this): strview {
for _ in 0..1024 {
this.bytes.push(0);
}
return this.bytes.span;
}
# Writes an entry's header, after a long name's own entry where it needs one.
def tar::header(&mut this, name: strview, mode: u32, size: u64, kind: u8): void {
var split := name.length;
if name.length > 100 {
# The last `/` leaving a name of 100 bytes or fewer, with 155 or fewer
# before it.
split = 0;
for k in 0..name.length {
if name[k] == '/' && name.length - k - 1 <= 100 && k <= 155 && k > 0 {
split = k;
break;
}
}
if split == 0 {
val longer := $"{name}\0";
this.header("././@LongLink", 0o644, longer.length, 'L');
this.bytes.append(longer.span);
this.pad();
split = name.length;
}
}
var block: u8[512];
for k in 0..512 {
block[k] = 0;
}
val base := split == name.length ? name : name.substr(split + 1, name.length - split - 1);
val prefix := split == name.length ? strview::blank() : name.substr(0, split);
put(&mut block, 0, base.substr(0, 100));
put_octal(&mut block, 100, 8, mode as u64);
put_octal(&mut block, 108, 8, 0);
put_octal(&mut block, 116, 8, 0);
put_octal(&mut block, 124, 12, size);
put_octal(&mut block, 136, 12, this.mtime as u64);
for k in 148..156 {
block[k] = ' ';
}
block[156] = kind;
put(&mut block, 257, "ustar");
put(&mut block, 263, "00");
put(&mut block, 265, "root");
put(&mut block, 297, "root");
put(&mut block, 345, prefix);
var sum := 0u;
for k in 0..512 {
sum += block[k] as u64;
}
put_octal(&mut block, 148, 7, sum);
block[155] = ' ';
this.bytes.append(strview::over((&block) as *u8, 512));
}
# Pads the archive to a whole block.
def tar::pad(&mut this): void {
while this.bytes.length % 512 != 0 {
this.bytes.push(0);
}
}
# Writes text into a header from a byte.
def put(block: &mut u8[512], at: u64, text: strview): void {
for k in 0..text.length {
block[at + k] = text[k];
}
}
# Writes a number in octal into a header, in a field of a width: zeros before
# it, and a zero byte after.
def put_octal(block: &mut u8[512], at: u64, width: u64, value: u64): void {
var left := value;
var k := width - 1;
block[at + k] = 0;
while k > 0 {
k--;
block[at + k] = ('0' + (left & 7) as u8);
left = left >> 3;
}
}
#! A zip file whose files are stored as they are, rather than compressed, with
#! who may run each in the Unix bits of its attributes.
pub struct zip {
pub var bytes: string;
pub var central: string; #! The central directory, written after the files.
pub var count: u64;
pub var mtime: i64; #! When every entry was written, in seconds since 1970.
}
#! Adds a folder.
#! @param this The zip file.
#! @param name Its path, which `/` is added to.
pub def zip::folder(&mut this, name: strview): void {
val named := $"{name}/";
this.add(named.span, strview::blank(), 0o040000 | 0o755, 0x10);
}
#! Adds a file.
#! @param this The zip file.
#! @param name Its path.
#! @param data What it holds.
#! @param mode Who may read, write, and run it.
pub def zip::file(&mut this, name: strview, data: span<u8>, mode: u32): void {
this.add(name, data, 0o100000 | mode, 0);
}
# Adds an entry: its header, its bytes, and its record in the central directory.
def zip::add(&mut this, name: strview, data: span<u8>, unix: u32, dos: u32): void {
val offset := this.bytes.length;
val crc := crc32(data);
val stamp := dos_stamp(this.mtime);
put_u32(&mut this.bytes, 0x04034b50);
put_u16(&mut this.bytes, 20);
put_u16(&mut this.bytes, 0x0800); # Names are UTF-8.
put_u16(&mut this.bytes, 0);
put_u32(&mut this.bytes, stamp);
put_u32(&mut this.bytes, crc);
put_u32(&mut this.bytes, data.length as u32);
put_u32(&mut this.bytes, data.length as u32);
put_u16(&mut this.bytes, name.length as u16);
put_u16(&mut this.bytes, 0);
this.bytes.append(name);
this.bytes.append(data);
put_u32(&mut this.central, 0x02014b50);
put_u16(&mut this.central, (3 << 8) | 20); # Made on Unix.
put_u16(&mut this.central, 20);
put_u16(&mut this.central, 0x0800);
put_u16(&mut this.central, 0);
put_u32(&mut this.central, stamp);
put_u32(&mut this.central, crc);
put_u32(&mut this.central, data.length as u32);
put_u32(&mut this.central, data.length as u32);
put_u16(&mut this.central, name.length as u16);
put_u16(&mut this.central, 0);
put_u16(&mut this.central, 0);
put_u16(&mut this.central, 0);
put_u16(&mut this.central, 0);
put_u32(&mut this.central, (unix << 16) | dos);
put_u32(&mut this.central, offset as u32);
this.central.append(name);
this.count++;
}
#! Ends the zip file with its central directory.
#! @param this The zip file.
#! @returns Its bytes.
pub def zip::finish(&mut this): strview {
val start := this.bytes.length;
this.bytes.append(this.central.span);
put_u32(&mut this.bytes, 0x06054b50);
put_u16(&mut this.bytes, 0);
put_u16(&mut this.bytes, 0);
put_u16(&mut this.bytes, this.count as u16);
put_u16(&mut this.bytes, this.count as u16);
put_u32(&mut this.bytes, this.central.length as u32);
put_u32(&mut this.bytes, start as u32);
put_u16(&mut this.bytes, 0);
return this.bytes.span;
}
# A time as MS-DOS keeps it, as zip files do: the date in the high half, the
# time in the low, in two-second steps, from 1980 on.
def dos_stamp(seconds: i64): u32 {
val at := seconds < 315532800 ? 315532800 : seconds;
val days := at / 86400;
val left := at % 86400;
val date := civil_of(days);
val day := ((date.year - 1980) as u32 << 9) | (date.month as u32 << 5) | date.day as u32;
val time := ((left / 3600) as u32 << 11) | (((left % 3600) / 60) as u32 << 5) | ((left % 60) / 2) as u32;
return (day << 16) | time;
}
# A day of the Gregorian calendar.
struct civil {
var year: i64;
var month: i64;
var day: i64;
}
# The date a number of days after 1970-01-01 is, as Howard Hinnant's
# `civil_from_days` works it out.
def civil_of(days: i64): civil {
val z := days + 719468;
val era := (z >= 0 ? z : z - 146096) / 146097;
val doe := z - era * 146097;
val yoe := (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
val doy := doe - (365 * yoe + yoe / 4 - yoe / 100);
val mp := (5 * doy + 2) / 153;
val day := doy - (153 * mp + 2) / 5 + 1;
val month := mp < 10 ? mp + 3 : mp - 9;
return civil { year = yoe + era * 400 + (month <= 2 ? 1 : 0), month = month, day = day };
}
#! An `ar` archive, as a Debian package is: its signature, then each member
#! with a header of 60 bytes.
pub struct ar {
pub var bytes: string;
pub var mtime: i64;
}
#! Starts an archive.
#! @param mtime When every member was written, in seconds since 1970.
#! @returns The archive, holding its signature.
pub def ar::new(mtime: i64): ar {
return ar { bytes = string::from("!<arch>\n", nothing {}), mtime = mtime };
}
#! Adds a member.
#! @param this The archive.
#! @param name Its name, of 16 bytes or fewer.
#! @param data What it holds.
pub def ar::member(&mut this, name: strview, data: span<u8>): void {
this.bytes.append(padded(name, 16).span);
this.bytes.append(padded($"{this.mtime}".span, 12).span);
this.bytes.append(padded("0", 6).span);
this.bytes.append(padded("0", 6).span);
this.bytes.append(padded("100644", 8).span);
this.bytes.append(padded($"{data.length}".span, 10).span);
this.bytes.append("`\n");
this.bytes.append(data);
if data.length % 2 != 0 {
this.bytes.push('\n');
}
}
# Text with spaces after it, to a width.
def padded(text: strview, width: u64): string {
var made := string::from(text, nothing {});
while made.length < width {
made.push(' ');
}
return made;
}
# Adds a number of two bytes, the low first.
def put_u16(into: &mut string, value: u32): void {
into.push((value & 0xff) as u8);
into.push(((value >> 8) & 0xff) as u8);
}
# Adds a number of four bytes, the low first.
def put_u32(into: &mut string, value: u32): void {
put_u16(into, value & 0xffff);
put_u16(into, value >> 16);
}