archival/*: move "applet:" snippets into .c files
[oweals/busybox.git] / archival / rpm2cpio.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini rpm2cpio implementation for busybox
4  *
5  * Copyright (C) 2001 by Laurence Anderson
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8  */
9
10 //applet:IF_RPM2CPIO(APPLET(rpm2cpio, BB_DIR_USR_BIN, BB_SUID_DROP))
11 //kbuild:lib-$(CONFIG_RPM2CPIO) += rpm2cpio.o
12
13 //usage:#define rpm2cpio_trivial_usage
14 //usage:       "package.rpm"
15 //usage:#define rpm2cpio_full_usage "\n\n"
16 //usage:       "Output a cpio archive of the rpm file"
17
18 #include "libbb.h"
19 #include "bb_archive.h"
20 #include "rpm.h"
21
22 enum { rpm_fd = STDIN_FILENO };
23
24 static unsigned skip_header(void)
25 {
26         struct rpm_header header;
27         unsigned len;
28
29         xread(rpm_fd, &header, sizeof(header));
30 //      if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC_STR, 3) != 0) {
31 //              bb_error_msg_and_die("invalid RPM header magic");
32 //      }
33 //      if (header.version != 1) {
34 //              bb_error_msg_and_die("unsupported RPM header version");
35 //      }
36         if (header.magic_and_ver != htonl(RPM_HEADER_MAGICnVER)) {
37                 bb_error_msg_and_die("invalid RPM header magic or unsupported version");
38                 // ": %x != %x", header.magic_and_ver, htonl(RPM_HEADER_MAGICnVER));
39         }
40
41         /* Seek past index entries, and past store */
42         len = 16 * ntohl(header.entries) + ntohl(header.size);
43         seek_by_jump(rpm_fd, len);
44
45         return sizeof(header) + len;
46 }
47
48 /* No getopt required */
49 int rpm2cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
50 int rpm2cpio_main(int argc UNUSED_PARAM, char **argv)
51 {
52         struct rpm_lead lead;
53         unsigned pos;
54
55         if (argv[1]) {
56                 xmove_fd(xopen(argv[1], O_RDONLY), rpm_fd);
57         }
58         xread(rpm_fd, &lead, sizeof(lead));
59
60         /* Just check the magic, the rest is irrelevant */
61         if (lead.magic != htonl(RPM_LEAD_MAGIC)) {
62                 bb_error_msg_and_die("invalid RPM magic");
63         }
64
65         /* Skip the signature header, align to 8 bytes */
66         pos = skip_header();
67         seek_by_jump(rpm_fd, (-(int)pos) & 7);
68
69         /* Skip the main header */
70         skip_header();
71
72         //if (SEAMLESS_COMPRESSION)
73         //      /* We need to know whether child (gzip/bzip/etc) exits abnormally */
74         //      signal(SIGCHLD, check_errors_in_children);
75
76         /* This works, but doesn't report uncompress errors (they happen in child) */
77         setup_unzip_on_fd(rpm_fd, /*fail_if_not_detected:*/ 1);
78         if (bb_copyfd_eof(rpm_fd, STDOUT_FILENO) < 0)
79                 bb_error_msg_and_die("error unpacking");
80
81         if (ENABLE_FEATURE_CLEAN_UP) {
82                 close(rpm_fd);
83         }
84
85         if (SEAMLESS_COMPRESSION) {
86                 check_errors_in_children(0);
87                 return bb_got_signal;
88         }
89         return EXIT_SUCCESS;
90 }