Merge from vendor branch TNFTP:
[dragonfly.git] / contrib / libarchive-2 / tar / write.c
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "bsdtar_platform.h"
27 __FBSDID("$FreeBSD: src/usr.bin/tar/write.c,v 1.63 2007/05/29 05:39:10 kientzle Exp $");
28
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
31 #endif
32 #ifdef HAVE_SYS_ACL_H
33 #include <sys/acl.h>
34 #endif
35 #ifdef HAVE_SYS_IOCTL_H
36 #include <sys/ioctl.h>
37 #endif
38 #ifdef HAVE_SYS_STAT_H
39 #include <sys/stat.h>
40 #endif
41 #ifdef HAVE_ATTR_XATTR_H
42 #include <attr/xattr.h>
43 #endif
44 #ifdef HAVE_ERRNO_H
45 #include <errno.h>
46 #endif
47 #ifdef HAVE_EXT2FS_EXT2_FS_H
48 #include <ext2fs/ext2_fs.h>
49 #endif
50 #ifdef HAVE_FCNTL_H
51 #include <fcntl.h>
52 #endif
53 #ifdef HAVE_FNMATCH_H
54 #include <fnmatch.h>
55 #endif
56 #ifdef HAVE_GRP_H
57 #include <grp.h>
58 #endif
59 #ifdef HAVE_LIMITS_H
60 #include <limits.h>
61 #endif
62 #ifdef HAVE_LINUX_FS_H
63 #include <linux/fs.h>   /* for Linux file flags */
64 #endif
65 #ifdef HAVE_LINUX_EXT2_FS_H
66 #include <linux/ext2_fs.h>      /* for Linux file flags */
67 #endif
68 #ifdef HAVE_PWD_H
69 #include <pwd.h>
70 #endif
71 #include <stdio.h>
72 #ifdef HAVE_STDLIB_H
73 #include <stdlib.h>
74 #endif
75 #ifdef HAVE_STRING_H
76 #include <string.h>
77 #endif
78 #ifdef HAVE_UNISTD_H
79 #include <unistd.h>
80 #endif
81
82 #include "bsdtar.h"
83 #include "tree.h"
84
85 /* Fixed size of uname/gname caches. */
86 #define name_cache_size 101
87
88 static const char * const NO_NAME = "(noname)";
89
90 /* Initial size of link cache. */
91 #define links_cache_initial_size 1024
92
93 struct archive_dir_entry {
94         struct archive_dir_entry        *next;
95         time_t                   mtime_sec;
96         int                      mtime_nsec;
97         char                    *name;
98 };
99
100 struct archive_dir {
101         struct archive_dir_entry *head, *tail;
102 };
103
104 struct links_cache {
105         unsigned long             number_entries;
106         size_t                    number_buckets;
107         struct links_entry      **buckets;
108 };
109
110 struct links_entry {
111         struct links_entry      *next;
112         struct links_entry      *previous;
113         int                      links;
114         dev_t                    dev;
115         ino_t                    ino;
116         char                    *name;
117 };
118
119 struct name_cache {
120         int     probes;
121         int     hits;
122         size_t  size;
123         struct {
124                 id_t id;
125                 const char *name;
126         } cache[name_cache_size];
127 };
128
129 static void              add_dir_list(struct bsdtar *bsdtar, const char *path,
130                              time_t mtime_sec, int mtime_nsec);
131 static int               append_archive(struct bsdtar *, struct archive *,
132                              struct archive *ina);
133 static int               append_archive_filename(struct bsdtar *,
134                              struct archive *, const char *fname);
135 static void              archive_names_from_file(struct bsdtar *bsdtar,
136                              struct archive *a);
137 static int               archive_names_from_file_helper(struct bsdtar *bsdtar,
138                              const char *line);
139 static int               copy_file_data(struct bsdtar *bsdtar,
140                              struct archive *a, struct archive *ina);
141 static void              create_cleanup(struct bsdtar *);
142 static void              free_buckets(struct bsdtar *, struct links_cache *);
143 static void              free_cache(struct name_cache *cache);
144 static const char *      lookup_gname(struct bsdtar *bsdtar, gid_t gid);
145 static int               lookup_gname_helper(struct bsdtar *bsdtar,
146                              const char **name, id_t gid);
147 static void              lookup_hardlink(struct bsdtar *,
148                              struct archive_entry *entry, const struct stat *);
149 static const char *      lookup_uname(struct bsdtar *bsdtar, uid_t uid);
150 static int               lookup_uname_helper(struct bsdtar *bsdtar,
151                              const char **name, id_t uid);
152 static int               new_enough(struct bsdtar *, const char *path,
153                              const struct stat *);
154 static void              setup_acls(struct bsdtar *, struct archive_entry *,
155                              const char *path);
156 static void              setup_xattrs(struct bsdtar *, struct archive_entry *,
157                              const char *path);
158 static void              test_for_append(struct bsdtar *);
159 static void              write_archive(struct archive *, struct bsdtar *);
160 static void              write_entry(struct bsdtar *, struct archive *,
161                              const struct stat *, const char *pathname,
162                              const char *accpath);
163 static int               write_file_data(struct bsdtar *, struct archive *,
164                              int fd);
165 static void              write_hierarchy(struct bsdtar *, struct archive *,
166                              const char *);
167
168 void
169 tar_mode_c(struct bsdtar *bsdtar)
170 {
171         struct archive *a;
172         int r;
173
174         if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
175                 bsdtar_errc(bsdtar, 1, 0, "no files or directories specified");
176
177         a = archive_write_new();
178
179         /* Support any format that the library supports. */
180         if (bsdtar->create_format == NULL) {
181                 r = archive_write_set_format_pax_restricted(a);
182                 bsdtar->create_format = "pax restricted";
183         } else {
184                 r = archive_write_set_format_by_name(a, bsdtar->create_format);
185         }
186         if (r != ARCHIVE_OK) {
187                 fprintf(stderr, "Can't use format %s: %s\n",
188                     bsdtar->create_format,
189                     archive_error_string(a));
190                 usage(bsdtar);
191         }
192
193         /*
194          * If user explicitly set the block size, then assume they
195          * want the last block padded as well.  Otherwise, use the
196          * default block size and accept archive_write_open_file()'s
197          * default padding decisions.
198          */
199         if (bsdtar->bytes_per_block != 0) {
200                 archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
201                 archive_write_set_bytes_in_last_block(a,
202                     bsdtar->bytes_per_block);
203         } else
204                 archive_write_set_bytes_per_block(a, DEFAULT_BYTES_PER_BLOCK);
205
206         if (bsdtar->compress_program) {
207                 archive_write_set_compression_program(a, bsdtar->compress_program);
208         } else {
209                 switch (bsdtar->create_compression) {
210                 case 0:
211                         archive_write_set_compression_none(a);
212                         break;
213 #ifdef HAVE_LIBBZ2
214                 case 'j': case 'y':
215                         archive_write_set_compression_bzip2(a);
216                         break;
217 #endif
218 #ifdef HAVE_LIBZ
219                 case 'z':
220                         archive_write_set_compression_gzip(a);
221                         break;
222 #endif
223                 default:
224                         bsdtar_errc(bsdtar, 1, 0,
225                             "Unrecognized compression option -%c",
226                             bsdtar->create_compression);
227                 }
228         }
229
230         r = archive_write_open_file(a, bsdtar->filename);
231         if (r != ARCHIVE_OK)
232                 bsdtar_errc(bsdtar, 1, 0, archive_error_string(a));
233
234         write_archive(a, bsdtar);
235
236         if (bsdtar->option_totals) {
237                 fprintf(stderr, "Total bytes written: " BSDTAR_FILESIZE_PRINTF "\n",
238                     (BSDTAR_FILESIZE_TYPE)archive_position_compressed(a));
239         }
240
241         archive_write_finish(a);
242 }
243
244 /*
245  * Same as 'c', except we only support tar or empty formats in
246  * uncompressed files on disk.
247  */
248 void
249 tar_mode_r(struct bsdtar *bsdtar)
250 {
251         off_t   end_offset;
252         int     format;
253         struct archive *a;
254         struct archive_entry *entry;
255         int     r;
256
257         /* Sanity-test some arguments and the file. */
258         test_for_append(bsdtar);
259
260         format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
261
262         bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT, 0666);
263         if (bsdtar->fd < 0)
264                 bsdtar_errc(bsdtar, 1, errno,
265                     "Cannot open %s", bsdtar->filename);
266
267         a = archive_read_new();
268         archive_read_support_compression_all(a);
269         archive_read_support_format_tar(a);
270         archive_read_support_format_gnutar(a);
271         r = archive_read_open_fd(a, bsdtar->fd, 10240);
272         if (r != ARCHIVE_OK)
273                 bsdtar_errc(bsdtar, 1, archive_errno(a),
274                     "Can't read archive %s: %s", bsdtar->filename,
275                     archive_error_string(a));
276         while (0 == archive_read_next_header(a, &entry)) {
277                 if (archive_compression(a) != ARCHIVE_COMPRESSION_NONE) {
278                         archive_read_finish(a);
279                         close(bsdtar->fd);
280                         bsdtar_errc(bsdtar, 1, 0,
281                             "Cannot append to compressed archive.");
282                 }
283                 /* Keep going until we hit end-of-archive */
284                 format = archive_format(a);
285         }
286
287         end_offset = archive_read_header_position(a);
288         archive_read_finish(a);
289
290         /* Re-open archive for writing */
291         a = archive_write_new();
292         archive_write_set_compression_none(a);
293         /*
294          * Set the format to be used for writing.  To allow people to
295          * extend empty files, we need to allow them to specify the format,
296          * which opens the possibility that they will specify a format that
297          * doesn't match the existing format.  Hence, the following bit
298          * of arcane ugliness.
299          */
300
301         if (bsdtar->create_format != NULL) {
302                 /* If the user requested a format, use that, but ... */
303                 archive_write_set_format_by_name(a,
304                     bsdtar->create_format);
305                 /* ... complain if it's not compatible. */
306                 format &= ARCHIVE_FORMAT_BASE_MASK;
307                 if (format != (int)(archive_format(a) & ARCHIVE_FORMAT_BASE_MASK)
308                     && format != ARCHIVE_FORMAT_EMPTY) {
309                         bsdtar_errc(bsdtar, 1, 0,
310                             "Format %s is incompatible with the archive %s.",
311                             bsdtar->create_format, bsdtar->filename);
312                 }
313         } else {
314                 /*
315                  * Just preserve the current format, with a little care
316                  * for formats that libarchive can't write.
317                  */
318                 if (format == ARCHIVE_FORMAT_TAR_GNUTAR)
319                         /* TODO: When gtar supports pax, use pax restricted. */
320                         format = ARCHIVE_FORMAT_TAR_USTAR;
321                 if (format == ARCHIVE_FORMAT_EMPTY)
322                         format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
323                 archive_write_set_format(a, format);
324         }
325         lseek(bsdtar->fd, end_offset, SEEK_SET); /* XXX check return val XXX */
326         archive_write_open_fd(a, bsdtar->fd); /* XXX check return val XXX */
327
328         write_archive(a, bsdtar); /* XXX check return val XXX */
329
330         if (bsdtar->option_totals) {
331                 fprintf(stderr, "Total bytes written: " BSDTAR_FILESIZE_PRINTF "\n",
332                     (BSDTAR_FILESIZE_TYPE)archive_position_compressed(a));
333         }
334
335         archive_write_finish(a);
336         close(bsdtar->fd);
337         bsdtar->fd = -1;
338 }
339
340 void
341 tar_mode_u(struct bsdtar *bsdtar)
342 {
343         off_t                    end_offset;
344         struct archive          *a;
345         struct archive_entry    *entry;
346         int                      format;
347         struct archive_dir_entry        *p;
348         struct archive_dir       archive_dir;
349
350         bsdtar->archive_dir = &archive_dir;
351         memset(&archive_dir, 0, sizeof(archive_dir));
352
353         format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
354
355         /* Sanity-test some arguments and the file. */
356         test_for_append(bsdtar);
357
358         bsdtar->fd = open(bsdtar->filename, O_RDWR);
359         if (bsdtar->fd < 0)
360                 bsdtar_errc(bsdtar, 1, errno,
361                     "Cannot open %s", bsdtar->filename);
362
363         a = archive_read_new();
364         archive_read_support_compression_all(a);
365         archive_read_support_format_tar(a);
366         archive_read_support_format_gnutar(a);
367         if (archive_read_open_fd(a, bsdtar->fd,
368             bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block :
369                 DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK) {
370                 bsdtar_errc(bsdtar, 1, 0,
371                     "Can't open %s: %s", bsdtar->filename,
372                     archive_error_string(a));
373         }
374
375         /* Build a list of all entries and their recorded mod times. */
376         while (0 == archive_read_next_header(a, &entry)) {
377                 if (archive_compression(a) != ARCHIVE_COMPRESSION_NONE) {
378                         archive_read_finish(a);
379                         close(bsdtar->fd);
380                         bsdtar_errc(bsdtar, 1, 0,
381                             "Cannot append to compressed archive.");
382                 }
383                 add_dir_list(bsdtar, archive_entry_pathname(entry),
384                     archive_entry_mtime(entry),
385                     archive_entry_mtime_nsec(entry));
386                 /* Record the last format determination we see */
387                 format = archive_format(a);
388                 /* Keep going until we hit end-of-archive */
389         }
390
391         end_offset = archive_read_header_position(a);
392         archive_read_finish(a);
393
394         /* Re-open archive for writing. */
395         a = archive_write_new();
396         archive_write_set_compression_none(a);
397         /*
398          * Set format to same one auto-detected above, except that
399          * we don't write GNU tar format, so use ustar instead.
400          */
401         if (format == ARCHIVE_FORMAT_TAR_GNUTAR)
402                 format = ARCHIVE_FORMAT_TAR_USTAR;
403         archive_write_set_format(a, format);
404         if (bsdtar->bytes_per_block != 0) {
405                 archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block);
406                 archive_write_set_bytes_in_last_block(a,
407                     bsdtar->bytes_per_block);
408         } else
409                 archive_write_set_bytes_per_block(a, DEFAULT_BYTES_PER_BLOCK);
410         lseek(bsdtar->fd, end_offset, SEEK_SET);
411         ftruncate(bsdtar->fd, end_offset);
412         archive_write_open_fd(a, bsdtar->fd);
413
414         write_archive(a, bsdtar);
415
416         if (bsdtar->option_totals) {
417                 fprintf(stderr, "Total bytes written: " BSDTAR_FILESIZE_PRINTF "\n",
418                     (BSDTAR_FILESIZE_TYPE)archive_position_compressed(a));
419         }
420
421         archive_write_finish(a);
422         close(bsdtar->fd);
423         bsdtar->fd = -1;
424
425         while (bsdtar->archive_dir->head != NULL) {
426                 p = bsdtar->archive_dir->head->next;
427                 free(bsdtar->archive_dir->head->name);
428                 free(bsdtar->archive_dir->head);
429                 bsdtar->archive_dir->head = p;
430         }
431         bsdtar->archive_dir->tail = NULL;
432 }
433
434
435 /*
436  * Write user-specified files/dirs to opened archive.
437  */
438 static void
439 write_archive(struct archive *a, struct bsdtar *bsdtar)
440 {
441         const char *arg;
442
443         if (bsdtar->names_from_file != NULL)
444                 archive_names_from_file(bsdtar, a);
445
446         while (*bsdtar->argv) {
447                 arg = *bsdtar->argv;
448                 if (arg[0] == '-' && arg[1] == 'C') {
449                         arg += 2;
450                         if (*arg == '\0') {
451                                 bsdtar->argv++;
452                                 arg = *bsdtar->argv;
453                                 if (arg == NULL) {
454                                         bsdtar_warnc(bsdtar, 1, 0,
455                                             "Missing argument for -C");
456                                         bsdtar->return_value = 1;
457                                         return;
458                                 }
459                         }
460                         set_chdir(bsdtar, arg);
461                 } else {
462                         if (*arg != '/' && (arg[0] != '@' || arg[1] != '/'))
463                                 do_chdir(bsdtar); /* Handle a deferred -C */
464                         if (*arg == '@') {
465                                 if (append_archive_filename(bsdtar, a,
466                                     arg + 1) != 0)
467                                         break;
468                         } else
469                                 write_hierarchy(bsdtar, a, arg);
470                 }
471                 bsdtar->argv++;
472         }
473
474         create_cleanup(bsdtar);
475         if (archive_write_close(a)) {
476                 bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
477                 bsdtar->return_value = 1;
478         }
479 }
480
481 /*
482  * Archive names specified in file.
483  *
484  * Unless --null was specified, a line containing exactly "-C" will
485  * cause the next line to be a directory to pass to chdir().  If
486  * --null is specified, then a line "-C" is just another filename.
487  */
488 void
489 archive_names_from_file(struct bsdtar *bsdtar, struct archive *a)
490 {
491         bsdtar->archive = a;
492
493         bsdtar->next_line_is_dir = 0;
494         process_lines(bsdtar, bsdtar->names_from_file,
495             archive_names_from_file_helper);
496         if (bsdtar->next_line_is_dir)
497                 bsdtar_errc(bsdtar, 1, errno,
498                     "Unexpected end of filename list; "
499                     "directory expected after -C");
500 }
501
502 static int
503 archive_names_from_file_helper(struct bsdtar *bsdtar, const char *line)
504 {
505         if (bsdtar->next_line_is_dir) {
506                 set_chdir(bsdtar, line);
507                 bsdtar->next_line_is_dir = 0;
508         } else if (!bsdtar->option_null && strcmp(line, "-C") == 0)
509                 bsdtar->next_line_is_dir = 1;
510         else {
511                 if (*line != '/')
512                         do_chdir(bsdtar); /* Handle a deferred -C */
513                 write_hierarchy(bsdtar, bsdtar->archive, line);
514         }
515         return (0);
516 }
517
518 /*
519  * Copy from specified archive to current archive.  Returns non-zero
520  * for write errors (which force us to terminate the entire archiving
521  * operation).  If there are errors reading the input archive, we set
522  * bsdtar->return_value but return zero, so the overall archiving
523  * operation will complete and return non-zero.
524  */
525 static int
526 append_archive_filename(struct bsdtar *bsdtar, struct archive *a,
527     const char *filename)
528 {
529         struct archive *ina;
530         int rc;
531
532         if (strcmp(filename, "-") == 0)
533                 filename = NULL; /* Library uses NULL for stdio. */
534
535         ina = archive_read_new();
536         archive_read_support_format_all(ina);
537         archive_read_support_compression_all(ina);
538         if (archive_read_open_file(ina, filename, 10240)) {
539                 bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(ina));
540                 bsdtar->return_value = 1;
541                 return (0);
542         }
543
544         rc = append_archive(bsdtar, a, ina);
545
546         if (archive_errno(ina)) {
547                 bsdtar_warnc(bsdtar, 0, "Error reading archive %s: %s",
548                     filename, archive_error_string(ina));
549                 bsdtar->return_value = 1;
550         }
551         archive_read_finish(ina);
552
553         return (rc);
554 }
555
556 static int
557 append_archive(struct bsdtar *bsdtar, struct archive *a, struct archive *ina)
558 {
559         struct archive_entry *in_entry;
560         int e;
561
562         while (0 == archive_read_next_header(ina, &in_entry)) {
563                 if (!new_enough(bsdtar, archive_entry_pathname(in_entry),
564                         archive_entry_stat(in_entry)))
565                         continue;
566                 if (excluded(bsdtar, archive_entry_pathname(in_entry)))
567                         continue;
568                 if (bsdtar->option_interactive &&
569                     !yes("copy '%s'", archive_entry_pathname(in_entry)))
570                         continue;
571                 if (bsdtar->verbose)
572                         safe_fprintf(stderr, "a %s",
573                             archive_entry_pathname(in_entry));
574
575                 e = archive_write_header(a, in_entry);
576                 if (e != ARCHIVE_OK) {
577                         if (!bsdtar->verbose)
578                                 bsdtar_warnc(bsdtar, 0, "%s: %s",
579                                     archive_entry_pathname(in_entry),
580                                     archive_error_string(a));
581                         else
582                                 fprintf(stderr, ": %s", archive_error_string(a));
583                 }
584                 if (e == ARCHIVE_FATAL)
585                         exit(1);
586
587                 if (e >= ARCHIVE_WARN)
588                         if (copy_file_data(bsdtar, a, ina))
589                                 exit(1);
590
591                 if (bsdtar->verbose)
592                         fprintf(stderr, "\n");
593         }
594
595         /* Note: If we got here, we saw no write errors, so return success. */
596         return (0);
597 }
598
599 /* Helper function to copy data between archives. */
600 static int
601 copy_file_data(struct bsdtar *bsdtar, struct archive *a, struct archive *ina)
602 {
603         char    buff[64*1024];
604         ssize_t bytes_read;
605         ssize_t bytes_written;
606
607         bytes_read = archive_read_data(ina, buff, sizeof(buff));
608         while (bytes_read > 0) {
609                 bytes_written = archive_write_data(a, buff, bytes_read);
610                 if (bytes_written < bytes_read) {
611                         bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
612                         return (-1);
613                 }
614                 bytes_read = archive_read_data(ina, buff, sizeof(buff));
615         }
616
617         return (0);
618 }
619
620 /*
621  * Add the file or dir hierarchy named by 'path' to the archive
622  */
623 static void
624 write_hierarchy(struct bsdtar *bsdtar, struct archive *a, const char *path)
625 {
626         struct tree *tree;
627         char symlink_mode = bsdtar->symlink_mode;
628         dev_t first_dev = 0;
629         int dev_recorded = 0;
630         int tree_ret;
631 #ifdef __linux
632         int      fd, r;
633         unsigned long fflags;
634 #endif
635
636         tree = tree_open(path);
637
638         if (!tree) {
639                 bsdtar_warnc(bsdtar, errno, "%s: Cannot open", path);
640                 bsdtar->return_value = 1;
641                 return;
642         }
643
644         while ((tree_ret = tree_next(tree))) {
645                 const char *name = tree_current_path(tree);
646                 const struct stat *st = NULL, *lst = NULL;
647                 int descend;
648
649                 if (tree_ret == TREE_ERROR_DIR)
650                         bsdtar_warnc(bsdtar, errno, "%s: Couldn't visit directory", name);
651                 if (tree_ret != TREE_REGULAR)
652                         continue;
653                 lst = tree_current_lstat(tree);
654                 if (lst == NULL) {
655                         /* Couldn't lstat(); must not exist. */
656                         bsdtar_warnc(bsdtar, errno, "%s: Cannot stat", name);
657
658                         /*
659                          * Report an error via the exit code if the failed
660                          * path is a prefix of what the user provided via
661                          * the command line.  (Testing for string equality
662                          * here won't work due to trailing '/' characters.)
663                          */
664                         if (memcmp(name, path, strlen(name)) == 0)
665                                 bsdtar->return_value = 1;
666
667                         continue;
668                 }
669                 if (S_ISLNK(lst->st_mode))
670                         st = tree_current_stat(tree);
671                 /* Default: descend into any dir or symlink to dir. */
672                 /* We'll adjust this later on. */
673                 descend = 0;
674                 if ((st != NULL) && S_ISDIR(st->st_mode))
675                         descend = 1;
676                 if ((lst != NULL) && S_ISDIR(lst->st_mode))
677                         descend = 1;
678
679                 /*
680                  * If user has asked us not to cross mount points,
681                  * then don't descend into into a dir on a different
682                  * device.
683                  */
684                 if (!dev_recorded) {
685                         first_dev = lst->st_dev;
686                         dev_recorded = 1;
687                 }
688                 if (bsdtar->option_dont_traverse_mounts) {
689                         if (lst != NULL && lst->st_dev != first_dev)
690                                 descend = 0;
691                 }
692
693                 /*
694                  * If this file/dir is flagged "nodump" and we're
695                  * honoring such flags, skip this file/dir.
696                  */
697 #ifdef HAVE_CHFLAGS
698                 if (bsdtar->option_honor_nodump &&
699                     (lst->st_flags & UF_NODUMP))
700                         continue;
701 #endif
702
703 #ifdef __linux
704                 /*
705                  * Linux has a nodump flag too but to read it
706                  * we have to open() the file/dir and do an ioctl on it...
707                  */
708                 if (bsdtar->option_honor_nodump &&
709                     ((fd = open(name, O_RDONLY|O_NONBLOCK)) >= 0) &&
710                     ((r = ioctl(fd, EXT2_IOC_GETFLAGS, &fflags)),
711                         close(fd), r) >= 0 &&
712                     (fflags & EXT2_NODUMP_FL))
713                         continue;
714 #endif
715
716                 /*
717                  * If this file/dir is excluded by a filename
718                  * pattern, skip it.
719                  */
720                 if (excluded(bsdtar, name))
721                         continue;
722
723                 /*
724                  * If the user vetoes this file/directory, skip it.
725                  */
726                 if (bsdtar->option_interactive &&
727                     !yes("add '%s'", name))
728                         continue;
729
730                 /*
731                  * If this is a dir, decide whether or not to recurse.
732                  */
733                 if (bsdtar->option_no_subdirs)
734                         descend = 0;
735
736                 /*
737                  * Distinguish 'L'/'P'/'H' symlink following.
738                  */
739                 switch(symlink_mode) {
740                 case 'H':
741                         /* 'H': After the first item, rest like 'P'. */
742                         symlink_mode = 'P';
743                         /* 'H': First item (from command line) like 'L'. */
744                         /* FALLTHROUGH */
745                 case 'L':
746                         /* 'L': Do descend through a symlink to dir. */
747                         /* 'L': Archive symlink to file as file. */
748                         lst = tree_current_stat(tree);
749                         /* If stat fails, we have a broken symlink;
750                          * in that case, archive the link as such. */
751                         if (lst == NULL)
752                                 lst = tree_current_lstat(tree);
753                         break;
754                 default:
755                         /* 'P': Don't descend through a symlink to dir. */
756                         if (!S_ISDIR(lst->st_mode))
757                                 descend = 0;
758                         /* 'P': Archive symlink to file as symlink. */
759                         /* lst = tree_current_lstat(tree); */
760                         break;
761                 }
762
763                 if (descend)
764                         tree_descend(tree);
765
766                 /*
767                  * Write the entry.  Note that write_entry() handles
768                  * pathname editing and newness testing.
769                  */
770                 write_entry(bsdtar, a, lst, name,
771                     tree_current_access_path(tree));
772         }
773         tree_close(tree);
774 }
775
776 /*
777  * Add a single filesystem object to the archive.
778  */
779 static void
780 write_entry(struct bsdtar *bsdtar, struct archive *a, const struct stat *st,
781     const char *pathname, const char *accpath)
782 {
783         struct archive_entry    *entry;
784         int                      e;
785         int                      fd;
786 #ifdef __linux
787         int                      r;
788         unsigned long            stflags;
789 #endif
790         static char              linkbuffer[PATH_MAX+1];
791
792         fd = -1;
793         entry = archive_entry_new();
794
795         archive_entry_set_pathname(entry, pathname);
796
797         /*
798          * Rewrite the pathname to be archived.  If rewrite
799          * fails, skip the entry.
800          */
801         if (edit_pathname(bsdtar, entry))
802                 goto abort;
803
804         /*
805          * In -u mode, check that the file is newer than what's
806          * already in the archive; in all modes, obey --newerXXX flags.
807          */
808         if (!new_enough(bsdtar, archive_entry_pathname(entry), st))
809                 goto abort;
810
811         if (!S_ISDIR(st->st_mode) && (st->st_nlink > 1))
812                 lookup_hardlink(bsdtar, entry, st);
813
814         /* Display entry as we process it. This format is required by SUSv2. */
815         if (bsdtar->verbose)
816                 safe_fprintf(stderr, "a %s", archive_entry_pathname(entry));
817
818         /* Read symbolic link information. */
819         if ((st->st_mode & S_IFMT) == S_IFLNK) {
820                 int lnklen;
821
822                 lnklen = readlink(accpath, linkbuffer, PATH_MAX);
823                 if (lnklen < 0) {
824                         if (!bsdtar->verbose)
825                                 bsdtar_warnc(bsdtar, errno,
826                                     "%s: Couldn't read symbolic link",
827                                     pathname);
828                         else
829                                 safe_fprintf(stderr,
830                                     ": Couldn't read symbolic link: %s",
831                                     strerror(errno));
832                         goto cleanup;
833                 }
834                 linkbuffer[lnklen] = 0;
835                 archive_entry_set_symlink(entry, linkbuffer);
836         }
837
838         /* Look up username and group name. */
839         archive_entry_set_uname(entry, lookup_uname(bsdtar, st->st_uid));
840         archive_entry_set_gname(entry, lookup_gname(bsdtar, st->st_gid));
841
842 #ifdef HAVE_CHFLAGS
843         if (st->st_flags != 0)
844                 archive_entry_set_fflags(entry, st->st_flags, 0);
845 #endif
846
847 #ifdef __linux
848         if ((S_ISREG(st->st_mode) || S_ISDIR(st->st_mode)) &&
849             ((fd = open(accpath, O_RDONLY|O_NONBLOCK)) >= 0) &&
850             ((r = ioctl(fd, EXT2_IOC_GETFLAGS, &stflags)), close(fd), (fd = -1), r) >= 0 &&
851             stflags) {
852                 archive_entry_set_fflags(entry, stflags, 0);
853         }
854 #endif
855
856         archive_entry_copy_stat(entry, st);
857         setup_acls(bsdtar, entry, accpath);
858         setup_xattrs(bsdtar, entry, accpath);
859
860         /*
861          * If it's a regular file (and non-zero in size) make sure we
862          * can open it before we start to write.  In particular, note
863          * that we can always archive a zero-length file, even if we
864          * can't read it.
865          */
866         if (S_ISREG(st->st_mode) && st->st_size > 0) {
867                 fd = open(accpath, O_RDONLY);
868                 if (fd < 0) {
869                         if (!bsdtar->verbose)
870                                 bsdtar_warnc(bsdtar, errno, "%s: could not open file", pathname);
871                         else
872                                 fprintf(stderr, ": %s", strerror(errno));
873                         goto cleanup;
874                 }
875         }
876
877         /* Non-regular files get archived with zero size. */
878         if (!S_ISREG(st->st_mode))
879                 archive_entry_set_size(entry, 0);
880
881         e = archive_write_header(a, entry);
882         if (e != ARCHIVE_OK) {
883                 if (!bsdtar->verbose)
884                         bsdtar_warnc(bsdtar, 0, "%s: %s", pathname,
885                             archive_error_string(a));
886                 else
887                         fprintf(stderr, ": %s", archive_error_string(a));
888         }
889
890         if (e == ARCHIVE_FATAL)
891                 exit(1);
892
893         /*
894          * If we opened a file earlier, write it out now.  Note that
895          * the format handler might have reset the size field to zero
896          * to inform us that the archive body won't get stored.  In
897          * that case, just skip the write.
898          */
899         if (e >= ARCHIVE_WARN && fd >= 0 && archive_entry_size(entry) > 0)
900                 if (write_file_data(bsdtar, a, fd))
901                         exit(1);
902
903 cleanup:
904         if (bsdtar->verbose)
905                 fprintf(stderr, "\n");
906
907 abort:
908         if (fd >= 0)
909                 close(fd);
910
911         if (entry != NULL)
912                 archive_entry_free(entry);
913 }
914
915
916 /* Helper function to copy file to archive, with stack-allocated buffer. */
917 static int
918 write_file_data(struct bsdtar *bsdtar, struct archive *a, int fd)
919 {
920         char    buff[64*1024];
921         ssize_t bytes_read;
922         ssize_t bytes_written;
923
924         /* XXX TODO: Allocate buffer on heap and store pointer to
925          * it in bsdtar structure; arrange cleanup as well. XXX */
926
927         bytes_read = read(fd, buff, sizeof(buff));
928         while (bytes_read > 0) {
929                 bytes_written = archive_write_data(a, buff, bytes_read);
930                 if (bytes_written < 0) {
931                         /* Write failed; this is bad */
932                         bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
933                         return (-1);
934                 }
935                 if (bytes_written < bytes_read) {
936                         /* Write was truncated; warn but continue. */
937                         bsdtar_warnc(bsdtar, 0,
938                             "Truncated write; file may have grown while being archived.");
939                         return (0);
940                 }
941                 bytes_read = read(fd, buff, sizeof(buff));
942         }
943         return 0;
944 }
945
946
947 static void
948 create_cleanup(struct bsdtar *bsdtar)
949 {
950         /* Free inode->pathname map used for hardlink detection. */
951         if (bsdtar->links_cache != NULL) {
952                 free_buckets(bsdtar, bsdtar->links_cache);
953                 free(bsdtar->links_cache);
954                 bsdtar->links_cache = NULL;
955         }
956
957         free_cache(bsdtar->uname_cache);
958         bsdtar->uname_cache = NULL;
959         free_cache(bsdtar->gname_cache);
960         bsdtar->gname_cache = NULL;
961 }
962
963
964 static void
965 free_buckets(struct bsdtar *bsdtar, struct links_cache *links_cache)
966 {
967         size_t i;
968
969         if (links_cache->buckets == NULL)
970                 return;
971
972         for (i = 0; i < links_cache->number_buckets; i++) {
973                 while (links_cache->buckets[i] != NULL) {
974                         struct links_entry *lp = links_cache->buckets[i]->next;
975                         if (bsdtar->option_warn_links)
976                                 bsdtar_warnc(bsdtar, 0, "Missing links to %s",
977                                     links_cache->buckets[i]->name);
978                         if (links_cache->buckets[i]->name != NULL)
979                                 free(links_cache->buckets[i]->name);
980                         free(links_cache->buckets[i]);
981                         links_cache->buckets[i] = lp;
982                 }
983         }
984         free(links_cache->buckets);
985         links_cache->buckets = NULL;
986 }
987
988 static void
989 lookup_hardlink(struct bsdtar *bsdtar, struct archive_entry *entry,
990     const struct stat *st)
991 {
992         struct links_cache      *links_cache;
993         struct links_entry      *le, **new_buckets;
994         int                      hash;
995         size_t                   i, new_size;
996
997         /* If necessary, initialize the links cache. */
998         links_cache = bsdtar->links_cache;
999         if (links_cache == NULL) {
1000                 bsdtar->links_cache = malloc(sizeof(struct links_cache));
1001                 if (bsdtar->links_cache == NULL)
1002                         bsdtar_errc(bsdtar, 1, ENOMEM,
1003                             "No memory for hardlink detection.");
1004                 links_cache = bsdtar->links_cache;
1005                 memset(links_cache, 0, sizeof(struct links_cache));
1006                 links_cache->number_buckets = links_cache_initial_size;
1007                 links_cache->buckets = malloc(links_cache->number_buckets *
1008                     sizeof(links_cache->buckets[0]));
1009                 if (links_cache->buckets == NULL) {
1010                         bsdtar_errc(bsdtar, 1, ENOMEM,
1011                             "No memory for hardlink detection.");
1012                 }
1013                 for (i = 0; i < links_cache->number_buckets; i++)
1014                         links_cache->buckets[i] = NULL;
1015         }
1016
1017         /* If the links cache overflowed and got flushed, don't bother. */
1018         if (links_cache->buckets == NULL)
1019                 return;
1020
1021         /* If the links cache is getting too full, enlarge the hash table. */
1022         if (links_cache->number_entries > links_cache->number_buckets * 2)
1023         {
1024                 new_size = links_cache->number_buckets * 2;
1025                 new_buckets = malloc(new_size * sizeof(struct links_entry *));
1026
1027                 if (new_buckets != NULL) {
1028                         memset(new_buckets, 0,
1029                             new_size * sizeof(struct links_entry *));
1030                         for (i = 0; i < links_cache->number_buckets; i++) {
1031                                 while (links_cache->buckets[i] != NULL) {
1032                                         /* Remove entry from old bucket. */
1033                                         le = links_cache->buckets[i];
1034                                         links_cache->buckets[i] = le->next;
1035
1036                                         /* Add entry to new bucket. */
1037                                         hash = (le->dev ^ le->ino) % new_size;
1038
1039                                         if (new_buckets[hash] != NULL)
1040                                                 new_buckets[hash]->previous =
1041                                                     le;
1042                                         le->next = new_buckets[hash];
1043                                         le->previous = NULL;
1044                                         new_buckets[hash] = le;
1045                                 }
1046                         }
1047                         free(links_cache->buckets);
1048                         links_cache->buckets = new_buckets;
1049                         links_cache->number_buckets = new_size;
1050                 } else {
1051                         free_buckets(bsdtar, links_cache);
1052                         bsdtar_warnc(bsdtar, ENOMEM,
1053                             "No more memory for recording hard links");
1054                         bsdtar_warnc(bsdtar, 0,
1055                             "Remaining links will be dumped as full files");
1056                 }
1057         }
1058
1059         /* Try to locate this entry in the links cache. */
1060         hash = ( st->st_dev ^ st->st_ino ) % links_cache->number_buckets;
1061         for (le = links_cache->buckets[hash]; le != NULL; le = le->next) {
1062                 if (le->dev == st->st_dev && le->ino == st->st_ino) {
1063                         archive_entry_copy_hardlink(entry, le->name);
1064
1065                         /*
1066                          * Decrement link count each time and release
1067                          * the entry if it hits zero.  This saves
1068                          * memory and is necessary for proper -l
1069                          * implementation.
1070                          */
1071                         if (--le->links <= 0) {
1072                                 if (le->previous != NULL)
1073                                         le->previous->next = le->next;
1074                                 if (le->next != NULL)
1075                                         le->next->previous = le->previous;
1076                                 if (le->name != NULL)
1077                                         free(le->name);
1078                                 if (links_cache->buckets[hash] == le)
1079                                         links_cache->buckets[hash] = le->next;
1080                                 links_cache->number_entries--;
1081                                 free(le);
1082                         }
1083
1084                         return;
1085                 }
1086         }
1087
1088         /* Add this entry to the links cache. */
1089         le = malloc(sizeof(struct links_entry));
1090         if (le != NULL)
1091                 le->name = strdup(archive_entry_pathname(entry));
1092         if ((le == NULL) || (le->name == NULL)) {
1093                 free_buckets(bsdtar, links_cache);
1094                 bsdtar_warnc(bsdtar, ENOMEM,
1095                     "No more memory for recording hard links");
1096                 bsdtar_warnc(bsdtar, 0,
1097                     "Remaining hard links will be dumped as full files");
1098                 if (le != NULL)
1099                         free(le);
1100                 return;
1101         }
1102         if (links_cache->buckets[hash] != NULL)
1103                 links_cache->buckets[hash]->previous = le;
1104         links_cache->number_entries++;
1105         le->next = links_cache->buckets[hash];
1106         le->previous = NULL;
1107         links_cache->buckets[hash] = le;
1108         le->dev = st->st_dev;
1109         le->ino = st->st_ino;
1110         le->links = st->st_nlink - 1;
1111 }
1112
1113 #ifdef HAVE_POSIX_ACL
1114 static void             setup_acl(struct bsdtar *bsdtar,
1115                              struct archive_entry *entry, const char *accpath,
1116                              int acl_type, int archive_entry_acl_type);
1117
1118 static void
1119 setup_acls(struct bsdtar *bsdtar, struct archive_entry *entry,
1120     const char *accpath)
1121 {
1122         archive_entry_acl_clear(entry);
1123
1124         setup_acl(bsdtar, entry, accpath,
1125             ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
1126         /* Only directories can have default ACLs. */
1127         if (S_ISDIR(archive_entry_mode(entry)))
1128                 setup_acl(bsdtar, entry, accpath,
1129                     ACL_TYPE_DEFAULT, ARCHIVE_ENTRY_ACL_TYPE_DEFAULT);
1130 }
1131
1132 static void
1133 setup_acl(struct bsdtar *bsdtar, struct archive_entry *entry,
1134     const char *accpath, int acl_type, int archive_entry_acl_type)
1135 {
1136         acl_t            acl;
1137         acl_tag_t        acl_tag;
1138         acl_entry_t      acl_entry;
1139         acl_permset_t    acl_permset;
1140         int              s, ae_id, ae_tag, ae_perm;
1141         const char      *ae_name;
1142
1143         /* Retrieve access ACL from file. */
1144         acl = acl_get_file(accpath, acl_type);
1145         if (acl != NULL) {
1146                 s = acl_get_entry(acl, ACL_FIRST_ENTRY, &acl_entry);
1147                 while (s == 1) {
1148                         ae_id = -1;
1149                         ae_name = NULL;
1150
1151                         acl_get_tag_type(acl_entry, &acl_tag);
1152                         if (acl_tag == ACL_USER) {
1153                                 ae_id = (int)*(uid_t *)acl_get_qualifier(acl_entry);
1154                                 ae_name = lookup_uname(bsdtar, ae_id);
1155                                 ae_tag = ARCHIVE_ENTRY_ACL_USER;
1156                         } else if (acl_tag == ACL_GROUP) {
1157                                 ae_id = (int)*(gid_t *)acl_get_qualifier(acl_entry);
1158                                 ae_name = lookup_gname(bsdtar, ae_id);
1159                                 ae_tag = ARCHIVE_ENTRY_ACL_GROUP;
1160                         } else if (acl_tag == ACL_MASK) {
1161                                 ae_tag = ARCHIVE_ENTRY_ACL_MASK;
1162                         } else if (acl_tag == ACL_USER_OBJ) {
1163                                 ae_tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1164                         } else if (acl_tag == ACL_GROUP_OBJ) {
1165                                 ae_tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1166                         } else if (acl_tag == ACL_OTHER) {
1167                                 ae_tag = ARCHIVE_ENTRY_ACL_OTHER;
1168                         } else {
1169                                 /* Skip types that libarchive can't support. */
1170                                 continue;
1171                         }
1172
1173                         acl_get_permset(acl_entry, &acl_permset);
1174                         ae_perm = 0;
1175                         /*
1176                          * acl_get_perm() is spelled differently on different
1177                          * platforms; see bsdtar_platform.h for details.
1178                          */
1179                         if (ACL_GET_PERM(acl_permset, ACL_EXECUTE))
1180                                 ae_perm |= ARCHIVE_ENTRY_ACL_EXECUTE;
1181                         if (ACL_GET_PERM(acl_permset, ACL_READ))
1182                                 ae_perm |= ARCHIVE_ENTRY_ACL_READ;
1183                         if (ACL_GET_PERM(acl_permset, ACL_WRITE))
1184                                 ae_perm |= ARCHIVE_ENTRY_ACL_WRITE;
1185
1186                         archive_entry_acl_add_entry(entry,
1187                             archive_entry_acl_type, ae_perm, ae_tag,
1188                             ae_id, ae_name);
1189
1190                         s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
1191                 }
1192                 acl_free(acl);
1193         }
1194 }
1195 #else
1196 static void
1197 setup_acls(struct bsdtar *bsdtar, struct archive_entry *entry,
1198     const char *accpath)
1199 {
1200         (void)bsdtar;
1201         (void)entry;
1202         (void)accpath;
1203 }
1204 #endif
1205
1206 #if HAVE_LISTXATTR && HAVE_LLISTXATTR && HAVE_GETXATTR && HAVE_LGETXATTR
1207
1208 static void
1209 setup_xattr(struct bsdtar *bsdtar, struct archive_entry *entry,
1210     const char *accpath, const char *name)
1211 {
1212         size_t size;
1213         void *value = NULL;
1214         char symlink_mode = bsdtar->symlink_mode;
1215
1216         if (symlink_mode == 'H')
1217                 size = getxattr(accpath, name, NULL, 0);
1218         else
1219                 size = lgetxattr(accpath, name, NULL, 0);
1220
1221         if (size == -1) {
1222                 bsdtar_warnc(bsdtar, errno, "Couldn't get extended attribute");
1223                 return;
1224         }
1225
1226         if (size > 0 && (value = malloc(size)) == NULL) {
1227                 bsdtar_errc(bsdtar, 1, errno, "Out of memory");
1228                 return;
1229         }
1230
1231         if (symlink_mode == 'H')
1232                 size = getxattr(accpath, name, value, size);
1233         else
1234                 size = lgetxattr(accpath, name, value, size);
1235
1236         if (size == -1) {
1237                 bsdtar_warnc(bsdtar, errno, "Couldn't get extended attribute");
1238                 return;
1239         }
1240
1241         archive_entry_xattr_add_entry(entry, name, value, size);
1242
1243         free(value);
1244 }
1245
1246 /*
1247  * Linux extended attribute support
1248  */
1249 static void
1250 setup_xattrs(struct bsdtar *bsdtar, struct archive_entry *entry,
1251     const char *accpath)
1252 {
1253         char *list, *p;
1254         size_t list_size;
1255         char symlink_mode = bsdtar->symlink_mode;
1256
1257         if (symlink_mode == 'H')
1258                 list_size = listxattr(accpath, NULL, 0);
1259         else
1260                 list_size = llistxattr(accpath, NULL, 0);
1261
1262         if (list_size == -1) {
1263                 bsdtar_warnc(bsdtar, errno,
1264                         "Couldn't list extended attributes");
1265                 return;
1266         } else if (list_size == 0)
1267                 return;
1268
1269         if ((list = malloc(list_size)) == NULL) {
1270                 bsdtar_errc(bsdtar, 1, errno, "Out of memory");
1271                 return;
1272         }
1273
1274         if (symlink_mode == 'H')
1275                 list_size = listxattr(accpath, list, list_size);
1276         else
1277                 list_size = llistxattr(accpath, list, list_size);
1278
1279         if (list_size == -1) {
1280                 bsdtar_warnc(bsdtar, errno,
1281                         "Couldn't list extended attributes");
1282                 free(list);
1283                 return;
1284         }
1285
1286         for (p = list; (p - list) < list_size; p += strlen(p) + 1) {
1287                 if (strncmp(p, "system.", 7) == 0 ||
1288                                 strncmp(p, "xfsroot.", 8) == 0)
1289                         continue;
1290
1291                 setup_xattr(bsdtar, entry, accpath, p);
1292         }
1293
1294         free(list);
1295 }
1296
1297 #else
1298
1299 /*
1300  * Generic (stub) extended attribute support.
1301  */
1302 static void
1303 setup_xattrs(struct bsdtar *bsdtar, struct archive_entry *entry,
1304     const char *accpath)
1305 {
1306         (void)bsdtar; /* UNUSED */
1307         (void)entry; /* UNUSED */
1308         (void)accpath; /* UNUSED */
1309 }
1310
1311 #endif
1312
1313 static void
1314 free_cache(struct name_cache *cache)
1315 {
1316         size_t i;
1317
1318         if (cache != NULL) {
1319                 for (i = 0; i < cache->size; i++) {
1320                         if (cache->cache[i].name != NULL &&
1321                             cache->cache[i].name != NO_NAME)
1322                                 free((void *)(uintptr_t)cache->cache[i].name);
1323                 }
1324                 free(cache);
1325         }
1326 }
1327
1328 /*
1329  * Lookup uid/gid from uname/gname, return NULL if no match.
1330  */
1331 static const char *
1332 lookup_name(struct bsdtar *bsdtar, struct name_cache **name_cache_variable,
1333     int (*lookup_fn)(struct bsdtar *, const char **, id_t), id_t id)
1334 {
1335         struct name_cache       *cache;
1336         const char *name;
1337         int slot;
1338
1339
1340         if (*name_cache_variable == NULL) {
1341                 *name_cache_variable = malloc(sizeof(struct name_cache));
1342                 if (*name_cache_variable == NULL)
1343                         bsdtar_errc(bsdtar, 1, ENOMEM, "No more memory");
1344                 memset(*name_cache_variable, 0, sizeof(struct name_cache));
1345                 (*name_cache_variable)->size = name_cache_size;
1346         }
1347
1348         cache = *name_cache_variable;
1349         cache->probes++;
1350
1351         slot = id % cache->size;
1352         if (cache->cache[slot].name != NULL) {
1353                 if (cache->cache[slot].id == id) {
1354                         cache->hits++;
1355                         if (cache->cache[slot].name == NO_NAME)
1356                                 return (NULL);
1357                         return (cache->cache[slot].name);
1358                 }
1359                 if (cache->cache[slot].name != NO_NAME)
1360                         free((void *)(uintptr_t)cache->cache[slot].name);
1361                 cache->cache[slot].name = NULL;
1362         }
1363
1364         if (lookup_fn(bsdtar, &name, id) == 0) {
1365                 if (name == NULL || name[0] == '\0') {
1366                         /* Cache the negative response. */
1367                         cache->cache[slot].name = NO_NAME;
1368                         cache->cache[slot].id = id;
1369                 } else {
1370                         cache->cache[slot].name = strdup(name);
1371                         if (cache->cache[slot].name != NULL) {
1372                                 cache->cache[slot].id = id;
1373                                 return (cache->cache[slot].name);
1374                         }
1375                         /*
1376                          * Conveniently, NULL marks an empty slot, so
1377                          * if the strdup() fails, we've just failed to
1378                          * cache it.  No recovery necessary.
1379                          */
1380                 }
1381         }
1382         return (NULL);
1383 }
1384
1385 static const char *
1386 lookup_uname(struct bsdtar *bsdtar, uid_t uid)
1387 {
1388         return (lookup_name(bsdtar, &bsdtar->uname_cache,
1389                     &lookup_uname_helper, (id_t)uid));
1390 }
1391
1392 static int
1393 lookup_uname_helper(struct bsdtar *bsdtar, const char **name, id_t id)
1394 {
1395         struct passwd   *pwent;
1396
1397         (void)bsdtar; /* UNUSED */
1398
1399         errno = 0;
1400         pwent = getpwuid((uid_t)id);
1401         if (pwent == NULL) {
1402                 *name = NULL;
1403                 if (errno != 0)
1404                         bsdtar_warnc(bsdtar, errno, "getpwuid(%d) failed", id);
1405                 return (errno);
1406         }
1407
1408         *name = pwent->pw_name;
1409         return (0);
1410 }
1411
1412 static const char *
1413 lookup_gname(struct bsdtar *bsdtar, gid_t gid)
1414 {
1415         return (lookup_name(bsdtar, &bsdtar->gname_cache,
1416                     &lookup_gname_helper, (id_t)gid));
1417 }
1418
1419 static int
1420 lookup_gname_helper(struct bsdtar *bsdtar, const char **name, id_t id)
1421 {
1422         struct group    *grent;
1423
1424         (void)bsdtar; /* UNUSED */
1425
1426         errno = 0;
1427         grent = getgrgid((gid_t)id);
1428         if (grent == NULL) {
1429                 *name = NULL;
1430                 if (errno != 0)
1431                         bsdtar_warnc(bsdtar, errno, "getgrgid(%d) failed", id);
1432                 return (errno);
1433         }
1434
1435         *name = grent->gr_name;
1436         return (0);
1437 }
1438
1439 /*
1440  * Test if the specified file is new enough to include in the archive.
1441  */
1442 int
1443 new_enough(struct bsdtar *bsdtar, const char *path, const struct stat *st)
1444 {
1445         struct archive_dir_entry *p;
1446
1447         /*
1448          * If this file/dir is excluded by a time comparison, skip it.
1449          */
1450         if (bsdtar->newer_ctime_sec > 0) {
1451                 if (st->st_ctime < bsdtar->newer_ctime_sec)
1452                         return (0); /* Too old, skip it. */
1453                 if (st->st_ctime == bsdtar->newer_ctime_sec
1454                     && ARCHIVE_STAT_CTIME_NANOS(st)
1455                     <= bsdtar->newer_ctime_nsec)
1456                         return (0); /* Too old, skip it. */
1457         }
1458         if (bsdtar->newer_mtime_sec > 0) {
1459                 if (st->st_mtime < bsdtar->newer_mtime_sec)
1460                         return (0); /* Too old, skip it. */
1461                 if (st->st_mtime == bsdtar->newer_mtime_sec
1462                     && ARCHIVE_STAT_MTIME_NANOS(st)
1463                     <= bsdtar->newer_mtime_nsec)
1464                         return (0); /* Too old, skip it. */
1465         }
1466
1467         /*
1468          * In -u mode, we only write an entry if it's newer than
1469          * what was already in the archive.
1470          */
1471         if (bsdtar->archive_dir != NULL &&
1472             bsdtar->archive_dir->head != NULL) {
1473                 for (p = bsdtar->archive_dir->head; p != NULL; p = p->next) {
1474                         if (pathcmp(path, p->name)==0)
1475                                 return (p->mtime_sec < st->st_mtime ||
1476                                     (p->mtime_sec == st->st_mtime &&
1477                                         p->mtime_nsec
1478                                         < ARCHIVE_STAT_MTIME_NANOS(st)));
1479                 }
1480         }
1481
1482         /* If the file wasn't rejected, include it. */
1483         return (1);
1484 }
1485
1486 /*
1487  * Add an entry to the dir list for 'u' mode.
1488  *
1489  * XXX TODO: Make this fast.
1490  */
1491 static void
1492 add_dir_list(struct bsdtar *bsdtar, const char *path,
1493     time_t mtime_sec, int mtime_nsec)
1494 {
1495         struct archive_dir_entry        *p;
1496
1497         /*
1498          * Search entire list to see if this file has appeared before.
1499          * If it has, override the timestamp data.
1500          */
1501         p = bsdtar->archive_dir->head;
1502         while (p != NULL) {
1503                 if (strcmp(path, p->name)==0) {
1504                         p->mtime_sec = mtime_sec;
1505                         p->mtime_nsec = mtime_nsec;
1506                         return;
1507                 }
1508                 p = p->next;
1509         }
1510
1511         p = malloc(sizeof(*p));
1512         if (p == NULL)
1513                 bsdtar_errc(bsdtar, 1, ENOMEM, "Can't read archive directory");
1514
1515         p->name = strdup(path);
1516         if (p->name == NULL)
1517                 bsdtar_errc(bsdtar, 1, ENOMEM, "Can't read archive directory");
1518         p->mtime_sec = mtime_sec;
1519         p->mtime_nsec = mtime_nsec;
1520         p->next = NULL;
1521         if (bsdtar->archive_dir->tail == NULL) {
1522                 bsdtar->archive_dir->head = bsdtar->archive_dir->tail = p;
1523         } else {
1524                 bsdtar->archive_dir->tail->next = p;
1525                 bsdtar->archive_dir->tail = p;
1526         }
1527 }
1528
1529 void
1530 test_for_append(struct bsdtar *bsdtar)
1531 {
1532         struct stat s;
1533
1534         if (*bsdtar->argv == NULL)
1535                 bsdtar_errc(bsdtar, 1, 0, "no files or directories specified");
1536         if (bsdtar->filename == NULL)
1537                 bsdtar_errc(bsdtar, 1, 0, "Cannot append to stdout.");
1538
1539         if (bsdtar->create_compression != 0)
1540                 bsdtar_errc(bsdtar, 1, 0,
1541                     "Cannot append to %s with compression", bsdtar->filename);
1542
1543         if (stat(bsdtar->filename, &s) != 0)
1544                 return;
1545
1546         if (!S_ISREG(s.st_mode))
1547                 bsdtar_errc(bsdtar, 1, 0,
1548                     "Cannot append to %s: not a regular file.",
1549                     bsdtar->filename);
1550 }