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

File permissions changed while unzipping. #101

Closed
rash805115 opened this issue Dec 17, 2018 · 5 comments
Closed

File permissions changed while unzipping. #101

rash805115 opened this issue Dec 17, 2018 · 5 comments

Comments

@rash805115
Copy link

Yauzl library is incorrectly unzipping my file permissions. Below is a test file.

const AdmZip = require('adm-zip')
const fs = require('fs-extra')
const path = require('path')
const yauzl = require('yauzl')
const yazl = require('yazl')

// Create an archive of a file whose permission is 755.
let createArchive = function () {
  return new Promise(function (resolve) {
    let zip = new yazl.ZipFile()
    zip.addFile('./node_modules/webpack-dev-server/bin/webpack-dev-server.js', 'webpack-dev-server.js')
    zip.end(function () {
      zip.outputStream
        .pipe(fs.createWriteStream('./test.zip'))
        .on('close', function () {
          resolve()
        })
    })
  })
}

// Check the permission of the file inside zip. It should be same as what we archived.
let checkArchive = function () {
  return new Promise(function (resolve) {
    let outputZip = new AdmZip('./test.zip')
    resolve(outputZip.getEntry('webpack-dev-server.js'))
  })
}

// Extract the file and check its permission. It should be the same as the original permission.
let extractArchive = function () {
  return new Promise(function (resolve) {
    yauzl.open('./test.zip', {lazyEntries: true}, (error, zip) => {
      zip.on('end', () => { resolve() })
      zip.on('entry', (entry) => {
        zip.openReadStream(entry, function (error, readStream) {
          if (error) return zip.emit('error', error)

          // Save current entry. Then read next.
          fs.ensureFile(path.join('./', entry.fileName), (error) => {
            if (error) return zip.emit('error', error)

            let outputStream = fs.createWriteStream('./' + entry.fileName)
            outputStream.on('error', (error) => { zip.emit('error', error) })
            outputStream.on('finish', () => { zip.readEntry() })

            readStream.on('error', (error) => { zip.emit('error', error) })
            readStream.pipe(outputStream)
          })
        })
      })

      zip.readEntry()
    })
  })
}

let modes = {}

let originalMode = fs.statSync('./node_modules/webpack-dev-server/bin/webpack-dev-server.js')
modes['original'] = ((originalMode.mode << 16) >>> 0)

createArchive()
  .then(function () {
    return checkArchive()
  })
  .then(function (permission) {
    modes['zipped'] = permission.header.attr
    return extractArchive()
  })
  .then(function () {
    let unzippedMode = fs.statSync('./webpack-dev-server.js')
    modes['unzipped'] = ((unzippedMode.mode << 16) >>> 0)

    console.log('Original ' + modes['original']) // 33261 i.e. 755
    console.log('Zipped   ' + modes['zipped']) // 33261 i.e. 755
    console.log('Unzipped ' + modes['unzipped']) // 33188 i.e. 644
  })

Output

Original 2179792896
Zipped   2179792896
Unzipped 2175008768

I have also verified that in Line 287, the same value as zipped is read, so I am not sure why the file is not set with correct permissions.

@thejoshwolfe
Copy link
Owner

yauzl does not write to the file system. This line from your example is where the file is being created:

let outputStream = fs.createWriteStream('./' + entry.fileName)

or possibly this line; i'm not familiar with the libraries you're using:

fs.ensureFile(path.join('./', entry.fileName), ...)

You'll need to set the file permission bits yourself. See the readme for how to get the file permissions from yauzl.

@rash805115
Copy link
Author

Ah I see. Thank you for explanation. I will have to change this line.

let outputStream = fs.createWriteStream('./' + entry.fileName, {mode: entry.externalFileAttributes})

But externalFileAttributes is this big number 2179792896 that I am not sure how to convert to 33261 and I couldn't find anything in docs.

@thejoshwolfe
Copy link
Owner

Ah sorry! I was thinking of the docs for yazl. My mistake.

If you trust that your zip files are all coming from a UNIX-like zipfile creator, then you can use this:

{mode: entry.externalFileAttributes >>> 16}

I have not done an analysis to see how common it is to encode the permission bits like this in the external file attributes, but I know it is typical in at least some situations. The meaning of the external file attributes is not specified by the zipfile specification.

It's probably appropriate for me to do this investigation and have a getter function in the Entry class like I do for getLastModDate(). An implementation would include the above logic in at least one case for that function.

@rash805115
Copy link
Author

It works! I was missing two things.

  1. It turns out that the method ensureFile() that I was using to ensure an empty file is present, creates the file with default 644 permission and this will never be overridden. So I am now using ensureDir() method to only ensure the directory structure exists, not file.
  2. The fs.createWriteStream() method with mode option then ensures that my file is created with correct permissions. I used your suggested conversion {mode: entry.externalFileAttributes >>> 16} and it works just fine.

Regarding yauzl code change, if the method is about this much only, then I can raise a PR with this code and appropriate README changes.

let getLastModDate = function () {
  return entry.externalFileAttributes >>> 16
}

I will leave this PR to your discretion. You can keep it open to track the changes, or close it.

@thejoshwolfe
Copy link
Owner

I opened #102 to implement the method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants