summaryrefslogtreecommitdiffstats
path: root/usr.bin/tar/write.c
blob: dace199dd44ec3bc500fcd48ddfeb92b5137f570 (plain)
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
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
/*-
 * Copyright (c) 2003-2004 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
 *    in this position and unchanged.
 * 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$");

#include <sys/stat.h>
#include <sys/types.h>
#ifdef HAVE_POSIX_ACL
#include <sys/acl.h>
#endif
#include <archive.h>
#include <archive_entry.h>
#include <errno.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <fts.h>
#include <grp.h>
#include <limits.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "bsdtar.h"

/* Fixed size of uname/gname cache. */
#define gname_cache_size 101
#define uname_cache_size 101

/* Initial size of link cache. */
#define links_cache_initial_size 1024

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 gname_cache {
	struct {
		gid_t id;
		char *name;
	} cache[gname_cache_size];
};

struct links_cache {
	unsigned long		  number_entries;
	size_t			  number_buckets;
	struct links_entry	**buckets;
	char			  stop_allocating;
};

struct links_entry {
	struct links_entry	*next;
	struct links_entry	*previous;
	int			 links;
	dev_t			 dev;
	ino_t			 ino;
	char			*name;
};


struct uname_cache {
	struct {
		uid_t id;
		char *name;
	} cache[uname_cache_size];
};

static void		 add_dir_list(struct bsdtar *bsdtar, const char *path,
			     time_t mtime_sec, int mtime_nsec);
static void		 archive_names_from_file(struct bsdtar *bsdtar,
			     struct archive *a);
static void		 create_cleanup(struct bsdtar *);
static int		 append_archive(struct bsdtar *, struct archive *,
			     const char *fname);
static const char *	 lookup_gname(struct bsdtar *bsdtar, gid_t gid);
static void		 lookup_hardlink(struct bsdtar *,
			     struct archive_entry *entry, const struct stat *);
static const char *	 lookup_uname(struct bsdtar *bsdtar, uid_t uid);
static int		 new_enough(struct bsdtar *, const char *path,
			     time_t mtime_sec, int mtime_nsec);
static void		 setup_acls(struct bsdtar *, struct archive_entry *,
			     const char *path);
static void		 test_for_append(struct bsdtar *);
static void		 write_archive(struct archive *, struct bsdtar *);
static void		 write_entry(struct bsdtar *, struct archive *,
			     struct stat *, const char *pathname,
			     unsigned pathlen, const char *accpath);
static int		 write_file_data(struct bsdtar *, struct archive *,
			     int fd);
static void		 write_heirarchy(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)
		archive_write_set_format_pax_restricted(a);
	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();
		}
	}

	archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);

	switch (bsdtar->create_compression) {
	case 'j': case 'y':
		archive_write_set_compression_bzip2(a);
		break;
	case 'z':
		archive_write_set_compression_gzip(a);
		break;
	}

	r = archive_write_open_file(a, bsdtar->filename);
	if (r != ARCHIVE_OK)
		bsdtar_errc(1, archive_errno(a),
		    archive_error_string(a));

	write_archive(a, bsdtar);

	archive_write_finish(a);
}

/*
 * Same as 'c', except we only support tar formats in uncompressed
 * files on disk.
 */
void
tar_mode_r(struct bsdtar *bsdtar)
{
	off_t	end_offset;
	int	format;
	struct archive *a;
	struct archive_entry *entry;

	/* Sanity-test some arguments and the file. */
	test_for_append(bsdtar);

	format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;

	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);
	archive_read_open_fd(a, bsdtar->fd, 10240);
	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 format to same one auto-detected above, except use
	 * ustar for appending to GNU tar, since the library doesn't
	 * write GNU tar format.
	 */
	if (format == ARCHIVE_FORMAT_TAR_GNUTAR)
		format = ARCHIVE_FORMAT_TAR_USTAR;
	archive_write_set_format(a, format);
	lseek(bsdtar->fd, end_offset, SEEK_SET);
	archive_write_open_fd(a, bsdtar->fd);

	write_archive(a, bsdtar);

	archive_write_finish(a);
	close(bsdtar->fd);
	bsdtar->fd = -1;
}

