tar 4.4.15 → 4.4.19
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/extract.js +4 -3
- package/lib/list.js +4 -3
- package/lib/mkdir.js +55 -48
- package/lib/normalize-windows-path.js +8 -0
- package/lib/pack.js +8 -18
- package/lib/path-reservations.js +149 -0
- package/lib/read-entry.js +4 -3
- package/lib/replace.js +2 -1
- package/lib/strip-absolute-path.js +12 -2
- package/lib/strip-trailing-slashes.js +24 -0
- package/lib/unpack.js +306 -105
- package/lib/write-entry.js +163 -70
- package/package.json +15 -12
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,14 @@ 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')
|
|
21
|
+
const stripSlash = require('./strip-trailing-slashes.js')
|
|
13
22
|
|
|
14
23
|
const ONENTRY = Symbol('onEntry')
|
|
15
24
|
const CHECKFS = Symbol('checkFs')
|
|
25
|
+
const CHECKFS2 = Symbol('checkFs2')
|
|
26
|
+
const PRUNECACHE = Symbol('pruneCache')
|
|
16
27
|
const ISREUSABLE = Symbol('isReusable')
|
|
17
28
|
const MAKEFS = Symbol('makeFs')
|
|
18
29
|
const FILE = Symbol('file')
|
|
@@ -34,7 +45,10 @@ const SKIP = Symbol('skip')
|
|
|
34
45
|
const DOCHOWN = Symbol('doChown')
|
|
35
46
|
const UID = Symbol('uid')
|
|
36
47
|
const GID = Symbol('gid')
|
|
48
|
+
const CHECKED_CWD = Symbol('checkedCwd')
|
|
37
49
|
const crypto = require('crypto')
|
|
50
|
+
const platform = process.env.TESTING_TAR_FAKE_PLATFORM || process.platform
|
|
51
|
+
const isWindows = platform === 'win32'
|
|
38
52
|
|
|
39
53
|
// Unlinks on Windows are not atomic.
|
|
40
54
|
//
|
|
@@ -53,7 +67,7 @@ const crypto = require('crypto')
|
|
|
53
67
|
// See: https://github.com/npm/node-tar/issues/183
|
|
54
68
|
/* istanbul ignore next */
|
|
55
69
|
const unlinkFile = (path, cb) => {
|
|
56
|
-
if (
|
|
70
|
+
if (!isWindows)
|
|
57
71
|
return fs.unlink(path, cb)
|
|
58
72
|
|
|
59
73
|
const name = path + '.DELETE.' + crypto.randomBytes(16).toString('hex')
|
|
@@ -66,7 +80,7 @@ const unlinkFile = (path, cb) => {
|
|
|
66
80
|
|
|
67
81
|
/* istanbul ignore next */
|
|
68
82
|
const unlinkFileSync = path => {
|
|
69
|
-
if (
|
|
83
|
+
if (!isWindows)
|
|
70
84
|
return fs.unlinkSync(path)
|
|
71
85
|
|
|
72
86
|
const name = path + '.DELETE.' + crypto.randomBytes(16).toString('hex')
|
|
@@ -80,6 +94,33 @@ const uint32 = (a, b, c) =>
|
|
|
80
94
|
: b === b >>> 0 ? b
|
|
81
95
|
: c
|
|
82
96
|
|
|
97
|
+
// clear the cache if it's a case-insensitive unicode-squashing match.
|
|
98
|
+
// we can't know if the current file system is case-sensitive or supports
|
|
99
|
+
// unicode fully, so we check for similarity on the maximally compatible
|
|
100
|
+
// representation. Err on the side of pruning, since all it's doing is
|
|
101
|
+
// preventing lstats, and it's not the end of the world if we get a false
|
|
102
|
+
// positive.
|
|
103
|
+
// Note that on windows, we always drop the entire cache whenever a
|
|
104
|
+
// symbolic link is encountered, because 8.3 filenames are impossible
|
|
105
|
+
// to reason about, and collisions are hazards rather than just failures.
|
|
106
|
+
const cacheKeyNormalize = path => stripSlash(normPath(path))
|
|
107
|
+
.normalize('NFKD')
|
|
108
|
+
.toLowerCase()
|
|
109
|
+
|
|
110
|
+
const pruneCache = (cache, abs) => {
|
|
111
|
+
abs = cacheKeyNormalize(abs)
|
|
112
|
+
for (const path of cache.keys()) {
|
|
113
|
+
const pnorm = cacheKeyNormalize(path)
|
|
114
|
+
if (pnorm === abs || pnorm.indexOf(abs + '/') === 0)
|
|
115
|
+
cache.delete(path)
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const dropCache = cache => {
|
|
120
|
+
for (const key of cache.keys())
|
|
121
|
+
cache.delete(key)
|
|
122
|
+
}
|
|
123
|
+
|
|
83
124
|
class Unpack extends Parser {
|
|
84
125
|
constructor (opt) {
|
|
85
126
|
if (!opt)
|
|
@@ -92,6 +133,10 @@ class Unpack extends Parser {
|
|
|
92
133
|
|
|
93
134
|
super(opt)
|
|
94
135
|
|
|
136
|
+
this[CHECKED_CWD] = false
|
|
137
|
+
|
|
138
|
+
this.reservations = pathReservations()
|
|
139
|
+
|
|
95
140
|
this.transform = typeof opt.transform === 'function' ? opt.transform : null
|
|
96
141
|
|
|
97
142
|
this.writable = true
|
|
@@ -134,7 +179,7 @@ class Unpack extends Parser {
|
|
|
134
179
|
this.forceChown = opt.forceChown === true
|
|
135
180
|
|
|
136
181
|
// turn ><?| in filenames into 0xf000-higher encoded forms
|
|
137
|
-
this.win32 = !!opt.win32 ||
|
|
182
|
+
this.win32 = !!opt.win32 || isWindows
|
|
138
183
|
|
|
139
184
|
// do not unpack over files that are newer than what's in the archive
|
|
140
185
|
this.newer = !!opt.newer
|
|
@@ -154,7 +199,7 @@ class Unpack extends Parser {
|
|
|
154
199
|
// links, and removes symlink directories rather than erroring
|
|
155
200
|
this.unlink = !!opt.unlink
|
|
156
201
|
|
|
157
|
-
this.cwd = path.resolve(opt.cwd || process.cwd())
|
|
202
|
+
this.cwd = normPath(path.resolve(opt.cwd || process.cwd()))
|
|
158
203
|
this.strip = +opt.strip || 0
|
|
159
204
|
this.processUmask = process.umask()
|
|
160
205
|
this.umask = typeof opt.umask === 'number' ? opt.umask : this.processUmask
|
|
@@ -175,27 +220,29 @@ class Unpack extends Parser {
|
|
|
175
220
|
|
|
176
221
|
[CHECKPATH] (entry) {
|
|
177
222
|
if (this.strip) {
|
|
178
|
-
const parts = entry.path.split(
|
|
223
|
+
const parts = normPath(entry.path).split('/')
|
|
179
224
|
if (parts.length < this.strip)
|
|
180
225
|
return false
|
|
181
226
|
entry.path = parts.slice(this.strip).join('/')
|
|
182
227
|
|
|
183
228
|
if (entry.type === 'Link') {
|
|
184
|
-
const linkparts = entry.linkpath.split(
|
|
229
|
+
const linkparts = normPath(entry.linkpath).split('/')
|
|
185
230
|
if (linkparts.length >= this.strip)
|
|
186
231
|
entry.linkpath = linkparts.slice(this.strip).join('/')
|
|
232
|
+
else
|
|
233
|
+
return false
|
|
187
234
|
}
|
|
188
235
|
}
|
|
189
236
|
|
|
190
237
|
if (!this.preservePaths) {
|
|
191
|
-
const p = entry.path
|
|
192
|
-
|
|
193
|
-
|
|
238
|
+
const p = normPath(entry.path)
|
|
239
|
+
const parts = p.split('/')
|
|
240
|
+
if (parts.includes('..') || isWindows && /^[a-z]:\.\.$/i.test(parts[0])) {
|
|
241
|
+
this.warn(`path contains '..'`, p)
|
|
194
242
|
return false
|
|
195
243
|
}
|
|
196
244
|
|
|
197
|
-
//
|
|
198
|
-
// so we only need to test this one to get both
|
|
245
|
+
// strip off the root
|
|
199
246
|
const s = stripAbsolutePath(p)
|
|
200
247
|
if (s[0]) {
|
|
201
248
|
entry.path = s[1]
|
|
@@ -203,18 +250,42 @@ class Unpack extends Parser {
|
|
|
203
250
|
}
|
|
204
251
|
}
|
|
205
252
|
|
|
253
|
+
if (path.isAbsolute(entry.path))
|
|
254
|
+
entry.absolute = normPath(path.resolve(entry.path))
|
|
255
|
+
else
|
|
256
|
+
entry.absolute = normPath(path.resolve(this.cwd, entry.path))
|
|
257
|
+
|
|
258
|
+
// if we somehow ended up with a path that escapes the cwd, and we are
|
|
259
|
+
// not in preservePaths mode, then something is fishy! This should have
|
|
260
|
+
// been prevented above, so ignore this for coverage.
|
|
261
|
+
/* istanbul ignore if - defense in depth */
|
|
262
|
+
if (!this.preservePaths &&
|
|
263
|
+
entry.absolute.indexOf(this.cwd + '/') !== 0 &&
|
|
264
|
+
entry.absolute !== this.cwd) {
|
|
265
|
+
this.warn('TAR_ENTRY_ERROR', 'path escaped extraction target', {
|
|
266
|
+
entry,
|
|
267
|
+
path: normPath(entry.path),
|
|
268
|
+
resolvedPath: entry.absolute,
|
|
269
|
+
cwd: this.cwd,
|
|
270
|
+
})
|
|
271
|
+
return false
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// an archive can set properties on the extraction directory, but it
|
|
275
|
+
// may not replace the cwd with a different kind of thing entirely.
|
|
276
|
+
if (entry.absolute === this.cwd &&
|
|
277
|
+
entry.type !== 'Directory' &&
|
|
278
|
+
entry.type !== 'GNUDumpDir')
|
|
279
|
+
return false
|
|
280
|
+
|
|
206
281
|
// only encode : chars that aren't drive letter indicators
|
|
207
282
|
if (this.win32) {
|
|
208
|
-
const
|
|
209
|
-
entry.
|
|
210
|
-
|
|
283
|
+
const { root: aRoot } = path.win32.parse(entry.absolute)
|
|
284
|
+
entry.absolute = aRoot + wc.encode(entry.absolute.substr(aRoot.length))
|
|
285
|
+
const { root: pRoot } = path.win32.parse(entry.path)
|
|
286
|
+
entry.path = pRoot + wc.encode(entry.path.substr(pRoot.length))
|
|
211
287
|
}
|
|
212
288
|
|
|
213
|
-
if (path.isAbsolute(entry.path))
|
|
214
|
-
entry.absolute = entry.path
|
|
215
|
-
else
|
|
216
|
-
entry.absolute = path.resolve(this.cwd, entry.path)
|
|
217
|
-
|
|
218
289
|
return true
|
|
219
290
|
}
|
|
220
291
|
|
|
@@ -258,7 +329,7 @@ class Unpack extends Parser {
|
|
|
258
329
|
}
|
|
259
330
|
|
|
260
331
|
[MKDIR] (dir, mode, cb) {
|
|
261
|
-
mkdir(dir, {
|
|
332
|
+
mkdir(normPath(dir), {
|
|
262
333
|
uid: this.uid,
|
|
263
334
|
gid: this.gid,
|
|
264
335
|
processUid: this.processUid,
|
|
@@ -292,21 +363,44 @@ class Unpack extends Parser {
|
|
|
292
363
|
return uint32(this.gid, entry.gid, this.processGid)
|
|
293
364
|
}
|
|
294
365
|
|
|
295
|
-
[FILE] (entry) {
|
|
366
|
+
[FILE] (entry, fullyDone) {
|
|
296
367
|
const mode = entry.mode & 0o7777 || this.fmode
|
|
297
368
|
const stream = new fsm.WriteStream(entry.absolute, {
|
|
298
369
|
mode: mode,
|
|
299
370
|
autoClose: false
|
|
300
371
|
})
|
|
301
|
-
stream.on('error', er =>
|
|
372
|
+
stream.on('error', er => {
|
|
373
|
+
if (stream.fd)
|
|
374
|
+
fs.close(stream.fd, () => {})
|
|
375
|
+
|
|
376
|
+
// flush all the data out so that we aren't left hanging
|
|
377
|
+
// if the error wasn't actually fatal. otherwise the parse
|
|
378
|
+
// is blocked, and we never proceed.
|
|
379
|
+
/* istanbul ignore next */
|
|
380
|
+
stream.write = () => true
|
|
381
|
+
this[ONERROR](er, entry)
|
|
382
|
+
fullyDone()
|
|
383
|
+
})
|
|
302
384
|
|
|
303
385
|
let actions = 1
|
|
304
386
|
const done = er => {
|
|
305
|
-
if (er)
|
|
306
|
-
|
|
387
|
+
if (er) {
|
|
388
|
+
/* istanbul ignore else - we should always have a fd by now */
|
|
389
|
+
if (stream.fd)
|
|
390
|
+
fs.close(stream.fd, () => {})
|
|
307
391
|
|
|
308
|
-
|
|
309
|
-
|
|
392
|
+
this[ONERROR](er, entry)
|
|
393
|
+
fullyDone()
|
|
394
|
+
return
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
if (--actions === 0) {
|
|
398
|
+
fs.close(stream.fd, er => {
|
|
399
|
+
fullyDone()
|
|
400
|
+
/* istanbul ignore next */
|
|
401
|
+
er ? this[ONERROR](er, entry) : this[UNPEND]()
|
|
402
|
+
})
|
|
403
|
+
}
|
|
310
404
|
}
|
|
311
405
|
|
|
312
406
|
stream.on('finish', _ => {
|
|
@@ -345,15 +439,18 @@ class Unpack extends Parser {
|
|
|
345
439
|
tx.pipe(stream)
|
|
346
440
|
}
|
|
347
441
|
|
|
348
|
-
[DIRECTORY] (entry) {
|
|
442
|
+
[DIRECTORY] (entry, fullyDone) {
|
|
349
443
|
const mode = entry.mode & 0o7777 || this.dmode
|
|
350
444
|
this[MKDIR](entry.absolute, mode, er => {
|
|
351
|
-
if (er)
|
|
445
|
+
if (er) {
|
|
446
|
+
fullyDone()
|
|
352
447
|
return this[ONERROR](er, entry)
|
|
448
|
+
}
|
|
353
449
|
|
|
354
450
|
let actions = 1
|
|
355
451
|
const done = _ => {
|
|
356
452
|
if (--actions === 0) {
|
|
453
|
+
fullyDone()
|
|
357
454
|
this[UNPEND]()
|
|
358
455
|
entry.resume()
|
|
359
456
|
}
|
|
@@ -378,12 +475,13 @@ class Unpack extends Parser {
|
|
|
378
475
|
entry.resume()
|
|
379
476
|
}
|
|
380
477
|
|
|
381
|
-
[SYMLINK] (entry) {
|
|
382
|
-
this[LINK](entry, entry.linkpath, 'symlink')
|
|
478
|
+
[SYMLINK] (entry, done) {
|
|
479
|
+
this[LINK](entry, entry.linkpath, 'symlink', done)
|
|
383
480
|
}
|
|
384
481
|
|
|
385
|
-
[HARDLINK] (entry) {
|
|
386
|
-
|
|
482
|
+
[HARDLINK] (entry, done) {
|
|
483
|
+
const linkpath = normPath(path.resolve(this.cwd, entry.linkpath))
|
|
484
|
+
this[LINK](entry, linkpath, 'link', done)
|
|
387
485
|
}
|
|
388
486
|
|
|
389
487
|
[PEND] () {
|
|
@@ -408,49 +506,122 @@ class Unpack extends Parser {
|
|
|
408
506
|
!this.unlink &&
|
|
409
507
|
st.isFile() &&
|
|
410
508
|
st.nlink <= 1 &&
|
|
411
|
-
|
|
509
|
+
!isWindows
|
|
412
510
|
}
|
|
413
511
|
|
|
414
512
|
// check if a thing is there, and if so, try to clobber it
|
|
415
513
|
[CHECKFS] (entry) {
|
|
416
514
|
this[PEND]()
|
|
515
|
+
const paths = [entry.path]
|
|
516
|
+
if (entry.linkpath)
|
|
517
|
+
paths.push(entry.linkpath)
|
|
518
|
+
this.reservations.reserve(paths, done => this[CHECKFS2](entry, done))
|
|
519
|
+
}
|
|
417
520
|
|
|
521
|
+
[PRUNECACHE] (entry) {
|
|
418
522
|
// if we are not creating a directory, and the path is in the dirCache,
|
|
419
523
|
// then that means we are about to delete the directory we created
|
|
420
524
|
// previously, and it is no longer going to be a directory, and neither
|
|
421
525
|
// is any of its children.
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
526
|
+
// If a symbolic link is encountered, all bets are off. There is no
|
|
527
|
+
// reasonable way to sanitize the cache in such a way we will be able to
|
|
528
|
+
// avoid having filesystem collisions. If this happens with a non-symlink
|
|
529
|
+
// entry, it'll just fail to unpack, but a symlink to a directory, using an
|
|
530
|
+
// 8.3 shortname or certain unicode attacks, can evade detection and lead
|
|
531
|
+
// to arbitrary writes to anywhere on the system.
|
|
532
|
+
if (entry.type === 'SymbolicLink')
|
|
533
|
+
dropCache(this.dirCache)
|
|
534
|
+
else if (entry.type !== 'Directory')
|
|
535
|
+
pruneCache(this.dirCache, entry.absolute)
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
[CHECKFS2] (entry, fullyDone) {
|
|
539
|
+
this[PRUNECACHE](entry)
|
|
540
|
+
|
|
541
|
+
const done = er => {
|
|
542
|
+
this[PRUNECACHE](entry)
|
|
543
|
+
fullyDone(er)
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
const checkCwd = () => {
|
|
547
|
+
this[MKDIR](this.cwd, this.dmode, er => {
|
|
548
|
+
if (er) {
|
|
549
|
+
this[ONERROR](er, entry)
|
|
550
|
+
done()
|
|
551
|
+
return
|
|
552
|
+
}
|
|
553
|
+
this[CHECKED_CWD] = true
|
|
554
|
+
start()
|
|
555
|
+
})
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
const start = () => {
|
|
559
|
+
if (entry.absolute !== this.cwd) {
|
|
560
|
+
const parent = normPath(path.dirname(entry.absolute))
|
|
561
|
+
if (parent !== this.cwd) {
|
|
562
|
+
return this[MKDIR](parent, this.dmode, er => {
|
|
563
|
+
if (er) {
|
|
564
|
+
this[ONERROR](er, entry)
|
|
565
|
+
done()
|
|
566
|
+
return
|
|
567
|
+
}
|
|
568
|
+
afterMakeParent()
|
|
569
|
+
})
|
|
570
|
+
}
|
|
428
571
|
}
|
|
572
|
+
afterMakeParent()
|
|
429
573
|
}
|
|
430
574
|
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
fs.lstat(entry.absolute, (er, st) => {
|
|
435
|
-
if (st && (this.keep || this.newer && st.mtime > entry.mtime))
|
|
575
|
+
const afterMakeParent = () => {
|
|
576
|
+
fs.lstat(entry.absolute, (lstatEr, st) => {
|
|
577
|
+
if (st && (this.keep || this.newer && st.mtime > entry.mtime)) {
|
|
436
578
|
this[SKIP](entry)
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
579
|
+
done()
|
|
580
|
+
return
|
|
581
|
+
}
|
|
582
|
+
if (lstatEr || this[ISREUSABLE](entry, st))
|
|
583
|
+
return this[MAKEFS](null, entry, done)
|
|
584
|
+
|
|
585
|
+
if (st.isDirectory()) {
|
|
440
586
|
if (entry.type === 'Directory') {
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
587
|
+
const needChmod = !this.noChmod &&
|
|
588
|
+
entry.mode &&
|
|
589
|
+
(st.mode & 0o7777) !== entry.mode
|
|
590
|
+
const afterChmod = er => this[MAKEFS](er, entry, done)
|
|
591
|
+
if (!needChmod)
|
|
592
|
+
return afterChmod()
|
|
593
|
+
return fs.chmod(entry.absolute, entry.mode, afterChmod)
|
|
594
|
+
}
|
|
595
|
+
// Not a dir entry, have to remove it.
|
|
596
|
+
// NB: the only way to end up with an entry that is the cwd
|
|
597
|
+
// itself, in such a way that == does not detect, is a
|
|
598
|
+
// tricky windows absolute path with UNC or 8.3 parts (and
|
|
599
|
+
// preservePaths:true, or else it will have been stripped).
|
|
600
|
+
// In that case, the user has opted out of path protections
|
|
601
|
+
// explicitly, so if they blow away the cwd, c'est la vie.
|
|
602
|
+
if (entry.absolute !== this.cwd) {
|
|
603
|
+
return fs.rmdir(entry.absolute, er =>
|
|
604
|
+
this[MAKEFS](er, entry, done))
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
// not a dir, and not reusable
|
|
609
|
+
// don't remove if the cwd, we want that error
|
|
610
|
+
if (entry.absolute === this.cwd)
|
|
611
|
+
return this[MAKEFS](null, entry, done)
|
|
612
|
+
|
|
613
|
+
unlinkFile(entry.absolute, er =>
|
|
614
|
+
this[MAKEFS](er, entry, done))
|
|
449
615
|
})
|
|
450
|
-
}
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
if (this[CHECKED_CWD])
|
|
619
|
+
start()
|
|
620
|
+
else
|
|
621
|
+
checkCwd()
|
|
451
622
|
}
|
|
452
623
|
|
|
453
|
-
[MAKEFS] (er, entry) {
|
|
624
|
+
[MAKEFS] (er, entry, done) {
|
|
454
625
|
if (er)
|
|
455
626
|
return this[ONERROR](er, entry)
|
|
456
627
|
|
|
@@ -458,82 +629,107 @@ class Unpack extends Parser {
|
|
|
458
629
|
case 'File':
|
|
459
630
|
case 'OldFile':
|
|
460
631
|
case 'ContiguousFile':
|
|
461
|
-
return this[FILE](entry)
|
|
632
|
+
return this[FILE](entry, done)
|
|
462
633
|
|
|
463
634
|
case 'Link':
|
|
464
|
-
return this[HARDLINK](entry)
|
|
635
|
+
return this[HARDLINK](entry, done)
|
|
465
636
|
|
|
466
637
|
case 'SymbolicLink':
|
|
467
|
-
return this[SYMLINK](entry)
|
|
638
|
+
return this[SYMLINK](entry, done)
|
|
468
639
|
|
|
469
640
|
case 'Directory':
|
|
470
641
|
case 'GNUDumpDir':
|
|
471
|
-
return this[DIRECTORY](entry)
|
|
642
|
+
return this[DIRECTORY](entry, done)
|
|
472
643
|
}
|
|
473
644
|
}
|
|
474
645
|
|
|
475
|
-
[LINK] (entry, linkpath, link) {
|
|
476
|
-
// XXX: get the type ('
|
|
646
|
+
[LINK] (entry, linkpath, link, done) {
|
|
647
|
+
// XXX: get the type ('symlink' or 'junction') for windows
|
|
477
648
|
fs[link](linkpath, entry.absolute, er => {
|
|
478
649
|
if (er)
|
|
479
650
|
return this[ONERROR](er, entry)
|
|
651
|
+
done()
|
|
480
652
|
this[UNPEND]()
|
|
481
653
|
entry.resume()
|
|
482
654
|
})
|
|
483
655
|
}
|
|
484
656
|
}
|
|
485
657
|
|
|
658
|
+
const callSync = fn => {
|
|
659
|
+
try {
|
|
660
|
+
return [null, fn()]
|
|
661
|
+
} catch (er) {
|
|
662
|
+
return [er, null]
|
|
663
|
+
}
|
|
664
|
+
}
|
|
486
665
|
class UnpackSync extends Unpack {
|
|
487
|
-
|
|
488
|
-
super(
|
|
666
|
+
[MAKEFS] (er, entry) {
|
|
667
|
+
return super[MAKEFS](er, entry, /* istanbul ignore next */ () => {})
|
|
489
668
|
}
|
|
490
669
|
|
|
491
670
|
[CHECKFS] (entry) {
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
671
|
+
this[PRUNECACHE](entry)
|
|
672
|
+
|
|
673
|
+
if (!this[CHECKED_CWD]) {
|
|
674
|
+
const er = this[MKDIR](this.cwd, this.dmode)
|
|
675
|
+
if (er)
|
|
676
|
+
return this[ONERROR](er, entry)
|
|
677
|
+
this[CHECKED_CWD] = true
|
|
499
678
|
}
|
|
500
679
|
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
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
|
-
}
|
|
680
|
+
// don't bother to make the parent if the current entry is the cwd,
|
|
681
|
+
// we've already checked it.
|
|
682
|
+
if (entry.absolute !== this.cwd) {
|
|
683
|
+
const parent = normPath(path.dirname(entry.absolute))
|
|
684
|
+
if (parent !== this.cwd) {
|
|
685
|
+
const mkParent = this[MKDIR](parent, this.dmode)
|
|
686
|
+
if (mkParent)
|
|
687
|
+
return this[ONERROR](mkParent, entry)
|
|
524
688
|
}
|
|
525
|
-
}
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
const [lstatEr, st] = callSync(() => fs.lstatSync(entry.absolute))
|
|
692
|
+
if (st && (this.keep || this.newer && st.mtime > entry.mtime))
|
|
693
|
+
return this[SKIP](entry)
|
|
694
|
+
|
|
695
|
+
if (lstatEr || this[ISREUSABLE](entry, st))
|
|
526
696
|
return this[MAKEFS](null, entry)
|
|
697
|
+
|
|
698
|
+
if (st.isDirectory()) {
|
|
699
|
+
if (entry.type === 'Directory') {
|
|
700
|
+
const needChmod = !this.noChmod &&
|
|
701
|
+
entry.mode &&
|
|
702
|
+
(st.mode & 0o7777) !== entry.mode
|
|
703
|
+
const [er] = needChmod ? callSync(() => {
|
|
704
|
+
fs.chmodSync(entry.absolute, entry.mode)
|
|
705
|
+
}) : []
|
|
706
|
+
return this[MAKEFS](er, entry)
|
|
707
|
+
}
|
|
708
|
+
// not a dir entry, have to remove it
|
|
709
|
+
const [er] = callSync(() => fs.rmdirSync(entry.absolute))
|
|
710
|
+
this[MAKEFS](er, entry)
|
|
527
711
|
}
|
|
712
|
+
|
|
713
|
+
// not a dir, and not reusable.
|
|
714
|
+
// don't remove if it's the cwd, since we want that error.
|
|
715
|
+
const [er] = entry.absolute === this.cwd ? []
|
|
716
|
+
: callSync(() => unlinkFileSync(entry.absolute))
|
|
717
|
+
this[MAKEFS](er, entry)
|
|
528
718
|
}
|
|
529
719
|
|
|
530
|
-
[FILE] (entry) {
|
|
720
|
+
[FILE] (entry, done) {
|
|
531
721
|
const mode = entry.mode & 0o7777 || this.fmode
|
|
532
722
|
|
|
533
723
|
const oner = er => {
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
724
|
+
let closeError
|
|
725
|
+
try {
|
|
726
|
+
fs.closeSync(fd)
|
|
727
|
+
} catch (e) {
|
|
728
|
+
closeError = e
|
|
729
|
+
}
|
|
730
|
+
if (er || closeError)
|
|
731
|
+
this[ONERROR](er || closeError, entry)
|
|
732
|
+
done()
|
|
537
733
|
}
|
|
538
734
|
|
|
539
735
|
let stream
|
|
@@ -594,11 +790,14 @@ class UnpackSync extends Unpack {
|
|
|
594
790
|
})
|
|
595
791
|
}
|
|
596
792
|
|
|
597
|
-
[DIRECTORY] (entry) {
|
|
793
|
+
[DIRECTORY] (entry, done) {
|
|
598
794
|
const mode = entry.mode & 0o7777 || this.dmode
|
|
599
795
|
const er = this[MKDIR](entry.absolute, mode)
|
|
600
|
-
if (er)
|
|
601
|
-
|
|
796
|
+
if (er) {
|
|
797
|
+
this[ONERROR](er, entry)
|
|
798
|
+
done()
|
|
799
|
+
return
|
|
800
|
+
}
|
|
602
801
|
if (entry.mtime && !this.noMtime) {
|
|
603
802
|
try {
|
|
604
803
|
fs.utimesSync(entry.absolute, entry.atime || new Date(), entry.mtime)
|
|
@@ -609,12 +808,13 @@ class UnpackSync extends Unpack {
|
|
|
609
808
|
fs.chownSync(entry.absolute, this[UID](entry), this[GID](entry))
|
|
610
809
|
} catch (er) {}
|
|
611
810
|
}
|
|
811
|
+
done()
|
|
612
812
|
entry.resume()
|
|
613
813
|
}
|
|
614
814
|
|
|
615
815
|
[MKDIR] (dir, mode) {
|
|
616
816
|
try {
|
|
617
|
-
return mkdir.sync(dir, {
|
|
817
|
+
return mkdir.sync(normPath(dir), {
|
|
618
818
|
uid: this.uid,
|
|
619
819
|
gid: this.gid,
|
|
620
820
|
processUid: this.processUid,
|
|
@@ -631,9 +831,10 @@ class UnpackSync extends Unpack {
|
|
|
631
831
|
}
|
|
632
832
|
}
|
|
633
833
|
|
|
634
|
-
[LINK] (entry, linkpath, link) {
|
|
834
|
+
[LINK] (entry, linkpath, link, done) {
|
|
635
835
|
try {
|
|
636
836
|
fs[link + 'Sync'](linkpath, entry.absolute)
|
|
837
|
+
done()
|
|
637
838
|
entry.resume()
|
|
638
839
|
} catch (er) {
|
|
639
840
|
return this[ONERROR](er, entry)
|