Add support for UNIX / DOS permissions #200

Merged
merged 1 commit into from Feb 19, 2015

Projects

None yet

2 participants

@dduponchel
Collaborator

Two new fields on ZipObject, unixPermissions and dosPermissions, hold the
UNIX or DOS permissions of the file. A new option of generate(),
platform (DOS or UNIX) controls the use of the permissions.

The default behavior is to generate DOS archives, without any
permissions, like before.

Bonus side-effect : Finder on mac doesn't use the DOS directory flag,
JSZip didn't properly recognize folders until now.

Fix #194 and #198.

@dduponchel dduponchel Add support for UNIX / DOS permissions
Two new fields on ZipObject, `unixPermissions` and `dosPermissions`, hold the
UNIX or DOS permissions of the file. A new option of `generate()`,
`platform` (DOS or UNIX) controls the use of the permissions.

The default behavior is to generate DOS archives, without any
permissions, like before.

Bonus side-effect : Finder on mac doesn't use the DOS directory flag,
JSZip didn't properly recognize folders until now.

Fix #194 and #198.
bb4984d
@Stuk Stuk commented on the diff Feb 19, 2015
lib/object.js
+ permissions &= 0x1B6; // 0666
+ }
+ if (unixPermissions && unixPermissions.readOnly) {
+ permissions &= 0x16D; // 0555
+ }
+
+ var extFileAttr = permissions << 16;
+
+ if (isDir) {
+ extFileAttr |= 0x4000 << 16; // UNX_IFDIR 0040000 see zipinfo.c
+ } else {
+ extFileAttr |= 0x8000 << 16; // UNX_IFREG 0100000 see zipinfo.c
+ }
+
+ return extFileAttr;
+};
@Stuk
Stuk Feb 19, 2015 Owner

Fun stuff :)

@Stuk
Owner
Stuk commented Feb 19, 2015

Wow, great work.

@Stuk Stuk merged commit 6c63447 into Stuk:master Feb 19, 2015

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
@dduponchel
Collaborator

Thanks !

@dduponchel dduponchel deleted the dduponchel:feature-permissions branch Feb 19, 2015
@adam-lynch adam-lynch referenced this pull request in sindresorhus/gulp-zip Feb 19, 2015
Closed

Problem with OSX's unzip #38

@dduponchel
Collaborator

I chose simplified permissions (readOnly/executable/etc versus the full unix/dos permissions/mode) for two reasons :

  • It's easy to get and set (no need for binary mask, octal is forbidden in strict mode)
  • nodejs on windows returns a non executable mode for folders (if generated with platform:"UNIX", this will render it unusable on linux/mac)

Unfortunately, this simplification may hurt too many nodejs users. If they compress a 660 file, JSZip will transform it into a 644 file (it ignores the "group" and "other" part). Same for folders : 750 ? You will get 755.

The best solution may be to replace the *Permissions fields with their numerical representation (from nodejs' stats.mode for example) and using them directly. No more surprises, the user get what is on the disk.
For the first issue (get/set permissions), it may be too much for a zip library to fully parse the file mode (nodejs' fs.Stats don't do that either). To easily set it, we can allow a string ("755", "640" for example) and transform it.
For the second issue, this edge case may be just a documentation issue : something like "Warning : if you set platform in nodejs, be sure to use process.platform as value. <explanation here>".

Does that sounds good to you @Stuk ?

We also lack an options parameter on folder() to set permissions :-)

@dduponchel dduponchel added a commit to dduponchel/jszip that referenced this pull request Mar 8, 2015
@dduponchel dduponchel Rework the unix/dos permissions and the dir option.
This is an update for #200.

If we don't keep the original unix/dos permissions, this will lead to
hard-to-debug issues, where the permissions are not exactly like they
were specified.
This commit changes that and uses the exact file mode. It also updates the
default umask from 022 to 002 (with unixPermissions == null, files will
get 0664 and folders 0775).

To put a folder with custom permissions, folder() is not a good
candidate : its behavior is to re-use existing entries. Instead, this
commit documents the existing dir attribute.
3b6c23f
This was referenced Mar 8, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment