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
+29 -21
+29 -21
View File
@@ -251,29 +251,39 @@ 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, for (const command of commands) {
cwd?: string try {
await exec(command, undefined, {
cwd,
env: {...(process.env as object), MSYS: 'winsymlinks:nativestrict'}
})
} catch (error) {
throw new Error(
`${command.split(' ')[0]} failed with error: ${error?.message}`
)
}
}
}
async function execReadCommands(
archivePath: string,
compressionMethod: CompressionMethod,
type: 'list' | 'extract'
): Promise<void> { ): Promise<void> {
const {commands, requiresTempDirectory} = await getCommands(
compressionMethod,
type,
archivePath
)
// The Windows BSD-tar fallback decompresses into cache.tar before reading // The Windows BSD-tar fallback decompresses into cache.tar before reading
// it. Isolate that intermediate for each list/extract operation, including // it. Isolate that intermediate for each list/extract operation, including
// debug listings during saves. Creation already has its own archive folder. // debug listings during saves.
const tempDirectory = requiresTempDirectory const tempDirectory = requiresTempDirectory
? await utils.createTempDirectory() ? await utils.createTempDirectory()
: undefined : undefined
try { try {
for (const command of commands) { await execCommands(commands, tempDirectory)
try {
await exec(command, undefined, {
cwd: tempDirectory ?? cwd,
env: {...(process.env as object), MSYS: 'winsymlinks:nativestrict'}
})
} catch (error) {
throw new Error(
`${command.split(' ')[0]} failed with error: ${error?.message}`
)
}
}
} 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)
} }