tar 5.0.5 → 5.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/unpack.js CHANGED
@@ -16,6 +16,8 @@ const mkdir = require('./mkdir.js')
16
16
  const mkdirSync = mkdir.sync
17
17
  const wc = require('./winchars.js')
18
18
  const pathReservations = require('./path-reservations.js')
19
+ const stripAbsolutePath = require('./strip-absolute-path.js')
20
+ const normPath = require('./normalize-windows-path.js')
19
21
 
20
22
  const ONENTRY = Symbol('onEntry')
21
23
  const CHECKFS = Symbol('checkFs')
@@ -41,14 +43,10 @@ const SKIP = Symbol('skip')
41
43
  const DOCHOWN = Symbol('doChown')
42
44
  const UID = Symbol('uid')
43
45
  const GID = Symbol('gid')
46
+ const CHECKED_CWD = Symbol('checkedCwd')
44
47
  const crypto = require('crypto')
45
48
  const getFlag = require('./get-write-flag.js')
46
49
 
47
- /* istanbul ignore next */
48
- const neverCalled = () => {
49
- throw new Error('sync function called cb somehow?!?')
50
- }
51
-
52
50
  // Unlinks on Windows are not atomic.
53
51
  //
54
52
  // This means that if you have a file entry, followed by another
@@ -93,6 +91,17 @@ const uint32 = (a, b, c) =>
93
91
  : b === b >>> 0 ? b
94
92
  : c
95
93
 
