Import libarchive-2.5.5.
[dragonfly.git] / contrib / libarchive-1.3.1 / libarchive / archive_write.c
blobdd468cff5dde272d7abf9ca2301137ccfac701af
1 /*-
2 * Copyright (c) 2003-2004 Tim Kientzle
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.18 2006/09/05 05:59:46 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.
38 #include <sys/wait.h>
39 #include <limits.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <time.h>
44 #include <unistd.h>
46 #include "archive.h"
47 #include "archive_entry.h"
48 #include "archive_private.h"
50 extern char **environ;
53 * Allocate, initialize and return an archive object.
55 struct archive *
56 archive_write_new(void)
58 struct archive *a;
59 unsigned char *nulls;
61 a = malloc(sizeof(*a));
62 if (a == NULL)
63 return (NULL);
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);
75 if (nulls == NULL) {
76 free(a);
77 return (NULL);
79 memset(nulls, 0, a->null_length);
80 a->nulls = nulls;
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
85 * ever use it.
87 archive_write_set_compression_none(a);
88 return (a);
92 * Set the block size. Returns 0 if successful.
94 int
95 archive_write_set_bytes_per_block(struct archive *a, int bytes_per_block)
97 __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW, "archive_write_set_bytes_per_block");
98 a->bytes_per_block = bytes_per_block;
99 return (ARCHIVE_OK);
103 * Get the current block size. -1 if it has never been set.
106 archive_write_get_bytes_per_block(struct archive *a)
108 __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY, "archive_write_get_bytes_per_block");
109 return (a->bytes_per_block);
113 * Set the size for the last block.
114 * Returns 0 if successful.
117 archive_write_set_bytes_in_last_block(struct archive *a, int bytes)
119 __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY, "archive_write_set_bytes_in_last_block");
120 a->bytes_in_last_block = bytes;
121 return (ARCHIVE_OK);
125 * Return the value set above. -1 indicates it has not been set.
128 archive_write_get_bytes_in_last_block(struct archive *a)
130 __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY, "archive_write_get_bytes_in_last_block");
131 return (a->bytes_in_last_block);
136 * dev/ino of a file to be rejected. Used to prevent adding
137 * an archive to itself recursively.
140 archive_write_set_skip_file(struct archive *a, dev_t d, ino_t i)
142 __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY, "archive_write_set_skip_file");
143 a->skip_file_dev = d;
144 a->skip_file_ino = i;
145 return (ARCHIVE_OK);
150 * Open the archive using the current settings.
153 archive_write_open(struct archive *a, void *client_data,
154 archive_open_callback *opener, archive_write_callback *writer,
155 archive_close_callback *closer)
157 int ret;
159 ret = ARCHIVE_OK;
160 __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW, "archive_write_open");
161 archive_string_empty(&a->error_string);
162 a->state = ARCHIVE_STATE_HEADER;
163 a->client_data = client_data;
164 a->client_writer = writer;
165 a->client_opener = opener;
166 a->client_closer = closer;
167 ret = (a->compression_init)(a);
168 if (a->format_init && ret == ARCHIVE_OK)
169 ret = (a->format_init)(a);
170 return (ret);
175 * Close out the archive.
177 * Be careful: user might just call write_new and then write_finish.
178 * Don't assume we actually wrote anything or performed any non-trivial
179 * initialization.
182 archive_write_close(struct archive *a)
184 int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
186 __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY, "archive_write_close");
188 /* Finish the last entry. */
189 if (a->state & ARCHIVE_STATE_DATA)
190 r = ((a->format_finish_entry)(a));
192 /* Finish off the archive. */
193 if (a->format_finish != NULL) {
194 r1 = (a->format_finish)(a);
195 if (r1 < r)
196 r = r1;
199 /* Finish the compression and close the stream. */
200 if (a->compression_finish != NULL) {
201 r1 = (a->compression_finish)(a);
202 if (r1 < r)
203 r = r1;
206 a->state = ARCHIVE_STATE_CLOSED;
207 return (r);
211 * Destroy the archive structure.
213 #if ARCHIVE_API_VERSION > 1
215 #else
216 /* Temporarily allow library to compile with either 1.x or 2.0 API. */
217 void
218 #endif
219 archive_write_finish(struct archive *a)
221 int r = ARCHIVE_OK;
223 __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY, "archive_write_finish");
224 if (a->state != ARCHIVE_STATE_CLOSED)
225 r = archive_write_close(a);
227 /* Release various dynamic buffers. */
228 free((void *)(uintptr_t)(const void *)a->nulls);
229 archive_string_free(&a->error_string);
230 a->magic = 0;
231 free(a);
232 #if ARCHIVE_API_VERSION > 1
233 /* libarchive 1.x erroneously declares this function "void" */
234 return (r);
235 #endif
240 * Write the appropriate header.
243 archive_write_header(struct archive *a, struct archive_entry *entry)
245 int ret;
247 __archive_check_magic(a, ARCHIVE_WRITE_MAGIC,
248 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA, "archive_write_header");
249 archive_string_empty(&a->error_string);
251 /* Finish last entry. */
252 if (a->state & ARCHIVE_STATE_DATA)
253 ((a->format_finish_entry)(a));
255 if (a->skip_file_dev != 0 &&
256 archive_entry_dev(entry) == a->skip_file_dev &&
257 a->skip_file_ino != 0 &&
258 archive_entry_ino(entry) == a->skip_file_ino) {
259 archive_set_error(a, 0, "Can't add archive to itself");
260 return (ARCHIVE_WARN);
263 /* Format and write header. */
264 ret = ((a->format_write_header)(a, entry));
266 a->state = ARCHIVE_STATE_DATA;
267 return (ret);
271 * Note that the compressor is responsible for blocking.
273 #if ARCHIVE_API_VERSION > 1
274 ssize_t
275 #else
276 /* Temporarily allow library to compile with either 1.x or 2.0 API. */
278 #endif
279 archive_write_data(struct archive *a, const void *buff, size_t s)
281 __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_DATA, "archive_write_data");
282 archive_string_empty(&a->error_string);
283 return ((a->format_write_data)(a, buff, s));