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 files created on Windows being unusable on Linux/MacOS (missing executable bits on directories) #59

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ function Entry(metadataPath, isDirectory, options) {
if (options.mode != null) {
this.setFileAttributesMode(options.mode);
} else {
this.setFileAttributesMode(isDirectory ? 0o40775 : 0o100664);
this.setFileAttributesMode(0o664);
}
if (isDirectory) {
this.crcAndFileSizeKnown = true;
Expand Down Expand Up @@ -435,6 +435,15 @@ Entry.prototype.setLastModDate = function(date) {
Entry.prototype.setFileAttributesMode = function(mode) {
if ((mode & 0xffff) !== mode) throw new Error("invalid mode. expected: 0 <= " + mode + " <= " + 0xffff);
// http://unix.stackexchange.com/questions/14705/the-zip-formats-external-file-attribute/14727#14727
if (this.isDirectory) {
// Set executable bit on directories if any other bits are set for that user/group/all
// Fixes creating unusable zip files on platforms that do not use an executable bit
mode |= ((mode >> 1) | (mode >> 2)) & 0o111;
mode |= 0o040000; // S_IFDIR
} else {
mode |= 0o100000; // S_IFREG
}

this.externalFileAttributes = (mode << 16) >>> 0;
};
// doFileDataPump() should not call pumpEntries() directly. see issue #9.
Expand Down
10 changes: 9 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ var BufferList = require("bl");
zipfile.addBuffer(bufferFrom("buffer"), "b.txt");
zipfile.addReadStream(new BufferList().append("stream"), "c.txt");
zipfile.addEmptyDirectory("d/");
zipfile.addEmptyDirectory("e");
zipfile.addEmptyDirectory("e", { mode: 0644 });
zipfile.end(function(finalSize) {
if (finalSize !== -1) throw new Error("finalSize should be unknown");
zipfile.outputStream.pipe(new BufferList(function(err, data) {
Expand All @@ -92,6 +92,14 @@ var BufferList = require("bl");
if (entry.fileName !== expectedName) {
throw new Error("unexpected entry fileName: " + entry.fileName + ", expected: " + expectedName);
}
var mode = entry.externalFileAttributes >>> 16;
if (/\/$/.test(entry.fileName)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (/\/$/.test(entry.fileName)) {
if (entry.fileName.startsWith('/')) {

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other spot in this file is using that regex to decide if it's a directory, so just matching that style, though I agree it's not particularly readable. So much so that your suggestion is actually backwards (should be fileName.endsWith('/')) ^_^. endsWith, however, is ES6+ only, and doesn't exist in Node v0.10, that this library is still being tested against.

// Directory file names end with '/'.
if (!(mode & 040000)) throw new Error("directory expected to have S_IFDIR, found " + mode.toString(8));
if (!(mode & 0111)) throw new Error("directory expected to have executable flags, found " + mode.toString(8));
} else {
if (!(mode & 0100000)) throw new Error("file expected to have S_IFREG, found " + mode.toString(8));
}
});
zipfile.on("end", function() {
if (entryNames.length === 0) console.log("optional parameters and directories: pass");
Expand Down