tar 4.4.12 → 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')
@@ -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,54 @@ 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('/')
186
212
  }
187
213
  }
188
214
 
189
215
  if (!this.preservePaths) {
190
- const p = entry.path
191
- if (p.match(/(^|\/|\\)\.\.(\\|\/|$)/)) {
192
- this.warn('path contains \'..\'', p)
216
+ const p = normPath(entry.path)
217
+ if (p.split('/').includes('..')) {
218
+ this.warn(`path contains '..'`, p)
193
219
  return false
194
220
  }
195
221
 
196
222
  // absolutes on posix are also absolutes on win32
197
223
  // 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)
224
+ const s = stripAbsolutePath(p)
225
+ if (s[0]) {
226
+ entry.path = s[1]
227
+ this.warn(`stripping ${s[0]} from absolute path`, p)
202
228
  }
203
229
  }
204
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
+
205
243
  // only encode : chars that aren't drive letter indicators
206
244
  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))
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))
210
249
  }
211
250
 
212
- if (path.isAbsolute(entry.path))
213
- entry.absolute = entry.path
214
- else
215
- entry.absolute = path.resolve(this.cwd, entry.path)
216
-
217
251
  return true
218
252
  }
219
253
 
@@ -257,7 +291,7 @@ class Unpack extends Parser {
257
291
  }
258
292
 
259
293
  [MKDIR] (dir, mode, cb) {
260
- mkdir(dir, {
294
+ mkdir(normPath(dir), {
261
295
  uid: this.uid,
262
296
  gid: this.gid,
263
297
  processUid: this.processUid,
@@ -291,21 +325,44 @@ class Unpack extends Parser {
291
325
  return uint32(this.gid, entry.gid, this.processGid)
292
326
  }
293
327
 
294
- [FILE] (entry) {
328
+ [FILE] (entry, fullyDone) {
295
329
  const mode = entry.mode & 0o7777 || this.fmode
296
330
  const stream = new fsm.WriteStream(entry.absolute, {
297
331
  mode: mode,
298
332
  autoClose: false
299
333
  })
300
- 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
+ })
301
346
 
302
347
  let actions = 1
303
348
  const done = er => {
304
- if (er)
305
- 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, () => {})
353
+
354
+ this[ONERROR](er, entry)
355
+ fullyDone()
356
+ return
357
+ }
306
358
 
307
- if (--actions === 0)
308
- fs.close(stream.fd, _ => this[UNPEND]())
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
+ }
309
366
  }
310
367
 
311
368
  stream.on('finish', _ => {
@@ -344,15 +401,18 @@ class Unpack extends Parser {
344
401
  tx.pipe(stream)
345
402
  }
346
403
 
347
- [DIRECTORY] (entry) {
404
+ [DIRECTORY] (entry, fullyDone) {
348
405
  const mode = entry.mode & 0o7777 || this.dmode
349
406
  this[MKDIR](entry.absolute, mode, er => {
350
- if (er)
407
+ if (er) {
408
+ fullyDone()
351
409
  return this[ONERROR](er, entry)
410
+ }
352
411
 
353
412
  let actions = 1
354
413
  const done = _ => {
355
414
  if (--actions === 0) {
415
+ fullyDone()
356
416
  this[UNPEND]()
357
417
  entry.resume()
358
418
  }
@@ -377,12 +437,13 @@ class Unpack extends Parser {
377
437
  entry.resume()
378
438
  }
379
439
 
380
- [SYMLINK] (entry) {
381
- this[LINK](entry, entry.linkpath, 'symlink')
440
+ [SYMLINK] (entry, done) {
441
+ this[LINK](entry, entry.linkpath, 'symlink', done)
382
442
  }
383
443
 
384
- [HARDLINK] (entry) {
385
- 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)
386
447
  }
387
448
 
388
449
  [PEND] () {
@@ -413,29 +474,92 @@ class Unpack extends Parser {
413
474
  // check if a thing is there, and if so, try to clobber it
414
475
  [CHECKFS] (entry) {
415
476
  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))
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) {
483
+ // if we are not creating a directory, and the path is in the dirCache,
484
+ // then that means we are about to delete the directory we created
485
+ // previously, and it is no longer going to be a directory, and neither
486
+ // is any of its children.
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
+ }
515
+ }
516
+ afterMakeParent()
517
+ }
518
+
519
+ const afterMakeParent = () => {
520
+ fs.lstat(entry.absolute, (lstatEr, st) => {
521
+ if (st && (this.keep || this.newer && st.mtime > entry.mtime)) {
421
522
  this[SKIP](entry)
422
- else if (er || this[ISREUSABLE](entry, st))
423
- this[MAKEFS](null, entry)
424
- 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()) {
425
530
  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))
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))
434
553
  })
435
- })
554
+ }
555
+
556
+ if (this[CHECKED_CWD])
557
+ start()
558
+ else
559
+ checkCwd()
436
560
  }
437
561
 
438
- [MAKEFS] (er, entry) {
562
+ [MAKEFS] (er, entry, done) {
439
563
  if (er)
440
564
  return this[ONERROR](er, entry)
441
565
 
@@ -443,67 +567,96 @@ class Unpack extends Parser {
443
567
  case 'File':
444
568
  case 'OldFile':
445
569
  case 'ContiguousFile':
446
- return this[FILE](entry)
570
+ return this[FILE](entry, done)
447
571
 
448
572
  case 'Link':
449
- return this[HARDLINK](entry)
573
+ return this[HARDLINK](entry, done)
450
574
 
451
575
  case 'SymbolicLink':
452
- return this[SYMLINK](entry)
576
+ return this[SYMLINK](entry, done)
453
577
 
454
578
  case 'Directory':
455
579
  case 'GNUDumpDir':
456
- return this[DIRECTORY](entry)
580
+ return this[DIRECTORY](entry, done)
457
581
  }
458
582
  }
459
583
 
460
- [LINK] (entry, linkpath, link) {
461
- // 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
462
586
  fs[link](linkpath, entry.absolute, er => {
463
587
  if (er)
464
588
  return this[ONERROR](er, entry)
589
+ done()
465
590
  this[UNPEND]()
466
591
  entry.resume()
467
592
  })
468
593
  }
469
594
  }
470
595
 
596
+ const callSync = fn => {
597
+ try {
598
+ return [null, fn()]
599
+ } catch (er) {
600
+ return [er, null]
601
+ }
602
+ }
471
603
  class UnpackSync extends Unpack {
472
- constructor (opt) {
473
- super(opt)
604
+ [MAKEFS] (er, entry) {
605
+ return super[MAKEFS](er, entry, /* istanbul ignore next */ () => {})
474
606
  }
475
607
 
476
608
  [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
- }
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
617
+ }
618
+
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)
500
627
  }
