read-entry.js 2.6 KB

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