mirror of https://github.com/vasi/pixz
Move pread -> read
parent
f06306a5e7
commit
101cb11c18
@ -1,425 +0,0 @@
|
||||
#include "pixz.h"
|
||||
|
||||
#include <archive.h>
|
||||
#include <archive_entry.h>
|
||||
|
||||
#include <getopt.h>
|
||||
|
||||
/* TODO
|
||||
* - Replace 'read' with 'pread'
|
||||
* - Make a filter for tar
|
||||
*/
|
||||
|
||||
#define DEBUG 0
|
||||
#if DEBUG
|
||||
#define debug(str, ...) fprintf(stderr, str "\n", ##__VA_ARGS__)
|
||||
#else
|
||||
#define debug(...)
|
||||
#endif
|
||||
|
||||
|
||||
#pragma mark DECLARE WANTED
|
||||
|
||||
typedef struct wanted_t wanted_t;
|
||||
struct wanted_t {
|
||||
wanted_t *next;
|
||||
char *name;
|
||||
off_t start, end;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
static wanted_t *gWantedFiles = NULL;
|
||||
|
||||
static bool spec_match(char *spec, char *name);
|
||||
static void wanted_files(size_t count, char **specs);
|
||||
static void wanted_free(wanted_t *w);
|
||||
|
||||
|
||||
#pragma mark DECLARE PIPELINE
|
||||
|
||||
typedef struct {
|
||||
uint8_t *input, *output;
|
||||
size_t insize, outsize;
|
||||
off_t uoffset; // uncompressed offset
|
||||
} io_block_t;
|
||||
|
||||
static void *block_create(void);
|
||||
static void block_free(void *data);
|
||||
static void read_thread(void);
|
||||
static void decode_thread(size_t thnum);
|
||||
|
||||
|
||||
#pragma mark DECLARE ARCHIVE
|
||||
|
||||
static pipeline_item_t *gArItem = NULL, *gArLastItem = NULL;
|
||||
static off_t gArLastOffset;
|
||||
static size_t gArLastSize;
|
||||
static wanted_t *gArWanted = NULL;
|
||||
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);
|
||||
|
||||
|
||||
#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);
|
||||
|
||||
|
||||
#pragma mark MAIN
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
gInFile = stdin;
|
||||
gOutFile = stdout;
|
||||
bool verify = true;
|
||||
int ch;
|
||||
while ((ch = getopt(argc, argv, "i:o:v")) != -1) {
|
||||
switch (ch) {
|
||||
case 'i':
|
||||
if (!(gInFile = fopen(optarg, "r")))
|
||||
die ("Can't open input file");
|
||||
break;
|
||||
case 'o':
|
||||
if (!(gOutFile = fopen(optarg, "w")))
|
||||
die ("Can't open output file");
|
||||
break;
|
||||
case 'v': verify = false; break;
|
||||
default:
|
||||
die("Unknown option");
|
||||
}
|
||||
}
|
||||
|
||||
decode_index();
|
||||
if (verify)
|
||||
gFileIndexOffset = read_file_index(0);
|
||||
wanted_files(argc - optind, argv + optind);
|
||||
#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) {
|
||||
gArWanted = gWantedFiles;
|
||||
wanted_t *w = gWantedFiles, *wlast = NULL;
|
||||
bool lastmulti = false;
|
||||
off_t lastoff = 0;
|
||||
|
||||
struct archive *ar = archive_read_new();
|
||||
archive_read_support_compression_none(ar);
|
||||
archive_read_support_format_tar(ar);
|
||||
archive_read_open(ar, NULL, tar_ok, tar_read, tar_ok);
|
||||
struct archive_entry *entry;
|
||||
while (true) {
|
||||
int aerr = archive_read_next_header(ar, &entry);
|
||||
if (aerr == ARCHIVE_EOF) {
|
||||
break;
|
||||
} else if (aerr != ARCHIVE_OK && aerr != ARCHIVE_WARN) {
|
||||
fprintf(stderr, "%s\n", archive_error_string(ar));
|
||||
die("Error reading archive entry");
|
||||
}
|
||||
|
||||
off_t off = archive_read_header_position(ar);
|
||||
const char *path = archive_entry_pathname(entry);
|
||||
if (!lastmulti) {
|
||||
if (wlast && wlast->size != off - lastoff)
|
||||
die("Index and archive show differing sizes for %s: %d vs %d",
|
||||
wlast->name, wlast->size, off - lastoff);
|
||||
lastoff = off;
|
||||
}
|
||||
|
||||
lastmulti = is_multi_header(path);
|
||||
if (lastmulti)
|
||||
continue;
|
||||
|
||||
if (!w)
|
||||
die("File %s missing in index", path);
|
||||
if (strcmp(path, w->name) != 0)
|
||||
die("Index and archive differ as to next file: %s vs %s",
|
||||
w->name, path);
|
||||
|
||||
wlast = w;
|
||||
w = w->next;
|
||||
}
|
||||
if (w && w->name)
|
||||
die("File %s missing in archive", w->name);
|
||||
tar_read(NULL, NULL, NULL); // write whatever's left
|
||||
} else {
|
||||
pipeline_item_t *pi;
|
||||
while ((pi = pipeline_merged())) {
|
||||
io_block_t *ib = (io_block_t*)(pi->data);
|
||||
fwrite(ib->output, ib->outsize, 1, gOutFile);
|
||||
queue_push(gPipelineStartQ, PIPELINE_ITEM, pi);
|
||||
}
|
||||
}
|
||||
|
||||
pipeline_destroy();
|
||||
wanted_free(gWantedFiles);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
#pragma mark BLOCKS
|
||||
|
||||
static void *block_create(void) {
|
||||
io_block_t *ib = malloc(sizeof(io_block_t));
|
||||
ib->input = malloc(gBlockInSize);
|
||||
ib->output = malloc(gBlockOutSize);
|
||||
return ib;
|
||||
}
|
||||
|
||||
static void block_free(void* data) {
|
||||
io_block_t *ib = (io_block_t*)data;
|
||||
free(ib->input);
|
||||
free(ib->output);
|
||||
free(ib);
|
||||
}
|
||||
|
||||
|
||||
#pragma mark SETUP
|
||||
|
||||
static void set_block_sizes() {
|
||||
lzma_index_iter iter;
|
||||
lzma_index_iter_init(&iter, gIndex);
|
||||
while (!lzma_index_iter_next(&iter, LZMA_INDEX_ITER_BLOCK)) {
|
||||
// exclude the file index block
|
||||
lzma_vli off = iter.block.compressed_file_offset;
|
||||
if (gFileIndexOffset && off == gFileIndexOffset)
|
||||
continue;
|
||||
|
||||
size_t in = iter.block.total_size,
|
||||
out = iter.block.uncompressed_size;
|
||||
if (out > gBlockOutSize)
|
||||
gBlockOutSize = out;
|
||||
if (in > gBlockInSize)
|
||||
gBlockInSize = in;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void wanted_free(wanted_t *w) {
|
||||
for (wanted_t *w = gWantedFiles; w; ) {
|
||||
wanted_t *tmp = w->next;
|
||||
free(w);
|
||||
w = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
static bool spec_match(char *spec, char *name) {
|
||||
bool match = true;
|
||||
for (; *spec; ++spec, ++name) {
|
||||
if (!*name || *spec != *name) { // spec must be equal or prefix
|
||||
match = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// If spec's a prefix of the file name, it must be a dir name
|
||||
return match && (!*name || *name == '/');
|
||||
}
|
||||
|
||||
static void wanted_files(size_t count, char **specs) {
|
||||
if (!gFileIndexOffset) {
|
||||
if (count)
|
||||
die("Can't filter non-tarball");
|
||||
gWantedFiles = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
// Remove trailing slashes from specs
|
||||
for (char **spec = specs; spec < specs + count; ++spec) {
|
||||
char *c = *spec;
|
||||
while (*c++) ; // forward to end
|
||||
while (--c >= *spec && *c == '/')
|
||||
*c = '\0';
|
||||
}
|
||||
|
||||
bool matched[count]; // for each spec, does it match?
|
||||
memset(matched, 0, sizeof(matched));
|
||||
wanted_t *last = NULL;
|
||||
|
||||
// Check each file in order, to see if we want it
|
||||
for (file_index_t *f = gFileIndex; f->name; f = f->next) {
|
||||
bool match = !count;
|
||||
for (char **spec = specs; spec < specs + count; ++spec) {
|
||||
if (spec_match(*spec, f->name)) {
|
||||
match = true;
|
||||
matched[spec - specs] = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (match) {
|
||||
wanted_t *w = malloc(sizeof(wanted_t));
|
||||
*w = (wanted_t){ .name = f->name, .start = f->offset,
|
||||
.end = f->next->offset, .next = NULL };
|
||||
w->size = w->end - w->start;
|
||||
if (last) {
|
||||
last->next = w;
|
||||
} else {
|
||||
gWantedFiles = w;
|
||||
}
|
||||
last = w;
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure each spec matched
|
||||
for (size_t i = 0; i < count; ++i) {
|
||||
if (!matched[i])
|
||||
die("\"%s\" not found in archive", *(specs + i));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#pragma mark THREADS
|
||||
|
||||
static void read_thread(void) {
|
||||
off_t offset = ftello(gInFile);
|
||||
wanted_t *w = gWantedFiles;
|
||||
|
||||
lzma_index_iter iter;
|
||||
lzma_index_iter_init(&iter, gIndex);
|
||||
while (!lzma_index_iter_next(&iter, LZMA_INDEX_ITER_BLOCK)) {
|
||||
// Don't decode the file-index
|
||||
off_t boffset = iter.block.compressed_file_offset;
|
||||
size_t bsize = iter.block.total_size;
|
||||
if (gFileIndexOffset && boffset == gFileIndexOffset)
|
||||
continue;
|
||||
|
||||
// Do we need this block?
|
||||
if (gWantedFiles) {
|
||||
off_t uend = iter.block.uncompressed_file_offset +
|
||||
iter.block.uncompressed_size;
|
||||
if (!w || w->start >= uend) {
|
||||
debug("read: skip %llu", iter.block.number_in_file);
|
||||
continue;
|
||||
}
|
||||
for ( ; w && w->end < uend; w = w->next) ;
|
||||
}
|
||||
debug("read: want %llu", iter.block.number_in_file);
|
||||
|
||||
// Get a block to work with
|
||||
pipeline_item_t *pi;
|
||||
queue_pop(gPipelineStartQ, (void**)&pi);
|
||||
io_block_t *ib = (io_block_t*)(pi->data);
|
||||
|
||||
// Seek if needed, and get the data
|
||||
if (offset != boffset) {
|
||||
fseeko(gInFile, boffset, SEEK_SET);
|
||||
offset = boffset;
|
||||
}
|
||||
ib->insize = fread(ib->input, 1, bsize, gInFile);
|
||||
if (ib->insize < bsize)
|
||||
die("Error reading block contents");
|
||||
offset += bsize;
|
||||
ib->uoffset = iter.block.uncompressed_file_offset;
|
||||
|
||||
pipeline_split(pi);
|
||||
}
|
||||
|
||||
pipeline_stop();
|
||||
}
|
||||
|
||||
static void decode_thread(size_t thnum) {
|
||||
lzma_stream stream = LZMA_STREAM_INIT;
|
||||
lzma_filter filters[LZMA_FILTERS_MAX + 1];
|
||||
lzma_block block = { .filters = filters, .check = gCheck, .version = 0 };
|
||||
|
||||
pipeline_item_t *pi;
|
||||
io_block_t *ib;
|
||||
|
||||
while (PIPELINE_STOP != queue_pop(gPipelineSplitQ, (void**)&pi)) {
|
||||
ib = (io_block_t*)(pi->data);
|
||||
|
||||
block.header_size = lzma_block_header_size_decode(*(ib->input));
|
||||
if (lzma_block_header_decode(&block, NULL, ib->input) != LZMA_OK)
|
||||
die("Error decoding block header");
|
||||
if (lzma_block_decoder(&stream, &block) != LZMA_OK)
|
||||
die("Error initializing block decode");
|
||||
|
||||
stream.avail_in = ib->insize - block.header_size;
|
||||
stream.next_in = ib->input + block.header_size;
|
||||
stream.avail_out = gBlockOutSize;
|
||||
stream.next_out = ib->output;
|
||||
|
||||
lzma_ret err = LZMA_OK;
|
||||
while (err != LZMA_STREAM_END) {
|
||||
if (err != LZMA_OK)
|
||||
die("Error decoding block");
|
||||
err = lzma_code(&stream, LZMA_FINISH);
|
||||
}
|
||||
|
||||
ib->outsize = stream.next_out - ib->output;
|
||||
queue_push(gPipelineMergeQ, PIPELINE_ITEM, pi);
|
||||
}
|
||||
lzma_end(&stream);
|
||||
}
|
||||
|
||||
|
||||
#pragma mark ARCHIVE
|
||||
|
||||
static int tar_ok(struct archive *ar, void *ref) {
|
||||
return ARCHIVE_OK;
|
||||
}
|
||||
|
||||
static bool tar_next_block(void) {
|
||||
if (gArItem && !gArNextItem && gArWanted) {
|
||||
io_block_t *ib = (io_block_t*)(gArItem->data);
|
||||
if (gArWanted->start < ib->uoffset + ib->outsize)
|
||||
return true; // No need
|
||||
}
|
||||
|
||||
if (gArLastItem)
|
||||
queue_push(gPipelineStartQ, PIPELINE_ITEM, gArLastItem);
|
||||
gArLastItem = gArItem;
|
||||
gArItem = pipeline_merged();
|
||||
gArNextItem = false;
|
||||
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
|
||||
if (gArItem) {
|
||||
io_block_t *ib = (io_block_t*)(gArItem->data);
|
||||
fwrite(ib->output + gArLastOffset, gArLastSize, 1, gOutFile);
|
||||
gArLastSize = 0;
|
||||
}
|
||||
|
||||
// Write the first wanted file
|
||||
if (!tar_next_block())
|
||||
return 0;
|
||||
|
||||
off_t off;
|
||||
size_t size;
|
||||
io_block_t *ib = (io_block_t*)(gArItem->data);
|
||||
if (gWantedFiles) {
|
||||
debug("tar want: %s", gArWanted->name);
|
||||
off = gArWanted->start - ib->uoffset;
|
||||
size = gArWanted->size;
|
||||
if (off < 0) {
|
||||
size += off;
|
||||
off = 0;
|
||||
}
|
||||
if (off + size >= ib->outsize) {
|
||||
size = ib->outsize - off;
|
||||
gArNextItem = true; // force the end of this block
|
||||
} else {
|
||||
gArWanted = gArWanted->next;
|
||||
}
|
||||
} else {
|
||||
off = 0;
|
||||
size = ib->outsize;
|
||||
}
|
||||
debug("tar off = %zu, size = %zu", off, size);
|
||||
|
||||
gArLastOffset = off;
|
||||
gArLastSize = size;
|
||||
if (bufp)
|
||||
*bufp = ib->output + off;
|
||||
return size;
|
||||
}
|
@ -1,104 +1,425 @@
|
||||
#include "pixz.h"
|
||||
|
||||
#include <archive.h>
|
||||
#include <archive_entry.h>
|
||||
|
||||
#pragma mark FUNCTION DECLARATIONS
|
||||
#include <getopt.h>
|
||||
|
||||
static void extract_file(const char *target);
|
||||
static void extract_block(off_t block_seek, off_t skip, off_t size);
|
||||
/* TODO
|
||||
* - Replace 'read' with 'pread'
|
||||
* - Make a filter for tar
|
||||
*/
|
||||
|
||||
#define DEBUG 0
|
||||
#if DEBUG
|
||||
#define debug(str, ...) fprintf(stderr, str "\n", ##__VA_ARGS__)
|
||||
#else
|
||||
#define debug(...)
|
||||
#endif
|
||||
|
||||
#pragma mark FUNCTION DEFINITIONS
|
||||
|
||||
#pragma mark DECLARE WANTED
|
||||
|
||||
typedef struct wanted_t wanted_t;
|
||||
struct wanted_t {
|
||||
wanted_t *next;
|
||||
char *name;
|
||||
off_t start, end;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
static wanted_t *gWantedFiles = NULL;
|
||||
|
||||
static bool spec_match(char *spec, char *name);
|
||||
static void wanted_files(size_t count, char **specs);
|
||||
static void wanted_free(wanted_t *w);
|
||||
|
||||
|
||||
#pragma mark DECLARE PIPELINE
|
||||
|
||||
typedef struct {
|
||||
uint8_t *input, *output;
|
||||
size_t insize, outsize;
|
||||
off_t uoffset; // uncompressed offset
|
||||
} io_block_t;
|
||||
|
||||
static void *block_create(void);
|
||||
static void block_free(void *data);
|
||||
static void read_thread(void);
|
||||
static void decode_thread(size_t thnum);
|
||||
|
||||
|
||||
#pragma mark DECLARE ARCHIVE
|
||||
|
||||
static pipeline_item_t *gArItem = NULL, *gArLastItem = NULL;
|
||||
static off_t gArLastOffset;
|
||||
static size_t gArLastSize;
|
||||
static wanted_t *gArWanted = NULL;
|
||||
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);
|
||||
|
||||
|
||||
#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);
|
||||
|
||||
|
||||
#pragma mark MAIN
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc != 3)
|
||||
die("Need two arguments");
|
||||
if (!(gInFile = fopen(argv[1], "r")))
|
||||
die("Can't open input file");
|
||||
char *target = argv[2];
|
||||
gInFile = stdin;
|
||||
gOutFile = stdout;
|
||||
bool verify = true;
|
||||
int ch;
|
||||
while ((ch = getopt(argc, argv, "i:o:v")) != -1) {
|
||||
switch (ch) {
|
||||
case 'i':
|
||||
if (!(gInFile = fopen(optarg, "r")))
|
||||
die ("Can't open input file");
|
||||
break;
|
||||
case 'o':
|
||||
if (!(gOutFile = fopen(optarg, "w")))
|
||||
die ("Can't open output file");
|
||||
break;
|
||||
case 'v': verify = false; break;
|
||||
default:
|
||||
die("Unknown option");
|
||||
}
|
||||
}
|
||||
|
||||
if (!read_file_index(0))
|
||||
die("File has no index");
|
||||
decode_index();
|
||||
if (verify)
|
||||
gFileIndexOffset = read_file_index(0);
|
||||
wanted_files(argc - optind, argv + optind);
|
||||
#if DEBUG
|
||||
for (wanted_t *w = gWantedFiles; w; w = w->next)
|
||||
debug("want: %s", w->name);
|
||||
#endif
|
||||
set_block_sizes();
|
||||
|
||||
extract_file(target);
|
||||
pipeline_create(block_create, block_free, read_thread, decode_thread);
|
||||
if (verify && gFileIndexOffset) {
|
||||
gArWanted = gWantedFiles;
|
||||
wanted_t *w = gWantedFiles, *wlast = NULL;
|
||||
bool lastmulti = false;
|
||||
off_t lastoff = 0;
|
||||
|
||||
struct archive *ar = archive_read_new();
|
||||
archive_read_support_compression_none(ar);
|
||||
archive_read_support_format_tar(ar);
|
||||
archive_read_open(ar, NULL, tar_ok, tar_read, tar_ok);
|
||||
struct archive_entry *entry;
|
||||
while (true) {
|
||||
int aerr = archive_read_next_header(ar, &entry);
|
||||
if (aerr == ARCHIVE_EOF) {
|
||||
break;
|
||||
} else if (aerr != ARCHIVE_OK && aerr != ARCHIVE_WARN) {
|
||||
fprintf(stderr, "%s\n", archive_error_string(ar));
|
||||
die("Error reading archive entry");
|
||||
}
|
||||
|
||||
off_t off = archive_read_header_position(ar);
|
||||
const char *path = archive_entry_pathname(entry);
|
||||
if (!lastmulti) {
|
||||
if (wlast && wlast->size != off - lastoff)
|
||||
die("Index and archive show differing sizes for %s: %d vs %d",
|
||||
wlast->name, wlast->size, off - lastoff);
|
||||
lastoff = off;
|
||||
}
|
||||
|
||||
lastmulti = is_multi_header(path);
|
||||
if (lastmulti)
|
||||
continue;
|
||||
|
||||
if (!w)
|
||||
die("File %s missing in index", path);
|
||||
if (strcmp(path, w->name) != 0)
|
||||
die("Index and archive differ as to next file: %s vs %s",
|
||||
w->name, path);
|
||||
|
||||
wlast = w;
|
||||
w = w->next;
|
||||
}
|
||||
if (w && w->name)
|
||||
die("File %s missing in archive", w->name);
|
||||
tar_read(NULL, NULL, NULL); // write whatever's left
|
||||
} else {
|
||||
pipeline_item_t *pi;
|
||||
while ((pi = pipeline_merged())) {
|
||||
io_block_t *ib = (io_block_t*)(pi->data);
|
||||
fwrite(ib->output, ib->outsize, 1, gOutFile);
|
||||
queue_push(gPipelineStartQ, PIPELINE_ITEM, pi);
|
||||
}
|
||||
}
|
||||
|
||||
free_file_index();
|
||||
lzma_index_end(gIndex, NULL);
|
||||
pipeline_destroy();
|
||||
wanted_free(gWantedFiles);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void extract_file(const char *target) {
|
||||
// find it in the index
|
||||
file_index_t *f;
|
||||
for (f = gFileIndex; f != NULL; f = f->next) {
|
||||
if (f->name && strcmp(f->name, target) == 0)
|
||||
|
||||
#pragma mark BLOCKS
|
||||
|
||||
static void *block_create(void) {
|
||||
io_block_t *ib = malloc(sizeof(io_block_t));
|
||||
ib->input = malloc(gBlockInSize);
|
||||
ib->output = malloc(gBlockOutSize);
|
||||
return ib;
|
||||
}
|
||||
|
||||
static void block_free(void* data) {
|
||||
io_block_t *ib = (io_block_t*)data;
|
||||
free(ib->input);
|
||||
free(ib->output);
|
||||
free(ib);
|
||||
}
|
||||
|
||||
|
||||
#pragma mark SETUP
|
||||
|
||||
static void set_block_sizes() {
|
||||
lzma_index_iter iter;
|
||||
lzma_index_iter_init(&iter, gIndex);
|
||||
while (!lzma_index_iter_next(&iter, LZMA_INDEX_ITER_BLOCK)) {
|
||||
// exclude the file index block
|
||||
lzma_vli off = iter.block.compressed_file_offset;
|
||||
if (gFileIndexOffset && off == gFileIndexOffset)
|
||||
continue;
|
||||
|
||||
size_t in = iter.block.total_size,
|
||||
out = iter.block.uncompressed_size;
|
||||
if (out > gBlockOutSize)
|
||||
gBlockOutSize = out;
|
||||
if (in > gBlockInSize)
|
||||
gBlockInSize = in;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void wanted_free(wanted_t *w) {
|
||||
for (wanted_t *w = gWantedFiles; w; ) {
|
||||
wanted_t *tmp = w->next;
|
||||
free(w);
|
||||
w = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
static bool spec_match(char *spec, char *name) {
|
||||
bool match = true;
|
||||
for (; *spec; ++spec, ++name) {
|
||||
if (!*name || *spec != *name) { // spec must be equal or prefix
|
||||
match = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// If spec's a prefix of the file name, it must be a dir name
|
||||
return match && (!*name || *name == '/');
|
||||
}
|
||||
|
||||
static void wanted_files(size_t count, char **specs) {
|
||||
if (!gFileIndexOffset) {
|
||||
if (count)
|
||||
die("Can't filter non-tarball");
|
||||
gWantedFiles = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
// Remove trailing slashes from specs
|
||||
for (char **spec = specs; spec < specs + count; ++spec) {
|
||||
char *c = *spec;
|
||||
while (*c++) ; // forward to end
|
||||
while (--c >= *spec && *c == '/')
|
||||
*c = '\0';
|
||||
}
|
||||
|
||||
bool matched[count]; // for each spec, does it match?
|
||||
memset(matched, 0, sizeof(matched));
|
||||
wanted_t *last = NULL;
|
||||
|
||||
// Check each file in order, to see if we want it
|
||||
for (file_index_t *f = gFileIndex; f->name; f = f->next) {
|
||||
bool match = !count;
|
||||
for (char **spec = specs; spec < specs + count; ++spec) {
|
||||
if (spec_match(*spec, f->name)) {
|
||||
match = true;
|
||||
matched[spec - specs] = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (match) {
|
||||
wanted_t *w = malloc(sizeof(wanted_t));
|
||||
*w = (wanted_t){ .name = f->name, .start = f->offset,
|
||||
.end = f->next->offset, .next = NULL };
|
||||
w->size = w->end - w->start;
|
||||
if (last) {
|
||||
last->next = w;
|
||||
} else {
|
||||
gWantedFiles = w;
|
||||
}
|
||||
last = w;
|
||||
}
|
||||
}
|
||||
if (!f)
|
||||
die("Can't find target file");
|
||||
off_t fstart = f->offset, fsize = f->next->offset - fstart;
|
||||
|
||||
// extract the data
|
||||
// Make sure each spec matched
|
||||
for (size_t i = 0; i < count; ++i) {
|
||||
if (!matched[i])
|
||||
die("\"%s\" not found in archive", *(specs + i));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#pragma mark THREADS
|
||||
|
||||
static void read_thread(void) {
|
||||
off_t offset = ftello(gInFile);
|
||||
wanted_t *w = gWantedFiles;
|
||||
|
||||
lzma_index_iter iter;
|
||||
lzma_index_iter_init(&iter, gIndex);
|
||||
if (lzma_index_iter_locate(&iter, fstart))
|
||||
die("Block with file contents can't be found");
|
||||
do {
|
||||
off_t bstart = iter.block.uncompressed_file_offset,
|
||||
bsize = iter.block.uncompressed_size;
|
||||
off_t dstart = fstart > bstart ? fstart - bstart : 0;
|
||||
bsize -= dstart;
|
||||
off_t dsize = fsize > bsize ? bsize : fsize;
|
||||
fsize -= dsize;
|
||||
while (!lzma_index_iter_next(&iter, LZMA_INDEX_ITER_BLOCK)) {
|
||||
// Don't decode the file-index
|
||||
off_t boffset = iter.block.compressed_file_offset;
|
||||
size_t bsize = iter.block.total_size;
|
||||
if (gFileIndexOffset && boffset == gFileIndexOffset)
|
||||
continue;
|
||||
|
||||
// Do we need this block?
|
||||
if (gWantedFiles) {
|
||||
off_t uend = iter.block.uncompressed_file_offset +
|
||||
iter.block.uncompressed_size;
|
||||
if (!w || w->start >= uend) {
|
||||
debug("read: skip %llu", iter.block.number_in_file);
|
||||
continue;
|
||||
}
|
||||
for ( ; w && w->end < uend; w = w->next) ;
|
||||
}
|
||||
debug("read: want %llu", iter.block.number_in_file);
|
||||
|
||||
// Get a block to work with
|
||||
pipeline_item_t *pi;
|
||||
queue_pop(gPipelineStartQ, (void**)&pi);
|
||||
io_block_t *ib = (io_block_t*)(pi->data);
|
||||
|
||||
extract_block(iter.block.compressed_file_offset, dstart, dsize);
|
||||
} while (fsize && !lzma_index_iter_next(&iter, LZMA_INDEX_ITER_BLOCK));
|
||||
if (fsize)
|
||||
die("Block with file contents missing");
|
||||
// Seek if needed, and get the data
|
||||
if (offset != boffset) {
|
||||
fseeko(gInFile, boffset, SEEK_SET);
|
||||
offset = boffset;
|
||||
}
|
||||
ib->insize = fread(ib->input, 1, bsize, gInFile);
|
||||
if (ib->insize < bsize)
|
||||
die("Error reading block contents");
|
||||
offset += bsize;
|
||||
ib->uoffset = iter.block.uncompressed_file_offset;
|
||||
|
||||
pipeline_split(pi);
|
||||
}
|
||||
|
||||
pipeline_stop();
|
||||
}
|
||||
|
||||
static void extract_block(off_t block_seek, off_t skip, off_t size) {
|
||||
void *bdata = decode_block_start(block_seek);
|
||||
static void decode_thread(size_t thnum) {
|
||||
lzma_stream stream = LZMA_STREAM_INIT;
|
||||
lzma_filter filters[LZMA_FILTERS_MAX + 1];
|
||||
lzma_block block = { .filters = filters, .check = gCheck, .version = 0 };
|
||||
|
||||
uint8_t ibuf[CHUNKSIZE], obuf[CHUNKSIZE];
|
||||
gStream.avail_in = 0;
|
||||
lzma_ret err = LZMA_OK;
|
||||
while (size && err != LZMA_STREAM_END) {
|
||||
gStream.next_out = obuf;
|
||||
gStream.avail_out = CHUNKSIZE;
|
||||
pipeline_item_t *pi;
|
||||
io_block_t *ib;
|
||||
|
||||
while (PIPELINE_STOP != queue_pop(gPipelineSplitQ, (void**)&pi)) {
|
||||
ib = (io_block_t*)(pi->data);
|
||||
|
||||
if (gStream.avail_in == 0) {
|
||||
gStream.avail_in = fread(ibuf, 1, CHUNKSIZE, gInFile);
|
||||
if (ferror(gInFile))
|
||||
die("Error reading block data");
|
||||
gStream.next_in = ibuf;
|
||||
}
|
||||
block.header_size = lzma_block_header_size_decode(*(ib->input));
|
||||
if (lzma_block_header_decode(&block, NULL, ib->input) != LZMA_OK)
|
||||
die("Error decoding block header");
|
||||
if (lzma_block_decoder(&stream, &block) != LZMA_OK)
|
||||
die("Error initializing block decode");
|
||||
|
||||
err = lzma_code(&gStream, LZMA_RUN);
|
||||
if (err != LZMA_OK && err != LZMA_STREAM_END)
|
||||
die("Error decoding block");
|
||||
stream.avail_in = ib->insize - block.header_size;
|
||||
stream.next_in = ib->input + block.header_size;
|
||||
stream.avail_out = gBlockOutSize;
|
||||
stream.next_out = ib->output;
|
||||
|
||||
// do we want to write?
|
||||
uint8_t *start = obuf;
|
||||
size_t out = gStream.next_out - obuf;
|
||||
if (out <= skip) {
|
||||
skip -= out;
|
||||
continue;
|
||||
lzma_ret err = LZMA_OK;
|
||||
while (err != LZMA_STREAM_END) {
|
||||
if (err != LZMA_OK)
|
||||
die("Error decoding block");
|
||||
err = lzma_code(&stream, LZMA_FINISH);
|
||||
}
|
||||
|
||||
// what do we want to write?
|
||||
start += skip;
|
||||
out -= skip;
|
||||
skip = 0;
|
||||
if (out > size)
|
||||
out = size;
|
||||
ib->outsize = stream.next_out - ib->output;
|
||||
queue_push(gPipelineMergeQ, PIPELINE_ITEM, pi);
|
||||
}
|
||||
lzma_end(&stream);
|
||||
}
|
||||
|
||||
|
||||
#pragma mark ARCHIVE
|
||||
|
||||
static int tar_ok(struct archive *ar, void *ref) {
|
||||
return ARCHIVE_OK;
|
||||
}
|
||||
|
||||
static bool tar_next_block(void) {
|
||||
if (gArItem && !gArNextItem && gArWanted) {
|
||||
io_block_t *ib = (io_block_t*)(gArItem->data);
|
||||
if (gArWanted->start < ib->uoffset + ib->outsize)
|
||||
return true; // No need
|
||||
}
|
||||
|
||||
if (gArLastItem)
|
||||
queue_push(gPipelineStartQ, PIPELINE_ITEM, gArLastItem);
|
||||
gArLastItem = gArItem;
|
||||
gArItem = pipeline_merged();
|
||||
gArNextItem = false;
|
||||
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
|
||||
if (gArItem) {
|
||||
io_block_t *ib = (io_block_t*)(gArItem->data);
|
||||
fwrite(ib->output + gArLastOffset, gArLastSize, 1, gOutFile);
|
||||
gArLastSize = 0;
|
||||
}
|
||||
|
||||
if (fwrite(start, out, 1, stdout) != 1)
|
||||
die("Error writing output");
|
||||
size -= out;
|
||||
// Write the first wanted file
|
||||
if (!tar_next_block())
|
||||
return 0;
|
||||
|
||||
off_t off;
|
||||
size_t size;
|
||||
io_block_t *ib = (io_block_t*)(gArItem->data);
|
||||
if (gWantedFiles) {
|
||||
debug("tar want: %s", gArWanted->name);
|
||||
off = gArWanted->start - ib->uoffset;
|
||||
size = gArWanted->size;
|
||||
if (off < 0) {
|
||||
size += off;
|
||||
off = 0;
|
||||
}
|
||||
if (off + size >= ib->outsize) {
|
||||
size = ib->outsize - off;
|
||||
gArNextItem = true; // force the end of this block
|
||||
} else {
|
||||
gArWanted = gArWanted->next;
|
||||
}
|
||||
} else {
|
||||
off = 0;
|
||||
size = ib->outsize;
|
||||
}
|
||||
if (size)
|
||||
die("Block data missing");
|
||||
debug("tar off = %zu, size = %zu", off, size);
|
||||
|
||||
lzma_end(&gStream);
|
||||
free(bdata);
|
||||
gArLastOffset = off;
|
||||
gArLastSize = size;
|
||||
if (bufp)
|
||||
*bufp = ib->output + off;
|
||||
return size;
|
||||
}
|
||||
|
Loading…
Reference in New Issue