read-entry.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. 'use strict'
  2. const MiniPass = require('minipass')
  3. const normPath = require('./normalize-windows-path.js')
  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 = normPath(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 = normPath(header.linkpath)
  59. this.uname = header.uname
  60. this.gname = header.gname
  61. if (ex)
  62. this[SLURP](ex)
  63. if (gex)
  64. this[SLURP](gex, true)
  65. }
  66. write (data) {
  67. const writeLen = data.length
  68. if (writeLen > this.blockRemain)
  69. throw new Error('writing more to entry than is appropriate')
  70. const r = this.remain
  71. const br = this.blockRemain
  72. this.remain = Math.max(0, r - writeLen)
  73. this.blockRemain = Math.max(0, br - writeLen)
  74. if (this.ignore)
  75. return true
  76. if (r >= writeLen)
  77. return super.write(data)
  78. // r < writeLen
  79. return super.write(data.slice(0, r))
  80. }
  81. [SLURP] (ex, global) {
  82. for (const k in ex) {
  83. // we slurp in everything except for the path attribute in
  84. // a global extended header, because that's weird.
  85. if (ex[k] !== null && ex[k] !== undefined &&
  86. !(global && k === 'path'))
  87. this[k] = k === 'path' || k === 'linkpath' ? normPath(ex[k]) : ex[k]
  88. }
  89. }
  90. }