2
0
mirror of https://github.com/vasi/pixz synced 2024-11-18 15:26:46 +00:00

Create wanted list even if no specs given

This commit is contained in:
Dave Vasilevsky 2010-10-13 22:37:56 -04:00
parent 5850a10301
commit bd678c0924

42
pread.c
View File

@ -96,7 +96,6 @@ static ssize_t tar_read(struct archive *ar, void *ref, const void **bufp) {
return 0; return 0;
io_block_t *ib = (io_block_t*)(gArItem->data); io_block_t *ib = (io_block_t*)(gArItem->data);
fprintf(stderr, "tar wanted: %s\n", gArWanted->name);
ssize_t off = gArWanted->start - ib->uoffset, size = gArWanted->size; ssize_t off = gArWanted->start - ib->uoffset, size = gArWanted->size;
if (off < 0) { if (off < 0) {
size += off; size += off;
@ -109,7 +108,6 @@ static ssize_t tar_read(struct archive *ar, void *ref, const void **bufp) {
gArWanted = gArWanted->next; gArWanted = gArWanted->next;
} }
fprintf(stderr, "tar read off = %zd, size = %zd\n", off, size);
gArLastOffset = off; gArLastOffset = off;
gArLastSize = size; gArLastSize = size;
*bufp = ib->output + off; *bufp = ib->output + off;
@ -119,8 +117,9 @@ static ssize_t tar_read(struct archive *ar, void *ref, const void **bufp) {
int main(int argc, char **argv) { int main(int argc, char **argv) {
gInFile = stdin; gInFile = stdin;
gOutFile = stdout; gOutFile = stdout;
bool verify = true;
int ch; int ch;
while ((ch = getopt(argc, argv, "i:o:")) != -1) { while ((ch = getopt(argc, argv, "i:o:v")) != -1) {
switch (ch) { switch (ch) {
case 'i': case 'i':
if (!(gInFile = fopen(optarg, "r"))) if (!(gInFile = fopen(optarg, "r")))
@ -130,11 +129,13 @@ int main(int argc, char **argv) {
if (!(gOutFile = fopen(optarg, "w"))) if (!(gOutFile = fopen(optarg, "w")))
die ("Can't open output file"); die ("Can't open output file");
break; break;
case 'v': verify = false; break;
default: default:
die("Unknown option"); die("Unknown option");
} }
} }
// TODO: ONly read index if necessary?
gFileIndexOffset = read_file_index(); gFileIndexOffset = read_file_index();
wanted_files(argc - optind, argv + optind); wanted_files(argc - optind, argv + optind);
#if DEBUG #if DEBUG
@ -145,7 +146,7 @@ int main(int argc, char **argv) {
gArWanted = gWantedFiles; gArWanted = gWantedFiles;
pipeline_create(block_create, block_free, read_thread, decode_thread); pipeline_create(block_create, block_free, read_thread, decode_thread);
if (gFileIndexOffset) { if (verify && gFileIndexOffset) {
struct archive *ar = archive_read_new(); struct archive *ar = archive_read_new();
archive_read_support_compression_none(ar); archive_read_support_compression_none(ar);
archive_read_support_format_tar(ar); archive_read_support_format_tar(ar);
@ -235,12 +236,12 @@ static bool spec_match(char *spec, char *name) {
} }
static void wanted_files(size_t count, char **specs) { static void wanted_files(size_t count, char **specs) {
if (count == 0) { if (!gFileIndexOffset) {
if (count)
die("Can't filter non-tarball");
gWantedFiles = NULL; gWantedFiles = NULL;
return; return;
} }
if (!gFileIndexOffset)
die("Can't filter non-tarball");
// Remove trailing slashes from specs // Remove trailing slashes from specs
for (char **spec = specs; spec < specs + count; ++spec) { for (char **spec = specs; spec < specs + count; ++spec) {
@ -253,25 +254,30 @@ static void wanted_files(size_t count, char **specs) {
bool matched[count]; // for each spec, does it match? bool matched[count]; // for each spec, does it match?
memset(matched, 0, sizeof(matched)); memset(matched, 0, sizeof(matched));
wanted_t *last = NULL; wanted_t *last = NULL;
// Check each file in order, to see if we want it // Check each file in order, to see if we want it
for (file_index_t *f = gFileIndex; f->name; f = f->next) { for (file_index_t *f = gFileIndex; f->name; f = f->next) {
bool match = !count;
for (char **spec = specs; spec < specs + count; ++spec) { for (char **spec = specs; spec < specs + count; ++spec) {
if (spec_match(*spec, f->name)) { if (spec_match(*spec, f->name)) {
wanted_t *w = malloc(sizeof(wanted_t)); match = true;
*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;
matched[spec - specs] = true; matched[spec - specs] = true;
break; 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 // Make sure each spec matched