2 * Copyright (c) 2003-2004 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD: src/lib/libarchive/archive_write.c,v 1.13 2004/11/05 05:26:30 kientzle Exp $");
31 * This file contains the "essential" portions of the write API, that
32 * is, stuff that will essentially always be used by any client that
33 * actually needs to write a archive. Optional pieces have been, as
34 * far as possible, separated out into separate files to reduce
35 * needlessly bloating statically-linked clients.
47 #include "archive_entry.h"
48 #include "archive_private.h"
50 extern char **environ;
53 * Allocate, initialize and return an archive object.
56 archive_write_new(void)
61 a = malloc(sizeof(*a));
64 memset(a, 0, sizeof(*a));
65 a->magic = ARCHIVE_WRITE_MAGIC;
66 a->user_uid = geteuid();
67 a->bytes_per_block = ARCHIVE_DEFAULT_BYTES_PER_BLOCK;
68 a->bytes_in_last_block = -1; /* Default */
69 a->state = ARCHIVE_STATE_NEW;
70 a->pformat_data = &(a->format_data);
72 /* Initialize a block of nulls for padding purposes. */
73 a->null_length = 1024;
74 nulls = malloc(a->null_length);
79 memset(nulls, 0, a->null_length);
82 * Set default compression, but don't set a default format.
83 * Were we to set a default format here, we would force every
84 * client to link in support for that format, even if they didn't
87 archive_write_set_compression_none(a);
93 * Set the block size. Returns 0 if successful.
96 archive_write_set_bytes_per_block(struct archive *a, int bytes_per_block)
98 archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW);
99 a->bytes_per_block = bytes_per_block;
105 * Set the size for the last block.
106 * Returns 0 if successful.
109 archive_write_set_bytes_in_last_block(struct archive *a, int bytes)
111 archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY);
112 a->bytes_in_last_block = bytes;
118 * Open the archive using the current settings.
121 archive_write_open(struct archive *a, void *client_data,
122 archive_open_callback *opener, archive_write_callback *writer,
123 archive_close_callback *closer)
128 archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW);
129 a->state = ARCHIVE_STATE_HEADER;
130 a->client_data = client_data;
131 a->client_writer = writer;
132 a->client_opener = opener;
133 a->client_closer = closer;
134 ret = (a->compression_init)(a);
135 if (a->format_init && ret == ARCHIVE_OK)
136 ret = (a->format_init)(a);
142 * Close out the archive.
144 * Be careful: user might just call write_new and then write_finish.
145 * Don't assume we actually wrote anything or performed any non-trivial
149 archive_write_close(struct archive *a)
151 archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY);
153 /* Finish the last entry. */
154 if (a->state & ARCHIVE_STATE_DATA)
155 ((a->format_finish_entry)(a));
157 /* Finish off the archive. */
158 if (a->format_finish != NULL)
159 (a->format_finish)(a);
161 /* Finish the compression and close the stream. */
162 if (a->compression_finish != NULL)
163 (a->compression_finish)(a);
165 a->state = ARCHIVE_STATE_CLOSED;
170 * Destroy the archive structure.
173 archive_write_finish(struct archive *a)
175 archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY);
176 if (a->state != ARCHIVE_STATE_CLOSED)
177 archive_write_close(a);
179 /* Release various dynamic buffers. */
180 free((void *)(uintptr_t)(const void *)a->nulls);
181 archive_string_free(&a->error_string);
188 * Write the appropriate header.
191 archive_write_header(struct archive *a, struct archive_entry *entry)
195 archive_check_magic(a, ARCHIVE_WRITE_MAGIC,
196 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA);
198 /* Finish last entry. */
199 if (a->state & ARCHIVE_STATE_DATA)
200 ((a->format_finish_entry)(a));
202 if (archive_entry_dev(entry) == a->skip_file_dev &&
203 archive_entry_ino(entry) == a->skip_file_ino) {
204 archive_set_error(a, 0, "Can't add archive to itself");
205 return (ARCHIVE_WARN);
208 /* Format and write header. */
209 ret = ((a->format_write_header)(a, entry));
211 a->state = ARCHIVE_STATE_DATA;
216 * Note that the compressor is responsible for blocking.
218 /* Should be "ssize_t", but that breaks the ABI. <sigh> */
220 archive_write_data(struct archive *a, const void *buff, size_t s)
223 archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_DATA);
224 ret = (a->format_write_data)(a, buff, s);
225 return (ret == ARCHIVE_OK ? (ssize_t)s : -1);