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