Skip to content

Commit a5d9a3b

Browse files
Aiqiao YanAiqiao Yan
authored andcommitted
Address PR feedback
1 parent 97f7baa commit a5d9a3b

6 files changed

Lines changed: 73 additions & 35 deletions

File tree

‎__tests__/tar.test.ts‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ jest.mock("@actions/exec");
1212
jest.mock("@actions/io");
1313

1414
const IS_WINDOWS = process.platform === "win32";
15+
const IS_64OS = process.platform != "win32" || process.arch === "x64";
1516

1617
function getTempDir(): string {
1718
return path.join(__dirname, "_temp", "tar");
@@ -46,12 +47,13 @@ test("zstd extract tar", async () => {
4647
const tarPath = IS_WINDOWS
4748
? `${process.env["windir"]}\\System32\\tar.exe`
4849
: "tar";
50+
const zstdOptions = IS_64OS ? "zstd -d --long=31" : "zstd -d --long=30";
4951
expect(execMock).toHaveBeenCalledTimes(1);
5052
expect(execMock).toHaveBeenCalledWith(
51-
`${tarPath}`,
53+
`"${tarPath}"`,
5254
[
5355
"--use-compress-program",
54-
"zstd -d",
56+
zstdOptions,
5557
"-xf",
5658
IS_WINDOWS ? archivePath.replace(/\\/g, "/") : archivePath,
5759
"-P",
@@ -78,7 +80,7 @@ test("gzip extract tar", async () => {
7880
: "tar";
7981
expect(execMock).toHaveBeenCalledTimes(1);
8082
expect(execMock).toHaveBeenCalledWith(
81-
`${tarPath}`,
83+
`"${tarPath}"`,
8284
[
8385
"-z",
8486
"-xf",
@@ -107,7 +109,7 @@ test("gzip extract GNU tar on windows", async () => {
107109
expect(isGnuMock).toHaveBeenCalledTimes(1);
108110
expect(execMock).toHaveBeenCalledTimes(1);
109111
expect(execMock).toHaveBeenCalledWith(
110-
`tar`,
112+
`"tar"`,
111113
[
112114
"-z",
113115
"-xf",
@@ -140,13 +142,14 @@ test("zstd create tar", async () => {
140142
const tarPath = IS_WINDOWS
141143
? `${process.env["windir"]}\\System32\\tar.exe`
142144
: "tar";
145+
const zstdOptions = IS_64OS ? "zstd -T0 --long=31" : "zstd -T0 --long=30";
143146

144147
expect(execMock).toHaveBeenCalledTimes(1);
145148
expect(execMock).toHaveBeenCalledWith(
146-
`${tarPath}`,
149+
`"${tarPath}"`,
147150
[
148151
"--use-compress-program",
149-
"zstd -T0",
152+
zstdOptions,
150153
"-cf",
151154
IS_WINDOWS
152155
? CacheFilename.Zstd.replace(/\\/g, "/")
@@ -184,7 +187,7 @@ test("gzip create tar", async () => {
184187

185188
expect(execMock).toHaveBeenCalledTimes(1);
186189
expect(execMock).toHaveBeenCalledWith(
187-
`${tarPath}`,
190+
`"${tarPath}"`,
188191
[
189192
"-z",
190193
"-cf",

‎dist/restore/index.js‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2237,10 +2237,9 @@ function createHttpClient() {
22372237
return new http_client_1.HttpClient("actions/cache", [bearerCredentialHandler], getRequestOptions());
22382238
}
22392239
function getCacheVersion(compressionMethod) {
2240+
const components = [core.getInput(constants_1.Inputs.Path, { required: true })].concat(compressionMethod == constants_1.CompressionMethod.Zstd ? [compressionMethod] : []);
22402241
// Add salt to cache version to support breaking changes in cache entry
2241-
const components = [core.getInput(constants_1.Inputs.Path, { required: true })].concat(compressionMethod == constants_1.CompressionMethod.Zstd
2242-
? [compressionMethod, versionSalt]
2243-
: versionSalt);
2242+
components.push(versionSalt);
22442243
return crypto
22452244
.createHash("sha256")
22462245
.update(components.join("|"))
@@ -3320,7 +3319,7 @@ function unlinkFile(path) {
33203319
return util.promisify(fs.unlink)(path);
33213320
}
33223321
exports.unlinkFile = unlinkFile;
3323-
function checkVersion(app) {
3322+
function getVersion(app) {
33243323
return __awaiter(this, void 0, void 0, function* () {
33253324
core.debug(`Checking ${app} --version`);
33263325
let versionOutput = "";
@@ -3344,7 +3343,7 @@ function checkVersion(app) {
33443343
}
33453344
function getCompressionMethod() {
33463345
return __awaiter(this, void 0, void 0, function* () {
3347-
const versionOutput = yield checkVersion("zstd");
3346+
const versionOutput = yield getVersion("zstd");
33483347
return versionOutput.toLowerCase().includes("zstd command line interface")
33493348
? constants_1.CompressionMethod.Zstd
33503349
: constants_1.CompressionMethod.Gzip;
@@ -3359,7 +3358,7 @@ function getCacheFileName(compressionMethod) {
33593358
exports.getCacheFileName = getCacheFileName;
33603359
function useGnuTar() {
33613360
return __awaiter(this, void 0, void 0, function* () {
3362-
const versionOutput = yield checkVersion("tar");
3361+
const versionOutput = yield getVersion("tar");
33633362
return versionOutput.toLowerCase().includes("gnu tar");
33643363
});
33653364
}
@@ -5068,7 +5067,7 @@ function execTar(args, cwd) {
50685067
var _a;
50695068
return __awaiter(this, void 0, void 0, function* () {
50705069
try {
5071-
yield exec_1.exec(`${yield getTarPath(args)}`, args, { cwd: cwd });
5070+
yield exec_1.exec(`"${yield getTarPath(args)}"`, args, { cwd: cwd });
50725071
}
50735072
catch (error) {
50745073
throw new Error(`Tar failed with error: ${(_a = error) === null || _a === void 0 ? void 0 : _a.message}`);
@@ -5079,14 +5078,22 @@ function getWorkingDirectory() {
50795078
var _a;
50805079
return _a = process.env["GITHUB_WORKSPACE"], (_a !== null && _a !== void 0 ? _a : process.cwd());
50815080
}
5081+
function isOS64() {
5082+
return process.platform != "win32" || process.arch === "x64";
5083+
}
50825084
function extractTar(archivePath, compressionMethod) {
50835085
return __awaiter(this, void 0, void 0, function* () {
50845086
// Create directory to extract tar into
50855087
const workingDirectory = getWorkingDirectory();
50865088
yield io.mkdirP(workingDirectory);
5089+
// --d: Decompress.
5090+
// --long=#: Enables long distance matching with # bits. Maximum is 30 (1GB) on 32-bit OS and 31 (2GB) on 64-bit.
50875091
const args = [
50885092
...(compressionMethod == constants_1.CompressionMethod.Zstd
5089-
? ["--use-compress-program", "zstd -d"]
5093+
? [
5094+
"--use-compress-program",
5095+
isOS64() ? "zstd -d --long=31" : "zstd -d --long=30"
5096+
]
50905097
: ["-z"]),
50915098
"-xf",
50925099
archivePath.replace(new RegExp("\\" + path.sep, "g"), "/"),
@@ -5105,10 +5112,14 @@ function createTar(archiveFolder, sourceDirectories, compressionMethod) {
51055112
const cacheFileName = utils.getCacheFileName(compressionMethod);
51065113
fs_1.writeFileSync(path.join(archiveFolder, manifestFilename), sourceDirectories.join("\n"));
51075114
// -T#: Compress using # working thread. If # is 0, attempt to detect and use the number of physical CPU cores.
5115+
// --long=#: Enables long distance matching with # bits. Maximum is 30 (1GB) on 32-bit OS and 31 (2GB) on 64-bit.
51085116
const workingDirectory = getWorkingDirectory();
51095117
const args = [
51105118
...(compressionMethod == constants_1.CompressionMethod.Zstd
5111-
? ["--use-compress-program", "zstd -T0"]
5119+
? [
5120+
"--use-compress-program",
5121+
isOS64() ? "zstd -T0 --long=31" : "zstd -T0 --long=30"
5122+
]
51125123
: ["-z"]),
51135124
"-cf",
51145125
cacheFileName.replace(new RegExp("\\" + path.sep, "g"), "/"),

‎dist/save/index.js‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2237,10 +2237,9 @@ function createHttpClient() {
22372237
return new http_client_1.HttpClient("actions/cache", [bearerCredentialHandler], getRequestOptions());
22382238
}
22392239
function getCacheVersion(compressionMethod) {
2240+
const components = [core.getInput(constants_1.Inputs.Path, { required: true })].concat(compressionMethod == constants_1.CompressionMethod.Zstd ? [compressionMethod] : []);
22402241
// Add salt to cache version to support breaking changes in cache entry
2241-
const components = [core.getInput(constants_1.Inputs.Path, { required: true })].concat(compressionMethod == constants_1.CompressionMethod.Zstd
2242-
? [compressionMethod, versionSalt]
2243-
: versionSalt);
2242+
components.push(versionSalt);
22442243
return crypto
22452244
.createHash("sha256")
22462245
.update(components.join("|"))
@@ -3320,7 +3319,7 @@ function unlinkFile(path) {
33203319
return util.promisify(fs.unlink)(path);
33213320
}
33223321
exports.unlinkFile = unlinkFile;
3323-
function checkVersion(app) {
3322+
function getVersion(app) {
33243323
return __awaiter(this, void 0, void 0, function* () {
33253324
core.debug(`Checking ${app} --version`);
33263325
let versionOutput = "";
@@ -3344,7 +3343,7 @@ function checkVersion(app) {
33443343
}
33453344
function getCompressionMethod() {
33463345
return __awaiter(this, void 0, void 0, function* () {
3347-
const versionOutput = yield checkVersion("zstd");
3346+
const versionOutput = yield getVersion("zstd");
33483347
return versionOutput.toLowerCase().includes("zstd command line interface")
33493348
? constants_1.CompressionMethod.Zstd
33503349
: constants_1.CompressionMethod.Gzip;
@@ -3359,7 +3358,7 @@ function getCacheFileName(compressionMethod) {
33593358
exports.getCacheFileName = getCacheFileName;
33603359
function useGnuTar() {
33613360
return __awaiter(this, void 0, void 0, function* () {
3362-
const versionOutput = yield checkVersion("tar");
3361+
const versionOutput = yield getVersion("tar");
33633362
return versionOutput.toLowerCase().includes("gnu tar");
33643363
});
33653364
}
@@ -5045,7 +5044,7 @@ function execTar(args, cwd) {
50455044
var _a;
50465045
return __awaiter(this, void 0, void 0, function* () {
50475046
try {
5048-
yield exec_1.exec(`${yield getTarPath(args)}`, args, { cwd: cwd });
5047+
yield exec_1.exec(`"${yield getTarPath(args)}"`, args, { cwd: cwd });
50495048
}
50505049
catch (error) {
50515050
throw new Error(`Tar failed with error: ${(_a = error) === null || _a === void 0 ? void 0 : _a.message}`);
@@ -5056,14 +5055,22 @@ function getWorkingDirectory() {
50565055
var _a;
50575056
return _a = process.env["GITHUB_WORKSPACE"], (_a !== null && _a !== void 0 ? _a : process.cwd());
50585057
}
5058+
function isOS64() {
5059+
return process.platform != "win32" || process.arch === "x64";
5060+
}
50595061
function extractTar(archivePath, compressionMethod) {
50605062
return __awaiter(this, void 0, void 0, function* () {
50615063
// Create directory to extract tar into
50625064
const workingDirectory = getWorkingDirectory();
50635065
yield io.mkdirP(workingDirectory);
5066+
// --d: Decompress.
5067+
// --long=#: Enables long distance matching with # bits. Maximum is 30 (1GB) on 32-bit OS and 31 (2GB) on 64-bit.
50645068
const args = [
50655069
...(compressionMethod == constants_1.CompressionMethod.Zstd
5066-
? ["--use-compress-program", "zstd -d"]
5070+
? [
5071+
"--use-compress-program",
5072+
isOS64() ? "zstd -d --long=31" : "zstd -d --long=30"
5073+
]
50675074
: ["-z"]),
50685075
"-xf",
50695076
archivePath.replace(new RegExp("\\" + path.sep, "g"), "/"),
@@ -5082,10 +5089,14 @@ function createTar(archiveFolder, sourceDirectories, compressionMethod) {
50825089
const cacheFileName = utils.getCacheFileName(compressionMethod);
50835090
fs_1.writeFileSync(path.join(archiveFolder, manifestFilename), sourceDirectories.join("\n"));
50845091
// -T#: Compress using # working thread. If # is 0, attempt to detect and use the number of physical CPU cores.
5092+
// --long=#: Enables long distance matching with # bits. Maximum is 30 (1GB) on 32-bit OS and 31 (2GB) on 64-bit.
50855093
const workingDirectory = getWorkingDirectory();
50865094
const args = [
50875095
...(compressionMethod == constants_1.CompressionMethod.Zstd
5088-
? ["--use-compress-program", "zstd -T0"]
5096+
? [
5097+
"--use-compress-program",
5098+
isOS64() ? "zstd -T0 --long=31" : "zstd -T0 --long=30"
5099+
]
50895100
: ["-z"]),
50905101
"-cf",
50915102
cacheFileName.replace(new RegExp("\\" + path.sep, "g"), "/"),

‎src/cacheHttpClient.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ function createHttpClient(): HttpClient {
8686
}
8787

8888
export function getCacheVersion(compressionMethod?: CompressionMethod): string {
89-
// Add salt to cache version to support breaking changes in cache entry
9089
const components = [core.getInput(Inputs.Path, { required: true })].concat(
91-
compressionMethod == CompressionMethod.Zstd
92-
? [compressionMethod, versionSalt]
93-
: versionSalt
90+
compressionMethod == CompressionMethod.Zstd ? [compressionMethod] : []
9491
);
9592

93+
// Add salt to cache version to support breaking changes in cache entry
94+
components.push(versionSalt);
95+
9696
return crypto
9797
.createHash("sha256")
9898
.update(components.join("|"))

‎src/tar.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async function getTarPath(args: string[]): Promise<string> {
2222

2323
async function execTar(args: string[], cwd?: string): Promise<void> {
2424
try {
25-
await exec(`${await getTarPath(args)}`, args, { cwd: cwd });
25+
await exec(`"${await getTarPath(args)}"`, args, { cwd: cwd });
2626
} catch (error) {
2727
throw new Error(`Tar failed with error: ${error?.message}`);
2828
}
@@ -32,16 +32,25 @@ function getWorkingDirectory(): string {
3232
return process.env["GITHUB_WORKSPACE"] ?? process.cwd();
3333
}
3434

35+
function isOS64(): boolean {
36+
return process.platform != "win32" || process.arch === "x64";
37+
}
38+
3539
export async function extractTar(
3640
archivePath: string,
3741
compressionMethod: CompressionMethod
3842
): Promise<void> {
3943
// Create directory to extract tar into
4044
const workingDirectory = getWorkingDirectory();
4145
await io.mkdirP(workingDirectory);
46+
// --d: Decompress.
47+
// --long=#: Enables long distance matching with # bits. Maximum is 30 (1GB) on 32-bit OS and 31 (2GB) on 64-bit.
4248
const args = [
4349
...(compressionMethod == CompressionMethod.Zstd
44-
? ["--use-compress-program", "zstd -d"]
50+
? [
51+
"--use-compress-program",
52+
isOS64() ? "zstd -d --long=31" : "zstd -d --long=30"
53+
]
4554
: ["-z"]),
4655
"-xf",
4756
archivePath.replace(new RegExp("\\" + path.sep, "g"), "/"),
@@ -65,10 +74,14 @@ export async function createTar(
6574
sourceDirectories.join("\n")
6675
);
6776
// -T#: Compress using # working thread. If # is 0, attempt to detect and use the number of physical CPU cores.
77+
// --long=#: Enables long distance matching with # bits. Maximum is 30 (1GB) on 32-bit OS and 31 (2GB) on 64-bit.
6878
const workingDirectory = getWorkingDirectory();
6979
const args = [
7080
...(compressionMethod == CompressionMethod.Zstd
71-
? ["--use-compress-program", "zstd -T0"]
81+
? [
82+
"--use-compress-program",
83+
isOS64() ? "zstd -T0 --long=31" : "zstd -T0 --long=30"
84+
]
7285
: ["-z"]),
7386
"-cf",
7487
cacheFileName.replace(new RegExp("\\" + path.sep, "g"), "/"),

‎src/utils/actionUtils.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export function unlinkFile(path: fs.PathLike): Promise<void> {
124124
return util.promisify(fs.unlink)(path);
125125
}
126126

127-
async function checkVersion(app: string): Promise<string> {
127+
async function getVersion(app: string): Promise<string> {
128128
core.debug(`Checking ${app} --version`);
129129
let versionOutput = "";
130130
try {
@@ -148,7 +148,7 @@ async function checkVersion(app: string): Promise<string> {
148148
}
149149

150150
export async function getCompressionMethod(): Promise<CompressionMethod> {
151-
const versionOutput = await checkVersion("zstd");
151+
const versionOutput = await getVersion("zstd");
152152
return versionOutput.toLowerCase().includes("zstd command line interface")
153153
? CompressionMethod.Zstd
154154
: CompressionMethod.Gzip;
@@ -161,6 +161,6 @@ export function getCacheFileName(compressionMethod: CompressionMethod): string {
161161
}
162162

163163
export async function useGnuTar(): Promise<boolean> {
164-
const versionOutput = await checkVersion("tar");
164+
const versionOutput = await getVersion("tar");
165165
return versionOutput.toLowerCase().includes("gnu tar");
166166
}

0 commit comments

Comments
 (0)