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