write.c revision 1.4 1 1.1 joerg /*-
2 1.1 joerg * Copyright (c) 2003-2007 Tim Kientzle
3 1.2 christos * Copyright (c) 2012 Michihiro NAKAJIMA
4 1.1 joerg * All rights reserved.
5 1.1 joerg *
6 1.1 joerg * Redistribution and use in source and binary forms, with or without
7 1.1 joerg * modification, are permitted provided that the following conditions
8 1.1 joerg * are met:
9 1.1 joerg * 1. Redistributions of source code must retain the above copyright
10 1.1 joerg * notice, this list of conditions and the following disclaimer.
11 1.1 joerg * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 joerg * notice, this list of conditions and the following disclaimer in the
13 1.1 joerg * documentation and/or other materials provided with the distribution.
14 1.1 joerg *
15 1.1 joerg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 1.1 joerg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 joerg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 joerg * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 joerg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 1.1 joerg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 1.1 joerg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 1.1 joerg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 1.1 joerg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 1.1 joerg * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 1.1 joerg */
26 1.1 joerg
27 1.1 joerg #include "bsdtar_platform.h"
28 1.1 joerg
29 1.1 joerg #ifdef HAVE_SYS_TYPES_H
30 1.1 joerg #include <sys/types.h>
31 1.1 joerg #endif
32 1.1 joerg #ifdef HAVE_SYS_STAT_H
33 1.1 joerg #include <sys/stat.h>
34 1.1 joerg #endif
35 1.1 joerg #ifdef HAVE_ATTR_XATTR_H
36 1.1 joerg #include <attr/xattr.h>
37 1.1 joerg #endif
38 1.1 joerg #ifdef HAVE_ERRNO_H
39 1.1 joerg #include <errno.h>
40 1.1 joerg #endif
41 1.1 joerg #ifdef HAVE_FCNTL_H
42 1.1 joerg #include <fcntl.h>
43 1.1 joerg #endif
44 1.1 joerg #ifdef HAVE_GRP_H
45 1.1 joerg #include <grp.h>
46 1.1 joerg #endif
47 1.2 christos #ifdef HAVE_IO_H
48 1.2 christos #include <io.h>
49 1.2 christos #endif
50 1.2 christos #ifdef HAVE_LIBGEN_H
51 1.2 christos #include <libgen.h>
52 1.2 christos #endif
53 1.1 joerg #ifdef HAVE_LIMITS_H
54 1.1 joerg #include <limits.h>
55 1.1 joerg #endif
56 1.2 christos #ifdef HAVE_PATHS_H
57 1.2 christos #include <paths.h>
58 1.1 joerg #endif
59 1.1 joerg #ifdef HAVE_PWD_H
60 1.1 joerg #include <pwd.h>
61 1.1 joerg #endif
62 1.2 christos #ifdef HAVE_STDINT_H
63 1.2 christos #include <stdint.h>
64 1.2 christos #endif
65 1.1 joerg #include <stdio.h>
66 1.1 joerg #ifdef HAVE_STDLIB_H
67 1.1 joerg #include <stdlib.h>
68 1.1 joerg #endif
69 1.1 joerg #ifdef HAVE_STRING_H
70 1.1 joerg #include <string.h>
71 1.1 joerg #endif
72 1.1 joerg #ifdef HAVE_UNISTD_H
73 1.1 joerg #include <unistd.h>
74 1.1 joerg #endif
75 1.1 joerg
76 1.1 joerg #include "bsdtar.h"
77 1.2 christos #include "err.h"
78 1.2 christos #include "line_reader.h"
79 1.1 joerg
80 1.2 christos #ifndef O_BINARY
81 1.2 christos #define O_BINARY 0
82 1.2 christos #endif
83 1.1 joerg
84 1.1 joerg struct archive_dir_entry {
85 1.1 joerg struct archive_dir_entry *next;
86 1.1 joerg time_t mtime_sec;
87 1.1 joerg int mtime_nsec;
88 1.1 joerg char *name;
89 1.1 joerg };
90 1.1 joerg
91 1.1 joerg struct archive_dir {
92 1.1 joerg struct archive_dir_entry *head, *tail;
93 1.1 joerg };
94 1.1 joerg
95 1.1 joerg static int append_archive(struct bsdtar *, struct archive *,
96 1.1 joerg struct archive *ina);
97 1.1 joerg static int append_archive_filename(struct bsdtar *,
98 1.1 joerg struct archive *, const char *fname);
99 1.1 joerg static void archive_names_from_file(struct bsdtar *bsdtar,
100 1.1 joerg struct archive *a);
101 1.2 christos static int copy_file_data_block(struct bsdtar *,
102 1.2 christos struct archive *a, struct archive *,
103 1.2 christos struct archive_entry *);
104 1.2 christos static void excluded_callback(struct archive *, void *,
105 1.2 christos struct archive_entry *);
106 1.2 christos static void report_write(struct bsdtar *, struct archive *,
107 1.2 christos struct archive_entry *, int64_t progress);
108 1.1 joerg static void test_for_append(struct bsdtar *);
109 1.2 christos static int metadata_filter(struct archive *, void *,
110 1.2 christos struct archive_entry *);
111 1.1 joerg static void write_archive(struct archive *, struct bsdtar *);
112 1.1 joerg static void write_entry(struct bsdtar *, struct archive *,
113 1.2 christos struct archive_entry *);
114 1.2 christos static void write_file(struct bsdtar *, struct archive *,
115 1.2 christos struct archive_entry *);
116 1.1 joerg static void write_hierarchy(struct bsdtar *, struct archive *,
117 1.1 joerg const char *);
118 1.1 joerg
119 1.2 christos #if defined(_WIN32) && !defined(__CYGWIN__)
120 1.2 christos /* Not a full lseek() emulation, but enough for our needs here. */
121 1.2 christos static int
122 1.2 christos seek_file(int fd, int64_t offset, int whence)
123 1.2 christos {
124 1.2 christos LARGE_INTEGER distance;
125 1.2 christos (void)whence; /* UNUSED */
126 1.2 christos distance.QuadPart = offset;
127 1.2 christos return (SetFilePointerEx((HANDLE)_get_osfhandle(fd),
128 1.2 christos distance, NULL, FILE_BEGIN) ? 1 : -1);
129 1.2 christos }
130 1.2 christos #define open _open
131 1.2 christos #define close _close
132 1.2 christos #define read _read
133 1.2 christos #ifdef lseek
134 1.2 christos #undef lseek
135 1.2 christos #endif
136 1.2 christos #define lseek seek_file
137 1.2 christos #endif
138 1.2 christos
139 1.2 christos static void
140 1.2 christos set_writer_options(struct bsdtar *bsdtar, struct archive *a)
141 1.2 christos {
142 1.2 christos const char *writer_options;
143 1.2 christos int r;
144 1.2 christos
145 1.2 christos writer_options = getenv(ENV_WRITER_OPTIONS);
146 1.2 christos if (writer_options != NULL) {
147 1.2 christos size_t module_len = sizeof(IGNORE_WRONG_MODULE_NAME) - 1;
148 1.2 christos size_t opt_len = strlen(writer_options) + 1;
149 1.2 christos char *p;
150 1.2 christos /* Set default write options. */
151 1.2 christos if ((p = malloc(module_len + opt_len)) == NULL)
152 1.2 christos lafe_errc(1, errno, "Out of memory");
153 1.2 christos /* Prepend magic code to ignore options for
154 1.2 christos * a format or filters which are not added to
155 1.2 christos * the archive write object. */
156 1.2 christos memcpy(p, IGNORE_WRONG_MODULE_NAME, module_len);
157 1.2 christos memcpy(p, writer_options, opt_len);
158 1.2 christos r = archive_write_set_options(a, p);
159 1.2 christos free(p);
160 1.2 christos if (r < ARCHIVE_WARN)
161 1.2 christos lafe_errc(1, 0, "%s", archive_error_string(a));
162 1.2 christos else
163 1.2 christos archive_clear_error(a);
164 1.2 christos }
165 1.2 christos if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options))
166 1.2 christos lafe_errc(1, 0, "%s", archive_error_string(a));
167 1.2 christos }
168 1.2 christos
169 1.2 christos static void
170 1.2 christos set_reader_options(struct bsdtar *bsdtar, struct archive *a)
171 1.2 christos {
172 1.2 christos const char *reader_options;
173 1.2 christos int r;
174 1.2 christos
175 1.2 christos (void)bsdtar; /* UNUSED */
176 1.2 christos
177 1.2 christos reader_options = getenv(ENV_READER_OPTIONS);
178 1.2 christos if (reader_options != NULL) {
179 1.2 christos size_t module_len = sizeof(IGNORE_WRONG_MODULE_NAME) - 1;
180 1.2 christos size_t opt_len = strlen(reader_options) + 1;
181 1.2 christos char *p;
182 1.2 christos /* Set default write options. */
183 1.2 christos if ((p = malloc(module_len + opt_len)) == NULL)
184 1.2 christos if (p == NULL)
185 1.2 christos lafe_errc(1, errno, "Out of memory");
186 1.2 christos /* Prepend magic code to ignore options for
187 1.2 christos * a format or filters which are not added to
188 1.2 christos * the archive write object. */
189 1.2 christos memcpy(p, IGNORE_WRONG_MODULE_NAME, module_len);
190 1.2 christos memcpy(p, reader_options, opt_len);
191 1.2 christos r = archive_read_set_options(a, p);
192 1.2 christos free(p);
193 1.2 christos if (r < ARCHIVE_WARN)
194 1.2 christos lafe_errc(1, 0, "%s", archive_error_string(a));
195 1.2 christos else
196 1.2 christos archive_clear_error(a);
197 1.2 christos }
198 1.4 christos if (bsdtar->flags & OPTFLAG_IGNORE_ZEROS)
199 1.4 christos if (archive_read_set_options(a,
200 1.4 christos "read_concatenated_archives") != ARCHIVE_OK)
201 1.4 christos lafe_errc(1, 0, "%s", archive_error_string(a));
202 1.2 christos }
203 1.2 christos
204 1.1 joerg void
205 1.1 joerg tar_mode_c(struct bsdtar *bsdtar)
206 1.1 joerg {
207 1.1 joerg struct archive *a;
208 1.2 christos const void *filter_name;
209 1.1 joerg int r;
210 1.1 joerg
211 1.1 joerg if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
212 1.2 christos lafe_errc(1, 0, "no files or directories specified");
213 1.1 joerg
214 1.1 joerg a = archive_write_new();
215 1.1 joerg
216 1.1 joerg /* Support any format that the library supports. */
217 1.2 christos if (cset_get_format(bsdtar->cset) == NULL) {
218 1.1 joerg r = archive_write_set_format_pax_restricted(a);
219 1.2 christos cset_set_format(bsdtar->cset, "pax restricted");
220 1.1 joerg } else {
221 1.2 christos r = archive_write_set_format_by_name(a,
222 1.2 christos cset_get_format(bsdtar->cset));
223 1.1 joerg }
224 1.1 joerg if (r != ARCHIVE_OK) {
225 1.1 joerg fprintf(stderr, "Can't use format %s: %s\n",
226 1.2 christos cset_get_format(bsdtar->cset),
227 1.1 joerg archive_error_string(a));
228 1.2 christos usage();
229 1.1 joerg }
230 1.1 joerg
231 1.2 christos archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
232 1.2 christos archive_write_set_bytes_in_last_block(a, bsdtar->bytes_in_last_block);
233 1.1 joerg
234 1.2 christos r = cset_write_add_filters(bsdtar->cset, a, &filter_name);
235 1.2 christos if (r < ARCHIVE_WARN) {
236 1.2 christos lafe_errc(1, 0, "Unsupported compression option --%s",
237 1.2 christos (const char *)filter_name);
238 1.1 joerg }
239 1.1 joerg
240 1.2 christos set_writer_options(bsdtar, a);
241 1.2 christos if (bsdtar->passphrase != NULL)
242 1.2 christos r = archive_write_set_passphrase(a, bsdtar->passphrase);
243 1.2 christos else
244 1.2 christos r = archive_write_set_passphrase_callback(a, bsdtar,
245 1.2 christos &passphrase_callback);
246 1.1 joerg if (r != ARCHIVE_OK)
247 1.2 christos lafe_errc(1, 0, "%s", archive_error_string(a));
248 1.2 christos if (ARCHIVE_OK != archive_write_open_filename(a, bsdtar->filename))
249 1.2 christos lafe_errc(1, 0, "%s", archive_error_string(a));
250 1.1 joerg write_archive(a, bsdtar);
251 1.1 joerg }
252 1.1 joerg
253 1.1 joerg /*
254 1.1 joerg * Same as 'c', except we only support tar or empty formats in
255 1.1 joerg * uncompressed files on disk.
256 1.1 joerg */
257 1.1 joerg void
258 1.1 joerg tar_mode_r(struct bsdtar *bsdtar)
259 1.1 joerg {
260 1.2 christos int64_t end_offset;
261 1.1 joerg int format;
262 1.1 joerg struct archive *a;
263 1.1 joerg struct archive_entry *entry;
264 1.1 joerg int r;
265 1.1 joerg
266 1.1 joerg /* Sanity-test some arguments and the file. */
267 1.1 joerg test_for_append(bsdtar);
268 1.1 joerg
269 1.1 joerg format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
270 1.1 joerg
271 1.2 christos #if defined(__BORLANDC__)
272 1.2 christos bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT | O_BINARY);
273 1.2 christos #else
274 1.2 christos bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT | O_BINARY, 0666);
275 1.2 christos #endif
276 1.1 joerg if (bsdtar->fd < 0)
277 1.2 christos lafe_errc(1, errno,
278 1.1 joerg "Cannot open %s", bsdtar->filename);
279 1.1 joerg
280 1.1 joerg a = archive_read_new();
281 1.2 christos archive_read_support_filter_all(a);
282 1.2 christos archive_read_support_format_empty(a);
283 1.1 joerg archive_read_support_format_tar(a);
284 1.1 joerg archive_read_support_format_gnutar(a);
285 1.2 christos set_reader_options(bsdtar, a);
286 1.1 joerg r = archive_read_open_fd(a, bsdtar->fd, 10240);
287 1.1 joerg if (r != ARCHIVE_OK)
288 1.2 christos lafe_errc(1, archive_errno(a),
289 1.1 joerg "Can't read archive %s: %s", bsdtar->filename,
290 1.1 joerg archive_error_string(a));
291 1.1 joerg while (0 == archive_read_next_header(a, &entry)) {
292 1.2 christos if (archive_filter_code(a, 0) != ARCHIVE_FILTER_NONE) {
293 1.2 christos archive_read_free(a);
294 1.1 joerg close(bsdtar->fd);
295 1.2 christos lafe_errc(1, 0,
296 1.1 joerg "Cannot append to compressed archive.");
297 1.1 joerg }
298 1.1 joerg /* Keep going until we hit end-of-archive */
299 1.1 joerg format = archive_format(a);
300 1.1 joerg }
301 1.1 joerg
302 1.1 joerg end_offset = archive_read_header_position(a);
303 1.2 christos archive_read_free(a);
304 1.1 joerg
305 1.1 joerg /* Re-open archive for writing */
306 1.1 joerg a = archive_write_new();
307 1.1 joerg /*
308 1.1 joerg * Set the format to be used for writing. To allow people to
309 1.1 joerg * extend empty files, we need to allow them to specify the format,
310 1.1 joerg * which opens the possibility that they will specify a format that
311 1.1 joerg * doesn't match the existing format. Hence, the following bit
312 1.1 joerg * of arcane ugliness.
313 1.1 joerg */
314 1.1 joerg
315 1.2 christos if (cset_get_format(bsdtar->cset) != NULL) {
316 1.1 joerg /* If the user requested a format, use that, but ... */
317 1.1 joerg archive_write_set_format_by_name(a,
318 1.2 christos cset_get_format(bsdtar->cset));
319 1.1 joerg /* ... complain if it's not compatible. */
320 1.1 joerg format &= ARCHIVE_FORMAT_BASE_MASK;
321 1.1 joerg if (format != (int)(archive_format(a) & ARCHIVE_FORMAT_BASE_MASK)
322 1.1 joerg && format != ARCHIVE_FORMAT_EMPTY) {
323 1.2 christos lafe_errc(1, 0,
324 1.1 joerg "Format %s is incompatible with the archive %s.",
325 1.2 christos cset_get_format(bsdtar->cset), bsdtar->filename);
326 1.1 joerg }
327 1.1 joerg } else {
328 1.1 joerg /*
329 1.1 joerg * Just preserve the current format, with a little care
330 1.1 joerg * for formats that libarchive can't write.
331 1.1 joerg */
332 1.1 joerg if (format == ARCHIVE_FORMAT_EMPTY)
333 1.1 joerg format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
334 1.1 joerg archive_write_set_format(a, format);
335 1.1 joerg }
336 1.2 christos if (lseek(bsdtar->fd, end_offset, SEEK_SET) < 0)
337 1.2 christos lafe_errc(1, errno, "Could not seek to archive end");
338 1.2 christos set_writer_options(bsdtar, a);
339 1.2 christos if (ARCHIVE_OK != archive_write_open_fd(a, bsdtar->fd))
340 1.2 christos lafe_errc(1, 0, "%s", archive_error_string(a));
341 1.1 joerg
342 1.1 joerg write_archive(a, bsdtar); /* XXX check return val XXX */
343 1.1 joerg
344 1.1 joerg close(bsdtar->fd);
345 1.1 joerg bsdtar->fd = -1;
346 1.1 joerg }
347 1.1 joerg
348 1.1 joerg void
349 1.1 joerg tar_mode_u(struct bsdtar *bsdtar)
350 1.1 joerg {
351 1.2 christos int64_t end_offset;
352 1.1 joerg struct archive *a;
353 1.1 joerg struct archive_entry *entry;
354 1.1 joerg int format;
355 1.1 joerg struct archive_dir_entry *p;
356 1.1 joerg struct archive_dir archive_dir;
357 1.1 joerg
358 1.1 joerg bsdtar->archive_dir = &archive_dir;
359 1.1 joerg memset(&archive_dir, 0, sizeof(archive_dir));
360 1.1 joerg
361 1.1 joerg format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
362 1.1 joerg
363 1.1 joerg /* Sanity-test some arguments and the file. */
364 1.1 joerg test_for_append(bsdtar);
365 1.1 joerg
366 1.2 christos bsdtar->fd = open(bsdtar->filename, O_RDWR | O_BINARY);
367 1.1 joerg if (bsdtar->fd < 0)
368 1.2 christos lafe_errc(1, errno,
369 1.1 joerg "Cannot open %s", bsdtar->filename);
370 1.1 joerg
371 1.1 joerg a = archive_read_new();
372 1.2 christos archive_read_support_filter_all(a);
373 1.1 joerg archive_read_support_format_tar(a);
374 1.1 joerg archive_read_support_format_gnutar(a);
375 1.2 christos set_reader_options(bsdtar, a);
376 1.2 christos if (archive_read_open_fd(a, bsdtar->fd, bsdtar->bytes_per_block)
377 1.2 christos != ARCHIVE_OK) {
378 1.2 christos lafe_errc(1, 0,
379 1.1 joerg "Can't open %s: %s", bsdtar->filename,
380 1.1 joerg archive_error_string(a));
381 1.1 joerg }
382 1.1 joerg
383 1.1 joerg /* Build a list of all entries and their recorded mod times. */
384 1.1 joerg while (0 == archive_read_next_header(a, &entry)) {
385 1.2 christos if (archive_filter_code(a, 0) != ARCHIVE_FILTER_NONE) {
386 1.2 christos archive_read_free(a);
387 1.1 joerg close(bsdtar->fd);
388 1.2 christos lafe_errc(1, 0,
389 1.1 joerg "Cannot append to compressed archive.");
390 1.1 joerg }
391 1.2 christos if (archive_match_exclude_entry(bsdtar->matching,
392 1.2 christos ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER |
393 1.2 christos ARCHIVE_MATCH_EQUAL, entry) != ARCHIVE_OK)
394 1.2 christos lafe_errc(1, 0, "Error : %s",
395 1.2 christos archive_error_string(bsdtar->matching));
396 1.1 joerg /* Record the last format determination we see */
397 1.1 joerg format = archive_format(a);
398 1.1 joerg /* Keep going until we hit end-of-archive */
399 1.1 joerg }
400 1.1 joerg
401 1.1 joerg end_offset = archive_read_header_position(a);
402 1.2 christos archive_read_free(a);
403 1.1 joerg
404 1.1 joerg /* Re-open archive for writing. */
405 1.1 joerg a = archive_write_new();
406 1.1 joerg /*
407 1.2 christos * Set format to same one auto-detected above.
408 1.1 joerg */
409 1.1 joerg archive_write_set_format(a, format);
410 1.2 christos archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
411 1.2 christos archive_write_set_bytes_in_last_block(a, bsdtar->bytes_in_last_block);
412 1.2 christos
413 1.2 christos if (lseek(bsdtar->fd, end_offset, SEEK_SET) < 0)
414 1.2 christos lafe_errc(1, errno, "Could not seek to archive end");
415 1.2 christos set_writer_options(bsdtar, a);
416 1.2 christos if (ARCHIVE_OK != archive_write_open_fd(a, bsdtar->fd))
417 1.2 christos lafe_errc(1, 0, "%s", archive_error_string(a));
418 1.1 joerg
419 1.1 joerg write_archive(a, bsdtar);
420 1.1 joerg
421 1.1 joerg close(bsdtar->fd);
422 1.1 joerg bsdtar->fd = -1;
423 1.1 joerg
424 1.1 joerg while (bsdtar->archive_dir->head != NULL) {
425 1.1 joerg p = bsdtar->archive_dir->head->next;
426 1.1 joerg free(bsdtar->archive_dir->head->name);
427 1.1 joerg free(bsdtar->archive_dir->head);
428 1.1 joerg bsdtar->archive_dir->head = p;
429 1.1 joerg }
430 1.1 joerg bsdtar->archive_dir->tail = NULL;
431 1.1 joerg }
432 1.1 joerg
433 1.1 joerg
434 1.1 joerg /*
435 1.1 joerg * Write user-specified files/dirs to opened archive.
436 1.1 joerg */
437 1.1 joerg static void
438 1.1 joerg write_archive(struct archive *a, struct bsdtar *bsdtar)
439 1.1 joerg {
440 1.1 joerg const char *arg;
441 1.1 joerg struct archive_entry *entry, *sparse_entry;
442 1.1 joerg
443 1.2 christos /* Choose a suitable copy buffer size */
444 1.2 christos bsdtar->buff_size = 64 * 1024;
445 1.2 christos while (bsdtar->buff_size < (size_t)bsdtar->bytes_per_block)
446 1.2 christos bsdtar->buff_size *= 2;
447 1.2 christos /* Try to compensate for space we'll lose to alignment. */
448 1.2 christos bsdtar->buff_size += 16 * 1024;
449 1.2 christos
450 1.2 christos /* Allocate a buffer for file data. */
451 1.2 christos if ((bsdtar->buff = malloc(bsdtar->buff_size)) == NULL)
452 1.2 christos lafe_errc(1, 0, "cannot allocate memory");
453 1.2 christos
454 1.1 joerg if ((bsdtar->resolver = archive_entry_linkresolver_new()) == NULL)
455 1.2 christos lafe_errc(1, 0, "cannot create link resolver");
456 1.1 joerg archive_entry_linkresolver_set_strategy(bsdtar->resolver,
457 1.1 joerg archive_format(a));
458 1.1 joerg
459 1.2 christos /* Create a read_disk object. */
460 1.2 christos if ((bsdtar->diskreader = archive_read_disk_new()) == NULL)
461 1.2 christos lafe_errc(1, 0, "Cannot create read_disk object");
462 1.2 christos /* Tell the read_disk how handle symlink. */
463 1.2 christos switch (bsdtar->symlink_mode) {
464 1.2 christos case 'H':
465 1.2 christos archive_read_disk_set_symlink_hybrid(bsdtar->diskreader);
466 1.2 christos break;
467 1.2 christos case 'L':
468 1.2 christos archive_read_disk_set_symlink_logical(bsdtar->diskreader);
469 1.2 christos break;
470 1.2 christos default:
471 1.2 christos archive_read_disk_set_symlink_physical(bsdtar->diskreader);
472 1.2 christos break;
473 1.2 christos }
474 1.2 christos /* Register entry filters. */
475 1.2 christos archive_read_disk_set_matching(bsdtar->diskreader,
476 1.2 christos bsdtar->matching, excluded_callback, bsdtar);
477 1.2 christos archive_read_disk_set_metadata_filter_callback(
478 1.2 christos bsdtar->diskreader, metadata_filter, bsdtar);
479 1.2 christos /* Set the behavior of archive_read_disk. */
480 1.2 christos archive_read_disk_set_behavior(bsdtar->diskreader,
481 1.2 christos bsdtar->readdisk_flags);
482 1.2 christos archive_read_disk_set_standard_lookup(bsdtar->diskreader);
483 1.2 christos
484 1.1 joerg if (bsdtar->names_from_file != NULL)
485 1.1 joerg archive_names_from_file(bsdtar, a);
486 1.1 joerg
487 1.1 joerg while (*bsdtar->argv) {
488 1.1 joerg arg = *bsdtar->argv;
489 1.1 joerg if (arg[0] == '-' && arg[1] == 'C') {
490 1.1 joerg arg += 2;
491 1.1 joerg if (*arg == '\0') {
492 1.1 joerg bsdtar->argv++;
493 1.1 joerg arg = *bsdtar->argv;
494 1.1 joerg if (arg == NULL) {
495 1.2 christos lafe_warnc(0, "%s",
496 1.1 joerg "Missing argument for -C");
497 1.1 joerg bsdtar->return_value = 1;
498 1.2 christos goto cleanup;
499 1.2 christos }
500 1.2 christos if (*arg == '\0') {
501 1.2 christos lafe_warnc(0,
502 1.2 christos "Meaningless argument for -C: ''");
503 1.2 christos bsdtar->return_value = 1;
504 1.2 christos goto cleanup;
505 1.1 joerg }
506 1.1 joerg }
507 1.1 joerg set_chdir(bsdtar, arg);
508 1.1 joerg } else {
509 1.2 christos if (*arg != '/')
510 1.1 joerg do_chdir(bsdtar); /* Handle a deferred -C */
511 1.1 joerg if (*arg == '@') {
512 1.1 joerg if (append_archive_filename(bsdtar, a,
513 1.1 joerg arg + 1) != 0)
514 1.1 joerg break;
515 1.1 joerg } else
516 1.1 joerg write_hierarchy(bsdtar, a, arg);
517 1.1 joerg }
518 1.1 joerg bsdtar->argv++;
519 1.1 joerg }
520 1.1 joerg
521 1.2 christos archive_read_disk_set_matching(bsdtar->diskreader, NULL, NULL, NULL);
522 1.2 christos archive_read_disk_set_metadata_filter_callback(
523 1.2 christos bsdtar->diskreader, NULL, NULL);
524 1.1 joerg entry = NULL;
525 1.1 joerg archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
526 1.1 joerg while (entry != NULL) {
527 1.2 christos int r;
528 1.2 christos struct archive_entry *entry2;
529 1.2 christos struct archive *disk = bsdtar->diskreader;
530 1.2 christos
531 1.2 christos /*
532 1.2 christos * This tricky code here is to correctly read the contents
533 1.2 christos * of the entry because the disk reader bsdtar->diskreader
534 1.2 christos * is pointing at does not have any information about the
535 1.2 christos * entry by this time and using archive_read_data_block()
536 1.2 christos * with the disk reader consequently must fail. And we
537 1.2 christos * have to re-open the entry to read the contents.
538 1.2 christos */
539 1.2 christos /* TODO: Work with -C option as well. */
540 1.2 christos r = archive_read_disk_open(disk,
541 1.2 christos archive_entry_sourcepath(entry));
542 1.2 christos if (r != ARCHIVE_OK) {
543 1.2 christos lafe_warnc(archive_errno(disk),
544 1.2 christos "%s", archive_error_string(disk));
545 1.2 christos bsdtar->return_value = 1;
546 1.2 christos goto next_entry;
547 1.2 christos }
548 1.2 christos
549 1.2 christos /*
550 1.2 christos * Invoke archive_read_next_header2() to work
551 1.2 christos * archive_read_data_block(), which is called via write_file(),
552 1.2 christos * without failure.
553 1.2 christos */
554 1.2 christos entry2 = archive_entry_new();
555 1.2 christos r = archive_read_next_header2(disk, entry2);
556 1.2 christos archive_entry_free(entry2);
557 1.2 christos if (r != ARCHIVE_OK) {
558 1.2 christos lafe_warnc(archive_errno(disk),
559 1.2 christos "%s", archive_error_string(disk));
560 1.2 christos if (r == ARCHIVE_FATAL)
561 1.2 christos bsdtar->return_value = 1;
562 1.2 christos archive_read_close(disk);
563 1.2 christos goto next_entry;
564 1.2 christos }
565 1.2 christos
566 1.2 christos write_file(bsdtar, a, entry);
567 1.2 christos archive_read_close(disk);
568 1.2 christos next_entry:
569 1.1 joerg archive_entry_free(entry);
570 1.1 joerg entry = NULL;
571 1.1 joerg archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
572 1.1 joerg }
573 1.1 joerg
574 1.1 joerg if (archive_write_close(a)) {
575 1.2 christos lafe_warnc(0, "%s", archive_error_string(a));
576 1.1 joerg bsdtar->return_value = 1;
577 1.1 joerg }
578 1.2 christos
579 1.2 christos cleanup:
580 1.2 christos /* Free file data buffer. */
581 1.2 christos free(bsdtar->buff);
582 1.2 christos archive_entry_linkresolver_free(bsdtar->resolver);
583 1.2 christos bsdtar->resolver = NULL;
584 1.2 christos archive_read_free(bsdtar->diskreader);
585 1.2 christos bsdtar->diskreader = NULL;
586 1.2 christos
587 1.2 christos if (bsdtar->flags & OPTFLAG_TOTALS) {
588 1.2 christos fprintf(stderr, "Total bytes written: %s\n",
589 1.2 christos tar_i64toa(archive_filter_bytes(a, -1)));
590 1.2 christos }
591 1.2 christos
592 1.2 christos archive_write_free(a);
593 1.1 joerg }
594 1.1 joerg
595 1.1 joerg /*
596 1.1 joerg * Archive names specified in file.
597 1.1 joerg *
598 1.1 joerg * Unless --null was specified, a line containing exactly "-C" will
599 1.1 joerg * cause the next line to be a directory to pass to chdir(). If
600 1.1 joerg * --null is specified, then a line "-C" is just another filename.
601 1.1 joerg */
602 1.2 christos static void
603 1.1 joerg archive_names_from_file(struct bsdtar *bsdtar, struct archive *a)
604 1.1 joerg {
605 1.2 christos struct lafe_line_reader *lr;
606 1.2 christos const char *line;
607 1.1 joerg
608 1.1 joerg bsdtar->next_line_is_dir = 0;
609 1.2 christos
610 1.2 christos lr = lafe_line_reader(bsdtar->names_from_file,
611 1.2 christos (bsdtar->flags & OPTFLAG_NULL));
612 1.2 christos while ((line = lafe_line_reader_next(lr)) != NULL) {
613 1.2 christos if (bsdtar->next_line_is_dir) {
614 1.2 christos if (*line != '\0')
615 1.2 christos set_chdir(bsdtar, line);
616 1.2 christos else {
617 1.2 christos lafe_warnc(0,
618 1.2 christos "Meaningless argument for -C: ''");
619 1.2 christos bsdtar->return_value = 1;
620 1.2 christos }
621 1.2 christos bsdtar->next_line_is_dir = 0;
622 1.2 christos } else if (((bsdtar->flags & OPTFLAG_NULL) == 0) &&
623 1.2 christos strcmp(line, "-C") == 0)
624 1.2 christos bsdtar->next_line_is_dir = 1;
625 1.2 christos else {
626 1.2 christos if (*line != '/')
627 1.2 christos do_chdir(bsdtar); /* Handle a deferred -C */
628 1.2 christos write_hierarchy(bsdtar, a, line);
629 1.2 christos }
630 1.2 christos }
631 1.2 christos lafe_line_reader_free(lr);
632 1.1 joerg if (bsdtar->next_line_is_dir)
633 1.2 christos lafe_errc(1, errno,
634 1.1 joerg "Unexpected end of filename list; "
635 1.1 joerg "directory expected after -C");
636 1.1 joerg }
637 1.1 joerg
638 1.1 joerg /*
639 1.1 joerg * Copy from specified archive to current archive. Returns non-zero
640 1.1 joerg * for write errors (which force us to terminate the entire archiving
641 1.1 joerg * operation). If there are errors reading the input archive, we set
642 1.1 joerg * bsdtar->return_value but return zero, so the overall archiving
643 1.1 joerg * operation will complete and return non-zero.
644 1.1 joerg */
645 1.1 joerg static int
646 1.1 joerg append_archive_filename(struct bsdtar *bsdtar, struct archive *a,
647 1.2 christos const char *raw_filename)
648 1.1 joerg {
649 1.1 joerg struct archive *ina;
650 1.2 christos const char *filename = raw_filename;
651 1.1 joerg int rc;
652 1.1 joerg
653 1.1 joerg if (strcmp(filename, "-") == 0)
654 1.1 joerg filename = NULL; /* Library uses NULL for stdio. */
655 1.1 joerg
656 1.1 joerg ina = archive_read_new();
657 1.1 joerg archive_read_support_format_all(ina);
658 1.2 christos archive_read_support_filter_all(ina);
659 1.2 christos set_reader_options(bsdtar, ina);
660 1.2 christos archive_read_set_options(ina, "mtree:checkfs");
661 1.2 christos if (bsdtar->passphrase != NULL)
662 1.2 christos rc = archive_read_add_passphrase(a, bsdtar->passphrase);
663 1.2 christos else
664 1.2 christos rc = archive_read_set_passphrase_callback(ina, bsdtar,
665 1.2 christos &passphrase_callback);
666 1.2 christos if (rc != ARCHIVE_OK)
667 1.2 christos lafe_errc(1, 0, "%s", archive_error_string(a));
668 1.2 christos if (archive_read_open_filename(ina, filename,
669 1.2 christos bsdtar->bytes_per_block)) {
670 1.2 christos lafe_warnc(0, "%s", archive_error_string(ina));
671 1.1 joerg bsdtar->return_value = 1;
672 1.1 joerg return (0);
673 1.1 joerg }
674 1.1 joerg
675 1.1 joerg rc = append_archive(bsdtar, a, ina);
676 1.1 joerg
677 1.2 christos if (rc != ARCHIVE_OK) {
678 1.2 christos lafe_warnc(0, "Error reading archive %s: %s",
679 1.2 christos raw_filename, archive_error_string(ina));
680 1.1 joerg bsdtar->return_value = 1;
681 1.1 joerg }
682 1.2 christos archive_read_free(ina);
683 1.1 joerg
684 1.1 joerg return (rc);
685 1.1 joerg }
686 1.1 joerg
687 1.1 joerg static int
688 1.1 joerg append_archive(struct bsdtar *bsdtar, struct archive *a, struct archive *ina)
689 1.1 joerg {
690 1.1 joerg struct archive_entry *in_entry;
691 1.1 joerg int e;
692 1.1 joerg
693 1.2 christos while (ARCHIVE_OK == (e = archive_read_next_header(ina, &in_entry))) {
694 1.2 christos if (archive_match_excluded(bsdtar->matching, in_entry))
695 1.1 joerg continue;
696 1.4 christos if(edit_pathname(bsdtar, in_entry))
697 1.4 christos continue;
698 1.2 christos if ((bsdtar->flags & OPTFLAG_INTERACTIVE) &&
699 1.1 joerg !yes("copy '%s'", archive_entry_pathname(in_entry)))
700 1.1 joerg continue;
701 1.2 christos if (bsdtar->verbose > 1) {
702 1.2 christos safe_fprintf(stderr, "a ");
703 1.2 christos list_item_verbose(bsdtar, stderr, in_entry);
704 1.2 christos } else if (bsdtar->verbose > 0)
705 1.1 joerg safe_fprintf(stderr, "a %s",
706 1.1 joerg archive_entry_pathname(in_entry));
707 1.2 christos if (need_report())
708 1.2 christos report_write(bsdtar, a, in_entry, 0);
709 1.1 joerg
710 1.1 joerg e = archive_write_header(a, in_entry);
711 1.1 joerg if (e != ARCHIVE_OK) {
712 1.1 joerg if (!bsdtar->verbose)
713 1.2 christos lafe_warnc(0, "%s: %s",
714 1.1 joerg archive_entry_pathname(in_entry),
715 1.1 joerg archive_error_string(a));
716 1.1 joerg else
717 1.1 joerg fprintf(stderr, ": %s", archive_error_string(a));
718 1.1 joerg }
719 1.1 joerg if (e == ARCHIVE_FATAL)
720 1.1 joerg exit(1);
721 1.1 joerg
722 1.1 joerg if (e >= ARCHIVE_WARN) {
723 1.1 joerg if (archive_entry_size(in_entry) == 0)
724 1.1 joerg archive_read_data_skip(ina);
725 1.2 christos else if (copy_file_data_block(bsdtar, a, ina, in_entry))
726 1.1 joerg exit(1);
727 1.1 joerg }
728 1.1 joerg
729 1.1 joerg if (bsdtar->verbose)
730 1.1 joerg fprintf(stderr, "\n");
731 1.1 joerg }
732 1.1 joerg
733 1.2 christos return (e == ARCHIVE_EOF ? ARCHIVE_OK : e);
734 1.1 joerg }
735 1.1 joerg
736 1.2 christos /* Helper function to copy file to archive. */
737 1.1 joerg static int
738 1.2 christos copy_file_data_block(struct bsdtar *bsdtar, struct archive *a,
739 1.2 christos struct archive *in_a, struct archive_entry *entry)
740 1.1 joerg {
741 1.2 christos size_t bytes_read;
742 1.1 joerg ssize_t bytes_written;
743 1.2 christos int64_t offset, progress = 0;
744 1.2 christos char *null_buff = NULL;
745 1.2 christos const void *buff;
746 1.2 christos int r;
747 1.1 joerg
748 1.2 christos while ((r = archive_read_data_block(in_a, &buff,
749 1.2 christos &bytes_read, &offset)) == ARCHIVE_OK) {
750 1.2 christos if (need_report())
751 1.2 christos report_write(bsdtar, a, entry, progress);
752 1.2 christos
753 1.2 christos if (offset > progress) {
754 1.2 christos int64_t sparse = offset - progress;
755 1.2 christos size_t ns;
756 1.2 christos
757 1.2 christos if (null_buff == NULL) {
758 1.2 christos null_buff = bsdtar->buff;
759 1.2 christos memset(null_buff, 0, bsdtar->buff_size);
760 1.2 christos }
761 1.2 christos
762 1.2 christos while (sparse > 0) {
763 1.2 christos if (sparse > (int64_t)bsdtar->buff_size)
764 1.2 christos ns = bsdtar->buff_size;
765 1.2 christos else
766 1.2 christos ns = (size_t)sparse;
767 1.2 christos bytes_written =
768 1.2 christos archive_write_data(a, null_buff, ns);
769 1.2 christos if (bytes_written < 0) {
770 1.2 christos /* Write failed; this is bad */
771 1.2 christos lafe_warnc(0, "%s",
772 1.2 christos archive_error_string(a));
773 1.2 christos return (-1);
774 1.2 christos }
775 1.2 christos if ((size_t)bytes_written < ns) {
776 1.2 christos /* Write was truncated; warn but
777 1.2 christos * continue. */
778 1.2 christos lafe_warnc(0,
779 1.2 christos "%s: Truncated write; file may "
780 1.2 christos "have grown while being archived.",
781 1.2 christos archive_entry_pathname(entry));
782 1.2 christos return (0);
783 1.2 christos }
784 1.2 christos progress += bytes_written;
785 1.2 christos sparse -= bytes_written;
786 1.2 christos }
787 1.2 christos }
788 1.1 joerg
789 1.1 joerg bytes_written = archive_write_data(a, buff, bytes_read);
790 1.2 christos if (bytes_written < 0) {
791 1.2 christos /* Write failed; this is bad */
792 1.2 christos lafe_warnc(0, "%s", archive_error_string(a));
793 1.1 joerg return (-1);
794 1.1 joerg }
795 1.2 christos if ((size_t)bytes_written < bytes_read) {
796 1.2 christos /* Write was truncated; warn but continue. */
797 1.2 christos lafe_warnc(0,
798 1.2 christos "%s: Truncated write; file may have grown "
799 1.2 christos "while being archived.",
800 1.2 christos archive_entry_pathname(entry));
801 1.2 christos return (0);
802 1.2 christos }
803 1.1 joerg progress += bytes_written;
804 1.1 joerg }
805 1.2 christos if (r < ARCHIVE_WARN) {
806 1.3 christos const char *s = archive_error_string(a);
807 1.3 christos if (s)
808 1.3 christos lafe_warnc(archive_errno(a), "%s", s);
809 1.2 christos return (-1);
810 1.2 christos }
811 1.2 christos return (0);
812 1.2 christos }
813 1.2 christos
814 1.2 christos static void
815 1.2 christos excluded_callback(struct archive *a, void *_data, struct archive_entry *entry)
816 1.2 christos {
817 1.2 christos struct bsdtar *bsdtar = (struct bsdtar *)_data;
818 1.2 christos
819 1.2 christos if (bsdtar->flags & OPTFLAG_NO_SUBDIRS)
820 1.2 christos return;
821 1.2 christos if (!archive_read_disk_can_descend(a))
822 1.2 christos return;
823 1.2 christos if ((bsdtar->flags & OPTFLAG_INTERACTIVE) &&
824 1.2 christos !yes("add '%s'", archive_entry_pathname(entry)))
825 1.2 christos return;
826 1.2 christos archive_read_disk_descend(a);
827 1.2 christos }
828 1.2 christos
829 1.2 christos static int
830 1.2 christos metadata_filter(struct archive *a, void *_data, struct archive_entry *entry)
831 1.2 christos {
832 1.2 christos struct bsdtar *bsdtar = (struct bsdtar *)_data;
833 1.2 christos
834 1.2 christos /* XXX TODO: check whether this filesystem is
835 1.2 christos * synthetic and/or local. Add a new
836 1.2 christos * --local-only option to skip non-local
837 1.2 christos * filesystems. Skip synthetic filesystems
838 1.2 christos * regardless.
839 1.2 christos *
840 1.2 christos * The results should be cached, since
841 1.2 christos * tree.c doesn't usually visit a directory
842 1.2 christos * and the directory contents together. A simple
843 1.2 christos * move-to-front list should perform quite well.
844 1.2 christos *
845 1.2 christos * Use archive_read_disk_current_filesystem_is_remote().
846 1.2 christos */
847 1.2 christos
848 1.2 christos /*
849 1.2 christos * If the user vetoes this file/directory, skip it.
850 1.2 christos * We want this to be fairly late; if some other
851 1.2 christos * check would veto this file, we shouldn't bother
852 1.2 christos * the user with it.
853 1.2 christos */
854 1.2 christos if ((bsdtar->flags & OPTFLAG_INTERACTIVE) &&
855 1.2 christos !yes("add '%s'", archive_entry_pathname(entry)))
856 1.2 christos return (0);
857 1.1 joerg
858 1.2 christos /* Note: if user vetoes, we won't descend. */
859 1.2 christos if (((bsdtar->flags & OPTFLAG_NO_SUBDIRS) == 0) &&
860 1.2 christos archive_read_disk_can_descend(a))
861 1.2 christos archive_read_disk_descend(a);
862 1.2 christos
863 1.2 christos return (1);
864 1.1 joerg }
865 1.1 joerg
866 1.1 joerg /*
867 1.1 joerg * Add the file or dir hierarchy named by 'path' to the archive
868 1.1 joerg */
869 1.1 joerg static void
870 1.1 joerg write_hierarchy(struct bsdtar *bsdtar, struct archive *a, const char *path)
871 1.1 joerg {
872 1.2 christos struct archive *disk = bsdtar->diskreader;
873 1.2 christos struct archive_entry *entry = NULL, *spare_entry = NULL;
874 1.2 christos int r;
875 1.1 joerg
876 1.2 christos r = archive_read_disk_open(disk, path);
877 1.2 christos if (r != ARCHIVE_OK) {
878 1.2 christos lafe_warnc(archive_errno(disk),
879 1.2 christos "%s", archive_error_string(disk));
880 1.1 joerg bsdtar->return_value = 1;
881 1.1 joerg return;
882 1.1 joerg }
883 1.2 christos bsdtar->first_fs = -1;
884 1.1 joerg
885 1.2 christos for (;;) {
886 1.2 christos archive_entry_free(entry);
887 1.2 christos entry = archive_entry_new();
888 1.2 christos r = archive_read_next_header2(disk, entry);
889 1.2 christos if (r == ARCHIVE_EOF)
890 1.2 christos break;
891 1.2 christos else if (r != ARCHIVE_OK) {
892 1.2 christos lafe_warnc(archive_errno(disk),
893 1.2 christos "%s", archive_error_string(disk));
894 1.2 christos if (r == ARCHIVE_FATAL || r == ARCHIVE_FAILED) {
895 1.1 joerg bsdtar->return_value = 1;
896 1.2 christos archive_entry_free(entry);
897 1.2 christos archive_read_close(disk);
898 1.2 christos return;
899 1.2 christos } else if (r < ARCHIVE_WARN)
900 1.2 christos continue;
901 1.1 joerg }
902 1.1 joerg
903 1.2 christos if (bsdtar->uid >= 0) {
904 1.2 christos archive_entry_set_uid(entry, bsdtar->uid);
905 1.2 christos if (!bsdtar->uname)
906 1.2 christos archive_entry_set_uname(entry,
907 1.2 christos archive_read_disk_uname(bsdtar->diskreader,
908 1.2 christos bsdtar->uid));
909 1.2 christos }
910 1.2 christos if (bsdtar->gid >= 0) {
911 1.2 christos archive_entry_set_gid(entry, bsdtar->gid);
912 1.2 christos if (!bsdtar->gname)
913 1.2 christos archive_entry_set_gname(entry,
914 1.2 christos archive_read_disk_gname(bsdtar->diskreader,
915 1.2 christos bsdtar->gid));
916 1.2 christos }
917 1.2 christos if (bsdtar->uname)
918 1.2 christos archive_entry_set_uname(entry, bsdtar->uname);
919 1.2 christos if (bsdtar->gname)
920 1.2 christos archive_entry_set_gname(entry, bsdtar->gname);
921 1.1 joerg
922 1.1 joerg /*
923 1.2 christos * Rewrite the pathname to be archived. If rewrite
924 1.2 christos * fails, skip the entry.
925 1.1 joerg */
926 1.2 christos if (edit_pathname(bsdtar, entry))
927 1.1 joerg continue;
928 1.1 joerg
929 1.2 christos /* Display entry as we process it. */
930 1.2 christos if (bsdtar->verbose > 1) {
931 1.2 christos safe_fprintf(stderr, "a ");
932 1.2 christos list_item_verbose(bsdtar, stderr, entry);
933 1.2 christos } else if (bsdtar->verbose > 0) {
934 1.2 christos /* This format is required by SUSv2. */
935 1.2 christos safe_fprintf(stderr, "a %s",
936 1.2 christos archive_entry_pathname(entry));
937 1.2 christos }
938 1.1 joerg
939 1.2 christos /* Non-regular files get archived with zero size. */
940 1.2 christos if (archive_entry_filetype(entry) != AE_IFREG)
941 1.2 christos archive_entry_set_size(entry, 0);
942 1.1 joerg
943 1.2 christos archive_entry_linkify(bsdtar->resolver, &entry, &spare_entry);
944 1.1 joerg
945 1.2 christos while (entry != NULL) {
946 1.2 christos write_file(bsdtar, a, entry);
947 1.2 christos archive_entry_free(entry);
948 1.2 christos entry = spare_entry;
949 1.2 christos spare_entry = NULL;
950 1.1 joerg }
951 1.1 joerg
952 1.2 christos if (bsdtar->verbose)
953 1.2 christos fprintf(stderr, "\n");
954 1.2 christos }
955 1.2 christos archive_entry_free(entry);
956 1.2 christos archive_read_close(disk);
957 1.2 christos }
958 1.1 joerg
959 1.2 christos /*
960 1.2 christos * Write a single file (or directory or other filesystem object) to
961 1.2 christos * the archive.
962 1.2 christos */
963 1.2 christos static void
964 1.2 christos write_file(struct bsdtar *bsdtar, struct archive *a,
965 1.2 christos struct archive_entry *entry)
966 1.2 christos {
967 1.2 christos write_entry(bsdtar, a, entry);
968 1.1 joerg }
969 1.1 joerg
970 1.1 joerg /*
971 1.2 christos * Write a single entry to the archive.
972 1.1 joerg */
973 1.1 joerg static void
974 1.2 christos write_entry(struct bsdtar *bsdtar, struct archive *a,
975 1.2 christos struct archive_entry *entry)
976 1.1 joerg {
977 1.1 joerg int e;
978 1.1 joerg
979 1.1 joerg e = archive_write_header(a, entry);
980 1.1 joerg if (e != ARCHIVE_OK) {
981 1.2 christos if (bsdtar->verbose > 0) {
982 1.2 christos safe_fprintf(stderr, "a ");
983 1.2 christos list_item_verbose(bsdtar, stderr, entry);
984 1.2 christos lafe_warnc(0, ": %s", archive_error_string(a));
985 1.2 christos } else {
986 1.2 christos lafe_warnc(0, "%s: %s",
987 1.1 joerg archive_entry_pathname(entry),
988 1.1 joerg archive_error_string(a));
989 1.2 christos }
990 1.1 joerg }
991 1.1 joerg
992 1.1 joerg if (e == ARCHIVE_FATAL)
993 1.1 joerg exit(1);
994 1.1 joerg
995 1.1 joerg /*
996 1.1 joerg * If we opened a file earlier, write it out now. Note that
997 1.1 joerg * the format handler might have reset the size field to zero
998 1.1 joerg * to inform us that the archive body won't get stored. In
999 1.1 joerg * that case, just skip the write.
1000 1.1 joerg */
1001 1.2 christos if (e >= ARCHIVE_WARN && archive_entry_size(entry) > 0) {
1002 1.2 christos if (copy_file_data_block(bsdtar, a, bsdtar->diskreader, entry))
1003 1.1 joerg exit(1);
1004 1.1 joerg }
1005 1.1 joerg }
1006 1.1 joerg
1007 1.1 joerg static void
1008 1.2 christos report_write(struct bsdtar *bsdtar, struct archive *a,
1009 1.2 christos struct archive_entry *entry, int64_t progress)
1010 1.1 joerg {
1011 1.2 christos uint64_t comp, uncomp;
1012 1.2 christos int compression;
1013 1.1 joerg
1014 1.1 joerg if (bsdtar->verbose)
1015 1.1 joerg fprintf(stderr, "\n");
1016 1.2 christos comp = archive_filter_bytes(a, -1);
1017 1.2 christos uncomp = archive_filter_bytes(a, 0);
1018 1.2 christos fprintf(stderr, "In: %d files, %s bytes;",
1019 1.2 christos archive_file_count(a), tar_i64toa(uncomp));
1020 1.2 christos if (comp >= uncomp)
1021 1.2 christos compression = 0;
1022 1.1 joerg else
1023 1.2 christos compression = (int)((uncomp - comp) * 100 / uncomp);
1024 1.2 christos fprintf(stderr,
1025 1.2 christos " Out: %s bytes, compression %d%%\n",
1026 1.2 christos tar_i64toa(comp), compression);
1027 1.2 christos /* Can't have two calls to tar_i64toa() pending, so split the output. */
1028 1.2 christos safe_fprintf(stderr, "Current: %s (%s",
1029 1.2 christos archive_entry_pathname(entry),
1030 1.2 christos tar_i64toa(progress));
1031 1.2 christos fprintf(stderr, "/%s bytes)\n",
1032 1.2 christos tar_i64toa(archive_entry_size(entry)));
1033 1.1 joerg }
1034 1.1 joerg
1035 1.1 joerg static void
1036 1.1 joerg test_for_append(struct bsdtar *bsdtar)
1037 1.1 joerg {
1038 1.1 joerg struct stat s;
1039 1.1 joerg
1040 1.1 joerg if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
1041 1.2 christos lafe_errc(1, 0, "no files or directories specified");
1042 1.1 joerg if (bsdtar->filename == NULL)
1043 1.2 christos lafe_errc(1, 0, "Cannot append to stdout.");
1044 1.1 joerg
1045 1.1 joerg if (stat(bsdtar->filename, &s) != 0)
1046 1.1 joerg return;
1047 1.1 joerg
1048 1.1 joerg if (!S_ISREG(s.st_mode) && !S_ISBLK(s.st_mode))
1049 1.2 christos lafe_errc(1, 0,
1050 1.1 joerg "Cannot append to %s: not a regular file.",
1051 1.1 joerg bsdtar->filename);
1052 1.2 christos
1053 1.2 christos /* Is this an appropriate check here on Windows? */
1054 1.2 christos /*
1055 1.2 christos if (GetFileType(handle) != FILE_TYPE_DISK)
1056 1.2 christos lafe_errc(1, 0, "Cannot append");
1057 1.2 christos */
1058 1.2 christos
1059 1.1 joerg }
1060