Merge from vendor branch CVS:
[dragonfly.git] / contrib / libarchive / archive_write.c
1 /*-
2  * Copyright (c) 2003-2004 Tim Kientzle
3  * All rights reserved.
4  *
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.
14  *
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.
25  */
26
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 $");
29
30 /*
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.
36  */
37
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>
45
46 #include "archive.h"
47 #include "archive_entry.h"
48 #include "archive_private.h"
49
50 extern char             **environ;
51
52 /*
53  * Allocate, initialize and return an archive object.
54  */
55 struct archive *
56 archive_write_new(void)
57 {
58         struct archive *a;
59         char *nulls;
60
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);
71
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);
78         }
79         memset(nulls, 0, a->null_length);
80         a->nulls = nulls;
81         /*
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.
86          */
87         archive_write_set_compression_none(a);
88         return (a);
89 }
90
91
92 /*
93  * Set the block size.  Returns 0 if successful.
94  */
95 int
96 archive_write_set_bytes_per_block(struct archive *a, int bytes_per_block)
97 {
98         archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW);
99         a->bytes_per_block = bytes_per_block;
100         return (ARCHIVE_OK);
101 }
102
103
104 /*
105  * Set the size for the last block.
106  * Returns 0 if successful.
107  */
108 int
109 archive_write_set_bytes_in_last_block(struct archive *a, int bytes)
110 {
111         archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY);
112         a->bytes_in_last_block = bytes;
113         return (ARCHIVE_OK);
114 }
115
116
117 /*
118  * Open the archive using the current settings.
119  */
120 int
121 archive_write_open(struct archive *a, void *client_data,
122     archive_open_callback *opener, archive_write_callback *writer,
123     archive_close_callback *closer)
124 {
125         int ret;
126
127         ret = ARCHIVE_OK;
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);
137         return (ret);
138 }
139
140
141 /*
142  * Close out the archive.
143  *
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
146  * initialization.
147  */
148 int
149 archive_write_close(struct archive *a)
150 {
151         archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY);
152
153         /* Finish the last entry. */
154         if (a->state & ARCHIVE_STATE_DATA)
155                 ((a->format_finish_entry)(a));
156
157         /* Finish off the archive. */
158         if (a->format_finish != NULL)
159                 (a->format_finish)(a);
160
161         /* Finish the compression and close the stream. */
162         if (a->compression_finish != NULL)
163                 (a->compression_finish)(a);
164
165         a->state = ARCHIVE_STATE_CLOSED;
166         return (ARCHIVE_OK);
167 }
168
169 /*
170  * Destroy the archive structure.
171  */
172 void
173 archive_write_finish(struct archive *a)
174 {
175         archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY);
176         if (a->state != ARCHIVE_STATE_CLOSED)
177                 archive_write_close(a);
178
179         /* Release various dynamic buffers. */
180         free((void *)(uintptr_t)(const void *)a->nulls);
181         archive_string_free(&a->error_string);
182         a->magic = 0;
183         free(a);
184 }
185
186
187 /*
188  * Write the appropriate header.
189  */
190 int
191 archive_write_header(struct archive *a, struct archive_entry *entry)
192 {
193         int ret;
194
195         archive_check_magic(a, ARCHIVE_WRITE_MAGIC,
196             ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA);
197
198         /* Finish last entry. */
199         if (a->state & ARCHIVE_STATE_DATA)
200                 ((a->format_finish_entry)(a));
201
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);
206         }
207
208         /* Format and write header. */
209         ret = ((a->format_write_header)(a, entry));
210
211         a->state = ARCHIVE_STATE_DATA;
212         return (ret);
213 }
214
215 /*
216  * Note that the compressor is responsible for blocking.
217  */
218 /* Should be "ssize_t", but that breaks the ABI.  <sigh> */
219 int
220 archive_write_data(struct archive *a, const void *buff, size_t s)
221 {
222         int ret;
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);
226 }