gen_build_files.sh uses bashism, document it
[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 tarball for details.
8  */
9 #include "libbb.h"
10 #include "unarchive.h"
11 #include "rpm.h"
12
13 enum { rpm_fd = STDIN_FILENO };
14
15 static unsigned skip_header(void)
16 {
17         struct rpm_header header;
18         unsigned len;
19
20         xread(rpm_fd, &header, sizeof(header));
21 //      if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC_STR, 3) != 0) {
22 //              bb_error_msg_and_die("invalid RPM header magic");
23 //      }
24 //      if (header.version != 1) {
25 //              bb_error_msg_and_die("unsupported RPM header version");
26 //      }
27         if (header.magic_and_ver != htonl(RPM_HEADER_MAGICnVER)) {
28                 bb_error_msg_and_die("invalid RPM header magic or unsupported version");
29                 // ": %x != %x", header.magic_and_ver, htonl(RPM_HEADER_MAGICnVER));
30         }
31
32         /* Seek past index entries, and past store */
33         len = 16 * ntohl(header.entries) + ntohl(header.size);
34         seek_by_jump(rpm_fd, len);
35
36         return sizeof(header) + len;
37 }
38
39 /* No getopt required */
40 int rpm2cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
41 int rpm2cpio_main(int argc UNUSED_PARAM, char **argv)
42 {
43         struct rpm_lead lead;
44         unsigned pos;
45
46         if (argv[1]) {
47                 xmove_fd(xopen(argv[1], O_RDONLY), rpm_fd);
48         }
49         xread(rpm_fd, &lead, sizeof(lead));
50
51         /* Just check the magic, the rest is irrelevant */
52         if (lead.magic != htonl(RPM_LEAD_MAGIC)) {
53                 bb_error_msg_and_die("invalid RPM magic");
54         }
55
56         /* Skip the signature header, align to 8 bytes */
57         pos = skip_header();
58         seek_by_jump(rpm_fd, (-(int)pos) & 7);
59
60         /* Skip the main header */
61         skip_header();
62
63 #if 0
64         /* This works, but doesn't report uncompress errors (they happen in child) */
65         setup_unzip_on_fd(rpm_fd /*fail_if_not_detected: 1*/);
66         if (bb_copyfd_eof(rpm_fd, STDOUT_FILENO) < 0)
67                 bb_error_msg_and_die("error unpacking");
68 #else
69         /* BLOAT */
70         {
71                 unsigned char magic[2];
72                 IF_DESKTOP(long long) int FAST_FUNC (*unpack)(int src_fd, int dst_fd);
73
74                 xread(rpm_fd, &magic, 2);
75                 unpack = unpack_gz_stream;
76                 if (magic[0] != 0x1f || magic[1] != 0x8b) {
77                         if (!ENABLE_FEATURE_SEAMLESS_BZ2
78                          || magic[0] != 'B' || magic[1] != 'Z'
79                         ) {
80                                 bb_error_msg_and_die("invalid gzip"
81                                                 IF_FEATURE_SEAMLESS_BZ2("/bzip2")
82                                                 " magic");
83                         }
84                         unpack = unpack_bz2_stream;
85                 }
86
87                 if (unpack(rpm_fd, STDOUT_FILENO) < 0)
88                         bb_error_msg_and_die("error unpacking");
89         }
90 #endif
91
92         if (ENABLE_FEATURE_CLEAN_UP) {
93                 close(rpm_fd);
94         }
95
96         return 0;
97 }