2 * Copyright (c) 2003-2007 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 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "bsdtar_platform.h"
27 __FBSDID("$FreeBSD: src/usr.bin/tar/write.c,v 1.62 2007/05/03 04:33:11 cperciva Exp $");
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
35 #ifdef HAVE_SYS_IOCTL_H
36 #include <sys/ioctl.h>
38 #ifdef HAVE_SYS_STAT_H
41 #ifdef HAVE_ATTR_XATTR_H
42 #include <attr/xattr.h>
47 #ifdef HAVE_EXT2FS_EXT2_FS_H
48 #include <ext2fs/ext2_fs.h>
62 #ifdef HAVE_LINUX_FS_H
63 #include <linux/fs.h> /* for Linux file flags */
65 #ifdef HAVE_LINUX_EXT2_FS_H
66 #include <linux/ext2_fs.h> /* for Linux file flags */
85 /* Fixed size of uname/gname caches. */
86 #define name_cache_size 101
88 static const char * const NO_NAME
= "(noname)";
90 /* Initial size of link cache. */
91 #define links_cache_initial_size 1024
93 struct archive_dir_entry
{
94 struct archive_dir_entry
*next
;
101 struct archive_dir_entry
*head
, *tail
;
105 unsigned long number_entries
;
106 size_t number_buckets
;
107 struct links_entry
**buckets
;
111 struct links_entry
*next
;
112 struct links_entry
*previous
;
126 } cache
[name_cache_size
];
129 static void add_dir_list(struct bsdtar
*bsdtar
, const char *path
,
130 time_t mtime_sec
, int mtime_nsec
);
131 static int append_archive(struct bsdtar
*, struct archive
*,
132 struct archive
*ina
);
133 static int append_archive_filename(struct bsdtar
*,
134 struct archive
*, const char *fname
);
135 static void archive_names_from_file(struct bsdtar
*bsdtar
,
137 static int archive_names_from_file_helper(struct bsdtar
*bsdtar
,
139 static int copy_file_data(struct bsdtar
*bsdtar
,
140 struct archive
*a
, struct archive
*ina
);
141 static void create_cleanup(struct bsdtar
*);
142 static void free_buckets(struct bsdtar
*, struct links_cache
*);
143 static void free_cache(struct name_cache
*cache
);
144 static const char * lookup_gname(struct bsdtar
*bsdtar
, gid_t gid
);
145 static int lookup_gname_helper(struct bsdtar
*bsdtar
,
146 const char **name
, id_t gid
);
147 static void lookup_hardlink(struct bsdtar
*,
148 struct archive_entry
*entry
, const struct stat
*);
149 static const char * lookup_uname(struct bsdtar
*bsdtar
, uid_t uid
);
150 static int lookup_uname_helper(struct bsdtar
*bsdtar
,
151 const char **name
, id_t uid
);
152 static int new_enough(struct bsdtar
*, const char *path
,
153 const struct stat
*);
154 static void setup_acls(struct bsdtar
*, struct archive_entry
*,
156 static void setup_xattrs(struct bsdtar
*, struct archive_entry
*,
158 static void test_for_append(struct bsdtar
*);
159 static void write_archive(struct archive
*, struct bsdtar
*);
160 static void write_entry(struct bsdtar
*, struct archive
*,
161 const struct stat
*, const char *pathname
,
162 const char *accpath
);
163 static int write_file_data(struct bsdtar
*, struct archive
*,
165 static void write_hierarchy(struct bsdtar
*, struct archive
*,
169 tar_mode_c(struct bsdtar
*bsdtar
)
174 if (*bsdtar
->argv
== NULL
&& bsdtar
->names_from_file
== NULL
)
175 bsdtar_errc(bsdtar
, 1, 0, "no files or directories specified");
177 a
= archive_write_new();
179 /* Support any format that the library supports. */
180 if (bsdtar
->create_format
== NULL
) {
181 r
= archive_write_set_format_pax_restricted(a
);
182 bsdtar
->create_format
= "pax restricted";
184 r
= archive_write_set_format_by_name(a
, bsdtar
->create_format
);
186 if (r
!= ARCHIVE_OK
) {
187 fprintf(stderr
, "Can't use format %s: %s\n",
188 bsdtar
->create_format
,
189 archive_error_string(a
));
194 * If user explicitly set the block size, then assume they
195 * want the last block padded as well. Otherwise, use the
196 * default block size and accept archive_write_open_file()'s
197 * default padding decisions.
199 if (bsdtar
->bytes_per_block
!= 0) {
200 archive_write_set_bytes_per_block(a
, bsdtar
->bytes_per_block
);
201 archive_write_set_bytes_in_last_block(a
,
202 bsdtar
->bytes_per_block
);
204 archive_write_set_bytes_per_block(a
, DEFAULT_BYTES_PER_BLOCK
);
206 if (bsdtar
->compress_program
) {
207 archive_write_set_compression_program(a
, bsdtar
->compress_program
);
209 switch (bsdtar
->create_compression
) {
211 archive_write_set_compression_none(a
);
215 archive_write_set_compression_bzip2(a
);
220 archive_write_set_compression_gzip(a
);
224 bsdtar_errc(bsdtar
, 1, 0,
225 "Unrecognized compression option -%c",
226 bsdtar
->create_compression
);
230 r
= archive_write_open_file(a
, bsdtar
->filename
);
232 bsdtar_errc(bsdtar
, 1, 0, archive_error_string(a
));
234 write_archive(a
, bsdtar
);
236 if (bsdtar
->option_totals
) {
237 fprintf(stderr
, "Total bytes written: " BSDTAR_FILESIZE_PRINTF
"\n",
238 (BSDTAR_FILESIZE_TYPE
)archive_position_compressed(a
));
241 archive_write_finish(a
);
245 * Same as 'c', except we only support tar or empty formats in
246 * uncompressed files on disk.
249 tar_mode_r(struct bsdtar
*bsdtar
)
254 struct archive_entry
*entry
;
257 /* Sanity-test some arguments and the file. */
258 test_for_append(bsdtar
);
260 format
= ARCHIVE_FORMAT_TAR_PAX_RESTRICTED
;
262 bsdtar
->fd
= open(bsdtar
->filename
, O_RDWR
| O_CREAT
, 0666);
264 bsdtar_errc(bsdtar
, 1, errno
,
265 "Cannot open %s", bsdtar
->filename
);
267 a
= archive_read_new();
268 archive_read_support_compression_all(a
);
269 archive_read_support_format_tar(a
);
270 archive_read_support_format_gnutar(a
);
271 r
= archive_read_open_fd(a
, bsdtar
->fd
, 10240);
273 bsdtar_errc(bsdtar
, 1, archive_errno(a
),
274 "Can't read archive %s: %s", bsdtar
->filename
,
275 archive_error_string(a
));
276 while (0 == archive_read_next_header(a
, &entry
)) {
277 if (archive_compression(a
) != ARCHIVE_COMPRESSION_NONE
) {
278 archive_read_finish(a
);
280 bsdtar_errc(bsdtar
, 1, 0,
281 "Cannot append to compressed archive.");
283 /* Keep going until we hit end-of-archive */
284 format
= archive_format(a
);
287 end_offset
= archive_read_header_position(a
);
288 archive_read_finish(a
);
290 /* Re-open archive for writing */
291 a
= archive_write_new();
292 archive_write_set_compression_none(a
);
294 * Set the format to be used for writing. To allow people to
295 * extend empty files, we need to allow them to specify the format,
296 * which opens the possibility that they will specify a format that
297 * doesn't match the existing format. Hence, the following bit
298 * of arcane ugliness.
301 if (bsdtar
->create_format
!= NULL
) {
302 /* If the user requested a format, use that, but ... */
303 archive_write_set_format_by_name(a
,
304 bsdtar
->create_format
);
305 /* ... complain if it's not compatible. */
306 format
&= ARCHIVE_FORMAT_BASE_MASK
;
307 if (format
!= (int)(archive_format(a
) & ARCHIVE_FORMAT_BASE_MASK
)
308 && format
!= ARCHIVE_FORMAT_EMPTY
) {
309 bsdtar_errc(bsdtar
, 1, 0,
310 "Format %s is incompatible with the archive %s.",
311 bsdtar
->create_format
, bsdtar
->filename
);
315 * Just preserve the current format, with a little care
316 * for formats that libarchive can't write.
318 if (format
== ARCHIVE_FORMAT_TAR_GNUTAR
)
319 /* TODO: When gtar supports pax, use pax restricted. */
320 format
= ARCHIVE_FORMAT_TAR_USTAR
;
321 if (format
== ARCHIVE_FORMAT_EMPTY
)
322 format
= ARCHIVE_FORMAT_TAR_PAX_RESTRICTED
;
323 archive_write_set_format(a
, format
);
325 lseek(bsdtar
->fd
, end_offset
, SEEK_SET
); /* XXX check return val XXX */
326 archive_write_open_fd(a
, bsdtar
->fd
); /* XXX check return val XXX */
328 write_archive(a
, bsdtar
); /* XXX check return val XXX */
330 if (bsdtar
->option_totals
) {
331 fprintf(stderr
, "Total bytes written: " BSDTAR_FILESIZE_PRINTF
"\n",
332 (BSDTAR_FILESIZE_TYPE
)archive_position_compressed(a
));
335 archive_write_finish(a
);
341 tar_mode_u(struct bsdtar
*bsdtar
)
345 struct archive_entry
*entry
;
347 struct archive_dir_entry
*p
;
348 struct archive_dir archive_dir
;
350 bsdtar
->archive_dir
= &archive_dir
;
351 memset(&archive_dir
, 0, sizeof(archive_dir
));
353 format
= ARCHIVE_FORMAT_TAR_PAX_RESTRICTED
;
355 /* Sanity-test some arguments and the file. */
356 test_for_append(bsdtar
);
358 bsdtar
->fd
= open(bsdtar
->filename
, O_RDWR
);
360 bsdtar_errc(bsdtar
, 1, errno
,
361 "Cannot open %s", bsdtar
->filename
);
363 a
= archive_read_new();
364 archive_read_support_compression_all(a
);
365 archive_read_support_format_tar(a
);
366 archive_read_support_format_gnutar(a
);
367 if (archive_read_open_fd(a
, bsdtar
->fd
,
368 bsdtar
->bytes_per_block
!= 0 ? bsdtar
->bytes_per_block
:
369 DEFAULT_BYTES_PER_BLOCK
) != ARCHIVE_OK
) {
370 bsdtar_errc(bsdtar
, 1, 0,
371 "Can't open %s: %s", bsdtar
->filename
,
372 archive_error_string(a
));
375 /* Build a list of all entries and their recorded mod times. */
376 while (0 == archive_read_next_header(a
, &entry
)) {
377 if (archive_compression(a
) != ARCHIVE_COMPRESSION_NONE
) {
378 archive_read_finish(a
);
380 bsdtar_errc(bsdtar
, 1, 0,
381 "Cannot append to compressed archive.");
383 add_dir_list(bsdtar
, archive_entry_pathname(entry
),
384 archive_entry_mtime(entry
),
385 archive_entry_mtime_nsec(entry
));
386 /* Record the last format determination we see */
387 format
= archive_format(a
);
388 /* Keep going until we hit end-of-archive */
391 end_offset
= archive_read_header_position(a
);
392 archive_read_finish(a
);
394 /* Re-open archive for writing. */
395 a
= archive_write_new();
396 archive_write_set_compression_none(a
);
398 * Set format to same one auto-detected above, except that
399 * we don't write GNU tar format, so use ustar instead.
401 if (format
== ARCHIVE_FORMAT_TAR_GNUTAR
)
402 format
= ARCHIVE_FORMAT_TAR_USTAR
;
403 archive_write_set_format(a
, format
);
404 if (bsdtar
->bytes_per_block
!= 0) {
405 archive_write_set_bytes_per_block(a
, bsdtar
->bytes_per_block
);
406 archive_write_set_bytes_in_last_block(a
,
407 bsdtar
->bytes_per_block
);
409 archive_write_set_bytes_per_block(a
, DEFAULT_BYTES_PER_BLOCK
);
410 lseek(bsdtar
->fd
, end_offset
, SEEK_SET
);
411 ftruncate(bsdtar
->fd
, end_offset
);
412 archive_write_open_fd(a
, bsdtar
->fd
);
414 write_archive(a
, bsdtar
);
416 if (bsdtar
->option_totals
) {
417 fprintf(stderr
, "Total bytes written: " BSDTAR_FILESIZE_PRINTF
"\n",
418 (BSDTAR_FILESIZE_TYPE
)archive_position_compressed(a
));
421 archive_write_finish(a
);
425 while (bsdtar
->archive_dir
->head
!= NULL
) {
426 p
= bsdtar
->archive_dir
->head
->next
;
427 free(bsdtar
->archive_dir
->head
->name
);
428 free(bsdtar
->archive_dir
->head
);
429 bsdtar
->archive_dir
->head
= p
;
431 bsdtar
->archive_dir
->tail
= NULL
;
436 * Write user-specified files/dirs to opened archive.
439 write_archive(struct archive
*a
, struct bsdtar
*bsdtar
)
443 if (bsdtar
->names_from_file
!= NULL
)
444 archive_names_from_file(bsdtar
, a
);
446 while (*bsdtar
->argv
) {
448 if (arg
[0] == '-' && arg
[1] == 'C') {
454 bsdtar_warnc(bsdtar
, 1, 0,
455 "Missing argument for -C");
456 bsdtar
->return_value
= 1;
460 set_chdir(bsdtar
, arg
);
462 if (*arg
!= '/' && (arg
[0] != '@' || arg
[1] != '/'))
463 do_chdir(bsdtar
); /* Handle a deferred -C */
465 if (append_archive_filename(bsdtar
, a
,
469 write_hierarchy(bsdtar
, a
, arg
);
474 create_cleanup(bsdtar
);
475 if (archive_write_close(a
)) {
476 bsdtar_warnc(bsdtar
, 0, "%s", archive_error_string(a
));
477 bsdtar
->return_value
= 1;
482 * Archive names specified in file.
484 * Unless --null was specified, a line containing exactly "-C" will
485 * cause the next line to be a directory to pass to chdir(). If
486 * --null is specified, then a line "-C" is just another filename.
489 archive_names_from_file(struct bsdtar
*bsdtar
, struct archive
*a
)
493 bsdtar
->next_line_is_dir
= 0;
494 process_lines(bsdtar
, bsdtar
->names_from_file
,
495 archive_names_from_file_helper
);
496 if (bsdtar
->next_line_is_dir
)
497 bsdtar_errc(bsdtar
, 1, errno
,
498 "Unexpected end of filename list; "
499 "directory expected after -C");
503 archive_names_from_file_helper(struct bsdtar
*bsdtar
, const char *line
)
505 if (bsdtar
->next_line_is_dir
) {
506 set_chdir(bsdtar
, line
);
507 bsdtar
->next_line_is_dir
= 0;
508 } else if (!bsdtar
->option_null
&& strcmp(line
, "-C") == 0)
509 bsdtar
->next_line_is_dir
= 1;
512 do_chdir(bsdtar
); /* Handle a deferred -C */
513 write_hierarchy(bsdtar
, bsdtar
->archive
, line
);
519 * Copy from specified archive to current archive. Returns non-zero
520 * for write errors (which force us to terminate the entire archiving
521 * operation). If there are errors reading the input archive, we set
522 * bsdtar->return_value but return zero, so the overall archiving
523 * operation will complete and return non-zero.
526 append_archive_filename(struct bsdtar
*bsdtar
, struct archive
*a
,
527 const char *filename
)
532 if (strcmp(filename
, "-") == 0)
533 filename
= NULL
; /* Library uses NULL for stdio. */
535 ina
= archive_read_new();
536 archive_read_support_format_all(ina
);
537 archive_read_support_compression_all(ina
);
538 if (archive_read_open_file(ina
, filename
, 10240)) {
539 bsdtar_warnc(bsdtar
, 0, "%s", archive_error_string(ina
));
540 bsdtar
->return_value
= 1;
544 rc
= append_archive(bsdtar
, a
, ina
);
546 if (archive_errno(ina
)) {
547 bsdtar_warnc(bsdtar
, 0, "Error reading archive %s: %s",
548 filename
, archive_error_string(ina
));
549 bsdtar
->return_value
= 1;
551 archive_read_finish(ina
);
557 append_archive(struct bsdtar
*bsdtar
, struct archive
*a
, struct archive
*ina
)
559 struct archive_entry
*in_entry
;
562 while (0 == archive_read_next_header(ina
, &in_entry
)) {
563 if (!new_enough(bsdtar
, archive_entry_pathname(in_entry
),
564 archive_entry_stat(in_entry
)))
566 if (excluded(bsdtar
, archive_entry_pathname(in_entry
)))
568 if (bsdtar
->option_interactive
&&
569 !yes("copy '%s'", archive_entry_pathname(in_entry
)))
572 safe_fprintf(stderr
, "a %s",
573 archive_entry_pathname(in_entry
));
575 e
= archive_write_header(a
, in_entry
);
576 if (e
!= ARCHIVE_OK
) {
577 if (!bsdtar
->verbose
)
578 bsdtar_warnc(bsdtar
, 0, "%s: %s",
579 archive_entry_pathname(in_entry
),
580 archive_error_string(a
));
582 fprintf(stderr
, ": %s", archive_error_string(a
));
584 if (e
== ARCHIVE_FATAL
)
587 if (e
>= ARCHIVE_WARN
)
588 if (copy_file_data(bsdtar
, a
, ina
))
592 fprintf(stderr
, "\n");
595 /* Note: If we got here, we saw no write errors, so return success. */
599 /* Helper function to copy data between archives. */
601 copy_file_data(struct bsdtar
*bsdtar
, struct archive
*a
, struct archive
*ina
)
605 ssize_t bytes_written
;
607 bytes_read
= archive_read_data(ina
, buff
, sizeof(buff
));
608 while (bytes_read
> 0) {
609 bytes_written
= archive_write_data(a
, buff
, bytes_read
);
610 if (bytes_written
< bytes_read
) {
611 bsdtar_warnc(bsdtar
, 0, "%s", archive_error_string(a
));
614 bytes_read
= archive_read_data(ina
, buff
, sizeof(buff
));
621 * Add the file or dir hierarchy named by 'path' to the archive
624 write_hierarchy(struct bsdtar
*bsdtar
, struct archive
*a
, const char *path
)
627 char symlink_mode
= bsdtar
->symlink_mode
;
629 int dev_recorded
= 0;
633 unsigned long fflags
;
636 tree
= tree_open(path
);
639 bsdtar_warnc(bsdtar
, errno
, "%s: Cannot open", path
);
640 bsdtar
->return_value
= 1;
644 while ((tree_ret
= tree_next(tree
))) {
645 const char *name
= tree_current_path(tree
);
646 const struct stat
*st
= NULL
, *lst
= NULL
;
649 if (tree_ret
== TREE_ERROR_DIR
)
650 bsdtar_warnc(bsdtar
, errno
, "%s: Couldn't visit directory", name
);
651 if (tree_ret
!= TREE_REGULAR
)
653 lst
= tree_current_lstat(tree
);
655 /* Couldn't lstat(); must not exist. */
656 bsdtar_warnc(bsdtar
, errno
, "%s: Cannot stat", name
);
659 * Report an error via the exit code if the failed
660 * path is a prefix of what the user provided via
661 * the command line. (Testing for string equality
662 * here won't work due to trailing '/' characters.)
664 if (memcmp(name
, path
, strlen(name
)) == 0)
665 bsdtar
->return_value
= 1;
669 if (S_ISLNK(lst
->st_mode
))
670 st
= tree_current_stat(tree
);
671 /* Default: descend into any dir or symlink to dir. */
672 /* We'll adjust this later on. */
674 if ((st
!= NULL
) && S_ISDIR(st
->st_mode
))
676 if ((lst
!= NULL
) && S_ISDIR(lst
->st_mode
))
680 * If user has asked us not to cross mount points,
681 * then don't descend into into a dir on a different
685 first_dev
= lst
->st_dev
;
688 if (bsdtar
->option_dont_traverse_mounts
) {
689 if (lst
!= NULL
&& lst
->st_dev
!= first_dev
)
694 * If this file/dir is flagged "nodump" and we're
695 * honoring such flags, skip this file/dir.
698 if (bsdtar
->option_honor_nodump
&&
699 (lst
->st_flags
& UF_NODUMP
))
705 * Linux has a nodump flag too but to read it
706 * we have to open() the file/dir and do an ioctl on it...
708 if (bsdtar
->option_honor_nodump
&&
709 ((fd
= open(name
, O_RDONLY
|O_NONBLOCK
)) >= 0) &&
710 ((r
= ioctl(fd
, EXT2_IOC_GETFLAGS
, &fflags
)),
711 close(fd
), r
) >= 0 &&
712 (fflags
& EXT2_NODUMP_FL
))
717 * If this file/dir is excluded by a filename
720 if (excluded(bsdtar
, name
))
724 * If the user vetoes this file/directory, skip it.
726 if (bsdtar
->option_interactive
&&
727 !yes("add '%s'", name
))
731 * If this is a dir, decide whether or not to recurse.
733 if (bsdtar
->option_no_subdirs
)
737 * Distinguish 'L'/'P'/'H' symlink following.
739 switch(symlink_mode
) {
741 /* 'H': After the first item, rest like 'P'. */
743 /* 'H': First item (from command line) like 'L'. */
746 /* 'L': Do descend through a symlink to dir. */
747 /* 'L': Archive symlink to file as file. */
748 lst
= tree_current_stat(tree
);
749 /* If stat fails, we have a broken symlink;
750 * in that case, archive the link as such. */
752 lst
= tree_current_lstat(tree
);
755 /* 'P': Don't descend through a symlink to dir. */
756 if (!S_ISDIR(lst
->st_mode
))
758 /* 'P': Archive symlink to file as symlink. */
759 /* lst = tree_current_lstat(tree); */
767 * Write the entry. Note that write_entry() handles
768 * pathname editing and newness testing.
770 write_entry(bsdtar
, a
, lst
, name
,
771 tree_current_access_path(tree
));
777 * Add a single filesystem object to the archive.
780 write_entry(struct bsdtar
*bsdtar
, struct archive
*a
, const struct stat
*st
,
781 const char *pathname
, const char *accpath
)
783 struct archive_entry
*entry
;
788 unsigned long stflags
;
790 static char linkbuffer
[PATH_MAX
+1];
793 entry
= archive_entry_new();
795 archive_entry_set_pathname(entry
, pathname
);
798 * Rewrite the pathname to be archived. If rewrite
799 * fails, skip the entry.
801 if (edit_pathname(bsdtar
, entry
))
805 * In -u mode, check that the file is newer than what's
806 * already in the archive; in all modes, obey --newerXXX flags.
808 if (!new_enough(bsdtar
, archive_entry_pathname(entry
), st
))
811 if (!S_ISDIR(st
->st_mode
) && (st
->st_nlink
> 1))
812 lookup_hardlink(bsdtar
, entry
, st
);
814 /* Display entry as we process it. This format is required by SUSv2. */
816 safe_fprintf(stderr
, "a %s", archive_entry_pathname(entry
));
818 /* Read symbolic link information. */
819 if ((st
->st_mode
& S_IFMT
) == S_IFLNK
) {
822 lnklen
= readlink(accpath
, linkbuffer
, PATH_MAX
);
824 if (!bsdtar
->verbose
)
825 bsdtar_warnc(bsdtar
, errno
,
826 "%s: Couldn't read symbolic link",
830 ": Couldn't read symbolic link: %s",
834 linkbuffer
[lnklen
] = 0;
835 archive_entry_set_symlink(entry
, linkbuffer
);
838 /* Look up username and group name. */
839 archive_entry_set_uname(entry
, lookup_uname(bsdtar
, st
->st_uid
));
840 archive_entry_set_gname(entry
, lookup_gname(bsdtar
, st
->st_gid
));
843 if (st
->st_flags
!= 0)
844 archive_entry_set_fflags(entry
, st
->st_flags
, 0);
848 if ((S_ISREG(st
->st_mode
) || S_ISDIR(st
->st_mode
)) &&
849 ((fd
= open(accpath
, O_RDONLY
|O_NONBLOCK
)) >= 0) &&
850 ((r
= ioctl(fd
, EXT2_IOC_GETFLAGS
, &stflags
)), close(fd
), (fd
= -1), r
) >= 0 &&
852 archive_entry_set_fflags(entry
, stflags
, 0);
856 archive_entry_copy_stat(entry
, st
);
857 setup_acls(bsdtar
, entry
, accpath
);
858 setup_xattrs(bsdtar
, entry
, accpath
);
861 * If it's a regular file (and non-zero in size) make sure we
862 * can open it before we start to write. In particular, note
863 * that we can always archive a zero-length file, even if we
866 if (S_ISREG(st
->st_mode
) && st
->st_size
> 0) {
867 fd
= open(accpath
, O_RDONLY
);
869 if (!bsdtar
->verbose
)
870 bsdtar_warnc(bsdtar
, errno
, "%s: could not open file", pathname
);
872 fprintf(stderr
, ": %s", strerror(errno
));
877 /* Non-regular files get archived with zero size. */
878 if (!S_ISREG(st
->st_mode
))
879 archive_entry_set_size(entry
, 0);
881 e
= archive_write_header(a
, entry
);
882 if (e
!= ARCHIVE_OK
) {
883 if (!bsdtar
->verbose
)
884 bsdtar_warnc(bsdtar
, 0, "%s: %s", pathname
,
885 archive_error_string(a
));
887 fprintf(stderr
, ": %s", archive_error_string(a
));
890 if (e
== ARCHIVE_FATAL
)
894 * If we opened a file earlier, write it out now. Note that
895 * the format handler might have reset the size field to zero
896 * to inform us that the archive body won't get stored. In
897 * that case, just skip the write.
899 if (e
>= ARCHIVE_WARN
&& fd
>= 0 && archive_entry_size(entry
) > 0)
900 if (write_file_data(bsdtar
, a
, fd
))
905 fprintf(stderr
, "\n");
912 archive_entry_free(entry
);
916 /* Helper function to copy file to archive, with stack-allocated buffer. */
918 write_file_data(struct bsdtar
*bsdtar
, struct archive
*a
, int fd
)
922 ssize_t bytes_written
;
924 /* XXX TODO: Allocate buffer on heap and store pointer to
925 * it in bsdtar structure; arrange cleanup as well. XXX */
927 bytes_read
= read(fd
, buff
, sizeof(buff
));
928 while (bytes_read
> 0) {
929 bytes_written
= archive_write_data(a
, buff
, bytes_read
);
930 if (bytes_written
< 0) {
931 /* Write failed; this is bad */
932 bsdtar_warnc(bsdtar
, 0, "%s", archive_error_string(a
));
935 if (bytes_written
< bytes_read
) {
936 /* Write was truncated; warn but continue. */
937 bsdtar_warnc(bsdtar
, 0,
938 "Truncated write; file may have grown while being archived.");
941 bytes_read
= read(fd
, buff
, sizeof(buff
));
948 create_cleanup(struct bsdtar
*bsdtar
)
950 /* Free inode->pathname map used for hardlink detection. */
951 if (bsdtar
->links_cache
!= NULL
) {
952 free_buckets(bsdtar
, bsdtar
->links_cache
);
953 free(bsdtar
->links_cache
);
954 bsdtar
->links_cache
= NULL
;
957 free_cache(bsdtar
->uname_cache
);
958 bsdtar
->uname_cache
= NULL
;
959 free_cache(bsdtar
->gname_cache
);
960 bsdtar
->gname_cache
= NULL
;
965 free_buckets(struct bsdtar
*bsdtar
, struct links_cache
*links_cache
)
969 if (links_cache
->buckets
== NULL
)
972 for (i
= 0; i
< links_cache
->number_buckets
; i
++) {
973 while (links_cache
->buckets
[i
] != NULL
) {
974 struct links_entry
*lp
= links_cache
->buckets
[i
]->next
;
975 if (bsdtar
->option_warn_links
)
976 bsdtar_warnc(bsdtar
, 0, "Missing links to %s",
977 links_cache
->buckets
[i
]->name
);
978 if (links_cache
->buckets
[i
]->name
!= NULL
)
979 free(links_cache
->buckets
[i
]->name
);
980 free(links_cache
->buckets
[i
]);
981 links_cache
->buckets
[i
] = lp
;
984 free(links_cache
->buckets
);
985 links_cache
->buckets
= NULL
;
989 lookup_hardlink(struct bsdtar
*bsdtar
, struct archive_entry
*entry
,
990 const struct stat
*st
)
992 struct links_cache
*links_cache
;
993 struct links_entry
*le
, **new_buckets
;
997 /* If necessary, initialize the links cache. */
998 links_cache
= bsdtar
->links_cache
;
999 if (links_cache
== NULL
) {
1000 bsdtar
->links_cache
= malloc(sizeof(struct links_cache
));
1001 if (bsdtar
->links_cache
== NULL
)
1002 bsdtar_errc(bsdtar
, 1, ENOMEM
,
1003 "No memory for hardlink detection.");
1004 links_cache
= bsdtar
->links_cache
;
1005 memset(links_cache
, 0, sizeof(struct links_cache
));
1006 links_cache
->number_buckets
= links_cache_initial_size
;
1007 links_cache
->buckets
= malloc(links_cache
->number_buckets
*
1008 sizeof(links_cache
->buckets
[0]));
1009 if (links_cache
->buckets
== NULL
) {
1010 bsdtar_errc(bsdtar
, 1, ENOMEM
,
1011 "No memory for hardlink detection.");
1013 for (i
= 0; i
< links_cache
->number_buckets
; i
++)
1014 links_cache
->buckets
[i
] = NULL
;
1017 /* If the links cache overflowed and got flushed, don't bother. */
1018 if (links_cache
->buckets
== NULL
)
1021 /* If the links cache is getting too full, enlarge the hash table. */
1022 if (links_cache
->number_entries
> links_cache
->number_buckets
* 2)
1024 new_size
= links_cache
->number_buckets
* 2;
1025 new_buckets
= malloc(new_size
* sizeof(struct links_entry
*));
1027 if (new_buckets
!= NULL
) {
1028 memset(new_buckets
, 0,
1029 new_size
* sizeof(struct links_entry
*));
1030 for (i
= 0; i
< links_cache
->number_buckets
; i
++) {
1031 while (links_cache
->buckets
[i
] != NULL
) {
1032 /* Remove entry from old bucket. */
1033 le
= links_cache
->buckets
[i
];
1034 links_cache
->buckets
[i
] = le
->next
;
1036 /* Add entry to new bucket. */
1037 hash
= (le
->dev
^ le
->ino
) % new_size
;
1039 if (new_buckets
[hash
] != NULL
)
1040 new_buckets
[hash
]->previous
=
1042 le
->next
= new_buckets
[hash
];
1043 le
->previous
= NULL
;
1044 new_buckets
[hash
] = le
;
1047 free(links_cache
->buckets
);
1048 links_cache
->buckets
= new_buckets
;
1049 links_cache
->number_buckets
= new_size
;
1051 free_buckets(bsdtar
, links_cache
);
1052 bsdtar_warnc(bsdtar
, ENOMEM
,
1053 "No more memory for recording hard links");
1054 bsdtar_warnc(bsdtar
, 0,
1055 "Remaining links will be dumped as full files");
1059 /* Try to locate this entry in the links cache. */
1060 hash
= ( st
->st_dev
^ st
->st_ino
) % links_cache
->number_buckets
;
1061 for (le
= links_cache
->buckets
[hash
]; le
!= NULL
; le
= le
->next
) {
1062 if (le
->dev
== st
->st_dev
&& le
->ino
== st
->st_ino
) {
1063 archive_entry_copy_hardlink(entry
, le
->name
);
1066 * Decrement link count each time and release
1067 * the entry if it hits zero. This saves
1068 * memory and is necessary for proper -l
1071 if (--le
->links
<= 0) {
1072 if (le
->previous
!= NULL
)
1073 le
->previous
->next
= le
->next
;
1074 if (le
->next
!= NULL
)
1075 le
->next
->previous
= le
->previous
;
1076 if (le
->name
!= NULL
)
1078 if (links_cache
->buckets
[hash
] == le
)
1079 links_cache
->buckets
[hash
] = le
->next
;
1080 links_cache
->number_entries
--;
1088 /* Add this entry to the links cache. */
1089 le
= malloc(sizeof(struct links_entry
));
1091 le
->name
= strdup(archive_entry_pathname(entry
));
1092 if ((le
== NULL
) || (le
->name
== NULL
)) {
1093 free_buckets(bsdtar
, links_cache
);
1094 bsdtar_warnc(bsdtar
, ENOMEM
,
1095 "No more memory for recording hard links");
1096 bsdtar_warnc(bsdtar
, 0,
1097 "Remaining hard links will be dumped as full files");
1102 if (links_cache
->buckets
[hash
] != NULL
)
1103 links_cache
->buckets
[hash
]->previous
= le
;
1104 links_cache
->number_entries
++;
1105 le
->next
= links_cache
->buckets
[hash
];
1106 le
->previous
= NULL
;
1107 links_cache
->buckets
[hash
] = le
;
1108 le
->dev
= st
->st_dev
;
1109 le
->ino
= st
->st_ino
;
1110 le
->links
= st
->st_nlink
- 1;
1113 #ifdef HAVE_POSIX_ACL
1114 static void setup_acl(struct bsdtar
*bsdtar
,
1115 struct archive_entry
*entry
, const char *accpath
,
1116 int acl_type
, int archive_entry_acl_type
);
1119 setup_acls(struct bsdtar
*bsdtar
, struct archive_entry
*entry
,
1120 const char *accpath
)
1122 archive_entry_acl_clear(entry
);
1124 setup_acl(bsdtar
, entry
, accpath
,
1125 ACL_TYPE_ACCESS
, ARCHIVE_ENTRY_ACL_TYPE_ACCESS
);
1126 /* Only directories can have default ACLs. */
1127 if (S_ISDIR(archive_entry_mode(entry
)))
1128 setup_acl(bsdtar
, entry
, accpath
,
1129 ACL_TYPE_DEFAULT
, ARCHIVE_ENTRY_ACL_TYPE_DEFAULT
);
1133 setup_acl(struct bsdtar
*bsdtar
, struct archive_entry
*entry
,
1134 const char *accpath
, int acl_type
, int archive_entry_acl_type
)
1138 acl_entry_t acl_entry
;
1139 acl_permset_t acl_permset
;
1140 int s
, ae_id
, ae_tag
, ae_perm
;
1141 const char *ae_name
;
1143 /* Retrieve access ACL from file. */
1144 acl
= acl_get_file(accpath
, acl_type
);
1146 s
= acl_get_entry(acl
, ACL_FIRST_ENTRY
, &acl_entry
);
1151 acl_get_tag_type(acl_entry
, &acl_tag
);
1152 if (acl_tag
== ACL_USER
) {
1153 ae_id
= (int)*(uid_t
*)acl_get_qualifier(acl_entry
);
1154 ae_name
= lookup_uname(bsdtar
, ae_id
);
1155 ae_tag
= ARCHIVE_ENTRY_ACL_USER
;
1156 } else if (acl_tag
== ACL_GROUP
) {
1157 ae_id
= (int)*(gid_t
*)acl_get_qualifier(acl_entry
);
1158 ae_name
= lookup_gname(bsdtar
, ae_id
);
1159 ae_tag
= ARCHIVE_ENTRY_ACL_GROUP
;
1160 } else if (acl_tag
== ACL_MASK
) {
1161 ae_tag
= ARCHIVE_ENTRY_ACL_MASK
;
1162 } else if (acl_tag
== ACL_USER_OBJ
) {
1163 ae_tag
= ARCHIVE_ENTRY_ACL_USER_OBJ
;
1164 } else if (acl_tag
== ACL_GROUP_OBJ
) {
1165 ae_tag
= ARCHIVE_ENTRY_ACL_GROUP_OBJ
;
1166 } else if (acl_tag
== ACL_OTHER
) {
1167 ae_tag
= ARCHIVE_ENTRY_ACL_OTHER
;
1169 /* Skip types that libarchive can't support. */
1173 acl_get_permset(acl_entry
, &acl_permset
);
1176 * acl_get_perm() is spelled differently on different
1177 * platforms; see bsdtar_platform.h for details.
1179 if (ACL_GET_PERM(acl_permset
, ACL_EXECUTE
))
1180 ae_perm
|= ARCHIVE_ENTRY_ACL_EXECUTE
;
1181 if (ACL_GET_PERM(acl_permset
, ACL_READ
))
1182 ae_perm
|= ARCHIVE_ENTRY_ACL_READ
;
1183 if (ACL_GET_PERM(acl_permset
, ACL_WRITE
))
1184 ae_perm
|= ARCHIVE_ENTRY_ACL_WRITE
;
1186 archive_entry_acl_add_entry(entry
,
1187 archive_entry_acl_type
, ae_perm
, ae_tag
,
1190 s
= acl_get_entry(acl
, ACL_NEXT_ENTRY
, &acl_entry
);
1197 setup_acls(struct bsdtar
*bsdtar
, struct archive_entry
*entry
,
1198 const char *accpath
)
1206 #if HAVE_LISTXATTR && HAVE_LLISTXATTR && HAVE_GETXATTR && HAVE_LGETXATTR
1209 setup_xattr(struct bsdtar
*bsdtar
, struct archive_entry
*entry
,
1210 const char *accpath
, const char *name
)
1214 char symlink_mode
= bsdtar
->symlink_mode
;
1216 if (symlink_mode
== 'H')
1217 size
= getxattr(accpath
, name
, NULL
, 0);
1219 size
= lgetxattr(accpath
, name
, NULL
, 0);
1222 bsdtar_warnc(bsdtar
, errno
, "Couldn't get extended attribute");
1226 if (size
> 0 && (value
= malloc(size
)) == NULL
) {
1227 bsdtar_errc(bsdtar
, 1, errno
, "Out of memory");
1231 if (symlink_mode
== 'H')
1232 size
= getxattr(accpath
, name
, value
, size
);
1234 size
= lgetxattr(accpath
, name
, value
, size
);
1237 bsdtar_warnc(bsdtar
, errno
, "Couldn't get extended attribute");
1241 archive_entry_xattr_add_entry(entry
, name
, value
, size
);
1247 * Linux extended attribute support
1250 setup_xattrs(struct bsdtar
*bsdtar
, struct archive_entry
*entry
,
1251 const char *accpath
)
1255 char symlink_mode
= bsdtar
->symlink_mode
;
1257 if (symlink_mode
== 'H')
1258 list_size
= listxattr(accpath
, NULL
, 0);
1260 list_size
= llistxattr(accpath
, NULL
, 0);
1262 if (list_size
== -1) {
1263 bsdtar_warnc(bsdtar
, errno
,
1264 "Couldn't list extended attributes");
1266 } else if (list_size
== 0)
1269 if ((list
= malloc(list_size
)) == NULL
) {
1270 bsdtar_errc(bsdtar
, 1, errno
, "Out of memory");
1274 if (symlink_mode
== 'H')
1275 list_size
= listxattr(accpath
, list
, list_size
);
1277 list_size
= llistxattr(accpath
, list
, list_size
);
1279 if (list_size
== -1) {
1280 bsdtar_warnc(bsdtar
, errno
,
1281 "Couldn't list extended attributes");
1286 for (p
= list
; (p
- list
) < list_size
; p
+= strlen(p
) + 1) {
1287 if (strncmp(p
, "system.", 7) == 0 ||
1288 strncmp(p
, "xfsroot.", 8) == 0)
1291 setup_xattr(bsdtar
, entry
, accpath
, p
);
1300 * Generic (stub) extended attribute support.
1303 setup_xattrs(struct bsdtar
*bsdtar
, struct archive_entry
*entry
,
1304 const char *accpath
)
1306 (void)bsdtar
; /* UNUSED */
1307 (void)entry
; /* UNUSED */
1308 (void)accpath
; /* UNUSED */
1314 free_cache(struct name_cache
*cache
)
1318 if (cache
!= NULL
) {
1319 for (i
= 0; i
< cache
->size
; i
++) {
1320 if (cache
->cache
[i
].name
!= NULL
&&
1321 cache
->cache
[i
].name
!= NO_NAME
)
1322 free((void *)(uintptr_t)cache
->cache
[i
].name
);
1329 * Lookup uid/gid from uname/gname, return NULL if no match.
1332 lookup_name(struct bsdtar
*bsdtar
, struct name_cache
**name_cache_variable
,
1333 int (*lookup_fn
)(struct bsdtar
*, const char **, id_t
), id_t id
)
1335 struct name_cache
*cache
;
1340 if (*name_cache_variable
== NULL
) {
1341 *name_cache_variable
= malloc(sizeof(struct name_cache
));
1342 if (*name_cache_variable
== NULL
)
1343 bsdtar_errc(bsdtar
, 1, ENOMEM
, "No more memory");
1344 memset(*name_cache_variable
, 0, sizeof(struct name_cache
));
1345 (*name_cache_variable
)->size
= name_cache_size
;
1348 cache
= *name_cache_variable
;
1351 slot
= id
% cache
->size
;
1352 if (cache
->cache
[slot
].name
!= NULL
) {
1353 if (cache
->cache
[slot
].id
== id
) {
1355 if (cache
->cache
[slot
].name
== NO_NAME
)
1357 return (cache
->cache
[slot
].name
);
1359 if (cache
->cache
[slot
].name
!= NO_NAME
)
1360 free((void *)(uintptr_t)cache
->cache
[slot
].name
);
1361 cache
->cache
[slot
].name
= NULL
;
1364 if (lookup_fn(bsdtar
, &name
, id
) == 0) {
1365 if (name
== NULL
|| name
[0] == '\0') {
1366 /* Cache the negative response. */
1367 cache
->cache
[slot
].name
= NO_NAME
;
1368 cache
->cache
[slot
].id
= id
;
1370 cache
->cache
[slot
].name
= strdup(name
);
1371 if (cache
->cache
[slot
].name
!= NULL
) {
1372 cache
->cache
[slot
].id
= id
;
1373 return (cache
->cache
[slot
].name
);
1376 * Conveniently, NULL marks an empty slot, so
1377 * if the strdup() fails, we've just failed to
1378 * cache it. No recovery necessary.
1386 lookup_uname(struct bsdtar
*bsdtar
, uid_t uid
)
1388 return (lookup_name(bsdtar
, &bsdtar
->uname_cache
,
1389 &lookup_uname_helper
, (id_t
)uid
));
1393 lookup_uname_helper(struct bsdtar
*bsdtar
, const char **name
, id_t id
)
1395 struct passwd
*pwent
;
1397 (void)bsdtar
; /* UNUSED */
1400 pwent
= getpwuid((uid_t
)id
);
1401 if (pwent
== NULL
) {
1404 bsdtar_warnc(bsdtar
, errno
, "getpwuid(%d) failed", id
);
1408 *name
= pwent
->pw_name
;
1413 lookup_gname(struct bsdtar
*bsdtar
, gid_t gid
)
1415 return (lookup_name(bsdtar
, &bsdtar
->gname_cache
,
1416 &lookup_gname_helper
, (id_t
)gid
));
1420 lookup_gname_helper(struct bsdtar
*bsdtar
, const char **name
, id_t id
)
1422 struct group
*grent
;
1424 (void)bsdtar
; /* UNUSED */
1427 grent
= getgrgid((gid_t
)id
);
1428 if (grent
== NULL
) {
1431 bsdtar_warnc(bsdtar
, errno
, "getgrgid(%d) failed", id
);
1435 *name
= grent
->gr_name
;
1440 * Test if the specified file is new enough to include in the archive.
1443 new_enough(struct bsdtar
*bsdtar
, const char *path
, const struct stat
*st
)
1445 struct archive_dir_entry
*p
;
1448 * If this file/dir is excluded by a time comparison, skip it.
1450 if (bsdtar
->newer_ctime_sec
> 0) {
1451 if (st
->st_ctime
< bsdtar
->newer_ctime_sec
)
1452 return (0); /* Too old, skip it. */
1453 if (st
->st_ctime
== bsdtar
->newer_ctime_sec
1454 && ARCHIVE_STAT_CTIME_NANOS(st
)
1455 <= bsdtar
->newer_ctime_nsec
)
1456 return (0); /* Too old, skip it. */
1458 if (bsdtar
->newer_mtime_sec
> 0) {
1459 if (st
->st_mtime
< bsdtar
->newer_mtime_sec
)
1460 return (0); /* Too old, skip it. */
1461 if (st
->st_mtime
== bsdtar
->newer_mtime_sec
1462 && ARCHIVE_STAT_MTIME_NANOS(st
)
1463 <= bsdtar
->newer_mtime_nsec
)
1464 return (0); /* Too old, skip it. */
1468 * In -u mode, we only write an entry if it's newer than
1469 * what was already in the archive.
1471 if (bsdtar
->archive_dir
!= NULL
&&
1472 bsdtar
->archive_dir
->head
!= NULL
) {
1473 for (p
= bsdtar
->archive_dir
->head
; p
!= NULL
; p
= p
->next
) {
1474 if (pathcmp(path
, p
->name
)==0)
1475 return (p
->mtime_sec
< st
->st_mtime
||
1476 (p
->mtime_sec
== st
->st_mtime
&&
1478 < ARCHIVE_STAT_MTIME_NANOS(st
)));
1482 /* If the file wasn't rejected, include it. */
1487 * Add an entry to the dir list for 'u' mode.
1489 * XXX TODO: Make this fast.
1492 add_dir_list(struct bsdtar
*bsdtar
, const char *path
,
1493 time_t mtime_sec
, int mtime_nsec
)
1495 struct archive_dir_entry
*p
;
1498 * Search entire list to see if this file has appeared before.
1499 * If it has, override the timestamp data.
1501 p
= bsdtar
->archive_dir
->head
;
1503 if (strcmp(path
, p
->name
)==0) {
1504 p
->mtime_sec
= mtime_sec
;
1505 p
->mtime_nsec
= mtime_nsec
;
1511 p
= malloc(sizeof(*p
));
1513 bsdtar_errc(bsdtar
, 1, ENOMEM
, "Can't read archive directory");
1515 p
->name
= strdup(path
);
1516 if (p
->name
== NULL
)
1517 bsdtar_errc(bsdtar
, 1, ENOMEM
, "Can't read archive directory");
1518 p
->mtime_sec
= mtime_sec
;
1519 p
->mtime_nsec
= mtime_nsec
;
1521 if (bsdtar
->archive_dir
->tail
== NULL
) {
1522 bsdtar
->archive_dir
->head
= bsdtar
->archive_dir
->tail
= p
;
1524 bsdtar
->archive_dir
->tail
->next
= p
;
1525 bsdtar
->archive_dir
->tail
= p
;
1530 test_for_append(struct bsdtar
*bsdtar
)
1534 if (*bsdtar
->argv
== NULL
)
1535 bsdtar_errc(bsdtar
, 1, 0, "no files or directories specified");
1536 if (bsdtar
->filename
== NULL
)
1537 bsdtar_errc(bsdtar
, 1, 0, "Cannot append to stdout.");
1539 if (bsdtar
->create_compression
!= 0)
1540 bsdtar_errc(bsdtar
, 1, 0,
1541 "Cannot append to %s with compression", bsdtar
->filename
);
1543 if (stat(bsdtar
->filename
, &s
) != 0)
1546 if (!S_ISREG(s
.st_mode
))
1547 bsdtar_errc(bsdtar
, 1, 0,
1548 "Cannot append to %s: not a regular file.",