void extract_cpio_gz(int fd) { archive_handle_t *archive_handle; unsigned char magic[2]; /* Initialise */ archive_handle = init_handle(); archive_handle->seek = seek_by_char; //archive_handle->action_header = header_list; archive_handle->action_data = data_extract_all; archive_handle->flags |= ARCHIVE_PRESERVE_DATE; archive_handle->flags |= ARCHIVE_CREATE_LEADING_DIRS; archive_handle->src_fd = fd; archive_handle->offset = 0; bb_xread_all(archive_handle->src_fd, &magic, 2); if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) { bb_error_msg_and_die("Invalid gzip magic"); } check_header_gzip(archive_handle->src_fd); bb_xchdir("/"); // Install RPM's to root archive_handle->src_fd = open_transformer(archive_handle->src_fd, inflate_gunzip); archive_handle->offset = 0; while (get_header_cpio(archive_handle) == EXIT_SUCCESS); }
/* No getopt required */ int rpm2cpio_main(int argc, char **argv) { struct rpm_lead lead; int rpm_fd; unsigned char magic[2]; if (argc == 1) { rpm_fd = STDIN_FILENO; } else { rpm_fd = bb_xopen(argv[1], O_RDONLY); } bb_xread_all(rpm_fd, &lead, sizeof(struct rpm_lead)); if (strncmp((char *) &lead.magic, RPM_MAGIC, 4) != 0) { bb_error_msg_and_die("Invalid RPM magic"); /* Just check the magic, the rest is irrelevant */ } /* Skip the signature header */ skip_header(rpm_fd); lseek(rpm_fd, (8 - (lseek(rpm_fd, 0, SEEK_CUR) % 8)) % 8, SEEK_CUR); /* Skip the main header */ skip_header(rpm_fd); bb_xread_all(rpm_fd, &magic, 2); if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) { bb_error_msg_and_die("Invalid gzip magic"); } check_header_gzip(rpm_fd); if (inflate_gunzip(rpm_fd, STDOUT_FILENO) != 0) { bb_error_msg("Error inflating"); } close(rpm_fd); return 0; }
int gunzip_main(int argc, char **argv) { char status = EXIT_SUCCESS; unsigned long opt; opt = bb_getopt_ulflags(argc, argv, "cftd"); /* if called as zcat */ if (strcmp(bb_applet_name, "zcat") == 0) { opt |= GUNZIP_OPT_STDOUT; } do { struct stat stat_buf; const char *old_path = argv[optind]; const char *delete_path = NULL; char *new_path = NULL; int src_fd; int dst_fd; optind++; if (old_path == NULL || strcmp(old_path, "-") == 0) { src_fd = STDIN_FILENO; opt |= GUNZIP_OPT_STDOUT; } else { src_fd = bb_xopen(old_path, O_RDONLY); /* Get the time stamp on the input file. */ xstat(old_path, &stat_buf); } /* Check that the input is sane. */ if (isatty(src_fd) && ((opt & GUNZIP_OPT_FORCE) == 0)) { bb_error_msg_and_die ("compressed data not read from terminal. Use -f to force it."); } /* Set output filename and number */ if (opt & GUNZIP_OPT_TEST) { dst_fd = bb_xopen(bb_dev_null, O_WRONLY); /* why does test use filenum 2 ? */ } else if (opt & GUNZIP_OPT_STDOUT) { dst_fd = STDOUT_FILENO; } else { char *extension; new_path = bb_xstrdup(old_path); extension = strrchr(new_path, '.'); #ifdef CONFIG_FEATURE_GUNZIP_UNCOMPRESS if (extension && (strcmp(extension, ".Z") == 0)) { *extension = '\0'; } else #endif if (extension && (strcmp(extension, ".gz") == 0)) { *extension = '\0'; } else if (extension && (strcmp(extension, ".tgz") == 0)) { extension[2] = 'a'; extension[3] = 'r'; } else { bb_error_msg_and_die("Invalid extension"); } /* Open output file (with correct permissions) */ dst_fd = bb_xopen3(new_path, O_WRONLY | O_CREAT, stat_buf.st_mode); /* If unzip succeeds remove the old file */ delete_path = old_path; } /* do the decompression, and cleanup */ if (bb_xread_char(src_fd) == 0x1f) { unsigned char magic2; magic2 = bb_xread_char(src_fd); #ifdef CONFIG_FEATURE_GUNZIP_UNCOMPRESS if (magic2 == 0x9d) { status = uncompress(src_fd, dst_fd); } else #endif if (magic2 == 0x8b) { check_header_gzip(src_fd); status = inflate_gunzip(src_fd, dst_fd); if (status != 0) { bb_error_msg_and_die("Error inflating"); } } else { bb_error_msg_and_die("Invalid magic"); } } else { bb_error_msg_and_die("Invalid magic"); } if ((status != EXIT_SUCCESS) && (new_path)) { /* Unzip failed, remove new path instead of old path */ delete_path = new_path; } if (dst_fd != STDOUT_FILENO) { close(dst_fd); } if (src_fd != STDIN_FILENO) { close(src_fd); } /* delete_path will be NULL if in test mode or from stdin */ if (delete_path && (unlink(delete_path) == -1)) { bb_error_msg_and_die("Couldn't remove %s", delete_path); } free(new_path); } while (optind < argc); return status; }