blob: 1c67dcc6e3fed99b856a6a7e8ce10aeab4f613c4 [file] [log] [blame]
Glenn L McGrathf8736d22001-06-26 01:19:34 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini rpm2cpio implementation for busybox
4 *
5 * Copyright (C) 2001 by Laurence Anderson
6 *
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrathf8736d22001-06-26 01:19:34 +00008 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +00009#include "libbb.h"
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000010#include "unarchive.h"
Denys Vlasenko27653ad2010-05-06 14:19:19 +000011#include "rpm.h"
Glenn L McGrathf8736d22001-06-26 01:19:34 +000012
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020013enum { rpm_fd = STDIN_FILENO };
14
15static unsigned skip_header(void)
Glenn L McGrathf8736d22001-06-26 01:19:34 +000016{
17 struct rpm_header header;
Denys Vlasenko0a130d52009-08-28 21:09:51 +020018 unsigned len;
Glenn L McGrathf8736d22001-06-26 01:19:34 +000019
Pascal Bellard7f214942009-08-28 06:20:33 +020020 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 Vlasenkoe6c483e2009-08-28 21:15:24 +020027 if (header.magic_and_ver != htonl(RPM_HEADER_MAGICnVER)) {
Pascal Bellard7f214942009-08-28 06:20:33 +020028 bb_error_msg_and_die("invalid RPM header magic or unsupported version");
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020029 // ": %x != %x", header.magic_and_ver, htonl(RPM_HEADER_MAGICnVER));
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000030 }
Denys Vlasenko0a130d52009-08-28 21:09:51 +020031
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 McGrathf8736d22001-06-26 01:19:34 +000037}
38
39/* No getopt required */
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000040int rpm2cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Pascal Bellard7f214942009-08-28 06:20:33 +020041int rpm2cpio_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrathf8736d22001-06-26 01:19:34 +000042{
43 struct rpm_lead lead;
Denys Vlasenko0a130d52009-08-28 21:09:51 +020044 unsigned pos;
Glenn L McGrathf8736d22001-06-26 01:19:34 +000045
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020046 if (argv[1]) {
47 xmove_fd(xopen(argv[1], O_RDONLY), rpm_fd);
Glenn L McGrathf8736d22001-06-26 01:19:34 +000048 }
Pascal Bellard7f214942009-08-28 06:20:33 +020049 xread(rpm_fd, &lead, sizeof(lead));
Glenn L McGrathf8736d22001-06-26 01:19:34 +000050
Pascal Bellard7f214942009-08-28 06:20:33 +020051 /* Just check the magic, the rest is irrelevant */
Denys Vlasenko27653ad2010-05-06 14:19:19 +000052 if (lead.magic != htonl(RPM_LEAD_MAGIC)) {
Pascal Bellard7f214942009-08-28 06:20:33 +020053 bb_error_msg_and_die("invalid RPM magic");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000054 }
55
Pascal Bellard7f214942009-08-28 06:20:33 +020056 /* Skip the signature header, align to 8 bytes */
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020057 pos = skip_header();
Denys Vlasenko27653ad2010-05-06 14:19:19 +000058 seek_by_jump(rpm_fd, (-(int)pos) & 7);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000059
Glenn L McGrathf8736d22001-06-26 01:19:34 +000060 /* Skip the main header */
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020061 skip_header();
Eric Andersenc7bda1c2004-03-15 08:29:22 +000062
Denys Vlasenko27653ad2010-05-06 14:19:19 +000063#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 Bellard7f214942009-08-28 06:20:33 +020067 bb_error_msg_and_die("error unpacking");
Denys Vlasenko27653ad2010-05-06 14:19:19 +000068#else
69 /* BLOAT */
70 {
Denys Vlasenko26b6ccf2010-06-02 14:14:48 +020071 union {
72 uint8_t b[4];
73 uint16_t b16[2];
74 uint32_t b32[1];
75 } magic;
Denys Vlasenko27653ad2010-05-06 14:19:19 +000076 IF_DESKTOP(long long) int FAST_FUNC (*unpack)(int src_fd, int dst_fd);
77
Denys Vlasenko26b6ccf2010-06-02 14:14:48 +020078 xread(rpm_fd, magic.b16, sizeof(magic.b16));
79 if (magic.b16[0] == GZIP_MAGIC) {
Denys Vlasenkoc88c1a02010-05-30 05:10:16 +020080 unpack = unpack_gz_stream;
81 } else
82 if (ENABLE_FEATURE_SEAMLESS_BZ2
Denys Vlasenko26b6ccf2010-06-02 14:14:48 +020083 && magic.b16[0] == BZIP2_MAGIC
Denys Vlasenkoc88c1a02010-05-30 05:10:16 +020084 ) {
Denys Vlasenko27653ad2010-05-06 14:19:19 +000085 unpack = unpack_bz2_stream;
Denys Vlasenkoc88c1a02010-05-30 05:10:16 +020086 } else
87 if (ENABLE_FEATURE_SEAMLESS_XZ
Denys Vlasenko26b6ccf2010-06-02 14:14:48 +020088 && magic.b16[0] == XZ_MAGIC1
Denys Vlasenkoc88c1a02010-05-30 05:10:16 +020089 ) {
90 /* .xz signature: 0xfd, '7', 'z', 'X', 'Z', 0x00 */
91 /* More info at: http://tukaani.org/xz/xz-file-format.txt */
Denys Vlasenko26b6ccf2010-06-02 14:14:48 +020092 xread(rpm_fd, magic.b32, sizeof(magic.b32));
93 if (magic.b32[0] != XZ_MAGIC2)
Denys Vlasenkoc88c1a02010-05-30 05:10:16 +020094 goto no_magic;
Denys Vlasenko26b6ccf2010-06-02 14:14:48 +020095 /* unpack_xz_stream wants fd at position 0 */
Denys Vlasenkoc88c1a02010-05-30 05:10:16 +020096 xlseek(rpm_fd, -6, SEEK_CUR);
97 unpack = unpack_xz_stream;
98 } else {
99 no_magic:
100 bb_error_msg_and_die("no gzip"
101 IF_FEATURE_SEAMLESS_BZ2("/bzip2")
102 IF_FEATURE_SEAMLESS_XZ("/xz")
103 " magic");
Denys Vlasenko27653ad2010-05-06 14:19:19 +0000104 }
Denys Vlasenko27653ad2010-05-06 14:19:19 +0000105 if (unpack(rpm_fd, STDOUT_FILENO) < 0)
106 bb_error_msg_and_die("error unpacking");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000107 }
Denys Vlasenko27653ad2010-05-06 14:19:19 +0000108#endif
Glenn L McGrath26a0d9a2001-07-13 06:49:18 +0000109
Pascal Bellard7f214942009-08-28 06:20:33 +0200110 if (ENABLE_FEATURE_CLEAN_UP) {
111 close(rpm_fd);
112 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000113
Glenn L McGrathf8736d22001-06-26 01:19:34 +0000114 return 0;
115}