blob: 79971faf373a68f8bd57415838a8bd9896bd40c3 [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 Andersened3ef502001-01-27 08:24:39 +000018#include <unistd.h>
19#include <string.h>
20#include <stdlib.h>
Eric Andersencff3fe32000-09-20 19:22:26 +000021
22/*
23 * Some general definitions
24 */
Mark Whitley59ab0252001-01-23 22:30:04 +000025
Eric Andersencff3fe32000-09-20 19:22:26 +000026#define RPM_MAGIC "\355\253\356\333"
27#define GZ_MAGIC_1 '\037'
28#define GZ_MAGIC_2 '\213'
29
30/*
31 * Global variables
32 */
Eric Andersencff3fe32000-09-20 19:22:26 +000033static char *progname;
34static int infile, outfile;
35
36/*
37 * Read a specified number of bytes from input file
38 */
Mark Whitley59ab0252001-01-23 22:30:04 +000039static void myread(int num, char *buffer)
Eric Andersencff3fe32000-09-20 19:22:26 +000040{
41 int err;
42
43 if ((err = read(infile, buffer, num)) != num) {
44 if (err < 0)
Matt Kraaia9819b22000-12-22 01:48:07 +000045 perror_msg_and_die(progname);
Eric Andersencff3fe32000-09-20 19:22:26 +000046 else
Matt Kraaidd19c692001-01-31 19:00:21 +000047 error_msg_and_die("Unexpected end of input file!");
Eric Andersencff3fe32000-09-20 19:22:26 +000048 }
49}
50
51/*
52 * Main program
53 */
Eric Andersen4bfb6b72000-11-29 21:39:02 +000054int rpmunpack_main(int argc, char **argv)
Eric Andersencff3fe32000-09-20 19:22:26 +000055{
56 int len, status = 0;
Eric Andersend35c2152001-01-25 23:49:09 +000057 RESERVE_BB_BUFFER(buffer, BUFSIZ);
Eric Andersencff3fe32000-09-20 19:22:26 +000058
59 /* Get our own program name */
60 if ((progname = strrchr(argv[0], '/')) == NULL)
61 progname = argv[0];
62 else
63 progname++;
64
65 /* Check for command line parameters */
66 if (argc>=2 && *argv[1]=='-') {
Eric Andersen67991cf2001-02-14 21:23:06 +000067 show_usage();
Eric Andersencff3fe32000-09-20 19:22:26 +000068 }
69
70 /* Open input file */
71 if (argc == 1)
72 infile = STDIN_FILENO;
Matt Kraaia9819b22000-12-22 01:48:07 +000073 else if ((infile = open(argv[1], O_RDONLY)) < 0)
74 perror_msg_and_die("%s", argv[1]);
Eric Andersencff3fe32000-09-20 19:22:26 +000075
76 /* Read magic ID and output filename */
Mark Whitley59ab0252001-01-23 22:30:04 +000077 myread(4, buffer);
Eric Andersencff3fe32000-09-20 19:22:26 +000078 if (strncmp(buffer, RPM_MAGIC, 4)) {
Eric Andersen4bfb6b72000-11-29 21:39:02 +000079 fprintf(stderr, "Input file is not in RPM format!\n");
Eric Andersencff3fe32000-09-20 19:22:26 +000080 exit(1);
81 }
Mark Whitley59ab0252001-01-23 22:30:04 +000082 myread(6, buffer); /* Skip flags */
83 myread(64, buffer);
Eric Andersencff3fe32000-09-20 19:22:26 +000084 buffer[64] = '\0';
85
86 /* Open output file */
87 strcat(buffer, ".cpio.gz");
88 if (infile == STDIN_FILENO)
89 outfile = STDOUT_FILENO;
Matt Kraaia9819b22000-12-22 01:48:07 +000090 else if ((outfile = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0)
91 perror_msg_and_die("%s", buffer);
Eric Andersencff3fe32000-09-20 19:22:26 +000092
93 /*
94 * Now search for the GZIP signature. This is rather awkward, but I don't
95 * know any other way how to find out the exact starting position of the
96 * archive within the input file. There are a couple of data structures
97 * and texts (obviously descriptions, installation shell scripts etc.)
98 * coming before the archive, but even they start at different offsets
99 * with different RPM files. However, it looks like the GZIP signature
100 * never appears before offset 0x200, so we skip these first couple of
101 * bytes to make the signature scan a little more reliable.
102 */
Mark Whitley59ab0252001-01-23 22:30:04 +0000103 myread(0x200 - 74, buffer);
Eric Andersencff3fe32000-09-20 19:22:26 +0000104 while (status < 2) {
Mark Whitley59ab0252001-01-23 22:30:04 +0000105 myread(1, buffer);
Eric Andersencff3fe32000-09-20 19:22:26 +0000106 if (status == 0 && buffer[0] == GZ_MAGIC_1)
107 status++;
108 else if (status == 1 && buffer[0] == GZ_MAGIC_2)
109 status++;
110 else
111 status = 0;
112 }
113 buffer[0] = GZ_MAGIC_1;
114 buffer[1] = GZ_MAGIC_2;
Matt Kraaia9819b22000-12-22 01:48:07 +0000115 if (write(outfile, buffer, 2) < 0)
116 perror_msg_and_die("write");
Eric Andersencff3fe32000-09-20 19:22:26 +0000117
118 /* Now simply copy the GZIP archive into the output file */
Mark Whitley59ab0252001-01-23 22:30:04 +0000119 while ((len = read(infile, buffer, BUFSIZ)) > 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000120 if (write(outfile, buffer, len) < 0)
121 perror_msg_and_die("write");
Eric Andersencff3fe32000-09-20 19:22:26 +0000122 }
Matt Kraaia9819b22000-12-22 01:48:07 +0000123 if (len < 0)
124 perror_msg_and_die("read");
125 return EXIT_SUCCESS;
Eric Andersencff3fe32000-09-20 19:22:26 +0000126}