2016-02-24 04:56:22 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// ENiGMA½
|
2017-05-11 03:21:07 +00:00
|
|
|
const Config = require('./config.js').config;
|
|
|
|
const stringFormat = require('./string_format.js');
|
|
|
|
const Errors = require('./enig_error.js').Errors;
|
|
|
|
const resolveMimeType = require('./mime_util.js').resolveMimeType;
|
2016-02-24 04:56:22 +00:00
|
|
|
|
|
|
|
// base/modules
|
2017-05-20 03:20:19 +00:00
|
|
|
const fs = require('graceful-fs');
|
2016-08-06 22:30:56 +00:00
|
|
|
const _ = require('lodash');
|
2018-03-09 04:39:42 +00:00
|
|
|
const pty = require('node-pty');
|
2016-02-24 04:56:22 +00:00
|
|
|
|
2016-10-03 03:39:29 +00:00
|
|
|
let archiveUtil;
|
|
|
|
|
|
|
|
class Archiver {
|
|
|
|
constructor(config) {
|
|
|
|
this.compress = config.compress;
|
|
|
|
this.decompress = config.decompress;
|
|
|
|
this.list = config.list;
|
|
|
|
this.extract = config.extract;
|
|
|
|
}
|
|
|
|
|
|
|
|
ok() {
|
2018-01-15 19:22:11 +00:00
|
|
|
return this.canCompress() && this.canDecompress();
|
2016-10-03 03:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
can(what) {
|
|
|
|
if(!_.has(this, [ what, 'cmd' ]) || !_.has(this, [ what, 'args' ])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _.isString(this[what].cmd) && Array.isArray(this[what].args) && this[what].args.length > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
canCompress() { return this.can('compress'); }
|
|
|
|
canDecompress() { return this.can('decompress'); }
|
2016-10-03 04:21:37 +00:00
|
|
|
canList() { return this.can('list'); } // :TODO: validate entryMatch
|
2016-10-03 03:39:29 +00:00
|
|
|
canExtract() { return this.can('extract'); }
|
|
|
|
}
|
|
|
|
|
2016-02-24 04:56:22 +00:00
|
|
|
module.exports = class ArchiveUtil {
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-02-24 04:56:22 +00:00
|
|
|
constructor() {
|
|
|
|
this.archivers = {};
|
|
|
|
this.longestSignature = 0;
|
|
|
|
}
|
|
|
|
|
2016-10-03 03:39:29 +00:00
|
|
|
// singleton access
|
|
|
|
static getInstance() {
|
|
|
|
if(!archiveUtil) {
|
|
|
|
archiveUtil = new ArchiveUtil();
|
|
|
|
archiveUtil.init();
|
|
|
|
}
|
2016-10-03 04:21:37 +00:00
|
|
|
return archiveUtil;
|
2016-10-03 03:39:29 +00:00
|
|
|
}
|
|
|
|
|
2016-02-24 04:56:22 +00:00
|
|
|
init() {
|
|
|
|
//
|
|
|
|
// Load configuration
|
|
|
|
//
|
2016-10-06 05:22:59 +00:00
|
|
|
if(_.has(Config, 'archives.archivers')) {
|
|
|
|
Object.keys(Config.archives.archivers).forEach(archKey => {
|
2016-10-03 03:39:29 +00:00
|
|
|
|
2016-10-06 05:22:59 +00:00
|
|
|
const archConfig = Config.archives.archivers[archKey];
|
2016-10-03 03:39:29 +00:00
|
|
|
const archiver = new Archiver(archConfig);
|
|
|
|
|
|
|
|
if(!archiver.ok()) {
|
|
|
|
// :TODO: Log warning - bad archiver/config
|
2016-02-24 04:56:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.archivers[archKey] = archiver;
|
2016-10-06 05:22:59 +00:00
|
|
|
});
|
|
|
|
}
|
2016-10-03 03:39:29 +00:00
|
|
|
|
2017-05-11 03:21:07 +00:00
|
|
|
if(_.isObject(Config.fileTypes)) {
|
|
|
|
Object.keys(Config.fileTypes).forEach(mimeType => {
|
|
|
|
const fileType = Config.fileTypes[mimeType];
|
|
|
|
if(fileType.sig) {
|
|
|
|
fileType.sig = new Buffer(fileType.sig, 'hex');
|
|
|
|
fileType.offset = fileType.offset || 0;
|
|
|
|
|
|
|
|
// :TODO: this is broken: sig is NOT this long, it's sig.length long; offset needs to allow for -negative values as well
|
|
|
|
const sigLen =fileType.offset + fileType.sig.length;
|
|
|
|
if(sigLen > this.longestSignature) {
|
|
|
|
this.longestSignature = sigLen;
|
|
|
|
}
|
|
|
|
}
|
2016-02-24 04:56:22 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2016-10-06 05:22:59 +00:00
|
|
|
|
2017-05-11 03:21:07 +00:00
|
|
|
getArchiver(mimeTypeOrExtension) {
|
|
|
|
mimeTypeOrExtension = resolveMimeType(mimeTypeOrExtension);
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2017-05-11 03:21:07 +00:00
|
|
|
if(!mimeTypeOrExtension) { // lookup returns false on failure
|
2016-10-06 05:22:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-11 03:21:07 +00:00
|
|
|
const archiveHandler = _.get( Config, [ 'fileTypes', mimeTypeOrExtension, 'archiveHandler'] );
|
|
|
|
if(archiveHandler) {
|
|
|
|
return _.get( Config, [ 'archives', 'archivers', archiveHandler ] );
|
2016-10-06 05:22:59 +00:00
|
|
|
}
|
2016-03-04 05:54:32 +00:00
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-03-04 05:54:32 +00:00
|
|
|
haveArchiver(archType) {
|
|
|
|
return this.getArchiver(archType) ? true : false;
|
2016-02-29 05:04:03 +00:00
|
|
|
}
|
2016-02-24 04:56:22 +00:00
|
|
|
|
2018-01-15 19:22:11 +00:00
|
|
|
// :TODO: implement me:
|
|
|
|
/*
|
|
|
|
detectTypeWithBuf(buf, cb) {
|
2016-10-03 03:39:29 +00:00
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
*/
|
2016-10-03 03:39:29 +00:00
|
|
|
|
2016-02-24 04:56:22 +00:00
|
|
|
detectType(path, cb) {
|
|
|
|
fs.open(path, 'r', (err, fd) => {
|
|
|
|
if(err) {
|
2016-10-06 05:22:59 +00:00
|
|
|
return cb(err);
|
2016-02-24 04:56:22 +00:00
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-10-06 05:22:59 +00:00
|
|
|
const buf = new Buffer(this.longestSignature);
|
2016-02-24 04:56:22 +00:00
|
|
|
fs.read(fd, buf, 0, buf.length, 0, (err, bytesRead) => {
|
|
|
|
if(err) {
|
2016-08-06 22:30:56 +00:00
|
|
|
return cb(err);
|
2016-02-24 04:56:22 +00:00
|
|
|
}
|
|
|
|
|
2017-05-11 03:21:07 +00:00
|
|
|
const archFormat = _.findKey(Config.fileTypes, fileTypeInfo => {
|
|
|
|
if(!fileTypeInfo.sig) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const lenNeeded = fileTypeInfo.offset + fileTypeInfo.sig.length;
|
2016-10-06 05:22:59 +00:00
|
|
|
|
2016-08-06 22:30:56 +00:00
|
|
|
if(bytesRead < lenNeeded) {
|
2016-02-24 04:56:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-11 03:21:07 +00:00
|
|
|
const comp = buf.slice(fileTypeInfo.offset, fileTypeInfo.offset + fileTypeInfo.sig.length);
|
|
|
|
return (fileTypeInfo.sig.equals(comp));
|
2016-02-24 04:56:22 +00:00
|
|
|
});
|
|
|
|
|
2016-10-06 05:22:59 +00:00
|
|
|
return cb(archFormat ? null : Errors.General('Unknown type'), archFormat);
|
2018-01-15 19:22:11 +00:00
|
|
|
});
|
2016-02-24 04:56:22 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-03 04:21:37 +00:00
|
|
|
spawnHandler(proc, action, cb) {
|
2016-08-06 22:30:56 +00:00
|
|
|
// pty.js doesn't currently give us a error when things fail,
|
|
|
|
// so we have this horrible, horrible hack:
|
|
|
|
let err;
|
2016-10-03 04:21:37 +00:00
|
|
|
proc.once('data', d => {
|
2017-05-11 03:21:07 +00:00
|
|
|
if(_.isString(d) && d.startsWith('execvp(3) failed.')) {
|
|
|
|
err = Errors.ExternalProcess(`${action} failed: ${d.trim()}`);
|
2016-08-06 22:30:56 +00:00
|
|
|
}
|
|
|
|
});
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-10-03 04:21:37 +00:00
|
|
|
proc.once('exit', exitCode => {
|
2017-05-11 03:21:07 +00:00
|
|
|
return cb(exitCode ? Errors.ExternalProcess(`${action} failed with exit code: ${exitCode}`) : err);
|
2018-01-15 19:22:11 +00:00
|
|
|
});
|
2016-08-06 22:30:56 +00:00
|
|
|
}
|
|
|
|
|
2016-02-24 04:56:22 +00:00
|
|
|
compressTo(archType, archivePath, files, cb) {
|
2016-03-04 05:54:32 +00:00
|
|
|
const archiver = this.getArchiver(archType);
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-02-24 04:56:22 +00:00
|
|
|
if(!archiver) {
|
2017-05-11 03:21:07 +00:00
|
|
|
return cb(Errors.Invalid(`Unknown archive type: ${archType}`));
|
2016-02-24 04:56:22 +00:00
|
|
|
}
|
|
|
|
|
2016-10-03 03:39:29 +00:00
|
|
|
const fmtObj = {
|
|
|
|
archivePath : archivePath,
|
2016-10-06 05:22:59 +00:00
|
|
|
fileList : files.join(' '), // :TODO: probably need same hack as extractTo here!
|
2016-10-03 03:39:29 +00:00
|
|
|
};
|
2016-02-24 04:56:22 +00:00
|
|
|
|
2016-10-03 03:39:29 +00:00
|
|
|
const args = archiver.compress.args.map( arg => stringFormat(arg, fmtObj) );
|
2017-05-20 02:28:15 +00:00
|
|
|
|
|
|
|
let proc;
|
|
|
|
try {
|
|
|
|
proc = pty.spawn(archiver.compress.cmd, args, this.getPtyOpts());
|
|
|
|
} catch(e) {
|
|
|
|
return cb(e);
|
|
|
|
}
|
2016-02-24 04:56:22 +00:00
|
|
|
|
2016-10-03 04:21:37 +00:00
|
|
|
return this.spawnHandler(proc, 'Compression', cb);
|
2016-02-24 04:56:22 +00:00
|
|
|
}
|
|
|
|
|
2016-10-06 05:22:59 +00:00
|
|
|
extractTo(archivePath, extractPath, archType, fileList, cb) {
|
|
|
|
let haveFileList;
|
|
|
|
|
|
|
|
if(!cb && _.isFunction(fileList)) {
|
|
|
|
cb = fileList;
|
|
|
|
fileList = [];
|
2018-01-15 19:22:11 +00:00
|
|
|
haveFileList = false;
|
2016-10-06 05:22:59 +00:00
|
|
|
} else {
|
|
|
|
haveFileList = true;
|
|
|
|
}
|
|
|
|
|
2016-03-04 05:54:32 +00:00
|
|
|
const archiver = this.getArchiver(archType);
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-03-04 05:54:32 +00:00
|
|
|
if(!archiver) {
|
2017-05-11 03:21:07 +00:00
|
|
|
return cb(Errors.Invalid(`Unknown archive type: ${archType}`));
|
2016-03-04 05:54:32 +00:00
|
|
|
}
|
2016-10-03 03:39:29 +00:00
|
|
|
|
|
|
|
const fmtObj = {
|
|
|
|
archivePath : archivePath,
|
|
|
|
extractPath : extractPath,
|
|
|
|
};
|
|
|
|
|
2016-10-06 05:22:59 +00:00
|
|
|
const action = haveFileList ? 'extract' : 'decompress';
|
|
|
|
|
|
|
|
// we need to treat {fileList} special in that it should be broken up to 0:n args
|
|
|
|
const args = archiver[action].args.map( arg => {
|
|
|
|
return '{fileList}' === arg ? arg : stringFormat(arg, fmtObj);
|
|
|
|
});
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-10-06 05:22:59 +00:00
|
|
|
const fileListPos = args.indexOf('{fileList}');
|
|
|
|
if(fileListPos > -1) {
|
|
|
|
// replace {fileList} with 0:n sep file list arguments
|
|
|
|
args.splice.apply(args, [fileListPos, 1].concat(fileList));
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:28:15 +00:00
|
|
|
let proc;
|
|
|
|
try {
|
|
|
|
proc = pty.spawn(archiver[action].cmd, args, this.getPtyOpts());
|
|
|
|
} catch(e) {
|
|
|
|
return cb(e);
|
|
|
|
}
|
2016-08-06 22:30:56 +00:00
|
|
|
|
2016-10-06 05:22:59 +00:00
|
|
|
return this.spawnHandler(proc, (haveFileList ? 'Extraction' : 'Decompression'), cb);
|
2016-10-03 04:21:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
listEntries(archivePath, archType, cb) {
|
|
|
|
const archiver = this.getArchiver(archType);
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-10-03 04:21:37 +00:00
|
|
|
if(!archiver) {
|
2018-01-15 19:22:11 +00:00
|
|
|
return cb(Errors.Invalid(`Unknown archive type: ${archType}`));
|
2016-10-03 04:21:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const fmtObj = {
|
|
|
|
archivePath : archivePath,
|
|
|
|
};
|
|
|
|
|
|
|
|
const args = archiver.list.args.map( arg => stringFormat(arg, fmtObj) );
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2017-05-20 02:28:15 +00:00
|
|
|
let proc;
|
|
|
|
try {
|
|
|
|
proc = pty.spawn(archiver.list.cmd, args, this.getPtyOpts());
|
|
|
|
} catch(e) {
|
|
|
|
return cb(e);
|
|
|
|
}
|
2016-10-03 04:21:37 +00:00
|
|
|
|
|
|
|
let output = '';
|
|
|
|
proc.on('data', data => {
|
|
|
|
// :TODO: hack for: execvp(3) failed.: No such file or directory
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-10-03 04:21:37 +00:00
|
|
|
output += data;
|
|
|
|
});
|
|
|
|
|
|
|
|
proc.once('exit', exitCode => {
|
|
|
|
if(exitCode) {
|
2017-05-11 03:21:07 +00:00
|
|
|
return cb(Errors.ExternalProcess(`List failed with exit code: ${exitCode}`));
|
2016-10-03 04:21:37 +00:00
|
|
|
}
|
2017-01-30 05:30:48 +00:00
|
|
|
|
|
|
|
const entryGroupOrder = archiver.list.entryGroupOrder || { byteSize : 1, fileName : 2 };
|
2016-10-03 04:21:37 +00:00
|
|
|
|
|
|
|
const entries = [];
|
2016-10-04 04:03:32 +00:00
|
|
|
const entryMatchRe = new RegExp(archiver.list.entryMatch, 'gm');
|
2016-10-03 04:21:37 +00:00
|
|
|
let m;
|
2016-10-04 04:03:32 +00:00
|
|
|
while((m = entryMatchRe.exec(output))) {
|
2016-10-03 04:21:37 +00:00
|
|
|
entries.push({
|
2017-01-30 05:30:48 +00:00
|
|
|
byteSize : parseInt(m[entryGroupOrder.byteSize]),
|
2017-02-18 04:56:28 +00:00
|
|
|
fileName : m[entryGroupOrder.fileName].trim(),
|
2016-10-03 04:21:37 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(null, entries);
|
2018-01-15 19:22:11 +00:00
|
|
|
});
|
2016-03-04 05:54:32 +00:00
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-03-04 05:54:32 +00:00
|
|
|
getPtyOpts() {
|
|
|
|
return {
|
|
|
|
// :TODO: cwd
|
|
|
|
name : 'enigma-archiver',
|
|
|
|
cols : 80,
|
|
|
|
rows : 24,
|
2018-01-15 19:22:11 +00:00
|
|
|
env : process.env,
|
2016-03-04 05:54:32 +00:00
|
|
|
};
|
2016-02-24 04:56:22 +00:00
|
|
|
}
|
2016-08-06 22:30:56 +00:00
|
|
|
};
|