void
tar_mode_u(struct bsdtar *bsdtar)
{
	off_t			 end_offset;
	struct archive		*a;
	struct archive_entry	*entry;
	const char		*filename;
	int			 format;
	struct archive_dir_entry	*p;
	struct archive_dir	 archive_dir;

	bsdtar->archive_dir = &archive_dir;
	memset(&archive_dir, 0, sizeof(archive_dir));

	filename = NULL;
	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);
	archive_read_open_fd(a, bsdtar->fd, bsdtar->bytes_per_block);

	/* 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);
	archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
	lseek(bsdtar->fd, end_offset, SEEK_SET);
	ftruncate(bsdtar->fd, end_offset);
	archive_write_open_fd(a, bsdtar->fd);

	write_archive(a, bsdtar);

	archive_write_finish(a);
	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;
	char *pending_dir;

	pending_dir = NULL;

	if (bsdtar->start_dir != NULL && chdir(bsdtar->start_dir))
		bsdtar_errc(1, errno, "chdir(%s) failed", bsdtar->start_dir);

	if (bsdtar->names_from_file != NULL)
		archive_names_from_file(bsdtar, a);

	while (*bsdtar->argv) {
		arg = *bsdtar->argv;
		if (arg[0] == 'C' && arg[1] == '=') {
			arg += 2;

			/*-
			 * The logic here for C=<dir> attempts to avoid
			 * chdir() as long as possible.  For example:
			 * "C=/foo C=/bar file"
			 *    needs chdir("/bar") but not chdir("/foo")
			 * "C=/foo C=bar file"
			 *    needs chdir("/foo/bar")
			 * "C=/foo C=bar /file1"
			 *    does not need chdir()
			 * "C=/foo C=bar /file1 file2"
			 *    needs chdir("/foo/bar") before file2
			 *
			 * The only correct way to handle this is to
			 * record a "pending" chdir request and only
			 * execute the real chdir when a non-absolute
			 * filename is seen on the command line.
			 *
			 * I went to all this work so that programs
			 * that build tar command lines don't have to
			 * worry about C= with non-existent
			 * directories; such requests will only fail
			 * if the directory must be accessed.
			 */
			if (pending_dir && *arg == '/') {
				/* The C=/foo C=/bar case; dump first one. */
				free(pending_dir);
				pending_dir = NULL;
			}
			if (pending_dir) {
				/* The C=/foo C=bar case; concatenate */
				char *old_pending = pending_dir;
				int old_len = strlen(old_pending);

				pending_dir =
				    malloc(old_len + 1 + strlen(arg));
				strcpy(pending_dir, old_pending);
				if (pending_dir[old_len - 1] != '/') {
					pending_dir[old_len] = '/';
					old_len ++;
				}
				strcpy(pending_dir + old_len, arg);
			} else {
				/* Easy case: no previously-saved dir. */
				pending_dir = strdup(arg);
			}
		} else {
			if (pending_dir &&
			    (*arg != '/' || (*arg == '@' && arg[1] != '/'))) {
				/* Handle a deferred -C request, see
				 * comments above. */
				if (chdir(pending_dir))
					bsdtar_errc(1, 0,
					    "could not chdir to '%s'\n",
					    pending_dir);
				free(pending_dir);
				pending_dir = NULL;
			}

			if (*arg == '@')
				append_archive(bsdtar, a, arg+1);
			else
				write_heirarchy(bsdtar, a, arg);
		}
		bsdtar->argv++;
	}

	create_cleanup(bsdtar);
}

