Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:only create tmp directory when compression is on. #767

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 26 additions & 22 deletions lib/file_transfer_agent/file_transfer_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -838,6 +842,6 @@ function FileTransferAgent(context) {
}

//TODO SNOW-992387: Create a function to renew expired client
function renewExpiredClient() {}
function renewExpiredClient() { }

module.exports = FileTransferAgent;
Loading