pax.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. 'use strict'
  2. const Header = require('./header.js')
  3. const path = require('path')
  4. class Pax {
  5. constructor (obj, global) {
  6. this.atime = obj.atime || null
  7. this.charset = obj.charset || null
  8. this.comment = obj.comment || null
  9. this.ctime = obj.ctime || null
  10. this.gid = obj.gid || null
  11. this.gname = obj.gname || null
  12. this.linkpath = obj.linkpath || null
  13. this.mtime = obj.mtime || null
  14. this.path = obj.path || null
  15. this.size = obj.size || null
  16. this.uid = obj.uid || null
  17. this.uname = obj.uname || null
  18. this.dev = obj.dev || null
  19. this.ino = obj.ino || null
  20. this.nlink = obj.nlink || null
  21. this.global = global || false
  22. }
  23. encode () {
  24. const body = this.encodeBody()
  25. if (body === '')
  26. return null
  27. const bodyLen = Buffer.byteLength(body)
  28. // round up to 512 bytes
  29. // add 512 for header
  30. const bufLen = 512 * Math.ceil(1 + bodyLen / 512)
  31. const buf = Buffer.allocUnsafe(bufLen)
  32. // 0-fill the header section, it might not hit every field
  33. for (let i = 0; i < 512; i++) {
  34. buf[i] = 0
  35. }
  36. new Header({
  37. // XXX split the path
  38. // then the path should be PaxHeader + basename, but less than 99,
  39. // prepend with the dirname
  40. path: ('PaxHeader/' + path.basename(this.path)).slice(0, 99),
  41. mode: this.mode || 0o644,
  42. uid: this.uid || null,
  43. gid: this.gid || null,
  44. size: bodyLen,
  45. mtime: this.mtime || null,
  46. type: this.global ? 'GlobalExtendedHeader' : 'ExtendedHeader',
  47. linkpath: '',
  48. uname: this.uname || '',
  49. gname: this.gname || '',
  50. devmaj: 0,
  51. devmin: 0,
  52. atime: this.atime || null,
  53. ctime: this.ctime || null
  54. }).encode(buf)
  55. buf.write(body, 512, bodyLen, 'utf8')
  56. // null pad after the body
  57. for (let i = bodyLen + 512; i < buf.length; i++) {
  58. buf[i] = 0
  59. }
  60. return buf
  61. }
  62. encodeBody () {
  63. return (
  64. this.encodeField('path') +
  65. this.encodeField('ctime') +
  66. this.encodeField('atime') +
  67. this.encodeField('dev') +
  68. this.encodeField('ino') +
  69. this.encodeField('nlink') +
  70. this.encodeField('charset') +
  71. this.encodeField('comment') +
  72. this.encodeField('gid') +
  73. this.encodeField('gname') +
  74. this.encodeField('linkpath') +
  75. this.encodeField('mtime') +
  76. this.encodeField('size') +
  77. this.encodeField('uid') +
  78. this.encodeField('uname')
  79. )
  80. }
  81. encodeField (field) {
  82. if (this[field] === null || this[field] === undefined)
  83. return ''
  84. const v = this[field] instanceof Date ? this[field].getTime() / 1000
  85. : this[field]
  86. const s = ' ' +
  87. (field === 'dev' || field === 'ino' || field === 'nlink'
  88. ? 'SCHILY.' : '') +
  89. field + '=' + v + '\n'
  90. const byteLen = Buffer.byteLength(s)
  91. // the digits includes the length of the digits in ascii base-10
  92. // so if it's 9 characters, then adding 1 for the 9 makes it 10
  93. // which makes it 11 chars.
  94. let digits = Math.floor(Math.log(byteLen) / Math.log(10)) + 1
  95. if (byteLen + digits >= Math.pow(10, digits))
  96. digits += 1
  97. const len = digits + byteLen
  98. return len + s
  99. }
  100. }
  101. Pax.parse = (string, ex, g) => new Pax(merge(parseKV(string), ex), g)
  102. const merge = (a, b) =>
  103. b ? Object.keys(a).reduce((s, k) => (s[k] = a[k], s), b) : a
  104. const parseKV = string =>
  105. string
  106. .replace(/\n$/, '')
  107. .split('\n')
  108. .reduce(parseKVLine, Object.create(null))
  109. const parseKVLine = (set, line) => {
  110. const n = parseInt(line, 10)
  111. // XXX Values with \n in them will fail this.
  112. // Refactor to not be a naive line-by-line parse.
  113. if (n !== Buffer.byteLength(line) + 1)
  114. return set
  115. line = line.substr((n + ' ').length)
  116. const kv = line.split('=')
  117. const k = kv.shift().replace(/^SCHILY\.(dev|ino|nlink)/, '$1')
  118. if (!k)
  119. return set
  120. const v = kv.join('=')
  121. set[k] = /^([A-Z]+\.)?([mac]|birth|creation)time$/.test(k)
  122. ? new Date(v * 1000)
  123. : /^[0-9]+$/.test(v) ? +v
  124. : v
  125. return set
  126. }
  127. module.exports = Pax