2004-02-09 23:22:54 +00:00
|
|
|
/*-
|
2007-01-09 08:05:56 +00:00
|
|
|
* Copyright (c) 2003-2007 Tim Kientzle
|
2004-02-09 23:22:54 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
2007-01-09 08:05:56 +00:00
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2004-02-09 23:22:54 +00:00
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file contains the "essential" portions of the read API, that
|
|
|
|
* is, stuff that will probably always be used by any client that
|
|
|
|
* actually needs to read an archive. Optional pieces have been, as
|
|
|
|
* far as possible, separated out into separate files to avoid
|
|
|
|
* needlessly bloating statically-linked clients.
|
|
|
|
*/
|
|
|
|
|
2004-03-09 19:50:41 +00:00
|
|
|
#include "archive_platform.h"
|
2004-02-09 23:22:54 +00:00
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
#ifdef HAVE_ERRNO_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <errno.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <stdio.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#ifdef HAVE_STDLIB_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <stdlib.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <string.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <unistd.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
#include "archive.h"
|
|
|
|
#include "archive_entry.h"
|
|
|
|
#include "archive_private.h"
|
2007-03-03 07:37:37 +00:00
|
|
|
#include "archive_read_private.h"
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
#define minimum(a, b) (a < b ? a : b)
|
|
|
|
|
|
|
|
static int build_stream(struct archive_read *);
|
2007-03-03 07:37:37 +00:00
|
|
|
static int choose_format(struct archive_read *);
|
2009-03-05 21:18:10 +00:00
|
|
|
static struct archive_vtable *archive_read_vtable(void);
|
|
|
|
static int _archive_read_close(struct archive *);
|
|
|
|
static int _archive_read_finish(struct archive *);
|
|
|
|
|
|
|
|
static struct archive_vtable *
|
|
|
|
archive_read_vtable(void)
|
|
|
|
{
|
|
|
|
static struct archive_vtable av;
|
|
|
|
static int inited = 0;
|
|
|
|
|
|
|
|
if (!inited) {
|
|
|
|
av.archive_finish = _archive_read_finish;
|
|
|
|
av.archive_close = _archive_read_close;
|
|
|
|
}
|
|
|
|
return (&av);
|
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate, initialize and return a struct archive object.
|
|
|
|
*/
|
|
|
|
struct archive *
|
|
|
|
archive_read_new(void)
|
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_read *a;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
a = (struct archive_read *)malloc(sizeof(*a));
|
2007-02-05 16:30:40 +00:00
|
|
|
if (a == NULL)
|
2005-09-21 05:25:06 +01:00
|
|
|
return (NULL);
|
2004-02-09 23:22:54 +00:00
|
|
|
memset(a, 0, sizeof(*a));
|
2007-03-03 07:37:37 +00:00
|
|
|
a->archive.magic = ARCHIVE_READ_MAGIC;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
a->archive.state = ARCHIVE_STATE_NEW;
|
2004-02-09 23:22:54 +00:00
|
|
|
a->entry = archive_entry_new();
|
2009-03-05 21:18:10 +00:00
|
|
|
a->archive.vtable = archive_read_vtable();
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
return (&a->archive);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-09-05 06:59:46 +01:00
|
|
|
* Record the do-not-extract-to file. This belongs in archive_read_extract.c.
|
2004-02-09 23:22:54 +00:00
|
|
|
*/
|
2006-09-05 06:59:46 +01:00
|
|
|
void
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_read_extract_set_skip_file(struct archive *_a, dev_t d, ino_t i)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_read *a = (struct archive_read *)_a;
|
|
|
|
__archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_ANY,
|
|
|
|
"archive_read_extract_set_skip_file");
|
2006-09-05 06:59:46 +01:00
|
|
|
a->skip_file_dev = d;
|
|
|
|
a->skip_file_ino = i;
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
2006-09-05 06:59:46 +01:00
|
|
|
|
2009-03-06 05:58:56 +00:00
|
|
|
/*
|
|
|
|
* Set read options for the format.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
archive_read_set_format_options(struct archive *_a, const char *s)
|
|
|
|
{
|
|
|
|
struct archive_read *a;
|
2009-03-07 01:17:13 +00:00
|
|
|
struct archive_format_descriptor *format;
|
2009-03-06 05:58:56 +00:00
|
|
|
char key[64], val[64];
|
2009-06-20 07:02:21 +01:00
|
|
|
char *valp;
|
2009-03-07 01:17:13 +00:00
|
|
|
size_t i;
|
2009-03-06 05:58:56 +00:00
|
|
|
int len, r;
|
|
|
|
|
2009-12-29 05:28:28 +00:00
|
|
|
__archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
|
|
|
|
"archive_read_set_format_options");
|
|
|
|
|
2009-04-17 01:47:16 +01:00
|
|
|
if (s == NULL || *s == '\0')
|
|
|
|
return (ARCHIVE_OK);
|
2009-03-06 05:58:56 +00:00
|
|
|
a = (struct archive_read *)_a;
|
2009-04-17 01:47:16 +01:00
|
|
|
__archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
|
|
|
|
ARCHIVE_STATE_NEW, "archive_read_set_format_options");
|
2009-03-07 01:17:13 +00:00
|
|
|
len = 0;
|
|
|
|
for (i = 0; i < sizeof(a->formats)/sizeof(a->formats[0]); i++) {
|
|
|
|
format = &a->formats[i];
|
|
|
|
if (format == NULL || format->options == NULL ||
|
|
|
|
format->name == NULL)
|
|
|
|
/* This format does not support option. */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
while ((len = __archive_parse_options(s, format->name,
|
|
|
|
sizeof(key), key, sizeof(val), val)) > 0) {
|
2009-06-20 07:02:21 +01:00
|
|
|
valp = val[0] == '\0' ? NULL : val;
|
|
|
|
a->format = format;
|
|
|
|
r = format->options(a, key, valp);
|
|
|
|
a->format = NULL;
|
2009-03-07 01:17:13 +00:00
|
|
|
if (r == ARCHIVE_FATAL)
|
|
|
|
return (r);
|
|
|
|
s += len;
|
|
|
|
}
|
2009-03-06 05:58:56 +00:00
|
|
|
}
|
|
|
|
if (len < 0) {
|
|
|
|
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
|
|
|
|
"Illegal format options.");
|
|
|
|
return (ARCHIVE_WARN);
|
|
|
|
}
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set read options for the filter.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
archive_read_set_filter_options(struct archive *_a, const char *s)
|
|
|
|
{
|
|
|
|
struct archive_read *a;
|
|
|
|
struct archive_read_filter *filter;
|
|
|
|
struct archive_read_filter_bidder *bidder;
|
|
|
|
char key[64], val[64];
|
|
|
|
int len, r;
|
|
|
|
|
2009-12-29 05:28:28 +00:00
|
|
|
__archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
|
|
|
|
"archive_read_set_filter_options");
|
|
|
|
|
2009-04-17 01:47:16 +01:00
|
|
|
if (s == NULL || *s == '\0')
|
|
|
|
return (ARCHIVE_OK);
|
2009-03-06 05:58:56 +00:00
|
|
|
a = (struct archive_read *)_a;
|
2009-04-17 01:47:16 +01:00
|
|
|
__archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
|
|
|
|
ARCHIVE_STATE_NEW, "archive_read_set_filter_options");
|
2009-03-06 05:58:56 +00:00
|
|
|
len = 0;
|
|
|
|
for (filter = a->filter; filter != NULL; filter = filter->upstream) {
|
|
|
|
bidder = filter->bidder;
|
2009-03-07 01:17:13 +00:00
|
|
|
if (bidder == NULL)
|
|
|
|
continue;
|
2009-03-06 05:58:56 +00:00
|
|
|
if (bidder->options == NULL)
|
|
|
|
/* This bidder does not support option */
|
|
|
|
continue;
|
|
|
|
while ((len = __archive_parse_options(s, filter->name,
|
|
|
|
sizeof(key), key, sizeof(val), val)) > 0) {
|
|
|
|
if (val[0] == '\0')
|
|
|
|
r = bidder->options(bidder, key, NULL);
|
|
|
|
else
|
|
|
|
r = bidder->options(bidder, key, val);
|
|
|
|
if (r == ARCHIVE_FATAL)
|
|
|
|
return (r);
|
|
|
|
s += len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (len < 0) {
|
|
|
|
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
|
|
|
|
"Illegal format options.");
|
|
|
|
return (ARCHIVE_WARN);
|
|
|
|
}
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set read options for the format and the filter.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
archive_read_set_options(struct archive *_a, const char *s)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
2009-12-29 05:28:28 +00:00
|
|
|
__archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
|
|
|
|
"archive_read_set_options");
|
|
|
|
archive_clear_error(_a);
|
|
|
|
|
2009-03-06 05:58:56 +00:00
|
|
|
r = archive_read_set_format_options(_a, s);
|
|
|
|
if (r != ARCHIVE_OK)
|
|
|
|
return (r);
|
|
|
|
r = archive_read_set_filter_options(_a, s);
|
|
|
|
if (r != ARCHIVE_OK)
|
|
|
|
return (r);
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the archive
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
archive_read_open(struct archive *a, void *client_data,
|
2006-01-17 04:49:04 +00:00
|
|
|
archive_open_callback *client_opener, archive_read_callback *client_reader,
|
|
|
|
archive_close_callback *client_closer)
|
2006-07-30 01:29:01 +01:00
|
|
|
{
|
|
|
|
/* Old archive_read_open() is just a thin shell around
|
|
|
|
* archive_read_open2. */
|
|
|
|
return archive_read_open2(a, client_data, client_opener,
|
|
|
|
client_reader, NULL, client_closer);
|
|
|
|
}
|
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
static ssize_t
|
2009-03-05 02:19:42 +00:00
|
|
|
client_read_proxy(struct archive_read_filter *self, const void **buff)
|
2008-12-06 06:45:15 +00:00
|
|
|
{
|
2009-03-05 02:19:42 +00:00
|
|
|
ssize_t r;
|
|
|
|
r = (self->archive->client.reader)(&self->archive->archive,
|
2008-12-06 06:45:15 +00:00
|
|
|
self->data, buff);
|
2009-03-05 02:19:42 +00:00
|
|
|
self->archive->archive.raw_position += r;
|
|
|
|
return (r);
|
2008-12-06 06:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t
|
2009-03-05 02:19:42 +00:00
|
|
|
client_skip_proxy(struct archive_read_filter *self, int64_t request)
|
2008-12-06 06:45:15 +00:00
|
|
|
{
|
2009-12-29 05:28:28 +00:00
|
|
|
int64_t ask, get, total;
|
|
|
|
/* Limit our maximum seek request to 1GB on platforms
|
|
|
|
* with 32-bit off_t (such as Windows). */
|
|
|
|
int64_t skip_limit = ((int64_t)1) << (sizeof(off_t) * 8 - 2);
|
|
|
|
|
2009-03-07 01:21:46 +00:00
|
|
|
if (self->archive->client.skipper == NULL)
|
|
|
|
return (0);
|
2009-12-29 05:28:28 +00:00
|
|
|
total = 0;
|
|
|
|
for (;;) {
|
|
|
|
ask = request;
|
|
|
|
if (ask > skip_limit)
|
|
|
|
ask = skip_limit;
|
|
|
|
get = (self->archive->client.skipper)(&self->archive->archive,
|
|
|
|
self->data, ask);
|
|
|
|
if (get == 0)
|
|
|
|
return (total);
|
|
|
|
request -= get;
|
|
|
|
self->archive->archive.raw_position += get;
|
|
|
|
total += get;
|
|
|
|
}
|
2008-12-06 06:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-03-05 02:19:42 +00:00
|
|
|
client_close_proxy(struct archive_read_filter *self)
|
2008-12-06 06:45:15 +00:00
|
|
|
{
|
|
|
|
int r = ARCHIVE_OK;
|
|
|
|
|
|
|
|
if (self->archive->client.closer != NULL)
|
|
|
|
r = (self->archive->client.closer)((struct archive *)self->archive,
|
|
|
|
self->data);
|
2009-03-05 21:18:10 +00:00
|
|
|
self->data = NULL;
|
2008-12-06 06:45:15 +00:00
|
|
|
return (r);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-30 01:29:01 +01:00
|
|
|
int
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_read_open2(struct archive *_a, void *client_data,
|
2006-07-30 01:29:01 +01:00
|
|
|
archive_open_callback *client_opener,
|
|
|
|
archive_read_callback *client_reader,
|
|
|
|
archive_skip_callback *client_skipper,
|
|
|
|
archive_close_callback *client_closer)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_read *a = (struct archive_read *)_a;
|
2009-03-05 02:19:42 +00:00
|
|
|
struct archive_read_filter *filter;
|
2004-02-09 23:22:54 +00:00
|
|
|
int e;
|
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
__archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
|
|
|
|
"archive_read_open");
|
2009-12-29 05:28:28 +00:00
|
|
|
archive_clear_error(&a->archive);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2006-01-17 04:49:04 +00:00
|
|
|
if (client_reader == NULL)
|
2004-08-14 04:45:45 +01:00
|
|
|
__archive_errx(1,
|
|
|
|
"No reader function provided to archive_read_open");
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/* Open data source. */
|
2006-01-17 04:49:04 +00:00
|
|
|
if (client_opener != NULL) {
|
2007-03-03 07:37:37 +00:00
|
|
|
e =(client_opener)(&a->archive, client_data);
|
2006-01-17 04:49:04 +00:00
|
|
|
if (e != 0) {
|
|
|
|
/* If the open failed, call the closer to clean up. */
|
|
|
|
if (client_closer)
|
2007-03-03 07:37:37 +00:00
|
|
|
(client_closer)(&a->archive, client_data);
|
2004-02-09 23:22:54 +00:00
|
|
|
return (e);
|
2006-01-17 04:49:04 +00:00
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
/* Save the client functions and mock up the initial source. */
|
|
|
|
a->client.reader = client_reader;
|
|
|
|
a->client.skipper = client_skipper;
|
|
|
|
a->client.closer = client_closer;
|
2004-06-04 02:36:10 +01:00
|
|
|
|
2009-03-05 02:19:42 +00:00
|
|
|
filter = calloc(1, sizeof(*filter));
|
|
|
|
if (filter == NULL)
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
filter->bidder = NULL;
|
|
|
|
filter->upstream = NULL;
|
|
|
|
filter->archive = a;
|
|
|
|
filter->data = client_data;
|
|
|
|
filter->read = client_read_proxy;
|
|
|
|
filter->skip = client_skip_proxy;
|
|
|
|
filter->close = client_close_proxy;
|
2009-03-07 00:25:33 +00:00
|
|
|
filter->name = "none";
|
|
|
|
filter->code = ARCHIVE_COMPRESSION_NONE;
|
2009-03-05 02:19:42 +00:00
|
|
|
a->filter = filter;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
/* Build out the input pipeline. */
|
|
|
|
e = build_stream(a);
|
2005-09-21 05:48:52 +01:00
|
|
|
if (e == ARCHIVE_OK)
|
2007-03-03 07:37:37 +00:00
|
|
|
a->archive.state = ARCHIVE_STATE_HEADER;
|
2005-09-21 05:48:52 +01:00
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
return (e);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-12-06 06:45:15 +00:00
|
|
|
* Allow each registered stream transform to bid on whether
|
|
|
|
* it wants to handle this stream. Repeat until we've finished
|
|
|
|
* building the pipeline.
|
2004-02-09 23:22:54 +00:00
|
|
|
*/
|
2008-12-06 06:45:15 +00:00
|
|
|
static int
|
|
|
|
build_stream(struct archive_read *a)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2009-03-05 02:19:42 +00:00
|
|
|
int number_bidders, i, bid, best_bid;
|
|
|
|
struct archive_read_filter_bidder *bidder, *best_bidder;
|
|
|
|
struct archive_read_filter *filter;
|
2009-12-29 05:30:23 +00:00
|
|
|
ssize_t avail;
|
2009-03-05 02:19:42 +00:00
|
|
|
int r;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2009-03-05 02:19:42 +00:00
|
|
|
for (;;) {
|
|
|
|
number_bidders = sizeof(a->bidders) / sizeof(a->bidders[0]);
|
|
|
|
|
|
|
|
best_bid = 0;
|
|
|
|
best_bidder = NULL;
|
|
|
|
|
|
|
|
bidder = a->bidders;
|
|
|
|
for (i = 0; i < number_bidders; i++, bidder++) {
|
|
|
|
if (bidder->bid != NULL) {
|
|
|
|
bid = (bidder->bid)(bidder, a->filter);
|
|
|
|
if (bid > best_bid) {
|
|
|
|
best_bid = bid;
|
|
|
|
best_bidder = bidder;
|
|
|
|
}
|
|
|
|
}
|
2008-12-06 06:45:15 +00:00
|
|
|
}
|
|
|
|
|
2009-03-05 02:19:42 +00:00
|
|
|
/* If no bidder, we're done. */
|
|
|
|
if (best_bidder == NULL) {
|
|
|
|
a->archive.compression_name = a->filter->name;
|
|
|
|
a->archive.compression_code = a->filter->code;
|
|
|
|
return (ARCHIVE_OK);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
2009-03-05 02:19:42 +00:00
|
|
|
filter
|
|
|
|
= (struct archive_read_filter *)calloc(1, sizeof(*filter));
|
|
|
|
if (filter == NULL)
|
2007-03-31 23:59:43 +01:00
|
|
|
return (ARCHIVE_FATAL);
|
2009-03-05 02:19:42 +00:00
|
|
|
filter->bidder = best_bidder;
|
|
|
|
filter->archive = a;
|
|
|
|
filter->upstream = a->filter;
|
|
|
|
r = (best_bidder->init)(filter);
|
|
|
|
if (r != ARCHIVE_OK) {
|
|
|
|
free(filter);
|
|
|
|
return (r);
|
|
|
|
}
|
2009-12-29 05:30:23 +00:00
|
|
|
/* Verify the filter by asking it for some data. */
|
|
|
|
__archive_read_filter_ahead(filter, 1, &avail);
|
|
|
|
if (avail < 0) {
|
|
|
|
/* If the read failed, bail out now. */
|
|
|
|
free(filter);
|
|
|
|
return (avail);
|
|
|
|
}
|
2009-03-05 02:19:42 +00:00
|
|
|
a->filter = filter;
|
2007-03-31 23:59:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
/*
|
|
|
|
* Read header of next entry.
|
|
|
|
*/
|
|
|
|
int
|
2009-04-17 01:47:16 +01:00
|
|
|
archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_read *a = (struct archive_read *)_a;
|
2004-02-09 23:22:54 +00:00
|
|
|
int slot, ret;
|
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
__archive_check_magic(_a, ARCHIVE_READ_MAGIC,
|
|
|
|
ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
|
|
|
|
"archive_read_next_header");
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2009-12-28 02:58:14 +00:00
|
|
|
++_a->file_count;
|
2004-02-09 23:22:54 +00:00
|
|
|
archive_entry_clear(entry);
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_clear_error(&a->archive);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2007-12-30 04:58:22 +00:00
|
|
|
/*
|
|
|
|
* If no format has yet been chosen, choose one.
|
|
|
|
*/
|
|
|
|
if (a->format == NULL) {
|
|
|
|
slot = choose_format(a);
|
|
|
|
if (slot < 0) {
|
|
|
|
a->archive.state = ARCHIVE_STATE_FATAL;
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
}
|
|
|
|
a->format = &(a->formats[slot]);
|
|
|
|
}
|
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
/*
|
|
|
|
* If client didn't consume entire data, skip any remainder
|
|
|
|
* (This is especially important for GNU incremental directories.)
|
|
|
|
*/
|
2007-03-03 07:37:37 +00:00
|
|
|
if (a->archive.state == ARCHIVE_STATE_DATA) {
|
|
|
|
ret = archive_read_data_skip(&a->archive);
|
2004-02-09 23:22:54 +00:00
|
|
|
if (ret == ARCHIVE_EOF) {
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_set_error(&a->archive, EIO, "Premature end-of-file.");
|
|
|
|
a->archive.state = ARCHIVE_STATE_FATAL;
|
2004-02-09 23:22:54 +00:00
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
}
|
2005-04-06 05:19:30 +01:00
|
|
|
if (ret != ARCHIVE_OK)
|
|
|
|
return (ret);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Record start-of-header. */
|
2007-03-03 07:37:37 +00:00
|
|
|
a->header_position = a->archive.file_position;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
ret = (a->format->read_header)(a, entry);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* EOF and FATAL are persistent at this layer. By
|
2006-12-28 05:21:48 +00:00
|
|
|
* modifying the state, we guarantee that future calls to
|
2004-02-09 23:22:54 +00:00
|
|
|
* read a header or read data will fail.
|
|
|
|
*/
|
|
|
|
switch (ret) {
|
|
|
|
case ARCHIVE_EOF:
|
2007-03-03 07:37:37 +00:00
|
|
|
a->archive.state = ARCHIVE_STATE_EOF;
|
2004-02-09 23:22:54 +00:00
|
|
|
break;
|
|
|
|
case ARCHIVE_OK:
|
2007-03-03 07:37:37 +00:00
|
|
|
a->archive.state = ARCHIVE_STATE_DATA;
|
2004-02-09 23:22:54 +00:00
|
|
|
break;
|
|
|
|
case ARCHIVE_WARN:
|
2007-03-03 07:37:37 +00:00
|
|
|
a->archive.state = ARCHIVE_STATE_DATA;
|
2004-02-09 23:22:54 +00:00
|
|
|
break;
|
|
|
|
case ARCHIVE_RETRY:
|
|
|
|
break;
|
|
|
|
case ARCHIVE_FATAL:
|
2007-03-03 07:37:37 +00:00
|
|
|
a->archive.state = ARCHIVE_STATE_FATAL;
|
2004-02-09 23:22:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-06-05 00:25:20 +01:00
|
|
|
a->read_data_output_offset = 0;
|
|
|
|
a->read_data_remaining = 0;
|
2004-02-09 23:22:54 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2009-04-17 01:47:16 +01:00
|
|
|
int
|
|
|
|
archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct archive_read *a = (struct archive_read *)_a;
|
|
|
|
*entryp = NULL;
|
|
|
|
ret = archive_read_next_header2(_a, a->entry);
|
|
|
|
*entryp = a->entry;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
/*
|
|
|
|
* Allow each registered format to bid on whether it wants to handle
|
|
|
|
* the next entry. Return index of winning bidder.
|
|
|
|
*/
|
|
|
|
static int
|
2007-03-03 07:37:37 +00:00
|
|
|
choose_format(struct archive_read *a)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
|
|
|
int slots;
|
|
|
|
int i;
|
|
|
|
int bid, best_bid;
|
|
|
|
int best_bid_slot;
|
|
|
|
|
|
|
|
slots = sizeof(a->formats) / sizeof(a->formats[0]);
|
|
|
|
best_bid = -1;
|
|
|
|
best_bid_slot = -1;
|
|
|
|
|
|
|
|
/* Set up a->format and a->pformat_data for convenience of bidders. */
|
|
|
|
a->format = &(a->formats[0]);
|
|
|
|
for (i = 0; i < slots; i++, a->format++) {
|
|
|
|
if (a->format->bid) {
|
|
|
|
bid = (a->format->bid)(a);
|
|
|
|
if (bid == ARCHIVE_FATAL)
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
if ((bid > best_bid) || (best_bid_slot < 0)) {
|
|
|
|
best_bid = bid;
|
|
|
|
best_bid_slot = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There were no bidders; this is a serious programmer error
|
|
|
|
* and demands a quick and definitive abort.
|
|
|
|
*/
|
|
|
|
if (best_bid_slot < 0)
|
2004-08-14 04:45:45 +01:00
|
|
|
__archive_errx(1, "No formats were registered; you must "
|
2004-02-09 23:22:54 +00:00
|
|
|
"invoke at least one archive_read_support_format_XXX "
|
|
|
|
"function in order to successfully read an archive.");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There were bidders, but no non-zero bids; this means we
|
|
|
|
* can't support this stream.
|
|
|
|
*/
|
|
|
|
if (best_bid < 1) {
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
|
2004-03-09 19:50:41 +00:00
|
|
|
"Unrecognized archive format");
|
2004-02-09 23:22:54 +00:00
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (best_bid_slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the file offset (within the uncompressed data stream) where
|
|
|
|
* the last header started.
|
|
|
|
*/
|
|
|
|
int64_t
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_read_header_position(struct archive *_a)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_read *a = (struct archive_read *)_a;
|
|
|
|
__archive_check_magic(_a, ARCHIVE_READ_MAGIC,
|
|
|
|
ARCHIVE_STATE_ANY, "archive_read_header_position");
|
2004-02-09 23:22:54 +00:00
|
|
|
return (a->header_position);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-06-02 09:14:43 +01:00
|
|
|
* Read data from an archive entry, using a read(2)-style interface.
|
|
|
|
* This is a convenience routine that just calls
|
|
|
|
* archive_read_data_block and copies the results into the client
|
|
|
|
* buffer, filling any gaps with zero bytes. Clients using this
|
|
|
|
* API can be completely ignorant of sparse-file issues; sparse files
|
|
|
|
* will simply be padded with nulls.
|
2004-06-05 00:25:20 +01:00
|
|
|
*
|
|
|
|
* DO NOT intermingle calls to this function and archive_read_data_block
|
|
|
|
* to read a single entry body.
|
2004-02-09 23:22:54 +00:00
|
|
|
*/
|
|
|
|
ssize_t
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_read_data(struct archive *_a, void *buff, size_t s)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_read *a = (struct archive_read *)_a;
|
2004-06-02 09:14:43 +01:00
|
|
|
char *dest;
|
2007-04-02 01:41:37 +01:00
|
|
|
const void *read_buf;
|
2004-06-02 09:14:43 +01:00
|
|
|
size_t bytes_read;
|
|
|
|
size_t len;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
bytes_read = 0;
|
2006-11-10 06:39:46 +00:00
|
|
|
dest = (char *)buff;
|
2004-06-02 09:14:43 +01:00
|
|
|
|
|
|
|
while (s > 0) {
|
2007-04-05 06:22:13 +01:00
|
|
|
if (a->read_data_remaining == 0) {
|
2007-04-02 01:41:37 +01:00
|
|
|
read_buf = a->read_data_block;
|
|
|
|
r = archive_read_data_block(&a->archive, &read_buf,
|
|
|
|
&a->read_data_remaining, &a->read_data_offset);
|
|
|
|
a->read_data_block = read_buf;
|
2004-06-02 09:14:43 +01:00
|
|
|
if (r == ARCHIVE_EOF)
|
|
|
|
return (bytes_read);
|
2005-09-10 19:05:54 +01:00
|
|
|
/*
|
|
|
|
* Error codes are all negative, so the status
|
|
|
|
* return here cannot be confused with a valid
|
|
|
|
* byte count. (ARCHIVE_OK is zero.)
|
|
|
|
*/
|
|
|
|
if (r < ARCHIVE_OK)
|
2004-06-02 09:14:43 +01:00
|
|
|
return (r);
|
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-06-02 09:14:43 +01:00
|
|
|
if (a->read_data_offset < a->read_data_output_offset) {
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
|
2005-09-10 19:05:54 +01:00
|
|
|
"Encountered out-of-order sparse blocks");
|
|
|
|
return (ARCHIVE_RETRY);
|
2007-01-03 13:16:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute the amount of zero padding needed. */
|
2007-01-03 20:01:44 +00:00
|
|
|
if (a->read_data_output_offset + (off_t)s <
|
2007-01-03 13:16:59 +00:00
|
|
|
a->read_data_offset) {
|
|
|
|
len = s;
|
|
|
|
} else if (a->read_data_output_offset <
|
|
|
|
a->read_data_offset) {
|
|
|
|
len = a->read_data_offset -
|
|
|
|
a->read_data_output_offset;
|
|
|
|
} else
|
|
|
|
len = 0;
|
|
|
|
|
|
|
|
/* Add zeroes. */
|
|
|
|
memset(dest, 0, len);
|
|
|
|
s -= len;
|
|
|
|
a->read_data_output_offset += len;
|
|
|
|
dest += len;
|
|
|
|
bytes_read += len;
|
|
|
|
|
|
|
|
/* Copy data if there is any space left. */
|
|
|
|
if (s > 0) {
|
2004-06-02 09:14:43 +01:00
|
|
|
len = a->read_data_remaining;
|
|
|
|
if (len > s)
|
|
|
|
len = s;
|
|
|
|
memcpy(dest, a->read_data_block, len);
|
|
|
|
s -= len;
|
2005-09-10 19:05:54 +01:00
|
|
|
a->read_data_block += len;
|
2004-06-02 09:14:43 +01:00
|
|
|
a->read_data_remaining -= len;
|
|
|
|
a->read_data_output_offset += len;
|
|
|
|
a->read_data_offset += len;
|
|
|
|
dest += len;
|
|
|
|
bytes_read += len;
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
2005-09-10 19:05:54 +01:00
|
|
|
return (bytes_read);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
2007-04-05 16:51:19 +01:00
|
|
|
#if ARCHIVE_API_VERSION < 3
|
|
|
|
/*
|
|
|
|
* Obsolete function provided for compatibility only. Note that the API
|
|
|
|
* of this function doesn't allow the caller to detect if the remaining
|
|
|
|
* data from the archive entry is shorter than the buffer provided, or
|
|
|
|
* even if an error occurred while reading data.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
archive_read_data_into_buffer(struct archive *a, void *d, ssize_t len)
|
|
|
|
{
|
|
|
|
|
|
|
|
archive_read_data(a, d, len);
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
/*
|
|
|
|
* Skip over all remaining data in this entry.
|
|
|
|
*/
|
|
|
|
int
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_read_data_skip(struct archive *_a)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_read *a = (struct archive_read *)_a;
|
2004-06-02 09:14:43 +01:00
|
|
|
int r;
|
2004-02-09 23:22:54 +00:00
|
|
|
const void *buff;
|
2005-09-24 22:15:00 +01:00
|
|
|
size_t size;
|
2004-06-02 09:14:43 +01:00
|
|
|
off_t offset;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
__archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
|
|
|
|
"archive_read_data_skip");
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2005-04-06 05:19:30 +01:00
|
|
|
if (a->format->read_data_skip != NULL)
|
|
|
|
r = (a->format->read_data_skip)(a);
|
|
|
|
else {
|
2007-03-03 07:37:37 +00:00
|
|
|
while ((r = archive_read_data_block(&a->archive,
|
|
|
|
&buff, &size, &offset))
|
2005-04-06 05:19:30 +01:00
|
|
|
== ARCHIVE_OK)
|
|
|
|
;
|
|
|
|
}
|
2004-06-02 09:14:43 +01:00
|
|
|
|
|
|
|
if (r == ARCHIVE_EOF)
|
|
|
|
r = ARCHIVE_OK;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
a->archive.state = ARCHIVE_STATE_HEADER;
|
2004-06-02 09:14:43 +01:00
|
|
|
return (r);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the next block of entry data from the archive.
|
|
|
|
* This is a zero-copy interface; the client receives a pointer,
|
|
|
|
* size, and file offset of the next available block of data.
|
|
|
|
*
|
|
|
|
* Returns ARCHIVE_OK if the operation is successful, ARCHIVE_EOF if
|
|
|
|
* the end of entry is encountered.
|
|
|
|
*/
|
|
|
|
int
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_read_data_block(struct archive *_a,
|
2004-06-02 09:14:43 +01:00
|
|
|
const void **buff, size_t *size, off_t *offset)
|
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_read *a = (struct archive_read *)_a;
|
|
|
|
__archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
|
|
|
|
"archive_read_data_block");
|
2004-06-02 09:14:43 +01:00
|
|
|
|
|
|
|
if (a->format->read_data == NULL) {
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
|
2004-06-02 09:14:43 +01:00
|
|
|
"Internal error: "
|
|
|
|
"No format_read_data_block function registered");
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (a->format->read_data)(a, buff, size, offset);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-08-07 20:22:50 +01:00
|
|
|
* Close the file and release most resources.
|
2004-02-09 23:22:54 +00:00
|
|
|
*
|
|
|
|
* Be careful: client might just call read_new and then read_finish.
|
|
|
|
* Don't assume we actually read anything or performed any non-trivial
|
|
|
|
* initialization.
|
|
|
|
*/
|
2009-03-05 21:18:10 +00:00
|
|
|
static int
|
|
|
|
_archive_read_close(struct archive *_a)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_read *a = (struct archive_read *)_a;
|
2006-09-05 06:59:46 +01:00
|
|
|
int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
|
2007-05-29 02:00:21 +01:00
|
|
|
size_t i, n;
|
2006-09-05 06:59:46 +01:00
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
__archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
|
|
|
|
ARCHIVE_STATE_ANY, "archive_read_close");
|
2009-04-17 01:47:16 +01:00
|
|
|
archive_clear_error(&a->archive);
|
2007-03-03 07:37:37 +00:00
|
|
|
a->archive.state = ARCHIVE_STATE_CLOSED;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2009-04-17 01:47:16 +01:00
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
/* Call cleanup functions registered by optional components. */
|
|
|
|
if (a->cleanup_archive_extract != NULL)
|
2006-09-05 06:59:46 +01:00
|
|
|
r = (a->cleanup_archive_extract)(a);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2007-05-29 02:00:21 +01:00
|
|
|
/* TODO: Clean up the formatters. */
|
|
|
|
|
2009-03-05 02:19:42 +00:00
|
|
|
/* Clean up the filter pipeline. */
|
|
|
|
while (a->filter != NULL) {
|
|
|
|
struct archive_read_filter *t = a->filter->upstream;
|
|
|
|
if (a->filter->close != NULL) {
|
|
|
|
r1 = (a->filter->close)(a->filter);
|
|
|
|
if (r1 < r)
|
|
|
|
r = r1;
|
|
|
|
}
|
|
|
|
free(a->filter->buffer);
|
|
|
|
free(a->filter);
|
|
|
|
a->filter = t;
|
2008-12-06 06:45:15 +00:00
|
|
|
}
|
|
|
|
|
2009-03-05 02:19:42 +00:00
|
|
|
/* Release the bidder objects. */
|
|
|
|
n = sizeof(a->bidders)/sizeof(a->bidders[0]);
|
2007-05-29 02:00:21 +01:00
|
|
|
for (i = 0; i < n; i++) {
|
2009-03-05 02:19:42 +00:00
|
|
|
if (a->bidders[i].free != NULL) {
|
|
|
|
r1 = (a->bidders[i].free)(&a->bidders[i]);
|
2007-05-29 02:00:21 +01:00
|
|
|
if (r1 < r)
|
|
|
|
r = r1;
|
|
|
|
}
|
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2006-09-05 06:59:46 +01:00
|
|
|
return (r);
|
2004-08-07 20:22:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Release memory and other resources.
|
|
|
|
*/
|
2009-12-29 05:28:28 +00:00
|
|
|
static int
|
2009-03-05 21:18:10 +00:00
|
|
|
_archive_read_finish(struct archive *_a)
|
2004-08-07 20:22:50 +01:00
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_read *a = (struct archive_read *)_a;
|
2004-08-07 20:22:50 +01:00
|
|
|
int i;
|
|
|
|
int slots;
|
2007-03-03 07:37:37 +00:00
|
|
|
int r = ARCHIVE_OK;
|
2004-08-07 20:22:50 +01:00
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
__archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_ANY,
|
|
|
|
"archive_read_finish");
|
|
|
|
if (a->archive.state != ARCHIVE_STATE_CLOSED)
|
|
|
|
r = archive_read_close(&a->archive);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-04-05 22:12:29 +01:00
|
|
|
/* Cleanup format-specific data. */
|
|
|
|
slots = sizeof(a->formats) / sizeof(a->formats[0]);
|
|
|
|
for (i = 0; i < slots; i++) {
|
2007-05-29 02:00:21 +01:00
|
|
|
a->format = &(a->formats[i]);
|
2004-04-05 22:12:29 +01:00
|
|
|
if (a->formats[i].cleanup)
|
|
|
|
(a->formats[i].cleanup)(a);
|
|
|
|
}
|
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_string_free(&a->archive.error_string);
|
2004-02-09 23:22:54 +00:00
|
|
|
if (a->entry)
|
|
|
|
archive_entry_free(a->entry);
|
2007-03-03 07:37:37 +00:00
|
|
|
a->archive.magic = 0;
|
2004-02-09 23:22:54 +00:00
|
|
|
free(a);
|
2007-03-03 07:37:37 +00:00
|
|
|
#if ARCHIVE_API_VERSION > 1
|
|
|
|
return (r);
|
|
|
|
#endif
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Used internally by read format handlers to register their bid and
|
|
|
|
* initialization functions.
|
|
|
|
*/
|
|
|
|
int
|
2007-03-03 07:37:37 +00:00
|
|
|
__archive_read_register_format(struct archive_read *a,
|
2004-02-09 23:22:54 +00:00
|
|
|
void *format_data,
|
2009-03-06 05:58:56 +00:00
|
|
|
const char *name,
|
2007-03-03 07:37:37 +00:00
|
|
|
int (*bid)(struct archive_read *),
|
2009-03-06 05:58:56 +00:00
|
|
|
int (*options)(struct archive_read *, const char *, const char *),
|
2007-03-03 07:37:37 +00:00
|
|
|
int (*read_header)(struct archive_read *, struct archive_entry *),
|
|
|
|
int (*read_data)(struct archive_read *, const void **, size_t *, off_t *),
|
|
|
|
int (*read_data_skip)(struct archive_read *),
|
|
|
|
int (*cleanup)(struct archive_read *))
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
|
|
|
int i, number_slots;
|
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
__archive_check_magic(&a->archive,
|
|
|
|
ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
|
|
|
|
"__archive_read_register_format");
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
number_slots = sizeof(a->formats) / sizeof(a->formats[0]);
|
|
|
|
|
|
|
|
for (i = 0; i < number_slots; i++) {
|
|
|
|
if (a->formats[i].bid == bid)
|
2004-04-14 00:45:37 +01:00
|
|
|
return (ARCHIVE_WARN); /* We've already installed */
|
2004-02-09 23:22:54 +00:00
|
|
|
if (a->formats[i].bid == NULL) {
|
|
|
|
a->formats[i].bid = bid;
|
2009-03-06 05:58:56 +00:00
|
|
|
a->formats[i].options = options;
|
2004-02-09 23:22:54 +00:00
|
|
|
a->formats[i].read_header = read_header;
|
2004-06-02 09:14:43 +01:00
|
|
|
a->formats[i].read_data = read_data;
|
2005-04-06 05:19:30 +01:00
|
|
|
a->formats[i].read_data_skip = read_data_skip;
|
2004-02-09 23:22:54 +00:00
|
|
|
a->formats[i].cleanup = cleanup;
|
2007-05-29 02:00:21 +01:00
|
|
|
a->formats[i].data = format_data;
|
2009-03-06 05:58:56 +00:00
|
|
|
a->formats[i].name = name;
|
2004-02-09 23:22:54 +00:00
|
|
|
return (ARCHIVE_OK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-14 04:45:45 +01:00
|
|
|
__archive_errx(1, "Not enough slots for format registration");
|
|
|
|
return (ARCHIVE_FATAL); /* Never actually called. */
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Used internally by decompression routines to register their bid and
|
|
|
|
* initialization functions.
|
|
|
|
*/
|
2009-03-05 02:19:42 +00:00
|
|
|
struct archive_read_filter_bidder *
|
|
|
|
__archive_read_get_bidder(struct archive_read *a)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
|
|
|
int i, number_slots;
|
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
__archive_check_magic(&a->archive,
|
|
|
|
ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
|
2009-03-05 02:19:42 +00:00
|
|
|
"__archive_read_get_bidder");
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2009-03-05 02:19:42 +00:00
|
|
|
number_slots = sizeof(a->bidders) / sizeof(a->bidders[0]);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
for (i = 0; i < number_slots; i++) {
|
2009-03-07 01:18:30 +00:00
|
|
|
if (a->bidders[i].bid == NULL) {
|
|
|
|
memset(a->bidders + i, 0, sizeof(a->bidders[0]));
|
2009-03-05 02:19:42 +00:00
|
|
|
return (a->bidders + i);
|
2009-03-07 01:18:30 +00:00
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
2004-08-14 04:45:45 +01:00
|
|
|
__archive_errx(1, "Not enough slots for compression registration");
|
2007-05-29 02:00:21 +01:00
|
|
|
return (NULL); /* Never actually executed. */
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
2008-01-03 17:54:26 +00:00
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
/*
|
|
|
|
* The next three functions comprise the peek/consume internal I/O
|
|
|
|
* system used by archive format readers. This system allows fairly
|
|
|
|
* flexible read-ahead and allows the I/O code to operate in a
|
|
|
|
* zero-copy manner most of the time.
|
|
|
|
*
|
2009-03-05 02:19:42 +00:00
|
|
|
* In the ideal case, filters generate blocks of data
|
2008-12-06 06:45:15 +00:00
|
|
|
* and __archive_read_ahead() just returns pointers directly into
|
|
|
|
* those blocks. Then __archive_read_consume() just bumps those
|
|
|
|
* pointers. Only if your request would span blocks does the I/O
|
|
|
|
* layer use a copy buffer to provide you with a contiguous block of
|
|
|
|
* data. The __archive_read_skip() is an optimization; it scans ahead
|
|
|
|
* very quickly (it usually translates into a seek() operation if
|
|
|
|
* you're reading uncompressed disk files).
|
|
|
|
*
|
|
|
|
* A couple of useful idioms:
|
|
|
|
* * "I just want some data." Ask for 1 byte and pay attention to
|
|
|
|
* the "number of bytes available" from __archive_read_ahead().
|
|
|
|
* You can consume more than you asked for; you just can't consume
|
2009-03-05 02:19:42 +00:00
|
|
|
* more than is available. If you consume everything that's
|
2008-12-06 06:45:15 +00:00
|
|
|
* immediately available, the next read_ahead() call will pull
|
|
|
|
* the next block.
|
|
|
|
* * "I want to output a large block of data." As above, ask for 1 byte,
|
|
|
|
* emit all that's available (up to whatever limit you have), then
|
|
|
|
* repeat until you're done.
|
|
|
|
* * "I want to peek ahead by a large amount." Ask for 4k or so, then
|
|
|
|
* double and repeat until you get an error or have enough. Note
|
|
|
|
* that the I/O layer will likely end up expanding its copy buffer
|
|
|
|
* to fit your request, so use this technique cautiously. This
|
|
|
|
* technique is used, for example, by some of the format tasting
|
|
|
|
* code that has uncertain look-ahead needs.
|
|
|
|
*
|
|
|
|
* TODO: Someday, provide a more generic __archive_read_seek() for
|
|
|
|
* those cases where it's useful. This is tricky because there are lots
|
|
|
|
* of cases where seek() is not available (reading gzip data from a
|
|
|
|
* network socket, for instance), so there needs to be a good way to
|
|
|
|
* communicate whether seek() is available and users of that interface
|
|
|
|
* need to use non-seeking strategies whenever seek() is not available.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Looks ahead in the input stream:
|
|
|
|
* * If 'avail' pointer is provided, that returns number of bytes available
|
|
|
|
* in the current buffer, which may be much larger than requested.
|
|
|
|
* * If end-of-file, *avail gets set to zero.
|
|
|
|
* * If error, *avail gets error code.
|
|
|
|
* * If request can be met, returns pointer to data, returns NULL
|
|
|
|
* if request is not met.
|
|
|
|
*
|
|
|
|
* Note: If you just want "some data", ask for 1 byte and pay attention
|
|
|
|
* to *avail, which will have the actual amount available. If you
|
|
|
|
* know exactly how many bytes you need, just ask for that and treat
|
|
|
|
* a NULL return as an error.
|
|
|
|
*
|
|
|
|
* Important: This does NOT move the file pointer. See
|
|
|
|
* __archive_read_consume() below.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is tricky. We need to provide our clients with pointers to
|
|
|
|
* contiguous blocks of memory but we want to avoid copying whenever
|
|
|
|
* possible.
|
|
|
|
*
|
|
|
|
* Mostly, this code returns pointers directly into the block of data
|
|
|
|
* provided by the client_read routine. It can do this unless the
|
|
|
|
* request would split across blocks. In that case, we have to copy
|
|
|
|
* into an internal buffer to combine reads.
|
|
|
|
*/
|
2008-01-03 17:54:26 +00:00
|
|
|
const void *
|
2008-12-06 06:45:15 +00:00
|
|
|
__archive_read_ahead(struct archive_read *a, size_t min, ssize_t *avail)
|
2009-03-05 02:19:42 +00:00
|
|
|
{
|
|
|
|
return (__archive_read_filter_ahead(a->filter, min, avail));
|
|
|
|
}
|
|
|
|
|
|
|
|
const void *
|
|
|
|
__archive_read_filter_ahead(struct archive_read_filter *filter,
|
|
|
|
size_t min, ssize_t *avail)
|
2008-01-03 17:54:26 +00:00
|
|
|
{
|
2008-12-06 06:45:15 +00:00
|
|
|
ssize_t bytes_read;
|
|
|
|
size_t tocopy;
|
2008-01-03 17:54:26 +00:00
|
|
|
|
2009-03-05 02:19:42 +00:00
|
|
|
if (filter->fatal) {
|
2008-12-06 06:45:15 +00:00
|
|
|
if (avail)
|
|
|
|
*avail = ARCHIVE_FATAL;
|
2008-01-03 17:54:26 +00:00
|
|
|
return (NULL);
|
2008-12-06 06:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Keep pulling more data until we can satisfy the request.
|
|
|
|
*/
|
|
|
|
for (;;) {
|
|
|
|
|
|
|
|
/*
|
2009-09-08 06:02:41 +01:00
|
|
|
* If we can satisfy from the copy buffer (and the
|
|
|
|
* copy buffer isn't empty), we're done. In particular,
|
|
|
|
* note that min == 0 is a perfectly well-defined
|
|
|
|
* request.
|
2008-12-06 06:45:15 +00:00
|
|
|
*/
|
2009-09-08 06:02:41 +01:00
|
|
|
if (filter->avail >= min && filter->avail > 0) {
|
2008-12-06 06:45:15 +00:00
|
|
|
if (avail != NULL)
|
2009-03-05 02:19:42 +00:00
|
|
|
*avail = filter->avail;
|
|
|
|
return (filter->next);
|
2008-12-06 06:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We can satisfy directly from client buffer if everything
|
|
|
|
* currently in the copy buffer is still in the client buffer.
|
|
|
|
*/
|
2009-03-05 02:19:42 +00:00
|
|
|
if (filter->client_total >= filter->client_avail + filter->avail
|
|
|
|
&& filter->client_avail + filter->avail >= min) {
|
2008-12-06 06:45:15 +00:00
|
|
|
/* "Roll back" to client buffer. */
|
2009-03-05 02:19:42 +00:00
|
|
|
filter->client_avail += filter->avail;
|
|
|
|
filter->client_next -= filter->avail;
|
2008-12-06 06:45:15 +00:00
|
|
|
/* Copy buffer is now empty. */
|
2009-03-05 02:19:42 +00:00
|
|
|
filter->avail = 0;
|
|
|
|
filter->next = filter->buffer;
|
2008-12-06 06:45:15 +00:00
|
|
|
/* Return data from client buffer. */
|
|
|
|
if (avail != NULL)
|
2009-03-05 02:19:42 +00:00
|
|
|
*avail = filter->client_avail;
|
|
|
|
return (filter->client_next);
|
2008-12-06 06:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Move data forward in copy buffer if necessary. */
|
2009-03-05 02:19:42 +00:00
|
|
|
if (filter->next > filter->buffer &&
|
|
|
|
filter->next + min > filter->buffer + filter->buffer_size) {
|
|
|
|
if (filter->avail > 0)
|
|
|
|
memmove(filter->buffer, filter->next, filter->avail);
|
|
|
|
filter->next = filter->buffer;
|
2008-12-06 06:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If we've used up the client data, get more. */
|
2009-03-05 02:19:42 +00:00
|
|
|
if (filter->client_avail <= 0) {
|
|
|
|
if (filter->end_of_file) {
|
2008-12-06 06:45:15 +00:00
|
|
|
if (avail != NULL)
|
|
|
|
*avail = 0;
|
|
|
|
return (NULL);
|
|
|
|
}
|
2009-03-05 02:19:42 +00:00
|
|
|
bytes_read = (filter->read)(filter,
|
|
|
|
&filter->client_buff);
|
2008-12-06 06:45:15 +00:00
|
|
|
if (bytes_read < 0) { /* Read error. */
|
2009-03-05 02:19:42 +00:00
|
|
|
filter->client_total = filter->client_avail = 0;
|
|
|
|
filter->client_next = filter->client_buff = NULL;
|
|
|
|
filter->fatal = 1;
|
2008-12-06 06:45:15 +00:00
|
|
|
if (avail != NULL)
|
|
|
|
*avail = ARCHIVE_FATAL;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
if (bytes_read == 0) { /* Premature end-of-file. */
|
2009-03-05 02:19:42 +00:00
|
|
|
filter->client_total = filter->client_avail = 0;
|
|
|
|
filter->client_next = filter->client_buff = NULL;
|
|
|
|
filter->end_of_file = 1;
|
2008-12-06 06:45:15 +00:00
|
|
|
/* Return whatever we do have. */
|
|
|
|
if (avail != NULL)
|
2009-03-05 02:19:42 +00:00
|
|
|
*avail = filter->avail;
|
2008-12-06 06:45:15 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
2009-03-05 02:19:42 +00:00
|
|
|
filter->position += bytes_read;
|
|
|
|
filter->client_total = bytes_read;
|
|
|
|
filter->client_avail = filter->client_total;
|
|
|
|
filter->client_next = filter->client_buff;
|
2008-12-06 06:45:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We can't satisfy the request from the copy
|
|
|
|
* buffer or the existing client data, so we
|
|
|
|
* need to copy more client data over to the
|
|
|
|
* copy buffer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Ensure the buffer is big enough. */
|
2009-03-05 02:19:42 +00:00
|
|
|
if (min > filter->buffer_size) {
|
2008-12-06 06:45:15 +00:00
|
|
|
size_t s, t;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
/* Double the buffer; watch for overflow. */
|
2009-03-05 02:19:42 +00:00
|
|
|
s = t = filter->buffer_size;
|
|
|
|
if (s == 0)
|
|
|
|
s = min;
|
2008-12-06 06:45:15 +00:00
|
|
|
while (s < min) {
|
|
|
|
t *= 2;
|
|
|
|
if (t <= s) { /* Integer overflow! */
|
2009-03-05 02:19:42 +00:00
|
|
|
archive_set_error(
|
|
|
|
&filter->archive->archive,
|
|
|
|
ENOMEM,
|
2008-12-06 06:45:15 +00:00
|
|
|
"Unable to allocate copy buffer");
|
2009-03-05 02:19:42 +00:00
|
|
|
filter->fatal = 1;
|
2008-12-06 06:45:15 +00:00
|
|
|
if (avail != NULL)
|
|
|
|
*avail = ARCHIVE_FATAL;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
s = t;
|
|
|
|
}
|
|
|
|
/* Now s >= min, so allocate a new buffer. */
|
|
|
|
p = (char *)malloc(s);
|
|
|
|
if (p == NULL) {
|
2009-03-05 02:19:42 +00:00
|
|
|
archive_set_error(
|
|
|
|
&filter->archive->archive,
|
|
|
|
ENOMEM,
|
2008-12-06 06:45:15 +00:00
|
|
|
"Unable to allocate copy buffer");
|
2009-03-05 02:19:42 +00:00
|
|
|
filter->fatal = 1;
|
2008-12-06 06:45:15 +00:00
|
|
|
if (avail != NULL)
|
|
|
|
*avail = ARCHIVE_FATAL;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
/* Move data into newly-enlarged buffer. */
|
2009-03-05 02:19:42 +00:00
|
|
|
if (filter->avail > 0)
|
|
|
|
memmove(p, filter->next, filter->avail);
|
|
|
|
free(filter->buffer);
|
|
|
|
filter->next = filter->buffer = p;
|
|
|
|
filter->buffer_size = s;
|
2008-12-06 06:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We can add client data to copy buffer. */
|
|
|
|
/* First estimate: copy to fill rest of buffer. */
|
2009-03-05 02:19:42 +00:00
|
|
|
tocopy = (filter->buffer + filter->buffer_size)
|
|
|
|
- (filter->next + filter->avail);
|
2008-12-06 06:45:15 +00:00
|
|
|
/* Don't waste time buffering more than we need to. */
|
2009-03-05 02:19:42 +00:00
|
|
|
if (tocopy + filter->avail > min)
|
|
|
|
tocopy = min - filter->avail;
|
2008-12-06 06:45:15 +00:00
|
|
|
/* Don't copy more than is available. */
|
2009-03-05 02:19:42 +00:00
|
|
|
if (tocopy > filter->client_avail)
|
|
|
|
tocopy = filter->client_avail;
|
2008-12-06 06:45:15 +00:00
|
|
|
|
2009-03-05 02:19:42 +00:00
|
|
|
memcpy(filter->next + filter->avail, filter->client_next,
|
2008-12-06 06:45:15 +00:00
|
|
|
tocopy);
|
|
|
|
/* Remove this data from client buffer. */
|
2009-03-05 02:19:42 +00:00
|
|
|
filter->client_next += tocopy;
|
|
|
|
filter->client_avail -= tocopy;
|
2008-12-06 06:45:15 +00:00
|
|
|
/* add it to copy buffer. */
|
2009-03-05 02:19:42 +00:00
|
|
|
filter->avail += tocopy;
|
2008-12-06 06:45:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Move the file pointer forward. This should be called after
|
|
|
|
* __archive_read_ahead() returns data to you. Don't try to move
|
|
|
|
* ahead by more than the amount of data available according to
|
|
|
|
* __archive_read_ahead().
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Mark the appropriate data as used. Note that the request here will
|
|
|
|
* often be much smaller than the size of the previous read_ahead
|
|
|
|
* request.
|
|
|
|
*/
|
|
|
|
ssize_t
|
|
|
|
__archive_read_consume(struct archive_read *a, size_t request)
|
|
|
|
{
|
2009-03-05 02:19:42 +00:00
|
|
|
ssize_t r;
|
|
|
|
r = __archive_read_filter_consume(a->filter, request);
|
|
|
|
a->archive.file_position += r;
|
|
|
|
return (r);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
|
|
|
__archive_read_filter_consume(struct archive_read_filter * filter,
|
|
|
|
size_t request)
|
|
|
|
{
|
|
|
|
if (filter->avail > 0) {
|
2008-12-06 06:45:15 +00:00
|
|
|
/* Read came from copy buffer. */
|
2009-03-05 02:19:42 +00:00
|
|
|
filter->next += request;
|
|
|
|
filter->avail -= request;
|
2008-12-06 06:45:15 +00:00
|
|
|
} else {
|
|
|
|
/* Read came from client buffer. */
|
2009-03-05 02:19:42 +00:00
|
|
|
filter->client_next += request;
|
|
|
|
filter->client_avail -= request;
|
2008-12-06 06:45:15 +00:00
|
|
|
}
|
|
|
|
return (request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Move the file pointer ahead by an arbitrary amount. If you're
|
|
|
|
* reading uncompressed data from a disk file, this will actually
|
|
|
|
* translate into a seek() operation. Even in cases where seek()
|
|
|
|
* isn't feasible, this at least pushes the read-and-discard loop
|
|
|
|
* down closer to the data source.
|
|
|
|
*/
|
|
|
|
int64_t
|
|
|
|
__archive_read_skip(struct archive_read *a, int64_t request)
|
2009-03-05 02:19:42 +00:00
|
|
|
{
|
2009-04-27 18:42:02 +01:00
|
|
|
int64_t skipped = __archive_read_skip_lenient(a, request);
|
|
|
|
if (skipped == request)
|
|
|
|
return (skipped);
|
|
|
|
/* We hit EOF before we satisfied the skip request. */
|
2009-12-29 05:28:28 +00:00
|
|
|
if (skipped < 0) // Map error code to 0 for error message below.
|
|
|
|
skipped = 0;
|
2009-04-27 18:42:02 +01:00
|
|
|
archive_set_error(&a->archive,
|
|
|
|
ARCHIVE_ERRNO_MISC,
|
|
|
|
"Truncated input file (needed %jd bytes, only %jd available)",
|
|
|
|
(intmax_t)request, (intmax_t)skipped);
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t
|
|
|
|
__archive_read_skip_lenient(struct archive_read *a, int64_t request)
|
|
|
|
{
|
|
|
|
int64_t skipped = __archive_read_filter_skip(a->filter, request);
|
|
|
|
if (skipped > 0)
|
|
|
|
a->archive.file_position += skipped;
|
|
|
|
return (skipped);
|
2009-03-05 02:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t
|
|
|
|
__archive_read_filter_skip(struct archive_read_filter *filter, int64_t request)
|
2008-12-06 06:45:15 +00:00
|
|
|
{
|
2009-12-29 05:28:28 +00:00
|
|
|
int64_t bytes_skipped, total_bytes_skipped = 0;
|
2008-12-06 06:45:15 +00:00
|
|
|
size_t min;
|
|
|
|
|
2009-03-05 02:19:42 +00:00
|
|
|
if (filter->fatal)
|
2008-12-06 06:45:15 +00:00
|
|
|
return (-1);
|
|
|
|
/*
|
|
|
|
* If there is data in the buffers already, use that first.
|
|
|
|
*/
|
2009-03-05 02:19:42 +00:00
|
|
|
if (filter->avail > 0) {
|
|
|
|
min = minimum(request, (off_t)filter->avail);
|
2009-04-27 18:42:02 +01:00
|
|
|
bytes_skipped = __archive_read_filter_consume(filter, min);
|
2008-12-06 06:45:15 +00:00
|
|
|
request -= bytes_skipped;
|
|
|
|
total_bytes_skipped += bytes_skipped;
|
|
|
|
}
|
2009-03-05 02:19:42 +00:00
|
|
|
if (filter->client_avail > 0) {
|
2009-12-29 05:28:28 +00:00
|
|
|
min = minimum(request, (int64_t)filter->client_avail);
|
2009-04-27 18:42:02 +01:00
|
|
|
bytes_skipped = __archive_read_filter_consume(filter, min);
|
2008-12-06 06:45:15 +00:00
|
|
|
request -= bytes_skipped;
|
|
|
|
total_bytes_skipped += bytes_skipped;
|
|
|
|
}
|
|
|
|
if (request == 0)
|
|
|
|
return (total_bytes_skipped);
|
|
|
|
/*
|
|
|
|
* If a client_skipper was provided, try that first.
|
|
|
|
*/
|
|
|
|
#if ARCHIVE_API_VERSION < 2
|
2009-03-05 02:19:42 +00:00
|
|
|
if ((filter->skip != NULL) && (request < SSIZE_MAX)) {
|
2008-12-06 06:45:15 +00:00
|
|
|
#else
|
2009-03-05 02:19:42 +00:00
|
|
|
if (filter->skip != NULL) {
|
2008-12-06 06:45:15 +00:00
|
|
|
#endif
|
2009-03-05 02:19:42 +00:00
|
|
|
bytes_skipped = (filter->skip)(filter, request);
|
2008-12-06 06:45:15 +00:00
|
|
|
if (bytes_skipped < 0) { /* error */
|
2009-03-05 02:19:42 +00:00
|
|
|
filter->client_total = filter->client_avail = 0;
|
|
|
|
filter->client_next = filter->client_buff = NULL;
|
|
|
|
filter->fatal = 1;
|
2008-12-06 06:45:15 +00:00
|
|
|
return (bytes_skipped);
|
|
|
|
}
|
|
|
|
total_bytes_skipped += bytes_skipped;
|
|
|
|
request -= bytes_skipped;
|
2009-03-05 02:19:42 +00:00
|
|
|
filter->client_next = filter->client_buff;
|
|
|
|
filter->client_avail = filter->client_total = 0;
|
2008-12-06 06:45:15 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Note that client_skipper will usually not satisfy the
|
|
|
|
* full request (due to low-level blocking concerns),
|
|
|
|
* so even if client_skipper is provided, we may still
|
|
|
|
* have to use ordinary reads to finish out the request.
|
|
|
|
*/
|
|
|
|
while (request > 0) {
|
|
|
|
ssize_t bytes_read;
|
2009-12-29 05:28:28 +00:00
|
|
|
(void)__archive_read_filter_ahead(filter, 1, &bytes_read);
|
2008-12-06 06:45:15 +00:00
|
|
|
if (bytes_read < 0)
|
|
|
|
return (bytes_read);
|
|
|
|
if (bytes_read == 0) {
|
2009-04-27 18:42:02 +01:00
|
|
|
return (total_bytes_skipped);
|
2008-12-06 06:45:15 +00:00
|
|
|
}
|
|
|
|
min = (size_t)(minimum(bytes_read, request));
|
2009-04-27 18:42:02 +01:00
|
|
|
bytes_read = __archive_read_filter_consume(filter, min);
|
2008-12-06 06:45:15 +00:00
|
|
|
total_bytes_skipped += bytes_read;
|
|
|
|
request -= bytes_read;
|
|
|
|
}
|
|
|
|
return (total_bytes_skipped);
|
2008-01-03 17:54:26 +00:00
|
|
|
}
|