From 6a99fb043d9ebcbd1ca0bc252802bf0fd7af14c7 Mon Sep 17 00:00:00 2001 From: Yahav Itzhak Date: Thu, 9 Aug 2018 18:17:00 +0300 Subject: [PATCH] Escape backslashes in filespecs on windows #5 --- .../downloadArtifacts.js | 7 ++-- ArtifactoryGenericUpload/uploadArtifacts.js | 7 ++-- jfrog-utils/jutils.js | 42 +++++++++++++++---- jfrog-utils/package.json | 1 + 4 files changed, 44 insertions(+), 13 deletions(-) diff --git a/ArtifactoryGenericDownload/downloadArtifacts.js b/ArtifactoryGenericDownload/downloadArtifacts.js index ee5dae58..b6888cd1 100644 --- a/ArtifactoryGenericDownload/downloadArtifacts.js +++ b/ArtifactoryGenericDownload/downloadArtifacts.js @@ -17,10 +17,11 @@ function RunTaskCbk(cliPath) { let fileSpec = tl.getInput("fileSpec", false); let collectBuildInfo = tl.getBoolInput("collectBuildInfo"); - console.log("Using file spec:"); - console.log(fileSpec); - // Write provided fileSpec to file try { + fileSpec = utils.fixWindowsPaths(fileSpec); + console.log("Using file spec:"); + console.log(fileSpec); + // Write provided fileSpec to file tl.writeFile(specPath, fileSpec); } catch (ex) { tl.setResult(tl.TaskResult.Failed, ex); diff --git a/ArtifactoryGenericUpload/uploadArtifacts.js b/ArtifactoryGenericUpload/uploadArtifacts.js index 12be48c6..bcb45d27 100644 --- a/ArtifactoryGenericUpload/uploadArtifacts.js +++ b/ArtifactoryGenericUpload/uploadArtifacts.js @@ -17,10 +17,11 @@ function RunTaskCbk(cliPath) { let fileSpec = tl.getInput("fileSpec", false); let collectBuildInfo = tl.getBoolInput("collectBuildInfo"); - console.log("Using file spec:"); - console.log(fileSpec); - // Write provided fileSpec to file try { + fileSpec = utils.fixWindowsPaths(fileSpec); + console.log("Using file spec:"); + console.log(fileSpec); + // Write provided fileSpec to file tl.writeFile(specPath, fileSpec); } catch (ex) { tl.setResult(tl.TaskResult.Failed, ex); diff --git a/jfrog-utils/jutils.js b/jfrog-utils/jutils.js index 98925aa3..9fa91384 100644 --- a/jfrog-utils/jutils.js +++ b/jfrog-utils/jutils.js @@ -1,4 +1,5 @@ -const fs = require('fs'); +const os = require('os'); +const fs = require('fs-extra'); const tl = require('vsts-task-lib/task'); const checksumStream = require('checksum-stream'); const path = require('path'); @@ -26,7 +27,8 @@ module.exports = { quote: quote, addArtifactoryCredentials: addArtifactoryCredentials, addStringParam: addStringParam, - addBoolParam: addBoolParam + addBoolParam: addBoolParam, + fixWindowsPaths: fixWindowsPaths }; function executeCliTask(runTaskFunc) { @@ -150,14 +152,13 @@ function downloadCli(attemptNumber) { }).on('error', handleError) .on('end', () => { if (res.statusCode >= 200 && res.statusCode < 300) { - fs.copyFile(cliTmpPath, versionedCliPath, () => { + fs.move(cliTmpPath, versionedCliPath).then(() => { if (!process.platform.startsWith("win")) { - fs.chmodSync(versionedCliPath, 0o555) + fs.chmodSync(versionedCliPath, 0o555); } console.log("Finished downloading jfrog cli"); - fs.unlinkSync(cliTmpPath); resolve(); - }); + }) } }) ).pipe( @@ -190,4 +191,31 @@ function getFileName() { executable += ".exe" } return executable -} \ No newline at end of file +} + +/** + * Escape single backslashes in fileSpec. + * / -> // + * // -> // + * @param fileSpec (String) - The file spec to escape + * @returns fileSpec (String) - The file spec after escaping + */ +function fixWindowsPaths(fileSpec) { + if (os.type() === "Windows_NT") { + fileSpec = fileSpec.replace(/([^\\])\\(?!\\)/g, '$1\\\\'); + validateSpecWithoutRegex(fileSpec); + return fileSpec + } + return fileSpec; +} + +function validateSpecWithoutRegex(fileSpec) { + let files = JSON.parse(fileSpec)["files"]; + for (const file of Object.keys(files)) { + let values = files[file]; + let regexp = values["regexp"]; + if (regexp && regexp.toLowerCase() === "true") { + throw ("The File Spec includes 'regexp: true' which is currently not supported."); + } + } +} diff --git a/jfrog-utils/package.json b/jfrog-utils/package.json index 49067e6d..18386928 100644 --- a/jfrog-utils/package.json +++ b/jfrog-utils/package.json @@ -5,6 +5,7 @@ "main": "jutils.js", "dependencies": { "vsts-task-lib": "^2.6.0", + "fs-extra": "^7.0.0", "checksum-stream": "^1.0.3", "request": "^2.87.0", "request-promise": "^4.2.2"