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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
|
/*-
* 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 "bsdtar_platform.h"
__FBSDID("$FreeBSD$");
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_ACL_H
#include <sys/acl.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_ATTR_XATTR_H
#include <attr/xattr.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_FNMATCH_H
#include <fnmatch.h>
#endif
#ifdef HAVE_GRP_H
#include <grp.h>
#endif
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
#ifdef HAVE_LINUX_FS_H
#include <linux/fs.h> /* for Linux file flags */
#endif
/*
* Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
* As the include guards don't agree, the order of include is important.
*/
#ifdef HAVE_LINUX_EXT2_FS_H
#include <linux/ext2_fs.h> /* for Linux file flags */
#endif
#if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
/* This header exists but is broken on Cygwin. */
#include <ext2fs/ext2_fs.h>
#endif
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "bsdtar.h"
#include "tree.h"
#include "err.h"
/* Size of buffer for holding file data prior to writing. */
#define FILEDATABUFLEN 65536
/* Fixed size of uname/gname caches. */
#define name_cache_size 101
static const char * const NO_NAME = "(noname)";
struct archive_dir_entry {
struct archive_dir_entry *next;
time_t mtime_sec;
int mtime_nsec;
char *name;
};
struct archive_dir {
struct archive_dir_entry *head, *tail;
};
struct name_cache {
int probes;
int hits;
size_t size;
struct {
id_t id;
const char *name;
} cache[name_cache_size];
};
static void add_dir_list(struct bsdtar *bsdtar, const char *path,
time_t mtime_sec, int mtime_nsec);
static int append_archive(struct bsdtar *, struct archive *,
struct archive *ina);
static int append_archive_filename(struct bsdtar *,
struct archive *, const char *fname);
static void archive_names_from_file(struct bsdtar *bsdtar,
struct archive *a);
static int archive_names_from_file_helper(struct bsdtar *bsdtar,
const char *line);
static int copy_file_data(struct bsdtar *, struct archive *a,
struct archive *ina, struct archive_entry *);
static int new_enough(struct bsdtar *, const char *path,
const struct stat *);
static void report_write(struct bsdtar *, struct archive *,
struct archive_entry *, int64_t progress);
static void test_for_append(struct bsdtar *);
static void write_archive(struct archive *, struct bsdtar *);
static void write_entry_backend(struct bsdtar *, struct archive *,
struct archive_entry *);
static int write_file_data(struct bsdtar *, struct archive *,
struct archive_entry *, int fd);
static void write_hierarchy(struct bsdtar *, struct archive *,
const char *);
void
tar_mode_c(struct bsdtar *bsdtar)
{
struct archive *a;
int r;
if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
bsdtar_errc(1, 0, "no files or directories specified");
a = archive_write_new();
/* Support any format that the library supports. */
if (bsdtar->create_format == NULL) {
r = archive_write_set_format_pax_restricted(a);
bsdtar->create_format = "pax restricted";
} else {
r = archive_write_set_format_by_name(a, bsdtar->create_format);
}
if (r != ARCHIVE_OK) {
fprintf(stderr, "Can't use format %s: %s\n",
bsdtar->create_format,
archive_error_string(a));
usage();
}
/*
* If user explicitly set the block size, then assume they
* want the last block padded as well. Otherwise, use the
* default block size and accept archive_write_open_file()'s
* default padding decisions.
*/
if (bsdtar->bytes_per_block != 0) {
archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
archive_write_set_bytes_in_last_block(a,
bsdtar->bytes_per_block);
} else
archive_write_set_bytes_per_block(a, DEFAULT_BYTES_PER_BLOCK);
if (bsdtar->compress_program) {
archive_write_set_compression_program(a, bsdtar->compress_program);
} else {
switch (bsdtar->create_compression) {
case 0:
r = archive_write_set_compression_none(a);
break;
case 'j': case 'y':
r = archive_write_set_compression_bzip2(a);
break;
case 'J':
r = archive_write_set_compression_xz(a);
break;
case OPTION_LZMA:
r = archive_write_set_compression_lzma(a);
break;
case 'z':
r = archive_write_set_compression_gzip(a);
break;
case 'Z':
r = archive_write_set_compression_compress(a);
break;
default:
bsdtar_errc(1, 0,
"Unrecognized compression option -%c",
bsdtar->create_compression);
}
if (r != ARCHIVE_OK) {
bsdtar_errc(1, 0,
"Unsupported compression option -%c",
bsdtar->create_compression);
}
}
if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options))
bsdtar_errc(1, 0, "%s", archive_error_string(a));
if (ARCHIVE_OK != archive_write_open_file(a, bsdtar->filename))
bsdtar_errc(1, 0, "%s", archive_error_string(a));
write_archive(a, bsdtar);
}
/*
* Same as 'c', except we only support tar or empty formats in
* uncompressed files on disk.
*/
void
tar_mode_r(struct bsdtar *bsdtar)
{
int64_t end_offset;
int format;
struct archive *a;
struct archive_entry *entry;
int r;
/* Sanity-test some arguments and the file. */
test_for_append(bsdtar);
format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT, 0666);
if (bsdtar->fd < 0)
bsdtar_errc(1, errno,
"Cannot open %s", bsdtar->filename);
a = archive_read_new();
archive_read_support_compression_all(a);
archive_read_support_format_tar(a);
archive_read_support_format_gnutar(a);
r = archive_read_open_fd(a, bsdtar->fd, 10240);
if (r != ARCHIVE_OK)
bsdtar_errc(1, archive_errno(a),
"Can't read archive %s: %s", bsdtar->filename,
archive_error_string(a));
while (0 == archive_read_next_header(a, &entry)) {
if (archive_compression(a) != ARCHIVE_COMPRESSION_NONE) {
archive_read_finish(a);
close(bsdtar->fd);
bsdtar_errc(1, 0,
"Cannot append to compressed archive.");
}
/* Keep going until we hit end-of-archive */
format = archive_format(a);
}
end_offset = archive_read_header_position(a);
archive_read_finish(a);
/* Re-open archive for writing */
a = archive_write_new();
archive_write_set_compression_none(a);
/*
* Set the format to be used for writing. To allow people to
* extend empty files, we need to allow them to specify the format,
* which opens the possibility that they will specify a format that
* doesn't match the existing format. Hence, the following bit
* of arcane ugliness.
*/
if (bsdtar->create_format != NULL) {
/* If the user requested a format, use that, but ... */
archive_write_set_format_by_name(a,
bsdtar->create_format);
/* ... complain if it's not compatible. */
format &= ARCHIVE_FORMAT_BASE_MASK;
if (format != (int)(archive_format(a) & ARCHIVE_FORMAT_BASE_MASK)
&& format != ARCHIVE_FORMAT_EMPTY) {
bsdtar_errc(1, 0,
"Format %s is incompatible with the archive %s.",
bsdtar->create_format, bsdtar->filename);
}
} else {
/*
* Just preserve the current format, with a little care
* for formats that libarchive can't write.
*/
if (format == ARCHIVE_FORMAT_TAR_GNUTAR)
/* TODO: When gtar supports pax, use pax restricted. */
format = ARCHIVE_FORMAT_TAR_USTAR;
if (format == ARCHIVE_FORMAT_EMPTY)
format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
archive_write_set_format(a, format);
}
if (lseek(bsdtar->fd, end_offset, SEEK_SET) < 0)
bsdtar_errc(1, errno, "Could not seek to archive end");
if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options))
bsdtar_errc(1, 0, "%s", archive_error_string(a));
if (ARCHIVE_OK != archive_write_open_fd(a, bsdtar->fd))
bsdtar_errc(1, 0, "%s", archive_error_string(a));
write_archive(a, bsdtar); /* XXX check return val XXX */
close(bsdtar->fd);
bsdtar->fd = -1;
}
void
tar_mode_u(struct bsdtar *bsdtar)
{
int64_t end_offset;
struct archive *a;
struct archive_entry *entry;
int format;
struct archive_dir_entry *p;
struct archive_dir archive_dir;
bsdtar->archive_dir = &archive_dir;
memset(&archive_dir, 0, sizeof(archive_dir));
format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
/* Sanity-test some arguments and the file. */
test_for_append(bsdtar);
bsdtar->fd = open(bsdtar->filename, O_RDWR);
if (bsdtar->fd < 0)
bsdtar_errc(1, errno,
"Cannot open %s", bsdtar->filename);
a = archive_read_new();
archive_read_support_compression_all(a);
archive_read_support_format_tar(a);
archive_read_support_format_gnutar(a);
if (archive_read_open_fd(a, bsdtar->fd,
bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block :
DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK) {
bsdtar_errc(1, 0,
"Can't open %s: %s", bsdtar->filename,
archive_error_string(a));
}
/* Build a list of all entries and their recorded mod times. */
while (0 == archive_read_next_header(a, &entry)) {
if (archive_compression(a) != ARCHIVE_COMPRESSION_NONE) {
archive_read_finish(a);
close(bsdtar->fd);
bsdtar_errc(1, 0,
"Cannot append to compressed archive.");
}
add_dir_list(bsdtar, archive_entry_pathname(entry),
archive_entry_mtime(entry),
archive_entry_mtime_nsec(entry));
/* Record the last format determination we see */
format = archive_format(a);
/* Keep going until we hit end-of-archive */
}
end_offset = archive_read_header_position(a);
archive_read_finish(a);
/* Re-open archive for writing. */
a = archive_write_new();
archive_write_set_compression_none(a);
/*
* Set format to same one auto-detected above, except that
* we don't write GNU tar format, so use ustar instead.
*/
if (format == ARCHIVE_FORMAT_TAR_GNUTAR)
format = ARCHIVE_FORMAT_TAR_USTAR;
archive_write_set_format(a, format);
if (bsdtar->bytes_per_block != 0) {
archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
archive_write_set_bytes_in_last_block(a,
bsdtar->bytes_per_block);
} else
archive_write_set_bytes_per_block(a, DEFAULT_BYTES_PER_BLOCK);
if (lseek(bsdtar->fd, end_offset, SEEK_SET) < 0)
bsdtar_errc(1, errno, "Could not seek to archive end");
if (ARCHIVE_OK != archive_write_set_options(a, bsdtar->option_options))
bsdtar_errc(1, 0, "%s", archive_error_string(a));
if (ARCHIVE_OK != archive_write_open_fd(a, bsdtar->fd))
bsdtar_errc(1, 0, "%s", archive_error_string(a));
write_archive(a, bsdtar);
close(bsdtar->fd);
bsdtar->fd = -1;
while (bsdtar->archive_dir->head != NULL) {
p = bsdtar->archive_dir->head->next;
free(bsdtar->archive_dir->head->name);
free(bsdtar->archive_dir->head);
bsdtar->archive_dir->head = p;
}
bsdtar->archive_dir->tail = NULL;
}
/*
* Write user-specified files/dirs to opened archive.
*/
static void
write_archive(struct archive *a, struct bsdtar *bsdtar)
{
const char *arg;
struct archive_entry *entry, *sparse_entry;
/* Allocate a buffer for file data. */
if ((bsdtar->buff = malloc(FILEDATABUFLEN)) == NULL)
bsdtar_errc(1, 0, "cannot allocate memory");
if ((bsdtar->resolver = archive_entry_linkresolver_new()) == NULL)
bsdtar_errc(1, 0, "cannot create link resolver");
archive_entry_linkresolver_set_strategy(bsdtar->resolver,
archive_format(a));
if ((bsdtar->diskreader = archive_read_disk_new()) == NULL)
bsdtar_errc(1, 0, "Cannot create read_disk object");
archive_read_disk_set_standard_lookup(bsdtar->diskreader);
if (bsdtar->names_from_file != NULL)
archive_names_from_file(bsdtar, a);
while (*bsdtar->argv) {
arg = *bsdtar->argv;
if (arg[0] == '-' && arg[1] == 'C') {
arg += 2;
if (*arg == '\0') {
bsdtar->argv++;
arg = *bsdtar->argv;
if (arg == NULL) {
bsdtar_warnc(0, "%s",
"Missing argument for -C");
bsdtar->return_value = 1;
goto cleanup;
}
}
set_chdir(bsdtar, arg);
} else {
if (*arg != '/' && (arg[0] != '@' || arg[1] != '/'))
do_chdir(bsdtar); /* Handle a deferred -C */
if (*arg == '@') {
if (append_archive_filename(bsdtar, a,
arg + 1) != 0)
break;
} else
#if defined(_WIN32) && !defined(__CYGWIN__)
write_hierarchy_win(bsdtar, a, arg,
write_hierarchy);
#else
write_hierarchy(bsdtar, a, arg);
#endif
}
bsdtar->argv++;
}
entry = NULL;
archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
while (entry != NULL) {
write_entry_backend(bsdtar, a, entry);
archive_entry_free(entry);
entry = NULL;
archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
}
if (archive_write_close(a)) {
bsdtar_warnc(0, "%s", archive_error_string(a));
bsdtar->return_value = 1;
}
cleanup:
/* Free file data buffer. */
free(bsdtar->buff);
archive_entry_linkresolver_free(bsdtar->resolver);
bsdtar->resolver = NULL;
archive_read_finish(bsdtar->diskreader);
bsdtar->diskreader = NULL;
if (bsdtar->option_totals) {
fprintf(stderr, "Total bytes written: %s\n",
tar_i64toa(archive_position_compressed(a)));
}
archive_write_finish(a);
}
/*
* Archive names specified in file.
*
* Unless --null was specified, a line containing exactly "-C" will
* cause the next line to be a directory to pass to chdir(). If
* --null is specified, then a line "-C" is just another filename.
*/
void
archive_names_from_file(struct bsdtar *bsdtar, struct archive *a)
{
bsdtar->archive = a;
bsdtar->next_line_is_dir = 0;
process_lines(bsdtar, bsdtar->names_from_file,
archive_names_from_file_helper);
if (bsdtar->next_line_is_dir)
bsdtar_errc(1, errno,
"Unexpected end of filename list; "
"directory expected after -C");
}
static int
archive_names_from_file_helper(struct bsdtar *bsdtar, const char *line)
{
if (bsdtar->next_line_is_dir) {
set_chdir(bsdtar, line);
bsdtar->next_line_is_dir = 0;
} else if (!bsdtar->option_null && strcmp(line, "-C") == 0)
bsdtar->next_line_is_dir = 1;
else {
if (*line != '/')
do_chdir(bsdtar); /* Handle a deferred -C */
write_hierarchy(bsdtar, bsdtar->archive, line);
}
return (0);
}
/*
* Copy from specified archive to current archive. Returns non-zero
* for write errors (which force us to terminate the entire archiving
* operation). If there are errors reading the input archive, we set
* bsdtar->return_value but return zero, so the overall archiving
* operation will complete and return non-zero.
*/
static int
append_archive_filename(struct bsdtar *bsdtar, struct archive *a,
const char *filename)
{
struct archive *ina;
int rc;
if (strcmp(filename, "-") == 0)
filename = NULL; /* Library uses NULL for stdio. */
ina = archive_read_new();
archive_read_support_format_all(ina);
archive_read_support_compression_all(ina);
if (archive_read_open_file(ina, filename, 10240)) {
bsdtar_warnc(0, "%s", archive_error_string(ina));
bsdtar->return_value = 1;
return (0);
}
rc = append_archive(bsdtar, a, ina);
if (rc != ARCHIVE_OK) {
bsdtar_warnc(0, "Error reading archive %s: %s",
filename, archive_error_string(ina));
bsdtar->return_value = 1;
}
archive_read_finish(ina);
return (rc);
}
static int
append_archive(struct bsdtar *bsdtar, struct archive *a, struct archive *ina)
{
struct archive_entry *in_entry;
int e;
while (0 == archive_read_next_header(ina, &in_entry)) {
if (!new_enough(bsdtar, archive_entry_pathname(in_entry),
archive_entry_stat(in_entry)))
continue;
if (excluded(bsdtar, archive_entry_pathname(in_entry)))
continue;
if (bsdtar->option_interactive &&
!yes("copy '%s'", archive_entry_pathname(in_entry)))
continue;
if (bsdtar->verbose)
safe_fprintf(stderr, "a %s",
archive_entry_pathname(in_entry));
if (need_report())
report_write(bsdtar, a, in_entry, 0);
e = archive_write_header(a, in_entry);
if (e != ARCHIVE_OK) {
if (!bsdtar->verbose)
bsdtar_warnc(0, "%s: %s",
archive_entry_pathname(in_entry),
archive_error_string(a));
else
fprintf(stderr, ": %s", archive_error_string(a));
}
if (e == ARCHIVE_FATAL)
exit(1);
if (e >= ARCHIVE_WARN) {
if (archive_entry_size(in_entry) == 0)
archive_read_data_skip(ina);
else if (copy_file_data(bsdtar, a, ina, in_entry))
exit(1);
}
if (bsdtar->verbose)
fprintf(stderr, "\n");
}
/* Note: If we got here, we saw no write errors, so return success. */
return (0);
}
/* Helper function to copy data between archives. */
static int
copy_file_data(struct bsdtar *bsdtar, struct archive *a,
struct archive *ina, struct archive_entry *entry)
{
ssize_t bytes_read;
ssize_t bytes_written;
int64_t progress = 0;
bytes_read = archive_read_data(ina, bsdtar->buff, FILEDATABUFLEN);
while (bytes_read > 0) {
if (need_report())
report_write(bsdtar, a, entry, progress);
bytes_written = archive_write_data(a, bsdtar->buff,
bytes_read);
if (bytes_written < bytes_read) {
bsdtar_warnc(0, "%s", archive_error_string(a));
return (-1);
}
progress += bytes_written;
bytes_read = archive_read_data(ina, bsdtar->buff,
FILEDATABUFLEN);
}
return (0);
}
/*
* Add the file or dir hierarchy named by 'path' to the archive
*/
static void
write_hierarchy(struct bsdtar *bsdtar, struct archive *a, const char *path)
{
struct archive_entry *entry = NULL, *spare_entry = NULL;
struct tree *tree;
char symlink_mode = bsdtar->symlink_mode;
dev_t first_dev = 0;
int dev_recorded = 0;
int tree_ret;
tree = tree_open(path);
if (!tree) {
bsdtar_warnc(errno, "%s: Cannot open", path);
bsdtar->return_value = 1;
return;
}
while ((tree_ret = tree_next(tree))) {
int r;
const char *name = tree_current_path(tree);
const struct stat *st = NULL; /* info to use for this entry */
const struct stat *lst = NULL; /* lstat() information */
int descend;
if (tree_ret == TREE_ERROR_FATAL)
bsdtar_errc(1, tree_errno(tree),
"%s: Unable to continue traversing directory tree",
name);
if (tree_ret == TREE_ERROR_DIR) {
bsdtar_warnc(errno,
"%s: Couldn't visit directory", name);
bsdtar->return_value = 1;
}
if (tree_ret != TREE_REGULAR)
continue;
/*
* If this file/dir is excluded by a filename
* pattern, skip it.
*/
if (excluded(bsdtar, name))
continue;
/*
* Get lstat() info from the tree library.
*/
lst = tree_current_lstat(tree);
if (lst == NULL) {
/* Couldn't lstat(); must not exist. */
bsdtar_warnc(errno, "%s: Cannot stat", name);
/* Return error if files disappear during traverse. */
bsdtar->return_value = 1;
continue;
}
/*
* Distinguish 'L'/'P'/'H' symlink following.
*/
switch(symlink_mode) {
case 'H':
/* 'H': After the first item, rest like 'P'. */
symlink_mode = 'P';
/* 'H': First item (from command line) like 'L'. */
/* FALLTHROUGH */
case 'L':
/* 'L': Do descend through a symlink to dir. */
descend = tree_current_is_dir(tree);
/* 'L': Follow symlinks to files. */
archive_read_disk_set_symlink_logical(bsdtar->diskreader);
/* 'L': Archive symlinks as targets, if we can. */
st = tree_current_stat(tree);
if (st != NULL)
break;
/* If stat fails, we have a broken symlink;
* in that case, don't follow the link. */
/* FALLTHROUGH */
default:
/* 'P': Don't descend through a symlink to dir. */
descend = tree_current_is_physical_dir(tree);
/* 'P': Don't follow symlinks to files. */
archive_read_disk_set_symlink_physical(bsdtar->diskreader);
/* 'P': Archive symlinks as symlinks. */
st = lst;
break;
}
/*
* If user has asked us not to cross mount points,
* then don't descend into into a dir on a different
* device.
*/
if (!dev_recorded) {
first_dev = lst->st_dev;
dev_recorded = 1;
}
if (bsdtar->option_dont_traverse_mounts) {
if (lst->st_dev != first_dev)
descend = 0;
}
/*
* In -u mode, check that the file is newer than what's
* already in the archive; in all modes, obey --newerXXX flags.
*/
if (!new_enough(bsdtar, name, st))
continue;
archive_entry_free(entry);
entry = archive_entry_new();
archive_entry_set_pathname(entry, name);
archive_entry_copy_sourcepath(entry,
tree_current_access_path(tree));
/* Populate the archive_entry with metadata from the disk. */
/* XXX TODO: Arrange to open a regular file before
* calling this so we can pass in an fd and shorten
* the race to query metadata. The linkify dance
* makes this more complex than it might sound. */
r = archive_read_disk_entry_from_file(bsdtar->diskreader,
entry, -1, st);
if (r != ARCHIVE_OK)
bsdtar_warnc(archive_errno(bsdtar->diskreader),
"%s", archive_error_string(bsdtar->diskreader));
if (r < ARCHIVE_WARN)
continue;
/* XXX TODO: Just use flag data from entry; avoid the
* duplicate check here. */
/*
* If this file/dir is flagged "nodump" and we're
* honoring such flags, skip this file/dir.
*/
#ifdef HAVE_STRUCT_STAT_ST_FLAGS
/* BSD systems store flags in struct stat */
if (bsdtar->option_honor_nodump &&
(lst->st_flags & UF_NODUMP))
continue;
#endif
#if defined(EXT2_IOC_GETFLAGS) && defined(EXT2_NODUMP_FL)
/* Linux uses ioctl to read flags. */
if (bsdtar->option_honor_nodump) {
int fd = open(name, O_RDONLY | O_NONBLOCK);
if (fd >= 0) {
unsigned long fflags;
int r = ioctl(fd, EXT2_IOC_GETFLAGS, &fflags);
close(fd);
if (r >= 0 && (fflags & EXT2_NODUMP_FL))
continue;
}
}
#endif
/*
* If the user vetoes this file/directory, skip it.
* We want this to be fairly late; if some other
* check would veto this file, we shouldn't bother
* the user with it.
*/
if (bsdtar->option_interactive &&
!yes("add '%s'", name))
continue;
/* Note: if user vetoes, we won't descend. */
if (descend && !bsdtar->option_no_subdirs)
tree_descend(tree);
/*
* Rewrite the pathname to be archived. If rewrite
* fails, skip the entry.
*/
if (edit_pathname(bsdtar, entry))
continue;
/* Display entry as we process it.
* This format is required by SUSv2. */
if (bsdtar->verbose)
safe_fprintf(stderr, "a %s",
archive_entry_pathname(entry));
/* Non-regular files get archived with zero size. */
if (!S_ISREG(st->st_mode))
archive_entry_set_size(entry, 0);
archive_entry_linkify(bsdtar->resolver, &entry, &spare_entry);
while (entry != NULL) {
write_entry_backend(bsdtar, a, entry);
archive_entry_free(entry);
entry = spare_entry;
spare_entry = NULL;
}
if (bsdtar->verbose)
fprintf(stderr, "\n");
}
archive_entry_free(entry);
tree_close(tree);
}
/*
* Backend for write_entry.
*/
static void
write_entry_backend(struct bsdtar *bsdtar, struct archive *a,
struct archive_entry *entry)
{
int fd = -1;
int e;
if (archive_entry_size(entry) > 0) {
const char *pathname = archive_entry_sourcepath(entry);
fd = open(pathname, O_RDONLY);
if (fd == -1) {
if (!bsdtar->verbose)
bsdtar_warnc(errno,
"%s: could not open file", pathname);
else
fprintf(stderr, ": %s", strerror(errno));
return;
}
}
e = archive_write_header(a, entry);
if (e != ARCHIVE_OK) {
if (!bsdtar->verbose)
bsdtar_warnc(0, "%s: %s",
archive_entry_pathname(entry),
archive_error_string(a));
else
fprintf(stderr, ": %s", archive_error_string(a));
}
if (e == ARCHIVE_FATAL)
exit(1);
/*
* If we opened a file earlier, write it out now. Note that
* the format handler might have reset the size field to zero
* to inform us that the archive body won't get stored. In
* that case, just skip the write.
*/
if (e >= ARCHIVE_WARN && fd >= 0 && archive_entry_size(entry) > 0) {
if (write_file_data(bsdtar, a, entry, fd))
exit(1);
}
/*
* If we opened a file, close it now even if there was an error
* which made us decide not to write the archive body.
*/
if (fd >= 0)
close(fd);
}
static void
report_write(struct bsdtar *bsdtar, struct archive *a,
struct archive_entry *entry, int64_t progress)
{
uint64_t comp, uncomp;
if (bsdtar->verbose)
fprintf(stderr, "\n");
comp = archive_position_compressed(a);
uncomp = archive_position_uncompressed(a);
fprintf(stderr, "In: %d files, %s bytes;",
archive_file_count(a), tar_i64toa(uncomp));
fprintf(stderr,
" Out: %s bytes, compression %d%%\n",
tar_i64toa(comp), (int)((uncomp - comp) * 100 / uncomp));
/* Can't have two calls to tar_i64toa() pending, so split the output. */
safe_fprintf(stderr, "Current: %s (%s",
archive_entry_pathname(entry),
tar_i64toa(progress));
fprintf(stderr, "/%s bytes)\n",
tar_i64toa(archive_entry_size(entry)));
}
/* Helper function to copy file to archive. */
static int
write_file_data(struct bsdtar *bsdtar, struct archive *a,
struct archive_entry *entry, int fd)
{
ssize_t bytes_read;
ssize_t bytes_written;
int64_t progress = 0;
bytes_read = read(fd, bsdtar->buff, FILEDATABUFLEN);
while (bytes_read > 0) {
if (need_report())
report_write(bsdtar, a, entry, progress);
bytes_written = archive_write_data(a, bsdtar->buff,
bytes_read);
if (bytes_written < 0) {
/* Write failed; this is bad */
bsdtar_warnc(0, "%s", archive_error_string(a));
return (-1);
}
if (bytes_written < bytes_read) {
/* Write was truncated; warn but continue. */
bsdtar_warnc(0,
"%s: Truncated write; file may have grown while being archived.",
archive_entry_pathname(entry));
return (0);
}
progress += bytes_written;
bytes_read = read(fd, bsdtar->buff, FILEDATABUFLEN);
}
return 0;
}
/*
* Test if the specified file is new enough to include in the archive.
*/
int
new_enough(struct bsdtar *bsdtar, const char *path, const struct stat *st)
{
struct archive_dir_entry *p;
/*
* If this file/dir is excluded by a time comparison, skip it.
*/
if (bsdtar->newer_ctime_sec > 0) {
if (st->st_ctime < bsdtar->newer_ctime_sec)
return (0); /* Too old, skip it. */
if (st->st_ctime == bsdtar->newer_ctime_sec
&& ARCHIVE_STAT_CTIME_NANOS(st)
<= bsdtar->newer_ctime_nsec)
return (0); /* Too old, skip it. */
}
if (bsdtar->newer_mtime_sec > 0) {
if (st->st_mtime < bsdtar->newer_mtime_sec)
return (0); /* Too old, skip it. */
if (st->st_mtime == bsdtar->newer_mtime_sec
&& ARCHIVE_STAT_MTIME_NANOS(st)
<= bsdtar->newer_mtime_nsec)
return (0); /* Too old, skip it. */
}
/*
* In -u mode, we only write an entry if it's newer than
* what was already in the archive.
*/
if (bsdtar->archive_dir != NULL &&
bsdtar->archive_dir->head != NULL) {
for (p = bsdtar->archive_dir->head; p != NULL; p = p->next) {
if (pathcmp(path, p->name)==0)
return (p->mtime_sec < st->st_mtime ||
(p->mtime_sec == st->st_mtime &&
p->mtime_nsec
< ARCHIVE_STAT_MTIME_NANOS(st)));
}
}
/* If the file wasn't rejected, include it. */
return (1);
}
/*
* Add an entry to the dir list for 'u' mode.
*
* XXX TODO: Make this fast.
*/
static void
add_dir_list(struct bsdtar *bsdtar, const char *path,
time_t mtime_sec, int mtime_nsec)
{
struct archive_dir_entry *p;
/*
* Search entire list to see if this file has appeared before.
* If it has, override the timestamp data.
*/
p = bsdtar->archive_dir->head;
while (p != NULL) {
if (strcmp(path, p->name)==0) {
p->mtime_sec = mtime_sec;
p->mtime_nsec = mtime_nsec;
return;
}
p = p->next;
}
p = malloc(sizeof(*p));
if (p == NULL)
bsdtar_errc(1, ENOMEM, "Can't read archive directory");
p->name = strdup(path);
if (p->name == NULL)
bsdtar_errc(1, ENOMEM, "Can't read archive directory");
p->mtime_sec = mtime_sec;
p->mtime_nsec = mtime_nsec;
p->next = NULL;
if (bsdtar->archive_dir->tail == NULL) {
bsdtar->archive_dir->head = bsdtar->archive_dir->tail = p;
} else {
bsdtar->archive_dir->tail->next = p;
bsdtar->archive_dir->tail = p;
}
}
void
test_for_append(struct bsdtar *bsdtar)
{
struct stat s;
if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
bsdtar_errc(1, 0, "no files or directories specified");
if (bsdtar->filename == NULL)
bsdtar_errc(1, 0, "Cannot append to stdout.");
if (bsdtar->create_compression != 0)
bsdtar_errc(1, 0,
"Cannot append to %s with compression", bsdtar->filename);
if (stat(bsdtar->filename, &s) != 0)
return;
if (!S_ISREG(s.st_mode) && !S_ISBLK(s.st_mode))
bsdtar_errc(1, 0,
"Cannot append to %s: not a regular file.",
bsdtar->filename);
}
|