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.14 2005/02/12 23:11:29 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 archive_string_empty(&a->error_string);
130 a->state = ARCHIVE_STATE_HEADER;
131 a->client_data = client_data;
132 a->client_writer = writer;
133 a->client_opener = opener;
134 a->client_closer = closer;
135 ret = (a->compression_init)(a);
136 if (a->format_init && ret == ARCHIVE_OK)
137 ret = (a->format_init)(a);
143 * Close out the archive.
145 * Be careful: user might just call write_new and then write_finish.
146 * Don't assume we actually wrote anything or performed any non-trivial
150 archive_write_close(struct archive *a)
152 archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY);
154 /* Finish the last entry. */
155 if (a->state & ARCHIVE_STATE_DATA)
156 ((a->format_finish_entry)(a));
158 /* Finish off the archive. */
159 if (a->format_finish != NULL)
160 (a->format_finish)(a);
162 /* Finish the compression and close the stream. */
163 if (a->compression_finish != NULL)
164 (a->compression_finish)(a);
166 a->state = ARCHIVE_STATE_CLOSED;
171 * Destroy the archive structure.
174 archive_write_finish(struct archive *a)
176 archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY);
177 if (a->state != ARCHIVE_STATE_CLOSED)
178 archive_write_close(a);
180 /* Release various dynamic buffers. */
181 free((void *)(uintptr_t)(const void *)a->nulls);
182 archive_string_free(&a->error_string);
189 * Write the appropriate header.
192 archive_write_header(struct archive *a, struct archive_entry *entry)
196 archive_check_magic(a, ARCHIVE_WRITE_MAGIC,
197 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA);
198 archive_string_empty(&a->error_string);
200 /* Finish last entry. */
201 if (a->state & ARCHIVE_STATE_DATA)
202 ((a->format_finish_entry)(a));
204 if (archive_entry_dev(entry) == a->skip_file_dev &&
205 archive_entry_ino(entry) == a->skip_file_ino) {
206 archive_set_error(a, 0, "Can't add archive to itself");
207 return (ARCHIVE_WARN);
210 /* Format and write header. */
211 ret = ((a->format_write_header)(a, entry));
213 a->state = ARCHIVE_STATE_DATA;
218 * Note that the compressor is responsible for blocking.
220 /* Should be "ssize_t", but that breaks the ABI. <sigh> */
222 archive_write_data(struct archive *a, const void *buff, size_t s)
225 archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_DATA);
226 archive_string_empty(&a->error_string);
227 ret = (a->format_write_data)(a, buff, s);
228 return (ret == ARCHIVE_OK ? (ssize_t)s : -1);