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.

header.js 8.9KB

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