From 16f0d6e2edea8cb670fd45d71cbbecef18bb803e Mon Sep 17 00:00:00 2001 From: Dave Vasilevsky Date: Sun, 10 Oct 2010 20:37:14 -0400 Subject: [PATCH] streaming writes --- write.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/write.c b/write.c index 6c64869..e7e2c36 100644 --- a/write.c +++ b/write.c @@ -294,6 +294,7 @@ static void block_queue_free(int type, void *p) { static void *encode_thread(void *arg) { int __attribute__((unused)) thnum = (uintptr_t)arg; + lzma_stream stream = LZMA_STREAM_INIT; while (true) { io_block_t *ib; @@ -302,16 +303,32 @@ static void *encode_thread(void *arg) { break; debug("encoder %d: received %zu", thnum, ib->seq); + block_init(&ib->block); - ib->outsize = 0; - if (lzma_block_buffer_encode(&ib->block, NULL, ib->input, ib->insize, - ib->output, &ib->outsize, gBlockOutSize) != LZMA_OK) - die("Error encoding block"); + if (lzma_block_header_encode(&ib->block, ib->output) != LZMA_OK) + die("Error encoding block header"); + ib->outsize = ib->block.header_size; + + if (lzma_block_encoder(&stream, &ib->block) != LZMA_OK) + die("Error creating block encoder"); + stream.next_in = ib->input; + stream.avail_in = ib->insize; + stream.next_out = ib->output + ib->outsize; + stream.avail_out = gBlockOutSize - ib->outsize; + + lzma_ret err = LZMA_OK; + while (err != LZMA_STREAM_END) { + err = lzma_code(&stream, LZMA_FINISH); + if (err != LZMA_OK && err != LZMA_STREAM_END) + die("Error encoding block"); + } + ib->outsize = stream.next_out - ib->output; debug("encoder %d: sending %zu", thnum, ib->seq); queue_push(gWriteQ, MSG_BLOCK, ib); } + lzma_end(&stream); return NULL; }