Changes for kernel and Busybox
[tomato.git] / release / src / router / busybox / archival / dpkg_deb.c
bloba04ec94077f9958cc0086ac8f16f970ffd1933b8
1 /* vi: set sw=4 ts=4: */
2 /*
3 * dpkg-deb packs, unpacks and provides information about Debian archives.
5 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
6 */
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"
21 #include "libbb.h"
22 #include "bb_archive.h"
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
30 int dpkg_deb_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
31 int dpkg_deb_main(int argc, char **argv)
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;
40 /* Setup the tar archive handle */
41 tar_archive = init_handle();
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;
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
61 opt_complementary = "c--efXx:e--cfXx:f--ceXx:X--cefx:x--cefX";
62 opt = getopt32(argv, "cefXx");
63 argv += optind;
64 argc -= optind;
66 if (opt & DPKG_DEB_OPT_CONTENTS) {
67 tar_archive->action_header = header_verbose_list;
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++;
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;
89 if (opt & DPKG_DEB_OPT_EXTRACT) {
90 tar_archive->action_header = header_list;
92 if (opt & (DPKG_DEB_OPT_EXTRACT_VERBOSE | DPKG_DEB_OPT_EXTRACT)) {
93 tar_archive->action_data = data_extract_all;
94 need_args = 2;
97 if (need_args != argc) {
98 bb_show_usage();
101 tar_archive->src_fd = ar_archive->src_fd = xopen(argv[0], O_RDONLY);
103 /* Work out where to extract the files */
104 /* 2nd argument is a dir name */
105 if (argv[1]) {
106 extract_dir = argv[1];
108 if (extract_dir) {
109 mkdir(extract_dir, 0777); /* bb_make_directory(extract_dir, 0777, 0) */
110 xchdir(extract_dir);
113 /* Do it */
114 unpack_ar_archive(ar_archive);
116 /* Cleanup */
117 if (ENABLE_FEATURE_CLEAN_UP)
118 close(ar_archive->src_fd);
120 return EXIT_SUCCESS;