blob: 0f36077c12b09e7eecd2689fb72d9b8c4e850fd2 [file] [log] [blame]
Eric Andersencff3fe32000-09-20 19:22:26 +00001/*
Eric Andersen4bfb6b72000-11-29 21:39:02 +00002 * rpmunpack for busybox
Eric Andersencff3fe32000-09-20 19:22:26 +00003 *
4 * rpmunpack.c - Utility program to unpack an RPM archive
5 *
6 * Gero Kuhlmann <gero@gkminix.han.de> 1998
7 *
8 * This program is public domain software; you can do whatever you like
9 * with this source, including modifying and redistributing it.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 */
15
Eric Andersen3570a342000-09-25 21:45:58 +000016#include "busybox.h"
Eric Andersencff3fe32000-09-20 19:22:26 +000017#include <fcntl.h>
Eric Andersencff3fe32000-09-20 19:22:26 +000018
19/*
20 * Some general definitions
21 */
Mark Whitley59ab0252001-01-23 22:30:04 +000022
Eric Andersencff3fe32000-09-20 19:22:26 +000023#define RPM_MAGIC "\355\253\356\333"
24#define GZ_MAGIC_1 '\037'
25#define GZ_MAGIC_2 '\213'
26
27/*
28 * Global variables
29 */
Eric Andersencff3fe32000-09-20 19:22:26 +000030static char *progname;
31static int infile, outfile;
32
33/*
34 * Read a specified number of bytes from input file
35 */
Mark Whitley59ab0252001-01-23 22:30:04 +000036static void myread(int num, char *buffer)
Eric Andersencff3fe32000-09-20 19:22:26 +000037{
38 int err;
39
40 if ((err = read(infile, buffer, num)) != num) {
41 if (err < 0)
Matt Kraaia9819b22000-12-22 01:48:07 +000042 perror_msg_and_die(progname);
Eric Andersencff3fe32000-09-20 19:22:26 +000043 else
Matt Kraaia9819b22000-12-22 01:48:07 +000044 error_msg_and_die("Unexpected end of input file!\n");
Eric Andersencff3fe32000-09-20 19:22:26 +000045 }
46}
47
48/*
49 * Main program
50 */
Eric Andersen4bfb6b72000-11-29 21:39:02 +000051int rpmunpack_main(int argc, char **argv)
Eric Andersencff3fe32000-09-20 19:22:26 +000052{
53 int len, status = 0;
Eric Andersend35c2152001-01-25 23:49:09 +000054 RESERVE_BB_BUFFER(buffer, BUFSIZ);
Eric Andersencff3fe32000-09-20 19:22:26 +000055
56 /* Get our own program name */
57 if ((progname = strrchr(argv[0], '/')) == NULL)
58 progname = argv[0];
59 else
60 progname++;
61
62 /* Check for command line parameters */
63 if (argc>=2 && *argv[1]=='-') {
Eric Andersen4bfb6b72000-11-29 21:39:02 +000064 usage(rpmunpack_usage);
Eric Andersencff3fe32000-09-20 19:22:26 +000065 }
66
67 /* Open input file */
68 if (argc == 1)
69 infile = STDIN_FILENO;
Matt Kraaia9819b22000-12-22 01:48:07 +000070 else if ((infile = open(argv[1], O_RDONLY)) < 0)
71 perror_msg_and_die("%s", argv[1]);
Eric Andersencff3fe32000-09-20 19:22:26 +000072
73 /* Read magic ID and output filename */
Mark Whitley59ab0252001-01-23 22:30:04 +000074 myread(4, buffer);
Eric Andersencff3fe32000-09-20 19:22:26 +000075 if (strncmp(buffer, RPM_MAGIC, 4)) {
Eric Andersen4bfb6b72000-11-29 21:39:02 +000076 fprintf(stderr, "Input file is not in RPM format!\n");
Eric Andersencff3fe32000-09-20 19:22:26 +000077 exit(1);
78 }
Mark Whitley59ab0252001-01-23 22:30:04 +000079 myread(6, buffer); /* Skip flags */
80 myread(64, buffer);
Eric Andersencff3fe32000-09-20 19:22:26 +000081 buffer[64] = '\0';
82
83 /* Open output file */
84 strcat(buffer, ".cpio.gz");
85 if (infile == STDIN_FILENO)
86 outfile = STDOUT_FILENO;
Matt Kraaia9819b22000-12-22 01:48:07 +000087 else if ((outfile = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0)
88 perror_msg_and_die("%s", buffer);
Eric Andersencff3fe32000-09-20 19:22:26 +000089
90 /*
91 * Now search for the GZIP signature. This is rather awkward, but I don't
92 * know any other way how to find out the exact starting position of the
93 * archive within the input file. There are a couple of data structures
94 * and texts (obviously descriptions, installation shell scripts etc.)
95 * coming before the archive, but even they start at different offsets
96 * with different RPM files. However, it looks like the GZIP signature
97 * never appears before offset 0x200, so we skip these first couple of
98 * bytes to make the signature scan a little more reliable.
99 */
Mark Whitley59ab0252001-01-23 22:30:04 +0000100 myread(0x200 - 74, buffer);
Eric Andersencff3fe32000-09-20 19:22:26 +0000101 while (status < 2) {
Mark Whitley59ab0252001-01-23 22:30:04 +0000102 myread(1, buffer);
Eric Andersencff3fe32000-09-20 19:22:26 +0000103 if (status == 0 && buffer[0] == GZ_MAGIC_1)
104 status++;
105 else if (status == 1 && buffer[0] == GZ_MAGIC_2)
106 status++;
107 else
108 status = 0;
109 }
110 buffer[0] = GZ_MAGIC_1;
111 buffer[1] = GZ_MAGIC_2;
Matt Kraaia9819b22000-12-22 01:48:07 +0000112 if (write(outfile, buffer, 2) < 0)
113 perror_msg_and_die("write");
Eric Andersencff3fe32000-09-20 19:22:26 +0000114
115 /* Now simply copy the GZIP archive into the output file */
Mark Whitley59ab0252001-01-23 22:30:04 +0000116 while ((len = read(infile, buffer, BUFSIZ)) > 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000117 if (write(outfile, buffer, len) < 0)
118 perror_msg_and_die("write");
Eric Andersencff3fe32000-09-20 19:22:26 +0000119 }
Matt Kraaia9819b22000-12-22 01:48:07 +0000120 if (len < 0)
121 perror_msg_and_die("read");
122 return EXIT_SUCCESS;
Eric Andersencff3fe32000-09-20 19:22:26 +0000123}