xz: fix decoding of LZMA2 streams having no uncompressed data.
[platform/upstream/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 source tree.
6  */
7
8 //usage:#define dpkg_deb_trivial_usage
9 //usage:       "[-cefxX] FILE [argument"
10 //usage:#define dpkg_deb_full_usage "\n\n"
11 //usage:       "Perform actions on Debian packages (.debs)\n"
12 //usage:     "\n        -c      List contents of filesystem tree"
13 //usage:     "\n        -e      Extract control files to [argument] directory"
14 //usage:     "\n        -f      Display control field name starting with [argument]"
15 //usage:     "\n        -x      Extract packages filesystem tree to directory"
16 //usage:     "\n        -X      Verbose extract"
17 //usage:
18 //usage:#define dpkg_deb_example_usage
19 //usage:       "$ dpkg-deb -X ./busybox_0.48-1_i386.deb /tmp\n"
20
21 #include "libbb.h"
22 #include "bb_archive.h"
23
24 #define DPKG_DEB_OPT_CONTENTS         1
25 #define DPKG_DEB_OPT_CONTROL          2
26 #define DPKG_DEB_OPT_FIELD            4
27 #define DPKG_DEB_OPT_EXTRACT          8
28 #define DPKG_DEB_OPT_EXTRACT_VERBOSE 16
29
30 int dpkg_deb_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
31 int dpkg_deb_main(int argc, char **argv)
32 {
33         archive_handle_t *ar_archive;
34         archive_handle_t *tar_archive;
35         llist_t *control_tar_llist = NULL;
36         unsigned opt;
37         const char *extract_dir;
38         int need_args;
39
40         /* Setup the tar archive handle */
41         tar_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->dpkg__sub_archive = tar_archive;
46         ar_archive->filter = filter_accept_list_reassign;
47
48 #if ENABLE_FEATURE_SEAMLESS_GZ
49         llist_add_to(&ar_archive->accept, (char*)"data.tar.gz");
50         llist_add_to(&control_tar_llist, (char*)"control.tar.gz");
51 #endif
52 #if ENABLE_FEATURE_SEAMLESS_BZ2
53         llist_add_to(&ar_archive->accept, (char*)"data.tar.bz2");
54         llist_add_to(&control_tar_llist, (char*)"control.tar.bz2");
55 #endif
56 #if ENABLE_FEATURE_SEAMLESS_LZMA
57         llist_add_to(&ar_archive->accept, (char*)"data.tar.lzma");
58         llist_add_to(&control_tar_llist, (char*)"control.tar.lzma");
59 #endif
60
61         opt_complementary = "c--efXx:e--cfXx:f--ceXx:X--cefx:x--cefX";
62         opt = getopt32(argv, "cefXx");
63         argv += optind;
64         argc -= optind;
65
66         if (opt & DPKG_DEB_OPT_CONTENTS) {
67                 tar_archive->action_header = header_verbose_list;
68         }
69         extract_dir = NULL;
70         need_args = 1;
71         if (opt & DPKG_DEB_OPT_CONTROL) {
72                 ar_archive->accept = control_tar_llist;
73                 tar_archive->action_data = data_extract_all;
74                 if (1 == argc) {
75                         extract_dir = "./DEBIAN";
76                 } else {
77                         need_args++;
78                 }
79         }
80         if (opt & DPKG_DEB_OPT_FIELD) {
81                 /* Print the entire control file
82                  * it should accept a second argument which specifies a
83                  * specific field to print */
84                 ar_archive->accept = control_tar_llist;
85                 llist_add_to(&(tar_archive->accept), (char*)"./control");
86                 tar_archive->filter = filter_accept_list;
87                 tar_archive->action_data = data_extract_to_stdout;
88         }
89         if (opt & DPKG_DEB_OPT_EXTRACT) {
90                 tar_archive->action_header = header_list;
91         }
92         if (opt & (DPKG_DEB_OPT_EXTRACT_VERBOSE | DPKG_DEB_OPT_EXTRACT)) {
93                 tar_archive->action_data = data_extract_all;
94                 need_args = 2;
95         }
96
97         if (need_args != argc) {
98                 bb_show_usage();
99         }
100
101         tar_archive->src_fd = ar_archive->src_fd = xopen(argv[0], O_RDONLY);
102
103         /* Work out where to extract the files */
104         /* 2nd argument is a dir name */
105         if (argv[1]) {
106                 extract_dir = argv[1];
107         }
108         if (extract_dir) {
109                 mkdir(extract_dir, 0777); /* bb_make_directory(extract_dir, 0777, 0) */
110                 xchdir(extract_dir);
111         }
112
113         /* Do it */
114         unpack_ar_archive(ar_archive);
115
116         /* Cleanup */
117         if (ENABLE_FEATURE_CLEAN_UP)
118                 close(ar_archive->src_fd);
119
120         return EXIT_SUCCESS;
121 }