From ce68daa10e16ef88934a6d00a301d1fd5133d19c Mon Sep 17 00:00:00 2001 From: Lovepreet Singh Date: Thu, 18 Aug 2022 11:01:22 +0000 Subject: [PATCH] Common getCompressionMethod for listTar and extractTar --- packages/cache/src/internal/constants.ts | 6 ++ packages/cache/src/internal/tar.ts | 113 ++++++++++------------- 2 files changed, 54 insertions(+), 65 deletions(-) diff --git a/packages/cache/src/internal/constants.ts b/packages/cache/src/internal/constants.ts index 2f78d326..8686a630 100644 --- a/packages/cache/src/internal/constants.ts +++ b/packages/cache/src/internal/constants.ts @@ -11,6 +11,12 @@ export enum CompressionMethod { Zstd = 'zstd' } +export enum TarOperation { + Create = 'create', + List = 'list', + Extract = 'extract' +} + // The default number of retry attempts. export const DefaultRetryAttempts = 2 diff --git a/packages/cache/src/internal/tar.ts b/packages/cache/src/internal/tar.ts index cb563a0a..34229ccf 100644 --- a/packages/cache/src/internal/tar.ts +++ b/packages/cache/src/internal/tar.ts @@ -3,7 +3,9 @@ import * as io from '@actions/io' import {existsSync, writeFileSync} from 'fs' import * as path from 'path' import * as utils from './cacheUtils' -import {CompressionMethod} from './constants' +import {CompressionMethod, TarOperation} from './constants' + +const IS_WINDOWS = process.platform === 'win32' async function getTarPath( args: string[], @@ -54,6 +56,41 @@ function getWorkingDirectory(): string { return process.env['GITHUB_WORKSPACE'] ?? process.cwd() } +// Common function for extractTar and listTar to get the compression method +function getCompressionProgram(compressionMethod: CompressionMethod): string[] { + // -d: Decompress. + // unzstd is equivalent to 'zstd -d' + // --long=#: Enables long distance matching with # bits. Maximum is 30 (1GB) on 32-bit OS and 31 (2GB) on 64-bit. + // Using 30 here because we also support 32-bit self-hosted runners. + switch (compressionMethod) { + case CompressionMethod.Zstd: + return [ + '--use-compress-program', + IS_WINDOWS ? 'zstd -d --long=30' : 'unzstd --long=30', + ] + case CompressionMethod.ZstdWithoutLong: + return [ + '--use-compress-program', + IS_WINDOWS ? 'zstd -d' : 'unzstd', + ] + default: + return ['-z'] + } +} + +export async function listTar( + archivePath: string, + compressionMethod: CompressionMethod +): Promise { + const args = [ + getCompressionProgram(compressionMethod), + '-tf', + archivePath.replace(new RegExp(`\\${path.sep}`, 'g'), '/'), + '-P' + ] + await execTar(args, compressionMethod) +} + export async function extractTar( archivePath: string, compressionMethod: CompressionMethod @@ -61,28 +98,8 @@ export async function extractTar( // Create directory to extract tar into const workingDirectory = getWorkingDirectory() await io.mkdirP(workingDirectory) - // -d: Decompress. - // unzstd is equivalent to 'zstd -d' - // --long=#: Enables long distance matching with # bits. Maximum is 30 (1GB) on 32-bit OS and 31 (2GB) on 64-bit. - // Using 30 here because we also support 32-bit self-hosted runners. - function getCompressionProgram(): string[] { - switch (compressionMethod) { - case CompressionMethod.Zstd: - if (process.platform === 'win32') { - return ['--use-compress-program', 'zstd -d --long=30'] - } - return ['--use-compress-program', 'unzstd --long=30'] - case CompressionMethod.ZstdWithoutLong: - if (process.platform === 'win32') { - return ['--use-compress-program', 'zstd -d'] - } - return ['--use-compress-program', 'unzstd'] - default: - return ['-z'] - } - } const args = [ - ...getCompressionProgram(), + getCompressionProgram(compressionMethod), '-xf', archivePath.replace(new RegExp(`\\${path.sep}`, 'g'), '/'), '-P', @@ -114,15 +131,15 @@ export async function createTar( function getCompressionProgram(): string[] { switch (compressionMethod) { case CompressionMethod.Zstd: - if (process.platform === 'win32') { - return ['--use-compress-program', 'zstd -T0 --long=30'] - } - return ['--use-compress-program', 'zstdmt --long=30'] + return [ + '--use-compress-program', + IS_WINDOWS ? 'zstd -T0 --long=30' : 'zstdmt --long=30' + ] case CompressionMethod.ZstdWithoutLong: - if (process.platform === 'win32') { - return ['--use-compress-program', 'zstd -T0'] - } - return ['--use-compress-program', 'zstdmt'] + return [ + '--use-compress-program', + IS_WINDOWS ? 'zstd -T0' : 'zstdmt' + ] default: return ['-z'] } @@ -141,38 +158,4 @@ export async function createTar( manifestFilename ] await execTar(args, compressionMethod, archiveFolder) -} - -export async function listTar( - archivePath: string, - compressionMethod: CompressionMethod -): Promise { - // -d: Decompress. - // unzstd is equivalent to 'zstd -d' - // --long=#: Enables long distance matching with # bits. - // Maximum is 30 (1GB) on 32-bit OS and 31 (2GB) on 64-bit. - // Using 30 here because we also support 32-bit self-hosted runners. - function getCompressionProgram(): string[] { - switch (compressionMethod) { - case CompressionMethod.Zstd: - if (process.platform === 'win32') { - return ['--use-compress-program', 'zstd -d --long=30'] - } - return ['--use-compress-program', 'unzstd --long=30'] - case CompressionMethod.ZstdWithoutLong: - if (process.platform === 'win32') { - return ['--use-compress-program', 'zstd -d'] - } - return ['--use-compress-program', 'unzstd'] - default: - return ['-z'] - } - } - const args = [ - ...getCompressionProgram(), - '-tf', - archivePath.replace(new RegExp(`\\${path.sep}`, 'g'), '/'), - '-P' - ] - await execTar(args, compressionMethod) -} +} \ No newline at end of file