pax.js 3.9 KB

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