Skip to content

Commit 56c24b0

Browse files
committed
fix: properly handle top-level files when using strip
Fix: #261
1 parent 8d75229 commit 56c24b0

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

lib/unpack.js

+2
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ class Unpack extends Parser {
205205
if (parts.length < this.strip)
206206
return false
207207
entry.path = parts.slice(this.strip).join('/')
208+
if (entry.path === '' && entry.type !== 'Directory' && entry.type !== 'GNUDumpDir')
209+
return false
208210

209211
if (entry.type === 'Link') {
210212
const linkparts = entry.linkpath.split(/\/|\\/)

test/unpack.js

+43
Original file line numberDiff line numberDiff line change
@@ -2658,3 +2658,46 @@ t.test('drop entry from dirCache if no longer a directory', t => {
26582658
check(t, path)
26592659
})
26602660
})
2661+
2662+
t.test('using strip option when top level file exists', t => {
2663+
const dir = path.resolve(unpackdir, 'strip-with-top-file')
2664+
mkdirp.sync(dir + '/sync/y')
2665+
mkdirp.sync(dir + '/async/y')
2666+
const data = makeTar([
2667+
{
2668+
path: 'top',
2669+
type: 'File',
2670+
size: 0,
2671+
},
2672+
{
2673+
path: 'x',
2674+
type: 'Directory',
2675+
},
2676+
{
2677+
path: 'x/a',
2678+
type: 'File',
2679+
size: 'a'.length,
2680+
},
2681+
'a',
2682+
'',
2683+
'',
2684+
])
2685+
t.plan(2)
2686+
const check = (t, path) => {
2687+
t.equal(fs.statSync(path).isDirectory(), true)
2688+
t.equal(fs.lstatSync(path + '/a').isFile(), true)
2689+
t.throws(() => fs.statSync(path + '/top'), { code: 'ENOENT' })
2690+
t.end()
2691+
}
2692+
t.test('async', t => {
2693+
const path = dir + '/async'
2694+
new Unpack({ cwd: path, strip: 1 })
2695+
.on('end', () => check(t, path))
2696+
.end(data)
2697+
})
2698+
t.test('sync', t => {
2699+
const path = dir + '/sync'
2700+
new UnpackSync({ cwd: path, strip: 1 }).end(data)
2701+
check(t, path)
2702+
})
2703+
})

0 commit comments

Comments
 (0)