tar 4.4.13 → 4.4.17

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