Skip to content

Commit

Permalink
fix: properly handle top-level files when using strip
Browse files Browse the repository at this point in the history
Fix: #261
  • Loading branch information
isaacs committed Aug 9, 2021
1 parent ea6f254 commit d688cad
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/unpack.js
Expand Up @@ -195,6 +195,8 @@ class Unpack extends Parser {
if (parts.length < this.strip)
return false
entry.path = parts.slice(this.strip).join('/')
if (entry.path === '' && entry.type !== 'Directory' && entry.type !== 'GNUDumpDir')
return false

if (entry.type === 'Link') {
const linkparts = entry.linkpath.split(/\/|\\/)
Expand Down
43 changes: 43 additions & 0 deletions test/unpack.js
Expand Up @@ -2545,3 +2545,46 @@ t.test('drop entry from dirCache if no longer a directory', t => {
check(t, path)
})
})

t.test('using strip option when top level file exists', t => {
const dir = path.resolve(unpackdir, 'strip-with-top-file')
mkdirp.sync(dir + '/sync/y')
mkdirp.sync(dir + '/async/y')
const data = makeTar([
{
path: 'top',
type: 'File',
size: 0,
},
{
path: 'x',
type: 'Directory',
},
{
path: 'x/a',
type: 'File',
size: 'a'.length,
},
'a',
'',
'',
])
t.plan(2)
const check = (t, path) => {
t.equal(fs.statSync(path).isDirectory(), true)
t.equal(fs.lstatSync(path + '/a').isFile(), true)
t.throws(() => fs.statSync(path + '/top'), { code: 'ENOENT' })
t.end()
}
t.test('async', t => {
const path = dir + '/async'
new Unpack({ cwd: path, strip: 1 })
.on('end', () => check(t, path))
.end(data)
})
t.test('sync', t => {
const path = dir + '/sync'
new UnpackSync({ cwd: path, strip: 1 }).end(data)
check(t, path)
})
})

0 comments on commit d688cad

Please sign in to comment.