refactor(deb): retain decoded GNU tar metadata (#17382)

This commit is contained in:
Vincent Koc
2026-12-06 12:43:36 +08:00
committed by GitHub
parent d634b7c7f2
commit 1e59087c47
+18 -27
View File
@@ -418,10 +418,8 @@ const parseTar = (
const files = new Map<string, Uint8Array>();
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<typeof tarText> | undefined;
let longlink: ReturnType<typeof tarText> | 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<Record<(typeof PAX_KEYS)[number], string>> = {};
@@ -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();