Magellan Linux

Contents of /trunk/mkinitrd-magellan/busybox/archival/rpm2cpio.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 532 - (show annotations) (download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 8 months ago) by niro
File MIME type: text/plain
File size: 2299 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd

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 "busybox.h"
10 #include "unarchive.h"
11
12 #define RPM_MAGIC "\355\253\356\333"
13 #define RPM_HEADER_MAGIC "\216\255\350"
14
15 struct rpm_lead {
16 unsigned char magic[4];
17 uint8_t major, minor;
18 uint16_t type;
19 uint16_t archnum;
20 char name[66];
21 uint16_t osnum;
22 uint16_t signature_type;
23 char reserved[16];
24 };
25
26 struct rpm_header {
27 char magic[3]; /* 3 byte magic: 0x8e 0xad 0xe8 */
28 uint8_t version; /* 1 byte version number */
29 uint32_t reserved; /* 4 bytes reserved */
30 uint32_t entries; /* Number of entries in header (4 bytes) */
31 uint32_t size; /* Size of store (4 bytes) */
32 };
33
34 static void skip_header(int rpm_fd)
35 {
36 struct rpm_header header;
37
38 xread(rpm_fd, &header, sizeof(struct rpm_header));
39 if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0) {
40 bb_error_msg_and_die("invalid RPM header magic"); /* Invalid magic */
41 }
42 if (header.version != 1) {
43 bb_error_msg_and_die("unsupported RPM header version"); /* This program only supports v1 headers */
44 }
45 header.entries = ntohl(header.entries);
46 header.size = ntohl(header.size);
47 lseek (rpm_fd, 16 * header.entries, SEEK_CUR); /* Seek past index entries */
48 lseek (rpm_fd, header.size, SEEK_CUR); /* Seek past store */
49 }
50
51 /* No getopt required */
52 int rpm2cpio_main(int argc, char **argv)
53 {
54 struct rpm_lead lead;
55 int rpm_fd;
56 unsigned char magic[2];
57
58 if (argc == 1) {
59 rpm_fd = STDIN_FILENO;
60 } else {
61 rpm_fd = xopen(argv[1], O_RDONLY);
62 }
63
64 xread(rpm_fd, &lead, sizeof(struct rpm_lead));
65 if (strncmp((char *) &lead.magic, RPM_MAGIC, 4) != 0) {
66 bb_error_msg_and_die("invalid RPM magic"); /* Just check the magic, the rest is irrelevant */
67 }
68
69 /* Skip the signature header */
70 skip_header(rpm_fd);
71 lseek(rpm_fd, (8 - (lseek(rpm_fd, 0, SEEK_CUR) % 8)) % 8, SEEK_CUR);
72
73 /* Skip the main header */
74 skip_header(rpm_fd);
75
76 xread(rpm_fd, &magic, 2);
77 if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
78 bb_error_msg_and_die("invalid gzip magic");
79 }
80
81 check_header_gzip(rpm_fd);
82 if (inflate_gunzip(rpm_fd, STDOUT_FILENO) < 0) {
83 bb_error_msg("error inflating");
84 }
85
86 close(rpm_fd);
87
88 return 0;
89 }