header.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. 'use strict'
  2. // parse a 512-byte header block to a data object, or vice-versa
  3. // encode returns `true` if a pax extended header is needed, because
  4. // the data could not be faithfully encoded in a simple header.
  5. // (Also, check header.needPax to see if it needs a pax header.)
  6. const types = require('./types.js')
  7. const pathModule = require('path').posix
  8. const large = require('./large-numbers.js')
  9. const SLURP = Symbol('slurp')
  10. const TYPE = Symbol('type')
  11. class Header {
  12. constructor (data, off, ex, gex) {
  13. this.cksumValid = false
  14. this.needPax = false
  15. this.nullBlock = false
  16. this.block = null
  17. this.path = null
  18. this.mode = null
  19. this.uid = null
  20. this.gid = null
  21. this.size = null
  22. this.mtime = null
  23. this.cksum = null
  24. this[TYPE] = '0'
  25. this.linkpath = null
  26. this.uname = null
  27. this.gname = null
  28. this.devmaj = 0
  29. this.devmin = 0
  30. this.atime = null
  31. this.ctime = null
  32. if (Buffer.isBuffer(data))
  33. this.decode(data, off || 0, ex, gex)
  34. else if (data)
  35. this.set(data)
  36. }
  37. decode (buf, off, ex, gex) {
  38. if (!off)
  39. off = 0
  40. if (!buf || !(buf.length >= off + 512))
  41. throw new Error('need 512 bytes for header')
  42. this.path = decString(buf, off, 100)
  43. this.mode = decNumber(buf, off + 100, 8)
  44. this.uid = decNumber(buf, off + 108, 8)
  45. this.gid = decNumber(buf, off + 116, 8)
  46. this.size = decNumber(buf, off + 124, 12)
  47. this.mtime = decDate(buf, off + 136, 12)
  48. this.cksum = decNumber(buf, off + 148, 12)
  49. // if we have extended or global extended headers, apply them now
  50. // See https://github.com/npm/node-tar/pull/187
  51. this[SLURP](ex)
  52. this[SLURP](gex, true)
  53. // old tar versions marked dirs as a file with a trailing /
  54. this[TYPE] = decString(buf, off + 156, 1)
  55. if (this[TYPE] === '')
  56. this[TYPE] = '0'
  57. if (this[TYPE] === '0' && this.path.substr(-1) === '/')
  58. this[TYPE] = '5'
  59. // tar implementations sometimes incorrectly put the stat(dir).size
  60. // as the size in the tarball, even though Directory entries are
  61. // not able to have any body at all. In the very rare chance that
  62. // it actually DOES have a body, we weren't going to do anything with
  63. // it anyway, and it'll just be a warning about an invalid header.
  64. if (this[TYPE] === '5')
  65. this.size = 0
  66. this.linkpath = decString(buf, off + 157, 100)
  67. if (buf.slice(off + 257, off + 265).toString() === 'ustar\u000000') {
  68. this.uname = decString(buf, off + 265, 32)
  69. this.gname = decString(buf, off + 297, 32)
  70. this.devmaj = decNumber(buf, off + 329, 8)
  71. this.devmin = decNumber(buf, off + 337, 8)
  72. if (buf[off + 475] !== 0) {
  73. // definitely a prefix, definitely >130 chars.
  74. const prefix = decString(buf, off + 345, 155)
  75. this.path = prefix + '/' + this.path
  76. } else {
  77. const prefix = decString(buf, off + 345, 130)
  78. if (prefix)
  79. this.path = prefix + '/' + this.path
  80. this.atime = decDate(buf, off + 476, 12)
  81. this.ctime = decDate(buf, off + 488, 12)
  82. }
  83. }
  84. let sum = 8 * 0x20
  85. for (let i = off; i < off + 148; i++)
  86. sum += buf[i]
  87. for (let i = off + 156; i < off + 512; i++)
  88. sum += buf[i]
  89. this.cksumValid = sum === this.cksum
  90. if (this.cksum === null && sum === 8 * 0x20)
  91. this.nullBlock = true
  92. }
  93. [SLURP] (ex, global) {
  94. for (const k in ex) {
  95. // we slurp in everything except for the path attribute in
  96. // a global extended header, because that's weird.
  97. if (ex[k] !== null && ex[k] !== undefined &&
  98. !(global && k === 'path'))
  99. this[k] = ex[k]
  100. }
  101. }
  102. encode (buf, off) {
  103. if (!buf) {
  104. buf = this.block = Buffer.alloc(512)
  105. off = 0
  106. }
  107. if (!off)
  108. off = 0
  109. if (!(buf.length >= off + 512))
  110. throw new Error('need 512 bytes for header')
  111. const prefixSize = this.ctime || this.atime ? 130 : 155
  112. const split = splitPrefix(this.path || '', prefixSize)
  113. const path = split[0]
  114. const prefix = split[1]
  115. this.needPax = split[2]
  116. this.needPax = encString(buf, off, 100, path) || this.needPax
  117. this.needPax = encNumber(buf, off + 100, 8, this.mode) || this.needPax
  118. this.needPax = encNumber(buf, off + 108, 8, this.uid) || this.needPax
  119. this.needPax = encNumber(buf, off + 116, 8, this.gid) || this.needPax
  120. this.needPax = encNumber(buf, off + 124, 12, this.size) || this.needPax
  121. this.needPax = encDate(buf, off + 136, 12, this.mtime) || this.needPax
  122. buf[off + 156] = this[TYPE].charCodeAt(0)
  123. this.needPax = encString(buf, off + 157, 100, this.linkpath) || this.needPax
  124. buf.write('ustar\u000000', off + 257, 8)
  125. this.needPax = encString(buf, off + 265, 32, this.uname) || this.needPax
  126. this.needPax = encString(buf, off + 297, 32, this.gname) || this.needPax
  127. this.needPax = encNumber(buf, off + 329, 8, this.devmaj) || this.needPax
  128. this.needPax = encNumber(buf, off + 337, 8, this.devmin) || this.needPax
  129. this.needPax = encString(buf, off + 345, prefixSize, prefix) || this.needPax
  130. if (buf[off + 475] !== 0)
  131. this.needPax = encString(buf, off + 345, 155, prefix) || this.needPax
  132. else {
  133. this.needPax = encString(buf, off + 345, 130, prefix) || this.needPax
  134. this.needPax = encDate(buf, off + 476, 12, this.atime) || this.needPax
  135. this.needPax = encDate(buf, off + 488, 12, this.ctime) || this.needPax
  136. }
  137. let sum = 8 * 0x20
  138. for (let i = off; i < off + 148; i++)
  139. sum += buf[i]
  140. for (let i = off + 156; i < off + 512; i++)
  141. sum += buf[i]
  142. this.cksum = sum
  143. encNumber(buf, off + 148, 8, this.cksum)
  144. this.cksumValid = true
  145. return this.needPax
  146. }
  147. set (data) {
  148. for (const i in data) {
  149. if (data[i] !== null && data[i] !== undefined)
  150. this[i] = data[i]
  151. }
  152. }
  153. get type () {
  154. return types.name.get(this[TYPE]) || this[TYPE]
  155. }
  156. get typeKey () {
  157. return this[TYPE]
  158. }
  159. set type (type) {
  160. if (types.code.has(type))
  161. this[TYPE] = types.code.get(type)
  162. else
  163. this[TYPE] = type
  164. }
  165. }
  166. const splitPrefix = (p, prefixSize) => {
  167. const pathSize = 100
  168. let pp = p
  169. let prefix = ''
  170. let ret
  171. const root = pathModule.parse(p).root || '.'
  172. if (Buffer.byteLength(pp) < pathSize)
  173. ret = [pp, prefix, false]
  174. else {
  175. // first set prefix to the dir, and path to the base
  176. prefix = pathModule.dirname(pp)
  177. pp = pathModule.basename(pp)
  178. do {
  179. // both fit!
  180. if (Buffer.byteLength(pp) <= pathSize &&
  181. Buffer.byteLength(prefix) <= prefixSize)
  182. ret = [pp, prefix, false]
  183. // prefix fits in prefix, but path doesn't fit in path
  184. else if (Buffer.byteLength(pp) > pathSize &&
  185. Buffer.byteLength(prefix) <= prefixSize)
  186. ret = [pp.substr(0, pathSize - 1), prefix, true]
  187. else {
  188. // make path take a bit from prefix
  189. pp = pathModule.join(pathModule.basename(prefix), pp)
  190. prefix = pathModule.dirname(prefix)
  191. }
  192. } while (prefix !== root && !ret)
  193. // at this point, found no resolution, just truncate
  194. if (!ret)
  195. ret = [p.substr(0, pathSize - 1), '', true]
  196. }
  197. return ret
  198. }
  199. const decString = (buf, off, size) =>
  200. buf.slice(off, off + size).toString('utf8').replace(/\0.*/, '')
  201. const decDate = (buf, off, size) =>
  202. numToDate(decNumber(buf, off, size))
  203. const numToDate = num => num === null ? null : new Date(num * 1000)
  204. const decNumber = (buf, off, size) =>
  205. buf[off] & 0x80 ? large.parse(buf.slice(off, off + size))
  206. : decSmallNumber(buf, off, size)
  207. const nanNull = value => isNaN(value) ? null : value
  208. const decSmallNumber = (buf, off, size) =>
  209. nanNull(parseInt(
  210. buf.slice(off, off + size)
  211. .toString('utf8').replace(/\0.*$/, '').trim(), 8))
  212. // the maximum encodable as a null-terminated octal, by field size
  213. const MAXNUM = {
  214. 12: 0o77777777777,
  215. 8: 0o7777777,
  216. }
  217. const encNumber = (buf, off, size, number) =>
  218. number === null ? false :
  219. number > MAXNUM[size] || number < 0
  220. ? (large.encode(number, buf.slice(off, off + size)), true)
  221. : (encSmallNumber(buf, off, size, number), false)
  222. const encSmallNumber = (buf, off, size, number) =>
  223. buf.write(octalString(number, size), off, size, 'ascii')
  224. const octalString = (number, size) =>
  225. padOctal(Math.floor(number).toString(8), size)
  226. const padOctal = (string, size) =>
  227. (string.length === size - 1 ? string
  228. : new Array(size - string.length - 1).join('0') + string + ' ') + '\0'
  229. const encDate = (buf, off, size, date) =>
  230. date === null ? false :
  231. encNumber(buf, off, size, date.getTime() / 1000)
  232. // enough to fill the longest string we've got
  233. const NULLS = new Array(156).join('\0')
  234. // pad with nulls, return true if it's longer or non-ascii
  235. const encString = (buf, off, size, string) =>
  236. string === null ? false :
  237. (buf.write(string + NULLS, off, size, 'utf8'),
  238. string.length !== Buffer.byteLength(string) || string.length > size)
  239. module.exports = Header