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(zip): support symlinks in zip files #283

Merged
merged 5 commits into from
Sep 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
131 changes: 89 additions & 42 deletions lib/zip.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/**
* Enhancements to adm-zip.
*
* @module xml
* @module zip
*
* @copyright
* Copyright (c) 2009-2013 by Appcelerator, Inc. All Rights Reserved.
* Copyright (c) 2009-2020 by Appcelerator, Inc. All Rights Reserved.
*
* @license
* Licensed under the terms of the Apache Public License
Expand All @@ -13,7 +11,13 @@
'use strict';

const afs = require('./fs'),
AdmZip = require('adm-zip');
fs = require('fs-extra'),
yauzl = require('yauzl'),
path = require('path');

const IFMT = 61440;
const IFDIR = 16384;
const IFLNK = 40960;

/**
* Extracts all files and applies the correct file permissions.
Expand All @@ -27,50 +31,93 @@ const afs = require('./fs'),
*/
exports.unzip = function unzip(file, dest, opts, finished) {
try {
const zip = new AdmZip(file),
zipEntries = zip.getEntries(),
len = zipEntries.length,
Utils = require('adm-zip/util'),
visitor = opts && opts.visitor || function () {},
const visitor = opts && opts.visitor || function () {},
overwrite = opts && Object.prototype.hasOwnProperty.call(opts, 'overwrite') ? !!opts.overwrite : true,
defaultPerm = opts && opts.defaultPerm || 0o644;
let i = 0;

// we need to do this self-calling setTimeout() loop otherwise
// the progress bar is never allowed to render
(function extractFile() {
if (i < len) {
try {
const entry = zipEntries[i];
yauzl.open(file, { lazyEntries: true }, function (err, zipfile) {
if (err) {
return finished(err);
}
let i = 0;
const len = zipfile.entryCount;
zipfile.once('error', err => finished(err, i, len));
zipfile.on('close', () => finished(null, i, len));
zipfile.on('entry', function (entry) {
if (entry.fileName.startsWith('__MACOSX/')) {
zipfile.readEntry();
return;
}

// handle visitor function!
if (visitor(entry, i, len) === false) {
zipfile.readEntry();
return;
}
const destFile = afs.resolvePath(dest, entry.fileName);

if (visitor(entry, i, len) !== false) {
if (entry.isDirectory) {
Utils.makeDir(afs.resolvePath(dest, entry.entryName.toString()));
} else {
const content = entry.getData();
if (!content) {
// FIXME: This is clearly wrong, but not sure if downstream depends on this behavior!
throw Utils.Errors.CANT_EXTRACT_FILE + '2'; // eslint-disable-line no-throw-literal
}
Utils.writeFileTo(
afs.resolvePath(dest, entry.entryName.toString()),
content,
overwrite,
(entry.header.attr && (entry.header.attr >>> 16) || defaultPerm) & 0o777
);
}
}
// convert external file attr int into a fs stat mode int
let mode = (entry.externalFileAttributes >> 16) & 0xFFFF;
// check if it's a symlink or dir (using stat mode constants)
const symlink = (mode & IFMT) === IFLNK;
let isDir = (mode & IFMT) === IFDIR;
// check for windows weird way of specifying a directory
// https://github.com/maxogden/extract-zip/issues/13#issuecomment-154494566
const madeBy = entry.versionMadeBy >> 8;
if (!isDir) {
isDir = (madeBy === 0 && entry.externalFileAttributes === 16);
}

// if no mode then default to default modes
if (mode === 0) {
mode = defaultPerm;
}

// If we're not overwriting and destiantion exists, move on to next entry
if (!overwrite && fs.pathExistsSync(destFile)) {
zipfile.readEntry();
return;
}

if (symlink) {
// How do we handle a symlink?
zipfile.openReadStream(entry, function (err, readStream) {
if (err) {
return finished(err, i, len);
}
fs.ensureDirSync(path.dirname(destFile));
const chunks = [];
readStream.on('data', chunk => chunks.push(chunk));
readStream.on('error', err => finished(err, i, len));
readStream.on('end', () => {
let str = Buffer.concat(chunks).toString('utf8');
fs.symlinkSync(str, destFile);
zipfile.readEntry();
});
});
} else if (isDir) {
fs.ensureDirSync(destFile, mode);
i++;
setTimeout(extractFile, 0);
} catch (ex) {
finished(ex, i, len);
zipfile.readEntry();
} else {
// file entry
zipfile.openReadStream(entry, function (err, readStream) {
if (err) {
return finished(err, i, len);
}
fs.ensureDirSync(path.dirname(destFile));

// pump file contents
readStream.on('end', () => zipfile.readEntry());
readStream.once('error', err => finished(err, i, len));
const writeStream = fs.createWriteStream(destFile, { mode });
readStream.pipe(writeStream);
i++;
});
}
} else {
// done!
finished(null, i, len);
}
}());
});
zipfile.readEntry();
});
} catch (ex) {
finished(ex);
}
Expand Down
34 changes: 28 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"keywords": [
"appcelerator"
],
"version": "1.0.1",
"version": "1.1.0",
"author": {
"name": "Appcelerator, Inc.",
"email": "npmjs@appcelerator.com"
Expand All @@ -32,7 +32,6 @@
"url": "http://github.com/appcelerator/node-appc.git"
},
"dependencies": {
"adm-zip": "^0.4.11",
"async": "^3.2.0",
"colors": "~1.4.0",
"fs-extra": "~9.0.0",
Expand All @@ -41,7 +40,8 @@
"sprintf": "^0.1.5",
"temp": "~0.9.0",
"uuid": "~8.3.0",
"xmldom": "^0.3.0"
"xmldom": "^0.3.0",
"yauzl": "^2.10.0"
},
"devDependencies": {
"@seadub/danger-plugin-dependencies": "0.1.0",
Expand Down
Binary file added test/resources/symlinks.zip
Binary file not shown.
40 changes: 35 additions & 5 deletions test/test-zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,62 @@
/* eslint no-unused-expressions: "off" */
'use strict';

var appc = require('../index'),
const appc = require('../index'),
assert = require('assert'),
fs = require('fs'),
path = require('path'),
temp = require('temp');
require('should');

describe('zip', function () {
it('namespace exists', function () {
appc.should.have.property('zip');
appc.zip.should.be.an.Object;
appc.zip.should.be.an.Object();
});

describe('#extractAll()', function () {
it('should extract all files with correct permissions', function (done) {
var tempDir = temp.mkdirSync();
const tempDir = temp.mkdirSync();
appc.zip.unzip(path.join(__dirname, 'resources', 'test.zip'), tempDir, null, function (err) {
assert(!err, 'expected unzip to not error');

fs.existsSync(path.join(tempDir, 'main.m')).should.be.ok;
fs.existsSync(path.join(tempDir, 'main.m')).should.be.ok();
(fs.statSync(path.join(tempDir, 'main.m')).mode & 0o777).should.equal(process.platform === 'win32' ? 0o666 : 0o644);

fs.existsSync(path.join(tempDir, 'ios-sim')).should.be.ok;
fs.existsSync(path.join(tempDir, 'ios-sim')).should.be.ok();
(fs.statSync(path.join(tempDir, 'ios-sim')).mode & 0o777).should.equal(process.platform === 'win32' ? 0o666 : 0o755);

done();
});
});

it('should preserve symlinks', function (done) {
const tempDir = temp.mkdirSync();
appc.zip.unzip(path.join(__dirname, 'resources', 'symlinks.zip'), tempDir, null, function (err) {
assert(!err, 'expected unzip to not error');

fs.existsSync(path.join(tempDir, 'symlinks/folder')).should.be.true();
const stat3 = fs.statSync(path.join(tempDir, 'symlinks/folder'));
stat3.isDirectory().should.be.true();

fs.existsSync(path.join(tempDir, 'symlinks/folder/testfile.txt')).should.be.true();
const stat4 = fs.statSync(path.join(tempDir, 'symlinks/folder/testfile.txt'));
stat4.isDirectory().should.be.false();
stat4.isFile().should.be.true();

fs.existsSync(path.join(tempDir, 'symlinks/link.txt')).should.be.true();
const stat = fs.lstatSync(path.join(tempDir, 'symlinks/link.txt'));
stat.isSymbolicLink().should.be.true(); // fails here

const p = path.join(tempDir, 'symlinks/folderlink');
fs.existsSync(p).should.be.true();
const stat2 = fs.lstatSync(p);
stat2.isSymbolicLink().should.be.true(); // fails here
const target = fs.readlinkSync(p);
target.should.eql('folder/');

done();
});
});
});
});