/* Archive names specified in file. */
void
archive_names_from_file(struct bsdtar *bsdtar, struct archive *a)
{
	FILE *f;
	char buff[1024];
	int l;

	if (strcmp(bsdtar->names_from_file, "-") == 0)
		f = stdin;
	else
		f = fopen(bsdtar->names_from_file, "r");

	while (fgets(buff, sizeof(buff), f) != NULL) {
		l = strlen(buff);
		if (buff[l-1] == '\n')
			buff[l-1] = '\0';

		/*
		 * This -C processing hack is really quite ugly, but
		 * gtar does it this way and pkg_create depends on it,
		 * so I guess I'm stuck having to implement it.
		 *
		 * Someday, pkg_create will just use libarchive directly
		 * and this will just be a bad memory.
		 */
		if (strcmp(buff, "-C") == 0) {
			if (fgets(buff, sizeof(buff), f) == NULL)
				bsdtar_errc(1, errno,
				    "Unexpected end of filename list; "
				    "directory expected after -C");
			l = strlen(buff);
			if (buff[l-1] == '\n')
				buff[l-1] = '\0';
			if (chdir(buff))
				bsdtar_errc(1, errno, "chdir(%s) failed", buff);
		} else {
			write_heirarchy(bsdtar, a, buff);
		}
	}

	if (strcmp(bsdtar->names_from_file, "-") != 0)
		fclose(f);
}

