2026-04-03 17:21:07 +09:00
|
|
|
import { spawn } from "bun"
|
|
|
|
|
|
|
|
|
|
import type { ArchiveEntry } from "../archive-entry-validator"
|
2026-04-04 14:27:46 +09:00
|
|
|
import { log } from "../logger"
|
|
|
|
|
|
|
|
|
|
const MAX_UNPARSED_TAR_LINE_RATIO = 0.1
|
|
|
|
|
const MAX_UNPARSED_TAR_LINE_COUNT = 5
|
2026-04-03 17:21:07 +09:00
|
|
|
|
|
|
|
|
function parseTarListedZipEntry(line: string): ArchiveEntry | null {
|
|
|
|
|
const match = line.match(
|
|
|
|
|
/^([^\s])\S*\s+\d+\s+\S+\s+\S+\s+\d+\s+\w+\s+\d+\s+(?:\d{2}:\d{2}|\d{4})\s+(.*)$/
|
|
|
|
|
)
|
|
|
|
|
if (!match) {
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const [, rawType, rawEntryPath] = match
|
2026-04-04 01:16:37 +09:00
|
|
|
if (rawType === "l" || rawType === "h") {
|
2026-04-03 17:21:07 +09:00
|
|
|
const arrowIndex = rawEntryPath.lastIndexOf(" -> ")
|
|
|
|
|
return {
|
|
|
|
|
path: arrowIndex === -1 ? rawEntryPath : rawEntryPath.slice(0, arrowIndex),
|
2026-04-04 01:16:37 +09:00
|
|
|
type: rawType === "l" ? "symlink" : "hardlink",
|
2026-04-03 17:21:07 +09:00
|
|
|
linkPath: arrowIndex === -1 ? undefined : rawEntryPath.slice(arrowIndex + 4),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
path: rawEntryPath,
|
|
|
|
|
type: rawType === "d" ? "directory" : "file",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 14:27:46 +09:00
|
|
|
function validateParsedTarListing(
|
|
|
|
|
totalLineCount: number,
|
|
|
|
|
unparsedLines: string[]
|
|
|
|
|
): void {
|
|
|
|
|
if (unparsedLines.length === 0) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const unparsedLineRatio = unparsedLines.length / totalLineCount
|
|
|
|
|
if (
|
|
|
|
|
unparsedLineRatio > MAX_UNPARSED_TAR_LINE_RATIO ||
|
|
|
|
|
unparsedLines.length > MAX_UNPARSED_TAR_LINE_COUNT
|
|
|
|
|
) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`zip entry listing failed: tar output format drift detected (${unparsedLines.length}/${totalLineCount} lines unparsed)`
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function parseTarListingOutput(stdout: string): ArchiveEntry[] {
|
|
|
|
|
const listingLines = stdout
|
|
|
|
|
.split(/\r?\n/)
|
|
|
|
|
.map(line => line.trim())
|
|
|
|
|
.filter(Boolean)
|
|
|
|
|
|
|
|
|
|
if (listingLines.length === 0) {
|
|
|
|
|
return []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const parsedEntries: ArchiveEntry[] = []
|
|
|
|
|
const unparsedLines: string[] = []
|
|
|
|
|
|
|
|
|
|
for (const listingLine of listingLines) {
|
|
|
|
|
const parsedEntry = parseTarListedZipEntry(listingLine)
|
|
|
|
|
if (parsedEntry === null) {
|
|
|
|
|
unparsedLines.push(listingLine)
|
|
|
|
|
log("warning: unparsed tar listing line", { line: listingLine })
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parsedEntries.push(parsedEntry)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
validateParsedTarListing(listingLines.length, unparsedLines)
|
|
|
|
|
|
|
|
|
|
return parsedEntries
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-03 17:21:07 +09:00
|
|
|
export async function listZipEntriesWithTar(
|
|
|
|
|
archivePath: string
|
|
|
|
|
): Promise<ArchiveEntry[]> {
|
|
|
|
|
const proc = spawn(["tar", "-tvf", archivePath], {
|
|
|
|
|
stdout: "pipe",
|
|
|
|
|
stderr: "pipe",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const [exitCode, stdout, stderr] = await Promise.all([
|
|
|
|
|
proc.exited,
|
|
|
|
|
new Response(proc.stdout).text(),
|
|
|
|
|
new Response(proc.stderr).text(),
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
if (exitCode !== 0) {
|
|
|
|
|
throw new Error(`zip entry listing failed (exit ${exitCode}): ${stderr}`)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 14:27:46 +09:00
|
|
|
return parseTarListingOutput(stdout)
|
2026-04-03 17:21:07 +09:00
|
|
|
}
|