"I'll think about it" != "apply it now". It means I need to think about it.
[oweals/busybox.git] / archival / dpkg_deb.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * dpkg-deb packs, unpacks and provides information about Debian archives.
4  *
5  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
6  */
7 #include "busybox.h"
8 #include "unarchive.h"
9
10 #define DPKG_DEB_OPT_CONTENTS   1
11 #define DPKG_DEB_OPT_CONTROL    2
12 #define DPKG_DEB_OPT_FIELD      4
13 #define DPKG_DEB_OPT_EXTRACT    8
14 #define DPKG_DEB_OPT_EXTRACT_VERBOSE    16
15
16 int dpkg_deb_main(int argc, char **argv)
17 {
18         archive_handle_t *ar_archive;
19         archive_handle_t *tar_archive;
20         llist_t *control_tar_llist = NULL;
21         unsigned long opt;
22         char *extract_dir = NULL;
23         short argcount = 1;
24
25         /* Setup the tar archive handle */
26         tar_archive = init_handle();
27
28         /* Setup an ar archive handle that refers to the gzip sub archive */
29         ar_archive = init_handle();
30         ar_archive->sub_archive = tar_archive;
31         ar_archive->filter = filter_accept_list_reassign;
32
33 #ifdef CONFIG_FEATURE_DEB_TAR_GZ
34         llist_add_to(&(ar_archive->accept), "data.tar.gz");
35         llist_add_to(&control_tar_llist, "control.tar.gz");
36 #endif
37
38 #ifdef CONFIG_FEATURE_DEB_TAR_BZ2
39         llist_add_to(&(ar_archive->accept), "data.tar.bz2");
40         llist_add_to(&control_tar_llist, "control.tar.bz2");
41 #endif
42
43         bb_opt_complementally = "?c--efXx:e--cfXx:f--ceXx:X--cefx:x--cefX";
44         opt = bb_getopt_ulflags(argc, argv, "cefXx");
45
46         if (opt & DPKG_DEB_OPT_CONTENTS) {
47                 tar_archive->action_header = header_verbose_list;
48         }
49         if (opt & DPKG_DEB_OPT_CONTROL) {
50                 ar_archive->accept = control_tar_llist;
51                 tar_archive->action_data = data_extract_all;
52                 if (optind + 1 == argc) {
53                         extract_dir = "./DEBIAN";
54                 } else {
55                         argcount++;
56                 }
57         }
58         if (opt & DPKG_DEB_OPT_FIELD) {
59                 /* Print the entire control file
60                  * it should accept a second argument which specifies a
61                  * specific field to print */
62                 ar_archive->accept = control_tar_llist;
63                 llist_add_to(&(tar_archive->accept), "./control");
64                 tar_archive->filter = filter_accept_list;
65                 tar_archive->action_data = data_extract_to_stdout;
66         }
67         if (opt & DPKG_DEB_OPT_EXTRACT) {
68                 tar_archive->action_header = header_list;
69         }
70         if (opt & (DPKG_DEB_OPT_EXTRACT_VERBOSE | DPKG_DEB_OPT_EXTRACT)) {
71                 tar_archive->action_data = data_extract_all;
72                 argcount = 2;
73         }
74
75         if ((optind + argcount) != argc) {
76                 bb_show_usage();
77         }
78
79         tar_archive->src_fd = ar_archive->src_fd = xopen(argv[optind++], O_RDONLY);
80
81         /* Workout where to extract the files */
82         /* 2nd argument is a dir name */
83         if (argv[optind]) {
84                 extract_dir = argv[optind];
85         }
86         if (extract_dir) {
87                 mkdir(extract_dir, 0777); /* bb_make_directory(extract_dir, 0777, 0) */
88                 xchdir(extract_dir);
89         }
90         unpack_ar_archive(ar_archive);
91
92         /* Cleanup */
93         close (ar_archive->src_fd);
94
95         return(EXIT_SUCCESS);
96 }