Attempt to detect when an archiver does not exist (HACK!)

This commit is contained in:
Bryan Ashby 2016-08-06 16:30:56 -06:00
parent 9af9bfe3d0
commit 26fb4692dc
1 changed files with 31 additions and 15 deletions

View File

@ -2,12 +2,12 @@
'use strict'; 'use strict';
// ENiGMA½ // ENiGMA½
let Config = require('./config.js').config; const Config = require('./config.js').config;
// base/modules // base/modules
let fs = require('fs'); const fs = require('fs');
let _ = require('lodash'); const _ = require('lodash');
let pty = require('ptyw.js'); const pty = require('ptyw.js');
module.exports = class ArchiveUtil { module.exports = class ArchiveUtil {
@ -74,15 +74,14 @@ module.exports = class ArchiveUtil {
let buf = new Buffer(this.longestSignature); let buf = new Buffer(this.longestSignature);
fs.read(fd, buf, 0, buf.length, 0, (err, bytesRead) => { fs.read(fd, buf, 0, buf.length, 0, (err, bytesRead) => {
if(err) { if(err) {
cb(err); return cb(err);
return;
} }
// return first match // return first match
const detected = _.findKey(this.archivers, arch => { const detected = _.findKey(this.archivers, arch => {
const lenNeeded = arch.offset + arch.sig.length; const lenNeeded = arch.offset + arch.sig.length;
if(buf.length < lenNeeded) { if(bytesRead < lenNeeded) {
return false; 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) { compressTo(archType, archivePath, files, cb) {
const archiver = this.getArchiver(archType); const archiver = this.getArchiver(archType);
@ -112,9 +132,7 @@ module.exports = class ArchiveUtil {
let comp = pty.spawn(archiver.compressCmd, args, this.getPtyOpts()); let comp = pty.spawn(archiver.compressCmd, args, this.getPtyOpts());
comp.once('exit', exitCode => { return this.spawnHandler(comp, 'Compression', cb);
cb(exitCode ? new Error(`Compression failed with exit code: ${exitCode}`) : null);
});
} }
extractTo(archivePath, extractPath, archType, cb) { extractTo(archivePath, extractPath, archType, cb) {
@ -133,10 +151,8 @@ module.exports = class ArchiveUtil {
} }
let comp = pty.spawn(archiver.decompressCmd, args, this.getPtyOpts()); let comp = pty.spawn(archiver.decompressCmd, args, this.getPtyOpts());
comp.once('exit', exitCode => { return this.spawnHandler(comp, 'Decompression', cb);
cb(exitCode ? new Error(`Decompression failed with exit code: ${exitCode}`) : null);
});
} }
getPtyOpts() { getPtyOpts() {
@ -148,4 +164,4 @@ module.exports = class ArchiveUtil {
env : process.env, env : process.env,
}; };
} }
} };