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$");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file contains the "essential" portions of the write API, that
|
|
|
|
* is, stuff that will essentially always be used by any client that
|
|
|
|
* actually needs to write a archive. Optional pieces have been, as
|
|
|
|
* far as possible, separated out into separate files to reduce
|
|
|
|
* needlessly bloating statically-linked clients.
|
|
|
|
*/
|
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
#ifdef HAVE_SYS_WAIT_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <sys/wait.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LIMITS_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <limits.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
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <time.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#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"
|
2004-06-27 18:38:13 +00:00
|
|
|
#include "archive_entry.h"
|
2004-02-09 23:22:54 +00:00
|
|
|
#include "archive_private.h"
|
2007-03-03 07:37:37 +00:00
|
|
|
#include "archive_write_private.h"
|
|
|
|
|
|
|
|
static struct archive_vtable *archive_write_vtable(void);
|
|
|
|
|
|
|
|
static int _archive_write_close(struct archive *);
|
|
|
|
static int _archive_write_finish(struct archive *);
|
|
|
|
static int _archive_write_header(struct archive *, struct archive_entry *);
|
|
|
|
static int _archive_write_finish_entry(struct archive *);
|
|
|
|
static ssize_t _archive_write_data(struct archive *, const void *, size_t);
|
|
|
|
|
|
|
|
static struct archive_vtable *
|
|
|
|
archive_write_vtable(void)
|
|
|
|
{
|
|
|
|
static struct archive_vtable av;
|
|
|
|
static int inited = 0;
|
|
|
|
|
|
|
|
if (!inited) {
|
|
|
|
av.archive_write_close = _archive_write_close;
|
|
|
|
av.archive_write_finish = _archive_write_finish;
|
|
|
|
av.archive_write_header = _archive_write_header;
|
|
|
|
av.archive_write_finish_entry = _archive_write_finish_entry;
|
|
|
|
av.archive_write_data = _archive_write_data;
|
|
|
|
}
|
|
|
|
return (&av);
|
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate, initialize and return an archive object.
|
|
|
|
*/
|
|
|
|
struct archive *
|
|
|
|
archive_write_new(void)
|
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_write *a;
|
2005-09-24 21:15:00 +00:00
|
|
|
unsigned char *nulls;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
a = (struct archive_write *)malloc(sizeof(*a));
|
2004-02-09 23:22:54 +00:00
|
|
|
if (a == NULL)
|
|
|
|
return (NULL);
|
|
|
|
memset(a, 0, sizeof(*a));
|
2007-03-03 07:37:37 +00:00
|
|
|
a->archive.magic = ARCHIVE_WRITE_MAGIC;
|
|
|
|
a->archive.state = ARCHIVE_STATE_NEW;
|
|
|
|
a->archive.vtable = archive_write_vtable();
|
2004-02-09 23:22:54 +00:00
|
|
|
a->bytes_per_block = ARCHIVE_DEFAULT_BYTES_PER_BLOCK;
|
|
|
|
a->bytes_in_last_block = -1; /* Default */
|
|
|
|
|
|
|
|
/* Initialize a block of nulls for padding purposes. */
|
|
|
|
a->null_length = 1024;
|
2006-11-10 06:39:46 +00:00
|
|
|
nulls = (unsigned char *)malloc(a->null_length);
|
2004-02-09 23:22:54 +00:00
|
|
|
if (nulls == NULL) {
|
|
|
|
free(a);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
memset(nulls, 0, a->null_length);
|
|
|
|
a->nulls = nulls;
|
|
|
|
/*
|
|
|
|
* Set default compression, but don't set a default format.
|
|
|
|
* Were we to set a default format here, we would force every
|
|
|
|
* client to link in support for that format, even if they didn't
|
|
|
|
* ever use it.
|
|
|
|
*/
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_write_set_compression_none(&a->archive);
|
|
|
|
return (&a->archive);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the block size. Returns 0 if successful.
|
|
|
|
*/
|
|
|
|
int
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_write_set_bytes_per_block(struct archive *_a, int bytes_per_block)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_write *a = (struct archive_write *)_a;
|
|
|
|
__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
|
|
|
|
ARCHIVE_STATE_NEW, "archive_write_set_bytes_per_block");
|
2004-02-09 23:22:54 +00:00
|
|
|
a->bytes_per_block = bytes_per_block;
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
}
|
|
|
|
|
2006-09-05 05:59:46 +00:00
|
|
|
/*
|
|
|
|
* Get the current block size. -1 if it has never been set.
|
|
|
|
*/
|
|
|
|
int
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_write_get_bytes_per_block(struct archive *_a)
|
2006-09-05 05:59:46 +00:00
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_write *a = (struct archive_write *)_a;
|
|
|
|
__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
|
|
|
|
ARCHIVE_STATE_ANY, "archive_write_get_bytes_per_block");
|
2006-09-05 05:59:46 +00:00
|
|
|
return (a->bytes_per_block);
|
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the size for the last block.
|
|
|
|
* Returns 0 if successful.
|
|
|
|
*/
|
|
|
|
int
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_write_set_bytes_in_last_block(struct archive *_a, int bytes)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_write *a = (struct archive_write *)_a;
|
|
|
|
__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
|
|
|
|
ARCHIVE_STATE_ANY, "archive_write_set_bytes_in_last_block");
|
2004-02-09 23:22:54 +00:00
|
|
|
a->bytes_in_last_block = bytes;
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
}
|
|
|
|
|
2006-09-05 05:59:46 +00:00
|
|
|
/*
|
|
|
|
* Return the value set above. -1 indicates it has not been set.
|
|
|
|
*/
|
|
|
|
int
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_write_get_bytes_in_last_block(struct archive *_a)
|
2006-09-05 05:59:46 +00:00
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_write *a = (struct archive_write *)_a;
|
|
|
|
__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
|
|
|
|
ARCHIVE_STATE_ANY, "archive_write_get_bytes_in_last_block");
|
2006-09-05 05:59:46 +00:00
|
|
|
return (a->bytes_in_last_block);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dev/ino of a file to be rejected. Used to prevent adding
|
|
|
|
* an archive to itself recursively.
|
|
|
|
*/
|
|
|
|
int
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_write_set_skip_file(struct archive *_a, dev_t d, ino_t i)
|
2006-09-05 05:59:46 +00:00
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_write *a = (struct archive_write *)_a;
|
|
|
|
__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
|
|
|
|
ARCHIVE_STATE_ANY, "archive_write_set_skip_file");
|
2006-09-05 05:59:46 +00:00
|
|
|
a->skip_file_dev = d;
|
|
|
|
a->skip_file_ino = i;
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
}
|
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the archive using the current settings.
|
|
|
|
*/
|
|
|
|
int
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_write_open(struct archive *_a, void *client_data,
|
2004-02-09 23:22:54 +00:00
|
|
|
archive_open_callback *opener, archive_write_callback *writer,
|
|
|
|
archive_close_callback *closer)
|
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_write *a = (struct archive_write *)_a;
|
2004-02-09 23:22:54 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ARCHIVE_OK;
|
2007-03-03 07:37:37 +00:00
|
|
|
__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
|
|
|
|
ARCHIVE_STATE_NEW, "archive_write_open");
|
|
|
|
archive_clear_error(&a->archive);
|
|
|
|
a->archive.state = ARCHIVE_STATE_HEADER;
|
2004-02-09 23:22:54 +00:00
|
|
|
a->client_data = client_data;
|
|
|
|
a->client_writer = writer;
|
|
|
|
a->client_opener = opener;
|
|
|
|
a->client_closer = closer;
|
2007-05-29 01:00:21 +00:00
|
|
|
ret = (a->compressor.init)(a);
|
2004-02-09 23:22:54 +00:00
|
|
|
if (a->format_init && ret == ARCHIVE_OK)
|
|
|
|
ret = (a->format_init)(a);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2004-08-07 19:22:50 +00:00
|
|
|
* Close out the archive.
|
2004-02-09 23:22:54 +00:00
|
|
|
*
|
|
|
|
* Be careful: user might just call write_new and then write_finish.
|
|
|
|
* Don't assume we actually wrote anything or performed any non-trivial
|
|
|
|
* initialization.
|
|
|
|
*/
|
2007-03-03 07:37:37 +00:00
|
|
|
static int
|
|
|
|
_archive_write_close(struct archive *_a)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_write *a = (struct archive_write *)_a;
|
2006-09-05 05:59:46 +00:00
|
|
|
int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
|
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
|
|
|
|
ARCHIVE_STATE_ANY, "archive_write_close");
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/* Finish the last entry. */
|
2007-03-03 07:37:37 +00:00
|
|
|
if (a->archive.state & ARCHIVE_STATE_DATA)
|
2006-09-05 05:59:46 +00:00
|
|
|
r = ((a->format_finish_entry)(a));
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/* Finish off the archive. */
|
2006-09-05 05:59:46 +00:00
|
|
|
if (a->format_finish != NULL) {
|
|
|
|
r1 = (a->format_finish)(a);
|
|
|
|
if (r1 < r)
|
|
|
|
r = r1;
|
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2007-05-29 01:00:21 +00:00
|
|
|
/* Release format resources. */
|
2007-03-03 07:37:37 +00:00
|
|
|
if (a->format_destroy != NULL) {
|
|
|
|
r1 = (a->format_destroy)(a);
|
|
|
|
if (r1 < r)
|
|
|
|
r = r1;
|
|
|
|
}
|
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
/* Finish the compression and close the stream. */
|
2007-05-29 01:00:21 +00:00
|
|
|
if (a->compressor.finish != NULL) {
|
|
|
|
r1 = (a->compressor.finish)(a);
|
|
|
|
if (r1 < r)
|
|
|
|
r = r1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close out the client stream. */
|
|
|
|
if (a->client_closer != NULL) {
|
|
|
|
r1 = (a->client_closer)(&a->archive, a->client_data);
|
2006-09-05 05:59:46 +00:00
|
|
|
if (r1 < r)
|
|
|
|
r = r1;
|
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
a->archive.state = ARCHIVE_STATE_CLOSED;
|
2006-09-05 05:59:46 +00:00
|
|
|
return (r);
|
2004-08-07 19:22:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Destroy the archive structure.
|
|
|
|
*/
|
2007-03-03 07:37:37 +00:00
|
|
|
static int
|
|
|
|
_archive_write_finish(struct archive *_a)
|
2004-08-07 19:22:50 +00:00
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_write *a = (struct archive_write *)_a;
|
|
|
|
int r = ARCHIVE_OK;
|
|
|
|
|
|
|
|
__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
|
|
|
|
ARCHIVE_STATE_ANY, "archive_write_finish");
|
|
|
|
if (a->archive.state != ARCHIVE_STATE_CLOSED)
|
|
|
|
r = archive_write_close(&a->archive);
|
2004-08-07 19:22:50 +00:00
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
/* Release various dynamic buffers. */
|
|
|
|
free((void *)(uintptr_t)(const void *)a->nulls);
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_string_free(&a->archive.error_string);
|
|
|
|
a->archive.magic = 0;
|
2004-02-09 23:22:54 +00:00
|
|
|
free(a);
|
2007-03-03 07:37:37 +00:00
|
|
|
return (r);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write the appropriate header.
|
|
|
|
*/
|
2007-03-03 07:37:37 +00:00
|
|
|
static int
|
|
|
|
_archive_write_header(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_write *a = (struct archive_write *)_a;
|
2006-11-26 19:00:50 +00:00
|
|
|
int ret, r2;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
|
2006-11-26 19:00:50 +00:00
|
|
|
ARCHIVE_STATE_DATA | ARCHIVE_STATE_HEADER, "archive_write_header");
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_clear_error(&a->archive);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2006-11-26 19:00:50 +00:00
|
|
|
/* In particular, "retry" and "fatal" get returned immediately. */
|
2007-03-03 07:37:37 +00:00
|
|
|
ret = archive_write_finish_entry(&a->archive);
|
2006-11-26 19:00:50 +00:00
|
|
|
if (ret < ARCHIVE_OK && ret != ARCHIVE_WARN)
|
|
|
|
return (ret);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2006-08-01 05:31:29 +00:00
|
|
|
if (a->skip_file_dev != 0 &&
|
|
|
|
archive_entry_dev(entry) == a->skip_file_dev &&
|
|
|
|
a->skip_file_ino != 0 &&
|
2004-06-27 18:38:13 +00:00
|
|
|
archive_entry_ino(entry) == a->skip_file_ino) {
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_set_error(&a->archive, 0,
|
|
|
|
"Can't add archive to itself");
|
2007-03-11 10:29:52 +00:00
|
|
|
return (ARCHIVE_FAILED);
|
2004-06-27 18:38:13 +00:00
|
|
|
}
|
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
/* Format and write header. */
|
2006-11-26 19:00:50 +00:00
|
|
|
r2 = ((a->format_write_header)(a, entry));
|
|
|
|
if (r2 < ret)
|
|
|
|
ret = r2;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
a->archive.state = ARCHIVE_STATE_DATA;
|
2004-02-09 23:22:54 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
static int
|
|
|
|
_archive_write_finish_entry(struct archive *_a)
|
2006-11-26 19:00:50 +00:00
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_write *a = (struct archive_write *)_a;
|
2006-11-26 19:00:50 +00:00
|
|
|
int ret = ARCHIVE_OK;
|
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
|
2006-11-26 19:00:50 +00:00
|
|
|
ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
|
|
|
|
"archive_write_finish_entry");
|
2007-03-03 07:37:37 +00:00
|
|
|
if (a->archive.state & ARCHIVE_STATE_DATA)
|
2006-11-26 19:00:50 +00:00
|
|
|
ret = (a->format_finish_entry)(a);
|
2007-03-03 07:37:37 +00:00
|
|
|
a->archive.state = ARCHIVE_STATE_HEADER;
|
2006-11-26 19:00:50 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
/*
|
|
|
|
* Note that the compressor is responsible for blocking.
|
|
|
|
*/
|
2007-03-03 07:37:37 +00:00
|
|
|
static ssize_t
|
|
|
|
_archive_write_data(struct archive *_a, const void *buff, size_t s)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_write *a = (struct archive_write *)_a;
|
|
|
|
__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
|
2006-11-26 19:00:50 +00:00
|
|
|
ARCHIVE_STATE_DATA, "archive_write_data");
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_clear_error(&a->archive);
|
2006-11-15 05:14:20 +00:00
|
|
|
return ((a->format_write_data)(a, buff, s));
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|