tar 6.1.1 → 6.1.5

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 CHANGED
@@ -6,6 +6,7 @@ const Unpack = require('./unpack.js')
6
6
  const fs = require('fs')
7
7
  const fsm = require('fs-minipass')
8
8
  const path = require('path')
9
+ const stripSlash = require('./strip-trailing-slashes.js')
9
10
 
10
11
  module.exports = (opt_, files, cb) => {
11
12
  if (typeof opt_ === 'function')
@@ -41,7 +42,7 @@ module.exports = (opt_, files, cb) => {
41
42
  // construct a filter that limits the file entries listed
42
43
  // include child entries if a dir is included
43
44
  const filesFilter = (opt, files) => {
44
- const map = new Map(files.map(f => [f.replace(/\/+$/, ''), true]))
45
+ const map = new Map(files.map(f => [stripSlash(f), true]))
45
46
  const filter = opt.filter
46
47
 
47
48
  const mapHas = (file, r) => {
@@ -55,8 +56,8 @@ const filesFilter = (opt, files) => {
55
56
  }
56
57
 
57
58
  opt.filter = filter
58
- ? (file, entry) => filter(file, entry) && mapHas(file.replace(/\/+$/, ''))
59
- : file => mapHas(file.replace(/\/+$/, ''))
59
+ ? (file, entry) => filter(file, entry) && mapHas(stripSlash(file))
60
+ : file => mapHas(stripSlash(file))
60
61
  }
61
62
 
62
63
  const extractFileSync = opt => {
package/lib/list.js CHANGED
@@ -9,6 +9,7 @@ const Parser = require('./parse.js')
9
9
  const fs = require('fs')
10
10
  const fsm = require('fs-minipass')
11
11
  const path = require('path')
12
+ const stripSlash = require('./strip-trailing-slashes.js')
12
13
 
13
14
  module.exports = (opt_, files, cb) => {
14
15
  if (typeof opt_ === 'function')
@@ -54,7 +55,7 @@ const onentryFunction = opt => {
54
55
  // construct a filter that limits the file entries listed
55
56
  // include child entries if a dir is included
56
57
  const filesFilter = (opt, files) => {
57
- const map = new Map(files.map(f => [f.replace(/\/+$/, ''), true]))
58
+ const map = new Map(files.map(f => [stripSlash(f), true]))
58
59
  const filter = opt.filter
59
60
 
60
61
  const mapHas = (file, r) => {
@@ -68,8 +69,8 @@ const filesFilter = (opt, files) => {
68
69
  }
69
70
 
70
71
  opt.filter = filter
71
- ? (file, entry) => filter(file, entry) && mapHas(file.replace(/\/+$/, ''))
72
- : file => mapHas(file.replace(/\/+$/, ''))
72
+ ? (file, entry) => filter(file, entry) && mapHas(stripSlash(file))
73
+ : file => mapHas(stripSlash(file))
73
74
  }
74
75
 
75
76
  const listFileSync = opt => {
@@ -0,0 +1,24 @@
1
+ // this is the only approach that was significantly faster than using
2
+ // str.replace(/\/+$/, '') for strings ending with a lot of / chars and
3
+ // containing multiple / chars.
4
+ const batchStrings = [
5
+ '/'.repeat(1024),
6
+ '/'.repeat(512),
7
+ '/'.repeat(256),
8
+ '/'.repeat(128),
9
+ '/'.repeat(64),
10
+ '/'.repeat(32),
11
+ '/'.repeat(16),
12
+ '/'.repeat(8),
13
+ '/'.repeat(4),
14
+ '/'.repeat(2),
15
+ '/',
16
+ ]
17
+
18
+ module.exports = str => {
19
+ for (const s of batchStrings) {
20
+ while (str.length >= s.length && str.slice(-1 * s.length) === s)
21
+ str = str.slice(0, -1 * s.length)
22
+ }
23
+ return str
24
+ }
package/lib/unpack.js CHANGED
@@ -205,6 +205,8 @@ class Unpack extends Parser {
205
205
  if (parts.length < this.strip)
206
206
  return false
207
207
  entry.path = parts.slice(this.strip).join('/')
208
+ if (entry.path === '' && entry.type !== 'Directory' && entry.type !== 'GNUDumpDir')
209
+ return false
208
210
 
209
211
  if (entry.type === 'Link') {
210
212
  const linkparts = entry.linkpath.split(/\/|\\/)
@@ -333,17 +335,35 @@ class Unpack extends Parser {
333
335
  mode: mode,
334
336
  autoClose: false,
335
337
  })
336
- stream.on('error', er => this[ONERROR](er, entry))
338
+ stream.on('error', er => {
339
+ if (stream.fd)
340
+ fs.close(stream.fd, () => {})
341
+ // flush all the data out so that we aren't left hanging
342
+ // if the error wasn't actually fatal. otherwise the parse
343
+ // is blocked, and we never proceed.
344
+ stream.write = () => true
345
+ this[ONERROR](er, entry)
346
+ fullyDone()
347
+ })
337
348
 
338
349
  let actions = 1
339
350
  const done = er => {
340
- if (er)
341
- 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
+ this[ONERROR](er, entry)
356
+ fullyDone()
357
+ return
358
+ }
342
359
 
343
360
  if (--actions === 0) {
344
361
  fs.close(stream.fd, er => {
362
+ if (er)
363
+ this[ONERROR](er, entry)
364
+ else
365
+ this[UNPEND]()
345
366
  fullyDone()
346
- er ? this[ONERROR](er, entry) : this[UNPEND]()
347
367
  })
348
368
  }
349
369
  }
@@ -378,7 +398,10 @@ class Unpack extends Parser {
378
398
 
379
399
  const tx = this.transform ? this.transform(entry) || entry : entry
380
400
  if (tx !== entry) {
381
- tx.on('error', er => this[ONERROR](er, entry))
401
+ tx.on('error', er => {
402
+ this[ONERROR](er, entry)
403
+ fullyDone()
404
+ })
382
405
  entry.pipe(tx)
383
406
  }
384
407
  tx.pipe(stream)
@@ -388,8 +411,9 @@ class Unpack extends Parser {
388
411
  const mode = entry.mode & 0o7777 || this.dmode
389
412
  this[MKDIR](entry.absolute, mode, er => {
390
413
  if (er) {
414
+ this[ONERROR](er, entry)
391
415
  fullyDone()
392
- return this[ONERROR](er, entry)
416
+ return
393
417
  }
394
418
 
395
419
  let actions = 1
@@ -465,10 +489,24 @@ class Unpack extends Parser {
465
489
  }
466
490
 
467
491
  [CHECKFS2] (entry, done) {
492
+ // if we are not creating a directory, and the path is in the dirCache,
493
+ // then that means we are about to delete the directory we created
494
+ // previously, and it is no longer going to be a directory, and neither
495
+ // is any of its children.
496
+ if (entry.type !== 'Directory') {
497
+ for (const path of this.dirCache.keys()) {
498
+ if (path === entry.absolute ||
499
+ path.indexOf(entry.absolute + '/') === 0 ||
500
+ path.indexOf(entry.absolute + '\\') === 0)
501
+ this.dirCache.delete(path)
502
+ }
503
+ }
504
+
468
505
  this[MKDIR](path.dirname(entry.absolute), this.dmode, er => {
469
506
  if (er) {
507
+ this[ONERROR](er, entry)
470
508
  done()
471
- return this[ONERROR](er, entry)
509
+ return
472
510
  }
473
511
  fs.lstat(entry.absolute, (er, st) => {
474
512
  if (st && (this.keep || this.newer && st.mtime > entry.mtime)) {
@@ -476,7 +514,6 @@ class Unpack extends Parser {
476
514
  done()
477
515
  } else if (er || this[ISREUSABLE](entry, st))
478
516
  this[MAKEFS](null, entry, done)
479
-
480
517
  else if (st.isDirectory()) {
481
518
  if (entry.type === 'Directory') {
482
519
  if (!this.noChmod && (!entry.mode || (st.mode & 0o7777) === entry.mode))
@@ -494,8 +531,11 @@ class Unpack extends Parser {
494
531
  }
495
532
 
496
533
  [MAKEFS] (er, entry, done) {
497
- if (er)
498
- return this[ONERROR](er, entry)
534
+ if (er) {
535
+ this[ONERROR](er, entry)
536
+ done()
537
+ return
538
+ }
499
539
 
500
540
  switch (entry.type) {
501
541
  case 'File':
@@ -519,16 +559,27 @@ class Unpack extends Parser {
519
559
  // XXX: get the type ('file' or 'dir') for windows
520
560
  fs[link](linkpath, entry.absolute, er => {
521
561
  if (er)
522
- return this[ONERROR](er, entry)
562
+ this[ONERROR](er, entry)
563
+ else {
564
+ this[UNPEND]()
565
+ entry.resume()
566
+ }
523
567
  done()
524
- this[UNPEND]()
525
- entry.resume()
526
568
  })
527
569
  }
528
570
  }
529
571
 
530
572
  class UnpackSync extends Unpack {
531
573
  [CHECKFS] (entry) {
574
+ if (entry.type !== 'Directory') {
575
+ for (const path of this.dirCache.keys()) {
576
+ if (path === entry.absolute ||
577
+ path.indexOf(entry.absolute + '/') === 0 ||
578
+ path.indexOf(entry.absolute + '\\') === 0)
579
+ this.dirCache.delete(path)
580
+ }
581
+ }
582
+
532
583
  const er = this[MKDIR](path.dirname(entry.absolute), this.dmode, neverCalled)
533
584
  if (er)
534
585
  return this[ONERROR](er, entry)
@@ -21,6 +21,8 @@ const OPENFILE = Symbol('openfile')
21
21
  const ONOPENFILE = Symbol('onopenfile')
22
22
  const CLOSE = Symbol('close')
23
23
  const MODE = Symbol('mode')
24
+ const AWAITDRAIN = Symbol('awaitDrain')
25
+ const ONDRAIN = Symbol('ondrain')
24
26
  const warner = require('./warn-mixin.js')
25
27
  const winchars = require('./winchars.js')
26
28
  const stripAbsolutePath = require('./strip-absolute-path.js')
@@ -49,6 +51,15 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
49
51
  this.noMtime = !!opt.noMtime
50
52
  this.mtime = opt.mtime || null
51
53
 
54
+ this.fd = null
55
+ this.blockLen = null
56
+ this.blockRemain = null
57
+ this.buf = null
58
+ this.offset = null
59
+ this.length = null
60
+ this.pos = null
61
+ this.remain = null
62
+
52
63
  if (typeof opt.onwarn === 'function')
53
64
  this.on('warn', opt.onwarn)
54
65
 
@@ -139,7 +150,7 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
139
150
  })
140
151
 
141
152
  if (this.header.encode() && !this.noPax) {
142
- this.write(new Pax({
153
+ super.write(new Pax({
143
154
  atime: this.portable ? null : this.header.atime,
144
155
  ctime: this.portable ? null : this.header.ctime,
145
156
  gid: this.portable ? null : this.header.gid,
@@ -154,7 +165,7 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
154
165
  nlink: this.portable ? null : this.stat.nlink,
155
166
  }).encode())
156
167
  }
157
- this.write(this.header.block)
168
+ super.write(this.header.block)
158
169
  }
159
170
 
160
171
  [DIRECTORY] () {
@@ -214,74 +225,107 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
214
225
  }
215
226
 
216
227
  [ONOPENFILE] (fd) {
217
- const blockLen = 512 * Math.ceil(this.stat.size / 512)
218
- const bufLen = Math.min(blockLen, this.maxReadSize)
219
- const buf = Buffer.allocUnsafe(bufLen)
220
- this[READ](fd, buf, 0, buf.length, 0, this.stat.size, blockLen)
228
+ this.fd = fd
229
+ this.blockLen = 512 * Math.ceil(this.stat.size / 512)
230
+ this.blockRemain = this.blockLen
231
+ const bufLen = Math.min(this.blockLen, this.maxReadSize)
232
+ this.buf = Buffer.allocUnsafe(bufLen)
233
+ this.offset = 0
234
+ this.pos = 0
235
+ this.remain = this.stat.size
236
+ this.length = this.buf.length
237
+ this[READ]()
221
238
  }
222
239
 
223
- [READ] (fd, buf, offset, length, pos, remain, blockRemain) {
240
+ [READ] () {
241
+ const { fd, buf, offset, length, pos } = this
224
242
  fs.read(fd, buf, offset, length, pos, (er, bytesRead) => {
225
243
  if (er) {
226
244
  // ignoring the error from close(2) is a bad practice, but at
227
245
  // this point we already have an error, don't need another one
228
- return this[CLOSE](fd, () => this.emit('error', er))
246
+ return this[CLOSE](() => this.emit('error', er))
229
247
  }
230
- this[ONREAD](fd, buf, offset, length, pos, remain, blockRemain, bytesRead)
248
+ this[ONREAD](bytesRead)
231
249
  })
232
250
  }
233
251
 
234
- [CLOSE] (fd, cb) {
235
- fs.close(fd, cb)
252
+ [CLOSE] (cb) {
253
+ fs.close(this.fd, cb)
236
254
  }
237
255
 
238
- [ONREAD] (fd, buf, offset, length, pos, remain, blockRemain, bytesRead) {
239
- if (bytesRead <= 0 && remain > 0) {
256
+ [ONREAD] (bytesRead) {
257
+ if (bytesRead <= 0 && this.remain > 0) {
240
258
  const er = new Error('encountered unexpected EOF')
241
259
  er.path = this.absolute
242
260
  er.syscall = 'read'
243
261
  er.code = 'EOF'
244
- return this[CLOSE](fd, () => this.emit('error', er))
262
+ return this[CLOSE](() => this.emit('error', er))
245
263
  }
246
264
 
247
- if (bytesRead > remain) {
265
+ if (bytesRead > this.remain) {
248
266
  const er = new Error('did not encounter expected EOF')
249
267
  er.path = this.absolute
250
268
  er.syscall = 'read'
251
269
  er.code = 'EOF'
252
- return this[CLOSE](fd, () => this.emit('error', er))
270
+ return this[CLOSE](() => this.emit('error', er))
253
271
  }
254
272
 
255
273
  // null out the rest of the buffer, if we could fit the block padding
256
- if (bytesRead === remain) {
257
- for (let i = bytesRead; i < length && bytesRead < blockRemain; i++) {
258
- buf[i + offset] = 0
274
+ // at the end of this loop, we've incremented bytesRead and this.remain
275
+ // to be incremented up to the blockRemain level, as if we had expected
276
+ // to get a null-padded file, and read it until the end. then we will
277
+ // decrement both remain and blockRemain by bytesRead, and know that we
278
+ // reached the expected EOF, without any null buffer to append.
279
+ if (bytesRead === this.remain) {
280
+ for (let i = bytesRead; i < this.length && bytesRead < this.blockRemain; i++) {
281
+ this.buf[i + this.offset] = 0
259
282
  bytesRead++
260
- remain++
283
+ this.remain++
261
284
  }
262
285
  }
263
286
 
264
- const writeBuf = offset === 0 && bytesRead === buf.length ?
265
- buf : buf.slice(offset, offset + bytesRead)
266
- remain -= bytesRead
267
- blockRemain -= bytesRead
268
- pos += bytesRead
269
- offset += bytesRead
287
+ const writeBuf = this.offset === 0 && bytesRead === this.buf.length ?
288
+ this.buf : this.buf.slice(this.offset, this.offset + bytesRead)
289
+
290
+ const flushed = this.write(writeBuf)
291
+ if (!flushed)
292
+ this[AWAITDRAIN](() => this[ONDRAIN]())
293
+ else
294
+ this[ONDRAIN]()
295
+ }
296
+
297
+ [AWAITDRAIN] (cb) {
298
+ this.once('drain', cb)
299
+ }
270
300
 
271
- this.write(writeBuf)
301
+ write (writeBuf) {
302
+ if (this.blockRemain < writeBuf.length) {
303
+ const er = new Error('writing more data than expected')
304
+ er.path = this.absolute
305
+ return this.emit('error', er)
306
+ }
307
+ this.remain -= writeBuf.length
308
+ this.blockRemain -= writeBuf.length
309
+ this.pos += writeBuf.length
310
+ this.offset += writeBuf.length
311
+ return super.write(writeBuf)
312
+ }
272
313
 
273
- if (!remain) {
274
- if (blockRemain)
275
- this.write(Buffer.alloc(blockRemain))
276
- return this[CLOSE](fd, er => er ? this.emit('error', er) : this.end())
314
+ [ONDRAIN] () {
315
+ if (!this.remain) {
316
+ if (this.blockRemain)
317
+ super.write(Buffer.alloc(this.blockRemain))
318
+ return this[CLOSE](er => er ? this.emit('error', er) : this.end())
277
319
  }
278
320
 
279
- if (offset >= length) {
280
- buf = Buffer.allocUnsafe(length)
281
- offset = 0
321
+ if (this.offset >= this.length) {
322
+ // if we only have a smaller bit left to read, alloc a smaller buffer
323
+ // otherwise, keep it the same length it was before.
324
+ this.buf = Buffer.allocUnsafe(Math.min(this.blockRemain, this.buf.length))
325
+ this.offset = 0
282
326
  }
283
- length = buf.length - offset
284
- this[READ](fd, buf, offset, length, pos, remain, blockRemain)
327
+ this.length = this.buf.length - this.offset
328
+ this[READ]()
285
329
  }
286
330
  })
287
331
 
@@ -298,25 +342,30 @@ class WriteEntrySync extends WriteEntry {
298
342
  this[ONOPENFILE](fs.openSync(this.absolute, 'r'))
299
343
  }
300
344
 
301
- [READ] (fd, buf, offset, length, pos, remain, blockRemain) {
345
+ [READ] () {
302
346
  let threw = true
303
347
  try {
348
+ const { fd, buf, offset, length, pos } = this
304
349
  const bytesRead = fs.readSync(fd, buf, offset, length, pos)
305
- this[ONREAD](fd, buf, offset, length, pos, remain, blockRemain, bytesRead)
350
+ this[ONREAD](bytesRead)
306
351
  threw = false
307
352
  } finally {
308
353
  // ignoring the error from close(2) is a bad practice, but at
309
354
  // this point we already have an error, don't need another one
310
355
  if (threw) {
311
356
  try {
312
- this[CLOSE](fd, () => {})
357
+ this[CLOSE](() => {})
313
358
  } catch (er) {}
314
359
  }
315
360
  }
316
361
  }
317
362
 
318
- [CLOSE] (fd, cb) {
319
- fs.closeSync(fd)
363
+ [AWAITDRAIN] (cb) {
364
+ cb()
365
+ }
366
+
367
+ [CLOSE] (cb) {
368
+ fs.closeSync(this.fd)
320
369
  cb()
321
370
  }
322
371
  }
@@ -421,7 +470,7 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
421
470
 
422
471
  end () {
423
472
  if (this.blockRemain)
424
- this.write(Buffer.alloc(this.blockRemain))
473
+ super.write(Buffer.alloc(this.blockRemain))
425
474
  return super.end()
426
475
  }
427
476
  })
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "author": "Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me/)",
3
3
  "name": "tar",
4
4
  "description": "tar for node",
5
- "version": "6.1.1",
5
+ "version": "6.1.5",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "https://github.com/npm/node-tar.git"