/* Copy from specified archive to current archive. */
static int
append_archive(struct bsdtar *bsdtar, struct archive *a, const char *filename)
{
	struct archive *ina;
	struct archive_entry *in_entry;
	int bytes_read, bytes_written;
	char buff[8192];

	ina = archive_read_new();
	archive_read_support_format_all(ina);
	archive_read_support_compression_all(ina);
	archive_read_open_file(ina, filename, 10240);
	while (0 == archive_read_next_header(ina, &in_entry)) {
		if (!new_enough(bsdtar, archive_entry_pathname(in_entry),
			archive_entry_mtime(in_entry),
			archive_entry_mtime_nsec(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));
		/* XXX handle/report errors XXX */
		archive_write_header(a, in_entry);
		bytes_read = archive_read_data(ina, buff, sizeof(buff));
		while (bytes_read > 0) {
			bytes_written =
			    archive_write_data(a, buff, bytes_read);
			if (bytes_written < bytes_read) {
				bsdtar_warnc( archive_errno(a), "%s",
				    archive_error_string(a));
				exit(1);
			}
			bytes_read =
			    archive_read_data(ina, buff, sizeof(buff));
		}
		if (bsdtar->verbose)
			fprintf(stderr, "\n");

	}
	if (archive_errno(ina))
		bsdtar_warnc(0, "Error reading archive %s: %s", filename,
		    archive_error_string(ina));

	return (0); /* TODO: Return non-zero on error */
}

/*
 * Add the file or dir heirarchy named by 'path' to the archive
 */
static void
write_heirarchy(struct bsdtar *bsdtar, struct archive *a, const char *path)
{
	FTS	*fts;
	FTSENT	*ftsent;
	int	 ftsoptions;
	char	*fts_argv[2];

	/*
	 * Sigh: fts_open modifies it's first parameter, so we have to
	 * copy 'path' to mutable storage.
	 */
	fts_argv[0] = strdup(path);
	fts_argv[1] = NULL;
	ftsoptions = FTS_PHYSICAL;
	switch (bsdtar->symlink_mode) {
	case 'H':
		ftsoptions |= FTS_COMFOLLOW;
		break;
	case 'L':
		ftsoptions = FTS_COMFOLLOW | FTS_LOGICAL;
		break;
	}
	if (bsdtar->option_dont_traverse_mounts)
		ftsoptions |= FTS_XDEV;

	fts = fts_open(fts_argv, ftsoptions, NULL);


	if (!fts) {
		bsdtar_warnc(errno, "%s: Cannot open", path);
		return;
	}

	while ((ftsent = fts_read(fts))) {
		switch (ftsent->fts_info) {
		case FTS_NS:
			bsdtar_warnc(ftsent->fts_errno, "%s: Could not stat",
			    ftsent->fts_path);
			break;
		case FTS_ERR:
			bsdtar_warnc(ftsent->fts_errno, "%s", ftsent->fts_path);
			break;
		case FTS_DNR:
			bsdtar_warnc(ftsent->fts_errno,
			    "%s: Cannot read directory contents",
			    ftsent->fts_path);
			break;
		case FTS_W:  /* Skip Whiteout entries */
			break;
		case FTS_DC: /* Directory that causes cycle */
			/* XXX Does this need special handling ? */
			break;
		case FTS_D:
			/*
			 * If this dir is flagged "nodump" and we're
			 * honoring such flags, tell FTS to skip the
			 * entire tree and don't write the entry for the
			 * directory itself.
			 */
#ifdef HAVE_CHFLAGS
			if (bsdtar->option_honor_nodump &&
			    (ftsent->fts_statp->st_flags & UF_NODUMP)) {
				fts_set(fts, ftsent, FTS_SKIP);
				break;
			}
#endif

			/*
			 * In -u mode, we need to check whether this
			 * is newer than what's already in the archive.
			 */
			if (!new_enough(bsdtar, ftsent->fts_path,
				ftsent->fts_statp->st_mtime,
				ftsent->fts_statp->st_mtimespec.tv_nsec))
				break;
			/*
			 * If this dir is excluded by a filename
			 * pattern, tell FTS to skip the entire tree
			 * and don't write the entry for the directory
			 * itself.
			 */
			if (excluded(bsdtar, ftsent->fts_path)) {
				fts_set(fts, ftsent, FTS_SKIP);
				break;
			}

			/*
			 * If the user vetoes the directory, skip
			 * the whole thing.
			 */
			if (bsdtar->option_interactive &&
			    !yes("add '%s'", ftsent->fts_path)) {
				fts_set(fts, ftsent, FTS_SKIP);
				break;
			}

			/*
			 * If we're not recursing, tell FTS to skip the
			 * tree but do fall through and write the entry
			 * for the dir itself.
			 */
			if (bsdtar->option_no_subdirs)
				fts_set(fts, ftsent, FTS_SKIP);
			write_entry(bsdtar, a, ftsent->fts_statp,
			    ftsent->fts_path, ftsent->fts_pathlen,
			    ftsent->fts_accpath);
			break;
		case FTS_F:
		case FTS_SL:
		case FTS_SLNONE:
		case FTS_DEFAULT:
			/*
			 * Skip this file if it's flagged "nodump" and we're
			 * honoring that flag.
			 */
#ifdef HAVE_CHFLAGS
			if (bsdtar->option_honor_nodump &&
			    (ftsent->fts_statp->st_flags & UF_NODUMP))
				break;
#endif
			/*
			 * Skip this file if it's excluded by a
			 * filename pattern.
			 */
			if (excluded(bsdtar, ftsent->fts_path))
				break;

			/*
			 * In -u mode, we need to check whether this
			 * is newer than what's already in the archive.
			 */
			if (!new_enough(bsdtar, ftsent->fts_path,
				ftsent->fts_statp->st_mtime,
				ftsent->fts_statp->st_mtimespec.tv_nsec))
				break;

			if (bsdtar->option_interactive &&
			    !yes("add '%s'", ftsent->fts_path)) {
				break;
			}

			write_entry(bsdtar, a, ftsent->fts_statp,
			    ftsent->fts_path, ftsent->fts_pathlen,
			    ftsent->fts_accpath);
			break;
		case FTS_DP:
			break;
		default:
			bsdtar_warnc(0, "%s: Heirarchy traversal error %d\n",
			    ftsent->fts_path,
			    ftsent->fts_info);
			break;
		}

	}
	if (errno)
		bsdtar_warnc(errno, "%s", path);
	if (fts_close(fts))
		bsdtar_warnc(errno, "fts_close failed");
	free(fts_argv[0]);
}

/*
 * Add a single filesystem object to the archive.
 */
static void
write_entry(struct bsdtar *bsdtar, struct archive *a, struct stat *st,
    const char *pathname, unsigned pathlen, const char *accpath)
{
	struct archive_entry	*entry;
	int			 e;
	int			 fd;
	char			*fflags = NULL;
	static char		 linkbuffer[PATH_MAX+1];

	(void)pathlen; /* UNUSED */

	fd = -1;
	entry = archive_entry_new();

	/* Non-regular files get archived with zero size. */
	if (!S_ISREG(st->st_mode))
		st->st_size = 0;

	/* Strip redundant "./" from start of filename. */
	if (pathname && pathname[0] == '.' && pathname[1] == '/') {
		pathname += 2;
		if (*pathname == 0)	/* This is the "./" directory. */
			goto cleanup;	/* Don't archive it ever. */
	}

	/* Strip leading '/' unless user has asked us not to. */
	if (pathname && pathname[0] == '/' && !bsdtar->option_absolute_paths)
		pathname++;

	archive_entry_set_pathname(entry, pathname);

	if (!S_ISDIR(st->st_mode) && (st->st_nlink > 1))
		lookup_hardlink(bsdtar, entry, st);

	/* Display entry as we process it. This format is required by SUSv2. */
	if (bsdtar->verbose)
		safe_fprintf(stderr, "a %s", pathname);

	/* Read symbolic link information. */
	if ((st->st_mode & S_IFMT) == S_IFLNK) {
		int lnklen;

		lnklen = readlink(accpath, linkbuffer, PATH_MAX);
		if (lnklen < 0) {
			if (!bsdtar->verbose)
				bsdtar_warnc(errno,
				    "%s: Couldn't read symbolic link",
				    pathname);
			else
				safe_fprintf(stderr,
				    ": Couldn't read symbolic link: %s",
				    strerror(errno));
			goto cleanup;
		}
		linkbuffer[lnklen] = 0;
		archive_entry_set_symlink(entry, linkbuffer);
	}

	/* Look up username and group name. */
	archive_entry_set_uname(entry, lookup_uname(bsdtar, st->st_uid));
	archive_entry_set_gname(entry, lookup_gname(bsdtar, st->st_gid));

#ifdef HAVE_CHFLAGS
	if (st->st_flags != 0)
		archive_entry_set_fflags(entry, st->st_flags, 0);
#endif

        archive_entry_copy_stat(entry, st);
	setup_acls(bsdtar, entry, accpath);

	/*
	 * If it's a regular file (and non-zero in size) make sure we
	 * can open it before we start to write.  In particular, note
	 * that we can always archive a zero-length file, even if we
	 * can't read it.
	 */
	if (S_ISREG(st->st_mode) && st->st_size > 0) {
		fd = open(accpath, O_RDONLY);
		if (fd < 0) {
			if (!bsdtar->verbose)
				bsdtar_warnc(errno, "%s", pathname);
			else
				fprintf(stderr, ": %s", strerror(errno));
			goto cleanup;
		}
	}

	e = archive_write_header(a, entry);
	if (e != ARCHIVE_OK) {
		if (!bsdtar->verbose)
			bsdtar_warnc(0, "%s: %s", pathname,
			    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 (fd >= 0 && archive_entry_size(entry) > 0)
		write_file_data(bsdtar, a, fd);

cleanup:
	if (fd >= 0)
		close(fd);

	if (entry != NULL)
		archive_entry_free(entry);

	if (bsdtar->verbose)
		fprintf(stderr, "\n");

	if (fflags != NULL) free(fflags);
}


/* Helper function to copy file to archive, with stack-allocated buffer. */
static int
write_file_data(struct bsdtar *bsdtar, struct archive *a, int fd)
{
	char	buff[64*1024];
	ssize_t	bytes_read;
	ssize_t	bytes_written;

	/* XXX TODO: Allocate buffer on heap and store pointer to
	 * it in bsdtar structure; arrange cleanup as well. XXX */
	(void)bsdtar;

	bytes_read = read(fd, buff, sizeof(buff));
	while (bytes_read > 0) {
		bytes_written = archive_write_data(a, buff, bytes_read);

		if (bytes_written == 0 && errno) {
			return -1; /* Write failed; this is bad */
		}
		bytes_read = read(fd, buff, sizeof(buff));
	}
	return 0;
}


static void
create_cleanup(struct bsdtar * bsdtar)
{
	struct links_cache *links_cache;
	struct uname_cache *ucache;
	struct gname_cache *gcache;
	size_t i;


	/* Free inode->pathname map used for hardlink detection. */
	if (bsdtar->links_cache != NULL) {
		links_cache = bsdtar->links_cache;
		if (links_cache->buckets != NULL) {
			for (i = 0; i < links_cache->number_buckets; i++) {
				while (links_cache->buckets[i] != NULL) {
					struct links_entry *lp =
					    links_cache->buckets[i]->next;
					if (bsdtar->option_warn_links)
						bsdtar_warnc(0,
						    "Missing links to %s",
						    links_cache->buckets[i]->name);
					if (links_cache->buckets[i]->name != NULL)
						free(links_cache->buckets[i]->name);
					free(links_cache->buckets[i]);
					links_cache->buckets[i] = lp;
				}
			}
			free(links_cache->buckets);
		}
		free(bsdtar->links_cache);
		bsdtar->links_cache = NULL;
	}

	if (bsdtar->uname_cache != NULL) {
		ucache = bsdtar->uname_cache;
		for(i = 0; i < uname_cache_size; i++) {
			if (ucache->cache[i].name != NULL)
				free(ucache->cache[i].name);
		}
		free(ucache);
		bsdtar->uname_cache = NULL;
	}

	if (bsdtar->gname_cache != NULL) {
		gcache = bsdtar->gname_cache;
		for(i = 0; i < gname_cache_size; i++) {
			if (gcache->cache[i].name != NULL)
				free(gcache->cache[i].name);
		}
		free(gcache);
		bsdtar->gname_cache = NULL;
	}
}


static void
lookup_hardlink(struct bsdtar *bsdtar, struct archive_entry *entry,
    const struct stat *st)
{
	struct links_cache	*links_cache;
	struct links_entry	*le, **new_buckets;
	int			 hash;
	size_t			 i, new_size;

	/* If necessary, initialize the links cache. */
	links_cache = bsdtar->links_cache;
	if (links_cache == NULL) {
		bsdtar->links_cache = malloc(sizeof(struct links_cache));
		if (bsdtar->links_cache == NULL)
			bsdtar_errc(1, ENOMEM,
			    "No memory for hardlink detection.");
		links_cache = bsdtar->links_cache;
		memset(links_cache, 0, sizeof(struct links_cache));
		links_cache->number_buckets = links_cache_initial_size;
		links_cache->buckets = malloc(links_cache->number_buckets *
		    sizeof(links_cache->buckets[0]));
		if (links_cache->buckets == NULL) {
			bsdtar_errc(1, ENOMEM,
			    "No memory for hardlink detection.");
		}
		for (i = 0; i < links_cache->number_buckets; i++)
			links_cache->buckets[i] = NULL;
	}

	/* If the links cache is getting too full, enlarge the hash table. */
	if (links_cache->number_entries > links_cache->number_buckets * 2 &&
	    !links_cache->stop_allocating)
	{
		int count;

		new_size = links_cache->number_buckets * 2;
		new_buckets = malloc(new_size * sizeof(struct links_entry *));

		count = 0;

		if (new_buckets != NULL) {
			memset(new_buckets, 0,
			    new_size * sizeof(struct links_entry *));
			for (i = 0; i < links_cache->number_buckets; i++) {
				while (links_cache->buckets[i] != NULL) {
					/* Remove entry from old bucket. */
					le = links_cache->buckets[i];
					links_cache->buckets[i] = le->next;

					/* Add entry to new bucket. */
					hash = (le->dev ^ le->ino) % new_size;

					if (new_buckets[hash] != NULL)
						new_buckets[hash]->previous =
						    le;
					le->next = new_buckets[hash];
					le->previous = NULL;
					new_buckets[hash] = le;
				}
			}
			free(links_cache->buckets);
			links_cache->buckets = new_buckets;
			links_cache->number_buckets = new_size;
		} else {
			links_cache->stop_allocating = 1;
			bsdtar_warnc(ENOMEM, "No more memory for recording "
			    "hard links; Remaining hard links will be "
			    "dumped as full files.");
		}
	}

	/* Try to locate this entry in the links cache. */
	hash = ( st->st_dev ^ st->st_ino ) % links_cache->number_buckets;
	for (le = links_cache->buckets[hash]; le != NULL; le = le->next) {
		if (le->dev == st->st_dev && le->ino == st->st_ino) {
			archive_entry_copy_hardlink(entry, le->name);

			/*
			 * Decrement link count each time and release
			 * the entry if it hits zero.  This saves
			 * memory and is necessary for proper -l
			 * implementation.
			 */
			if (--le->links <= 0) {
				if (le->previous != NULL)
					le->previous->next = le->next;
				if (le->next != NULL)
					le->next->previous = le->previous;
				if (le->name != NULL)
					free(le->name);
				if (links_cache->buckets[hash] == le)
					links_cache->buckets[hash] = le->next;
				links_cache->number_entries--;
				free(le);
			}

			return;
		}
	}

	if (links_cache->stop_allocating)
		return;

	/* Add this entry to the links cache. */
	le = malloc(sizeof(struct links_entry));
	if (le == NULL) {
		links_cache->stop_allocating = 1;
		bsdtar_warnc(ENOMEM, "No more memory for recording "
		    "hard links; Remaining hard links will be dumped "
		    "as full files.");
		return;
	}
	if (links_cache->buckets[hash] != NULL)
		links_cache->buckets[hash]->previous = le;
	links_cache->number_entries++;
	le->next = links_cache->buckets[hash];
	le->previous = NULL;
	links_cache->buckets[hash] = le;
	le->dev = st->st_dev;
	le->ino = st->st_ino;
	le->links = st->st_nlink - 1;
	le->name = strdup(archive_entry_pathname(entry));
}

#ifdef HAVE_POSIX_ACL
void			setup_acl(struct bsdtar *bsdtar,
			     struct archive_entry *entry, const char *accpath,
			     int acl_type, int archive_entry_acl_type);

void
setup_acls(struct bsdtar *bsdtar, struct archive_entry *entry,
    const char *accpath)
{
	archive_entry_acl_clear(entry);

	setup_acl(bsdtar, entry, accpath,
	    ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
	/* Only directories can have default ACLs. */
	if (S_ISDIR(archive_entry_mode(entry)))
		setup_acl(bsdtar, entry, accpath,
		    ACL_TYPE_DEFAULT, ARCHIVE_ENTRY_ACL_TYPE_DEFAULT);
}

void
setup_acl(struct bsdtar *bsdtar, struct archive_entry *entry,
    const char *accpath, int acl_type, int archive_entry_acl_type)
{
	acl_t		 acl;
	acl_tag_t	 acl_tag;
	acl_entry_t	 acl_entry;
	acl_permset_t	 acl_permset;
	int		 s, ae_id, ae_tag, ae_perm;
	const char	*ae_name;

	/* Retrieve access ACL from file. */
	acl = acl_get_file(accpath, acl_type);
	if (acl != NULL) {
		s = acl_get_entry(acl, ACL_FIRST_ENTRY, &acl_entry);
		while (s == 1) {
			ae_id = -1;
			ae_name = NULL;

			acl_get_tag_type(acl_entry, &acl_tag);
			if (acl_tag == ACL_USER) {
				ae_id = (int)*(uid_t *)acl_get_qualifier(acl_entry);
				ae_name = lookup_uname(bsdtar, ae_id);
				ae_tag = ARCHIVE_ENTRY_ACL_USER;
			} else if (acl_tag == ACL_GROUP) {
				ae_id = (int)*(gid_t *)acl_get_qualifier(acl_entry);
				ae_name = lookup_gname(bsdtar, ae_id);
				ae_tag = ARCHIVE_ENTRY_ACL_GROUP;
			} else if (acl_tag == ACL_MASK) {
				ae_tag = ARCHIVE_ENTRY_ACL_MASK;
			} else if (acl_tag == ACL_USER_OBJ) {
				ae_tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
			} else if (acl_tag == ACL_GROUP_OBJ) {
				ae_tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
			} else if (acl_tag == ACL_OTHER) {
				ae_tag = ARCHIVE_ENTRY_ACL_OTHER;
			} else {
				/* Skip types that libarchive can't support. */
				continue;
			}

			acl_get_permset(acl_entry, &acl_permset);
			ae_perm = 0;
			if (acl_get_perm_np(acl_permset, ACL_EXECUTE))
				ae_perm |= ARCHIVE_ENTRY_ACL_EXECUTE;
			if (acl_get_perm_np(acl_permset, ACL_READ))
				ae_perm |= ARCHIVE_ENTRY_ACL_READ;
			if (acl_get_perm_np(acl_permset, ACL_WRITE))
				ae_perm |= ARCHIVE_ENTRY_ACL_WRITE;

			archive_entry_acl_add_entry(entry,
			    archive_entry_acl_type, ae_perm, ae_tag,
			    ae_id, ae_name);

			s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
		}
		acl_free(acl);
	}
}
#else
void
setup_acls(struct bsdtar *bsdtar, struct archive_entry *entry,
    const char *accpath)
{
	(void)bsdtar;
	(void)entry;
	(void)accpath;
}
#endif

/*
 * Lookup gid from gname and uid from uname.
 *
 */
const char *
lookup_uname(struct bsdtar *bsdtar, uid_t uid)
{
	struct passwd		*pwent;
	struct uname_cache	*cache;
	int slot;


	if (bsdtar->uname_cache == NULL) {
		bsdtar->uname_cache = malloc(sizeof(struct uname_cache));
		memset(bsdtar->uname_cache, 0, sizeof(struct uname_cache));
	}

	cache = bsdtar->uname_cache;

	slot = uid % uname_cache_size;
	if (cache->cache[slot].name != NULL) {
		if (cache->cache[slot].id == uid)
			return (cache->cache[slot].name);

		free(cache->cache[slot].name);
		cache->cache[slot].name = NULL;
	}

	pwent = getpwuid(uid);
	if (pwent == NULL) {
		if (errno)
			bsdtar_warnc(errno, "getpwuid(%d) failed", uid);
		return (NULL);
	} else if (pwent->pw_name != NULL && pwent->pw_name[0] != '\0') {
		cache->cache[slot].name = strdup(pwent->pw_name);
		cache->cache[slot].id = uid;
		return (cache->cache[slot].name);
	}
	return (NULL);
}

const char *
lookup_gname(struct bsdtar *bsdtar, gid_t gid)
{
	struct group		*grent;
	struct gname_cache	*cache;
	int slot;

	if (bsdtar->gname_cache == NULL) {
		bsdtar->gname_cache = malloc(sizeof(struct gname_cache));
		memset(bsdtar->gname_cache, 0, sizeof(struct gname_cache));
	}

	cache = bsdtar->gname_cache;
	slot = gid % gname_cache_size;
	if (cache->cache[slot].name != NULL) {
		if (cache->cache[slot].id == gid)
			return (cache->cache[slot].name);
		free(cache->cache[slot].name);
		cache->cache[slot].name = NULL;
	}

	grent = getgrgid(gid);
	if (grent == NULL) {
		if (errno)
			bsdtar_warnc(errno, "getgrgid(%d) failed", gid);
		return (NULL);
	} else if (grent->gr_name != NULL && grent->gr_name[0] != '\0') {
		cache->cache[slot].name = strdup(grent->gr_name);
		cache->cache[slot].id = gid;
		return (cache->cache[slot].name);
	}
	return (NULL);
}

/*
 * Test if the specified file is newer than what's already
 * in the archive.
 */
int
new_enough(struct bsdtar *bsdtar, const char *path,
    time_t mtime_sec, int mtime_nsec)
{
	struct archive_dir_entry *p;

	if (path[0] == '.' && path[1] == '/' && path[2] != '\0')
		path += 2;

	if (bsdtar->archive_dir == NULL  ||
	    bsdtar->archive_dir->head == NULL)
		return (1);

	for (p = bsdtar->archive_dir->head; p != NULL; p = p->next) {
		if (strcmp(path, p->name)==0)
			return (p->mtime_sec < mtime_sec ||
				(p->mtime_sec == mtime_sec &&
				 p->mtime_nsec < mtime_nsec));
	}
	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;

	if (path[0] == '.' && path[1] == '/' && path[2] != '\0')
		path += 2;

	/*
	 * 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));
	p->name = strdup(path);
	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_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)
		bsdtar_errc(1, errno, "Cannot stat %s", bsdtar->filename);

	if (!S_ISREG(s.st_mode))
		bsdtar_errc(1, 0, "Cannot append to %s: not a regular file.",
		    bsdtar->filename);
}
OpenPOWER on IntegriCloud