32ef4ca75ca1c66bd2bfdd2eef446825e3f19066
[oweals/busybox.git] / archival / dpkg_deb.c
1 /*
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
17 /*
18  *      Merge this applet into dpkg when dpkg becomes more stable
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include "busybox.h"
28
29 typedef 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
40 extern ar_headers_t get_ar_headers(int srcFd);
41 extern int tar_unzip_init(int tarFd);
42 extern int readTarFile(int tarFd, int extractFlag, int listFlag, 
43         int tostdoutFlag, int verboseFlag, char** extractList, char** excludeList);
44
45 const int dpkg_deb_contents = 1;
46 const int dpkg_deb_control = 2;
47 //      const int dpkg_deb_info = 4;
48 const int dpkg_deb_extract = 8;
49 const int dpkg_deb_verbose_extract = 16;
50
51 extern int deb_extract(int optflags, const char *dir_name, const char *deb_filename)
52 {
53         char **extract_list = NULL;
54         ar_headers_t *ar_headers = NULL;
55         char ar_filename[15];
56         int extract_flag = FALSE;
57         int list_flag = FALSE;
58         int verbose_flag = FALSE;
59         int extract_to_stdout = FALSE;
60         int srcFd = 0;
61         int status;
62
63         if (dpkg_deb_contents == (dpkg_deb_contents & optflags)) {
64                 strcpy(ar_filename, "data.tar.gz");
65                 verbose_flag = TRUE;
66                 list_flag = TRUE;
67         }
68         if (dpkg_deb_control == (dpkg_deb_control & optflags)) {
69                 strcpy(ar_filename, "control.tar.gz");  
70                 extract_flag = TRUE;
71         }
72         if (dpkg_deb_extract == (dpkg_deb_extract & optflags)) {
73                 strcpy(ar_filename, "data.tar.gz");
74                 extract_flag = TRUE;
75         }
76         if (dpkg_deb_verbose_extract == (dpkg_deb_verbose_extract & optflags)) {
77                 strcpy(ar_filename, "data.tar.gz");
78                 extract_flag = TRUE;
79                 list_flag = TRUE;
80         }
81
82         ar_headers = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));    
83         srcFd = open(deb_filename, O_RDONLY);
84         
85         *ar_headers = get_ar_headers(srcFd);
86         if (ar_headers->next == NULL) {
87                 error_msg_and_die("Couldnt find %s in %s", ar_filename, deb_filename);
88         }
89
90         while (ar_headers->next != NULL) {
91                 if (strcmp(ar_headers->name, ar_filename) == 0) {
92                         break;
93                 }
94                 ar_headers = ar_headers->next;
95         }
96         lseek(srcFd, ar_headers->offset, SEEK_SET);
97         srcFd = tar_unzip_init(srcFd);
98         if ( dir_name != NULL) { 
99                 if (is_directory(dir_name, TRUE, NULL)==FALSE) {
100                         mkdir(dir_name, 0755);
101                 }
102                 if (chdir(dir_name)==-1) {
103                         error_msg_and_die("Cannot change to dir %s", dir_name);
104                 }
105         }
106         status = readTarFile(srcFd, extract_flag, list_flag, extract_to_stdout, verbose_flag, NULL, extract_list);
107
108         return status;
109 }
110
111 extern int dpkg_deb_main(int argc, char **argv)
112 {
113         char *target_dir = NULL;
114         int opt = 0;
115         int optflag = 0;        
116         
117         while ((opt = getopt(argc, argv, "cexX")) != -1) {
118                 switch (opt) {
119                         case 'c':
120                                 optflag |= dpkg_deb_contents;
121                                 break;
122                         case 'e':
123                                 optflag |= dpkg_deb_control;
124                                 break;
125                         case 'X':
126                                 optflag |= dpkg_deb_verbose_extract;
127                                 break;
128                         case 'x':
129                                 optflag |= dpkg_deb_extract;
130                                 break;
131 /*                      case 'I':
132                                 optflag |= dpkg_deb_info;
133                                 break;
134 */
135                         default:
136                                 usage(dpkg_deb_usage);
137                                 return EXIT_FAILURE;
138                 }
139         }
140
141         if (((optind + 1 ) > argc) || (optflag == 0))  {
142                 usage(dpkg_deb_usage);
143                 return(EXIT_FAILURE);
144         }
145         if ((optflag & dpkg_deb_control) || (optflag & dpkg_deb_extract) || (optflag & dpkg_deb_verbose_extract)) {
146                 if ( (optind + 1) == argc ) {
147                         target_dir = (char *) xmalloc(7);
148                         strcpy(target_dir, "DEBIAN");
149                 } else {
150                         target_dir = (char *) xmalloc(strlen(argv[optind + 1]));
151                         strcpy(target_dir, argv[optind + 1]);
152                 }
153         }
154         deb_extract(optflag, target_dir, argv[optind]);
155 /*      else if (optflag & dpkg_deb_info) {
156                 extract_flag = TRUE;
157                 extract_to_stdout = TRUE;
158                 strcpy(ar_filename, "control.tar.gz");
159                 extract_list = argv+optind+1;
160                 printf("list one is [%s]\n",extract_list[0]);
161         }
162 */
163         return(EXIT_SUCCESS);
164 }