Skip to content
This repository has been archived by the owner on Jul 3, 2019. It is now read-only.

Commit

Permalink
fix(extract-stream): compensate for bug in tar-fs chowning
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Feb 22, 2017
1 parent 73c14b2 commit 1834b3a
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion lib/extract-stream.js
@@ -1,20 +1,65 @@
'use strict'

var chownr = require('chownr')
var gunzip = require('./util/gunzip-maybe')
var path = require('path')
var pipeline = require('mississippi').pipeline
var tar = require('tar-fs')
var to = require('mississippi').to

module.exports = extractStream
function extractStream (dest, opts) {
opts = opts || {}
var errEmitted = false
var tarStream = makeTarStream(dest, opts)
var target = to(function (chunk, enc, cb) {
tarStream.write(chunk, enc, cb)
}, function flush (cb) {
// TODO - there's a bug in tar-fs so we have to do this manually.
// This code ms.to() stunt can be removed afer the issue is fixed:
// https://github.com/mafintosh/tar-fs/issues/59
tarStream.end(function () {
if (
process.platform !== 'win32' &&
process.getuid &&
process.getuid() === 0 &&
(typeof opts.uid === 'number' || typeof opts.gid === 'number')
) {
chownr(
dest,
typeof opts.uid === 'number' ? opts.uid : process.getuid(),
typeof opts.gid === 'number' ? opts.gid : process.getgid(),
cb
)
} else {
cb()
}
})
})
target.on('error', function (err) {
if (!errEmitted) {
errEmitted = true
tarStream.emit('error', err)
}
})
tarStream.on('error', function (err) {
if (!errEmitted) {
errEmitted = true
target.emit('error', err)
}
})
return target
}

function makeTarStream (dest, opts) {
var sawIgnores = {}
return pipeline(gunzip(), tar.extract(dest, {
map: function (header) {
if (process.platform !== 'win32') {
if (process.getuid && process.getuid() === 0) {
header.uid = opts.uid == null ? header.uid : opts.uid
header.gid = opts.gid == null ? header.gid : opts.gid
}

// Note: This mirrors logic in the fs read operations that are
// employed during tarball creation, in the fstream-npm module.
// It is duplicated here to handle tarballs that are created
Expand Down

0 comments on commit 1834b3a

Please sign in to comment.