tar 6.1.4 → 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/unpack.js CHANGED
@@ -335,17 +335,35 @@ class Unpack extends Parser {
335
335
  mode: mode,
336
336
  autoClose: false,
337
337
  })
338
- 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
+ })
339
348
 
340
349
  let actions = 1
341
350
  const done = er => {
342
- if (er)
343
- 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
+ }
344
359
 
345
360
  if (--actions === 0) {
346
361
  fs.close(stream.fd, er => {
362
+ if (er)
363
+ this[ONERROR](er, entry)
364
+ else
365
+ this[UNPEND]()
347
366
  fullyDone()
348
- er ? this[ONERROR](er, entry) : this[UNPEND]()
349
367
  })
350
368
  }
351
369
  }
@@ -380,7 +398,10 @@ class Unpack extends Parser {
380
398
 
381
399
  const tx = this.transform ? this.transform(entry) || entry : entry
382
400
  if (tx !== entry) {
383
- tx.on('error', er => this[ONERROR](er, entry))
401
+ tx.on('error', er => {
402
+ this[ONERROR](er, entry)
403
+ fullyDone()
404
+ })
384
405
  entry.pipe(tx)
385
406
  }
386
407
  tx.pipe(stream)
@@ -390,8 +411,9 @@ class Unpack extends Parser {
390
411
  const mode = entry.mode & 0o7777 || this.dmode
391
412
  this[MKDIR](entry.absolute, mode, er => {
392
413
  if (er) {
414
+ this[ONERROR](er, entry)
393
415
  fullyDone()
394
- return this[ONERROR](er, entry)
416
+ return
395
417
  }
396
418
 
397
419
  let actions = 1
@@ -482,8 +504,9 @@ class Unpack extends Parser {
482
504
 
483
505
  this[MKDIR](path.dirname(entry.absolute), this.dmode, er => {
484
506
  if (er) {
507
+ this[ONERROR](er, entry)
485
508
  done()
486
- return this[ONERROR](er, entry)
509
+ return
487
510
  }
488
511
  fs.lstat(entry.absolute, (er, st) => {
489
512
  if (st && (this.keep || this.newer && st.mtime > entry.mtime)) {
@@ -491,7 +514,6 @@ class Unpack extends Parser {
491
514
  done()
492
515
  } else if (er || this[ISREUSABLE](entry, st))
493
516
  this[MAKEFS](null, entry, done)
494
-
495
517
  else if (st.isDirectory()) {
496
518
  if (entry.type === 'Directory') {
497
519
  if (!this.noChmod && (!entry.mode || (st.mode & 0o7777) === entry.mode))
@@ -509,8 +531,11 @@ class Unpack extends Parser {
509
531
  }
510
532
 
511
533
  [MAKEFS] (er, entry, done) {
512
- if (er)
513
- return this[ONERROR](er, entry)
534
+ if (er) {
535
+ this[ONERROR](er, entry)
536
+ done()
537
+ return
538
+ }
514
539
 
515
540
  switch (entry.type) {
516
541
  case 'File':
@@ -534,10 +559,12 @@ class Unpack extends Parser {
534
559
  // XXX: get the type ('file' or 'dir') for windows
535
560
  fs[link](linkpath, entry.absolute, er => {
536
561
  if (er)
537
- return this[ONERROR](er, entry)
562
+ this[ONERROR](er, entry)
563
+ else {
564
+ this[UNPEND]()
565
+ entry.resume()
566
+ }
538
567
  done()
539
- this[UNPEND]()
540
- entry.resume()
541
568
  })
542
569
  }
543
570
  }
@@ -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.4",
5
+ "version": "6.1.5",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "https://github.com/npm/node-tar.git"