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. }
  88. for (let i = off + 156; i < off + 512; i++) {
  89. sum += buf[i]
  90. }
  91. this.cksumValid = sum === this.cksum
  92. if (this.cksum === null && sum === 8 * 0x20)
  93. this.nullBlock = true
  94. }
  95. [SLURP] (ex, global) {
  96. for (let k in ex) {
  97. // we slurp in everything except for the path attribute in
  98. // a global extended header, because that's weird.
  99. if (ex[k] !== null && ex[k] !== undefined &&
  100. !(global && k === 'path'))
  101. this[k] = ex[k]
  102. }
  103. }
  104. encode (buf, off) {
  105. if (!buf) {
  106. buf = this.block = Buffer.alloc(512)
  107. off = 0
  108. }
  109. if (!off)
  110. off = 0
  111. if (!(buf.length >= off + 512))
  112. throw new Error('need 512 bytes for header')
  113. const prefixSize = this.ctime || this.atime ? 130 : 155
  114. const split = splitPrefix(this.path || '', prefixSize)
  115. const path = split[0]
  116. const prefix = split[1]
  117. this.needPax = split[2]
  118. this.needPax = encString(buf, off, 100, path) || this.needPax
  119. this.needPax = encNumber(buf, off + 100, 8, this.mode) || this.needPax
  120. this.needPax = encNumber(buf, off + 108, 8, this.uid) || this.needPax
  121. this.needPax = encNumber(buf, off + 116, 8, this.gid) || this.needPax
  122. this.needPax = encNumber(buf, off + 124, 12, this.size) || this.needPax
  123. this.needPax = encDate(buf, off + 136, 12, this.mtime) || this.needPax
  124. buf[off + 156] = this[TYPE].charCodeAt(0)
  125. this.needPax = encString(buf, off + 157, 100, this.linkpath) || this.needPax
  126. buf.write('ustar\u000000', off + 257, 8)
  127. this.needPax = encString(buf, off + 265, 32, this.uname) || this.needPax
  128. this.needPax = encString(buf, off + 297, 32, this.gname) || this.needPax
  129. this.needPax = encNumber(buf, off + 329, 8, this.devmaj) || this.needPax
  130. this.needPax = encNumber(buf, off + 337, 8, this.devmin) || this.needPax
  131. this.needPax = encString(buf, off + 345, prefixSize, prefix) || this.needPax
  132. if (buf[off + 475] !== 0)
  133. this.needPax = encString(buf, off + 345, 155, prefix) || this.needPax
  134. else {
  135. this.needPax = encString(buf, off + 345, 130, prefix) || this.needPax
  136. this.needPax = encDate(buf, off + 476, 12, this.atime) || this.needPax
  137. this.needPax = encDate(buf, off + 488, 12, this.ctime) || this.needPax
  138. }
  139. let sum = 8 * 0x20
  140. for (let i = off; i < off + 148; i++) {
  141. sum += buf[i]
  142. }
  143. for (let i = off + 156; i < off + 512; i++) {
  144. sum += buf[i]
  145. }
  146. this.cksum = sum
  147. encNumber(buf, off + 148, 8, this.cksum)
  148. this.cksumValid = true
  149. return this.needPax
  150. }
  151. set (data) {
  152. for (let i in data) {
  153. if (data[i] !== null && data[i] !== undefined)
  154. this[i] = data[i]
  155. }
  156. }
  157. get type () {
  158. return types.name.get(this[TYPE]) || this[TYPE]
  159. }
  160. get typeKey () {
  161. return this[TYPE]
  162. }
  163. set type (type) {
  164. if (types.code.has(type))
  165. this[TYPE] = types.code.get(type)
  166. else
  167. this[TYPE] = type
  168. }
  169. }
  170. const splitPrefix = (p, prefixSize) => {
  171. const pathSize = 100
  172. let pp = p
  173. let prefix = ''
  174. let ret
  175. const root = pathModule.parse(p).root || '.'
  176. if (Buffer.byteLength(pp) < pathSize)
  177. ret = [pp, prefix, false]
  178. else {
  179. // first set prefix to the dir, and path to the base
  180. prefix = pathModule.dirname(pp)
  181. pp = pathModule.basename(pp)
  182. do {
  183. // both fit!
  184. if (Buffer.byteLength(pp) <= pathSize &&
  185. Buffer.byteLength(prefix) <= prefixSize)
  186. ret = [pp, prefix, false]
  187. // prefix fits in prefix, but path doesn't fit in path
  188. else if (Buffer.byteLength(pp) > pathSize &&
  189. Buffer.byteLength(prefix) <= prefixSize)
  190. ret = [pp.substr(0, pathSize - 1), prefix, true]
  191. else {
  192. // make path take a bit from prefix
  193. pp = pathModule.join(pathModule.basename(prefix), pp)
  194. prefix = pathModule.dirname(prefix)
  195. }
  196. } while (prefix !== root && !ret)
  197. // at this point, found no resolution, just truncate
  198. if (!ret)
  199. ret = [p.substr(0, pathSize - 1), '', true]
  200. }
  201. return ret
  202. }
  203. const decString = (buf, off, size) =>
  204. buf.slice(off, off + size).toString('utf8').replace(/\0.*/, '')
  205. const decDate = (buf, off, size) =>
  206. numToDate(decNumber(buf, off, size))
  207. const numToDate = num => num === null ? null : new Date(num * 1000)
  208. const decNumber = (buf, off, size) =>
  209. buf[off] & 0x80 ? large.parse(buf.slice(off, off + size))
  210. : decSmallNumber(buf, off, size)
  211. const nanNull = value => isNaN(value) ? null : value
  212. const decSmallNumber = (buf, off, size) =>
  213. nanNull(parseInt(
  214. buf.slice(off, off + size)
  215. .toString('utf8').replace(/\0.*$/, '').trim(), 8))
  216. // the maximum encodable as a null-terminated octal, by field size
  217. const MAXNUM = {
  218. 12: 0o77777777777,
  219. 8 : 0o7777777
  220. }
  221. const encNumber = (buf, off, size, number) =>
  222. number === null ? false :
  223. number > MAXNUM[size] || number < 0
  224. ? (large.encode(number, buf.slice(off, off + size)), true)
  225. : (encSmallNumber(buf, off, size, number), false)
  226. const encSmallNumber = (buf, off, size, number) =>
  227. buf.write(octalString(number, size), off, size, 'ascii')
  228. const octalString = (number, size) =>
  229. padOctal(Math.floor(number).toString(8), size)
  230. const padOctal = (string, size) =>
  231. (string.length === size - 1 ? string
  232. : new Array(size - string.length - 1).join('0') + string + ' ') + '\0'
  233. const encDate = (buf, off, size, date) =>
  234. date === null ? false :
  235. encNumber(buf, off, size, date.getTime() / 1000)
  236. // enough to fill the longest string we've got
  237. const NULLS = new Array(156).join('\0')
  238. // pad with nulls, return true if it's longer or non-ascii
  239. const encString = (buf, off, size, string) =>
  240. string === null ? false :
  241. (buf.write(string + NULLS, off, size, 'utf8'),
  242. string.length !== Buffer.byteLength(string) || string.length > size)
  243. module.exports = Header