Skip to content

Commit

Permalink
fix writing massive entries
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Jun 17, 2023
1 parent 7525b47 commit 91a5313
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
18 changes: 17 additions & 1 deletion headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ exports.encode = function encode (opts) {
b4a.write(buf, encodeOct(opts.mode & MASK, 6), 100)
b4a.write(buf, encodeOct(opts.uid, 6), 108)
b4a.write(buf, encodeOct(opts.gid, 6), 116)
b4a.write(buf, encodeOct(opts.size, 11), 124)
encodeSize(opts.size, buf, 124)
b4a.write(buf, encodeOct((opts.mtime.getTime() / 1000) | 0, 11), 136)

buf[156] = ZERO_OFFSET + toTypeflag(opts.type)
Expand Down Expand Up @@ -244,6 +244,22 @@ function encodeOct (val, n) {
return ZEROS.slice(0, n - val.length) + val + ' '
}

function encodeSizeBin (num, buf, off) {
buf[off] = 0x80
for (let i = 11; i > 0; i--) {
buf[off + i] = num & 0xff
num = Math.floor(num / 0x100)
}
}

function encodeSize (num, buf, off) {
if (num.toString(8).length > 11) {
encodeSizeBin(num, buf, off)
} else {
b4a.write(buf, encodeOct(num, 11), off)
}
}

/* Copied from the node-tar repo and modified to meet
* tar-stream coding standard.
*
Expand Down
48 changes: 48 additions & 0 deletions test/dual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const test = require('brittle')
const { Readable } = require('streamx')
const tar = require('../')

test('write and read huge archive', function (t) {
t.plan(2)

const pack = tar.pack()
const extract = tar.extract()

extract.on('entry', function (header, stream, next) {
let size = 0

stream.on('data', function (data) {
size += data.byteLength
})

stream.on('end', function () {
t.is(size, header.size)
next()
})
})

pack.pipe(extract, function (err) {
t.ok(!err, 'pipeline finished')
})

const entry = pack.entry({
name: 'huge.txt',
size: 10 * 1024 * 1024 * 1024
})

const buf = Buffer.alloc(1024 * 1024)

let pushed = 0

const rs = new Readable({
read (cb) {
this.push(buf)
pushed += buf.byteLength
if (pushed === entry.header.size) this.push(null)
cb(null)
}
})

rs.pipe(entry)
pack.finalize()
})
3 changes: 2 additions & 1 deletion test/slow/huge.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const tar = require('../..')
const fixtures = require('../fixtures')

test('huge', function (t) {
t.plan(1)
t.plan(3)

const extract = tar.extract()
let noEntries = false
Expand Down Expand Up @@ -48,6 +48,7 @@ test('huge', function (t) {

noEntries = true
stream.pipe(countStream)
callback()
})

extract.on('finish', function () {
Expand Down

0 comments on commit 91a5313

Please sign in to comment.