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$");
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 r = archive_write_set_compression_bzip2(cpio->archive);
469 r = archive_write_set_compression_gzip(cpio->archive);
472 r = archive_write_set_compression_compress(cpio->archive);
476 r = archive_write_set_compression_none(cpio->archive);
479 cpio_errc(1, 0, "Unrecognized compression option");
482 cpio_errc(1, 0, "Unsupported compression format");
484 if (strcmp(cpio->format, "cpio"))
485 r = archive_write_set_format_cpio(cpio->archive);
486 else if (strcmp(cpio->format, "odc"))
487 r = archive_write_set_format_cpio(cpio->archive);
488 else if (strcmp(cpio->format, "newc"))
489 r = archive_write_set_format_cpio(cpio->archive);
490 else if (strcmp(cpio->format, "ustar"))
491 r = archive_write_set_format_cpio(cpio->archive);
493 r = archive_write_set_format_by_name(cpio->archive, cpio->format);
496 cpio_errc(1, 0, archive_error_string(cpio->archive));
497 archive_write_set_bytes_per_block(cpio->archive, cpio->bytes_per_block);
498 cpio->linkresolver = archive_entry_linkresolver_new();
499 archive_entry_linkresolver_set_strategy(cpio->linkresolver,
500 archive_format(cpio->archive));
502 r = archive_write_open_file(cpio->archive, cpio->filename);
504 cpio_errc(1, 0, archive_error_string(cpio->archive));
505 lr = process_lines_init("-", cpio->line_separator);
506 while ((p = process_lines_next(lr)) != NULL)
507 file_to_archive(cpio, p);
508 process_lines_free(lr);
511 * The hardlink detection may have queued up a couple of entries
512 * that can now be flushed.
515 archive_entry_linkify(cpio->linkresolver, &entry, &spare);
516 while (entry != NULL) {
517 entry_to_archive(cpio, entry);
518 archive_entry_free(entry);
520 archive_entry_linkify(cpio->linkresolver, &entry, &spare);
523 r = archive_write_close(cpio->archive);
525 cpio_errc(1, 0, archive_error_string(cpio->archive));
528 blocks = (archive_position_uncompressed(cpio->archive) + 511)
530 fprintf(stderr, "%lu %s\n", blocks,
531 blocks == 1 ? "block" : "blocks");
533 archive_write_finish(cpio->archive);
537 * This is used by both out mode (to copy objects from disk into
538 * an archive) and pass mode (to copy objects from disk to
539 * an archive_write_disk "archive").
542 file_to_archive(struct cpio *cpio, const char *srcpath)
545 const char *destpath;
546 struct archive_entry *entry, *spare;
549 #if !defined(_WIN32) || defined(__CYGWIN__)
555 * Create an archive_entry describing the source file.
557 * XXX TODO: rework to use archive_read_disk_entry_from_file()
559 entry = archive_entry_new();
561 cpio_errc(1, 0, "Couldn't allocate entry");
562 archive_entry_copy_sourcepath(entry, srcpath);
564 /* Get stat information. */
565 if (cpio->option_follow_links)
566 r = stat(srcpath, &st);
568 r = lstat(srcpath, &st);
570 cpio_warnc(errno, "Couldn't stat \"%s\"", srcpath);
571 archive_entry_free(entry);
575 if (cpio->uid_override >= 0)
576 st.st_uid = cpio->uid_override;
577 if (cpio->gid_override >= 0)
578 st.st_gid = cpio->gid_override;
579 archive_entry_copy_stat(entry, &st);
581 #if !defined(_WIN32) || defined(__CYGWIN__)
582 /* If its a symlink, pull the target. */
583 if (S_ISLNK(st.st_mode)) {
584 lnklen = readlink(srcpath, cpio->buff, cpio->buff_size);
587 "%s: Couldn't read symbolic link", srcpath);
588 archive_entry_free(entry);
591 cpio->buff[lnklen] = 0;
592 archive_entry_set_symlink(entry, cpio->buff);
597 * Generate a destination path for this entry.
598 * "destination path" is the name to which it will be copied in
599 * pass mode or the name that will go into the archive in
604 len = strlen(cpio->destdir) + strlen(srcpath) + 8;
605 if (len >= cpio->pass_destpath_alloc) {
606 while (len >= cpio->pass_destpath_alloc) {
607 cpio->pass_destpath_alloc += 512;
608 cpio->pass_destpath_alloc *= 2;
610 free(cpio->pass_destpath);
611 cpio->pass_destpath = malloc(cpio->pass_destpath_alloc);
612 if (cpio->pass_destpath == NULL)
614 "Can't allocate path buffer");
616 strcpy(cpio->pass_destpath, cpio->destdir);
620 strcat(cpio->pass_destpath, p);
621 destpath = cpio->pass_destpath;
623 if (cpio->option_rename)
624 destpath = cpio_rename(destpath);
625 if (destpath == NULL)
627 archive_entry_copy_pathname(entry, destpath);
630 * If we're trying to preserve hardlinks, match them here.
633 if (cpio->linkresolver != NULL
634 && !S_ISDIR(st.st_mode)) {
635 archive_entry_linkify(cpio->linkresolver, &entry, &spare);
639 r = entry_to_archive(cpio, entry);
640 archive_entry_free(entry);
644 r = entry_to_archive(cpio, spare);
645 archive_entry_free(spare);
651 entry_to_archive(struct cpio *cpio, struct archive_entry *entry)
653 const char *destpath = archive_entry_pathname(entry);
654 const char *srcpath = archive_entry_sourcepath(entry);
659 /* Print out the destination name to the user. */
661 fprintf(stderr,"%s", destpath);
664 * Option_link only makes sense in pass mode and for
665 * regular files. Also note: if a link operation fails
666 * because of cross-device restrictions, we'll fall back
667 * to copy mode for that entry.
669 * TODO: Test other cpio implementations to see if they
670 * hard-link anything other than regular files here.
672 if (cpio->option_link
673 && archive_entry_filetype(entry) == AE_IFREG)
675 struct archive_entry *t;
676 /* Save the original entry in case we need it later. */
677 t = archive_entry_clone(entry);
679 cpio_errc(1, ENOMEM, "Can't create link");
680 /* Note: link(2) doesn't create parent directories,
681 * so we use archive_write_header() instead as a
683 archive_entry_set_hardlink(t, srcpath);
684 /* This is a straight link that carries no data. */
685 archive_entry_set_size(t, 0);
686 r = archive_write_header(cpio->archive, t);
687 archive_entry_free(t);
689 cpio_warnc(archive_errno(cpio->archive),
690 archive_error_string(cpio->archive));
691 if (r == ARCHIVE_FATAL)
694 if (r != ARCHIVE_OK && archive_errno(cpio->archive) == EXDEV) {
695 /* Cross-device link: Just fall through and use
696 * the original entry to copy the file over. */
697 cpio_warnc(0, "Copying file instead");
704 * Make sure we can open the file (if necessary) before
705 * trying to write the header.
707 if (archive_entry_filetype(entry) == AE_IFREG) {
708 if (archive_entry_size(entry) > 0) {
709 fd = open(srcpath, O_RDONLY);
712 "%s: could not open file", srcpath);
717 archive_entry_set_size(entry, 0);
720 r = archive_write_header(cpio->archive, entry);
723 cpio_warnc(archive_errno(cpio->archive),
726 archive_error_string(cpio->archive));
728 if (r == ARCHIVE_FATAL)
731 if (r >= ARCHIVE_WARN && fd >= 0) {
732 bytes_read = read(fd, cpio->buff, cpio->buff_size);
733 while (bytes_read > 0) {
734 r = archive_write_data(cpio->archive,
735 cpio->buff, bytes_read);
737 cpio_errc(1, archive_errno(cpio->archive),
738 archive_error_string(cpio->archive));
739 if (r < bytes_read) {
741 "Truncated write; file may have grown while being archived.");
743 bytes_read = read(fd, cpio->buff, cpio->buff_size);
747 fd = restore_time(cpio, entry, srcpath, fd);
751 fprintf(stderr,"\n");
758 restore_time(struct cpio *cpio, struct archive_entry *entry,
759 const char *name, int fd)
762 static int warned = 0;
764 (void)cpio; /* UNUSED */
765 (void)entry; /* UNUSED */
766 (void)name; /* UNUSED */
769 cpio_warnc(0, "Can't restore access times on this platform");
773 #if defined(_WIN32) && !defined(__CYGWIN__)
774 struct __timeval times[2];
776 struct timeval times[2];
779 if (!cpio->option_atime_restore)
782 times[1].tv_sec = archive_entry_mtime(entry);
783 times[1].tv_usec = archive_entry_mtime_nsec(entry) / 1000;
785 times[0].tv_sec = archive_entry_atime(entry);
786 times[0].tv_usec = archive_entry_atime_nsec(entry) / 1000;
789 if (fd >= 0 && futimes(fd, times) == 0)
793 * Some platform cannot restore access times if the file descriptor
802 if (lutimes(name, times) != 0)
804 if (!S_ISLNK(archive_entry_mode(entry)) && utimes(name, times) != 0)
806 cpio_warnc(errno, "Can't update time for %s", name);
813 mode_in(struct cpio *cpio)
816 struct archive_entry *entry;
818 const char *destpath;
819 unsigned long blocks;
822 ext = archive_write_disk_new();
824 cpio_errc(1, 0, "Couldn't allocate restore object");
825 r = archive_write_disk_set_options(ext, cpio->extract_flags);
827 cpio_errc(1, 0, archive_error_string(ext));
828 a = archive_read_new();
830 cpio_errc(1, 0, "Couldn't allocate archive object");
832 archive_read_support_format_cpio(a);
833 archive_read_support_format_tar(a);
835 archive_read_support_compression_all(a);
836 archive_read_support_format_all(a);
839 if (archive_read_open_file(a, cpio->filename, cpio->bytes_per_block))
840 cpio_errc(1, archive_errno(a),
841 archive_error_string(a));
843 r = archive_read_next_header(a, &entry);
844 if (r == ARCHIVE_EOF)
846 if (r != ARCHIVE_OK) {
847 cpio_errc(1, archive_errno(a),
848 archive_error_string(a));
850 if (excluded(cpio, archive_entry_pathname(entry)))
852 if (cpio->option_rename) {
853 destpath = cpio_rename(archive_entry_pathname(entry));
854 archive_entry_set_pathname(entry, destpath);
856 destpath = archive_entry_pathname(entry);
857 if (destpath == NULL)
860 fprintf(stdout, "%s\n", destpath);
861 if (cpio->uid_override >= 0)
862 archive_entry_set_uid(entry, cpio->uid_override);
863 if (cpio->gid_override >= 0)
864 archive_entry_set_gid(entry, cpio->gid_override);
865 r = archive_write_header(ext, entry);
866 if (r != ARCHIVE_OK) {
867 fprintf(stderr, "%s: %s\n",
868 archive_entry_pathname(entry),
869 archive_error_string(ext));
870 } else if (archive_entry_size(entry) > 0) {
871 r = copy_data(a, ext);
874 r = archive_read_close(a);
876 cpio_errc(1, 0, archive_error_string(a));
877 r = archive_write_close(ext);
879 cpio_errc(1, 0, archive_error_string(ext));
881 blocks = (archive_position_uncompressed(a) + 511)
883 fprintf(stderr, "%lu %s\n", blocks,
884 blocks == 1 ? "block" : "blocks");
886 archive_read_finish(a);
887 archive_write_finish(ext);
892 copy_data(struct archive *ar, struct archive *aw)
900 r = archive_read_data_block(ar, &block, &size, &offset);
901 if (r == ARCHIVE_EOF)
903 if (r != ARCHIVE_OK) {
904 cpio_warnc(archive_errno(ar),
905 "%s", archive_error_string(ar));
908 r = archive_write_data_block(aw, block, size, offset);
909 if (r != ARCHIVE_OK) {
910 cpio_warnc(archive_errno(aw),
911 archive_error_string(aw));
918 mode_list(struct cpio *cpio)
921 struct archive_entry *entry;
922 unsigned long blocks;
925 a = archive_read_new();
927 cpio_errc(1, 0, "Couldn't allocate archive object");
929 archive_read_support_format_cpio(a);
930 archive_read_support_format_tar(a);
932 archive_read_support_compression_all(a);
933 archive_read_support_format_all(a);
936 if (archive_read_open_file(a, cpio->filename, cpio->bytes_per_block))
937 cpio_errc(1, archive_errno(a),
938 archive_error_string(a));
940 r = archive_read_next_header(a, &entry);
941 if (r == ARCHIVE_EOF)
943 if (r != ARCHIVE_OK) {
944 cpio_errc(1, archive_errno(a),
945 archive_error_string(a));
947 if (excluded(cpio, archive_entry_pathname(entry)))
950 list_item_verbose(cpio, entry);
952 fprintf(stdout, "%s\n", archive_entry_pathname(entry));
954 r = archive_read_close(a);
956 cpio_errc(1, 0, archive_error_string(a));
958 blocks = (archive_position_uncompressed(a) + 511)
960 fprintf(stderr, "%lu %s\n", blocks,
961 blocks == 1 ? "block" : "blocks");
963 archive_read_finish(a);
968 * Display information about the current file.
970 * The format here roughly duplicates the output of 'ls -l'.
971 * This is based on SUSv2, where 'tar tv' is documented as
972 * listing additional information in an "unspecified format,"
973 * and 'pax -l' is documented as using the same format as 'ls -l'.
976 list_item_verbose(struct cpio *cpio, struct archive_entry *entry)
980 char uids[16], gids[16];
981 const char *uname, *gname;
983 const struct stat *st;
988 st = archive_entry_stat(entry);
993 if (cpio->option_numeric_uid_gid) {
994 /* Format numeric uid/gid for display. */
995 snprintf(uids, sizeof(uids), "%jd",
996 (intmax_t)archive_entry_uid(entry));
998 snprintf(gids, sizeof(gids), "%jd",
999 (intmax_t)archive_entry_gid(entry));
1002 /* Use uname if it's present, else lookup name from uid. */
1003 uname = archive_entry_uname(entry);
1005 uname = lookup_uname(cpio, archive_entry_uid(entry));
1006 /* Use gname if it's present, else lookup name from gid. */
1007 gname = archive_entry_gname(entry);
1009 gname = lookup_gname(cpio, archive_entry_gid(entry));
1012 /* Print device number or file size. */
1013 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
1014 snprintf(size, sizeof(size), "%lu,%lu",
1015 (unsigned long)major(st->st_rdev),
1016 (unsigned long)minor(st->st_rdev)); /* ls(1) also casts here. */
1018 snprintf(size, sizeof(size), CPIO_FILESIZE_PRINTF,
1019 (CPIO_FILESIZE_TYPE)st->st_size);
1022 /* Format the time using 'ls -l' conventions. */
1023 tim = (time_t)st->st_mtime;
1024 #if defined(_WIN32) && !defined(__CYGWIN__)
1025 /* Windows' strftime function does not support %e format. */
1026 if (abs(tim - now) > (365/2)*86400)
1027 fmt = cpio->day_first ? "%d %b %Y" : "%b %d %Y";
1029 fmt = cpio->day_first ? "%d %b %H:%M" : "%b %d %H:%M";
1031 if (abs(tim - now) > (365/2)*86400)
1032 fmt = cpio->day_first ? "%e %b %Y" : "%b %e %Y";
1034 fmt = cpio->day_first ? "%e %b %H:%M" : "%b %e %H:%M";
1036 strftime(date, sizeof(date), fmt, localtime(&tim));
1038 fprintf(out, "%s%3d %-8s %-8s %8s %12s %s",
1039 archive_entry_strmode(entry),
1040 archive_entry_nlink(entry),
1041 uname, gname, size, date,
1042 archive_entry_pathname(entry));
1044 /* Extra information for links. */
1045 if (archive_entry_hardlink(entry)) /* Hard link */
1046 fprintf(out, " link to %s", archive_entry_hardlink(entry));
1047 else if (archive_entry_symlink(entry)) /* Symbolic link */
1048 fprintf(out, " -> %s", archive_entry_symlink(entry));
1053 mode_pass(struct cpio *cpio, const char *destdir)
1055 unsigned long blocks;
1056 struct line_reader *lr;
1060 /* Ensure target dir has a trailing '/' to simplify path surgery. */
1061 cpio->destdir = malloc(strlen(destdir) + 8);
1062 strcpy(cpio->destdir, destdir);
1063 if (destdir[strlen(destdir) - 1] != '/')
1064 strcat(cpio->destdir, "/");
1066 cpio->archive = archive_write_disk_new();
1067 if (cpio->archive == NULL)
1068 cpio_errc(1, 0, "Failed to allocate archive object");
1069 r = archive_write_disk_set_options(cpio->archive, cpio->extract_flags);
1070 if (r != ARCHIVE_OK)
1071 cpio_errc(1, 0, archive_error_string(cpio->archive));
1072 cpio->linkresolver = archive_entry_linkresolver_new();
1073 archive_write_disk_set_standard_lookup(cpio->archive);
1074 lr = process_lines_init("-", cpio->line_separator);
1075 while ((p = process_lines_next(lr)) != NULL)
1076 file_to_archive(cpio, p);
1077 process_lines_free(lr);
1079 archive_entry_linkresolver_free(cpio->linkresolver);
1080 r = archive_write_close(cpio->archive);
1081 if (r != ARCHIVE_OK)
1082 cpio_errc(1, 0, archive_error_string(cpio->archive));
1085 blocks = (archive_position_uncompressed(cpio->archive) + 511)
1087 fprintf(stderr, "%lu %s\n", blocks,
1088 blocks == 1 ? "block" : "blocks");
1091 archive_write_finish(cpio->archive);
1095 * Prompt for a new name for this entry. Returns a pointer to the
1096 * new name or NULL if the entry should not be copied. This
1097 * implements the semantics defined in POSIX.1-1996, which specifies
1098 * that an input of '.' means the name should be unchanged. GNU cpio
1099 * treats '.' as a literal new name.
1102 cpio_rename(const char *name)
1104 static char buff[1024];
1108 t = fopen("/dev/tty", "r+");
1111 fprintf(t, "%s (Enter/./(new name))? ", name);
1114 p = fgets(buff, sizeof(buff), t);
1117 /* End-of-file is a blank line. */
1120 while (*p == ' ' || *p == '\t')
1122 if (*p == '\n' || *p == '\0')
1125 if (*p == '.' && p[1] == '\n')
1126 /* Single period preserves original name. */
1129 /* Trim the final newline. */
1130 while (*p != '\0' && *p != '\n')
1132 /* Overwrite the final \n with a null character. */
1139 * Read lines from file and do something with each one. If option_null
1140 * is set, lines are terminated with zero bytes; otherwise, they're
1141 * terminated with newlines.
1143 * This uses a self-sizing buffer to handle arbitrarily-long lines.
1145 struct line_reader {
1147 char *buff, *buff_end, *line_start, *line_end, *p;
1154 struct line_reader *
1155 process_lines_init(const char *pathname, char separator)
1157 struct line_reader *lr;
1159 lr = calloc(1, sizeof(*lr));
1161 cpio_errc(1, ENOMEM, "Can't open %s", pathname);
1163 lr->separator = separator;
1164 lr->pathname = strdup(pathname);
1166 if (strcmp(pathname, "-") == 0)
1169 lr->f = fopen(pathname, "r");
1171 cpio_errc(1, errno, "Couldn't open %s", pathname);
1172 lr->buff_length = 8192;
1173 lr->buff = malloc(lr->buff_length);
1174 if (lr->buff == NULL)
1175 cpio_errc(1, ENOMEM, "Can't read %s", pathname);
1176 lr->line_start = lr->line_end = lr->buff_end = lr->buff;
1182 process_lines_next(struct line_reader *lr)
1184 size_t bytes_wanted, bytes_read, new_buff_size;
1185 char *line_start, *p;
1188 /* If there's a line in the buffer, return it immediately. */
1189 while (lr->line_end < lr->buff_end) {
1190 if (*lr->line_end == lr->separator) {
1191 *lr->line_end = '\0';
1192 line_start = lr->line_start;
1193 lr->line_start = lr->line_end + 1;
1194 lr->line_end = lr->line_start;
1195 return (line_start);
1200 /* If we're at end-of-file, process the final data. */
1201 if (lr->f == NULL) {
1202 /* If there's more text, return one last line. */
1203 if (lr->line_end > lr->line_start) {
1204 *lr->line_end = '\0';
1205 line_start = lr->line_start;
1206 lr->line_start = lr->line_end + 1;
1207 lr->line_end = lr->line_start;
1208 return (line_start);
1210 /* Otherwise, we're done. */
1214 /* Buffer only has part of a line. */
1215 if (lr->line_start > lr->buff) {
1216 /* Move a leftover fractional line to the beginning. */
1217 memmove(lr->buff, lr->line_start,
1218 lr->buff_end - lr->line_start);
1219 lr->buff_end -= lr->line_start - lr->buff;
1220 lr->line_end -= lr->line_start - lr->buff;
1221 lr->line_start = lr->buff;
1223 /* Line is too big; enlarge the buffer. */
1224 new_buff_size = lr->buff_length * 2;
1225 if (new_buff_size <= lr->buff_length)
1226 cpio_errc(1, ENOMEM,
1227 "Line too long in %s", lr->pathname);
1228 lr->buff_length = new_buff_size;
1229 p = realloc(lr->buff, new_buff_size);
1231 cpio_errc(1, ENOMEM,
1232 "Line too long in %s", lr->pathname);
1233 lr->buff_end = p + (lr->buff_end - lr->buff);
1234 lr->line_end = p + (lr->line_end - lr->buff);
1235 lr->line_start = lr->buff = p;
1238 /* Get some more data into the buffer. */
1239 bytes_wanted = lr->buff + lr->buff_length - lr->buff_end;
1240 bytes_read = fread(lr->buff_end, 1, bytes_wanted, lr->f);
1241 lr->buff_end += bytes_read;
1244 cpio_errc(1, errno, "Can't read %s", lr->pathname);
1254 process_lines_free(struct line_reader *lr)
1262 free_cache(struct name_cache *cache)
1266 if (cache != NULL) {
1267 for (i = 0; i < cache->size; i++)
1268 free(cache->cache[i].name);
1274 * Lookup uname/gname from uid/gid, return NULL if no match.
1277 lookup_name(struct cpio *cpio, struct name_cache **name_cache_variable,
1278 int (*lookup_fn)(struct cpio *, const char **, id_t), id_t id)
1281 struct name_cache *cache;
1286 if (*name_cache_variable == NULL) {
1287 *name_cache_variable = malloc(sizeof(struct name_cache));
1288 if (*name_cache_variable == NULL)
1289 cpio_errc(1, ENOMEM, "No more memory");
1290 memset(*name_cache_variable, 0, sizeof(struct name_cache));
1291 (*name_cache_variable)->size = name_cache_size;
1294 cache = *name_cache_variable;
1297 slot = id % cache->size;
1298 if (cache->cache[slot].name != NULL) {
1299 if (cache->cache[slot].id == id) {
1301 return (cache->cache[slot].name);
1303 free(cache->cache[slot].name);
1304 cache->cache[slot].name = NULL;
1307 if (lookup_fn(cpio, &name, id) == 0) {
1308 if (name == NULL || name[0] == '\0') {
1309 /* If lookup failed, format it as a number. */
1310 snprintf(asnum, sizeof(asnum), "%u", (unsigned)id);
1313 cache->cache[slot].name = strdup(name);
1314 if (cache->cache[slot].name != NULL) {
1315 cache->cache[slot].id = id;
1316 return (cache->cache[slot].name);
1319 * Conveniently, NULL marks an empty slot, so
1320 * if the strdup() fails, we've just failed to
1321 * cache it. No recovery necessary.
1328 lookup_uname(struct cpio *cpio, uid_t uid)
1330 return (lookup_name(cpio, &cpio->uname_cache,
1331 &lookup_uname_helper, (id_t)uid));
1335 lookup_uname_helper(struct cpio *cpio, const char **name, id_t id)
1337 struct passwd *pwent;
1339 (void)cpio; /* UNUSED */
1342 pwent = getpwuid((uid_t)id);
1343 if (pwent == NULL) {
1346 cpio_warnc(errno, "getpwuid(%d) failed", id);
1350 *name = pwent->pw_name;
1355 lookup_gname(struct cpio *cpio, gid_t gid)
1357 return (lookup_name(cpio, &cpio->gname_cache,
1358 &lookup_gname_helper, (id_t)gid));
1362 lookup_gname_helper(struct cpio *cpio, const char **name, id_t id)
1364 struct group *grent;
1366 (void)cpio; /* UNUSED */
1369 grent = getgrgid((gid_t)id);
1370 if (grent == NULL) {
1373 cpio_warnc(errno, "getgrgid(%d) failed", id);
1377 *name = grent->gr_name;