blob: cd6786cc7a230809a2e40ea8e02f4a05e565603c [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
Denys Vlasenko66620fa2013-11-14 09:53:52 +010010//kbuild:lib-$(CONFIG_RPM2CPIO) += rpm2cpio.o
11
Pere Orga1f4447b2011-03-27 22:40:30 +020012//usage:#define rpm2cpio_trivial_usage
13//usage: "package.rpm"
14//usage:#define rpm2cpio_full_usage "\n\n"
15//usage: "Output a cpio archive of the rpm file"
16
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000017#include "libbb.h"
Denys Vlasenkod184a722011-09-22 12:45:14 +020018#include "bb_archive.h"
Denys Vlasenko27653ad2010-05-06 14:19:19 +000019#include "rpm.h"
Glenn L McGrathf8736d22001-06-26 01:19:34 +000020
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020021enum { rpm_fd = STDIN_FILENO };
22
23static unsigned skip_header(void)
Glenn L McGrathf8736d22001-06-26 01:19:34 +000024{
25 struct rpm_header header;
Denys Vlasenko0a130d52009-08-28 21:09:51 +020026 unsigned len;
Glenn L McGrathf8736d22001-06-26 01:19:34 +000027
Pascal Bellard7f214942009-08-28 06:20:33 +020028 xread(rpm_fd, &header, sizeof(header));
29// if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC_STR, 3) != 0) {
30// bb_error_msg_and_die("invalid RPM header magic");
31// }
32// if (header.version != 1) {
33// bb_error_msg_and_die("unsupported RPM header version");
34// }
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020035 if (header.magic_and_ver != htonl(RPM_HEADER_MAGICnVER)) {
Pascal Bellard7f214942009-08-28 06:20:33 +020036 bb_error_msg_and_die("invalid RPM header magic or unsupported version");
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020037 // ": %x != %x", header.magic_and_ver, htonl(RPM_HEADER_MAGICnVER));
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000038 }
Denys Vlasenko0a130d52009-08-28 21:09:51 +020039
40 /* Seek past index entries, and past store */
41 len = 16 * ntohl(header.entries) + ntohl(header.size);
42 seek_by_jump(rpm_fd, len);
43
44 return sizeof(header) + len;
Glenn L McGrathf8736d22001-06-26 01:19:34 +000045}
46
47/* No getopt required */
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000048int rpm2cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Pascal Bellard7f214942009-08-28 06:20:33 +020049int rpm2cpio_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrathf8736d22001-06-26 01:19:34 +000050{
51 struct rpm_lead lead;
Denys Vlasenko0a130d52009-08-28 21:09:51 +020052 unsigned pos;
Glenn L McGrathf8736d22001-06-26 01:19:34 +000053
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020054 if (argv[1]) {
55 xmove_fd(xopen(argv[1], O_RDONLY), rpm_fd);
Glenn L McGrathf8736d22001-06-26 01:19:34 +000056 }
Pascal Bellard7f214942009-08-28 06:20:33 +020057 xread(rpm_fd, &lead, sizeof(lead));
Glenn L McGrathf8736d22001-06-26 01:19:34 +000058
Pascal Bellard7f214942009-08-28 06:20:33 +020059 /* Just check the magic, the rest is irrelevant */
Denys Vlasenko27653ad2010-05-06 14:19:19 +000060 if (lead.magic != htonl(RPM_LEAD_MAGIC)) {
Pascal Bellard7f214942009-08-28 06:20:33 +020061 bb_error_msg_and_die("invalid RPM magic");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000062 }
63
Pascal Bellard7f214942009-08-28 06:20:33 +020064 /* Skip the signature header, align to 8 bytes */
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020065 pos = skip_header();
Denys Vlasenko27653ad2010-05-06 14:19:19 +000066 seek_by_jump(rpm_fd, (-(int)pos) & 7);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000067
Glenn L McGrathf8736d22001-06-26 01:19:34 +000068 /* Skip the main header */
Denys Vlasenkoe6c483e2009-08-28 21:15:24 +020069 skip_header();
Eric Andersenc7bda1c2004-03-15 08:29:22 +000070
Denys Vlasenkofaac1d32012-03-06 16:33:42 +010071 //if (SEAMLESS_COMPRESSION)
72 // /* We need to know whether child (gzip/bzip/etc) exits abnormally */
73 // signal(SIGCHLD, check_errors_in_children);
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010074
Denys Vlasenko27653ad2010-05-06 14:19:19 +000075 /* This works, but doesn't report uncompress errors (they happen in child) */
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010076 setup_unzip_on_fd(rpm_fd, /*fail_if_not_detected:*/ 1);
Denys Vlasenko27653ad2010-05-06 14:19:19 +000077 if (bb_copyfd_eof(rpm_fd, STDOUT_FILENO) < 0)
Pascal Bellard7f214942009-08-28 06:20:33 +020078 bb_error_msg_and_die("error unpacking");
Glenn L McGrath26a0d9a2001-07-13 06:49:18 +000079
Pascal Bellard7f214942009-08-28 06:20:33 +020080 if (ENABLE_FEATURE_CLEAN_UP) {
81 close(rpm_fd);
82 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000083
Denys Vlasenkofaac1d32012-03-06 16:33:42 +010084 if (SEAMLESS_COMPRESSION) {
85 check_errors_in_children(0);
86 return bb_got_signal;
87 }
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010088 return EXIT_SUCCESS;
Glenn L McGrathf8736d22001-06-26 01:19:34 +000089}