From cfa025697d31974bcbc3afb7e2fdc34be64e820d Mon Sep 17 00:00:00 2001 From: Dave Vasilevsky Date: Thu, 14 Oct 2010 00:39:43 -0400 Subject: [PATCH] Fix writing last file; Make pixz_read into a function --- pixz.h | 2 +- read.c | 28 ++++++++++++++++------------ write.c | 1 - 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/pixz.h b/pixz.h index 4c52792..630cbfd 100644 --- a/pixz.h +++ b/pixz.h @@ -23,7 +23,7 @@ #pragma mark UTILS -FILE *gInFile; +FILE *gInFile, *gOutFile; lzma_stream gStream; extern lzma_index *gIndex; diff --git a/read.c b/read.c index a86804d..7331d3e 100644 --- a/read.c +++ b/read.c @@ -5,11 +5,6 @@ #include -/* TODO - * - Replace 'read' with 'pread' - * - Make a filter for tar - */ - #define DEBUG 0 #if DEBUG #define debug(str, ...) fprintf(stderr, str "\n", ##__VA_ARGS__) @@ -60,15 +55,16 @@ static bool gArNextItem = false; static int tar_ok(struct archive *ar, void *ref); static ssize_t tar_read(struct archive *ar, void *ref, const void **bufp); static bool tar_next_block(void); +static void tar_write_last(void); #pragma mark DECLARE UTILS -static FILE *gOutFile; static lzma_vli gFileIndexOffset = 0; static size_t gBlockInSize = 0, gBlockOutSize = 0; static void set_block_sizes(void); +static void pixz_read(bool verify, size_t nspecs, char **specs); #pragma mark MAIN @@ -93,16 +89,21 @@ int main(int argc, char **argv) { die("Unknown option"); } } + pixz_read(verify, argc - optind, argv + optind); + return 0; +} +static void pixz_read(bool verify, size_t nspecs, char **specs) { decode_index(); if (verify) gFileIndexOffset = read_file_index(0); - wanted_files(argc - optind, argv + optind); + wanted_files(nspecs, specs); + set_block_sizes(); + #if DEBUG for (wanted_t *w = gWantedFiles; w; w = w->next) debug("want: %s", w->name); #endif - set_block_sizes(); pipeline_create(block_create, block_free, read_thread, decode_thread); if (verify && gFileIndexOffset) { @@ -149,7 +150,7 @@ int main(int argc, char **argv) { } if (w && w->name) die("File %s missing in archive", w->name); - tar_read(NULL, NULL, NULL); // write whatever's left + tar_write_last(); // write whatever's left } else { pipeline_item_t *pi; while ((pi = pipeline_merged())) { @@ -161,7 +162,6 @@ int main(int argc, char **argv) { pipeline_destroy(); wanted_free(gWantedFiles); - return 0; } @@ -382,13 +382,17 @@ static bool tar_next_block(void) { return gArItem; } -static ssize_t tar_read(struct archive *ar, void *ref, const void **bufp) { - // If we got here, the last bit of archive is ok to write +static void tar_write_last(void) { if (gArItem) { io_block_t *ib = (io_block_t*)(gArItem->data); fwrite(ib->output + gArLastOffset, gArLastSize, 1, gOutFile); gArLastSize = 0; } +} + +static ssize_t tar_read(struct archive *ar, void *ref, const void **bufp) { + // If we got here, the last bit of archive is ok to write + tar_write_last(); // Write the first wanted file if (!tar_next_block()) diff --git a/write.c b/write.c index 7409f2d..7e3ce77 100644 --- a/write.c +++ b/write.c @@ -34,7 +34,6 @@ static size_t gReadItemCount = 0; static lzma_filter gFilters[LZMA_FILTERS_MAX + 1]; -static FILE *gOutFile = NULL; static uint8_t gFileIndexBuf[CHUNKSIZE]; static size_t gFileIndexBufPos = 0;