archival/*: move "kbuild:" snippets into .c files
[platform/upstream/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 //kbuild:lib-$(CONFIG_RPM2CPIO) += rpm2cpio.o
11
12 //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
17 #include "libbb.h"
18 #include "bb_archive.h"
19 #include "rpm.h"
20
21 enum { rpm_fd = STDIN_FILENO };
22
23 static unsigned skip_header(void)
24 {
25         struct rpm_header header;
26         unsigned len;
27
28         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 //      }
35         if (header.magic_and_ver != htonl(RPM_HEADER_MAGICnVER)) {
36                 bb_error_msg_and_die("invalid RPM header magic or unsupported version");
37                 // ": %x != %x", header.magic_and_ver, htonl(RPM_HEADER_MAGICnVER));
38         }
39
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;
45 }
46
47 /* No getopt required */
48 int rpm2cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
49 int rpm2cpio_main(int argc UNUSED_PARAM, char **argv)
50 {
51         struct rpm_lead lead;
52         unsigned pos;
53
54         if (argv[1]) {
55                 xmove_fd(xopen(argv[1], O_RDONLY), rpm_fd);
56         }
57         xread(rpm_fd, &lead, sizeof(lead));
58
59         /* Just check the magic, the rest is irrelevant */
60         if (lead.magic != htonl(RPM_LEAD_MAGIC)) {
61                 bb_error_msg_and_die("invalid RPM magic");
62         }
63
64         /* Skip the signature header, align to 8 bytes */
65         pos = skip_header();
66         seek_by_jump(rpm_fd, (-(int)pos) & 7);
67
68         /* Skip the main header */
69         skip_header();
70
71         //if (SEAMLESS_COMPRESSION)
72         //      /* We need to know whether child (gzip/bzip/etc) exits abnormally */
73         //      signal(SIGCHLD, check_errors_in_children);
74
75         /* This works, but doesn't report uncompress errors (they happen in child) */
76         setup_unzip_on_fd(rpm_fd, /*fail_if_not_detected:*/ 1);
77         if (bb_copyfd_eof(rpm_fd, STDOUT_FILENO) < 0)
78                 bb_error_msg_and_die("error unpacking");
79
80         if (ENABLE_FEATURE_CLEAN_UP) {
81                 close(rpm_fd);
82         }
83
84         if (SEAMLESS_COMPRESSION) {
85                 check_errors_in_children(0);
86                 return bb_got_signal;
87         }
88         return EXIT_SUCCESS;
89 }