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 | */ |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 10 | #include "unarchive.h" |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 11 | #include "rpm.h" |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 12 | |
Denys Vlasenko | e6c483e | 2009-08-28 21:15:24 +0200 | [diff] [blame] | 13 | enum { rpm_fd = STDIN_FILENO }; |
| 14 | |
| 15 | static unsigned skip_header(void) |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 16 | { |
| 17 | struct rpm_header header; |
Denys Vlasenko | 0a130d5 | 2009-08-28 21:09:51 +0200 | [diff] [blame] | 18 | unsigned len; |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 19 | |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 20 | xread(rpm_fd, &header, sizeof(header)); |
| 21 | // if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC_STR, 3) != 0) { |
| 22 | // bb_error_msg_and_die("invalid RPM header magic"); |
| 23 | // } |
| 24 | // if (header.version != 1) { |
| 25 | // bb_error_msg_and_die("unsupported RPM header version"); |
| 26 | // } |
Denys Vlasenko | e6c483e | 2009-08-28 21:15:24 +0200 | [diff] [blame] | 27 | if (header.magic_and_ver != htonl(RPM_HEADER_MAGICnVER)) { |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 28 | bb_error_msg_and_die("invalid RPM header magic or unsupported version"); |
Denys Vlasenko | e6c483e | 2009-08-28 21:15:24 +0200 | [diff] [blame] | 29 | // ": %x != %x", header.magic_and_ver, htonl(RPM_HEADER_MAGICnVER)); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 30 | } |
Denys Vlasenko | 0a130d5 | 2009-08-28 21:09:51 +0200 | [diff] [blame] | 31 | |
| 32 | /* Seek past index entries, and past store */ |
| 33 | len = 16 * ntohl(header.entries) + ntohl(header.size); |
| 34 | seek_by_jump(rpm_fd, len); |
| 35 | |
| 36 | return sizeof(header) + len; |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | /* No getopt required */ |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 40 | int rpm2cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 41 | int rpm2cpio_main(int argc UNUSED_PARAM, char **argv) |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 42 | { |
| 43 | struct rpm_lead lead; |
Denys Vlasenko | 0a130d5 | 2009-08-28 21:09:51 +0200 | [diff] [blame] | 44 | unsigned pos; |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 45 | |
Denys Vlasenko | e6c483e | 2009-08-28 21:15:24 +0200 | [diff] [blame] | 46 | if (argv[1]) { |
| 47 | xmove_fd(xopen(argv[1], O_RDONLY), rpm_fd); |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 48 | } |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 49 | xread(rpm_fd, &lead, sizeof(lead)); |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 50 | |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 51 | /* Just check the magic, the rest is irrelevant */ |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 52 | if (lead.magic != htonl(RPM_LEAD_MAGIC)) { |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 53 | bb_error_msg_and_die("invalid RPM magic"); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 56 | /* Skip the signature header, align to 8 bytes */ |
Denys Vlasenko | e6c483e | 2009-08-28 21:15:24 +0200 | [diff] [blame] | 57 | pos = skip_header(); |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 58 | seek_by_jump(rpm_fd, (-(int)pos) & 7); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 59 | |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 60 | /* Skip the main header */ |
Denys Vlasenko | e6c483e | 2009-08-28 21:15:24 +0200 | [diff] [blame] | 61 | skip_header(); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 62 | |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 63 | #if 0 |
| 64 | /* This works, but doesn't report uncompress errors (they happen in child) */ |
| 65 | setup_unzip_on_fd(rpm_fd /*fail_if_not_detected: 1*/); |
| 66 | if (bb_copyfd_eof(rpm_fd, STDOUT_FILENO) < 0) |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 67 | bb_error_msg_and_die("error unpacking"); |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 68 | #else |
| 69 | /* BLOAT */ |
| 70 | { |
Denys Vlasenko | 26b6ccf | 2010-06-02 14:14:48 +0200 | [diff] [blame] | 71 | union { |
| 72 | uint8_t b[4]; |
| 73 | uint16_t b16[2]; |
| 74 | uint32_t b32[1]; |
| 75 | } magic; |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 76 | IF_DESKTOP(long long) int FAST_FUNC (*unpack)(int src_fd, int dst_fd); |
| 77 | |
Denys Vlasenko | 894fa0a | 2010-06-26 05:01:16 +0200 | [diff] [blame] | 78 | xread(rpm_fd, magic.b16, sizeof(magic.b16[0])); |
Denys Vlasenko | 26b6ccf | 2010-06-02 14:14:48 +0200 | [diff] [blame] | 79 | if (magic.b16[0] == GZIP_MAGIC) { |
Denys Vlasenko | c88c1a0 | 2010-05-30 05:10:16 +0200 | [diff] [blame] | 80 | unpack = unpack_gz_stream; |
| 81 | } else |
| 82 | if (ENABLE_FEATURE_SEAMLESS_BZ2 |
Denys Vlasenko | 26b6ccf | 2010-06-02 14:14:48 +0200 | [diff] [blame] | 83 | && magic.b16[0] == BZIP2_MAGIC |
Denys Vlasenko | c88c1a0 | 2010-05-30 05:10:16 +0200 | [diff] [blame] | 84 | ) { |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 85 | unpack = unpack_bz2_stream; |
Denys Vlasenko | c88c1a0 | 2010-05-30 05:10:16 +0200 | [diff] [blame] | 86 | } else |
| 87 | if (ENABLE_FEATURE_SEAMLESS_XZ |
Denys Vlasenko | 26b6ccf | 2010-06-02 14:14:48 +0200 | [diff] [blame] | 88 | && magic.b16[0] == XZ_MAGIC1 |
Denys Vlasenko | c88c1a0 | 2010-05-30 05:10:16 +0200 | [diff] [blame] | 89 | ) { |
Denys Vlasenko | 894fa0a | 2010-06-26 05:01:16 +0200 | [diff] [blame] | 90 | xread(rpm_fd, magic.b32, sizeof(magic.b32[0])); |
Denys Vlasenko | 26b6ccf | 2010-06-02 14:14:48 +0200 | [diff] [blame] | 91 | if (magic.b32[0] != XZ_MAGIC2) |
Denys Vlasenko | c88c1a0 | 2010-05-30 05:10:16 +0200 | [diff] [blame] | 92 | goto no_magic; |
Denys Vlasenko | 45f6616 | 2010-07-01 05:12:28 +0200 | [diff] [blame] | 93 | /* unpack_xz_stream wants fd at position 6, no need to seek */ |
| 94 | //xlseek(rpm_fd, -6, SEEK_CUR); |
Denys Vlasenko | c88c1a0 | 2010-05-30 05:10:16 +0200 | [diff] [blame] | 95 | unpack = unpack_xz_stream; |
| 96 | } else { |
| 97 | no_magic: |
| 98 | bb_error_msg_and_die("no gzip" |
| 99 | IF_FEATURE_SEAMLESS_BZ2("/bzip2") |
| 100 | IF_FEATURE_SEAMLESS_XZ("/xz") |
| 101 | " magic"); |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 102 | } |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 103 | if (unpack(rpm_fd, STDOUT_FILENO) < 0) |
| 104 | bb_error_msg_and_die("error unpacking"); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 105 | } |
Denys Vlasenko | 27653ad | 2010-05-06 14:19:19 +0000 | [diff] [blame] | 106 | #endif |
Glenn L McGrath | 26a0d9a | 2001-07-13 06:49:18 +0000 | [diff] [blame] | 107 | |
Pascal Bellard | 7f21494 | 2009-08-28 06:20:33 +0200 | [diff] [blame] | 108 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 109 | close(rpm_fd); |
| 110 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 111 | |
Glenn L McGrath | f8736d2 | 2001-06-26 01:19:34 +0000 | [diff] [blame] | 112 | return 0; |
| 113 | } |