blob: ff4a0d1b065b1e36cc542dc255e8ec26b14e44c0 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrathf8736d22001-06-26 01:19:34 +00008 */
Pere Orga1f4447b2011-03-27 22:40:30 +02009
10//usage:#define rpm2cpio_trivial_usage
11//usage: "package.rpm"
12//usage:#define rpm2cpio_full_usage "\n\n"
13//usage: "Output a cpio archive of the rpm file"
14
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000015#include "libbb.h"
Denys Vlasenko833d4e72010-11-03 02:38:31 +010016#include "archive.h"
Denys Vlasenko27653ad2010-05-06 14:19:19 +000017#include "rpm.h"
Glenn L McGrathf8736d22001-06-26 01:19:34 +000018
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020019enum { rpm_fd = STDIN_FILENO };
20
21static unsigned skip_header(void)
Glenn L McGrathf8736d22001-06-26 01:19:34 +000022{
23 struct rpm_header header;
Denys Vlasenko0a130d52009-08-28 21:09:51 +020024 unsigned len;
Glenn L McGrathf8736d22001-06-26 01:19:34 +000025
Pascal Bellard7f214942009-08-28 06:20:33 +020026 xread(rpm_fd, &header, sizeof(header));
27// if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC_STR, 3) != 0) {
28// bb_error_msg_and_die("invalid RPM header magic");
29// }
30// if (header.version != 1) {
31// bb_error_msg_and_die("unsupported RPM header version");
32// }
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020033 if (header.magic_and_ver != htonl(RPM_HEADER_MAGICnVER)) {
Pascal Bellard7f214942009-08-28 06:20:33 +020034 bb_error_msg_and_die("invalid RPM header magic or unsupported version");
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020035 // ": %x != %x", header.magic_and_ver, htonl(RPM_HEADER_MAGICnVER));
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000036 }
Denys Vlasenko0a130d52009-08-28 21:09:51 +020037
38 /* Seek past index entries, and past store */
39 len = 16 * ntohl(header.entries) + ntohl(header.size);
40 seek_by_jump(rpm_fd, len);
41
42 return sizeof(header) + len;
Glenn L McGrathf8736d22001-06-26 01:19:34 +000043}
44
45/* No getopt required */
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000046int rpm2cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Pascal Bellard7f214942009-08-28 06:20:33 +020047int rpm2cpio_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrathf8736d22001-06-26 01:19:34 +000048{
49 struct rpm_lead lead;
Denys Vlasenko0a130d52009-08-28 21:09:51 +020050 unsigned pos;
Glenn L McGrathf8736d22001-06-26 01:19:34 +000051
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020052 if (argv[1]) {
53 xmove_fd(xopen(argv[1], O_RDONLY), rpm_fd);
Glenn L McGrathf8736d22001-06-26 01:19:34 +000054 }
Pascal Bellard7f214942009-08-28 06:20:33 +020055 xread(rpm_fd, &lead, sizeof(lead));
Glenn L McGrathf8736d22001-06-26 01:19:34 +000056
Pascal Bellard7f214942009-08-28 06:20:33 +020057 /* Just check the magic, the rest is irrelevant */
Denys Vlasenko27653ad2010-05-06 14:19:19 +000058 if (lead.magic != htonl(RPM_LEAD_MAGIC)) {
Pascal Bellard7f214942009-08-28 06:20:33 +020059 bb_error_msg_and_die("invalid RPM magic");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000060 }
61
Pascal Bellard7f214942009-08-28 06:20:33 +020062 /* Skip the signature header, align to 8 bytes */
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020063 pos = skip_header();
Denys Vlasenko27653ad2010-05-06 14:19:19 +000064 seek_by_jump(rpm_fd, (-(int)pos) & 7);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000065
Glenn L McGrathf8736d22001-06-26 01:19:34 +000066 /* Skip the main header */
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020067 skip_header();
Eric Andersenc7bda1c2004-03-15 08:29:22 +000068
Denys Vlasenko27653ad2010-05-06 14:19:19 +000069#if 0
70 /* This works, but doesn't report uncompress errors (they happen in child) */
71 setup_unzip_on_fd(rpm_fd /*fail_if_not_detected: 1*/);
72 if (bb_copyfd_eof(rpm_fd, STDOUT_FILENO) < 0)
Pascal Bellard7f214942009-08-28 06:20:33 +020073 bb_error_msg_and_die("error unpacking");
Denys Vlasenko27653ad2010-05-06 14:19:19 +000074#else
75 /* BLOAT */
76 {
Denys Vlasenko26b6ccf2010-06-02 14:14:48 +020077 union {
78 uint8_t b[4];
79 uint16_t b16[2];
80 uint32_t b32[1];
81 } magic;
Denys Vlasenko27653ad2010-05-06 14:19:19 +000082 IF_DESKTOP(long long) int FAST_FUNC (*unpack)(int src_fd, int dst_fd);
83
Denys Vlasenko894fa0a2010-06-26 05:01:16 +020084 xread(rpm_fd, magic.b16, sizeof(magic.b16[0]));
Denys Vlasenko26b6ccf2010-06-02 14:14:48 +020085 if (magic.b16[0] == GZIP_MAGIC) {
Denys Vlasenkoc88c1a02010-05-30 05:10:16 +020086 unpack = unpack_gz_stream;
87 } else
88 if (ENABLE_FEATURE_SEAMLESS_BZ2
Denys Vlasenko26b6ccf2010-06-02 14:14:48 +020089 && magic.b16[0] == BZIP2_MAGIC
Denys Vlasenkoc88c1a02010-05-30 05:10:16 +020090 ) {
Denys Vlasenko27653ad2010-05-06 14:19:19 +000091 unpack = unpack_bz2_stream;
Denys Vlasenkoc88c1a02010-05-30 05:10:16 +020092 } else
93 if (ENABLE_FEATURE_SEAMLESS_XZ
Denys Vlasenko26b6ccf2010-06-02 14:14:48 +020094 && magic.b16[0] == XZ_MAGIC1
Denys Vlasenkoc88c1a02010-05-30 05:10:16 +020095 ) {
Denys Vlasenko894fa0a2010-06-26 05:01:16 +020096 xread(rpm_fd, magic.b32, sizeof(magic.b32[0]));
Denys Vlasenko26b6ccf2010-06-02 14:14:48 +020097 if (magic.b32[0] != XZ_MAGIC2)
Denys Vlasenkoc88c1a02010-05-30 05:10:16 +020098 goto no_magic;
Denys Vlasenko45f66162010-07-01 05:12:28 +020099 /* unpack_xz_stream wants fd at position 6, no need to seek */
100 //xlseek(rpm_fd, -6, SEEK_CUR);
Denys Vlasenkoc88c1a02010-05-30 05:10:16 +0200101 unpack = unpack_xz_stream;
102 } else {
103 no_magic:
104 bb_error_msg_and_die("no gzip"
105 IF_FEATURE_SEAMLESS_BZ2("/bzip2")
106 IF_FEATURE_SEAMLESS_XZ("/xz")
107 " magic");
Denys Vlasenko27653ad2010-05-06 14:19:19 +0000108 }
Denys Vlasenko27653ad2010-05-06 14:19:19 +0000109 if (unpack(rpm_fd, STDOUT_FILENO) < 0)
110 bb_error_msg_and_die("error unpacking");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000111 }
Denys Vlasenko27653ad2010-05-06 14:19:19 +0000112#endif
Glenn L McGrath26a0d9a2001-07-13 06:49:18 +0000113
Pascal Bellard7f214942009-08-28 06:20:33 +0200114 if (ENABLE_FEATURE_CLEAN_UP) {
115 close(rpm_fd);
116 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000117
Glenn L McGrathf8736d22001-06-26 01:19:34 +0000118 return 0;
119}