Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini rpm2cpio implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2001 by Laurence Anderson |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 8 | */ |
Pere Orga | 1f4447b | 2011-03-27 22:40:30 +0200 | [diff] [blame] | 9 | |
Denys Vlasenko | f6beef6 | 2013-11-14 11:39:00 +0100 | [diff] [blame] | 10 | //config:config RPM2CPIO |
| 11 | //config: bool "rpm2cpio" |
| 12 | //config: default y |
| 13 | //config: help |
| 14 | //config: Converts a RPM file into a CPIO archive. |
| 15 | |
Denys Vlasenko | 36184a4 | 2013-11-14 09:54:24 +0100 | [diff] [blame] | 16 | //applet:IF_RPM2CPIO(APPLET(rpm2cpio, BB_DIR_USR_BIN, BB_SUID_DROP)) |
Denys Vlasenko | 66620fa | 2013-11-14 09:53:52 +0100 | [diff] [blame] | 17 | //kbuild:lib-$(CONFIG_RPM2CPIO) += rpm2cpio.o |
| 18 | |
Pere Orga | 1f4447b | 2011-03-27 22:40:30 +0200 | [diff] [blame] | 19 | //usage:#define rpm2cpio_trivial_usage |
| 20 | //usage: "package.rpm" |
| 21 | //usage:#define rpm2cpio_full_usage "\n\n" |
| 22 | //usage: "Output a cpio archive of the rpm file" |
| 23 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 24 | #include "libbb.h" |
Denys Vlasenko | d184a72 | 2011-09-22 12:45:14 +0200 | [diff] [blame] | 25 | #include "bb_archive.h" |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 26 | #include "rpm.h" |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 27 | |
Denys Vlasenko | e6c483e | 2009-08-28 21:15:24 +0200 | [diff] [blame] | 28 | enum { rpm_fd = STDIN_FILENO }; |
| 29 | |
| 30 | static unsigned skip_header(void) |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 31 | { |
| 32 | struct rpm_header header; |
Denys Vlasenko | 0a130d5 | 2009-08-28 21:09:51 +0200 | [diff] [blame] | 33 | unsigned len; |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 34 | |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 35 | xread(rpm_fd, &header, sizeof(header)); |
| 36 | // if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC_STR, 3) != 0) { |
| 37 | // bb_error_msg_and_die("invalid RPM header magic"); |
| 38 | // } |
| 39 | // if (header.version != 1) { |
| 40 | // bb_error_msg_and_die("unsupported RPM header version"); |
| 41 | // } |
Denys Vlasenko | e6c483e | 2009-08-28 21:15:24 +0200 | [diff] [blame] | 42 | if (header.magic_and_ver != htonl(RPM_HEADER_MAGICnVER)) { |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 43 | bb_error_msg_and_die("invalid RPM header magic or unsupported version"); |
Denys Vlasenko | e6c483e | 2009-08-28 21:15:24 +0200 | [diff] [blame] | 44 | // ": %x != %x", header.magic_and_ver, htonl(RPM_HEADER_MAGICnVER)); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 45 | } |
Denys Vlasenko | 0a130d5 | 2009-08-28 21:09:51 +0200 | [diff] [blame] | 46 | |
| 47 | /* Seek past index entries, and past store */ |
| 48 | len = 16 * ntohl(header.entries) + ntohl(header.size); |
| 49 | seek_by_jump(rpm_fd, len); |
| 50 | |
| 51 | return sizeof(header) + len; |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /* No getopt required */ |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 55 | int rpm2cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 56 | int rpm2cpio_main(int argc UNUSED_PARAM, char **argv) |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 57 | { |
| 58 | struct rpm_lead lead; |
Denys Vlasenko | 0a130d5 | 2009-08-28 21:09:51 +0200 | [diff] [blame] | 59 | unsigned pos; |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 60 | |
Denys Vlasenko | e6c483e | 2009-08-28 21:15:24 +0200 | [diff] [blame] | 61 | if (argv[1]) { |
| 62 | xmove_fd(xopen(argv[1], O_RDONLY), rpm_fd); |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 63 | } |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 64 | xread(rpm_fd, &lead, sizeof(lead)); |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 65 | |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 66 | /* Just check the magic, the rest is irrelevant */ |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 67 | if (lead.magic != htonl(RPM_LEAD_MAGIC)) { |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 68 | bb_error_msg_and_die("invalid RPM magic"); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 71 | /* Skip the signature header, align to 8 bytes */ |
Denys Vlasenko | e6c483e | 2009-08-28 21:15:24 +0200 | [diff] [blame] | 72 | pos = skip_header(); |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 73 | seek_by_jump(rpm_fd, (-(int)pos) & 7); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 74 | |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 75 | /* Skip the main header */ |
Denys Vlasenko | e6c483e | 2009-08-28 21:15:24 +0200 | [diff] [blame] | 76 | skip_header(); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 77 | |
Denys Vlasenko | faac1d3 | 2012-03-06 16:33:42 +0100 | [diff] [blame] | 78 | //if (SEAMLESS_COMPRESSION) |
| 79 | // /* We need to know whether child (gzip/bzip/etc) exits abnormally */ |
| 80 | // signal(SIGCHLD, check_errors_in_children); |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 81 | |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 82 | /* This works, but doesn't report uncompress errors (they happen in child) */ |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 83 | setup_unzip_on_fd(rpm_fd, /*fail_if_not_detected:*/ 1); |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 84 | if (bb_copyfd_eof(rpm_fd, STDOUT_FILENO) < 0) |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 85 | bb_error_msg_and_die("error unpacking"); |
Glenn L McGrath | 26a0d9a | 2001-07-13 06:49:18 +0000 | [diff] [blame] | 86 | |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 87 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 88 | close(rpm_fd); |
| 89 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 90 | |
Denys Vlasenko | faac1d3 | 2012-03-06 16:33:42 +0100 | [diff] [blame] | 91 | if (SEAMLESS_COMPRESSION) { |
| 92 | check_errors_in_children(0); |
| 93 | return bb_got_signal; |
| 94 | } |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 95 | return EXIT_SUCCESS; |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 96 | } |