94
+ const pruneCache = (cache, abs) => {
95
+ // clear the cache if it's a case-insensitive match, since we can't
96
+ // know if the current file system is case-sensitive or not.
97
+ abs = normPath(abs).toLowerCase()
98
+ for (const path of cache.keys()) {
99
+ const plower = path.toLowerCase()
100
+ if (plower === abs || plower.toLowerCase().indexOf(abs + '/') === 0)
101
+ cache.delete(path)
102
+ }
103
+ }
104
+
96
105
  class Unpack extends Parser {
97
106
  constructor (opt) {
98
107
  if (!opt)
@@ -105,6 +114,8 @@ class Unpack extends Parser {
105
114
 
106
115
  super(opt)
107
116
 
117
+ this[CHECKED_CWD] = false
118
+
108
119
  this.reservations = pathReservations()
109
120
 
110
121
  this.transform = typeof opt.transform === 'function' ? opt.transform : null
@@ -169,7 +180,7 @@ class Unpack extends Parser {
169
180
  // links, and removes symlink directories rather than erroring
170
181
  this.unlink = !!opt.unlink
171
182
 
172
- this.cwd = path.resolve(opt.cwd || process.cwd())
183
+ this.cwd = normPath(path.resolve(opt.cwd || process.cwd()))
173
184
  this.strip = +opt.strip || 0
174
185
  this.processUmask = process.umask()
175
186
  this.umask = typeof opt.umask === 'number' ? opt.umask : this.processUmask
@@ -199,21 +210,23 @@ class Unpack extends Parser {
199
210
 
200
211
  [CHECKPATH] (entry) {
201
212
  if (this.strip) {
202
- const parts = entry.path.split(/\/|\\/)
213
+ const parts = normPath(entry.path).split('/')
203
214
  if (parts.length < this.strip)
204
215
  return false
205
216
  entry.path = parts.slice(this.strip).join('/')
206
217
 
207
218
  if (entry.type === 'Link') {
208
- const linkparts = entry.linkpath.split(/\/|\\/)
219
+ const linkparts = normPath(entry.linkpath).split('/')
209
220
  if (linkparts.length >= this.strip)
210
221
  entry.linkpath = linkparts.slice(this.strip).join('/')
222
+ else
223
+ return false
211
224
  }
212
225
  }
213
226
 
214
227
  if (!this.preservePaths) {
215
- const p = entry.path
216
- if (p.match(/(^|\/|\\)\.\.(\\|\/|$)/)) {
228
+ const p = normPath(entry.path)
229
+ if (p.split('/').includes('..')) {
217
230
  this.warn('TAR_ENTRY_ERROR', `path contains '..'`, {
218
231
  entry,
219
232
  path: p,
@@ -223,29 +236,36 @@ class Unpack extends Parser {
223
236
 
224
237
  // absolutes on posix are also absolutes on win32
225
238
  // so we only need to test this one to get both
226
- if (path.win32.isAbsolute(p)) {
227
- const parsed = path.win32.parse(p)
228
- entry.path = p.substr(parsed.root.length)
229
- const r = parsed.root
230
- this.warn('TAR_ENTRY_INFO', `stripping ${r} from absolute path`, {
239
+ const [root, stripped] = stripAbsolutePath(p)
240
+ if (root) {
241
+ entry.path = stripped
242
+ this.warn('TAR_ENTRY_INFO', `stripping ${root} from absolute path`, {
231
243
  entry,
232
244
  path: p,
233
245
  })
234
246
  }
235
247
  }
236
248
 
249
+ if (path.isAbsolute(entry.path))
250
+ entry.absolute = normPath(path.resolve(entry.path))
251
+ else
252
+ entry.absolute = normPath(path.resolve(this.cwd, entry.path))
253
+
254
+ // an archive can set properties on the extraction directory, but it
255
+ // may not replace the cwd with a different kind of thing entirely.
256
+ if (entry.absolute === this.cwd &&
257
+ entry.type !== 'Directory' &&
258
+ entry.type !== 'GNUDumpDir')
259
+ return false
260
+
237
261
  // only encode : chars that aren't drive letter indicators
238
262
  if (this.win32) {
239
- const parsed = path.win32.parse(entry.path)
240
- entry.path = parsed.root === '' ? wc.encode(entry.path)
241
- : parsed.root + wc.encode(entry.path.substr(parsed.root.length))
263
+ const { root: aRoot } = path.win32.parse(entry.absolute)
264
+ entry.absolute = aRoot + wc.encode(entry.absolute.substr(aRoot.length))
265
+ const { root: pRoot } = path.win32.parse(entry.path)
266
+ entry.path = pRoot + wc.encode(entry.path.substr(pRoot.length))
242
267
  }
243
268
 
244
- if (path.isAbsolute(entry.path))
245
- entry.absolute = entry.path
246
- else
247
- entry.absolute = path.resolve(this.cwd, entry.path)
248
-
249
269
  return true
250
270
  }
251
271
 
@@ -289,7 +309,7 @@ class Unpack extends Parser {
289
309
  }
290
310
 
291
311
  [MKDIR] (dir, mode, cb) {
292
- mkdir(dir, {
312
+ mkdir(normPath(dir), {
293
313
  uid: this.uid,
294
314
  gid: this.gid,
295
315
  processUid: this.processUid,
@@ -330,17 +350,37 @@ class Unpack extends Parser {
330
350
  mode: mode,
331
351
  autoClose: false
332
352
  })
333
- stream.on('error', er => this[ONERROR](er, entry))
353
+ stream.on('error', er => {
354
+ if (stream.fd)
355
+ fs.close(stream.fd, () => {})
356
+
357
+ // flush all the data out so that we aren't left hanging
358
+ // if the error wasn't actually fatal. otherwise the parse
359
+ // is blocked, and we never proceed.
360
+ stream.write = () => true
361
+ this[ONERROR](er, entry)
362
+ fullyDone()
363
+ })
334
364
 
335
365
  let actions = 1
336
366
  const done = er => {
337
- if (er)
338
- return this[ONERROR](er, entry)
367
+ if (er) {
368
+ /* istanbul ignore else - we should always have a fd by now */
369
+ if (stream.fd)
370
+ fs.close(stream.fd, () => {})
371
+
372
+ this[ONERROR](er, entry)
373
+ fullyDone()
374
+ return
375
+ }
339
376
 
340
377
  if (--actions === 0) {
341
378
  fs.close(stream.fd, er => {
379
+ if (er)
380
+ this[ONERROR](er, entry)
381
+ else
382
+ this[UNPEND]()
342
383
  fullyDone()
343
- er ? this[ONERROR](er, entry) : this[UNPEND]()
344
384
  })
345
385
  }
346
386
  }
@@ -375,7 +415,10 @@ class Unpack extends Parser {
375
415
 
376
416
  const tx = this.transform ? this.transform(entry) || entry : entry
377
417
  if (tx !== entry) {
378
- tx.on('error', er => this[ONERROR](er, entry))
418
+ tx.on('error', er => {
419
+ this[ONERROR](er, entry)
420
+ fullyDone()
421
+ })
379
422
  entry.pipe(tx)
380
423
  }
381
424
  tx.pipe(stream)
@@ -385,8 +428,9 @@ class Unpack extends Parser {
385
428
  const mode = entry.mode & 0o7777 || this.dmode
386
429
  this[MKDIR](entry.absolute, mode, er => {
387
430
  if (er) {
431
+ this[ONERROR](er, entry)
388
432
  fullyDone()
389
- return this[ONERROR](er, entry)
433
+ return
390
434
  }
391
435
 
392
436
  let actions = 1
@@ -424,7 +468,8 @@ class Unpack extends Parser {
424
468
  }
425
469
 
426
470
  [HARDLINK] (entry, done) {
427
- this[LINK](entry, path.resolve(this.cwd, entry.linkpath), 'link', done)
471
+ const linkpath = normPath(path.resolve(this.cwd, entry.linkpath))
472
+ this[LINK](entry, linkpath, 'link', done)
428
473
  }
429
474
 
430
475
  [PEND] () {
@@ -461,36 +506,91 @@ class Unpack extends Parser {
461
506
  this.reservations.reserve(paths, done => this[CHECKFS2](entry, done))
462
507
  }
463
508
  [CHECKFS2] (entry, done) {
464
- this[MKDIR](path.dirname(entry.absolute), this.dmode, er => {
465
- if (er) {
466
- done()
467
- return this[ONERROR](er, entry)
509
+ // if we are not creating a directory, and the path is in the dirCache,
510
+ // then that means we are about to delete the directory we created
511
+ // previously, and it is no longer going to be a directory, and neither
512
+ // is any of its children.
513
+ if (entry.type !== 'Directory')
514
+ pruneCache(this.dirCache, entry.absolute)
515
+
516
+ const checkCwd = () => {
517
+ this[MKDIR](this.cwd, this.dmode, er => {
518
+ if (er) {
519
+ this[ONERROR](er, entry)
520
+ done()
521
+ return
522
+ }
523
+ this[CHECKED_CWD] = true
524
+ start()
525
+ })
526
+ }
527
+
528
+ const start = () => {
529
+ if (entry.absolute !== this.cwd) {
530
+ const parent = normPath(path.dirname(entry.absolute))
531
+ if (parent !== this.cwd) {
532
+ return this[MKDIR](parent, this.dmode, er => {
533
+ if (er) {
534
+ this[ONERROR](er, entry)
535
+ done()
536
+ return
537
+ }
538
+ afterMakeParent()
539
+ })
540
+ }
468
541
  }
469
- fs.lstat(entry.absolute, (er, st) => {
542
+ afterMakeParent()
543
+ }
544
+
545
+ const afterMakeParent = () => {
546
+ fs.lstat(entry.absolute, (lstatEr, st) => {
470
547
  if (st && (this.keep || this.newer && st.mtime > entry.mtime)) {
471
548
  this[SKIP](entry)
472
549
  done()
473
- } else if (er || this[ISREUSABLE](entry, st)) {
474
- this[MAKEFS](null, entry, done)
550
+ return
475
551
  }
476
- else if (st.isDirectory()) {
552
+ if (lstatEr || this[ISREUSABLE](entry, st))
553
+ return this[MAKEFS](null, entry, done)
554
+
555
+ if (st.isDirectory()) {
477
556
  if (entry.type === 'Directory') {
478
- if (!entry.mode || (st.mode & 0o7777) === entry.mode)
479
- this[MAKEFS](null, entry, done)
480
- else
481
- fs.chmod(entry.absolute, entry.mode,
482
- er => this[MAKEFS](er, entry, done))
483
- } else
484
- fs.rmdir(entry.absolute, er => this[MAKEFS](er, entry, done))
485
- } else
486
- unlinkFile(entry.absolute, er => this[MAKEFS](er, entry, done))
557
+ const needChmod = !this.noChmod &&
558
+ entry.mode &&
559
+ (st.mode & 0o7777) !== entry.mode
560
+ const afterChmod = er => this[MAKEFS](er, entry, done)
561
+ if (!needChmod)
562
+ return afterChmod()
563
+ return fs.chmod(entry.absolute, entry.mode, afterChmod)
564
+ }
565
+ // not a dir entry, have to remove it.
566
+ if (entry.absolute !== this.cwd) {
567
+ return fs.rmdir(entry.absolute, er =>
568
+ this[MAKEFS](er, entry, done))
569
+ }
570
+ }
571
+
572
+ // not a dir, and not reusable
573
+ // don't remove if the cwd, we want that error
574
+ if (entry.absolute === this.cwd)
575
+ return this[MAKEFS](null, entry, done)
576
+
577
+ unlinkFile(entry.absolute, er =>
578
+ this[MAKEFS](er, entry, done))
487
579
  })
488
- })
580
+ }
581
+
582
+ if (this[CHECKED_CWD])
583
+ start()
584
+ else
585
+ checkCwd()
489
586
  }
490
587
 
491
588
  [MAKEFS] (er, entry, done) {
492
- if (er)
493
- return this[ONERROR](er, entry)
589
+ if (er) {
590
+ this[ONERROR](er, entry)
591
+ done()
592
+ return
593
+ }
494
594
 
495
595
  switch (entry.type) {
496
596
  case 'File':
@@ -511,50 +611,80 @@ class Unpack extends Parser {
511
611
  }
512
612
 
513
613
  [LINK] (entry, linkpath, link, done) {
514
- // XXX: get the type ('file' or 'dir') for windows
614
+ // XXX: get the type ('symlink' or 'junction') for windows
515
615
  fs[link](linkpath, entry.absolute, er => {
516
616
  if (er)
517
- return this[ONERROR](er, entry)
617
+ this[ONERROR](er, entry)
618
+ else {
619
+ this[UNPEND]()
620
+ entry.resume()
621
+ }
518
622
  done()
519
- this[UNPEND]()
520
- entry.resume()
521
623
  })
522
624
  }
523
625
  }
524
626
 
627
+ const callSync = fn => {
628
+ try {
629
+ return [null, fn()]
630
+ } catch (er) {
631
+ return [er, null]
632
+ }
633
+ }
525
634
  class UnpackSync extends Unpack {
526
- constructor (opt) {
527
- super(opt)
635
+ [MAKEFS] (er, entry) {
636
+ return super[MAKEFS](er, entry, () => {})
528
637
  }
529
638
 
530
639
  [CHECKFS] (entry) {
531
- const er = this[MKDIR](path.dirname(entry.absolute), this.dmode, neverCalled)
532
- if (er)
533
- return this[ONERROR](er, entry)
534
- try {
535
- const st = fs.lstatSync(entry.absolute)
536
- if (this.keep || this.newer && st.mtime > entry.mtime)
537
- return this[SKIP](entry)
538
- else if (this[ISREUSABLE](entry, st))
539
- return this[MAKEFS](null, entry, neverCalled)
540
- else {
541
- try {
542
- if (st.isDirectory()) {
543
- if (entry.type === 'Directory') {
544
- if (entry.mode && (st.mode & 0o7777) !== entry.mode)
545
- fs.chmodSync(entry.absolute, entry.mode)
546
- } else
547
- fs.rmdirSync(entry.absolute)
548
- } else
549
- unlinkFileSync(entry.absolute)
550
- return this[MAKEFS](null, entry, neverCalled)
551
- } catch (er) {
552
- return this[ONERROR](er, entry)
553
- }
640
+ if (entry.type !== 'Directory')
641
+ pruneCache(this.dirCache, entry.absolute)
642
+
643
+ if (!this[CHECKED_CWD]) {
644
+ const er = this[MKDIR](this.cwd, this.dmode)
645
+ if (er)
646
+ return this[ONERROR](er, entry)
647
+ this[CHECKED_CWD] = true
648
+ }
649
+
650
+ // don't bother to make the parent if the current entry is the cwd,
651
+ // we've already checked it.
652
+ if (entry.absolute !== this.cwd) {
653
+ const parent = normPath(path.dirname(entry.absolute))
654
+ if (parent !== this.cwd) {
655
+ const mkParent = this[MKDIR](parent, this.dmode)
656
+ if (mkParent)
657
+ return this[ONERROR](mkParent, entry)
554
658
  }
555
- } catch (er) {
556
- return this[MAKEFS](null, entry, neverCalled)
557
659
  }
660
+
661
+ const [lstatEr, st] = callSync(() => fs.lstatSync(entry.absolute))
662
+ if (st && (this.keep || this.newer && st.mtime > entry.mtime))
663
+ return this[SKIP](entry)
664
+
665
+ if (lstatEr || this[ISREUSABLE](entry, st))
666
+ return this[MAKEFS](null, entry)
667
+
668
+ if (st.isDirectory()) {
669
+ if (entry.type === 'Directory') {
670
+ const needChmod = !this.noChmod &&
671
+ entry.mode &&
672
+ (st.mode & 0o7777) !== entry.mode
673
+ const [er] = needChmod ? callSync(() => {
674
+ fs.chmodSync(entry.absolute, entry.mode)
675
+ }) : []
676
+ return this[MAKEFS](er, entry)
677
+ }
678
+ // not a dir entry, have to remove it
679
+ const [er] = callSync(() => fs.rmdirSync(entry.absolute))
680
+ this[MAKEFS](er, entry)
681
+ }
682
+
683
+ // not a dir, and not reusable.
684
+ // don't remove if it's the cwd, since we want that error.
685
+ const [er] = entry.absolute === this.cwd ? []
686
+ : callSync(() => unlinkFileSync(entry.absolute))
687
+ this[MAKEFS](er, entry)
558
688
  }
559
689
 
560
690
  [FILE] (entry, _) {
@@ -649,7 +779,7 @@ class UnpackSync extends Unpack {
649
779
 
650
780
  [MKDIR] (dir, mode) {
651
781
  try {
652
- return mkdir.sync(dir, {
782
+ return mkdir.sync(normPath(dir), {
653
783
  uid: this.uid,
654
784
  gid: this.gid,
655
785
  processUid: this.processUid,