header.js 8.8 KB

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