tar 4.4.15 → 4.4.16

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
@@ -1,5 +1,11 @@
1
1
  'use strict'
2
2
 
3
+ // the PEND/UNPEND stuff tracks whether we're ready to emit end/close yet.
4
+ // but the path reservations are required to avoid race conditions where
5
+ // parallelized unpack ops may mess with one another, due to dependencies
6
+ // (like a Link depending on its target) or destructive operations (like
7
+ // clobbering an fs object to create one of a different type.)
8
+
3
9
  const assert = require('assert')
4
10
  const EE = require('events').EventEmitter
5
11
  const Parser = require('./parse.js')
@@ -10,9 +16,12 @@ const mkdir = require('./mkdir.js')
10
16
  const mkdirSync = mkdir.sync
11
17
  const wc = require('./winchars.js')
12
18
  const stripAbsolutePath = require('./strip-absolute-path.js')
19
+ const pathReservations = require('./path-reservations.js')
20
+ const normPath = require('./normalize-windows-path.js')
13
21
 
14
22
  const ONENTRY = Symbol('onEntry')
15
23
  const CHECKFS = Symbol('checkFs')
24
+ const CHECKFS2 = Symbol('checkFs2')
16
25
  const ISREUSABLE = Symbol('isReusable')
17
26
  const MAKEFS = Symbol('makeFs')
18
27
  const FILE = Symbol('file')
@@ -34,6 +43,7 @@ const SKIP = Symbol('skip')
34
43
  const DOCHOWN = Symbol('doChown')
35
44
  const UID = Symbol('uid')
36
45
  const GID = Symbol('gid')
46
+ const CHECKED_CWD = Symbol('checkedCwd')
37
47
  const crypto = require('crypto')
38
48
 
39
49
  // Unlinks on Windows are not atomic.
@@ -80,6 +90,17 @@ const uint32 = (a, b, c) =>
80
90
  : b === b >>> 0 ? b
81
91
  : c
82
92
 
