update seamless uncompression code
[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 //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
15 #include "libbb.h"
16 #include "bb_archive.h"
17 #include "rpm.h"
18
19 enum { rpm_fd = STDIN_FILENO };
20
21 static unsigned skip_header(void)
22 {
23         struct rpm_header header;
24         unsigned len;
25
26         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 //      }
33         if (header.magic_and_ver != htonl(RPM_HEADER_MAGICnVER)) {
34                 bb_error_msg_and_die("invalid RPM header magic or unsupported version");
35                 // ": %x != %x", header.magic_and_ver, htonl(RPM_HEADER_MAGICnVER));
36         }
37
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;
43 }
44
45 #if SEAMLESS_COMPRESSION
46 static void handle_SIGCHLD(int signo UNUSED_PARAM)
47 {
48         int status;
49
50         /* Wait for any child without blocking */
51         for (;;) {
52                 if (wait_any_nohang(&status) < 0)
53                         /* wait failed?! I'm confused... */
54                         return;
55                 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
56                         /* this child exited with 0 */
57                         continue;
58                 /* Cannot happen?
59                 if (!WIFSIGNALED(status) && !WIFEXITED(status)) ???; */
60                 bb_got_signal = 1;
61         }
62 }
63 #endif
64
65 /* No getopt required */
66 int rpm2cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
67 int rpm2cpio_main(int argc UNUSED_PARAM, char **argv)
68 {
69         struct rpm_lead lead;
70         unsigned pos;
71
72         if (argv[1]) {
73                 xmove_fd(xopen(argv[1], O_RDONLY), rpm_fd);
74         }
75         xread(rpm_fd, &lead, sizeof(lead));
76
77         /* Just check the magic, the rest is irrelevant */
78         if (lead.magic != htonl(RPM_LEAD_MAGIC)) {
79                 bb_error_msg_and_die("invalid RPM magic");
80         }
81
82         /* Skip the signature header, align to 8 bytes */
83         pos = skip_header();
84         seek_by_jump(rpm_fd, (-(int)pos) & 7);
85
86         /* Skip the main header */
87         skip_header();
88
89 #if SEAMLESS_COMPRESSION
90         /* We need to know whether child (gzip/bzip/etc) exits abnormally */
91         signal(SIGCHLD, handle_SIGCHLD);
92 #endif
93
94         /* This works, but doesn't report uncompress errors (they happen in child) */
95         setup_unzip_on_fd(rpm_fd, /*fail_if_not_detected:*/ 1);
96         if (bb_copyfd_eof(rpm_fd, STDOUT_FILENO) < 0)
97                 bb_error_msg_and_die("error unpacking");
98
99         if (ENABLE_FEATURE_CLEAN_UP) {
100                 close(rpm_fd);
101         }
102
103 #if SEAMLESS_COMPRESSION
104         return bb_got_signal;
105 #else
106         return EXIT_SUCCESS;
107 #endif
108 }