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.
|
|
|
|
*/
|
|
|
|
|
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_SYS_TYPES_H
|
2004-02-12 20:35:59 +00:00
|
|
|
#include <sys/types.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ERRNO_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <errno.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
|
|
|
#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
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
#include "archive.h"
|
|
|
|
#include "archive_private.h"
|
2007-03-03 07:37:37 +00:00
|
|
|
#include "archive_read_private.h"
|
|
|
|
#include "archive_write_disk_private.h"
|
2004-08-27 03:40:48 +00:00
|
|
|
|
2004-06-03 23:29:47 +00:00
|
|
|
struct extract {
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive *ad; /* archive_write_disk object */
|
2004-08-27 03:40:48 +00:00
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
/* Progress function invoked during extract. */
|
|
|
|
void (*extract_progress)(void *);
|
|
|
|
void *extract_progress_user_data;
|
2004-06-03 23:29:47 +00:00
|
|
|
};
|
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
static int archive_read_extract_cleanup(struct archive_read *);
|
|
|
|
static int copy_data(struct archive *ar, struct archive *aw);
|
|
|
|
static struct extract *get_extract(struct archive_read *);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
static struct extract *
|
|
|
|
get_extract(struct archive_read *a)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
/* If we haven't initialized, do it now. */
|
|
|
|
/* This also sets up a lot of global state. */
|
2004-06-03 23:29:47 +00:00
|
|
|
if (a->extract == NULL) {
|
2007-03-03 07:37:37 +00:00
|
|
|
a->extract = (struct extract *)malloc(sizeof(*a->extract));
|
|
|
|
if (a->extract == NULL) {
|
|
|
|
archive_set_error(&a->archive, ENOMEM, "Can't extract");
|
|
|
|
return (NULL);
|
2005-05-21 19:45:56 +00:00
|
|
|
}
|
2007-03-03 07:37:37 +00:00
|
|
|
a->extract->ad = archive_write_disk_new();
|
|
|
|
if (a->extract->ad == NULL) {
|
|
|
|
archive_set_error(&a->archive, ENOMEM, "Can't extract");
|
|
|
|
return (NULL);
|
2004-06-27 23:27:28 +00:00
|
|
|
}
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_write_disk_set_standard_lookup(a->extract->ad);
|
|
|
|
a->cleanup_archive_extract = archive_read_extract_cleanup;
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
2007-03-03 07:37:37 +00:00
|
|
|
return (a->extract);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
int
|
|
|
|
archive_read_extract(struct archive *_a, struct archive_entry *entry, int flags)
|
2006-10-04 02:08:04 +00:00
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_read *a = (struct archive_read *)_a;
|
2006-10-04 02:08:04 +00:00
|
|
|
struct extract *extract;
|
2007-03-03 07:37:37 +00:00
|
|
|
int r, r2;
|
2006-10-04 02:08:04 +00:00
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
extract = get_extract(a);
|
|
|
|
if (extract == NULL)
|
2006-10-04 02:08:04 +00:00
|
|
|
return (ARCHIVE_FATAL);
|
2004-06-03 23:29:47 +00:00
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
/* Set up for this particular entry. */
|
|
|
|
extract = a->extract;
|
|
|
|
archive_write_disk_set_options(a->extract->ad, flags);
|
|
|
|
archive_write_disk_set_skip_file(a->extract->ad,
|
|
|
|
a->skip_file_dev, a->skip_file_ino);
|
|
|
|
r = archive_write_header(a->extract->ad, entry);
|
2007-04-16 04:04:50 +00:00
|
|
|
if (r < ARCHIVE_WARN)
|
|
|
|
r = ARCHIVE_WARN;
|
2007-04-15 01:01:20 +00:00
|
|
|
if (r != ARCHIVE_OK)
|
|
|
|
/* If _write_header failed, copy the error. */
|
|
|
|
archive_set_error(&a->archive,
|
|
|
|
archive_errno(extract->ad),
|
|
|
|
"%s", archive_error_string(extract->ad));
|
|
|
|
else
|
|
|
|
/* Otherwise, pour data into the entry. */
|
2007-03-03 07:37:37 +00:00
|
|
|
r = copy_data(_a, a->extract->ad);
|
|
|
|
r2 = archive_write_finish_entry(a->extract->ad);
|
2007-04-16 04:04:50 +00:00
|
|
|
if (r2 < ARCHIVE_WARN)
|
|
|
|
r2 = ARCHIVE_WARN;
|
2007-03-03 07:37:37 +00:00
|
|
|
/* Use the first message. */
|
2007-04-15 01:01:20 +00:00
|
|
|
if (r2 != ARCHIVE_OK && r == ARCHIVE_OK)
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_set_error(&a->archive,
|
|
|
|
archive_errno(extract->ad),
|
|
|
|
"%s", archive_error_string(extract->ad));
|
|
|
|
/* Use the worst error return. */
|
|
|
|
if (r2 < r)
|
|
|
|
r = r2;
|
2004-07-08 05:24:48 +00:00
|
|
|
return (r);
|
2004-06-27 03:19:01 +00:00
|
|
|
}
|
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
void
|
|
|
|
archive_read_extract_set_progress_callback(struct archive *_a,
|
|
|
|
void (*progress_func)(void *), void *user_data)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_read *a = (struct archive_read *)_a;
|
|
|
|
struct extract *extract = get_extract(a);
|
|
|
|
if (extract != NULL) {
|
|
|
|
extract->extract_progress = progress_func;
|
|
|
|
extract->extract_progress_user_data = user_data;
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-03-03 07:37:37 +00:00
|
|
|
copy_data(struct archive *ar, struct archive *aw)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
|
|
|
int r;
|
2007-03-03 07:37:37 +00:00
|
|
|
const void *buff;
|
|
|
|
size_t size;
|
|
|
|
off_t offset;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
for (;;) {
|
|
|
|
r = archive_read_data_block(ar, &buff, &size, &offset);
|
|
|
|
if (r == ARCHIVE_EOF)
|
|
|
|
return (ARCHIVE_OK);
|
2004-06-27 23:27:28 +00:00
|
|
|
if (r != ARCHIVE_OK)
|
|
|
|
return (r);
|
2007-03-03 07:37:37 +00:00
|
|
|
r = archive_write_data_block(aw, buff, size, offset);
|
2007-04-16 04:04:50 +00:00
|
|
|
if (r < ARCHIVE_WARN)
|
|
|
|
r = ARCHIVE_WARN;
|
2007-04-07 03:37:59 +00:00
|
|
|
if (r != ARCHIVE_OK) {
|
|
|
|
archive_set_error(ar, archive_errno(aw),
|
|
|
|
"%s", archive_error_string(aw));
|
2006-03-21 16:55:46 +00:00
|
|
|
return (r);
|
2007-04-07 03:37:59 +00:00
|
|
|
}
|
2006-03-21 16:55:46 +00:00
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
2004-04-05 21:12:29 +00:00
|
|
|
|
|
|
|
/*
|
2007-03-03 07:37:37 +00:00
|
|
|
* Cleanup function for archive_extract.
|
2006-03-21 16:55:46 +00:00
|
|
|
*/
|
|
|
|
static int
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_read_extract_cleanup(struct archive_read *a)
|
2006-03-21 16:55:46 +00:00
|
|
|
{
|
|
|
|
int ret = ARCHIVE_OK;
|
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
#if ARCHIVE_API_VERSION > 1
|
|
|
|
ret =
|
2006-03-21 16:55:46 +00:00
|
|
|
#endif
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_write_finish(a->extract->ad);
|
|
|
|
free(a->extract);
|
|
|
|
a->extract = NULL;
|
2006-03-21 16:55:46 +00:00
|
|
|
return (ret);
|
|
|
|
}
|