tar 5.0.9 → 6.0.1

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,8 +16,6 @@ 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')
21
19
 
22
20
  const ONENTRY = Symbol('onEntry')
23
21
  const CHECKFS = Symbol('checkFs')
@@ -43,10 +41,14 @@ const SKIP = Symbol('skip')
43
41
  const DOCHOWN = Symbol('doChown')
44
42
  const UID = Symbol('uid')
45
43
  const GID = Symbol('gid')
46
- const CHECKED_CWD = Symbol('checkedCwd')
47
44
  const crypto = require('crypto')
48
45
  const getFlag = require('./get-write-flag.js')
49
46
 
47
+ /* istanbul ignore next */
48
+ const neverCalled = () => {
49
+ throw new Error('sync function called cb somehow?!?')
50
+ }
51
+
50
52
  // Unlinks on Windows are not atomic.
51
53
  //
52
54
  // This means that if you have a file entry, followed by another
@@ -91,17 +93,6 @@ const uint32 = (a, b, c) =>
91
93
  : b === b >>> 0 ? b
92
94
  : c
93
95
 
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
-
105
96
  class Unpack extends Parser {
106
97
  constructor (opt) {
107
98
  if (!opt)
@@ -114,8 +105,6 @@ class Unpack extends Parser {
114
105
 
115
106
  super(opt)
116
107
 
117
- this[CHECKED_CWD] = false
118
-
119
108
  this.reservations = pathReservations()
120
109
 
121
110
  this.transform = typeof opt.transform === 'function' ? opt.transform : null
@@ -180,7 +169,7 @@ class Unpack extends Parser {
180
169
  // links, and removes symlink directories rather than erroring
181
170
  this.unlink = !!opt.unlink
182
171
 
183
- this.cwd = normPath(path.resolve(opt.cwd || process.cwd()))
172
+ this.cwd = path.resolve(opt.cwd || process.cwd())
184
173
  this.strip = +opt.strip || 0
185
174
  this.processUmask = process.umask()
186
175
  this.umask = typeof opt.umask === 'number' ? opt.umask : this.processUmask
@@ -210,23 +199,21 @@ class Unpack extends Parser {
210
199
 
211
200
  [CHECKPATH] (entry) {
212
201
  if (this.strip) {
213
- const parts = normPath(entry.path).split('/')
202
+ const parts = entry.path.split(/\/|\\/)
214
203
  if (parts.length < this.strip)
215
204
  return false
216
205
  entry.path = parts.slice(this.strip).join('/')
217
206
 
218
207
  if (entry.type === 'Link') {
219
- const linkparts = normPath(entry.linkpath).split('/')
208
+ const linkparts = entry.linkpath.split(/\/|\\/)
220
209
  if (linkparts.length >= this.strip)
221
210
  entry.linkpath = linkparts.slice(this.strip).join('/')
222
- else
223
- return false
224
211
  }
225
212
  }
226
213
 
227
214
  if (!this.preservePaths) {
228
- const p = normPath(entry.path)
229
- if (p.split('/').includes('..')) {
215
+ const p = entry.path
216
+ if (p.match(/(^|\/|\\)\.\.(\\|\/|$)/)) {
230
217
  this.warn('TAR_ENTRY_ERROR', `path contains '..'`, {
231
218
  entry,
232
219
  path: p,
@@ -236,36 +223,29 @@ class Unpack extends Parser {
236
223
 
237
224
  // absolutes on posix are also absolutes on win32
238
225
  // so we only need to test this one to get both
239
- const [root, stripped] = stripAbsolutePath(p)
240
- if (root) {
241
- entry.path = stripped
242
- this.warn('TAR_ENTRY_INFO', `stripping ${root} from absolute path`, {
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`, {
243
231
  entry,
244
232
  path: p,
245
233
  })
246
234
  }
247
235
  }
248
236
 
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
-
261
237
  // only encode : chars that aren't drive letter indicators
262
238
  if (this.win32) {
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))
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))
267
242
  }
268
243
 
244
+ if (path.isAbsolute(entry.path))
245
+ entry.absolute = entry.path
246
+ else
247
+ entry.absolute = path.resolve(this.cwd, entry.path)
248
+
269
249
  return true
270
250
  }
271
251
 
@@ -309,7 +289,7 @@ class Unpack extends Parser {
309
289
  }
310
290
 
311
291
  [MKDIR] (dir, mode, cb) {
312
- mkdir(normPath(dir), {
292
+ mkdir(dir, {
313
293
  uid: this.uid,
314
294
  gid: this.gid,
315
295
  processUid: this.processUid,
@@ -350,37 +330,17 @@ class Unpack extends Parser {
350
330
  mode: mode,
351
331
  autoClose: false
352
332
  })
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
- })
333
+ stream.on('error', er => this[ONERROR](er, entry))
364
334
 
365
335
  let actions = 1
366
336
  const done = er => {
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
- }
337
+ if (er)
338
+ return this[ONERROR](er, entry)
376
339
 
377
340
  if (--actions === 0) {
378
341
  fs.close(stream.fd, er => {
379
- if (er)
380
- this[ONERROR](er, entry)
381
- else
382
- this[UNPEND]()
383
342
  fullyDone()
343
+ er ? this[ONERROR](er, entry) : this[UNPEND]()
384
344
  })
385
345
  }
386
346
  }
@@ -415,10 +375,7 @@ class Unpack extends Parser {
415
375
 
416
376
  const tx = this.transform ? this.transform(entry) || entry : entry
417
377
  if (tx !== entry) {
418
- tx.on('error', er => {
419
- this[ONERROR](er, entry)
420
- fullyDone()
421
- })
378
+ tx.on('error', er => this[ONERROR](er, entry))
422
379
  entry.pipe(tx)
423
380
  }
424
381
  tx.pipe(stream)
@@ -428,9 +385,8 @@ class Unpack extends Parser {
428
385
  const mode = entry.mode & 0o7777 || this.dmode
429
386
  this[MKDIR](entry.absolute, mode, er => {
430
387
  if (er) {
431
- this[ONERROR](er, entry)
432
388
  fullyDone()
433
- return
389
+ return this[ONERROR](er, entry)
434
390
  }
435
391
 
436
392
  let actions = 1
@@ -468,8 +424,7 @@ class Unpack extends Parser {
468
424
  }
469
425
 
470
426
  [HARDLINK] (entry, done) {
471
- const linkpath = normPath(path.resolve(this.cwd, entry.linkpath))
472
- this[LINK](entry, linkpath, 'link', done)
427
+ this[LINK](entry, path.resolve(this.cwd, entry.linkpath), 'link', done)
473
428
  }
474
429
 
475
430
  [PEND] () {
@@ -506,91 +461,36 @@ class Unpack extends Parser {
506
461
  this.reservations.reserve(paths, done => this[CHECKFS2](entry, done))
507
462
  }
508
463
  [CHECKFS2] (entry, done) {
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
- }
464
+ this[MKDIR](path.dirname(entry.absolute), this.dmode, er => {
465
+ if (er) {
466
+ done()
467
+ return this[ONERROR](er, entry)
541
468
  }
542
- afterMakeParent()
543
- }
544
-
545
- const afterMakeParent = () => {
546
- fs.lstat(entry.absolute, (lstatEr, st) => {
469
+ fs.lstat(entry.absolute, (er, st) => {
547
470
  if (st && (this.keep || this.newer && st.mtime > entry.mtime)) {
548
471
  this[SKIP](entry)
549
472
  done()
550
- return
473
+ } else if (er || this[ISREUSABLE](entry, st)) {
474
+ this[MAKEFS](null, entry, done)
551
475
  }
552
- if (lstatEr || this[ISREUSABLE](entry, st))
553
- return this[MAKEFS](null, entry, done)
554
-
555
- if (st.isDirectory()) {
476
+ else if (st.isDirectory()) {
556
477
  if (entry.type === 'Directory') {
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))
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))
579
487
  })
580
- }
581
-
582
- if (this[CHECKED_CWD])
583
- start()
584
- else
585
- checkCwd()
488
+ })
586
489
  }
587
490
 
588
491
  [MAKEFS] (er, entry, done) {
589
- if (er) {
590
- this[ONERROR](er, entry)
591
- done()
592
- return
593
- }
492
+ if (er)
493
+ return this[ONERROR](er, entry)
594
494
 
595
495
  switch (entry.type) {
596
496
  case 'File':
@@ -611,80 +511,50 @@ class Unpack extends Parser {
611
511
  }
612
512
 
613
513
  [LINK] (entry, linkpath, link, done) {
614
- // XXX: get the type ('symlink' or 'junction') for windows
514
+ // XXX: get the type ('file' or 'dir') for windows
615
515
  fs[link](linkpath, entry.absolute, er => {
616
516
  if (er)
617
- this[ONERROR](er, entry)
618
- else {
619
- this[UNPEND]()
620
- entry.resume()
621
- }
517
+ return this[ONERROR](er, entry)
622
518
  done()
519
+ this[UNPEND]()
520
+ entry.resume()
623
521
  })
624
522
  }
625
523
  }
626
524
 
627
- const callSync = fn => {
628
- try {
629
- return [null, fn()]
630
- } catch (er) {
631
- return [er, null]
632
- }
633
- }
634
525
  class UnpackSync extends Unpack {
635
- [MAKEFS] (er, entry) {
636
- return super[MAKEFS](er, entry, () => {})
526
+ constructor (opt) {
527
+ super(opt)
637
528
  }
638
529
 
639
530
  [CHECKFS] (entry) {
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)
658
- }
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)
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
+ }
677
554
  }
678
- // not a dir entry, have to remove it
679
- const [er] = callSync(() => fs.rmdirSync(entry.absolute))
680
- this[MAKEFS](er, entry)
555
+ } catch (er) {
556
+ return this[MAKEFS](null, entry, neverCalled)
681
557
  }
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)
688
558
  }
689
559
 
690
560
  [FILE] (entry, _) {
@@ -779,7 +649,7 @@ class UnpackSync extends Unpack {
779
649
 
780
650
  [MKDIR] (dir, mode) {
781
651
  try {
782
- return mkdir.sync(normPath(dir), {
652
+ return mkdir.sync(dir, {
783
653
  uid: this.uid,
784
654
  gid: this.gid,
785
655
  processUid: this.processUid,