import { MultiReader } from "../io/readers.ts";import { BufReader } from "../io/bufio.ts";import { assert } from "../testing/asserts.ts";
const recordSize = 512;const ustar = "ustar\u000000";
class FileReader implements Deno.Reader { private file?: Deno.File;
constructor(private filePath: string) {}
public async read(p: Uint8Array): Promise<number | null> { if (!this.file) { this.file = await Deno.open(this.filePath, { read: true }); } const res = await Deno.read(this.file.rid, p); if (res === null) { Deno.close(this.file.rid); this.file = undefined; } return res; }}
function trim(buffer: Uint8Array): Uint8Array { const index = buffer.findIndex((v): boolean => v === 0); if (index < 0) return buffer; return buffer.subarray(0, index);}
function clean(length: number): Uint8Array { const buffer = new Uint8Array(length); buffer.fill(0, 0, length - 1); return buffer;}
function pad(num: number, bytes: number, base?: number): string { const numString = num.toString(base || 8); return "000000000000".substr(numString.length + 12 - bytes) + numString;}
const ustarStructure: Array<{ field: string; length: number }> = [ { field: "fileName", length: 100, }, { field: "fileMode", length: 8, }, { field: "uid", length: 8, }, { field: "gid", length: 8, }, { field: "fileSize", length: 12, }, { field: "mtime", length: 12, }, { field: "checksum", length: 8, }, { field: "type", length: 1, }, { field: "linkName", length: 100, }, { field: "ustar", length: 8, }, { field: "owner", length: 32, }, { field: "group", length: 32, }, { field: "majorNumber", length: 8, }, { field: "minorNumber", length: 8, }, { field: "fileNamePrefix", length: 155, }, { field: "padding", length: 12, },];
function formatHeader(data: TarData): Uint8Array { const encoder = new TextEncoder(), buffer = clean(512); let offset = 0; ustarStructure.forEach(function (value): void { const entry = encoder.encode(data[value.field as keyof TarData] || ""); buffer.set(entry, offset); offset += value.length; }); return buffer;}
function parseHeader(buffer: Uint8Array): { [key: string]: Uint8Array } { const data: { [key: string]: Uint8Array } = {}; let offset = 0; ustarStructure.forEach(function (value): void { const arr = buffer.subarray(offset, offset + value.length); data[value.field] = arr; offset += value.length; }); return data;}
export interface TarData { fileName?: string; fileNamePrefix?: string; fileMode?: string; uid?: string; gid?: string; fileSize?: string; mtime?: string; checksum?: string; type?: string; ustar?: string; owner?: string; group?: string;}
export interface TarDataWithSource extends TarData { filePath?: string; reader?: Deno.Reader;}
export interface TarInfo { fileMode?: number; mtime?: number; uid?: number; gid?: number; owner?: string; group?: string;}
export interface TarOptions extends TarInfo { filePath?: string;
reader?: Deno.Reader;
contentSize?: number;}
export interface UntarOptions extends TarInfo { fileName: string;}
export class Tar { data: TarDataWithSource[]; written: number; out: Uint8Array; private blockSize: number;
constructor(recordsPerBlock?: number) { this.data = []; this.written = 0; this.blockSize = (recordsPerBlock || 20) * recordSize; this.out = clean(this.blockSize); }
async append(fn: string, opts: TarOptions): Promise<void> { if (typeof fn !== "string") { throw new Error("file name not specified"); } let fileName = fn; let fileNamePrefix: string | undefined; if (fileName.length > 100) { let i = fileName.length; while (i >= 0) { i = fileName.lastIndexOf("/", i); if (i <= 155) { fileNamePrefix = fileName.substr(0, i); fileName = fileName.substr(i + 1); break; } i--; } const errMsg = "ustar format does not allow a long file name (length of [file name" + "prefix] + / + [file name] must be shorter than 256 bytes)"; if (i < 0 || fileName.length > 100) { throw new Error(errMsg); } else { assert(fileNamePrefix != null); if (fileNamePrefix.length > 155) { throw new Error(errMsg); } } }
opts = opts || {};
let info: Deno.FileInfo | undefined; if (opts.filePath) { info = await Deno.stat(opts.filePath); }
const mode = opts.fileMode || (info && info.mode) || parseInt("777", 8) & 0xfff, mtime = Math.floor( opts.mtime ?? (info?.mtime ?? new Date()).valueOf() / 1000 ), uid = opts.uid || 0, gid = opts.gid || 0; if (typeof opts.owner === "string" && opts.owner.length >= 32) { throw new Error( "ustar format does not allow owner name length >= 32 bytes" ); } if (typeof opts.group === "string" && opts.group.length >= 32) { throw new Error( "ustar format does not allow group name length >= 32 bytes" ); }
const fileSize = info?.size ?? opts.contentSize; assert(fileSize != null, "fileSize must be set"); const tarData: TarDataWithSource = { fileName, fileNamePrefix, fileMode: pad(mode, 7), uid: pad(uid, 7), gid: pad(gid, 7), fileSize: pad(fileSize, 11), mtime: pad(mtime, 11), checksum: " ", type: "0", ustar, owner: opts.owner || "", group: opts.group || "", filePath: opts.filePath, reader: opts.reader, };
let checksum = 0; const encoder = new TextEncoder(); Object.keys(tarData) .filter((key): boolean => ["filePath", "reader"].indexOf(key) < 0) .forEach(function (key): void { checksum += encoder .encode(tarData[key as keyof TarData]) .reduce((p, c): number => p + c, 0); });
tarData.checksum = pad(checksum, 6) + "\u0000 "; this.data.push(tarData); }
getReader(): Deno.Reader { const readers: Deno.Reader[] = []; this.data.forEach((tarData): void => { let { reader } = tarData; const { filePath } = tarData; const headerArr = formatHeader(tarData); readers.push(new Deno.Buffer(headerArr)); if (!reader) { assert(filePath != null); reader = new FileReader(filePath); } readers.push(reader);
assert(tarData.fileSize != null, "fileSize must be set"); readers.push( new Deno.Buffer( clean( recordSize - (parseInt(tarData.fileSize, 8) % recordSize || recordSize) ) ) ); });
readers.push(new Deno.Buffer(clean(recordSize * 2))); return new MultiReader(...readers); }}
export class Untar { reader: BufReader; block: Uint8Array;
constructor(reader: Deno.Reader) { this.reader = new BufReader(reader); this.block = new Uint8Array(recordSize); }
async extract(writer: Deno.Writer): Promise<UntarOptions> { await this.reader.readFull(this.block); const header = parseHeader(this.block);
let checksum = 0; const encoder = new TextEncoder(), decoder = new TextDecoder("ascii"); Object.keys(header) .filter((key): boolean => key !== "checksum") .forEach(function (key): void { checksum += header[key].reduce((p, c): number => p + c, 0); }); checksum += encoder.encode(" ").reduce((p, c): number => p + c, 0);
if (parseInt(decoder.decode(header.checksum), 8) !== checksum) { throw new Error("checksum error"); }
const magic = decoder.decode(header.ustar); if (magic !== ustar) { throw new Error(`unsupported archive format: ${magic}`); }
const meta: UntarOptions = { fileName: decoder.decode(trim(header.fileName)), }; const fileNamePrefix = trim(header.fileNamePrefix); if (fileNamePrefix.byteLength > 0) { meta.fileName = decoder.decode(fileNamePrefix) + "/" + meta.fileName; } (["fileMode", "mtime", "uid", "gid"] as [ "fileMode", "mtime", "uid", "gid" ]).forEach((key): void => { const arr = trim(header[key]); if (arr.byteLength > 0) { meta[key] = parseInt(decoder.decode(arr), 8); } }); (["owner", "group"] as ["owner", "group"]).forEach((key): void => { const arr = trim(header[key]); if (arr.byteLength > 0) { meta[key] = decoder.decode(arr); } });
const len = parseInt(decoder.decode(header.fileSize), 8); let rest = len; while (rest > 0) { await this.reader.readFull(this.block); const arr = rest < recordSize ? this.block.subarray(0, rest) : this.block; await Deno.copy(new Deno.Buffer(arr), writer); rest -= recordSize; }
return meta; }}