2 * Copyright (c) 2003-2004 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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$");
31 #include <sys/types.h>
36 #include <archive_entry.h>
49 #include <ext2fs/ext2_fs.h>
50 #include <sys/ioctl.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;
71 struct archive_dir_entry *head, *tail;
75 unsigned long number_entries;
76 size_t number_buckets;
77 struct links_entry **buckets;
81 struct links_entry *next;
82 struct links_entry *previous;
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 *,
103 static void archive_names_from_file(struct bsdtar *bsdtar,
105 static int archive_names_from_file_helper(struct bsdtar *bsdtar,
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 *,
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 *,
129 static void write_heirarchy(struct bsdtar *, struct archive *,
133 tar_mode_c(struct bsdtar *bsdtar)
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";
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));
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);
168 archive_write_set_bytes_per_block(a, DEFAULT_BYTES_PER_BLOCK);
170 switch (bsdtar->create_compression) {
175 archive_write_set_compression_bzip2(a);
180 archive_write_set_compression_gzip(a);
184 bsdtar_errc(bsdtar, 1, 0,
185 "Unrecognized compression option -%c",
186 bsdtar->create_compression);
189 r = archive_write_open_file(a, bsdtar->filename);
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
208 tar_mode_r(struct bsdtar *bsdtar)
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);
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);
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);
271 tar_mode_u(struct bsdtar *bsdtar)
275 struct archive_entry *entry;
276 const char *filename;
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));
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);
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);
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);
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);
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.
367 write_archive(struct archive *a, struct bsdtar *bsdtar)
371 if (bsdtar->names_from_file != NULL)
372 archive_names_from_file(bsdtar, a);
374 while (*bsdtar->argv) {
376 if (arg[0] == '-' && arg[1] == 'C') {
382 bsdtar_warnc(bsdtar, 1, 0,
383 "Missing argument for -C");
384 bsdtar->return_value = 1;
388 set_chdir(bsdtar, arg);
390 if (*arg != '/' || (arg[0] == '@' && arg[1] != '/'))
391 do_chdir(bsdtar); /* Handle a deferred -C */
393 if (append_archive(bsdtar, a, arg + 1) != 0)
396 write_heirarchy(bsdtar, a, arg);
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.
413 archive_names_from_file(struct bsdtar *bsdtar, struct 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");
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;
436 do_chdir(bsdtar); /* Handle a deferred -C */
437 write_heirarchy(bsdtar, bsdtar->archive, line);
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.
449 append_archive(struct bsdtar *bsdtar, struct archive *a, const char *filename)
452 struct archive_entry *in_entry;
453 int bytes_read, bytes_written;
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;
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)))
472 if (excluded(bsdtar, archive_entry_pathname(in_entry)))
474 if (bsdtar->option_interactive &&
475 !yes("copy '%s'", archive_entry_pathname(in_entry)))
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;
487 bytes_read = archive_read_data(ina, buff, sizeof(buff));
488 while (bytes_read > 0) {
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));
497 archive_read_data(ina, buff, sizeof(buff));
500 fprintf(stderr, "\n");
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;
509 return (0); /* TODO: Return non-zero on error */
513 * Add the file or dir heirarchy named by 'path' to the archive
516 write_heirarchy(struct bsdtar *bsdtar, struct archive *a, const char *path)
524 unsigned long fflags;
528 * Sigh: fts_open modifies it's first parameter, so we have to
529 * copy 'path' to mutable storage.
531 fts_argv[0] = strdup(path);
532 if (fts_argv[0] == NULL)
533 bsdtar_errc(bsdtar, 1, ENOMEM, "Can't open %s", path);
535 ftsoptions = FTS_PHYSICAL;
536 switch (bsdtar->symlink_mode) {
538 ftsoptions |= FTS_COMFOLLOW;
541 ftsoptions = FTS_COMFOLLOW | FTS_LOGICAL;
544 if (bsdtar->option_dont_traverse_mounts)
545 ftsoptions |= FTS_XDEV;
547 fts = fts_open(fts_argv, ftsoptions, NULL);
551 bsdtar_warnc(bsdtar, errno, "%s: Cannot open", path);
552 bsdtar->return_value = 1;
556 while ((ftsent = fts_read(fts))) {
557 switch (ftsent->fts_info) {
559 bsdtar_warnc(bsdtar, ftsent->fts_errno,
560 "%s: Could not stat", ftsent->fts_path);
561 bsdtar->return_value = 1;
564 bsdtar_warnc(bsdtar, ftsent->fts_errno, "%s",
566 bsdtar->return_value = 1;
569 bsdtar_warnc(bsdtar, ftsent->fts_errno,
570 "%s: Cannot read directory contents",
572 bsdtar->return_value = 1;
574 case FTS_W: /* Skip Whiteout entries */
576 case FTS_DC: /* Directory that causes cycle */
577 /* XXX Does this need special handling ? */
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
587 if (bsdtar->option_honor_nodump &&
588 (ftsent->fts_statp->st_flags & UF_NODUMP)) {
589 fts_set(fts, ftsent, FTS_SKIP);
596 * Linux has a nodump flag too but to read it
597 * we have to open() the dir and do an ioctl on it...
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);
610 * In -u mode, we need to check whether this
611 * is newer than what's already in the archive.
613 if (!new_enough(bsdtar, ftsent->fts_path,
614 ftsent->fts_statp->st_mtime,
615 ARCHIVE_STAT_MTIME_NANOS(ftsent->fts_statp)))
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
623 if (excluded(bsdtar, ftsent->fts_path)) {
624 fts_set(fts, ftsent, FTS_SKIP);
629 * If the user vetoes the directory, skip
632 if (bsdtar->option_interactive &&
633 !yes("add '%s'", ftsent->fts_path)) {
634 fts_set(fts, ftsent, FTS_SKIP);
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.
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);
654 * Skip this file if it's flagged "nodump" and we're
655 * honoring that flag.
657 #if defined(HAVE_CHFLAGS) && defined(UF_NODUMP)
658 if (bsdtar->option_honor_nodump &&
659 (ftsent->fts_statp->st_flags & UF_NODUMP))
665 * Linux has a nodump flag too but to read it
666 * we have to open() the file and do an ioctl on it...
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))
678 * Skip this file if it's excluded by a
681 if (excluded(bsdtar, ftsent->fts_path))
685 * In -u mode, we need to check whether this
686 * is newer than what's already in the archive.
688 if (!new_enough(bsdtar, ftsent->fts_path,
689 ftsent->fts_statp->st_mtime,
690 ARCHIVE_STAT_MTIME_NANOS(ftsent->fts_statp)))
693 if (bsdtar->option_interactive &&
694 !yes("add '%s'", ftsent->fts_path)) {
698 write_entry(bsdtar, a, ftsent->fts_statp,
699 ftsent->fts_path, ftsent->fts_pathlen,
700 ftsent->fts_accpath);
705 bsdtar_warnc(bsdtar, 0,
706 "%s: Heirarchy traversal error %d\n",
714 bsdtar_warnc(bsdtar, errno, "%s", path);
716 bsdtar_warnc(bsdtar, errno, "fts_close failed");
721 * Add a single filesystem object to the archive.
724 write_entry(struct bsdtar *bsdtar, struct archive *a, struct stat *st,
725 const char *pathname, unsigned pathlen, const char *accpath)
727 struct archive_entry *entry;
732 unsigned long stflags;
734 static char linkbuffer[PATH_MAX+1];
736 (void)pathlen; /* UNUSED */
739 entry = archive_entry_new();
741 /* Non-regular files get archived with zero size. */
742 if (!S_ISREG(st->st_mode))
745 /* Strip redundant "./" from start of filename. */
746 if (pathname != NULL && pathname[0] == '.' && pathname[1] == '/') {
748 if (*pathname == '\0') /* This is the "./" directory. */
749 goto cleanup; /* Don't archive it ever. */
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;
763 archive_entry_set_pathname(entry, pathname);
765 if (!S_ISDIR(st->st_mode) && (st->st_nlink > 1))
766 lookup_hardlink(bsdtar, entry, st);
768 /* Display entry as we process it. This format is required by SUSv2. */
770 safe_fprintf(stderr, "a %s", pathname);
772 /* Read symbolic link information. */
773 if ((st->st_mode & S_IFMT) == S_IFLNK) {
776 lnklen = readlink(accpath, linkbuffer, PATH_MAX);
778 if (!bsdtar->verbose)
779 bsdtar_warnc(bsdtar, errno,
780 "%s: Couldn't read symbolic link",
784 ": Couldn't read symbolic link: %s",
788 linkbuffer[lnklen] = 0;
789 archive_entry_set_symlink(entry, linkbuffer);
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));
797 if (st->st_flags != 0)
798 archive_entry_set_fflags(entry, st->st_flags, 0);
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 &&
806 archive_entry_set_fflags(entry, stflags, 0);
810 archive_entry_copy_stat(entry, st);
811 setup_acls(bsdtar, entry, accpath);
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
819 if (S_ISREG(st->st_mode) && st->st_size > 0) {
820 fd = open(accpath, O_RDONLY);
822 if (!bsdtar->verbose)
823 bsdtar_warnc(bsdtar, errno, "%s", pathname);
825 fprintf(stderr, ": %s", strerror(errno));
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));
836 fprintf(stderr, ": %s", archive_error_string(a));
839 if (e == ARCHIVE_FATAL)
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.
848 if (fd >= 0 && archive_entry_size(entry) > 0)
849 write_file_data(bsdtar, a, fd);
856 archive_entry_free(entry);
859 fprintf(stderr, "\n");
863 /* Helper function to copy file to archive, with stack-allocated buffer. */
865 write_file_data(struct bsdtar *bsdtar, struct archive *a, int fd)
869 ssize_t bytes_written;
871 /* XXX TODO: Allocate buffer on heap and store pointer to
872 * it in bsdtar structure; arrange cleanup as well. XXX */
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));
887 create_cleanup(struct bsdtar *bsdtar)
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;
896 free_cache(bsdtar->uname_cache);
897 bsdtar->uname_cache = NULL;
898 free_cache(bsdtar->gname_cache);
899 bsdtar->gname_cache = NULL;
904 free_buckets(struct bsdtar *bsdtar, struct links_cache *links_cache)
908 if (links_cache->buckets == NULL)
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;
923 free(links_cache->buckets);
924 links_cache->buckets = NULL;
928 lookup_hardlink(struct bsdtar *bsdtar, struct archive_entry *entry,
929 const struct stat *st)
931 struct links_cache *links_cache;
932 struct links_entry *le, **new_buckets;
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.");
952 for (i = 0; i < links_cache->number_buckets; i++)
953 links_cache->buckets[i] = NULL;
956 /* If the links cache overflowed and got flushed, don't bother. */
957 if (links_cache->buckets == NULL)
960 /* If the links cache is getting too full, enlarge the hash table. */
961 if (links_cache->number_entries > links_cache->number_buckets * 2)
965 new_size = links_cache->number_buckets * 2;
966 new_buckets = malloc(new_size * sizeof(struct links_entry *));
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;
979 /* Add entry to new bucket. */
980 hash = (le->dev ^ le->ino) % new_size;
982 if (new_buckets[hash] != NULL)
983 new_buckets[hash]->previous =
985 le->next = new_buckets[hash];
987 new_buckets[hash] = le;
990 free(links_cache->buckets);
991 links_cache->buckets = new_buckets;
992 links_cache->number_buckets = new_size;
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");
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);
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
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)
1021 if (links_cache->buckets[hash] == le)
1022 links_cache->buckets[hash] = le->next;
1023 links_cache->number_entries--;
1031 /* Add this entry to the links cache. */
1032 le = malloc(sizeof(struct links_entry));
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");
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;
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);
1062 setup_acls(struct bsdtar *bsdtar, struct archive_entry *entry,
1063 const char *accpath)
1065 archive_entry_acl_clear(entry);
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);
1076 setup_acl(struct bsdtar *bsdtar, struct archive_entry *entry,
1077 const char *accpath, int acl_type, int archive_entry_acl_type)
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;
1086 /* Retrieve access ACL from file. */
1087 acl = acl_get_file(accpath, acl_type);
1089 s = acl_get_entry(acl, ACL_FIRST_ENTRY, &acl_entry);
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;
1112 /* Skip types that libarchive can't support. */
1116 acl_get_permset(acl_entry, &acl_permset);
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;
1125 archive_entry_acl_add_entry(entry,
1126 archive_entry_acl_type, ae_perm, ae_tag,
1129 s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
1136 setup_acls(struct bsdtar *bsdtar, struct archive_entry *entry,
1137 const char *accpath)
1146 free_cache(struct name_cache *cache)
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);
1161 * Lookup uid/gid from uname/gname, return NULL if no match.
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)
1167 struct name_cache *cache;
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;
1180 cache = *name_cache_variable;
1183 slot = id % cache->size;
1184 if (cache->cache[slot].name != NULL) {
1185 if (cache->cache[slot].id == id) {
1187 if (cache->cache[slot].name == NO_NAME)
1189 return (cache->cache[slot].name);
1191 if (cache->cache[slot].name != NO_NAME)
1192 free((void *)(uintptr_t)cache->cache[slot].name);
1193 cache->cache[slot].name = NULL;
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;
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);
1208 * Conveniently, NULL marks an empty slot, so
1209 * if the strdup() fails, we've just failed to
1210 * cache it. No recovery necessary.
1218 lookup_uname(struct bsdtar *bsdtar, uid_t uid)
1220 return (lookup_name(bsdtar, &bsdtar->uname_cache,
1221 &lookup_uname_helper, (id_t)uid));
1225 lookup_uname_helper(struct bsdtar *bsdtar, const char **name, id_t id)
1227 struct passwd *pwent;
1229 (void)bsdtar; /* UNUSED */
1232 pwent = getpwuid((uid_t)id);
1233 if (pwent == NULL) {
1236 bsdtar_warnc(bsdtar, errno, "getpwuid(%d) failed", id);
1240 *name = pwent->pw_name;
1245 lookup_gname(struct bsdtar *bsdtar, gid_t gid)
1247 return (lookup_name(bsdtar, &bsdtar->gname_cache,
1248 &lookup_gname_helper, (id_t)gid));
1252 lookup_gname_helper(struct bsdtar *bsdtar, const char **name, id_t id)
1254 struct group *grent;
1256 (void)bsdtar; /* UNUSED */
1259 grent = getgrgid((gid_t)id);
1260 if (grent == NULL) {
1263 bsdtar_warnc(bsdtar, errno, "getgrgid(%d) failed", id);
1267 *name = grent->gr_name;
1272 * Test if the specified file is newer than what's already
1276 new_enough(struct bsdtar *bsdtar, const char *path,
1277 time_t mtime_sec, int mtime_nsec)
1279 struct archive_dir_entry *p;
1281 if (path[0] == '.' && path[1] == '/' && path[2] != '\0')
1284 if (bsdtar->archive_dir == NULL ||
1285 bsdtar->archive_dir->head == NULL)
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));
1298 * Add an entry to the dir list for 'u' mode.
1300 * XXX TODO: Make this fast.
1303 add_dir_list(struct bsdtar *bsdtar, const char *path,
1304 time_t mtime_sec, int mtime_nsec)
1306 struct archive_dir_entry *p;
1308 if (path[0] == '.' && path[1] == '/' && path[2] != '\0')
1312 * Search entire list to see if this file has appeared before.
1313 * If it has, override the timestamp data.
1315 p = bsdtar->archive_dir->head;
1317 if (strcmp(path, p->name)==0) {
1318 p->mtime_sec = mtime_sec;
1319 p->mtime_nsec = mtime_nsec;
1325 p = malloc(sizeof(*p));
1327 bsdtar_errc(bsdtar, 1, ENOMEM, "Can't read archive directory");
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;
1335 if (bsdtar->archive_dir->tail == NULL) {
1336 bsdtar->archive_dir->head = bsdtar->archive_dir->tail = p;
1338 bsdtar->archive_dir->tail->next = p;
1339 bsdtar->archive_dir->tail = p;
1344 test_for_append(struct bsdtar *bsdtar)
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.");
1353 if (bsdtar->create_compression != 0)
1354 bsdtar_errc(bsdtar, 1, 0,
1355 "Cannot append to %s with compression", bsdtar->filename);
1357 if (stat(bsdtar->filename, &s) != 0)
1358 bsdtar_errc(bsdtar, 1, errno,
1359 "Cannot stat %s", bsdtar->filename);
1361 if (!S_ISREG(s.st_mode))
1362 bsdtar_errc(bsdtar, 1, 0,
1363 "Cannot append to %s: not a regular file.",