read-entry.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. 'use strict'
  2. const types = require('./types.js')
  3. const MiniPass = require('minipass')
  4. const SLURP = Symbol('slurp')
  5. module.exports = class ReadEntry extends MiniPass {
  6. constructor (header, ex, gex) {
  7. super()
  8. // read entries always start life paused. this is to avoid the
  9. // situation where Minipass's auto-ending empty streams results
  10. // in an entry ending before we're ready for it.
  11. this.pause()
  12. this.extended = ex
  13. this.globalExtended = gex
  14. this.header = header
  15. this.startBlockSize = 512 * Math.ceil(header.size / 512)
  16. this.blockRemain = this.startBlockSize
  17. this.remain = header.size
  18. this.type = header.type
  19. this.meta = false
  20. this.ignore = false
  21. switch (this.type) {
  22. case 'File':
  23. case 'OldFile':
  24. case 'Link':
  25. case 'SymbolicLink':
  26. case 'CharacterDevice':
  27. case 'BlockDevice':
  28. case 'Directory':
  29. case 'FIFO':
  30. case 'ContiguousFile':
  31. case 'GNUDumpDir':
  32. break
  33. case 'NextFileHasLongLinkpath':
  34. case 'NextFileHasLongPath':
  35. case 'OldGnuLongPath':
  36. case 'GlobalExtendedHeader':
  37. case 'ExtendedHeader':
  38. case 'OldExtendedHeader':
  39. this.meta = true
  40. break
  41. // NOTE: gnutar and bsdtar treat unrecognized types as 'File'
  42. // it may be worth doing the same, but with a warning.
  43. default:
  44. this.ignore = true
  45. }
  46. this.path = header.path
  47. this.mode = header.mode
  48. if (this.mode)
  49. this.mode = this.mode & 0o7777
  50. this.uid = header.uid
  51. this.gid = header.gid
  52. this.uname = header.uname
  53. this.gname = header.gname
  54. this.size = header.size
  55. this.mtime = header.mtime
  56. this.atime = header.atime
  57. this.ctime = header.ctime
  58. this.linkpath = header.linkpath
  59. this.uname = header.uname
  60. this.gname = header.gname
  61. if (ex) this[SLURP](ex)
  62. if (gex) this[SLURP](gex, true)
  63. }
  64. write (data) {
  65. const writeLen = data.length
  66. if (writeLen > this.blockRemain)
  67. throw new Error('writing more to entry than is appropriate')
  68. const r = this.remain
  69. const br = this.blockRemain
  70. this.remain = Math.max(0, r - writeLen)
  71. this.blockRemain = Math.max(0, br - writeLen)
  72. if (this.ignore)
  73. return true
  74. if (r >= writeLen)
  75. return super.write(data)
  76. // r < writeLen
  77. return super.write(data.slice(0, r))
  78. }
  79. [SLURP] (ex, global) {
  80. for (let k in ex) {
  81. // we slurp in everything except for the path attribute in
  82. // a global extended header, because that's weird.
  83. if (ex[k] !== null && ex[k] !== undefined &&
  84. !(global && k === 'path'))
  85. this[k] = ex[k]
  86. }
  87. }
  88. }