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 * 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.
28 #include "cpio_platform.h"
29 __FBSDID("$FreeBSD: src/usr.bin/cpio/cpio.c,v 1.15 2008/12/06 07:30:40 kientzle Exp $");
31 #include <sys/types.h>
33 #include <archive_entry.h>
35 #ifdef HAVE_SYS_MKDEV_H
36 #include <sys/mkdev.h>
38 #ifdef HAVE_SYS_STAT_H
41 #ifdef HAVE_SYS_TIME_H
69 #ifdef HAVE_SYS_TIME_H
79 /* Fixed size of uname/gname caches. */
80 #define name_cache_size 101
89 } cache
[name_cache_size
];
92 static int copy_data(struct archive
*, struct archive
*);
93 static const char *cpio_rename(const char *name
);
94 static int entry_to_archive(struct cpio
*, struct archive_entry
*);
95 static int file_to_archive(struct cpio
*, const char *);
96 static void free_cache(struct name_cache
*cache
);
97 static void list_item_verbose(struct cpio
*, struct archive_entry
*);
98 static void long_help(void);
99 static const char *lookup_gname(struct cpio
*, gid_t gid
);
100 static int lookup_gname_helper(struct cpio
*,
101 const char **name
, id_t gid
);
102 static const char *lookup_uname(struct cpio
*, uid_t uid
);
103 static int lookup_uname_helper(struct cpio
*,
104 const char **name
, id_t uid
);
105 static void mode_in(struct cpio
*);
106 static void mode_list(struct cpio
*);
107 static void mode_out(struct cpio
*);
108 static void mode_pass(struct cpio
*, const char *);
109 static int restore_time(struct cpio
*, struct archive_entry
*,
110 const char *, int fd
);
111 static void usage(void);
112 static void version(void);
115 main(int argc
, char *argv
[])
117 static char buff
[16384];
118 struct cpio _cpio
; /* Allocated on stack. */
124 memset(cpio
, 0, sizeof(*cpio
));
126 cpio
->buff_size
= sizeof(buff
);
127 #if defined(_WIN32) && !defined(__CYGWIN__)
128 /* Make sure open() function will be used with a binary mode. */
129 /* on cygwin, we need something similar, but instead link against */
130 /* a special startup object, binmode.o */
131 _set_fmode(_O_BINARY
);
134 /* Need cpio_progname before calling cpio_warnc. */
136 cpio_progname
= "bsdcpio";
138 #if defined(_WIN32) && !defined(__CYGWIN__)
139 cpio_progname
= strrchr(*argv
, '\\');
141 cpio_progname
= strrchr(*argv
, '/');
143 if (cpio_progname
!= NULL
)
146 cpio_progname
= *argv
;
149 cpio
->uid_override
= -1;
150 cpio
->gid_override
= -1;
153 cpio
->line_separator
= '\n';
156 cpio
->compress
= '\0';
157 cpio
->extract_flags
= ARCHIVE_EXTRACT_NO_AUTODIR
;
158 cpio
->extract_flags
|= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER
;
159 cpio
->extract_flags
|= ARCHIVE_EXTRACT_SECURE_SYMLINKS
;
160 cpio
->extract_flags
|= ARCHIVE_EXTRACT_SECURE_NODOTDOT
;
161 cpio
->extract_flags
|= ARCHIVE_EXTRACT_PERM
;
162 cpio
->extract_flags
|= ARCHIVE_EXTRACT_FFLAGS
;
163 cpio
->extract_flags
|= ARCHIVE_EXTRACT_ACL
;
164 #if defined(_WIN32) || defined(__CYGWIN__)
165 if (bsdcpio_is_privileged())
169 cpio
->extract_flags
|= ARCHIVE_EXTRACT_OWNER
;
170 cpio
->bytes_per_block
= 512;
171 cpio
->filename
= NULL
;
173 while ((opt
= cpio_getopt(cpio
)) != -1) {
175 case '0': /* GNU convention: --null, -0 */
176 cpio
->line_separator
= '\0';
178 case 'A': /* NetBSD/OpenBSD */
179 cpio
->option_append
= 1;
181 case 'a': /* POSIX 1997 */
182 cpio
->option_atime_restore
= 1;
184 case 'B': /* POSIX 1997 */
185 cpio
->bytes_per_block
= 5120;
187 case 'C': /* NetBSD/OpenBSD */
188 cpio
->bytes_per_block
= atoi(cpio
->optarg
);
189 if (cpio
->bytes_per_block
<= 0)
190 cpio_errc(1, 0, "Invalid blocksize %s", cpio
->optarg
);
192 case 'c': /* POSIX 1997 */
193 cpio
->format
= "odc";
195 case 'd': /* POSIX 1997 */
196 cpio
->extract_flags
&= ~ARCHIVE_EXTRACT_NO_AUTODIR
;
198 case 'E': /* NetBSD/OpenBSD */
199 include_from_file(cpio
, cpio
->optarg
);
201 case 'F': /* NetBSD/OpenBSD/GNU cpio */
202 cpio
->filename
= cpio
->optarg
;
204 case 'f': /* POSIX 1997 */
205 exclude(cpio
, cpio
->optarg
);
207 case 'H': /* GNU cpio (also --format) */
208 cpio
->format
= cpio
->optarg
;
213 case 'I': /* NetBSD/OpenBSD */
214 cpio
->filename
= cpio
->optarg
;
216 case 'i': /* POSIX 1997 */
217 if (cpio
->mode
!= '\0')
219 "Cannot use both -i and -%c", cpio
->mode
);
222 case OPTION_INSECURE
:
223 cpio
->extract_flags
&= ~ARCHIVE_EXTRACT_SECURE_SYMLINKS
;
224 cpio
->extract_flags
&= ~ARCHIVE_EXTRACT_SECURE_NODOTDOT
;
226 case 'L': /* GNU cpio */
227 cpio
->option_follow_links
= 1;
229 case 'l': /* POSIX 1997 */
230 cpio
->option_link
= 1;
232 case 'm': /* POSIX 1997 */
233 cpio
->extract_flags
|= ARCHIVE_EXTRACT_TIME
;
235 case 'n': /* GNU cpio */
236 cpio
->option_numeric_uid_gid
= 1;
238 case OPTION_NO_PRESERVE_OWNER
: /* GNU cpio */
239 cpio
->extract_flags
&= ~ARCHIVE_EXTRACT_OWNER
;
241 case 'O': /* GNU cpio */
242 cpio
->filename
= cpio
->optarg
;
244 case 'o': /* POSIX 1997 */
245 if (cpio
->mode
!= '\0')
247 "Cannot use both -o and -%c", cpio
->mode
);
250 case 'p': /* POSIX 1997 */
251 if (cpio
->mode
!= '\0')
253 "Cannot use both -p and -%c", cpio
->mode
);
255 cpio
->extract_flags
&= ~ARCHIVE_EXTRACT_SECURE_NODOTDOT
;
257 case OPTION_QUIET
: /* GNU cpio */
260 case 'R': /* GNU cpio, also --owner */
261 if (owner_parse(cpio
->optarg
, &uid
, &gid
))
264 cpio
->uid_override
= uid
;
266 cpio
->gid_override
= gid
;
268 case 'r': /* POSIX 1997 */
269 cpio
->option_rename
= 1;
271 case 't': /* POSIX 1997 */
272 cpio
->option_list
= 1;
274 case 'u': /* POSIX 1997 */
276 &= ~ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER
;
278 case 'v': /* POSIX 1997 */
281 case OPTION_VERSION
: /* GNU convention */
286 * cpio_getopt() handles -W specially, so it's not
289 case 'W': /* Obscure, but useful GNU convention. */
292 case 'y': /* tar convention */
294 cpio
->compress
= opt
;
296 cpio_warnc(0, "bzip2 compression not supported by "
297 "this version of bsdcpio");
300 case 'Z': /* tar convention */
301 cpio
->compress
= opt
;
303 case 'z': /* tar convention */
305 cpio
->compress
= opt
;
307 cpio_warnc(0, "gzip compression not supported by "
308 "this version of bsdcpio");
317 * Sanity-check args, error out on nonsensical combinations.
319 /* -t implies -i if no mode was specified. */
320 if (cpio
->option_list
&& cpio
->mode
== '\0')
323 if (cpio
->option_list
&& cpio
->mode
!= 'i')
324 cpio_errc(1, 0, "Option -t requires -i", cpio
->mode
);
325 /* -n requires -it */
326 if (cpio
->option_numeric_uid_gid
&& !cpio
->option_list
)
327 cpio_errc(1, 0, "Option -n requires -it");
328 /* Can only specify format when writing */
329 if (cpio
->format
!= NULL
&& cpio
->mode
!= 'o')
330 cpio_errc(1, 0, "Option --format requires -o");
332 if (cpio
->option_link
&& cpio
->mode
!= 'p')
333 cpio_errc(1, 0, "Option -l requires -p");
334 /* TODO: Flag other nonsensical combinations. */
336 switch (cpio
->mode
) {
338 /* TODO: Implement old binary format in libarchive,
340 if (cpio
->format
== NULL
)
341 cpio
->format
= "odc"; /* Default format */
346 while (*cpio
->argv
!= NULL
) {
347 include(cpio
, *cpio
->argv
);
351 if (cpio
->option_list
)
357 if (*cpio
->argv
== NULL
|| **cpio
->argv
== '\0')
359 "-p mode requires a target directory");
360 mode_pass(cpio
, *cpio
->argv
);
364 "Must specify at least one of -i, -o, or -p");
367 free_cache(cpio
->gname_cache
);
368 free_cache(cpio
->uname_cache
);
379 fprintf(stderr
, "Brief Usage:\n");
380 fprintf(stderr
, " List: %s -it < archive\n", p
);
381 fprintf(stderr
, " Extract: %s -i < archive\n", p
);
382 fprintf(stderr
, " Create: %s -o < filenames > archive\n", p
);
383 fprintf(stderr
, " Help: %s --help\n", p
);
387 static const char *long_help_msg
=
388 "First option must be a mode specifier:\n"
389 " -i Input -o Output -p Pass\n"
392 "Create: %p -o [options] < [list of files] > [archive]\n"
394 " -y Compress archive with bzip2\n"
397 " -z Compress archive with gzip\n"
399 " --format {odc|newc|ustar} Select archive format\n"
400 "List: %p -it < [archive]\n"
401 "Extract: %p -i [options] < [archive]\n";
405 * Note that the word 'bsdcpio' will always appear in the first line
408 * In particular, /bin/sh scripts that need to test for the presence
409 * of bsdcpio can use the following template:
411 * if (cpio --help 2>&1 | grep bsdcpio >/dev/null 2>&1 ) then \
412 * echo bsdcpio; else echo not bsdcpio; fi
420 prog
= cpio_progname
;
424 p
= (strcmp(prog
,"bsdcpio") != 0) ? "(bsdcpio)" : "";
425 printf("%s%s: manipulate archive files\n", prog
, p
);
427 for (p
= long_help_msg
; *p
!= '\0'; p
++) {
443 fprintf(stdout
,"bsdcpio %s -- %s\n",
444 BSDCPIO_VERSION_STRING
,
450 mode_out(struct cpio
*cpio
)
452 unsigned long blocks
;
453 struct archive_entry
*entry
, *spare
;
454 struct line_reader
*lr
;
458 if (cpio
->option_append
)
459 cpio_errc(1, 0, "Append mode not yet supported.");
460 cpio
->archive
= archive_write_new();
461 if (cpio
->archive
== NULL
)
462 cpio_errc(1, 0, "Failed to allocate archive object");
463 switch (cpio
->compress
) {
466 archive_write_set_compression_bzip2(cpio
->archive
);
471 archive_write_set_compression_gzip(cpio
->archive
);
475 archive_write_set_compression_compress(cpio
->archive
);
478 archive_write_set_compression_none(cpio
->archive
);
481 r
= archive_write_set_format_by_name(cpio
->archive
, cpio
->format
);
483 cpio_errc(1, 0, archive_error_string(cpio
->archive
));
484 archive_write_set_bytes_per_block(cpio
->archive
, cpio
->bytes_per_block
);
485 cpio
->linkresolver
= archive_entry_linkresolver_new();
486 archive_entry_linkresolver_set_strategy(cpio
->linkresolver
,
487 archive_format(cpio
->archive
));
489 r
= archive_write_open_file(cpio
->archive
, cpio
->filename
);
491 cpio_errc(1, 0, archive_error_string(cpio
->archive
));
492 lr
= process_lines_init("-", cpio
->line_separator
);
493 while ((p
= process_lines_next(lr
)) != NULL
)
494 file_to_archive(cpio
, p
);
495 process_lines_free(lr
);
498 * The hardlink detection may have queued up a couple of entries
499 * that can now be flushed.
502 archive_entry_linkify(cpio
->linkresolver
, &entry
, &spare
);
503 while (entry
!= NULL
) {
504 entry_to_archive(cpio
, entry
);
505 archive_entry_free(entry
);
507 archive_entry_linkify(cpio
->linkresolver
, &entry
, &spare
);
510 r
= archive_write_close(cpio
->archive
);
512 cpio_errc(1, 0, archive_error_string(cpio
->archive
));
515 blocks
= (archive_position_uncompressed(cpio
->archive
) + 511)
517 fprintf(stderr
, "%lu %s\n", blocks
,
518 blocks
== 1 ? "block" : "blocks");
520 archive_write_finish(cpio
->archive
);
524 * This is used by both out mode (to copy objects from disk into
525 * an archive) and pass mode (to copy objects from disk to
526 * an archive_write_disk "archive").
529 file_to_archive(struct cpio
*cpio
, const char *srcpath
)
532 const char *destpath
;
533 struct archive_entry
*entry
, *spare
;
536 #if !defined(_WIN32) || defined(__CYGWIN__)
542 * Create an archive_entry describing the source file.
544 * XXX TODO: rework to use archive_read_disk_entry_from_file()
546 entry
= archive_entry_new();
548 cpio_errc(1, 0, "Couldn't allocate entry");
549 archive_entry_copy_sourcepath(entry
, srcpath
);
551 /* Get stat information. */
552 if (cpio
->option_follow_links
)
553 r
= stat(srcpath
, &st
);
555 r
= lstat(srcpath
, &st
);
557 cpio_warnc(errno
, "Couldn't stat \"%s\"", srcpath
);
558 archive_entry_free(entry
);
562 if (cpio
->uid_override
>= 0)
563 st
.st_uid
= cpio
->uid_override
;
564 if (cpio
->gid_override
>= 0)
565 st
.st_gid
= cpio
->uid_override
;
566 archive_entry_copy_stat(entry
, &st
);
568 #if !defined(_WIN32) || defined(__CYGWIN__)
569 /* If its a symlink, pull the target. */
570 if (S_ISLNK(st
.st_mode
)) {
571 lnklen
= readlink(srcpath
, cpio
->buff
, cpio
->buff_size
);
574 "%s: Couldn't read symbolic link", srcpath
);
575 archive_entry_free(entry
);
578 cpio
->buff
[lnklen
] = 0;
579 archive_entry_set_symlink(entry
, cpio
->buff
);
584 * Generate a destination path for this entry.
585 * "destination path" is the name to which it will be copied in
586 * pass mode or the name that will go into the archive in
591 len
= strlen(cpio
->destdir
) + strlen(srcpath
) + 8;
592 if (len
>= cpio
->pass_destpath_alloc
) {
593 while (len
>= cpio
->pass_destpath_alloc
) {
594 cpio
->pass_destpath_alloc
+= 512;
595 cpio
->pass_destpath_alloc
*= 2;
597 free(cpio
->pass_destpath
);
598 cpio
->pass_destpath
= malloc(cpio
->pass_destpath_alloc
);
599 if (cpio
->pass_destpath
== NULL
)
601 "Can't allocate path buffer");
603 strcpy(cpio
->pass_destpath
, cpio
->destdir
);
607 strcat(cpio
->pass_destpath
, p
);
608 destpath
= cpio
->pass_destpath
;
610 if (cpio
->option_rename
)
611 destpath
= cpio_rename(destpath
);
612 if (destpath
== NULL
)
614 archive_entry_copy_pathname(entry
, destpath
);
617 * If we're trying to preserve hardlinks, match them here.
620 if (cpio
->linkresolver
!= NULL
621 && !S_ISDIR(st
.st_mode
)) {
622 archive_entry_linkify(cpio
->linkresolver
, &entry
, &spare
);
626 r
= entry_to_archive(cpio
, entry
);
627 archive_entry_free(entry
);
631 r
= entry_to_archive(cpio
, spare
);
632 archive_entry_free(spare
);
638 entry_to_archive(struct cpio
*cpio
, struct archive_entry
*entry
)
640 const char *destpath
= archive_entry_pathname(entry
);
641 const char *srcpath
= archive_entry_sourcepath(entry
);
646 /* Print out the destination name to the user. */
648 fprintf(stderr
,"%s", destpath
);
651 * Option_link only makes sense in pass mode and for
652 * regular files. Also note: if a link operation fails
653 * because of cross-device restrictions, we'll fall back
654 * to copy mode for that entry.
656 * TODO: Test other cpio implementations to see if they
657 * hard-link anything other than regular files here.
659 if (cpio
->option_link
660 && archive_entry_filetype(entry
) == AE_IFREG
)
662 struct archive_entry
*t
;
663 /* Save the original entry in case we need it later. */
664 t
= archive_entry_clone(entry
);
666 cpio_errc(1, ENOMEM
, "Can't create link");
667 /* Note: link(2) doesn't create parent directories,
668 * so we use archive_write_header() instead as a
670 archive_entry_set_hardlink(t
, srcpath
);
671 /* This is a straight link that carries no data. */
672 archive_entry_set_size(t
, 0);
673 r
= archive_write_header(cpio
->archive
, t
);
674 archive_entry_free(t
);
676 cpio_warnc(archive_errno(cpio
->archive
),
677 archive_error_string(cpio
->archive
));
678 if (r
== ARCHIVE_FATAL
)
681 if (r
!= ARCHIVE_OK
&& archive_errno(cpio
->archive
) == EXDEV
) {
682 /* Cross-device link: Just fall through and use
683 * the original entry to copy the file over. */
684 cpio_warnc(0, "Copying file instead");
691 * Make sure we can open the file (if necessary) before
692 * trying to write the header.
694 if (archive_entry_filetype(entry
) == AE_IFREG
) {
695 if (archive_entry_size(entry
) > 0) {
696 fd
= open(srcpath
, O_RDONLY
);
699 "%s: could not open file", srcpath
);
704 archive_entry_set_size(entry
, 0);
707 r
= archive_write_header(cpio
->archive
, entry
);
710 cpio_warnc(archive_errno(cpio
->archive
),
713 archive_error_string(cpio
->archive
));
715 if (r
== ARCHIVE_FATAL
)
718 if (r
>= ARCHIVE_WARN
&& fd
>= 0) {
719 bytes_read
= read(fd
, cpio
->buff
, cpio
->buff_size
);
720 while (bytes_read
> 0) {
721 r
= archive_write_data(cpio
->archive
,
722 cpio
->buff
, bytes_read
);
724 cpio_errc(1, archive_errno(cpio
->archive
),
725 archive_error_string(cpio
->archive
));
726 if (r
< bytes_read
) {
728 "Truncated write; file may have grown while being archived.");
730 bytes_read
= read(fd
, cpio
->buff
, cpio
->buff_size
);
734 fd
= restore_time(cpio
, entry
, srcpath
, fd
);
738 fprintf(stderr
,"\n");
745 restore_time(struct cpio
*cpio
, struct archive_entry
*entry
,
746 const char *name
, int fd
)
749 static int warned
= 0;
751 (void)cpio
; /* UNUSED */
752 (void)entry
; /* UNUSED */
753 (void)name
; /* UNUSED */
756 cpio_warnc(0, "Can't restore access times on this platform");
760 #if defined(_WIN32) && !defined(__CYGWIN__)
761 struct __timeval times
[2];
763 struct timeval times
[2];
766 if (!cpio
->option_atime_restore
)
769 times
[1].tv_sec
= archive_entry_mtime(entry
);
770 times
[1].tv_usec
= archive_entry_mtime_nsec(entry
) / 1000;
772 times
[0].tv_sec
= archive_entry_atime(entry
);
773 times
[0].tv_usec
= archive_entry_atime_nsec(entry
) / 1000;
776 if (fd
>= 0 && futimes(fd
, times
) == 0)
780 * Some platform cannot restore access times if the file descriptor
789 if (lutimes(name
, times
) != 0)
791 if (!S_ISLNK(archive_entry_mode(entry
)) && utimes(name
, times
) != 0)
793 cpio_warnc(errno
, "Can't update time for %s", name
);
800 mode_in(struct cpio
*cpio
)
803 struct archive_entry
*entry
;
805 const char *destpath
;
806 unsigned long blocks
;
809 ext
= archive_write_disk_new();
811 cpio_errc(1, 0, "Couldn't allocate restore object");
812 r
= archive_write_disk_set_options(ext
, cpio
->extract_flags
);
814 cpio_errc(1, 0, archive_error_string(ext
));
815 a
= archive_read_new();
817 cpio_errc(1, 0, "Couldn't allocate archive object");
818 archive_read_support_compression_all(a
);
819 archive_read_support_format_all(a
);
821 if (archive_read_open_file(a
, cpio
->filename
, cpio
->bytes_per_block
))
822 cpio_errc(1, archive_errno(a
),
823 archive_error_string(a
));
825 r
= archive_read_next_header(a
, &entry
);
826 if (r
== ARCHIVE_EOF
)
828 if (r
!= ARCHIVE_OK
) {
829 cpio_errc(1, archive_errno(a
),
830 archive_error_string(a
));
832 if (excluded(cpio
, archive_entry_pathname(entry
)))
834 if (cpio
->option_rename
) {
835 destpath
= cpio_rename(archive_entry_pathname(entry
));
836 archive_entry_set_pathname(entry
, destpath
);
838 destpath
= archive_entry_pathname(entry
);
839 if (destpath
== NULL
)
842 fprintf(stdout
, "%s\n", destpath
);
843 if (cpio
->uid_override
>= 0)
844 archive_entry_set_uid(entry
, cpio
->uid_override
);
845 if (cpio
->gid_override
>= 0)
846 archive_entry_set_gid(entry
, cpio
->gid_override
);
847 r
= archive_write_header(ext
, entry
);
848 if (r
!= ARCHIVE_OK
) {
849 fprintf(stderr
, "%s: %s\n",
850 archive_entry_pathname(entry
),
851 archive_error_string(ext
));
852 } else if (archive_entry_size(entry
) > 0) {
853 r
= copy_data(a
, ext
);
856 r
= archive_read_close(a
);
858 cpio_errc(1, 0, archive_error_string(a
));
859 r
= archive_write_close(ext
);
861 cpio_errc(1, 0, archive_error_string(ext
));
863 blocks
= (archive_position_uncompressed(a
) + 511)
865 fprintf(stderr
, "%lu %s\n", blocks
,
866 blocks
== 1 ? "block" : "blocks");
868 archive_read_finish(a
);
869 archive_write_finish(ext
);
874 copy_data(struct archive
*ar
, struct archive
*aw
)
882 r
= archive_read_data_block(ar
, &block
, &size
, &offset
);
883 if (r
== ARCHIVE_EOF
)
885 if (r
!= ARCHIVE_OK
) {
886 cpio_warnc(archive_errno(ar
),
887 "%s", archive_error_string(ar
));
890 r
= archive_write_data_block(aw
, block
, size
, offset
);
891 if (r
!= ARCHIVE_OK
) {
892 cpio_warnc(archive_errno(aw
),
893 archive_error_string(aw
));
900 mode_list(struct cpio
*cpio
)
903 struct archive_entry
*entry
;
904 unsigned long blocks
;
907 a
= archive_read_new();
909 cpio_errc(1, 0, "Couldn't allocate archive object");
910 archive_read_support_compression_all(a
);
911 archive_read_support_format_all(a
);
913 if (archive_read_open_file(a
, cpio
->filename
, cpio
->bytes_per_block
))
914 cpio_errc(1, archive_errno(a
),
915 archive_error_string(a
));
917 r
= archive_read_next_header(a
, &entry
);
918 if (r
== ARCHIVE_EOF
)
920 if (r
!= ARCHIVE_OK
) {
921 cpio_errc(1, archive_errno(a
),
922 archive_error_string(a
));
924 if (excluded(cpio
, archive_entry_pathname(entry
)))
927 list_item_verbose(cpio
, entry
);
929 fprintf(stdout
, "%s\n", archive_entry_pathname(entry
));
931 r
= archive_read_close(a
);
933 cpio_errc(1, 0, archive_error_string(a
));
935 blocks
= (archive_position_uncompressed(a
) + 511)
937 fprintf(stderr
, "%lu %s\n", blocks
,
938 blocks
== 1 ? "block" : "blocks");
940 archive_read_finish(a
);
945 * Display information about the current file.
947 * The format here roughly duplicates the output of 'ls -l'.
948 * This is based on SUSv2, where 'tar tv' is documented as
949 * listing additional information in an "unspecified format,"
950 * and 'pax -l' is documented as using the same format as 'ls -l'.
953 list_item_verbose(struct cpio
*cpio
, struct archive_entry
*entry
)
957 char uids
[16], gids
[16];
958 const char *uname
, *gname
;
960 const struct stat
*st
;
965 st
= archive_entry_stat(entry
);
970 if (cpio
->option_numeric_uid_gid
) {
971 /* Format numeric uid/gid for display. */
972 snprintf(uids
, sizeof(uids
), "%jd",
973 (intmax_t)archive_entry_uid(entry
));
975 snprintf(gids
, sizeof(gids
), "%jd",
976 (intmax_t)archive_entry_gid(entry
));
979 /* Use uname if it's present, else lookup name from uid. */
980 uname
= archive_entry_uname(entry
);
982 uname
= lookup_uname(cpio
, archive_entry_uid(entry
));
983 /* Use gname if it's present, else lookup name from gid. */
984 gname
= archive_entry_gname(entry
);
986 gname
= lookup_gname(cpio
, archive_entry_gid(entry
));
989 /* Print device number or file size. */
990 if (S_ISCHR(st
->st_mode
) || S_ISBLK(st
->st_mode
)) {
991 snprintf(size
, sizeof(size
), "%lu,%lu",
992 (unsigned long)major(st
->st_rdev
),
993 (unsigned long)minor(st
->st_rdev
)); /* ls(1) also casts here. */
995 snprintf(size
, sizeof(size
), CPIO_FILESIZE_PRINTF
,
996 (CPIO_FILESIZE_TYPE
)st
->st_size
);
999 /* Format the time using 'ls -l' conventions. */
1000 tim
= (time_t)st
->st_mtime
;
1001 #if defined(_WIN32) && !defined(__CYGWIN__)
1002 /* Windows' strftime function does not support %e format. */
1003 if (abs(tim
- now
) > (365/2)*86400)
1004 fmt
= cpio
->day_first
? "%d %b %Y" : "%b %d %Y";
1006 fmt
= cpio
->day_first
? "%d %b %H:%M" : "%b %d %H:%M";
1008 if (abs(tim
- now
) > (365/2)*86400)
1009 fmt
= cpio
->day_first
? "%e %b %Y" : "%b %e %Y";
1011 fmt
= cpio
->day_first
? "%e %b %H:%M" : "%b %e %H:%M";
1013 strftime(date
, sizeof(date
), fmt
, localtime(&tim
));
1015 fprintf(out
, "%s%3d %-8s %-8s %8s %12s %s",
1016 archive_entry_strmode(entry
),
1017 archive_entry_nlink(entry
),
1018 uname
, gname
, size
, date
,
1019 archive_entry_pathname(entry
));
1021 /* Extra information for links. */
1022 if (archive_entry_hardlink(entry
)) /* Hard link */
1023 fprintf(out
, " link to %s", archive_entry_hardlink(entry
));
1024 else if (archive_entry_symlink(entry
)) /* Symbolic link */
1025 fprintf(out
, " -> %s", archive_entry_symlink(entry
));
1030 mode_pass(struct cpio
*cpio
, const char *destdir
)
1032 unsigned long blocks
;
1033 struct line_reader
*lr
;
1037 /* Ensure target dir has a trailing '/' to simplify path surgery. */
1038 cpio
->destdir
= malloc(strlen(destdir
) + 8);
1039 strcpy(cpio
->destdir
, destdir
);
1040 if (destdir
[strlen(destdir
) - 1] != '/')
1041 strcat(cpio
->destdir
, "/");
1043 cpio
->archive
= archive_write_disk_new();
1044 if (cpio
->archive
== NULL
)
1045 cpio_errc(1, 0, "Failed to allocate archive object");
1046 r
= archive_write_disk_set_options(cpio
->archive
, cpio
->extract_flags
);
1047 if (r
!= ARCHIVE_OK
)
1048 cpio_errc(1, 0, archive_error_string(cpio
->archive
));
1049 cpio
->linkresolver
= archive_entry_linkresolver_new();
1050 archive_write_disk_set_standard_lookup(cpio
->archive
);
1051 lr
= process_lines_init("-", cpio
->line_separator
);
1052 while ((p
= process_lines_next(lr
)) != NULL
)
1053 file_to_archive(cpio
, p
);
1054 process_lines_free(lr
);
1056 archive_entry_linkresolver_free(cpio
->linkresolver
);
1057 r
= archive_write_close(cpio
->archive
);
1058 if (r
!= ARCHIVE_OK
)
1059 cpio_errc(1, 0, archive_error_string(cpio
->archive
));
1062 blocks
= (archive_position_uncompressed(cpio
->archive
) + 511)
1064 fprintf(stderr
, "%lu %s\n", blocks
,
1065 blocks
== 1 ? "block" : "blocks");
1068 archive_write_finish(cpio
->archive
);
1072 * Prompt for a new name for this entry. Returns a pointer to the
1073 * new name or NULL if the entry should not be copied. This
1074 * implements the semantics defined in POSIX.1-1996, which specifies
1075 * that an input of '.' means the name should be unchanged. GNU cpio
1076 * treats '.' as a literal new name.
1079 cpio_rename(const char *name
)
1081 static char buff
[1024];
1085 t
= fopen("/dev/tty", "r+");
1088 fprintf(t
, "%s (Enter/./(new name))? ", name
);
1091 p
= fgets(buff
, sizeof(buff
), t
);
1094 /* End-of-file is a blank line. */
1097 while (*p
== ' ' || *p
== '\t')
1099 if (*p
== '\n' || *p
== '\0')
1102 if (*p
== '.' && p
[1] == '\n')
1103 /* Single period preserves original name. */
1106 /* Trim the final newline. */
1107 while (*p
!= '\0' && *p
!= '\n')
1109 /* Overwrite the final \n with a null character. */
1116 * Read lines from file and do something with each one. If option_null
1117 * is set, lines are terminated with zero bytes; otherwise, they're
1118 * terminated with newlines.
1120 * This uses a self-sizing buffer to handle arbitrarily-long lines.
1122 struct line_reader
{
1124 char *buff
, *buff_end
, *line_start
, *line_end
, *p
;
1131 struct line_reader
*
1132 process_lines_init(const char *pathname
, char separator
)
1134 struct line_reader
*lr
;
1136 lr
= calloc(1, sizeof(*lr
));
1138 cpio_errc(1, ENOMEM
, "Can't open %s", pathname
);
1140 lr
->separator
= separator
;
1141 lr
->pathname
= strdup(pathname
);
1143 if (strcmp(pathname
, "-") == 0)
1146 lr
->f
= fopen(pathname
, "r");
1148 cpio_errc(1, errno
, "Couldn't open %s", pathname
);
1149 lr
->buff_length
= 8192;
1150 lr
->buff
= malloc(lr
->buff_length
);
1151 if (lr
->buff
== NULL
)
1152 cpio_errc(1, ENOMEM
, "Can't read %s", pathname
);
1153 lr
->line_start
= lr
->line_end
= lr
->buff_end
= lr
->buff
;
1159 process_lines_next(struct line_reader
*lr
)
1161 size_t bytes_wanted
, bytes_read
, new_buff_size
;
1162 char *line_start
, *p
;
1165 /* If there's a line in the buffer, return it immediately. */
1166 while (lr
->line_end
< lr
->buff_end
) {
1167 if (*lr
->line_end
== lr
->separator
) {
1168 *lr
->line_end
= '\0';
1169 line_start
= lr
->line_start
;
1170 lr
->line_start
= lr
->line_end
+ 1;
1171 lr
->line_end
= lr
->line_start
;
1172 return (line_start
);
1177 /* If we're at end-of-file, process the final data. */
1178 if (lr
->f
== NULL
) {
1179 /* If there's more text, return one last line. */
1180 if (lr
->line_end
> lr
->line_start
) {
1181 *lr
->line_end
= '\0';
1182 line_start
= lr
->line_start
;
1183 lr
->line_start
= lr
->line_end
+ 1;
1184 lr
->line_end
= lr
->line_start
;
1185 return (line_start
);
1187 /* Otherwise, we're done. */
1191 /* Buffer only has part of a line. */
1192 if (lr
->line_start
> lr
->buff
) {
1193 /* Move a leftover fractional line to the beginning. */
1194 memmove(lr
->buff
, lr
->line_start
,
1195 lr
->buff_end
- lr
->line_start
);
1196 lr
->buff_end
-= lr
->line_start
- lr
->buff
;
1197 lr
->line_end
-= lr
->line_start
- lr
->buff
;
1198 lr
->line_start
= lr
->buff
;
1200 /* Line is too big; enlarge the buffer. */
1201 new_buff_size
= lr
->buff_length
* 2;
1202 if (new_buff_size
<= lr
->buff_length
)
1203 cpio_errc(1, ENOMEM
,
1204 "Line too long in %s", lr
->pathname
);
1205 lr
->buff_length
= new_buff_size
;
1206 p
= realloc(lr
->buff
, new_buff_size
);
1208 cpio_errc(1, ENOMEM
,
1209 "Line too long in %s", lr
->pathname
);
1210 lr
->buff_end
= p
+ (lr
->buff_end
- lr
->buff
);
1211 lr
->line_end
= p
+ (lr
->line_end
- lr
->buff
);
1212 lr
->line_start
= lr
->buff
= p
;
1215 /* Get some more data into the buffer. */
1216 bytes_wanted
= lr
->buff
+ lr
->buff_length
- lr
->buff_end
;
1217 bytes_read
= fread(lr
->buff_end
, 1, bytes_wanted
, lr
->f
);
1218 lr
->buff_end
+= bytes_read
;
1221 cpio_errc(1, errno
, "Can't read %s", lr
->pathname
);
1231 process_lines_free(struct line_reader
*lr
)
1239 free_cache(struct name_cache
*cache
)
1243 if (cache
!= NULL
) {
1244 for (i
= 0; i
< cache
->size
; i
++)
1245 free(cache
->cache
[i
].name
);
1251 * Lookup uname/gname from uid/gid, return NULL if no match.
1254 lookup_name(struct cpio
*cpio
, struct name_cache
**name_cache_variable
,
1255 int (*lookup_fn
)(struct cpio
*, const char **, id_t
), id_t id
)
1258 struct name_cache
*cache
;
1263 if (*name_cache_variable
== NULL
) {
1264 *name_cache_variable
= malloc(sizeof(struct name_cache
));
1265 if (*name_cache_variable
== NULL
)
1266 cpio_errc(1, ENOMEM
, "No more memory");
1267 memset(*name_cache_variable
, 0, sizeof(struct name_cache
));
1268 (*name_cache_variable
)->size
= name_cache_size
;
1271 cache
= *name_cache_variable
;
1274 slot
= id
% cache
->size
;
1275 if (cache
->cache
[slot
].name
!= NULL
) {
1276 if (cache
->cache
[slot
].id
== id
) {
1278 return (cache
->cache
[slot
].name
);
1280 free(cache
->cache
[slot
].name
);
1281 cache
->cache
[slot
].name
= NULL
;
1284 if (lookup_fn(cpio
, &name
, id
) == 0) {
1285 if (name
== NULL
|| name
[0] == '\0') {
1286 /* If lookup failed, format it as a number. */
1287 snprintf(asnum
, sizeof(asnum
), "%u", (unsigned)id
);
1290 cache
->cache
[slot
].name
= strdup(name
);
1291 if (cache
->cache
[slot
].name
!= NULL
) {
1292 cache
->cache
[slot
].id
= id
;
1293 return (cache
->cache
[slot
].name
);
1296 * Conveniently, NULL marks an empty slot, so
1297 * if the strdup() fails, we've just failed to
1298 * cache it. No recovery necessary.
1305 lookup_uname(struct cpio
*cpio
, uid_t uid
)
1307 return (lookup_name(cpio
, &cpio
->uname_cache
,
1308 &lookup_uname_helper
, (id_t
)uid
));
1312 lookup_uname_helper(struct cpio
*cpio
, const char **name
, id_t id
)
1314 struct passwd
*pwent
;
1316 (void)cpio
; /* UNUSED */
1319 pwent
= getpwuid((uid_t
)id
);
1320 if (pwent
== NULL
) {
1323 cpio_warnc(errno
, "getpwuid(%d) failed", id
);
1327 *name
= pwent
->pw_name
;
1332 lookup_gname(struct cpio
*cpio
, gid_t gid
)
1334 return (lookup_name(cpio
, &cpio
->gname_cache
,
1335 &lookup_gname_helper
, (id_t
)gid
));
1339 lookup_gname_helper(struct cpio
*cpio
, const char **name
, id_t id
)
1341 struct group
*grent
;
1343 (void)cpio
; /* UNUSED */
1346 grent
= getgrgid((gid_t
)id
);
1347 if (grent
== NULL
) {
1350 cpio_warnc(errno
, "getgrgid(%d) failed", id
);
1354 *name
= grent
->gr_name
;