blob: 8239c3c43a618291fbd00957c92394859a8ac386 [file] [log] [blame]
Glenn L McGrath58a40852001-01-02 23:49:26 +00001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 */
16
Glenn L McGrathbc9afad2001-02-11 03:32:41 +000017/*
18 * Merge this applet into dpkg when dpkg becomes more stable
19 */
20
Glenn L McGrath58a40852001-01-02 23:49:26 +000021#include <stdio.h>
22#include <stdlib.h>
23#include <fcntl.h>
Eric Andersened3ef502001-01-27 08:24:39 +000024#include <unistd.h>
25#include <string.h>
26#include <stdlib.h>
Glenn L McGrathf608da42001-04-07 02:40:59 +000027#include <signal.h>
Glenn L McGrath58a40852001-01-02 23:49:26 +000028#include "busybox.h"
29
Eric Andersenfdefbbb2001-04-05 06:04:11 +000030/* From gunzip.c */
31extern int gz_open(FILE *compressed_file, int *pid);
32extern void gz_close(int gunzip_pid);
33
Glenn L McGrath58a40852001-01-02 23:49:26 +000034typedef struct ar_headers_s {
35 char *name;
36 size_t size;
37 uid_t uid;
38 gid_t gid;
39 mode_t mode;
40 time_t mtime;
41 off_t offset;
42 struct ar_headers_s *next;
43} ar_headers_t;
44
Glenn L McGrathbc9afad2001-02-11 03:32:41 +000045extern ar_headers_t get_ar_headers(int srcFd);
Glenn L McGrath58a40852001-01-02 23:49:26 +000046extern int tar_unzip_init(int tarFd);
47extern int readTarFile(int tarFd, int extractFlag, int listFlag,
Glenn L McGrathbc9afad2001-02-11 03:32:41 +000048 int tostdoutFlag, int verboseFlag, char** extractList, char** excludeList);
Glenn L McGrath58a40852001-01-02 23:49:26 +000049
Glenn L McGrath821fbf02001-02-12 11:16:26 +000050static const int dpkg_deb_contents = 1;
51static const int dpkg_deb_control = 2;
Glenn L McGrath59c09d02001-01-03 01:44:49 +000052// const int dpkg_deb_info = 4;
Glenn L McGrath821fbf02001-02-12 11:16:26 +000053static const int dpkg_deb_extract = 8;
54static const int dpkg_deb_verbose_extract = 16;
55static const int dpkg_deb_list = 32;
Glenn L McGrathbc9afad2001-02-11 03:32:41 +000056
57extern int deb_extract(int optflags, const char *dir_name, const char *deb_filename)
58{
59 char **extract_list = NULL;
60 ar_headers_t *ar_headers = NULL;
61 char ar_filename[15];
Glenn L McGrath58a40852001-01-02 23:49:26 +000062 int extract_flag = FALSE;
63 int list_flag = FALSE;
64 int verbose_flag = FALSE;
65 int extract_to_stdout = FALSE;
Glenn L McGrathbc9afad2001-02-11 03:32:41 +000066 int srcFd = 0;
67 int status;
Eric Andersenfdefbbb2001-04-05 06:04:11 +000068 pid_t pid;
69 FILE *comp_file = NULL;
Glenn L McGrathbc9afad2001-02-11 03:32:41 +000070
71 if (dpkg_deb_contents == (dpkg_deb_contents & optflags)) {
72 strcpy(ar_filename, "data.tar.gz");
73 verbose_flag = TRUE;
74 list_flag = TRUE;
75 }
Glenn L McGrath821fbf02001-02-12 11:16:26 +000076 if (dpkg_deb_list == (dpkg_deb_list & optflags)) {
77 strcpy(ar_filename, "data.tar.gz");
78 list_flag = TRUE;
79 }
Glenn L McGrathbc9afad2001-02-11 03:32:41 +000080 if (dpkg_deb_control == (dpkg_deb_control & optflags)) {
81 strcpy(ar_filename, "control.tar.gz");
82 extract_flag = TRUE;
83 }
84 if (dpkg_deb_extract == (dpkg_deb_extract & optflags)) {
85 strcpy(ar_filename, "data.tar.gz");
86 extract_flag = TRUE;
87 }
88 if (dpkg_deb_verbose_extract == (dpkg_deb_verbose_extract & optflags)) {
89 strcpy(ar_filename, "data.tar.gz");
90 extract_flag = TRUE;
91 list_flag = TRUE;
92 }
93
94 ar_headers = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
95 srcFd = open(deb_filename, O_RDONLY);
96
97 *ar_headers = get_ar_headers(srcFd);
98 if (ar_headers->next == NULL) {
99 error_msg_and_die("Couldnt find %s in %s", ar_filename, deb_filename);
100 }
101
102 while (ar_headers->next != NULL) {
103 if (strcmp(ar_headers->name, ar_filename) == 0) {
104 break;
105 }
106 ar_headers = ar_headers->next;
107 }
108 lseek(srcFd, ar_headers->offset, SEEK_SET);
Eric Andersenfdefbbb2001-04-05 06:04:11 +0000109 /* Uncompress the file */
110 comp_file = fdopen(srcFd, "r");
111 if ((srcFd = gz_open(comp_file, &pid)) == EXIT_FAILURE) {
112 error_msg_and_die("Couldnt unzip file");
113 }
Glenn L McGrathbc9afad2001-02-11 03:32:41 +0000114 if ( dir_name != NULL) {
115 if (is_directory(dir_name, TRUE, NULL)==FALSE) {
116 mkdir(dir_name, 0755);
117 }
118 if (chdir(dir_name)==-1) {
119 error_msg_and_die("Cannot change to dir %s", dir_name);
120 }
121 }
Eric Andersenfdefbbb2001-04-05 06:04:11 +0000122 status = readTarFile(srcFd, extract_flag, list_flag,
123 extract_to_stdout, verbose_flag, NULL, extract_list);
Glenn L McGrathf608da42001-04-07 02:40:59 +0000124
125 /* we are deliberately terminating the child so we can safely ignore this */
126 signal(SIGTERM, SIG_IGN);
Eric Andersenfdefbbb2001-04-05 06:04:11 +0000127 gz_close(pid);
Glenn L McGrathf608da42001-04-07 02:40:59 +0000128 close(srcFd);
Eric Andersenfdefbbb2001-04-05 06:04:11 +0000129 fclose(comp_file);
Glenn L McGrathbc9afad2001-02-11 03:32:41 +0000130
131 return status;
132}
133
134extern int dpkg_deb_main(int argc, char **argv)
135{
136 char *target_dir = NULL;
137 int opt = 0;
138 int optflag = 0;
Glenn L McGrath58a40852001-01-02 23:49:26 +0000139
Glenn L McGrath821fbf02001-02-12 11:16:26 +0000140 while ((opt = getopt(argc, argv, "cexXl")) != -1) {
Glenn L McGrath58a40852001-01-02 23:49:26 +0000141 switch (opt) {
142 case 'c':
143 optflag |= dpkg_deb_contents;
144 break;
145 case 'e':
146 optflag |= dpkg_deb_control;
147 break;
Glenn L McGrathbc9afad2001-02-11 03:32:41 +0000148 case 'X':
149 optflag |= dpkg_deb_verbose_extract;
150 break;
151 case 'x':
152 optflag |= dpkg_deb_extract;
153 break;
Glenn L McGrath821fbf02001-02-12 11:16:26 +0000154 case 'l':
155 optflag |= dpkg_deb_list;
156 break;
Glenn L McGrath58a40852001-01-02 23:49:26 +0000157/* case 'I':
158 optflag |= dpkg_deb_info;
159 break;
160*/
Glenn L McGrath58a40852001-01-02 23:49:26 +0000161 default:
Eric Andersen67991cf2001-02-14 21:23:06 +0000162 show_usage();
Glenn L McGrath58a40852001-01-02 23:49:26 +0000163 }
164 }
165
166 if (((optind + 1 ) > argc) || (optflag == 0)) {
Eric Andersen67991cf2001-02-14 21:23:06 +0000167 show_usage();
Glenn L McGrath58a40852001-01-02 23:49:26 +0000168 }
Glenn L McGrathbc9afad2001-02-11 03:32:41 +0000169 if ((optflag & dpkg_deb_control) || (optflag & dpkg_deb_extract) || (optflag & dpkg_deb_verbose_extract)) {
170 if ( (optind + 1) == argc ) {
171 target_dir = (char *) xmalloc(7);
172 strcpy(target_dir, "DEBIAN");
173 } else {
174 target_dir = (char *) xmalloc(strlen(argv[optind + 1]));
175 strcpy(target_dir, argv[optind + 1]);
176 }
Glenn L McGrath58a40852001-01-02 23:49:26 +0000177 }
Glenn L McGrathbc9afad2001-02-11 03:32:41 +0000178 deb_extract(optflag, target_dir, argv[optind]);
Glenn L McGrath58a40852001-01-02 23:49:26 +0000179/* else if (optflag & dpkg_deb_info) {
180 extract_flag = TRUE;
181 extract_to_stdout = TRUE;
182 strcpy(ar_filename, "control.tar.gz");
183 extract_list = argv+optind+1;
184 printf("list one is [%s]\n",extract_list[0]);
185 }
186*/
Glenn L McGrath58a40852001-01-02 23:49:26 +0000187 return(EXIT_SUCCESS);
188}