Ignore SIGTERM prior to gz_close()
[oweals/busybox.git] / 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 <signal.h>
28 #include "busybox.h"
29
30 /* From gunzip.c */
31 extern int gz_open(FILE *compressed_file, int *pid);
32 extern void gz_close(int gunzip_pid);
33
34 typedef 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
45 extern ar_headers_t get_ar_headers(int srcFd);
46 extern int tar_unzip_init(int tarFd);
47 extern int readTarFile(int tarFd, int extractFlag, int listFlag, 
48         int tostdoutFlag, int verboseFlag, char** extractList, char** excludeList);
49
50 static const int dpkg_deb_contents = 1;
51 static const int dpkg_deb_control = 2;
52 //      const int dpkg_deb_info = 4;
53 static const int dpkg_deb_extract = 8;
54 static const int dpkg_deb_verbose_extract = 16;
55 static const int dpkg_deb_list = 32;
56
57 extern 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];
62         int extract_flag = FALSE;
63         int list_flag = FALSE;
64         int verbose_flag = FALSE;
65         int extract_to_stdout = FALSE;
66         int srcFd = 0;
67         int status;
68         pid_t pid;
69         FILE *comp_file = NULL;
70
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         }
76         if (dpkg_deb_list == (dpkg_deb_list & optflags)) {
77                 strcpy(ar_filename, "data.tar.gz");
78                 list_flag = TRUE;
79         }
80         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);
109         /* 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         }
114         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         }
122         status = readTarFile(srcFd, extract_flag, list_flag, 
123                 extract_to_stdout, verbose_flag, NULL, extract_list);
124
125         /* we are deliberately terminating the child so we can safely ignore this */
126         signal(SIGTERM, SIG_IGN);
127         gz_close(pid);
128         close(srcFd);
129         fclose(comp_file);
130
131         return status;
132 }
133
134 extern int dpkg_deb_main(int argc, char **argv)
135 {
136         char *target_dir = NULL;
137         int opt = 0;
138         int optflag = 0;        
139         
140         while ((opt = getopt(argc, argv, "cexXl")) != -1) {
141                 switch (opt) {
142                         case 'c':
143                                 optflag |= dpkg_deb_contents;
144                                 break;
145                         case 'e':
146                                 optflag |= dpkg_deb_control;
147                                 break;
148                         case 'X':
149                                 optflag |= dpkg_deb_verbose_extract;
150                                 break;
151                         case 'x':
152                                 optflag |= dpkg_deb_extract;
153                                 break;
154                         case 'l':
155                                 optflag |= dpkg_deb_list;
156                                 break;
157 /*                      case 'I':
158                                 optflag |= dpkg_deb_info;
159                                 break;
160 */
161                         default:
162                                 show_usage();
163                 }
164         }
165
166         if (((optind + 1 ) > argc) || (optflag == 0))  {
167                 show_usage();
168         }
169         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                 }
177         }
178         deb_extract(optflag, target_dir, argv[optind]);
179 /*      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 */
187         return(EXIT_SUCCESS);
188 }