You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

read-entry.js 2.8KB

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