2 const Buffer = require('./buffer.js')
3 const Header = require('./header.js')
4 const path = require('path')
7 constructor (obj, global) {
8 this.atime = obj.atime || null
9 this.charset = obj.charset || null
10 this.comment = obj.comment || null
11 this.ctime = obj.ctime || null
12 this.gid = obj.gid || null
13 this.gname = obj.gname || null
14 this.linkpath = obj.linkpath || null
15 this.mtime = obj.mtime || null
16 this.path = obj.path || null
17 this.size = obj.size || null
18 this.uid = obj.uid || null
19 this.uname = obj.uname || null
20 this.dev = obj.dev || null
21 this.ino = obj.ino || null
22 this.nlink = obj.nlink || null
23 this.global = global || false
27 const body = this.encodeBody()
31 const bodyLen = Buffer.byteLength(body)
32 // round up to 512 bytes
34 const bufLen = 512 * Math.ceil(1 + bodyLen / 512)
35 const buf = Buffer.allocUnsafe(bufLen)
37 // 0-fill the header section, it might not hit every field
38 for (let i = 0; i < 512; i++) {
44 // then the path should be PaxHeader + basename, but less than 99,
45 // prepend with the dirname
46 path: ('PaxHeader/' + path.basename(this.path)).slice(0, 99),
47 mode: this.mode || 0o644,
48 uid: this.uid || null,
49 gid: this.gid || null,
51 mtime: this.mtime || null,
52 type: this.global ? 'GlobalExtendedHeader' : 'ExtendedHeader',
54 uname: this.uname || '',
55 gname: this.gname || '',
58 atime: this.atime || null,
59 ctime: this.ctime || null
62 buf.write(body, 512, bodyLen, 'utf8')
64 // null pad after the body
65 for (let i = bodyLen + 512; i < buf.length; i++) {
74 this.encodeField('path') +
75 this.encodeField('ctime') +
76 this.encodeField('atime') +
77 this.encodeField('dev') +
78 this.encodeField('ino') +
79 this.encodeField('nlink') +
80 this.encodeField('charset') +
81 this.encodeField('comment') +
82 this.encodeField('gid') +
83 this.encodeField('gname') +
84 this.encodeField('linkpath') +
85 this.encodeField('mtime') +
86 this.encodeField('size') +
87 this.encodeField('uid') +
88 this.encodeField('uname')
93 if (this[field] === null || this[field] === undefined)
95 const v = this[field] instanceof Date ? this[field].getTime() / 1000
98 (field === 'dev' || field === 'ino' || field === 'nlink'
100 field + '=' + v + '\n'
101 const byteLen = Buffer.byteLength(s)
102 // the digits includes the length of the digits in ascii base-10
103 // so if it's 9 characters, then adding 1 for the 9 makes it 10
104 // which makes it 11 chars.
105 let digits = Math.floor(Math.log(byteLen) / Math.log(10)) + 1
106 if (byteLen + digits >= Math.pow(10, digits))
108 const len = digits + byteLen
113 Pax.parse = (string, ex, g) => new Pax(merge(parseKV(string), ex), g)
115 const merge = (a, b) =>
116 b ? Object.keys(a).reduce((s, k) => (s[k] = a[k], s), b) : a
118 const parseKV = string =>
122 .reduce(parseKVLine, Object.create(null))
124 const parseKVLine = (set, line) => {
125 const n = parseInt(line, 10)
127 // XXX Values with \n in them will fail this.
128 // Refactor to not be a naive line-by-line parse.
129 if (n !== Buffer.byteLength(line) + 1)
132 line = line.substr((n + ' ').length)
133 const kv = line.split('=')
134 const k = kv.shift().replace(/^SCHILY\.(dev|ino|nlink)/, '$1')
138 const v = kv.join('=')
139 set[k] = /^([A-Z]+\.)?([mac]|birth|creation)time$/.test(k)
141 : /^[0-9]+$/.test(v) ? +v