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