93
+ const pruneCache = (cache, abs) => {
94
+ // clear the cache if it's a case-insensitive match, since we can't
95
+ // know if the current file system is case-sensitive or not.
96
+ abs = normPath(abs).toLowerCase()
97
+ for (const path of cache.keys()) {
98
+ const plower = path.toLowerCase()
99
+ if (plower === abs || plower.toLowerCase().indexOf(abs + '/') === 0)
100
+ cache.delete(path)
101
+ }
102
+ }
103
+
83
104
  class Unpack extends Parser {
84
105
  constructor (opt) {
85
106
  if (!opt)
@@ -92,6 +113,10 @@ class Unpack extends Parser {
92
113
 
93
114
  super(opt)
94
115
 
116
+ this[CHECKED_CWD] = false
117
+
118
+ this.reservations = pathReservations()
119
+
95
120
  this.transform = typeof opt.transform === 'function' ? opt.transform : null
96
121
 
97
122
  this.writable = true
@@ -154,7 +179,7 @@ class Unpack extends Parser {
154
179
  // links, and removes symlink directories rather than erroring
155
180
  this.unlink = !!opt.unlink
156
181
 
157
- this.cwd = path.resolve(opt.cwd || process.cwd())
182
+ this.cwd = normPath(path.resolve(opt.cwd || process.cwd()))
158
183
  this.strip = +opt.strip || 0
159
184
  this.processUmask = process.umask()
160
185
  this.umask = typeof opt.umask === 'number' ? opt.umask : this.processUmask
@@ -175,22 +200,22 @@ class Unpack extends Parser {
175
200
 
176
201
  [CHECKPATH] (entry) {
177
202
  if (this.strip) {
178
- const parts = entry.path.split(/\/|\\/)
203
+ const parts = normPath(entry.path).split('/')
179
204
  if (parts.length < this.strip)
180
205
  return false
181
206
  entry.path = parts.slice(this.strip).join('/')
182
207
 
183
208
  if (entry.type === 'Link') {
184
- const linkparts = entry.linkpath.split(/\/|\\/)
209
+ const linkparts = normPath(entry.linkpath).split('/')
185
210
  if (linkparts.length >= this.strip)
186
211
  entry.linkpath = linkparts.slice(this.strip).join('/')
187
212
  }
188
213
  }
189
214
 
190
215
  if (!this.preservePaths) {
191
- const p = entry.path
192
- if (p.match(/(^|\/|\\)\.\.(\\|\/|$)/)) {
193
- this.warn('path contains \'..\'', p)
216
+ const p = normPath(entry.path)
217
+ if (p.split('/').includes('..')) {
218
+ this.warn(`path contains '..'`, p)
194
219
  return false
195
220
  }
196
221
 
@@ -203,18 +228,26 @@ class Unpack extends Parser {
203
228
  }
204
229
  }
205
230
 
231
+ if (path.isAbsolute(entry.path))
232
+ entry.absolute = normPath(path.resolve(entry.path))
233
+ else
234
+ entry.absolute = normPath(path.resolve(this.cwd, entry.path))
235
+
236
+ // an archive can set properties on the extraction directory, but it
237
+ // may not replace the cwd with a different kind of thing entirely.
238
+ if (entry.absolute === this.cwd &&
239
+ entry.type !== 'Directory' &&
240
+ entry.type !== 'GNUDumpDir')
241
+ return false
242
+
206
243
  // only encode : chars that aren't drive letter indicators
207
244
  if (this.win32) {
208
- const parsed = path.win32.parse(entry.path)
209
- entry.path = parsed.root === '' ? wc.encode(entry.path)
210
- : parsed.root + wc.encode(entry.path.substr(parsed.root.length))
245
+ const { root: aRoot } = path.win32.parse(entry.absolute)
246
+ entry.absolute = aRoot + wc.encode(entry.absolute.substr(aRoot.length))
247
+ const { root: pRoot } = path.win32.parse(entry.path)
248
+ entry.path = pRoot + wc.encode(entry.path.substr(pRoot.length))
211
249
  }
212
250
 
213
- if (path.isAbsolute(entry.path))
214
- entry.absolute = entry.path
215
- else
216
- entry.absolute = path.resolve(this.cwd, entry.path)
217
-
218
251
  return true
219
252
  }
220
253
 
@@ -258,7 +291,7 @@ class Unpack extends Parser {
258
291
  }
259
292
 
260
293
  [MKDIR] (dir, mode, cb) {
261
- mkdir(dir, {
294
+ mkdir(normPath(dir), {
262
295
  uid: this.uid,
263
296
  gid: this.gid,
264
297
  processUid: this.processUid,
@@ -292,21 +325,44 @@ class Unpack extends Parser {
292
325
  return uint32(this.gid, entry.gid, this.processGid)
293
326
  }
294
327
 
295
- [FILE] (entry) {
328
+ [FILE] (entry, fullyDone) {
296
329
  const mode = entry.mode & 0o7777 || this.fmode
297
330
  const stream = new fsm.WriteStream(entry.absolute, {
298
331
  mode: mode,
299
332
  autoClose: false
300
333
  })
301
- stream.on('error', er => this[ONERROR](er, entry))
334
+ stream.on('error', er => {
335
+ if (stream.fd)
336
+ fs.close(stream.fd, () => {})
337
+
338
+ // flush all the data out so that we aren't left hanging
339
+ // if the error wasn't actually fatal. otherwise the parse
340
+ // is blocked, and we never proceed.
341
+ /* istanbul ignore next */
342
+ stream.write = () => true
343
+ this[ONERROR](er, entry)
344
+ fullyDone()
345
+ })
302
346
 
303
347
  let actions = 1
304
348
  const done = er => {
305
- if (er)
306
- return this[ONERROR](er, entry)
349
+ if (er) {
350
+ /* istanbul ignore else - we should always have a fd by now */
351
+ if (stream.fd)
352
+ fs.close(stream.fd, () => {})
307
353
 
308
- if (--actions === 0)
309
- fs.close(stream.fd, _ => this[UNPEND]())
354
+ this[ONERROR](er, entry)
355
+ fullyDone()
356
+ return
357
+ }
358
+
359
+ if (--actions === 0) {
360
+ fs.close(stream.fd, er => {
361
+ fullyDone()
362
+ /* istanbul ignore next */
363
+ er ? this[ONERROR](er, entry) : this[UNPEND]()
364
+ })
365
+ }
310
366
  }
311
367
 
312
368
  stream.on('finish', _ => {
@@ -345,15 +401,18 @@ class Unpack extends Parser {
345
401
  tx.pipe(stream)
346
402
  }
347
403
 
348
- [DIRECTORY] (entry) {
404
+ [DIRECTORY] (entry, fullyDone) {
349
405
  const mode = entry.mode & 0o7777 || this.dmode
350
406
  this[MKDIR](entry.absolute, mode, er => {
351
- if (er)
407
+ if (er) {
408
+ fullyDone()
352
409
  return this[ONERROR](er, entry)
410
+ }
353
411
 
354
412
  let actions = 1
355
413
  const done = _ => {
356
414
  if (--actions === 0) {
415
+ fullyDone()
357
416
  this[UNPEND]()
358
417
  entry.resume()
359
418
  }
@@ -378,12 +437,13 @@ class Unpack extends Parser {
378
437
  entry.resume()
379
438
  }
380
439
 
381
- [SYMLINK] (entry) {
382
- this[LINK](entry, entry.linkpath, 'symlink')
440
+ [SYMLINK] (entry, done) {
441
+ this[LINK](entry, entry.linkpath, 'symlink', done)
383
442
  }
384
443
 
385
- [HARDLINK] (entry) {
386
- this[LINK](entry, path.resolve(this.cwd, entry.linkpath), 'link')
444
+ [HARDLINK] (entry, done) {
445
+ const linkpath = normPath(path.resolve(this.cwd, entry.linkpath))
446
+ this[LINK](entry, linkpath, 'link', done)
387
447
  }
388
448
 
389
449
  [PEND] () {
@@ -414,43 +474,92 @@ class Unpack extends Parser {
414
474
  // check if a thing is there, and if so, try to clobber it
415
475
  [CHECKFS] (entry) {
416
476
  this[PEND]()
417
-
477
+ const paths = [entry.path]
478
+ if (entry.linkpath)
479
+ paths.push(entry.linkpath)
480
+ this.reservations.reserve(paths, done => this[CHECKFS2](entry, done))
481
+ }
482
+ [CHECKFS2] (entry, done) {
418
483
  // if we are not creating a directory, and the path is in the dirCache,
419
484
  // then that means we are about to delete the directory we created
420
485
  // previously, and it is no longer going to be a directory, and neither
421
486
  // is any of its children.
422
- if (entry.type !== 'Directory') {
423
- for (const path of this.dirCache.keys()) {
424
- if (path === entry.absolute ||
425
- path.indexOf(entry.absolute + '/') === 0 ||
426
- path.indexOf(entry.absolute + '\\') === 0)
427
- this.dirCache.delete(path)
487
+ if (entry.type !== 'Directory')
488
+ pruneCache(this.dirCache, entry.absolute)
489
+
490
+ const checkCwd = () => {
491
+ this[MKDIR](this.cwd, this.dmode, er => {
492
+ if (er) {
493
+ this[ONERROR](er, entry)
494
+ done()
495
+ return
496
+ }
497
+ this[CHECKED_CWD] = true
498
+ start()
499
+ })
500
+ }
501
+
502
+ const start = () => {
503
+ if (entry.absolute !== this.cwd) {
504
+ const parent = normPath(path.dirname(entry.absolute))
505
+ if (parent !== this.cwd) {
506
+ return this[MKDIR](parent, this.dmode, er => {
507
+ if (er) {
508
+ this[ONERROR](er, entry)
509
+ done()
510
+ return
511
+ }
512
+ afterMakeParent()
513
+ })
514
+ }
428
515
  }
516
+ afterMakeParent()
429
517
  }
430
518
 
431
- this[MKDIR](path.dirname(entry.absolute), this.dmode, er => {
432
- if (er)
433
- return this[ONERROR](er, entry)
434
- fs.lstat(entry.absolute, (er, st) => {
435
- if (st && (this.keep || this.newer && st.mtime > entry.mtime))
519
+ const afterMakeParent = () => {
520
+ fs.lstat(entry.absolute, (lstatEr, st) => {
521
+ if (st && (this.keep || this.newer && st.mtime > entry.mtime)) {
436
522
  this[SKIP](entry)
437
- else if (er || this[ISREUSABLE](entry, st))
438
- this[MAKEFS](null, entry)
439
- else if (st.isDirectory()) {
523
+ done()
524
+ return
525
+ }
526
+ if (lstatEr || this[ISREUSABLE](entry, st))
527
+ return this[MAKEFS](null, entry, done)
528
+
529
+ if (st.isDirectory()) {
440
530
  if (entry.type === 'Directory') {
441
- if (!entry.mode || (st.mode & 0o7777) === entry.mode)
442
- this[MAKEFS](null, entry)
443
- else
444
- fs.chmod(entry.absolute, entry.mode, er => this[MAKEFS](er, entry))
445
- } else
446
- fs.rmdir(entry.absolute, er => this[MAKEFS](er, entry))
447
- } else
448
- unlinkFile(entry.absolute, er => this[MAKEFS](er, entry))
531
+ const needChmod = !this.noChmod &&
532
+ entry.mode &&
533
+ (st.mode & 0o7777) !== entry.mode
534
+ const afterChmod = er => this[MAKEFS](er, entry, done)
535
+ if (!needChmod)
536
+ return afterChmod()
537
+ return fs.chmod(entry.absolute, entry.mode, afterChmod)
538
+ }
539
+ // not a dir entry, have to remove it.
540
+ if (entry.absolute !== this.cwd) {
541
+ return fs.rmdir(entry.absolute, er =>
542
+ this[MAKEFS](er, entry, done))
543
+ }
544
+ }
545
+
546
+ // not a dir, and not reusable
547
+ // don't remove if the cwd, we want that error
548
+ if (entry.absolute === this.cwd)
549
+ return this[MAKEFS](null, entry, done)
550
+
551
+ unlinkFile(entry.absolute, er =>
552
+ this[MAKEFS](er, entry, done))
449
553
  })
450
- })
554
+ }
555
+
556
+ if (this[CHECKED_CWD])
557
+ start()
558
+ else
559
+ checkCwd()
451
560
  }
452
561
 
453
- [MAKEFS] (er, entry) {
562
+ [MAKEFS] (er, entry, done) {
454
563
  if (er)
455
564
  return this[ONERROR](er, entry)
456
565
 
@@ -458,76 +567,96 @@ class Unpack extends Parser {
458
567
  case 'File':
459
568
  case 'OldFile':
460
569
  case 'ContiguousFile':
461
- return this[FILE](entry)
570
+ return this[FILE](entry, done)
462
571
 
463
572
  case 'Link':
464
- return this[HARDLINK](entry)
573
+ return this[HARDLINK](entry, done)
465
574
 
466
575
  case 'SymbolicLink':
467
- return this[SYMLINK](entry)
576
+ return this[SYMLINK](entry, done)
468
577
 
469
578
  case 'Directory':
470
579
  case 'GNUDumpDir':
471
- return this[DIRECTORY](entry)
580
+ return this[DIRECTORY](entry, done)
472
581
  }
473
582
  }
474
583
 
475
- [LINK] (entry, linkpath, link) {
476
- // XXX: get the type ('file' or 'dir') for windows
584
+ [LINK] (entry, linkpath, link, done) {
585
+ // XXX: get the type ('symlink' or 'junction') for windows
477
586
  fs[link](linkpath, entry.absolute, er => {
478
587
  if (er)
479
588
  return this[ONERROR](er, entry)
589
+ done()
480
590
  this[UNPEND]()
481
591
  entry.resume()
482
592
  })
483
593
  }
484
594
  }
485
595
 
596
+ const callSync = fn => {
597
+ try {
598
+ return [null, fn()]
599
+ } catch (er) {
600
+ return [er, null]
601
+ }
602
+ }
486
603
  class UnpackSync extends Unpack {
487
- constructor (opt) {
488
- super(opt)
604
+ [MAKEFS] (er, entry) {
605
+ return super[MAKEFS](er, entry, /* istanbul ignore next */ () => {})
489
606
  }
490
607
 
491
608
  [CHECKFS] (entry) {
492
- if (entry.type !== 'Directory') {
493
- for (const path of this.dirCache.keys()) {
494
- if (path === entry.absolute ||
495
- path.indexOf(entry.absolute + '/') === 0 ||
496
- path.indexOf(entry.absolute + '\\') === 0)
497
- this.dirCache.delete(path)
498
- }
609
+ if (entry.type !== 'Directory')
610
+ pruneCache(this.dirCache, entry.absolute)
611
+
612
+ if (!this[CHECKED_CWD]) {
613
+ const er = this[MKDIR](this.cwd, this.dmode)
614
+ if (er)
615
+ return this[ONERROR](er, entry)
616
+ this[CHECKED_CWD] = true
499
617
  }
500
618
 
501
- const er = this[MKDIR](path.dirname(entry.absolute), this.dmode)
502
- if (er)
503
- return this[ONERROR](er, entry)
504
- try {
505
- const st = fs.lstatSync(entry.absolute)
506
- if (this.keep || this.newer && st.mtime > entry.mtime)
507
- return this[SKIP](entry)
508
- else if (this[ISREUSABLE](entry, st))
509
- return this[MAKEFS](null, entry)
510
- else {
511
- try {
512
- if (st.isDirectory()) {
513
- if (entry.type === 'Directory') {
514
- if (entry.mode && (st.mode & 0o7777) !== entry.mode)
515
- fs.chmodSync(entry.absolute, entry.mode)
516
- } else
517
- fs.rmdirSync(entry.absolute)
518
- } else
519
- unlinkFileSync(entry.absolute)
520
- return this[MAKEFS](null, entry)
521
- } catch (er) {
522
- return this[ONERROR](er, entry)
523
- }
619
+ // don't bother to make the parent if the current entry is the cwd,
620
+ // we've already checked it.
621
+ if (entry.absolute !== this.cwd) {
622
+ const parent = normPath(path.dirname(entry.absolute))
623
+ if (parent !== this.cwd) {
624
+ const mkParent = this[MKDIR](parent, this.dmode)
625
+ if (mkParent)
626
+ return this[ONERROR](mkParent, entry)
524
627
  }
525
- } catch (er) {
628
+ }
629
+
630
+ const [lstatEr, st] = callSync(() => fs.lstatSync(entry.absolute))
631
+ if (st && (this.keep || this.newer && st.mtime > entry.mtime))
632
+ return this[SKIP](entry)
633
+
634
+ if (lstatEr || this[ISREUSABLE](entry, st))
526
635
  return this[MAKEFS](null, entry)
636
+
637
+ if (st.isDirectory()) {
638
+ if (entry.type === 'Directory') {
639
+ const needChmod = !this.noChmod &&
640
+ entry.mode &&
641
+ (st.mode & 0o7777) !== entry.mode
642
+ const [er] = needChmod ? callSync(() => {
643
+ fs.chmodSync(entry.absolute, entry.mode)
644
+ }) : []
645
+ return this[MAKEFS](er, entry)
646
+ }
647
+ // not a dir entry, have to remove it
648
+ const [er] = callSync(() => fs.rmdirSync(entry.absolute))
649
+ this[MAKEFS](er, entry)
527
650
  }
651
+
652
+ // not a dir, and not reusable.
653
+ // don't remove if it's the cwd, since we want that error.
654
+ const [er] = entry.absolute === this.cwd ? []
655
+ : callSync(() => unlinkFileSync(entry.absolute))
656
+ this[MAKEFS](er, entry)
528
657
  }
529
658
 
530
- [FILE] (entry) {
659
+ [FILE] (entry, _) {
531
660
  const mode = entry.mode & 0o7777 || this.fmode
532
661
 
533
662
  const oner = er => {
@@ -594,7 +723,7 @@ class UnpackSync extends Unpack {
594
723
  })
595
724
  }
596
725
 
597
- [DIRECTORY] (entry) {
726
+ [DIRECTORY] (entry, _) {
598
727
  const mode = entry.mode & 0o7777 || this.dmode
599
728
  const er = this[MKDIR](entry.absolute, mode)
600
729
  if (er)
@@ -614,7 +743,7 @@ class UnpackSync extends Unpack {
614
743
 
615
744
  [MKDIR] (dir, mode) {
616
745
  try {
617
- return mkdir.sync(dir, {
746
+ return mkdir.sync(normPath(dir), {
618
747
  uid: this.uid,
619
748
  gid: this.gid,
620
749
  processUid: this.processUid,
@@ -631,7 +760,7 @@ class UnpackSync extends Unpack {
631
760
  }
632
761
  }
633
762
 
634
- [LINK] (entry, linkpath, link) {
763
+ [LINK] (entry, linkpath, link, _) {
635
764
  try {
636
765
  fs[link + 'Sync'](linkpath, entry.absolute)
637
766
  entry.resume()