From 2b1cfb6e1cf38c50cc925ff18c0d82b0b0efab71 Mon Sep 17 00:00:00 2001 From: JohnFraleigh Date: Fri, 16 Feb 2024 11:19:18 -0500 Subject: [PATCH] fix:only create tmp directory when compression is on --- .../file_transfer_agent.js | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/lib/file_transfer_agent/file_transfer_agent.js b/lib/file_transfer_agent/file_transfer_agent.js index 719949dad..5520c7d66 100644 --- a/lib/file_transfer_agent/file_transfer_agent.js +++ b/lib/file_transfer_agent/file_transfer_agent.js @@ -353,11 +353,13 @@ function FileTransferAgent(context) { */ async function uploadOneFile(meta) { meta['realSrcFilePath'] = meta['srcFilePath']; - const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'tmp')); - meta['tmpDir'] = tmpDir; + try { if (meta['requireCompress']) { - const result = await SnowflakeFileUtil.compressFileWithGZIP(meta['srcFilePath'], meta['tmpDir']); + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'tmp')); + const result = await SnowflakeFileUtil.compressFileWithGZIP(meta['srcFilePath'], tmpDir); + + meta['tmpDir'] = tmpDir; meta['realSrcFilePath'] = result.name; } const result = await SnowflakeFileUtil.getDigestAndSizeForFile(meta['realSrcFilePath']); @@ -378,25 +380,27 @@ function FileTransferAgent(context) { meta['errorDetails'] = err.toString(); meta['errorDetails'] += ` file=${meta['srcFileName']}, real file=${meta['realSrcFilePath']}`; } finally { - // Remove all files inside tmp folder - const matchingFileNames = glob.sync(path.join(meta['tmpDir'], meta['srcFileName'] + '*')); - for (const matchingFileName of matchingFileNames) { - await new Promise((resolve, reject) => { - fs.unlink(matchingFileName, err => { - if (err) { - reject(err); - } - resolve(); + // Remove all files inside tmp folder if compression is set + if (meta['requireCompress']) { + const matchingFileNames = glob.sync(path.join(meta['tmpDir'], meta['srcFileName'] + '*')); + for (const matchingFileName of matchingFileNames) { + await new Promise((resolve, reject) => { + fs.unlink(matchingFileName, err => { + if (err) { + reject(err); + } + resolve(); + }); }); - }); - } - // Delete tmp folder - fs.rmdir(meta['tmpDir'], (err) => { - if (err) { - throw (err); } + // Delete tmp folder + fs.rmdir(meta['tmpDir'], (err) => { + if (err) { + throw (err); + } - }); + }); + } } return meta; @@ -562,7 +566,7 @@ function FileTransferAgent(context) { const quoteIndex = command.substring(startIndex).indexOf('\''); let endIndex = spaceIndex; if (quoteIndex !== -1 && quoteIndex < spaceIndex) { - endIndex = quoteIndex; + endIndex = quoteIndex; } const filePath = command.substring(startIndex, startIndex + endIndex); return filePath; @@ -757,7 +761,7 @@ function FileTransferAgent(context) { if (sourceCompression === 'auto_detect') { autoDetect = true; - } else if (sourceCompression === typeof('undefined')) { + } else if (sourceCompression === typeof ('undefined')) { autoDetect = false; } else { userSpecifiedSourceCompression = fileCompressionType.lookupByMimeSubType(sourceCompression); @@ -838,6 +842,6 @@ function FileTransferAgent(context) { } //TODO SNOW-992387: Create a function to renew expired client -function renewExpiredClient() {} +function renewExpiredClient() { } module.exports = FileTransferAgent;