blob: c0843818915451ac87bbb6910081fd55ea86c078 [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
29typedef struct ar_headers_s {
30 char *name;
31 size_t size;
32 uid_t uid;
33 gid_t gid;
34 mode_t mode;
35 time_t mtime;
36 off_t offset;
37 struct ar_headers_s *next;
38} ar_headers_t;
39
Glenn L McGrathbc9afad2001-02-11 03:32:41 +000040extern ar_headers_t get_ar_headers(int srcFd);
Glenn L McGrath58a40852001-01-02 23:49:26 +000041extern int tar_unzip_init(int tarFd);
42extern int readTarFile(int tarFd, int extractFlag, int listFlag,
Glenn L McGrathbc9afad2001-02-11 03:32:41 +000043 int tostdoutFlag, int verboseFlag, char** extractList, char** excludeList);
Glenn L McGrath58a40852001-01-02 23:49:26 +000044
Glenn L McGrath821fbf02001-02-12 11:16:26 +000045static const int dpkg_deb_contents = 1;
46static const int dpkg_deb_control = 2;
Glenn L McGrath59c09d02001-01-03 01:44:49 +000047// const int dpkg_deb_info = 4;
Glenn L McGrath821fbf02001-02-12 11:16:26 +000048static const int dpkg_deb_extract = 8;
49static const int dpkg_deb_verbose_extract = 16;
50static const int dpkg_deb_list = 32;
Glenn L McGrathbc9afad2001-02-11 03:32:41 +000051
52extern int deb_extract(int optflags, const char *dir_name, const char *deb_filename)
53{
54 char **extract_list = NULL;
55 ar_headers_t *ar_headers = NULL;
56 char ar_filename[15];
Glenn L McGrath58a40852001-01-02 23:49:26 +000057 int extract_flag = FALSE;
58 int list_flag = FALSE;
59 int verbose_flag = FALSE;
60 int extract_to_stdout = FALSE;
Glenn L McGrathbc9afad2001-02-11 03:32:41 +000061 int srcFd = 0;
62 int status;
63
64 if (dpkg_deb_contents == (dpkg_deb_contents & optflags)) {
65 strcpy(ar_filename, "data.tar.gz");
66 verbose_flag = TRUE;
67 list_flag = TRUE;
68 }
Glenn L McGrath821fbf02001-02-12 11:16:26 +000069 if (dpkg_deb_list == (dpkg_deb_list & optflags)) {
70 strcpy(ar_filename, "data.tar.gz");
71 list_flag = TRUE;
72 }
Glenn L McGrathbc9afad2001-02-11 03:32:41 +000073 if (dpkg_deb_control == (dpkg_deb_control & optflags)) {
74 strcpy(ar_filename, "control.tar.gz");
75 extract_flag = TRUE;
76 }
77 if (dpkg_deb_extract == (dpkg_deb_extract & optflags)) {
78 strcpy(ar_filename, "data.tar.gz");
79 extract_flag = TRUE;
80 }
81 if (dpkg_deb_verbose_extract == (dpkg_deb_verbose_extract & optflags)) {
82 strcpy(ar_filename, "data.tar.gz");
83 extract_flag = TRUE;
84 list_flag = TRUE;
85 }
86
87 ar_headers = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
88 srcFd = open(deb_filename, O_RDONLY);
89
90 *ar_headers = get_ar_headers(srcFd);
91 if (ar_headers->next == NULL) {
92 error_msg_and_die("Couldnt find %s in %s", ar_filename, deb_filename);
93 }
94
95 while (ar_headers->next != NULL) {
96 if (strcmp(ar_headers->name, ar_filename) == 0) {
97 break;
98 }
99 ar_headers = ar_headers->next;
100 }
101 lseek(srcFd, ar_headers->offset, SEEK_SET);
102 srcFd = tar_unzip_init(srcFd);
103 if ( dir_name != NULL) {
104 if (is_directory(dir_name, TRUE, NULL)==FALSE) {
105 mkdir(dir_name, 0755);
106 }
107 if (chdir(dir_name)==-1) {
108 error_msg_and_die("Cannot change to dir %s", dir_name);
109 }
110 }
111 status = readTarFile(srcFd, extract_flag, list_flag, extract_to_stdout, verbose_flag, NULL, extract_list);
112
113 return status;
114}
115
116extern int dpkg_deb_main(int argc, char **argv)
117{
118 char *target_dir = NULL;
119 int opt = 0;
120 int optflag = 0;
Glenn L McGrath58a40852001-01-02 23:49:26 +0000121
Glenn L McGrath821fbf02001-02-12 11:16:26 +0000122 while ((opt = getopt(argc, argv, "cexXl")) != -1) {
Glenn L McGrath58a40852001-01-02 23:49:26 +0000123 switch (opt) {
124 case 'c':
125 optflag |= dpkg_deb_contents;
126 break;
127 case 'e':
128 optflag |= dpkg_deb_control;
129 break;
Glenn L McGrathbc9afad2001-02-11 03:32:41 +0000130 case 'X':
131 optflag |= dpkg_deb_verbose_extract;
132 break;
133 case 'x':
134 optflag |= dpkg_deb_extract;
135 break;
Glenn L McGrath821fbf02001-02-12 11:16:26 +0000136 case 'l':
137 optflag |= dpkg_deb_list;
138 break;
Glenn L McGrath58a40852001-01-02 23:49:26 +0000139/* case 'I':
140 optflag |= dpkg_deb_info;
141 break;
142*/
Glenn L McGrath58a40852001-01-02 23:49:26 +0000143 default:
Eric Andersen67991cf2001-02-14 21:23:06 +0000144 show_usage();
Glenn L McGrath58a40852001-01-02 23:49:26 +0000145 }
146 }
147
148 if (((optind + 1 ) > argc) || (optflag == 0)) {
Eric Andersen67991cf2001-02-14 21:23:06 +0000149 show_usage();
Glenn L McGrath58a40852001-01-02 23:49:26 +0000150 }
Glenn L McGrathbc9afad2001-02-11 03:32:41 +0000151 if ((optflag & dpkg_deb_control) || (optflag & dpkg_deb_extract) || (optflag & dpkg_deb_verbose_extract)) {
152 if ( (optind + 1) == argc ) {
153 target_dir = (char *) xmalloc(7);
154 strcpy(target_dir, "DEBIAN");
155 } else {
156 target_dir = (char *) xmalloc(strlen(argv[optind + 1]));
157 strcpy(target_dir, argv[optind + 1]);
158 }
Glenn L McGrath58a40852001-01-02 23:49:26 +0000159 }
Glenn L McGrathbc9afad2001-02-11 03:32:41 +0000160 deb_extract(optflag, target_dir, argv[optind]);
Glenn L McGrath58a40852001-01-02 23:49:26 +0000161/* else if (optflag & dpkg_deb_info) {
162 extract_flag = TRUE;
163 extract_to_stdout = TRUE;
164 strcpy(ar_filename, "control.tar.gz");
165 extract_list = argv+optind+1;
166 printf("list one is [%s]\n",extract_list[0]);
167 }
168*/
Glenn L McGrath58a40852001-01-02 23:49:26 +0000169 return(EXIT_SUCCESS);
170}