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