501
- } 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))
502
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)
503
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)
504
657
  }
505
658
 
506
- [FILE] (entry) {
659
+ [FILE] (entry, _) {
507
660
  const mode = entry.mode & 0o7777 || this.fmode
508
661
 
509
662
  const oner = er => {
@@ -570,7 +723,7 @@ class UnpackSync extends Unpack {
570
723
  })
571
724
  }
572
725
 
573
- [DIRECTORY] (entry) {
726
+ [DIRECTORY] (entry, _) {
574
727
  const mode = entry.mode & 0o7777 || this.dmode
575
728
  const er = this[MKDIR](entry.absolute, mode)
576
729
  if (er)
@@ -590,7 +743,7 @@ class UnpackSync extends Unpack {
590
743
 
591
744
  [MKDIR] (dir, mode) {
592
745
  try {
593
- return mkdir.sync(dir, {
746
+ return mkdir.sync(normPath(dir), {
594
747
  uid: this.uid,
595
748
  gid: this.gid,
596
749
  processUid: this.processUid,
@@ -607,7 +760,7 @@ class UnpackSync extends Unpack {
607
760
  }
608
761
  }
609
762
 
610
- [LINK] (entry, linkpath, link) {
763
+ [LINK] (entry, linkpath, link, _) {
611
764
  try {
612
765
  fs[link + 'Sync'](linkpath, entry.absolute)
613
766
  entry.resume()