Patch from Konstantin Isakov <ikm@pisem.net>:
[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 #include <fcntl.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <getopt.h>
21 #include "unarchive.h"
22 #include "busybox.h"
23
24 extern int dpkg_deb_main(int argc, char **argv)
25 {
26         archive_handle_t *ar_archive;
27         archive_handle_t *tar_gz_archive;
28         int opt = 0;
29 #ifndef CONFIG_FEATURE_DPKG_DEB_EXTRACT_ONLY
30         const llist_t *control_tar_gz_llist = add_to_list(NULL, "control.tar.gz");
31 #endif
32 #ifndef CONFIG_AR
33         char magic[7];
34 #endif
35         
36         /* a .deb file is an ar archive that contain three files,
37          * data.tar.gz, control.tar.gz and debian
38          */
39         
40         /* Setup the tar archive handle */
41         tar_gz_archive = init_handle();
42
43         /* Setup an ar archive handle that refers to the gzip sub archive */    
44         ar_archive = init_handle();
45         ar_archive->action_data_subarchive = get_header_tar_gz;
46         ar_archive->sub_archive = tar_gz_archive;
47         ar_archive->filter = filter_accept_list;
48         ar_archive->accept = add_to_list(NULL, "data.tar.gz");
49
50 #ifndef CONFIG_FEATURE_DPKG_DEB_EXTRACT_ONLY
51         while ((opt = getopt(argc, argv, "cefXx")) != -1) {
52 #else
53         while ((opt = getopt(argc, argv, "x")) != -1) {
54 #endif
55                 switch (opt) {
56 #ifndef CONFIG_FEATURE_DPKG_DEB_EXTRACT_ONLY
57                         case 'c':
58                                 tar_gz_archive->action_header = header_verbose_list;
59                                 break;
60                         case 'e':
61                                 ar_archive->accept = control_tar_gz_llist;
62                                 tar_gz_archive->action_data = data_extract_all;
63                                 break;
64                         case 'f':
65                                 /* Print the entire control file
66                                  * it should accept a second argument which specifies a 
67                                  * specific field to print */
68                                 ar_archive->accept = control_tar_gz_llist;
69                                 tar_gz_archive->accept = add_to_list(NULL, "./control");;
70                                 tar_gz_archive->filter = filter_accept_list;
71                                 tar_gz_archive->action_data = data_extract_to_stdout;
72                                 break;
73                         case 'X':
74                                 tar_gz_archive->action_header = header_list;
75 #endif
76                         case 'x':
77                                 tar_gz_archive->action_data = data_extract_all;
78                                 break;
79                         default:
80                                 show_usage();
81                 }
82         }
83
84         if (optind + 2 < argc)  {
85                 show_usage();
86         }
87
88         tar_gz_archive->src_fd = ar_archive->src_fd = xopen(argv[optind++], O_RDONLY);
89
90         /* Workout where to extract the files */
91         /* 2nd argument is a dir name */
92         mkdir(argv[optind], 0777);
93         chdir(argv[optind]);
94
95 #ifdef CONFIG_AR
96         unpack_ar_archive(ar_archive);
97 #else
98         xread_all(ar_archive->src_fd, magic, 7);
99         if (strncmp(magic, "!<arch>", 7) != 0) {
100                 error_msg_and_die("Invalid ar magic");
101         }
102         ar_archive->offset += 7;
103
104         while (get_header_ar(ar_archive) == EXIT_SUCCESS);
105 #endif
106
107         /* Cleanup */
108         close (ar_archive->src_fd);
109
110         return(EXIT_SUCCESS);
111 }
112