1
0
Fork 0

Common getCompressionMethod for listTar and extractTar

pull/1152/head
Lovepreet Singh 2022-08-18 11:01:22 +00:00
parent 98a4069558
commit ce68daa10e
2 changed files with 54 additions and 65 deletions

View File

@ -11,6 +11,12 @@ export enum CompressionMethod {
Zstd = 'zstd' Zstd = 'zstd'
} }
export enum TarOperation {
Create = 'create',
List = 'list',
Extract = 'extract'
}
// The default number of retry attempts. // The default number of retry attempts.
export const DefaultRetryAttempts = 2 export const DefaultRetryAttempts = 2

View File

@ -3,7 +3,9 @@ import * as io from '@actions/io'
import {existsSync, writeFileSync} from 'fs' import {existsSync, writeFileSync} from 'fs'
import * as path from 'path' import * as path from 'path'
import * as utils from './cacheUtils' import * as utils from './cacheUtils'
import {CompressionMethod} from './constants' import {CompressionMethod, TarOperation} from './constants'
const IS_WINDOWS = process.platform === 'win32'
async function getTarPath( async function getTarPath(
args: string[], args: string[],
@ -54,6 +56,41 @@ function getWorkingDirectory(): string {
return process.env['GITHUB_WORKSPACE'] ?? process.cwd() 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<void> {
const args = [
getCompressionProgram(compressionMethod),
'-tf',
archivePath.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
'-P'
]
await execTar(args, compressionMethod)
}
export async function extractTar( export async function extractTar(
archivePath: string, archivePath: string,
compressionMethod: CompressionMethod compressionMethod: CompressionMethod
@ -61,28 +98,8 @@ 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)
// -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 = [ const args = [
...getCompressionProgram(), getCompressionProgram(compressionMethod),
'-xf', '-xf',
archivePath.replace(new RegExp(`\\${path.sep}`, 'g'), '/'), archivePath.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
'-P', '-P',
@ -114,15 +131,15 @@ export async function createTar(
function getCompressionProgram(): string[] { function getCompressionProgram(): string[] {
switch (compressionMethod) { switch (compressionMethod) {
case CompressionMethod.Zstd: case CompressionMethod.Zstd:
if (process.platform === 'win32') { return [
return ['--use-compress-program', 'zstd -T0 --long=30'] '--use-compress-program',
} IS_WINDOWS ? 'zstd -T0 --long=30' : 'zstdmt --long=30'
return ['--use-compress-program', 'zstdmt --long=30'] ]
case CompressionMethod.ZstdWithoutLong: case CompressionMethod.ZstdWithoutLong:
if (process.platform === 'win32') { return [
return ['--use-compress-program', 'zstd -T0'] '--use-compress-program',
} IS_WINDOWS ? 'zstd -T0' : 'zstdmt'
return ['--use-compress-program', 'zstdmt'] ]
default: default:
return ['-z'] return ['-z']
} }
@ -141,38 +158,4 @@ export async function createTar(
manifestFilename manifestFilename
] ]
await execTar(args, compressionMethod, archiveFolder) await execTar(args, compressionMethod, archiveFolder)
} }
export async function listTar(
archivePath: string,
compressionMethod: CompressionMethod
): Promise<void> {
// -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)
}