blob: 7057570f513316e638cad051e59b40e2632a04c2 [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
maxwen27116ba2015-08-14 21:41:28 +020010//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
16//applet:IF_RPM2CPIO(APPLET(rpm2cpio, BB_DIR_USR_BIN, BB_SUID_DROP))
17//kbuild:lib-$(CONFIG_RPM2CPIO) += rpm2cpio.o
18
Pere Orga1f4447b2011-03-27 22:40:30 +020019//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 Vlasenkob6adbf12007-05-26 19:00:18 +000024#include "libbb.h"
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020025#include "bb_archive.h"
Denys Vlasenko27653ad2010-05-06 14:19:19 +000026#include "rpm.h"
Glenn L McGrathf8736d22001-06-26 01:19:34 +000027
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020028enum { rpm_fd = STDIN_FILENO };
29
30static unsigned skip_header(void)
Glenn L McGrathf8736d22001-06-26 01:19:34 +000031{
32 struct rpm_header header;
Denys Vlasenko0a130d52009-08-28 21:09:51 +020033 unsigned len;
Glenn L McGrathf8736d22001-06-26 01:19:34 +000034
Pascal Bellard7f214942009-08-28 06:20:33 +020035 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 Vlasenkoe6c483e2009-08-28 21:15:24 +020042 if (header.magic_and_ver != htonl(RPM_HEADER_MAGICnVER)) {
Pascal Bellard7f214942009-08-28 06:20:33 +020043 bb_error_msg_and_die("invalid RPM header magic or unsupported version");
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020044 // ": %x != %x", header.magic_and_ver, htonl(RPM_HEADER_MAGICnVER));
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000045 }
Denys Vlasenko0a130d52009-08-28 21:09:51 +020046
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 McGrathf8736d22001-06-26 01:19:34 +000052}
53
54/* No getopt required */
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000055int rpm2cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Pascal Bellard7f214942009-08-28 06:20:33 +020056int rpm2cpio_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrathf8736d22001-06-26 01:19:34 +000057{
58 struct rpm_lead lead;
Denys Vlasenko0a130d52009-08-28 21:09:51 +020059 unsigned pos;
Glenn L McGrathf8736d22001-06-26 01:19:34 +000060
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020061 if (argv[1]) {
62 xmove_fd(xopen(argv[1], O_RDONLY), rpm_fd);
Glenn L McGrathf8736d22001-06-26 01:19:34 +000063 }
Pascal Bellard7f214942009-08-28 06:20:33 +020064 xread(rpm_fd, &lead, sizeof(lead));
Glenn L McGrathf8736d22001-06-26 01:19:34 +000065
Pascal Bellard7f214942009-08-28 06:20:33 +020066 /* Just check the magic, the rest is irrelevant */
Denys Vlasenko27653ad2010-05-06 14:19:19 +000067 if (lead.magic != htonl(RPM_LEAD_MAGIC)) {
Pascal Bellard7f214942009-08-28 06:20:33 +020068 bb_error_msg_and_die("invalid RPM magic");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000069 }
70
Pascal Bellard7f214942009-08-28 06:20:33 +020071 /* Skip the signature header, align to 8 bytes */
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020072 pos = skip_header();
Denys Vlasenko27653ad2010-05-06 14:19:19 +000073 seek_by_jump(rpm_fd, (-(int)pos) & 7);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000074
Glenn L McGrathf8736d22001-06-26 01:19:34 +000075 /* Skip the main header */
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020076 skip_header();
Eric Andersenc7bda1c2004-03-15 08:29:22 +000077
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020078 //if (SEAMLESS_COMPRESSION)
79 // /* We need to know whether child (gzip/bzip/etc) exits abnormally */
80 // signal(SIGCHLD, check_errors_in_children);
81
Denys Vlasenko27653ad2010-05-06 14:19:19 +000082 /* This works, but doesn't report uncompress errors (they happen in child) */
maxwen27116ba2015-08-14 21:41:28 +020083 setup_unzip_on_fd(rpm_fd, /*fail_if_not_compressed:*/ 1);
Denys Vlasenko27653ad2010-05-06 14:19:19 +000084 if (bb_copyfd_eof(rpm_fd, STDOUT_FILENO) < 0)
Pascal Bellard7f214942009-08-28 06:20:33 +020085 bb_error_msg_and_die("error unpacking");
Glenn L McGrath26a0d9a2001-07-13 06:49:18 +000086
Pascal Bellard7f214942009-08-28 06:20:33 +020087 if (ENABLE_FEATURE_CLEAN_UP) {
88 close(rpm_fd);
89 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000090
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020091 if (SEAMLESS_COMPRESSION) {
92 check_errors_in_children(0);
93 return bb_got_signal;
94 }
95 return EXIT_SUCCESS;
Glenn L McGrathf8736d22001-06-26 01:19:34 +000096}