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