read-entry.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. 'use strict'
  2. const types = require('./types.js')
  3. const MiniPass = require('minipass')
  4. const normPath = require('./normalize-windows-path.js')
  5. const SLURP = Symbol('slurp')
  6. module.exports = class ReadEntry extends MiniPass {
  7. constructor (header, ex, gex) {
  8. super()
  9. // read entries always start life paused. this is to avoid the
  10. // situation where Minipass's auto-ending empty streams results
  11. // in an entry ending before we're ready for it.
  12. this.pause()
  13. this.extended = ex
  14. this.globalExtended = gex
  15. this.header = header
  16. this.startBlockSize = 512 * Math.ceil(header.size / 512)
  17. this.blockRemain = this.startBlockSize
  18. this.remain = header.size
  19. this.type = header.type
  20. this.meta = false
  21. this.ignore = false
  22. switch (this.type) {
  23. case 'File':
  24. case 'OldFile':
  25. case 'Link':
  26. case 'SymbolicLink':
  27. case 'CharacterDevice':
  28. case 'BlockDevice':
  29. case 'Directory':
  30. case 'FIFO':
  31. case 'ContiguousFile':
  32. case 'GNUDumpDir':
  33. break
  34. case 'NextFileHasLongLinkpath':
  35. case 'NextFileHasLongPath':
  36. case 'OldGnuLongPath':
  37. case 'GlobalExtendedHeader':
  38. case 'ExtendedHeader':
  39. case 'OldExtendedHeader':
  40. this.meta = true
  41. break
  42. // NOTE: gnutar and bsdtar treat unrecognized types as 'File'
  43. // it may be worth doing the same, but with a warning.
  44. default:
  45. this.ignore = true
  46. }
  47. this.path = normPath(header.path)
  48. this.mode = header.mode
  49. if (this.mode)
  50. this.mode = this.mode & 0o7777
  51. this.uid = header.uid
  52. this.gid = header.gid
  53. this.uname = header.uname
  54. this.gname = header.gname
  55. this.size = header.size
  56. this.mtime = header.mtime
  57. this.atime = header.atime
  58. this.ctime = header.ctime
  59. this.linkpath = normPath(header.linkpath)
  60. this.uname = header.uname
  61. this.gname = header.gname
  62. if (ex) this[SLURP](ex)
  63. if (gex) 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 (let 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] = k === 'path' || k === 'linkpath' ? normPath(ex[k]) : ex[k]
  87. }
  88. }
  89. }