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