From 26fb4692dc1b4c42e9644d804f1e1b2148d61622 Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Sat, 6 Aug 2016 16:30:56 -0600 Subject: [PATCH] Attempt to detect when an archiver does not exist (HACK!) --- core/archive_util.js | 46 +++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/core/archive_util.js b/core/archive_util.js index 914e5e47..094f7d1a 100644 --- a/core/archive_util.js +++ b/core/archive_util.js @@ -2,12 +2,12 @@ 'use strict'; // ENiGMA½ -let Config = require('./config.js').config; +const Config = require('./config.js').config; // base/modules -let fs = require('fs'); -let _ = require('lodash'); -let pty = require('ptyw.js'); +const fs = require('fs'); +const _ = require('lodash'); +const pty = require('ptyw.js'); module.exports = class ArchiveUtil { @@ -74,15 +74,14 @@ module.exports = class ArchiveUtil { let buf = new Buffer(this.longestSignature); fs.read(fd, buf, 0, buf.length, 0, (err, bytesRead) => { if(err) { - cb(err); - return; + return cb(err); } // return first match const detected = _.findKey(this.archivers, arch => { const lenNeeded = arch.offset + arch.sig.length; - if(buf.length < lenNeeded) { + if(bytesRead < lenNeeded) { return false; } @@ -95,6 +94,27 @@ module.exports = class ArchiveUtil { }); } + spawnHandler(comp, action, cb) { + // pty.js doesn't currently give us a error when things fail, + // so we have this horrible, horrible hack: + let err; + comp.once('data', d => { + if(_.isString(d) && d.startsWith('execvp(3) failed.: No such file or directory')) { + err = new Error(`${action} failed: ${d.trim()}`); + } + }); + + comp.once('exit', exitCode => { + if(exitCode) { + return cb(new Error(`${action} failed with exit code: ${exitCode}`)); + } + if(err) { + return cb(err); + } + return cb(null); + }); + } + compressTo(archType, archivePath, files, cb) { const archiver = this.getArchiver(archType); @@ -112,9 +132,7 @@ module.exports = class ArchiveUtil { let comp = pty.spawn(archiver.compressCmd, args, this.getPtyOpts()); - comp.once('exit', exitCode => { - cb(exitCode ? new Error(`Compression failed with exit code: ${exitCode}`) : null); - }); + return this.spawnHandler(comp, 'Compression', cb); } extractTo(archivePath, extractPath, archType, cb) { @@ -133,10 +151,8 @@ module.exports = class ArchiveUtil { } let comp = pty.spawn(archiver.decompressCmd, args, this.getPtyOpts()); - - comp.once('exit', exitCode => { - cb(exitCode ? new Error(`Decompression failed with exit code: ${exitCode}`) : null); - }); + + return this.spawnHandler(comp, 'Decompression', cb); } getPtyOpts() { @@ -148,4 +164,4 @@ module.exports = class ArchiveUtil { env : process.env, }; } -} +};