Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function appendMeta (fd, pos, meta, cb) {
var data = Buffer.from(JSON.stringify(meta), 'utf8')
var header = tarHeader.encode({
name: '___index.json',
type: 'file',
mode: parseInt('644', 8),
uid: 0,
gid: 0,
mtime: new Date(),
size: data.length
})
// leftover bytes to reach 512 block boundary, plus another 512 * 2 = 1024 to mark the end-of-file
var padding = Buffer.alloc(512 - (data.length % 512) + 512 + 512).fill(0)
var buf = Buffer.concat([header, data, padding])
fs.write(fd, buf, 0, buf.length, pos, cb)
}
function write (archive, start) {
// 3. Prepare the tar archive for appending.
var fsOpts = {
flags: 'r+',
start: start !== undefined ? start : 0
}
if (fsOpts.start < 0) fsOpts.start = 0
var appendStream = fs.createWriteStream(self.filepath, fsOpts)
// 4. Write tar header, without size info (yet).
var header = tarHeader.encode({
name: filepath,
type: 'file',
mode: parseInt('644', 8),
uid: 0,
gid: 0,
mtime: new Date(),
size: 0
})
appendStream.write(header)
// 5. Write data.
t.pipe(appendStream)
t.on('end', function () {
// 6. Pad the remaining bytes to fit a 512-byte block.
var leftover = 512 - (size % 512)
if (leftover === 512) leftover = 0
function writeIndex (fd, offset, index, cb) {
var meta = { index: index }
try {
var json = Buffer.from(JSON.stringify(meta), 'utf8')
var header = tarHeader.encode({
name: '___index.json',
type: 'file',
mode: parseInt('644', 8),
uid: 0,
gid: 0,
mtime: new Date(),
size: json.length
})
var leftover = json.length % 512 === 0 ? 0 : 512 - json.length % 512
var padding = Buffer.alloc(leftover).fill(0)
var finalBuf = Buffer.concat([header, json, padding])
fs.write(fd, finalBuf, 0, finalBuf.length, offset, function (err, bytesWritten) {
if (err) cb(err)
else if (bytesWritten < finalBuf.length) cb(new Error('failed to fully write index'))
else cb(null, bytesWritten)
})