From 1e59087c47ff6dc4ec250a2a536092a4afa41960 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sun, 6 Dec 2026 12:43:36 +0800 Subject: [PATCH] refactor(deb): retain decoded GNU tar metadata (#17382) --- src/deb/DebPackageParser.ts | 45 +++++++++++++++---------------------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/src/deb/DebPackageParser.ts b/src/deb/DebPackageParser.ts index fe5c9cc8fa..a34f955da0 100644 --- a/src/deb/DebPackageParser.ts +++ b/src/deb/DebPackageParser.ts @@ -418,10 +418,8 @@ const parseTar = ( const files = new Map(); const result = { entries, files, undecodableNames: 0 }; let offset = 0; - let longname: string | null = null; - let longnameUndecodable = false; - let longlinkUndecodable = false; - let longlink: string | null = null; + let longname: ReturnType | undefined; + let longlink: ReturnType | undefined; // Only the PAX keys this reader uses are kept, so a run of headers with // fresh keys cannot grow or repeatedly copy an object. const pax: Partial> = {}; @@ -508,7 +506,7 @@ const parseTar = ( const rawName = tarText(header.subarray(0, 100)); const headerName = prefix.text.length > 0 ? `${prefix.text}/${rawName.text}` : rawName.text; - const rawPath = pax.path ?? globalPax.path ?? longname ?? headerName; + const rawPath = pax.path ?? globalPax.path ?? longname?.text ?? headerName; const storedSize = extension ? size : tarStoredDataBytes(typeFlag, rawPath, size); @@ -534,17 +532,13 @@ const parseTar = ( ), }; } - if (typeFlag === TAR_TYPEFLAGS.gnuLongName) { + if ( + typeFlag === TAR_TYPEFLAGS.gnuLongName || + typeFlag === TAR_TYPEFLAGS.gnuLongLink + ) { const decoded = tarText(payload); - longname = decoded.text; - longnameUndecodable = decoded.undecodable; - offset = dataStart + tarPaddedSize(size); - continue; - } - if (typeFlag === TAR_TYPEFLAGS.gnuLongLink) { - const decoded = tarText(payload); - longlink = decoded.text; - longlinkUndecodable = decoded.undecodable; + if (typeFlag === TAR_TYPEFLAGS.gnuLongName) longname = decoded; + else longlink = decoded; offset = dataStart + tarPaddedSize(size); continue; } @@ -640,22 +634,21 @@ const parseTar = ( pax[key as keyof typeof pax] !== undefined ? paxUndecodable.has(key) : globalUndecodable.has(key); - const path = logicalTarPath(fileFields.path ?? longname ?? headerName); + const path = logicalTarPath( + fileFields.path ?? longname?.text ?? headerName, + ); const headerLink = tarText(header.subarray(157, 257)); - const linkText = fileFields.linkpath ?? longlink ?? headerLink.text; + const linkText = fileFields.linkpath ?? longlink?.text ?? headerLink.text; const link = linkText.length === 0 ? null : linkText; const nameUndecodable = fileFields.path !== undefined ? undecodableField("path") - : longname === null - ? rawName.undecodable || prefix.undecodable - : longnameUndecodable; + : (longname?.undecodable ?? + (rawName.undecodable || prefix.undecodable)); const linkUndecodable = fileFields.linkpath !== undefined ? undecodableField("linkpath") - : longlink === null - ? headerLink.undecodable - : longlinkUndecodable; + : (longlink?.undecodable ?? headerLink.undecodable); if (nameUndecodable || linkUndecodable) result.undecodableNames += 1; const mode = nonNegativeTarNumber(header.subarray(100, 108)); // PAX owner records override the fixed fields, which can hold shortened @@ -688,10 +681,8 @@ const parseTar = ( link_target: type === "symlink" || type === "hardlink" ? link : null, }); if (type === "file") files.set(path.length === 0 ? "." : path, payload); - longname = null; - longlink = null; - longnameUndecodable = false; - longlinkUndecodable = false; + longname = undefined; + longlink = undefined; for (const key of PAX_KEYS) delete pax[key]; extensionHeaders = 0; paxUndecodable.clear();