From 9525afddd3f510a7dbd81c719d53a89cdb4b9cf5 Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Sun, 29 Jan 2017 19:56:46 -0700 Subject: [PATCH] * Created new npm module: temptmp: This replaces node-temp usage & solves global temp file cleanup issue with concept of temp "sessions" --- core/file_area.js | 18 ++++++++---------- core/scanner_tossers/ftn_bso.js | 25 +++++++++++-------------- core/transfer_file.js | 17 +++++++---------- mods/upload.js | 10 ++++++---- package.json | 2 +- 5 files changed, 33 insertions(+), 39 deletions(-) diff --git a/core/file_area.js b/core/file_area.js index ab847b05..d0e94e10 100644 --- a/core/file_area.js +++ b/core/file_area.js @@ -9,6 +9,7 @@ const FileEntry = require('./file_entry.js'); const FileDb = require('./database.js').dbs.file; const ArchiveUtil = require('./archive_util.js'); const CRC32 = require('./crc.js').CRC32; +const Log = require('./logger.js').log; // deps const _ = require('lodash'); @@ -16,7 +17,7 @@ const async = require('async'); const fs = require('fs'); const crypto = require('crypto'); const paths = require('path'); -const temp = require('temp').track(); // track() cleans up temp dir/files for us +const temptmp = require('temptmp').createTrackedSession('file_area'); const iconv = require('iconv-lite'); exports.isInternalArea = isInternalArea; @@ -298,7 +299,7 @@ function populateFileEntryWithArchive(fileEntry, filePath, stepInfo, iterator, c return callback(null, [] ); } - temp.mkdir('enigextract-', (err, tempDir) => { + temptmp.mkdir( { prefix : 'enigextract-' }, (err, tempDir) => { if(err) { return callback(err); } @@ -338,14 +339,11 @@ function populateFileEntryWithArchive(fileEntry, filePath, stepInfo, iterator, c return next(null); }); }, () => { - // cleanup, but don't wait... - /* - :TODO: fix global temp cleanup issue!!! - - temp.cleanup( err => { - // :TODO: Log me! - });*/ - + // cleanup but don't wait + temptmp.cleanup( paths => { + // note: don't use client logger here - may not be avail + Log.debug( { paths : paths, sessionId : temptmp.sessionId }, 'Cleaned up temporary files' ); + }); return callback(null); }); }, diff --git a/core/scanner_tossers/ftn_bso.js b/core/scanner_tossers/ftn_bso.js index 85fad2f7..856722b5 100644 --- a/core/scanner_tossers/ftn_bso.js +++ b/core/scanner_tossers/ftn_bso.js @@ -18,7 +18,7 @@ const paths = require('path'); const async = require('async'); const fs = require('fs'); const later = require('later'); -const temp = require('temp').track(); // track() cleans up temp dir/files for us +const temptmp = require('temptmp').createTrackedSession('ftn_bso'); const assert = require('assert'); const gaze = require('gaze'); const fse = require('fs-extra'); @@ -1153,14 +1153,14 @@ function FTNMessageScanTossModule() { }; this.createTempDirectories = function(cb) { - temp.mkdir('enigftnexport-', (err, tempDir) => { + temptmp.mkdir( { prefix : 'enigftnexport-' }, (err, tempDir) => { if(err) { return cb(err); } self.exportTempDir = tempDir; - temp.mkdir('enigftnimport-', (err, tempDir) => { + temptmp.mkdir( { prefix : 'enigftnimport-' }, (err, tempDir) => { self.importTempDir = tempDir; cb(err); @@ -1290,21 +1290,18 @@ FTNMessageScanTossModule.prototype.shutdown = function(cb) { // // Clean up temp dir/files we created // - /* - :TODO: fix global temp cleanup issue!!! - - temp.cleanup((err, stats) => { - const fullStats = Object.assign(stats, { exportTemp : this.exportTempDir, importTemp : this.importTempDir } ); + temptmp.cleanup( paths => { + const fullStats = { + exportDir : this.exportTempDir, + importTemp : this.importTempDir, + paths : paths, + sessionId : temptmp.sessionId, + }; - if(err) { - Log.warn(fullStats, 'Failed cleaning up temporary directories!'); - } else { - Log.trace(fullStats, 'Temporary directories cleaned up'); - } + Log.trace(fullStats, 'Temporary directories cleaned up'); FTNMessageScanTossModule.super_.prototype.shutdown.call(this, cb); }); - */ FTNMessageScanTossModule.super_.prototype.shutdown.call(this, cb); }; diff --git a/core/transfer_file.js b/core/transfer_file.js index 28022c06..20cddeac 100644 --- a/core/transfer_file.js +++ b/core/transfer_file.js @@ -9,12 +9,13 @@ const Errors = require('./enig_error.js').Errors; const DownloadQueue = require('./download_queue.js'); const StatLog = require('./stat_log.js'); const FileEntry = require('./file_entry.js'); +const Log = require('./logger.js').log; // deps const async = require('async'); const _ = require('lodash'); const pty = require('ptyw.js'); -const temp = require('temp').track(); // track() cleans up temp dir/files for us +const temptmp = require('temptmp').createTrackedSession('transfer_file'); const paths = require('path'); const fs = require('fs'); const fse = require('fs-extra'); @@ -264,7 +265,7 @@ exports.getModule = class TransferFileModule extends MenuModule { return callback(null, null); } - temp.open( { prefix : TEMP_SUFFIX, suffix : '.txt' }, (err, tempFileInfo) => { + temptmp.open( { prefix : TEMP_SUFFIX, suffix : '.txt' }, (err, tempFileInfo) => { if(err) { return callback(err); // failed to create it } @@ -511,15 +512,11 @@ exports.getModule = class TransferFileModule extends MenuModule { }); } }, - function cleanupTempFiles(callback) { - /* :TODO: figure out the global temp cleanup() issue!!@! - temp.cleanup( err => { - if(err) { - self.client.log.warn( { error : err.message }, 'Failed to clean up temporary file/directory(s)' ); - } - return callback(null); // ignore err + function cleanupTempFiles(callback) { + temptmp.cleanup( paths => { + Log.debug( { paths : paths, sessionId : temptmp.sessionId }, 'Temporary files cleaned up' ); }); - */ + return callback(null); }, function updateUserAndSystemStats(callback) { diff --git a/mods/upload.js b/mods/upload.js index c7f491e1..748ecf9c 100644 --- a/mods/upload.js +++ b/mods/upload.js @@ -10,11 +10,12 @@ const scanFile = require('../core/file_area.js').scanFile; const ansiGoto = require('../core/ansi_term.js').goto; const moveFileWithCollisionHandling = require('../core/file_util.js').moveFileWithCollisionHandling; const pathWithTerminatingSeparator = require('../core/file_util.js').pathWithTerminatingSeparator; +const Log = require('../core/logger.js').log; // deps const async = require('async'); const _ = require('lodash'); -const temp = require('temp').track(); // track() cleans up temp dir/files for us +const temptmp = require('temptmp').createTrackedSession('upload'); const paths = require('path'); exports.moduleInfo = { @@ -142,15 +143,16 @@ exports.getModule = class UploadModule extends MenuModule { leave() { // remove any temp files - only do this when if(this.isFileTransferComplete()) { - // :TODO: fix global temp cleanup issue!!! - //temp.cleanup(); // remove any temp files + temptmp.cleanup( paths => { + Log.debug( { paths : paths, sessionId : temptmp.sessionId }, 'Temporary files cleaned up' ); + }); } super.leave(); } performBlindUpload(cb) { - temp.mkdir('enigul-', (err, tempRecvDirectory) => { + temptmp.mkdir( { prefix : 'enigul-' }, (err, tempRecvDirectory) => { if(err) { return cb(err); } diff --git a/package.json b/package.json index 2a07b7d8..b17fc63b 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "ptyw.js": "NuSkooler/ptyw.js", "sqlite3": "^3.1.1", "ssh2": "^0.5.1", - "temp": "^0.8.3" + "temptmp" : "^1.0.0" }, "devDependencies": { "lodash-migrate": "^0.3.16"