refactor(cache): scope tar scratch directories to reads

Keep execCommands as a plain executor that honors its cwd argument. Move
scratch-directory allocation and cleanup into a shared list/extract
helper, leaving archive creation on the caller-supplied directory.
This commit is contained in:
qmuntal committed 2026-09-17 16:55:25 +02:00
1 parent fce8715913
commit cd705bb67f
1 file changed
+25 -17
+25 -17
View File
@@ -251,21 +251,11 @@ async function getCompressionProgram(
} }
// Executes all commands as separate processes // Executes all commands as separate processes
async function execCommands( async function execCommands(commands: string[], cwd?: string): Promise<void> {
{commands, requiresTempDirectory}: TarCommands,
cwd?: string
): Promise<void> {
// The Windows BSD-tar fallback decompresses into cache.tar before reading
// it. Isolate that intermediate for each list/extract operation, including
// debug listings during saves. Creation already has its own archive folder.
const tempDirectory = requiresTempDirectory
? await utils.createTempDirectory()
: undefined
try {
for (const command of commands) { for (const command of commands) {
try { try {
await exec(command, undefined, { await exec(command, undefined, {
cwd: tempDirectory ?? cwd, cwd,
env: {...(process.env as object), MSYS: 'winsymlinks:nativestrict'} env: {...(process.env as object), MSYS: 'winsymlinks:nativestrict'}
}) })
} catch (error) { } catch (error) {
@@ -274,6 +264,26 @@ async function execCommands(
) )
} }
} }
}
async function execReadCommands(
archivePath: string,
compressionMethod: CompressionMethod,
type: 'list' | 'extract'
): Promise<void> {
const {commands, requiresTempDirectory} = await getCommands(
compressionMethod,
type,
archivePath
)
// The Windows BSD-tar fallback decompresses into cache.tar before reading
// it. Isolate that intermediate for each list/extract operation, including
// debug listings during saves.
const tempDirectory = requiresTempDirectory
? await utils.createTempDirectory()
: undefined
try {
await execCommands(commands, tempDirectory)
} finally { } finally {
if (tempDirectory) { if (tempDirectory) {
await io.rmRF(tempDirectory) await io.rmRF(tempDirectory)
@@ -286,8 +296,7 @@ export async function listTar(
archivePath: string, archivePath: string,
compressionMethod: CompressionMethod compressionMethod: CompressionMethod
): Promise<void> { ): Promise<void> {
const commands = await getCommands(compressionMethod, 'list', archivePath) await execReadCommands(archivePath, compressionMethod, 'list')
await execCommands(commands)
} }
// Extract a tar // Extract a tar
@@ -298,8 +307,7 @@ export async function extractTar(
// Create directory to extract tar into // Create directory to extract tar into
const workingDirectory = getWorkingDirectory() const workingDirectory = getWorkingDirectory()
await io.mkdirP(workingDirectory) await io.mkdirP(workingDirectory)
const commands = await getCommands(compressionMethod, 'extract', archivePath) await execReadCommands(archivePath, compressionMethod, 'extract')
await execCommands(commands)
} }
// Create a tar // Create a tar
@@ -313,6 +321,6 @@ export async function createTar(
path.join(archiveFolder, ManifestFilename), path.join(archiveFolder, ManifestFilename),
sourceDirectories.join('\n') sourceDirectories.join('\n')
) )
const commands = await getCommands(compressionMethod, 'create') const {commands} = await getCommands(compressionMethod, 'create')
await execCommands(commands, archiveFolder) await execCommands(commands, archiveFolder)
} }