Skip to content

Commit

Permalink
Common getCompressionMethod for listTar and extractTar
Browse files Browse the repository at this point in the history
  • Loading branch information
lvpx committed Aug 18, 2022
1 parent 98a4069 commit ce68daa
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 65 deletions.
6 changes: 6 additions & 0 deletions packages/cache/src/internal/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
113 changes: 48 additions & 65 deletions packages/cache/src/internal/tar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[],
Expand Down Expand Up @@ -54,35 +56,50 @@ 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<void> {
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
): Promise<void> {
// 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',
Expand Down Expand Up @@ -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']
}
Expand All @@ -141,38 +158,4 @@ export async function createTar(
manifestFilename
]
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)
}
}

0 comments on commit ce68daa

Please sign in to comment.