Sync with FreeBSD. This adds read-only support for zip and ISO9660.
[dragonfly.git] / contrib / bsdtar / write.c
blob0ffe77a70a901443f8f566726aa12f0137f51a82
1 /*-
2 * Copyright (c) 2003-2004 Tim Kientzle
3 * All rights reserved.
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.
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.
27 #include "bsdtar_platform.h"
28 __FBSDID("$FreeBSD: src/usr.bin/tar/write.c,v 1.40 2005/04/24 05:53:37 kientzle Exp $");
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 <grp.h>
41 #include <limits.h>
42 #include <pwd.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #ifdef __linux
48 #include <ext2fs/ext2_fs.h>
49 #include <sys/ioctl.h>
50 #endif
52 #include "bsdtar.h"
53 #include "tree.h"
55 /* Fixed size of uname/gname caches. */
56 #define name_cache_size 101
58 static const char * const NO_NAME = "(noname)";
60 /* Initial size of link cache. */
61 #define links_cache_initial_size 1024
63 struct archive_dir_entry {
64 struct archive_dir_entry *next;
65 time_t mtime_sec;
66 int mtime_nsec;
67 char *name;
70 struct archive_dir {
71 struct archive_dir_entry *head, *tail;
74 struct links_cache {
75 unsigned long number_entries;
76 size_t number_buckets;
77 struct links_entry **buckets;
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;
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];
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 const struct stat *);
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 const 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_hierarchy(struct bsdtar *, struct archive *,
130 const char *);
132 void
133 tar_mode_c(struct bsdtar *bsdtar)
135 struct archive *a;
136 int r;
138 if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
139 bsdtar_errc(bsdtar, 1, 0, "no files or directories specified");
141 a = archive_write_new();
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);
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);
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.
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);
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);
189 r = archive_write_open_file(a, bsdtar->filename);
190 if (r != ARCHIVE_OK)
191 bsdtar_errc(bsdtar, 1, 0, archive_error_string(a));
193 write_archive(a, bsdtar);
195 if (bsdtar->option_totals) {
196 fprintf(stderr, "Total bytes written: " BSDTAR_FILESIZE_PRINTF "\n",
197 (BSDTAR_FILESIZE_TYPE)archive_position_compressed(a));
200 archive_write_finish(a);
204 * Same as 'c', except we only support tar formats in uncompressed
205 * files on disk.
207 void
208 tar_mode_r(struct bsdtar *bsdtar)
210 off_t end_offset;
211 int format;
212 struct archive *a;
213 struct archive_entry *entry;
215 /* Sanity-test some arguments and the file. */
216 test_for_append(bsdtar);
218 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
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);
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.");
237 /* Keep going until we hit end-of-archive */
238 format = archive_format(a);
241 end_offset = archive_read_header_position(a);
242 archive_read_finish(a);
244 /* Re-open archive for writing */
245 a = archive_write_new();
246 archive_write_set_compression_none(a);
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.
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 */
258 write_archive(a, bsdtar); /* XXX check return val XXX */
260 if (bsdtar->option_totals) {
261 fprintf(stderr, "Total bytes written: " BSDTAR_FILESIZE_PRINTF "\n",
262 (BSDTAR_FILESIZE_TYPE)archive_position_compressed(a));
265 archive_write_finish(a);
266 close(bsdtar->fd);
267 bsdtar->fd = -1;
270 void
271 tar_mode_u(struct bsdtar *bsdtar)
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;
281 bsdtar->archive_dir = &archive_dir;
282 memset(&archive_dir, 0, sizeof(archive_dir));
284 filename = NULL;
285 format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
287 /* Sanity-test some arguments and the file. */
288 test_for_append(bsdtar);
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);
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);
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.");
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 */
319 end_offset = archive_read_header_position(a);
320 archive_read_finish(a);
322 /* Re-open archive for writing. */
323 a = archive_write_new();
324 archive_write_set_compression_none(a);
326 * Set format to same one auto-detected above, except that
327 * we don't write GNU tar format, so use ustar instead.
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);
342 write_archive(a, bsdtar);
344 if (bsdtar->option_totals) {
345 fprintf(stderr, "Total bytes written: " BSDTAR_FILESIZE_PRINTF "\n",
346 (BSDTAR_FILESIZE_TYPE)archive_position_compressed(a));
349 archive_write_finish(a);
350 close(bsdtar->fd);
351 bsdtar->fd = -1;
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;
359 bsdtar->archive_dir->tail = NULL;
364 * Write user-specified files/dirs to opened archive.
366 static void
367 write_archive(struct archive *a, struct bsdtar *bsdtar)
369 const char *arg;
371 if (bsdtar->names_from_file != NULL)
372 archive_names_from_file(bsdtar, a);
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;
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_hierarchy(bsdtar, a, arg);
398 bsdtar->argv++;
401 create_cleanup(bsdtar);
402 archive_write_close(a);
406 * Archive names specified in file.
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.
412 void
413 archive_names_from_file(struct bsdtar *bsdtar, struct archive *a)
415 bsdtar->archive = a;
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");
426 static int
427 archive_names_from_file_helper(struct bsdtar *bsdtar, const char *line)
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_hierarchy(bsdtar, bsdtar->archive, line);
439 return (0);
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.
448 static int
449 append_archive(struct bsdtar *bsdtar, struct archive *a, const char *filename)
451 struct archive *ina;
452 struct archive_entry *in_entry;
453 int bytes_read, bytes_written;
454 char buff[8192];
456 if (strcmp(filename, "-") == 0)
457 filename = NULL; /* Library uses NULL for stdio. */
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);
467 while (0 == archive_read_next_header(ina, &in_entry)) {
468 if (!new_enough(bsdtar, archive_entry_pathname(in_entry),
469 archive_entry_stat(in_entry)))
470 continue;
471 if (excluded(bsdtar, archive_entry_pathname(in_entry)))
472 continue;
473 if (bsdtar->option_interactive &&
474 !yes("copy '%s'", archive_entry_pathname(in_entry)))
475 continue;
476 if (bsdtar->verbose)
477 safe_fprintf(stderr, "a %s",
478 archive_entry_pathname(in_entry));
479 /* XXX handle/report errors XXX */
480 if (archive_write_header(a, in_entry)) {
481 bsdtar_warnc(bsdtar, 0, "%s",
482 archive_error_string(ina));
483 bsdtar->return_value = 1;
484 return (-1);
486 bytes_read = archive_read_data(ina, buff, sizeof(buff));
487 while (bytes_read > 0) {
488 bytes_written =
489 archive_write_data(a, buff, bytes_read);
490 if (bytes_written < bytes_read) {
491 bsdtar_warnc(bsdtar, archive_errno(a), "%s",
492 archive_error_string(a));
493 return (-1);
495 bytes_read =
496 archive_read_data(ina, buff, sizeof(buff));
498 if (bsdtar->verbose)
499 fprintf(stderr, "\n");
502 if (archive_errno(ina)) {
503 bsdtar_warnc(bsdtar, 0, "Error reading archive %s: %s",
504 filename, archive_error_string(ina));
505 bsdtar->return_value = 1;
508 return (0); /* TODO: Return non-zero on error */
512 * Add the file or dir hierarchy named by 'path' to the archive
514 static void
515 write_hierarchy(struct bsdtar *bsdtar, struct archive *a, const char *path)
517 struct tree *tree;
518 char symlink_mode = bsdtar->symlink_mode;
519 dev_t first_dev = 0;
520 int dev_recorded = 0;
521 int tree_ret;
522 #ifdef __linux
523 int fd, r;
524 unsigned long fflags;
525 #endif
527 tree = tree_open(path);
529 if (!tree) {
530 bsdtar_warnc(bsdtar, errno, "%s: Cannot open", path);
531 bsdtar->return_value = 1;
532 return;
535 while ((tree_ret = tree_next(tree))) {
536 const char *name = tree_current_path(tree);
537 const struct stat *st = NULL, *lst = NULL;
538 int descend;
540 if (tree_ret == TREE_ERROR)
541 bsdtar_warnc(bsdtar, errno, "%s", name);
542 if (tree_ret != TREE_REGULAR)
543 continue;
544 lst = tree_current_lstat(tree);
545 if (lst == NULL) {
546 /* Couldn't lstat(); must not exist. */
547 bsdtar_warnc(bsdtar, errno, "%s: Cannot open", path);
548 bsdtar->return_value = 1;
549 continue;
551 if (S_ISLNK(lst->st_mode))
552 st = tree_current_stat(tree);
553 /* Default: descend into any dir or symlink to dir. */
554 /* We'll adjust this later on. */
555 descend = 0;
556 if ((st != NULL) && S_ISDIR(st->st_mode))
557 descend = 1;
558 if ((lst != NULL) && S_ISDIR(lst->st_mode))
559 descend = 1;
562 * If user has asked us not to cross mount points,
563 * then don't descend into into a dir on a different
564 * device.
566 if (!dev_recorded) {
567 first_dev = lst->st_dev;
568 dev_recorded = 1;
570 if (bsdtar->option_dont_traverse_mounts) {
571 if (lst != NULL && lst->st_dev != first_dev)
572 descend = 0;
576 * If this file/dir is flagged "nodump" and we're
577 * honoring such flags, skip this file/dir.
579 #ifdef HAVE_CHFLAGS
580 if (bsdtar->option_honor_nodump &&
581 (lst->st_flags & UF_NODUMP))
582 continue;
583 #endif
585 #ifdef __linux
587 * Linux has a nodump flag too but to read it
588 * we have to open() the file/dir and do an ioctl on it...
590 if (bsdtar->option_honor_nodump &&
591 ((fd = open(name, O_RDONLY|O_NONBLOCK)) >= 0) &&
592 ((r = ioctl(fd, EXT2_IOC_GETFLAGS, &fflags)),
593 close(fd), r) >= 0 &&
594 (fflags & EXT2_NODUMP_FL))
595 continue;
596 #endif
599 * If this file/dir is excluded by a filename
600 * pattern, skip it.
602 if (excluded(bsdtar, name))
603 continue;
606 * If the user vetoes this file/directory, skip it.
608 if (bsdtar->option_interactive &&
609 !yes("add '%s'", name))
610 continue;
613 * If this is a dir, decide whether or not to recurse.
615 if (bsdtar->option_no_subdirs)
616 descend = 0;
619 * Distinguish 'L'/'P'/'H' symlink following.
621 switch(symlink_mode) {
622 case 'H':
623 /* 'H': First item (from command line) like 'L'. */
624 lst = tree_current_stat(tree);
625 /* 'H': After the first item, rest like 'P'. */
626 symlink_mode = 'P';
627 break;
628 case 'L':
629 /* 'L': Do descend through a symlink to dir. */
630 /* 'L': Archive symlink to file as file. */
631 lst = tree_current_stat(tree);
632 break;
633 default:
634 /* 'P': Don't descend through a symlink to dir. */
635 if (!S_ISDIR(lst->st_mode))
636 descend = 0;
637 /* 'P': Archive symlink to file as symlink. */
638 /* lst = tree_current_lstat(tree); */
639 break;
642 if (descend)
643 tree_descend(tree);
646 * In -u mode, we need to check whether this
647 * is newer than what's already in the archive.
648 * In all modes, we need to obey --newerXXX flags.
650 if (new_enough(bsdtar, name, lst)) {
651 write_entry(bsdtar, a, lst, name,
652 tree_current_pathlen(tree),
653 tree_current_access_path(tree));
656 tree_close(tree);
660 * Add a single filesystem object to the archive.
662 static void
663 write_entry(struct bsdtar *bsdtar, struct archive *a, const struct stat *st,
664 const char *pathname, unsigned pathlen, const char *accpath)
666 struct archive_entry *entry;
667 int e;
668 int fd;
669 #ifdef __linux
670 int r;
671 unsigned long stflags;
672 #endif
673 static char linkbuffer[PATH_MAX+1];
675 (void)pathlen; /* UNUSED */
677 fd = -1;
678 entry = archive_entry_new();
680 archive_entry_set_pathname(entry, pathname);
683 * Rewrite the pathname to be archived. If rewrite
684 * fails, skip the entry.
686 if (edit_pathname(bsdtar, entry))
687 goto abort;
689 if (!S_ISDIR(st->st_mode) && (st->st_nlink > 1))
690 lookup_hardlink(bsdtar, entry, st);
692 /* Display entry as we process it. This format is required by SUSv2. */
693 if (bsdtar->verbose)
694 safe_fprintf(stderr, "a %s", archive_entry_pathname(entry));
696 /* Read symbolic link information. */
697 if ((st->st_mode & S_IFMT) == S_IFLNK) {
698 int lnklen;
700 lnklen = readlink(accpath, linkbuffer, PATH_MAX);
701 if (lnklen < 0) {
702 if (!bsdtar->verbose)
703 bsdtar_warnc(bsdtar, errno,
704 "%s: Couldn't read symbolic link",
705 pathname);
706 else
707 safe_fprintf(stderr,
708 ": Couldn't read symbolic link: %s",
709 strerror(errno));
710 goto cleanup;
712 linkbuffer[lnklen] = 0;
713 archive_entry_set_symlink(entry, linkbuffer);
716 /* Look up username and group name. */
717 archive_entry_set_uname(entry, lookup_uname(bsdtar, st->st_uid));
718 archive_entry_set_gname(entry, lookup_gname(bsdtar, st->st_gid));
720 #ifdef HAVE_CHFLAGS
721 if (st->st_flags != 0)
722 archive_entry_set_fflags(entry, st->st_flags, 0);
723 #endif
725 #ifdef __linux
726 if ((S_ISREG(st->st_mode) || S_ISDIR(st->st_mode)) &&
727 ((fd = open(accpath, O_RDONLY|O_NONBLOCK)) >= 0) &&
728 ((r = ioctl(fd, EXT2_IOC_GETFLAGS, &stflags)), close(fd), (fd = -1), r) >= 0 &&
729 stflags) {
730 archive_entry_set_fflags(entry, stflags, 0);
732 #endif
734 archive_entry_copy_stat(entry, st);
735 setup_acls(bsdtar, entry, accpath);
738 * If it's a regular file (and non-zero in size) make sure we
739 * can open it before we start to write. In particular, note
740 * that we can always archive a zero-length file, even if we
741 * can't read it.
743 if (S_ISREG(st->st_mode) && st->st_size > 0) {
744 fd = open(accpath, O_RDONLY);
745 if (fd < 0) {
746 if (!bsdtar->verbose)
747 bsdtar_warnc(bsdtar, errno, "%s", pathname);
748 else
749 fprintf(stderr, ": %s", strerror(errno));
750 goto cleanup;
754 /* Non-regular files get archived with zero size. */
755 if (!S_ISREG(st->st_mode))
756 archive_entry_set_size(entry, 0);
758 e = archive_write_header(a, entry);
759 if (e != ARCHIVE_OK) {
760 if (!bsdtar->verbose)
761 bsdtar_warnc(bsdtar, 0, "%s: %s", pathname,
762 archive_error_string(a));
763 else
764 fprintf(stderr, ": %s", archive_error_string(a));
767 if (e == ARCHIVE_FATAL)
768 exit(1);
771 * If we opened a file earlier, write it out now. Note that
772 * the format handler might have reset the size field to zero
773 * to inform us that the archive body won't get stored. In
774 * that case, just skip the write.
776 if (fd >= 0 && archive_entry_size(entry) > 0)
777 write_file_data(bsdtar, a, fd);
779 cleanup:
780 if (bsdtar->verbose)
781 fprintf(stderr, "\n");
783 abort:
784 if (fd >= 0)
785 close(fd);
787 if (entry != NULL)
788 archive_entry_free(entry);
792 /* Helper function to copy file to archive, with stack-allocated buffer. */
793 static int
794 write_file_data(struct bsdtar *bsdtar, struct archive *a, int fd)
796 char buff[64*1024];
797 ssize_t bytes_read;
798 ssize_t bytes_written;
800 /* XXX TODO: Allocate buffer on heap and store pointer to
801 * it in bsdtar structure; arrange cleanup as well. XXX */
802 (void)bsdtar;
804 bytes_read = read(fd, buff, sizeof(buff));
805 while (bytes_read > 0) {
806 bytes_written = archive_write_data(a, buff, bytes_read);
807 if (bytes_written <= 0)
808 return (-1); /* Write failed; this is bad */
809 bytes_read = read(fd, buff, sizeof(buff));
811 return 0;
815 static void
816 create_cleanup(struct bsdtar *bsdtar)
818 /* Free inode->pathname map used for hardlink detection. */
819 if (bsdtar->links_cache != NULL) {
820 free_buckets(bsdtar, bsdtar->links_cache);
821 free(bsdtar->links_cache);
822 bsdtar->links_cache = NULL;
825 free_cache(bsdtar->uname_cache);
826 bsdtar->uname_cache = NULL;
827 free_cache(bsdtar->gname_cache);
828 bsdtar->gname_cache = NULL;
832 static void
833 free_buckets(struct bsdtar *bsdtar, struct links_cache *links_cache)
835 size_t i;
837 if (links_cache->buckets == NULL)
838 return;
840 for (i = 0; i < links_cache->number_buckets; i++) {
841 while (links_cache->buckets[i] != NULL) {
842 struct links_entry *lp = links_cache->buckets[i]->next;
843 if (bsdtar->option_warn_links)
844 bsdtar_warnc(bsdtar, 0, "Missing links to %s",
845 links_cache->buckets[i]->name);
846 if (links_cache->buckets[i]->name != NULL)
847 free(links_cache->buckets[i]->name);
848 free(links_cache->buckets[i]);
849 links_cache->buckets[i] = lp;
852 free(links_cache->buckets);
853 links_cache->buckets = NULL;
856 static void
857 lookup_hardlink(struct bsdtar *bsdtar, struct archive_entry *entry,
858 const struct stat *st)
860 struct links_cache *links_cache;
861 struct links_entry *le, **new_buckets;
862 int hash;
863 size_t i, new_size;
865 /* If necessary, initialize the links cache. */
866 links_cache = bsdtar->links_cache;
867 if (links_cache == NULL) {
868 bsdtar->links_cache = malloc(sizeof(struct links_cache));
869 if (bsdtar->links_cache == NULL)
870 bsdtar_errc(bsdtar, 1, ENOMEM,
871 "No memory for hardlink detection.");
872 links_cache = bsdtar->links_cache;
873 memset(links_cache, 0, sizeof(struct links_cache));
874 links_cache->number_buckets = links_cache_initial_size;
875 links_cache->buckets = malloc(links_cache->number_buckets *
876 sizeof(links_cache->buckets[0]));
877 if (links_cache->buckets == NULL) {
878 bsdtar_errc(bsdtar, 1, ENOMEM,
879 "No memory for hardlink detection.");
881 for (i = 0; i < links_cache->number_buckets; i++)
882 links_cache->buckets[i] = NULL;
885 /* If the links cache overflowed and got flushed, don't bother. */
886 if (links_cache->buckets == NULL)
887 return;
889 /* If the links cache is getting too full, enlarge the hash table. */
890 if (links_cache->number_entries > links_cache->number_buckets * 2)
892 int count;
894 new_size = links_cache->number_buckets * 2;
895 new_buckets = malloc(new_size * sizeof(struct links_entry *));
897 count = 0;
899 if (new_buckets != NULL) {
900 memset(new_buckets, 0,
901 new_size * sizeof(struct links_entry *));
902 for (i = 0; i < links_cache->number_buckets; i++) {
903 while (links_cache->buckets[i] != NULL) {
904 /* Remove entry from old bucket. */
905 le = links_cache->buckets[i];
906 links_cache->buckets[i] = le->next;
908 /* Add entry to new bucket. */
909 hash = (le->dev ^ le->ino) % new_size;
911 if (new_buckets[hash] != NULL)
912 new_buckets[hash]->previous =
914 le->next = new_buckets[hash];
915 le->previous = NULL;
916 new_buckets[hash] = le;
919 free(links_cache->buckets);
920 links_cache->buckets = new_buckets;
921 links_cache->number_buckets = new_size;
922 } else {
923 free_buckets(bsdtar, links_cache);
924 bsdtar_warnc(bsdtar, ENOMEM,
925 "No more memory for recording hard links");
926 bsdtar_warnc(bsdtar, 0,
927 "Remaining links will be dumped as full files");
931 /* Try to locate this entry in the links cache. */
932 hash = ( st->st_dev ^ st->st_ino ) % links_cache->number_buckets;
933 for (le = links_cache->buckets[hash]; le != NULL; le = le->next) {
934 if (le->dev == st->st_dev && le->ino == st->st_ino) {
935 archive_entry_copy_hardlink(entry, le->name);
938 * Decrement link count each time and release
939 * the entry if it hits zero. This saves
940 * memory and is necessary for proper -l
941 * implementation.
943 if (--le->links <= 0) {
944 if (le->previous != NULL)
945 le->previous->next = le->next;
946 if (le->next != NULL)
947 le->next->previous = le->previous;
948 if (le->name != NULL)
949 free(le->name);
950 if (links_cache->buckets[hash] == le)
951 links_cache->buckets[hash] = le->next;
952 links_cache->number_entries--;
953 free(le);
956 return;
960 /* Add this entry to the links cache. */
961 le = malloc(sizeof(struct links_entry));
962 if (le != NULL)
963 le->name = strdup(archive_entry_pathname(entry));
964 if ((le == NULL) || (le->name == NULL)) {
965 free_buckets(bsdtar, links_cache);
966 bsdtar_warnc(bsdtar, ENOMEM,
967 "No more memory for recording hard links");
968 bsdtar_warnc(bsdtar, 0,
969 "Remaining hard links will be dumped as full files");
970 if (le != NULL)
971 free(le);
972 return;
974 if (links_cache->buckets[hash] != NULL)
975 links_cache->buckets[hash]->previous = le;
976 links_cache->number_entries++;
977 le->next = links_cache->buckets[hash];
978 le->previous = NULL;
979 links_cache->buckets[hash] = le;
980 le->dev = st->st_dev;
981 le->ino = st->st_ino;
982 le->links = st->st_nlink - 1;
985 #ifdef HAVE_POSIX_ACL
986 void setup_acl(struct bsdtar *bsdtar,
987 struct archive_entry *entry, const char *accpath,
988 int acl_type, int archive_entry_acl_type);
990 void
991 setup_acls(struct bsdtar *bsdtar, struct archive_entry *entry,
992 const char *accpath)
994 archive_entry_acl_clear(entry);
996 setup_acl(bsdtar, entry, accpath,
997 ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
998 /* Only directories can have default ACLs. */
999 if (S_ISDIR(archive_entry_mode(entry)))
1000 setup_acl(bsdtar, entry, accpath,
1001 ACL_TYPE_DEFAULT, ARCHIVE_ENTRY_ACL_TYPE_DEFAULT);
1004 void
1005 setup_acl(struct bsdtar *bsdtar, struct archive_entry *entry,
1006 const char *accpath, int acl_type, int archive_entry_acl_type)
1008 acl_t acl;
1009 acl_tag_t acl_tag;
1010 acl_entry_t acl_entry;
1011 acl_permset_t acl_permset;
1012 int s, ae_id, ae_tag, ae_perm;
1013 const char *ae_name;
1015 /* Retrieve access ACL from file. */
1016 acl = acl_get_file(accpath, acl_type);
1017 if (acl != NULL) {
1018 s = acl_get_entry(acl, ACL_FIRST_ENTRY, &acl_entry);
1019 while (s == 1) {
1020 ae_id = -1;
1021 ae_name = NULL;
1023 acl_get_tag_type(acl_entry, &acl_tag);
1024 if (acl_tag == ACL_USER) {
1025 ae_id = (int)*(uid_t *)acl_get_qualifier(acl_entry);
1026 ae_name = lookup_uname(bsdtar, ae_id);
1027 ae_tag = ARCHIVE_ENTRY_ACL_USER;
1028 } else if (acl_tag == ACL_GROUP) {
1029 ae_id = (int)*(gid_t *)acl_get_qualifier(acl_entry);
1030 ae_name = lookup_gname(bsdtar, ae_id);
1031 ae_tag = ARCHIVE_ENTRY_ACL_GROUP;
1032 } else if (acl_tag == ACL_MASK) {
1033 ae_tag = ARCHIVE_ENTRY_ACL_MASK;
1034 } else if (acl_tag == ACL_USER_OBJ) {
1035 ae_tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1036 } else if (acl_tag == ACL_GROUP_OBJ) {
1037 ae_tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1038 } else if (acl_tag == ACL_OTHER) {
1039 ae_tag = ARCHIVE_ENTRY_ACL_OTHER;
1040 } else {
1041 /* Skip types that libarchive can't support. */
1042 continue;
1045 acl_get_permset(acl_entry, &acl_permset);
1046 ae_perm = 0;
1048 * acl_get_perm() is spelled differently on different
1049 * platforms; see bsdtar_platform.h for details.
1051 if (ACL_GET_PERM(acl_permset, ACL_EXECUTE))
1052 ae_perm |= ARCHIVE_ENTRY_ACL_EXECUTE;
1053 if (ACL_GET_PERM(acl_permset, ACL_READ))
1054 ae_perm |= ARCHIVE_ENTRY_ACL_READ;
1055 if (ACL_GET_PERM(acl_permset, ACL_WRITE))
1056 ae_perm |= ARCHIVE_ENTRY_ACL_WRITE;
1058 archive_entry_acl_add_entry(entry,
1059 archive_entry_acl_type, ae_perm, ae_tag,
1060 ae_id, ae_name);
1062 s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
1064 acl_free(acl);
1067 #else
1068 void
1069 setup_acls(struct bsdtar *bsdtar, struct archive_entry *entry,
1070 const char *accpath)
1072 (void)bsdtar;
1073 (void)entry;
1074 (void)accpath;
1076 #endif
1078 static void
1079 free_cache(struct name_cache *cache)
1081 size_t i;
1083 if (cache != NULL) {
1084 for(i = 0; i < cache->size; i++) {
1085 if (cache->cache[i].name != NULL &&
1086 cache->cache[i].name != NO_NAME)
1087 free((void *)(uintptr_t)cache->cache[i].name);
1089 free(cache);
1094 * Lookup uid/gid from uname/gname, return NULL if no match.
1096 static const char *
1097 lookup_name(struct bsdtar *bsdtar, struct name_cache **name_cache_variable,
1098 int (*lookup_fn)(struct bsdtar *, const char **, id_t), id_t id)
1100 struct name_cache *cache;
1101 const char *name;
1102 int slot;
1105 if (*name_cache_variable == NULL) {
1106 *name_cache_variable = malloc(sizeof(struct name_cache));
1107 if (*name_cache_variable == NULL)
1108 bsdtar_errc(bsdtar, 1, ENOMEM, "No more memory");
1109 memset(*name_cache_variable, 0, sizeof(struct name_cache));
1110 (*name_cache_variable)->size = name_cache_size;
1113 cache = *name_cache_variable;
1114 cache->probes++;
1116 slot = id % cache->size;
1117 if (cache->cache[slot].name != NULL) {
1118 if (cache->cache[slot].id == id) {
1119 cache->hits++;
1120 if (cache->cache[slot].name == NO_NAME)
1121 return (NULL);
1122 return (cache->cache[slot].name);
1124 if (cache->cache[slot].name != NO_NAME)
1125 free((void *)(uintptr_t)cache->cache[slot].name);
1126 cache->cache[slot].name = NULL;
1129 if (lookup_fn(bsdtar, &name, id) == 0) {
1130 if (name == NULL || name[0] == '\0') {
1131 /* Cache the negative response. */
1132 cache->cache[slot].name = NO_NAME;
1133 cache->cache[slot].id = id;
1134 } else {
1135 cache->cache[slot].name = strdup(name);
1136 if (cache->cache[slot].name != NULL) {
1137 cache->cache[slot].id = id;
1138 return (cache->cache[slot].name);
1141 * Conveniently, NULL marks an empty slot, so
1142 * if the strdup() fails, we've just failed to
1143 * cache it. No recovery necessary.
1147 return (NULL);
1150 static const char *
1151 lookup_uname(struct bsdtar *bsdtar, uid_t uid)
1153 return (lookup_name(bsdtar, &bsdtar->uname_cache,
1154 &lookup_uname_helper, (id_t)uid));
1157 static int
1158 lookup_uname_helper(struct bsdtar *bsdtar, const char **name, id_t id)
1160 struct passwd *pwent;
1162 (void)bsdtar; /* UNUSED */
1164 errno = 0;
1165 pwent = getpwuid((uid_t)id);
1166 if (pwent == NULL) {
1167 *name = NULL;
1168 if (errno != 0)
1169 bsdtar_warnc(bsdtar, errno, "getpwuid(%d) failed", id);
1170 return (errno);
1173 *name = pwent->pw_name;
1174 return (0);
1177 static const char *
1178 lookup_gname(struct bsdtar *bsdtar, gid_t gid)
1180 return (lookup_name(bsdtar, &bsdtar->gname_cache,
1181 &lookup_gname_helper, (id_t)gid));
1184 static int
1185 lookup_gname_helper(struct bsdtar *bsdtar, const char **name, id_t id)
1187 struct group *grent;
1189 (void)bsdtar; /* UNUSED */
1191 errno = 0;
1192 grent = getgrgid((gid_t)id);
1193 if (grent == NULL) {
1194 *name = NULL;
1195 if (errno != 0)
1196 bsdtar_warnc(bsdtar, errno, "getgrgid(%d) failed", id);
1197 return (errno);
1200 *name = grent->gr_name;
1201 return (0);
1205 * Test if the specified file is new enough to include in the archive.
1208 new_enough(struct bsdtar *bsdtar, const char *path, const struct stat *st)
1210 struct archive_dir_entry *p;
1213 * If this file/dir is excluded by a time comparison, skip it.
1215 if (bsdtar->newer_ctime_sec > 0) {
1216 if (st->st_ctime < bsdtar->newer_ctime_sec)
1217 return (0); /* Too old, skip it. */
1218 if (st->st_ctime == bsdtar->newer_ctime_sec
1219 && ARCHIVE_STAT_CTIME_NANOS(st)
1220 <= bsdtar->newer_ctime_nsec)
1221 return (0); /* Too old, skip it. */
1223 if (bsdtar->newer_mtime_sec > 0) {
1224 if (st->st_mtime < bsdtar->newer_mtime_sec)
1225 return (0); /* Too old, skip it. */
1226 if (st->st_mtime == bsdtar->newer_mtime_sec
1227 && ARCHIVE_STAT_MTIME_NANOS(st)
1228 <= bsdtar->newer_mtime_nsec)
1229 return (0); /* Too old, skip it. */
1233 * In -u mode, we only write an entry if it's newer than
1234 * what was already in the archive.
1236 if (bsdtar->archive_dir != NULL &&
1237 bsdtar->archive_dir->head != NULL) {
1238 /* Ignore leading './' when comparing names. */
1239 if (path[0] == '.' && path[1] == '/' && path[2] != '\0')
1240 path += 2;
1242 for (p = bsdtar->archive_dir->head; p != NULL; p = p->next) {
1243 if (strcmp(path, p->name)==0)
1244 return (p->mtime_sec < st->st_mtime ||
1245 (p->mtime_sec == st->st_mtime &&
1246 p->mtime_nsec
1247 < ARCHIVE_STAT_MTIME_NANOS(st)));
1251 /* If the file wasn't rejected, include it. */
1252 return (1);
1256 * Add an entry to the dir list for 'u' mode.
1258 * XXX TODO: Make this fast.
1260 static void
1261 add_dir_list(struct bsdtar *bsdtar, const char *path,
1262 time_t mtime_sec, int mtime_nsec)
1264 struct archive_dir_entry *p;
1266 if (path[0] == '.' && path[1] == '/' && path[2] != '\0')
1267 path += 2;
1270 * Search entire list to see if this file has appeared before.
1271 * If it has, override the timestamp data.
1273 p = bsdtar->archive_dir->head;
1274 while (p != NULL) {
1275 if (strcmp(path, p->name)==0) {
1276 p->mtime_sec = mtime_sec;
1277 p->mtime_nsec = mtime_nsec;
1278 return;
1280 p = p->next;
1283 p = malloc(sizeof(*p));
1284 if (p == NULL)
1285 bsdtar_errc(bsdtar, 1, ENOMEM, "Can't read archive directory");
1287 p->name = strdup(path);
1288 if (p->name == NULL)
1289 bsdtar_errc(bsdtar, 1, ENOMEM, "Can't read archive directory");
1290 p->mtime_sec = mtime_sec;
1291 p->mtime_nsec = mtime_nsec;
1292 p->next = NULL;
1293 if (bsdtar->archive_dir->tail == NULL) {
1294 bsdtar->archive_dir->head = bsdtar->archive_dir->tail = p;
1295 } else {
1296 bsdtar->archive_dir->tail->next = p;
1297 bsdtar->archive_dir->tail = p;
1301 void
1302 test_for_append(struct bsdtar *bsdtar)
1304 struct stat s;
1306 if (*bsdtar->argv == NULL)
1307 bsdtar_errc(bsdtar, 1, 0, "no files or directories specified");
1308 if (bsdtar->filename == NULL)
1309 bsdtar_errc(bsdtar, 1, 0, "Cannot append to stdout.");
1311 if (bsdtar->create_compression != 0)
1312 bsdtar_errc(bsdtar, 1, 0,
1313 "Cannot append to %s with compression", bsdtar->filename);
1315 if (stat(bsdtar->filename, &s) != 0)
1316 bsdtar_errc(bsdtar, 1, errno,
1317 "Cannot stat %s", bsdtar->filename);
1319 if (!S_ISREG(s.st_mode))
1320 bsdtar_errc(bsdtar, 1, 0,
1321 "Cannot append to %s: not a regular file.",
1322 bsdtar->filename);