123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- #include "libbb.h"
- #include "common_bufsiz.h"
- #include "bb_archive.h"
- enum {
- OPT_EXTRACT = (1 << 0),
- OPT_TEST = (1 << 1),
- OPT_NUL_TERMINATED = (1 << 2),
- OPT_UNCONDITIONAL = (1 << 3),
- OPT_VERBOSE = (1 << 4),
- OPT_CREATE_LEADING_DIR = (1 << 5),
- OPT_PRESERVE_MTIME = (1 << 6),
- OPT_DEREF = (1 << 7),
- OPT_FILE = (1 << 8),
- OPT_OWNER = (1 << 9),
- OPTBIT_OWNER = 9,
- IF_FEATURE_CPIO_O(OPTBIT_CREATE ,)
- IF_FEATURE_CPIO_O(OPTBIT_FORMAT ,)
- IF_FEATURE_CPIO_P(OPTBIT_PASSTHROUGH,)
- IF_LONG_OPTS( OPTBIT_QUIET ,)
- IF_LONG_OPTS( OPTBIT_2STDOUT ,)
- OPT_CREATE = IF_FEATURE_CPIO_O((1 << OPTBIT_CREATE )) + 0,
- OPT_FORMAT = IF_FEATURE_CPIO_O((1 << OPTBIT_FORMAT )) + 0,
- OPT_PASSTHROUGH = IF_FEATURE_CPIO_P((1 << OPTBIT_PASSTHROUGH)) + 0,
- OPT_QUIET = IF_LONG_OPTS( (1 << OPTBIT_QUIET )) + 0,
- OPT_2STDOUT = IF_LONG_OPTS( (1 << OPTBIT_2STDOUT )) + 0,
- };
- #define OPTION_STR "it0uvdmLF:R:"
- struct globals {
- struct bb_uidgid_t owner_ugid;
- } FIX_ALIASING;
- #define G (*(struct globals*)bb_common_bufsiz1)
- void BUG_cpio_globals_too_big(void);
- #define INIT_G() do { \
- setup_common_bufsiz(); \
- G.owner_ugid.uid = -1L; \
- G.owner_ugid.gid = -1L; \
- } while (0)
- #if ENABLE_FEATURE_CPIO_O
- static off_t cpio_pad4(off_t size)
- {
- int i;
- i = (- size) & 3;
- size += i;
- while (--i >= 0)
- bb_putchar('\0');
- return size;
- }
- static NOINLINE int cpio_o(void)
- {
- struct name_s {
- struct name_s *next;
- char name[1];
- };
- struct inodes_s {
- struct inodes_s *next;
- struct name_s *names;
- struct stat st;
- };
- struct inodes_s *links = NULL;
- off_t bytes = 0;
- while (1) {
- const char *name;
- char *line;
- struct stat st;
- line = (option_mask32 & OPT_NUL_TERMINATED)
- ? bb_get_chunk_from_file(stdin, NULL)
- : xmalloc_fgetline(stdin);
- if (line) {
-
- name = line;
- while (name[0] == '.' && name[1] == '/') {
- while (*++name == '/')
- continue;
- }
- if (!*name) {
- free(line);
- continue;
- }
- if ((option_mask32 & OPT_DEREF)
- ? stat(name, &st)
- : lstat(name, &st)
- ) {
- abort_cpio_o:
- bb_simple_perror_msg_and_die(name);
- }
- if (G.owner_ugid.uid != (uid_t)-1L)
- st.st_uid = G.owner_ugid.uid;
- if (G.owner_ugid.gid != (gid_t)-1L)
- st.st_gid = G.owner_ugid.gid;
- if (!(S_ISLNK(st.st_mode) || S_ISREG(st.st_mode)))
- st.st_size = 0;
-
- if (!S_ISDIR(st.st_mode) && st.st_nlink > 1) {
- struct name_s *n;
- struct inodes_s *l;
-
- l = links;
- while (1) {
- if (l == NULL) {
-
- l = xzalloc(sizeof(*l));
- l->st = st;
- l->next = links;
- links = l;
- break;
- }
- if (l->st.st_ino == st.st_ino) {
-
- break;
- }
- l = l->next;
- }
-
- n = xmalloc(sizeof(*n) + strlen(name));
- strcpy(n->name, name);
- n->next = l->names;
- l->names = n;
- free(line);
- continue;
- }
- } else {
- next_link:
- if (links) {
-
- st = links->st;
- name = links->names->name;
- links->names = links->names->next;
-
- if (links->names == NULL)
- links = links->next;
- else
- st.st_size = 0;
-
- } else {
-
- name = cpio_TRAILER;
-
- memset(&st, 0, sizeof(st));
-
- }
- }
- bytes += printf("070701"
- "%08X%08X%08X%08X%08X%08X%08X"
- "%08X%08X%08X%08X"
- "%08X"
- "00000000"
- "%s%c",
- (unsigned)(uint32_t) st.st_ino,
- (unsigned)(uint32_t) st.st_mode,
- (unsigned)(uint32_t) st.st_uid,
- (unsigned)(uint32_t) st.st_gid,
- (unsigned)(uint32_t) st.st_nlink,
- (unsigned)(uint32_t) st.st_mtime,
- (unsigned)(uint32_t) st.st_size,
- (unsigned)(uint32_t) major(st.st_dev),
- (unsigned)(uint32_t) minor(st.st_dev),
- (unsigned)(uint32_t) major(st.st_rdev),
- (unsigned)(uint32_t) minor(st.st_rdev),
- (unsigned)(strlen(name) + 1),
- name, '\0');
- bytes = cpio_pad4(bytes);
- if (st.st_size) {
- if (S_ISLNK(st.st_mode)) {
- char *lpath = xmalloc_readlink_or_warn(name);
- if (!lpath)
- goto abort_cpio_o;
- bytes += printf("%s", lpath);
- free(lpath);
- } else {
- int fd = xopen(name, O_RDONLY);
- fflush_all();
-
- bb_copyfd_exact_size(fd, STDOUT_FILENO, st.st_size);
- bytes += st.st_size;
- close(fd);
- }
- bytes = cpio_pad4(bytes);
- }
- if (!line) {
- if (name != cpio_TRAILER)
- goto next_link;
-
- return EXIT_SUCCESS;
- }
- free(line);
- }
- }
- #endif
- int cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int cpio_main(int argc UNUSED_PARAM, char **argv)
- {
- archive_handle_t *archive_handle;
- char *cpio_filename;
- char *cpio_owner;
- IF_FEATURE_CPIO_O(const char *cpio_fmt = "";)
- unsigned opt;
- #if ENABLE_LONG_OPTS
- const char *long_opts =
- "extract\0" No_argument "i"
- "list\0" No_argument "t"
- #if ENABLE_FEATURE_CPIO_O
- "create\0" No_argument "o"
- "format\0" Required_argument "H"
- #if ENABLE_FEATURE_CPIO_P
- "pass-through\0" No_argument "p"
- #endif
- #endif
- "owner\0" Required_argument "R"
- "verbose\0" No_argument "v"
- "quiet\0" No_argument "\xff"
- "to-stdout\0" No_argument "\xfe"
- ;
- #endif
- INIT_G();
- archive_handle = init_handle();
-
- archive_handle->ah_flags = ARCHIVE_EXTRACT_NEWER;
-
-
-
-
- #if !ENABLE_FEATURE_CPIO_O
- opt = getopt32long(argv, OPTION_STR, long_opts, &cpio_filename, &cpio_owner);
- #else
- opt = getopt32long(argv, OPTION_STR "oH:" IF_FEATURE_CPIO_P("p"), long_opts,
- &cpio_filename, &cpio_owner, &cpio_fmt);
- #endif
- argv += optind;
- if (opt & OPT_OWNER) {
- parse_chown_usergroup_or_die(&G.owner_ugid, cpio_owner);
- archive_handle->cpio__owner = G.owner_ugid;
- }
- #if !ENABLE_FEATURE_CPIO_O
- if (opt & OPT_FILE) {
- xmove_fd(xopen(cpio_filename, O_RDONLY), STDIN_FILENO);
- }
- #else
- if ((opt & (OPT_FILE|OPT_CREATE)) == OPT_FILE) {
- xmove_fd(xopen(cpio_filename, O_RDONLY), STDIN_FILENO);
- }
- if (opt & OPT_PASSTHROUGH) {
- pid_t pid;
- struct fd_pair pp;
- if (argv[0] == NULL)
- bb_show_usage();
- if (opt & OPT_CREATE_LEADING_DIR)
- mkdir(argv[0], 0777);
-
- #if !BB_MMU
- pp.rd = 3;
- pp.wr = 4;
- if (!re_execed) {
- close(3);
- close(4);
- xpiped_pair(pp);
- }
- #else
- xpiped_pair(pp);
- #endif
- pid = fork_or_rexec(argv - optind);
- if (pid == 0) {
- close(pp.rd);
- xmove_fd(pp.wr, STDOUT_FILENO);
- goto dump;
- }
-
- USE_FOR_NOMMU(argv[-optind][0] &= 0x7f);
- xchdir(*argv++);
- close(pp.wr);
- xmove_fd(pp.rd, STDIN_FILENO);
-
- opt |= OPT_EXTRACT;
- goto skip;
- }
-
- if (opt & OPT_CREATE) {
- if (cpio_fmt[0] != 'n')
- bb_show_usage();
- if (opt & OPT_FILE) {
- xmove_fd(xopen(cpio_filename, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO);
- }
- dump:
- return cpio_o();
- }
- skip:
- #endif
-
- if ((opt & (OPT_TEST | OPT_EXTRACT)) == 0) {
- bb_show_usage();
- }
- if (opt & OPT_TEST) {
-
- opt &= ~OPT_EXTRACT;
- archive_handle->action_header = header_list;
- }
- if (opt & OPT_EXTRACT) {
- archive_handle->action_data = data_extract_all;
- if (opt & OPT_2STDOUT)
- archive_handle->action_data = data_extract_to_stdout;
- }
- if (opt & OPT_UNCONDITIONAL) {
- archive_handle->ah_flags |= ARCHIVE_UNLINK_OLD;
- archive_handle->ah_flags &= ~ARCHIVE_EXTRACT_NEWER;
- }
- if (opt & OPT_VERBOSE) {
- if (archive_handle->action_header == header_list) {
- archive_handle->action_header = header_verbose_list;
- } else {
- archive_handle->action_header = header_list;
- }
- }
- if (opt & OPT_CREATE_LEADING_DIR) {
- archive_handle->ah_flags |= ARCHIVE_CREATE_LEADING_DIRS;
- }
- if (opt & OPT_PRESERVE_MTIME) {
- archive_handle->ah_flags |= ARCHIVE_RESTORE_DATE;
- }
- while (*argv) {
- archive_handle->filter = filter_accept_list;
- llist_add_to(&archive_handle->accept, *argv);
- argv++;
- }
-
- archive_handle->cpio__blocks = (off_t)-1;
- while (get_header_cpio(archive_handle) == EXIT_SUCCESS)
- continue;
- create_links_from_list(archive_handle->link_placeholders);
- if (archive_handle->cpio__blocks != (off_t)-1
- && !(opt & OPT_QUIET)
- ) {
- fprintf(stderr, "%"OFF_FMT"u blocks\n", archive_handle->cpio__blocks);
- }
- return EXIT_SUCCESS;
- }
|