tar 5.0.4 → 5.0.8

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,21 @@ 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('/')
211
222
  }
212
223
  }
213
224
 
214
225
  if (!this.preservePaths) {
215
- const p = entry.path
216
- if (p.match(/(^|\/|\\)\.\.(\\|\/|$)/)) {
226
+ const p = normPath(entry.path)
227
+ if (p.split('/').includes('..')) {
217
228
  this.warn('TAR_ENTRY_ERROR', `path contains '..'`, {
218
229
  entry,
219
230
  path: p,
@@ -223,29 +234,36 @@ class Unpack extends Parser {
223
234
 
224
235
  // absolutes on posix are also absolutes on win32
225
236
  // 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`, {
237
+ const [root, stripped] = stripAbsolutePath(p)
238
+ if (root) {
239
+ entry.path = stripped
240
+ this.warn('TAR_ENTRY_INFO', `stripping ${root} from absolute path`, {
231
241
  entry,
232
242
  path: p,
233
243
  })
234
244
  }
235
245
  }
236
246
 
247
+ if (path.isAbsolute(entry.path))
248
+ entry.absolute = normPath(path.resolve(entry.path))
249
+ else
250
+ entry.absolute = normPath(path.resolve(this.cwd, entry.path))
251
+
252
+ // an archive can set properties on the extraction directory, but it
253
+ // may not replace the cwd with a different kind of thing entirely.
254
+ if (entry.absolute === this.cwd &&
255
+ entry.type !== 'Directory' &&
256
+ entry.type !== 'GNUDumpDir')
257
+ return false
258
+
237
259
  // only encode : chars that aren't drive letter indicators
238
260
  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))
261
+ const { root: aRoot } = path.win32.parse(entry.absolute)
262
+ entry.absolute = aRoot + wc.encode(entry.absolute.substr(aRoot.length))
263
+ const { root: pRoot } = path.win32.parse(entry.path)
264
+ entry.path = pRoot + wc.encode(entry.path.substr(pRoot.length))
242
265
  }
243
266
 
244
- if (path.isAbsolute(entry.path))
245
- entry.absolute = entry.path
246
- else
247
- entry.absolute = path.resolve(this.cwd, entry.path)
248
-
249
267
  return true
250
268
  }
251
269
 
@@ -289,7 +307,7 @@ class Unpack extends Parser {
289
307
  }
290
308
 
291
309
  [MKDIR] (dir, mode, cb) {
292
- mkdir(dir, {
310
+ mkdir(normPath(dir), {
293
311
  uid: this.uid,
294
312
  gid: this.gid,
295
313
  processUid: this.processUid,
@@ -330,17 +348,37 @@ class Unpack extends Parser {
330
348
  mode: mode,
331
349
  autoClose: false
332
350
  })
333
- stream.on('error', er => this[ONERROR](er, entry))
351
+ stream.on('error', er => {
352
+ if (stream.fd)
353
+ fs.close(stream.fd, () => {})
354
+
355
+ // flush all the data out so that we aren't left hanging
356
+ // if the error wasn't actually fatal. otherwise the parse
357
+ // is blocked, and we never proceed.
358
+ stream.write = () => true
359
+ this[ONERROR](er, entry)
360
+ fullyDone()
361
+ })
334
362
 
335
363
  let actions = 1
336
364
  const done = er => {
337
- if (er)
338
- return this[ONERROR](er, entry)
365
+ if (er) {
366
+ /* istanbul ignore else - we should always have a fd by now */
367
+ if (stream.fd)
368
+ fs.close(stream.fd, () => {})
369
+
370
+ this[ONERROR](er, entry)
371
+ fullyDone()
372
+ return
373
+ }
339
374
 
340
375
  if (--actions === 0) {
341
376
  fs.close(stream.fd, er => {
377
+ if (er)
378
+ this[ONERROR](er, entry)
379
+ else
380
+ this[UNPEND]()
342
381
  fullyDone()
343
- er ? this[ONERROR](er, entry) : this[UNPEND]()
344
382
  })
345
383
  }
346
384
  }
@@ -375,7 +413,10 @@ class Unpack extends Parser {
375
413
 
376
414
  const tx = this.transform ? this.transform(entry) || entry : entry
377
415
  if (tx !== entry) {
378
- tx.on('error', er => this[ONERROR](er, entry))
416
+ tx.on('error', er => {
417
+ this[ONERROR](er, entry)
418
+ fullyDone()
419
+ })
379
420
  entry.pipe(tx)
380
421
  }
381
422
  tx.pipe(stream)
@@ -385,8 +426,9 @@ class Unpack extends Parser {
385
426
  const mode = entry.mode & 0o7777 || this.dmode
386
427
  this[MKDIR](entry.absolute, mode, er => {
387
428
  if (er) {
429
+ this[ONERROR](er, entry)
388
430
  fullyDone()
389
- return this[ONERROR](er, entry)
431
+ return
390
432
  }
391
433
 
392
434
  let actions = 1
@@ -424,7 +466,8 @@ class Unpack extends Parser {
424
466
  }
425
467
 
426
468
  [HARDLINK] (entry, done) {
427
- this[LINK](entry, path.resolve(this.cwd, entry.linkpath), 'link', done)
469
+ const linkpath = normPath(path.resolve(this.cwd, entry.linkpath))
470
+ this[LINK](entry, linkpath, 'link', done)
428
471
  }
429
472
 
430
473
  [PEND] () {
@@ -461,36 +504,91 @@ class Unpack extends Parser {
461
504
  this.reservations.reserve(paths, done => this[CHECKFS2](entry, done))
462
505
  }
463
506
  [CHECKFS2] (entry, done) {
464
- this[MKDIR](path.dirname(entry.absolute), this.dmode, er => {
465
- if (er) {
466
- done()
467
- return this[ONERROR](er, entry)
507
+ // if we are not creating a directory, and the path is in the dirCache,
508
+ // then that means we are about to delete the directory we created
509
+ // previously, and it is no longer going to be a directory, and neither
510
+ // is any of its children.
511
+ if (entry.type !== 'Directory')
512
+ pruneCache(this.dirCache, entry.absolute)
513
+
514
+ const checkCwd = () => {
515
+ this[MKDIR](this.cwd, this.dmode, er => {
516
+ if (er) {
517
+ this[ONERROR](er, entry)
518
+ done()
519
+ return
520
+ }
521
+ this[CHECKED_CWD] = true
522
+ start()
523
+ })
524
+ }
525
+
526
+ const start = () => {
527
+ if (entry.absolute !== this.cwd) {
528
+ const parent = normPath(path.dirname(entry.absolute))
529
+ if (parent !== this.cwd) {
530
+ return this[MKDIR](parent, this.dmode, er => {
531
+ if (er) {
532
+ this[ONERROR](er, entry)
533
+ done()
534
+ return
535
+ }
536
+ afterMakeParent()
537
+ })
538
+ }
468
539
  }
469
- fs.lstat(entry.absolute, (er, st) => {
540
+ afterMakeParent()
541
+ }
542
+
543
+ const afterMakeParent = () => {
544
+ fs.lstat(entry.absolute, (lstatEr, st) => {
470
545
  if (st && (this.keep || this.newer && st.mtime > entry.mtime)) {
471
546
  this[SKIP](entry)
472
547
  done()
473
- } else if (er || this[ISREUSABLE](entry, st)) {
474
- this[MAKEFS](null, entry, done)
548
+ return
475
549
  }
476
- else if (st.isDirectory()) {
550
+ if (lstatEr || this[ISREUSABLE](entry, st))
551
+ return this[MAKEFS](null, entry, done)
552
+
553
+ if (st.isDirectory()) {
477
554
  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))
555
+ const needChmod = !this.noChmod &&
556
+ entry.mode &&
557
+ (st.mode & 0o7777) !== entry.mode
558
+ const afterChmod = er => this[MAKEFS](er, entry, done)
559
+ if (!needChmod)
560
+ return afterChmod()
561
+ return fs.chmod(entry.absolute, entry.mode, afterChmod)
562
+ }
563
+ // not a dir entry, have to remove it.
564
+ if (entry.absolute !== this.cwd) {
565
+ return fs.rmdir(entry.absolute, er =>
566
+ this[MAKEFS](er, entry, done))
567
+ }
568
+ }
569
+
570
+ // not a dir, and not reusable
571
+ // don't remove if the cwd, we want that error
572
+ if (entry.absolute === this.cwd)
573
+ return this[MAKEFS](null, entry, done)
574
+
575
+ unlinkFile(entry.absolute, er =>
576
+ this[MAKEFS](er, entry, done))
487
577
  })
488
- })
578
+ }
579
+
580
+ if (this[CHECKED_CWD])
581
+ start()
582
+ else
583
+ checkCwd()
489
584
  }
490
585
 
491
586
  [MAKEFS] (er, entry, done) {
492
- if (er)
493
- return this[ONERROR](er, entry)
587
+ if (er) {
588
+ this[ONERROR](er, entry)
589
+ done()
590
+ return
591
+ }
494
592
 
495
593
  switch (entry.type) {
496
594
  case 'File':
@@ -511,50 +609,80 @@ class Unpack extends Parser {
511
609
  }
512
610
 
513
611
  [LINK] (entry, linkpath, link, done) {
514
- // XXX: get the type ('file' or 'dir') for windows
612
+ // XXX: get the type ('symlink' or 'junction') for windows
515
613
  fs[link](linkpath, entry.absolute, er => {
516
614
  if (er)
517
- return this[ONERROR](er, entry)
615
+ this[ONERROR](er, entry)
616
+ else {
617
+ this[UNPEND]()
618
+ entry.resume()
619
+ }
518
620
  done()
519
- this[UNPEND]()
520
- entry.resume()
521
621
  })
522
622
  }
523
623
  }
524
624
 
625
+ const callSync = fn => {
626
+ try {
627
+ return [null, fn()]
628
+ } catch (er) {
629
+ return [er, null]
630
+ }
631
+ }
525
632
  class UnpackSync extends Unpack {
526
- constructor (opt) {
527
- super(opt)
633
+ [MAKEFS] (er, entry) {
634
+ return super[MAKEFS](er, entry, () => {})
528
635
  }
529
636
 
530
637
  [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
- }
638
+ if (entry.type !== 'Directory')
639
+ pruneCache(this.dirCache, entry.absolute)
640
+
641
+ if (!this[CHECKED_CWD]) {
642
+ const er = this[MKDIR](this.cwd, this.dmode)
643
+ if (er)
644
+ return this[ONERROR](er, entry)
645
+ this[CHECKED_CWD] = true
646
+ }
647
+
648
+ // don't bother to make the parent if the current entry is the cwd,
649
+ // we've already checked it.
650
+ if (entry.absolute !== this.cwd) {
651
+ const parent = normPath(path.dirname(entry.absolute))
652
+ if (parent !== this.cwd) {
653
+ const mkParent = this[MKDIR](parent, this.dmode)
654
+ if (mkParent)
655
+ return this[ONERROR](mkParent, entry)
554
656
  }
555
- } catch (er) {
556
- return this[MAKEFS](null, entry, neverCalled)
557
657
  }
658
+
659
+ const [lstatEr, st] = callSync(() => fs.lstatSync(entry.absolute))
660
+ if (st && (this.keep || this.newer && st.mtime > entry.mtime))
661
+ return this[SKIP](entry)
662
+
663
+ if (lstatEr || this[ISREUSABLE](entry, st))
664
+ return this[MAKEFS](null, entry)
665
+
666
+ if (st.isDirectory()) {
667
+ if (entry.type === 'Directory') {
668
+ const needChmod = !this.noChmod &&
669
+ entry.mode &&
670
+ (st.mode & 0o7777) !== entry.mode
671
+ const [er] = needChmod ? callSync(() => {
672
+ fs.chmodSync(entry.absolute, entry.mode)
673
+ }) : []
674
+ return this[MAKEFS](er, entry)
675
+ }
676
+ // not a dir entry, have to remove it
677
+ const [er] = callSync(() => fs.rmdirSync(entry.absolute))
678
+ this[MAKEFS](er, entry)
679
+ }
680
+
681
+ // not a dir, and not reusable.
682
+ // don't remove if it's the cwd, since we want that error.
683
+ const [er] = entry.absolute === this.cwd ? []
684
+ : callSync(() => unlinkFileSync(entry.absolute))
685
+ this[MAKEFS](er, entry)
558
686
  }
559
687
 
560
688
  [FILE] (entry, _) {
@@ -649,7 +777,7 @@ class UnpackSync extends Unpack {
649
777
 
650
778
  [MKDIR] (dir, mode) {
651
779
  try {
652
- return mkdir.sync(dir, {
780
+ return mkdir.sync(normPath(dir), {
653
781
  uid: this.uid,
654
782
  gid: this.gid,
655
783
  processUid: this.processUid,