1 /*-
2  * Copyright (c) 2003-2007 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  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_write.c,v 1.27 2008/03/14 23:09:02 kientzle Exp $");
28 
29 /*
30  * This file contains the "essential" portions of the write API, that
31  * is, stuff that will essentially always be used by any client that
32  * actually needs to write an archive.  Optional pieces have been, as
33  * far as possible, separated out into separate files to reduce
34  * needlessly bloating statically-linked clients.
35  */
36 
37 #ifdef HAVE_SYS_WAIT_H
38 #include <sys/wait.h>
39 #endif
40 #ifdef HAVE_ERRNO_H
41 #include <errno.h>
42 #endif
43 #ifdef HAVE_LIMITS_H
44 #include <limits.h>
45 #endif
46 #include <stdio.h>
47 #ifdef HAVE_STDLIB_H
48 #include <stdlib.h>
49 #endif
50 #ifdef HAVE_STRING_H
51 #include <string.h>
52 #endif
53 #include <time.h>
54 #ifdef HAVE_UNISTD_H
55 #include <unistd.h>
56 #endif
57 
58 #include "archive.h"
59 #include "archive_entry.h"
60 #include "archive_private.h"
61 #include "archive_write_private.h"
62 
63 static struct archive_vtable *archive_write_vtable(void);
64 
65 static int	_archive_write_close(struct archive *);
66 static int	_archive_write_finish(struct archive *);
67 static int	_archive_write_header(struct archive *, struct archive_entry *);
68 static int	_archive_write_finish_entry(struct archive *);
69 static ssize_t	_archive_write_data(struct archive *, const void *, size_t);
70 
71 static struct archive_vtable *
archive_write_vtable(void)72 archive_write_vtable(void)
73 {
74 	static struct archive_vtable av;
75 	static int inited = 0;
76 
77 	if (!inited) {
78 		av.archive_close = _archive_write_close;
79 		av.archive_finish = _archive_write_finish;
80 		av.archive_write_header = _archive_write_header;
81 		av.archive_write_finish_entry = _archive_write_finish_entry;
82 		av.archive_write_data = _archive_write_data;
83 		inited = 1;
84 	}
85 	return (&av);
86 }
87 
88 /*
89  * Allocate, initialize and return an archive object.
90  */
91 struct archive *
archive_write_new(void)92 archive_write_new(void)
93 {
94 	struct archive_write *a;
95 	unsigned char *nulls;
96 
97 	a = (struct archive_write *)malloc(sizeof(*a));
98 	if (a == NULL)
99 		return (NULL);
100 	memset(a, 0, sizeof(*a));
101 	a->archive.magic = ARCHIVE_WRITE_MAGIC;
102 	a->archive.state = ARCHIVE_STATE_NEW;
103 	a->archive.vtable = archive_write_vtable();
104 	/*
105 	 * The value 10240 here matches the traditional tar default,
106 	 * but is otherwise arbitrary.
107 	 * TODO: Set the default block size from the format selected.
108 	 */
109 	a->bytes_per_block = 10240;
110 	a->bytes_in_last_block = -1;	/* Default */
111 
112 	/* Initialize a block of nulls for padding purposes. */
113 	a->null_length = 1024;
114 	nulls = (unsigned char *)malloc(a->null_length);
115 	if (nulls == NULL) {
116 		free(a);
117 		return (NULL);
118 	}
119 	memset(nulls, 0, a->null_length);
120 	a->nulls = nulls;
121 	/*
122 	 * Set default compression, but don't set a default format.
123 	 * Were we to set a default format here, we would force every
124 	 * client to link in support for that format, even if they didn't
125 	 * ever use it.
126 	 */
127 	archive_write_set_compression_none(&a->archive);
128 	return (&a->archive);
129 }
130 
131 /*
132  * Set write options for the format. Returns 0 if successful.
133  */
134 int
archive_write_set_format_options(struct archive * _a,const char * s)135 archive_write_set_format_options(struct archive *_a, const char *s)
136 {
137 	struct archive_write *a = (struct archive_write *)_a;
138 	char key[64], val[64];
139 	int len, r, ret = ARCHIVE_OK;
140 
141 	__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
142 	    ARCHIVE_STATE_NEW, "archive_write_set_format_options");
143 	archive_clear_error(&a->archive);
144 
145 	if (s == NULL || *s == '\0')
146 		return (ARCHIVE_OK);
147 	if (a->format_options == NULL)
148 		/* This format does not support option. */
149 		return (ARCHIVE_OK);
150 
151 	while ((len = __archive_parse_options(s, a->format_name,
152 	    sizeof(key), key, sizeof(val), val)) > 0) {
153 		if (val[0] == '\0')
154 			r = a->format_options(a, key, NULL);
155 		else
156 			r = a->format_options(a, key, val);
157 		if (r == ARCHIVE_FATAL)
158 			return (r);
159 		if (r < ARCHIVE_OK) { /* This key was not handled. */
160 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
161 			    "Unsupported option ``%s''", key);
162 			ret = ARCHIVE_WARN;
163 		}
164 		s += len;
165 	}
166 	if (len < 0) {
167 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
168 		    "Malformed options string.");
169 		return (ARCHIVE_WARN);
170 	}
171 	return (ret);
172 }
173 
174 /*
175  * Set write options for the compressor. Returns 0 if successful.
176  */
177 int
archive_write_set_compressor_options(struct archive * _a,const char * s)178 archive_write_set_compressor_options(struct archive *_a, const char *s)
179 {
180 	struct archive_write *a = (struct archive_write *)_a;
181 	char key[64], val[64];
182 	int len, r;
183 	int ret = ARCHIVE_OK;
184 
185 	__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
186 	    ARCHIVE_STATE_NEW, "archive_write_set_compressor_options");
187 	archive_clear_error(&a->archive);
188 
189 	if (s == NULL || *s == '\0')
190 		return (ARCHIVE_OK);
191 	if (a->compressor.options == NULL) {
192 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
193 		    "Unsupported option ``%s''", s);
194 		/* This compressor does not support option. */
195 		return (ARCHIVE_WARN);
196 	}
197 
198 	while ((len = __archive_parse_options(s, a->archive.compression_name,
199 	    sizeof(key), key, sizeof(val), val)) > 0) {
200 		if (val[0] == '\0')
201 			r = a->compressor.options(a, key, NULL);
202 		else
203 			r = a->compressor.options(a, key, val);
204 		if (r == ARCHIVE_FATAL)
205 			return (r);
206 		if (r < ARCHIVE_OK) {
207 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
208 			    "Unsupported option ``%s''", key);
209 			ret = ARCHIVE_WARN;
210 		}
211 		s += len;
212 	}
213 	if (len < 0) {
214 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
215 		    "Illegal format options.");
216 		return (ARCHIVE_WARN);
217 	}
218 	return (ret);
219 }
220 
221 /*
222  * Set write options for the format and the compressor. Returns 0 if successful.
223  */
224 int
archive_write_set_options(struct archive * _a,const char * s)225 archive_write_set_options(struct archive *_a, const char *s)
226 {
227 	int r1, r2;
228 
229 	r1 = archive_write_set_format_options(_a, s);
230 	if (r1 < ARCHIVE_WARN)
231 		return (r1);
232 	r2 = archive_write_set_compressor_options(_a, s);
233 	if (r2 < ARCHIVE_WARN)
234 		return (r2);
235 	if (r1 == ARCHIVE_WARN && r2 == ARCHIVE_WARN)
236 		return (ARCHIVE_WARN);
237 	return (ARCHIVE_OK);
238 }
239 
240 /*
241  * Set the block size.  Returns 0 if successful.
242  */
243 int
archive_write_set_bytes_per_block(struct archive * _a,int bytes_per_block)244 archive_write_set_bytes_per_block(struct archive *_a, int bytes_per_block)
245 {
246 	struct archive_write *a = (struct archive_write *)_a;
247 	__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
248 	    ARCHIVE_STATE_NEW, "archive_write_set_bytes_per_block");
249 	a->bytes_per_block = bytes_per_block;
250 	return (ARCHIVE_OK);
251 }
252 
253 /*
254  * Get the current block size.  -1 if it has never been set.
255  */
256 int
archive_write_get_bytes_per_block(struct archive * _a)257 archive_write_get_bytes_per_block(struct archive *_a)
258 {
259 	struct archive_write *a = (struct archive_write *)_a;
260 	__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
261 	    ARCHIVE_STATE_ANY, "archive_write_get_bytes_per_block");
262 	return (a->bytes_per_block);
263 }
264 
265 /*
266  * Set the size for the last block.
267  * Returns 0 if successful.
268  */
269 int
archive_write_set_bytes_in_last_block(struct archive * _a,int bytes)270 archive_write_set_bytes_in_last_block(struct archive *_a, int bytes)
271 {
272 	struct archive_write *a = (struct archive_write *)_a;
273 	__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
274 	    ARCHIVE_STATE_ANY, "archive_write_set_bytes_in_last_block");
275 	a->bytes_in_last_block = bytes;
276 	return (ARCHIVE_OK);
277 }
278 
279 /*
280  * Return the value set above.  -1 indicates it has not been set.
281  */
282 int
archive_write_get_bytes_in_last_block(struct archive * _a)283 archive_write_get_bytes_in_last_block(struct archive *_a)
284 {
285 	struct archive_write *a = (struct archive_write *)_a;
286 	__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
287 	    ARCHIVE_STATE_ANY, "archive_write_get_bytes_in_last_block");
288 	return (a->bytes_in_last_block);
289 }
290 
291 
292 /*
293  * dev/ino of a file to be rejected.  Used to prevent adding
294  * an archive to itself recursively.
295  */
296 int
archive_write_set_skip_file(struct archive * _a,dev_t d,ino_t i)297 archive_write_set_skip_file(struct archive *_a, dev_t d, ino_t i)
298 {
299 	struct archive_write *a = (struct archive_write *)_a;
300 	__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
301 	    ARCHIVE_STATE_ANY, "archive_write_set_skip_file");
302 	a->skip_file_dev = d;
303 	a->skip_file_ino = i;
304 	return (ARCHIVE_OK);
305 }
306 
307 
308 /*
309  * Open the archive using the current settings.
310  */
311 int
archive_write_open(struct archive * _a,void * client_data,archive_open_callback * opener,archive_write_callback * writer,archive_close_callback * closer)312 archive_write_open(struct archive *_a, void *client_data,
313     archive_open_callback *opener, archive_write_callback *writer,
314     archive_close_callback *closer)
315 {
316 	struct archive_write *a = (struct archive_write *)_a;
317 	int ret;
318 
319 	__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
320 	    ARCHIVE_STATE_NEW, "archive_write_open");
321 	archive_clear_error(&a->archive);
322 	a->archive.state = ARCHIVE_STATE_HEADER;
323 	a->client_data = client_data;
324 	a->client_writer = writer;
325 	a->client_opener = opener;
326 	a->client_closer = closer;
327 	ret = (a->compressor.init)(a);
328 	if (a->format_init && ret == ARCHIVE_OK)
329 		ret = (a->format_init)(a);
330 	return (ret);
331 }
332 
333 
334 /*
335  * Close out the archive.
336  *
337  * Be careful: user might just call write_new and then write_finish.
338  * Don't assume we actually wrote anything or performed any non-trivial
339  * initialization.
340  */
341 static int
_archive_write_close(struct archive * _a)342 _archive_write_close(struct archive *_a)
343 {
344 	struct archive_write *a = (struct archive_write *)_a;
345 	int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
346 
347 	__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
348 	    ARCHIVE_STATE_ANY, "archive_write_close");
349 
350 	/* Finish the last entry. */
351 	if (a->archive.state & ARCHIVE_STATE_DATA)
352 		r = ((a->format_finish_entry)(a));
353 
354 	/* Finish off the archive. */
355 	if (a->format_finish != NULL) {
356 		r1 = (a->format_finish)(a);
357 		if (r1 < r)
358 			r = r1;
359 	}
360 
361 	/* Release format resources. */
362 	if (a->format_destroy != NULL) {
363 		r1 = (a->format_destroy)(a);
364 		if (r1 < r)
365 			r = r1;
366 	}
367 
368 	/* Finish the compression and close the stream. */
369 	if (a->compressor.finish != NULL) {
370 		r1 = (a->compressor.finish)(a);
371 		if (r1 < r)
372 			r = r1;
373 	}
374 
375 	/* Close out the client stream. */
376 	if (a->client_closer != NULL) {
377 		r1 = (a->client_closer)(&a->archive, a->client_data);
378 		if (r1 < r)
379 			r = r1;
380 	}
381 
382 	a->archive.state = ARCHIVE_STATE_CLOSED;
383 	return (r);
384 }
385 
386 /*
387  * Destroy the archive structure.
388  */
389 static int
_archive_write_finish(struct archive * _a)390 _archive_write_finish(struct archive *_a)
391 {
392 	struct archive_write *a = (struct archive_write *)_a;
393 	int r = ARCHIVE_OK;
394 
395 	__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
396 	    ARCHIVE_STATE_ANY, "archive_write_finish");
397 	if (a->archive.state != ARCHIVE_STATE_CLOSED)
398 		r = archive_write_close(&a->archive);
399 
400 	/* Release various dynamic buffers. */
401 	free((void *)(uintptr_t)(const void *)a->nulls);
402 	archive_string_free(&a->archive.error_string);
403 	a->archive.magic = 0;
404 	free(a);
405 	return (r);
406 }
407 
408 /*
409  * Write the appropriate header.
410  */
411 static int
_archive_write_header(struct archive * _a,struct archive_entry * entry)412 _archive_write_header(struct archive *_a, struct archive_entry *entry)
413 {
414 	struct archive_write *a = (struct archive_write *)_a;
415 	int ret, r2;
416 
417 	__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
418 	    ARCHIVE_STATE_DATA | ARCHIVE_STATE_HEADER, "archive_write_header");
419 	archive_clear_error(&a->archive);
420 
421 	/* In particular, "retry" and "fatal" get returned immediately. */
422 	ret = archive_write_finish_entry(&a->archive);
423 	if (ret < ARCHIVE_OK && ret != ARCHIVE_WARN)
424 		return (ret);
425 
426 	if (a->skip_file_dev != 0 &&
427 	    archive_entry_dev(entry) == a->skip_file_dev &&
428 	    a->skip_file_ino != 0 &&
429 	    archive_entry_ino(entry) == a->skip_file_ino) {
430 		archive_set_error(&a->archive, 0,
431 		    "Can't add archive to itself");
432 		return (ARCHIVE_FAILED);
433 	}
434 
435 	/* Format and write header. */
436 	r2 = ((a->format_write_header)(a, entry));
437 	if (r2 < ret)
438 		ret = r2;
439 
440 	a->archive.state = ARCHIVE_STATE_DATA;
441 	return (ret);
442 }
443 
444 static int
_archive_write_finish_entry(struct archive * _a)445 _archive_write_finish_entry(struct archive *_a)
446 {
447 	struct archive_write *a = (struct archive_write *)_a;
448 	int ret = ARCHIVE_OK;
449 
450 	__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
451 	    ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
452 	    "archive_write_finish_entry");
453 	if (a->archive.state & ARCHIVE_STATE_DATA)
454 		ret = (a->format_finish_entry)(a);
455 	a->archive.state = ARCHIVE_STATE_HEADER;
456 	return (ret);
457 }
458 
459 /*
460  * Note that the compressor is responsible for blocking.
461  */
462 static ssize_t
_archive_write_data(struct archive * _a,const void * buff,size_t s)463 _archive_write_data(struct archive *_a, const void *buff, size_t s)
464 {
465 	struct archive_write *a = (struct archive_write *)_a;
466 	__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
467 	    ARCHIVE_STATE_DATA, "archive_write_data");
468 	archive_clear_error(&a->archive);
469 	return ((a->format_write_data)(a, buff, s));
470 }
471 
472 int
archive_write_skip(struct archive * _a,off_t s)473 archive_write_skip(struct archive *_a, off_t s)
474 {
475 	struct archive_write *a = (struct archive_write *)_a;
476 	__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
477 	    ARCHIVE_STATE_DATA, "archive_write_skip");
478 	archive_clear_error(&a->archive);
479 	if (a->format_skip_data == NULL) {
480 		archive_set_error(&a->archive, ENOSYS,
481 		    "No format skip handler registered");
482 		return (ARCHIVE_FATAL);
483 	}
484 
485 	/* Adjust raw position. */
486 	a->archive.raw_position += s;
487 
488 	return ((a->format_skip_data)(a, s));
489 }
490