blob: 309f8d7628c2809c57afa449105427b3120c5aa0 [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 McGrath58a40852001-01-02 23:49:26 +000027#include "busybox.h"
28
Eric Andersenfdefbbb2001-04-05 06:04:11 +000029/* From gunzip.c */
30extern int gz_open(FILE *compressed_file, int *pid);
31extern void gz_close(int gunzip_pid);
32
Glenn L McGrath58a40852001-01-02 23:49:26 +000033typedef struct ar_headers_s {
34 char *name;
35 size_t size;
36 uid_t uid;
37 gid_t gid;
38 mode_t mode;
39 time_t mtime;
40 off_t offset;
41 struct ar_headers_s *next;
42} ar_headers_t;
43
Glenn L McGrathbc9afad2001-02-11 03:32:41 +000044extern ar_headers_t get_ar_headers(int srcFd);
Glenn L McGrath58a40852001-01-02 23:49:26 +000045extern int tar_unzip_init(int tarFd);
46extern int readTarFile(int tarFd, int extractFlag, int listFlag,
Glenn L McGrathbc9afad2001-02-11 03:32:41 +000047 int tostdoutFlag, int verboseFlag, char** extractList, char** excludeList);
Glenn L McGrath58a40852001-01-02 23:49:26 +000048
Glenn L McGrath821fbf02001-02-12 11:16:26 +000049static const int dpkg_deb_contents = 1;
50static const int dpkg_deb_control = 2;
Glenn L McGrath59c09d02001-01-03 01:44:49 +000051// const int dpkg_deb_info = 4;
Glenn L McGrath821fbf02001-02-12 11:16:26 +000052static const int dpkg_deb_extract = 8;
53static const int dpkg_deb_verbose_extract = 16;
54static const int dpkg_deb_list = 32;
Glenn L McGrathbc9afad2001-02-11 03:32:41 +000055
56extern int deb_extract(int optflags, const char *dir_name, const char *deb_filename)
57{
58 char **extract_list = NULL;
59 ar_headers_t *ar_headers = NULL;
60 char ar_filename[15];
Glenn L McGrath58a40852001-01-02 23:49:26 +000061 int extract_flag = FALSE;
62 int list_flag = FALSE;
63 int verbose_flag = FALSE;
64 int extract_to_stdout = FALSE;
Glenn L McGrathbc9afad2001-02-11 03:32:41 +000065 int srcFd = 0;
66 int status;
Eric Andersenfdefbbb2001-04-05 06:04:11 +000067 pid_t pid;
68 FILE *comp_file = NULL;
Glenn L McGrathbc9afad2001-02-11 03:32:41 +000069
70 if (dpkg_deb_contents == (dpkg_deb_contents & optflags)) {
71 strcpy(ar_filename, "data.tar.gz");
72 verbose_flag = TRUE;
73 list_flag = TRUE;
74 }
Glenn L McGrath821fbf02001-02-12 11:16:26 +000075 if (dpkg_deb_list == (dpkg_deb_list & optflags)) {
76 strcpy(ar_filename, "data.tar.gz");
77 list_flag = TRUE;
78 }
Glenn L McGrathbc9afad2001-02-11 03:32:41 +000079 if (dpkg_deb_control == (dpkg_deb_control & optflags)) {
80 strcpy(ar_filename, "control.tar.gz");
81 extract_flag = TRUE;
82 }
83 if (dpkg_deb_extract == (dpkg_deb_extract & optflags)) {
84 strcpy(ar_filename, "data.tar.gz");
85 extract_flag = TRUE;
86 }
87 if (dpkg_deb_verbose_extract == (dpkg_deb_verbose_extract & optflags)) {
88 strcpy(ar_filename, "data.tar.gz");
89 extract_flag = TRUE;
90 list_flag = TRUE;
91 }
92
93 ar_headers = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
94 srcFd = open(deb_filename, O_RDONLY);
95
96 *ar_headers = get_ar_headers(srcFd);
97 if (ar_headers->next == NULL) {
98 error_msg_and_die("Couldnt find %s in %s", ar_filename, deb_filename);
99 }
100
101 while (ar_headers->next != NULL) {
102 if (strcmp(ar_headers->name, ar_filename) == 0) {
103 break;
104 }
105 ar_headers = ar_headers->next;
106 }
107 lseek(srcFd, ar_headers->offset, SEEK_SET);
Eric Andersenfdefbbb2001-04-05 06:04:11 +0000108 /* Uncompress the file */
109 comp_file = fdopen(srcFd, "r");
110 if ((srcFd = gz_open(comp_file, &pid)) == EXIT_FAILURE) {
111 error_msg_and_die("Couldnt unzip file");
112 }
Glenn L McGrathbc9afad2001-02-11 03:32:41 +0000113 if ( dir_name != NULL) {
114 if (is_directory(dir_name, TRUE, NULL)==FALSE) {
115 mkdir(dir_name, 0755);
116 }
117 if (chdir(dir_name)==-1) {
118 error_msg_and_die("Cannot change to dir %s", dir_name);
119 }
120 }
Eric Andersenfdefbbb2001-04-05 06:04:11 +0000121 status = readTarFile(srcFd, extract_flag, list_flag,
122 extract_to_stdout, verbose_flag, NULL, extract_list);
123 close(srcFd);
124 gz_close(pid);
125 fclose(comp_file);
Glenn L McGrathbc9afad2001-02-11 03:32:41 +0000126
127 return status;
128}
129
130extern int dpkg_deb_main(int argc, char **argv)
131{
132 char *target_dir = NULL;
133 int opt = 0;
134 int optflag = 0;
Glenn L McGrath58a40852001-01-02 23:49:26 +0000135
Glenn L McGrath821fbf02001-02-12 11:16:26 +0000136 while ((opt = getopt(argc, argv, "cexXl")) != -1) {
Glenn L McGrath58a40852001-01-02 23:49:26 +0000137 switch (opt) {
138 case 'c':
139 optflag |= dpkg_deb_contents;
140 break;
141 case 'e':
142 optflag |= dpkg_deb_control;
143 break;
Glenn L McGrathbc9afad2001-02-11 03:32:41 +0000144 case 'X':
145 optflag |= dpkg_deb_verbose_extract;
146 break;
147 case 'x':
148 optflag |= dpkg_deb_extract;
149 break;
Glenn L McGrath821fbf02001-02-12 11:16:26 +0000150 case 'l':
151 optflag |= dpkg_deb_list;
152 break;
Glenn L McGrath58a40852001-01-02 23:49:26 +0000153/* case 'I':
154 optflag |= dpkg_deb_info;
155 break;
156*/
Glenn L McGrath58a40852001-01-02 23:49:26 +0000157 default:
Eric Andersen67991cf2001-02-14 21:23:06 +0000158 show_usage();
Glenn L McGrath58a40852001-01-02 23:49:26 +0000159 }
160 }
161
162 if (((optind + 1 ) > argc) || (optflag == 0)) {
Eric Andersen67991cf2001-02-14 21:23:06 +0000163 show_usage();
Glenn L McGrath58a40852001-01-02 23:49:26 +0000164 }
Glenn L McGrathbc9afad2001-02-11 03:32:41 +0000165 if ((optflag & dpkg_deb_control) || (optflag & dpkg_deb_extract) || (optflag & dpkg_deb_verbose_extract)) {
166 if ( (optind + 1) == argc ) {
167 target_dir = (char *) xmalloc(7);
168 strcpy(target_dir, "DEBIAN");
169 } else {
170 target_dir = (char *) xmalloc(strlen(argv[optind + 1]));
171 strcpy(target_dir, argv[optind + 1]);
172 }
Glenn L McGrath58a40852001-01-02 23:49:26 +0000173 }
Glenn L McGrathbc9afad2001-02-11 03:32:41 +0000174 deb_extract(optflag, target_dir, argv[optind]);
Glenn L McGrath58a40852001-01-02 23:49:26 +0000175/* else if (optflag & dpkg_deb_info) {
176 extract_flag = TRUE;
177 extract_to_stdout = TRUE;
178 strcpy(ar_filename, "control.tar.gz");
179 extract_list = argv+optind+1;
180 printf("list one is [%s]\n",extract_list[0]);
181 }
182*/
Glenn L McGrath58a40852001-01-02 23:49:26 +0000183 return(EXIT_SUCCESS);
184}