blob: 81b5352a2b0f14600b7f0617a2764bb1d7425b46 [file] [log] [blame]
Bernhard Reutner-Fischerd9cf7ac2006-04-12 18:39:58 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath58a40852001-01-02 23:49:26 +00002/*
Bernhard Reutner-Fischerd9cf7ac2006-04-12 18:39:58 +00003 * dpkg-deb packs, unpacks and provides information about Debian archives.
Glenn L McGrath58a40852001-01-02 23:49:26 +00004 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02005 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath58a40852001-01-02 23:49:26 +00006 */
Pere Orga1f4447b2011-03-27 22:40:30 +02007
Denys Vlasenkof6beef62013-11-14 11:39:00 +01008//config:config DPKG_DEB
9//config: bool "dpkg_deb"
10//config: default n
11//config: select FEATURE_SEAMLESS_GZ
12//config: help
13//config: dpkg-deb unpacks and provides information about Debian archives.
14//config:
15//config: This implementation of dpkg-deb cannot pack archives.
16//config:
17//config: Unless you have a specific application which requires dpkg-deb,
18//config: say N here.
19//config:
20//config:config FEATURE_DPKG_DEB_EXTRACT_ONLY
21//config: bool "Extract only (-x)"
22//config: default n
23//config: depends on DPKG_DEB
24//config: help
25//config: This reduces dpkg-deb to the equivalent of
26//config: "ar -p <deb> data.tar.gz | tar -zx". However it saves space as none
27//config: of the extra dpkg-deb, ar or tar options are needed, they are linked
28//config: to internally.
29
Denys Vlasenko36184a42013-11-14 09:54:24 +010030//applet:IF_DPKG_DEB(APPLET_ODDNAME(dpkg-deb, dpkg_deb, BB_DIR_USR_BIN, BB_SUID_DROP, dpkg_deb))
Denys Vlasenko66620fa2013-11-14 09:53:52 +010031//kbuild:lib-$(CONFIG_DPKG_DEB) += dpkg_deb.o
32
Pere Orga1f4447b2011-03-27 22:40:30 +020033//usage:#define dpkg_deb_trivial_usage
34//usage: "[-cefxX] FILE [argument"
35//usage:#define dpkg_deb_full_usage "\n\n"
36//usage: "Perform actions on Debian packages (.debs)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +020037//usage: "\n -c List contents of filesystem tree"
38//usage: "\n -e Extract control files to [argument] directory"
39//usage: "\n -f Display control field name starting with [argument]"
40//usage: "\n -x Extract packages filesystem tree to directory"
41//usage: "\n -X Verbose extract"
42//usage:
43//usage:#define dpkg_deb_example_usage
44//usage: "$ dpkg-deb -X ./busybox_0.48-1_i386.deb /tmp\n"
45
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000046#include "libbb.h"
Denys Vlasenkod184a722011-09-22 12:45:14 +020047#include "bb_archive.h"
Glenn L McGrath58a40852001-01-02 23:49:26 +000048
Denys Vlasenkofb132e42010-10-29 11:46:52 +020049#define DPKG_DEB_OPT_CONTENTS 1
50#define DPKG_DEB_OPT_CONTROL 2
51#define DPKG_DEB_OPT_FIELD 4
52#define DPKG_DEB_OPT_EXTRACT 8
53#define DPKG_DEB_OPT_EXTRACT_VERBOSE 16
Glenn L McGrath1f28b902004-01-07 09:24:06 +000054
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000055int dpkg_deb_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +000056int dpkg_deb_main(int argc, char **argv)
Glenn L McGrathbc9afad2001-02-11 03:32:41 +000057{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000058 archive_handle_t *ar_archive;
Glenn L McGrath18bbca12002-11-05 01:52:23 +000059 archive_handle_t *tar_archive;
Glenn L McGrath66125c82002-12-08 00:54:33 +000060 llist_t *control_tar_llist = NULL;
Denis Vlasenko67b23e62006-10-03 21:00:06 +000061 unsigned opt;
Denis Vlasenko0381d422008-07-10 23:06:00 +000062 const char *extract_dir;
63 int need_args;
Glenn L McGrath1f28b902004-01-07 09:24:06 +000064
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000065 /* Setup the tar archive handle */
Glenn L McGrath18bbca12002-11-05 01:52:23 +000066 tar_archive = init_handle();
Glenn L McGrath9aff9032001-06-13 07:26:39 +000067
Eric Andersenc7bda1c2004-03-15 08:29:22 +000068 /* Setup an ar archive handle that refers to the gzip sub archive */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000069 ar_archive = init_handle();
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +010070 ar_archive->dpkg__sub_archive = tar_archive;
Glenn L McGrath18bbca12002-11-05 01:52:23 +000071 ar_archive->filter = filter_accept_list_reassign;
72
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +000073#if ENABLE_FEATURE_SEAMLESS_GZ
Pascal Bellardc62f2292010-06-07 01:20:41 +020074 llist_add_to(&ar_archive->accept, (char*)"data.tar.gz");
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000075 llist_add_to(&control_tar_llist, (char*)"control.tar.gz");
Glenn L McGrath18bbca12002-11-05 01:52:23 +000076#endif
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +000077#if ENABLE_FEATURE_SEAMLESS_BZ2
Pascal Bellardc62f2292010-06-07 01:20:41 +020078 llist_add_to(&ar_archive->accept, (char*)"data.tar.bz2");
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000079 llist_add_to(&control_tar_llist, (char*)"control.tar.bz2");
Glenn L McGrath18bbca12002-11-05 01:52:23 +000080#endif
Pascal Bellardc62f2292010-06-07 01:20:41 +020081#if ENABLE_FEATURE_SEAMLESS_LZMA
82 llist_add_to(&ar_archive->accept, (char*)"data.tar.lzma");
83 llist_add_to(&control_tar_llist, (char*)"control.tar.lzma");
84#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000085
Denis Vlasenkoe7fca512007-11-10 01:32:18 +000086 opt_complementary = "c--efXx:e--cfXx:f--ceXx:X--cefx:x--cefX";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000087 opt = getopt32(argv, "cefXx");
Denis Vlasenko0381d422008-07-10 23:06:00 +000088 argv += optind;
89 argc -= optind;
Glenn L McGrath1f28b902004-01-07 09:24:06 +000090
91 if (opt & DPKG_DEB_OPT_CONTENTS) {
92 tar_archive->action_header = header_verbose_list;
93 }
Denis Vlasenko0381d422008-07-10 23:06:00 +000094 extract_dir = NULL;
95 need_args = 1;
Glenn L McGrath1f28b902004-01-07 09:24:06 +000096 if (opt & DPKG_DEB_OPT_CONTROL) {
97 ar_archive->accept = control_tar_llist;
98 tar_archive->action_data = data_extract_all;
Denis Vlasenko0381d422008-07-10 23:06:00 +000099 if (1 == argc) {
Glenn L McGrath1f28b902004-01-07 09:24:06 +0000100 extract_dir = "./DEBIAN";
101 } else {
Denis Vlasenko0381d422008-07-10 23:06:00 +0000102 need_args++;
Glenn L McGrath58a40852001-01-02 23:49:26 +0000103 }
104 }
Glenn L McGrath1f28b902004-01-07 09:24:06 +0000105 if (opt & DPKG_DEB_OPT_FIELD) {
106 /* Print the entire control file
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000107 * it should accept a second argument which specifies a
Glenn L McGrath1f28b902004-01-07 09:24:06 +0000108 * specific field to print */
109 ar_archive->accept = control_tar_llist;
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000110 llist_add_to(&(tar_archive->accept), (char*)"./control");
Glenn L McGrath1f28b902004-01-07 09:24:06 +0000111 tar_archive->filter = filter_accept_list;
112 tar_archive->action_data = data_extract_to_stdout;
113 }
114 if (opt & DPKG_DEB_OPT_EXTRACT) {
115 tar_archive->action_header = header_list;
116 }
117 if (opt & (DPKG_DEB_OPT_EXTRACT_VERBOSE | DPKG_DEB_OPT_EXTRACT)) {
118 tar_archive->action_data = data_extract_all;
Denis Vlasenko0381d422008-07-10 23:06:00 +0000119 need_args = 2;
Glenn L McGrath1f28b902004-01-07 09:24:06 +0000120 }
Glenn L McGrath58a40852001-01-02 23:49:26 +0000121
Denis Vlasenko0381d422008-07-10 23:06:00 +0000122 if (need_args != argc) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000123 bb_show_usage();
Glenn L McGrath58a40852001-01-02 23:49:26 +0000124 }
Glenn L McGrath6785b512001-04-12 11:48:02 +0000125
Denis Vlasenko0381d422008-07-10 23:06:00 +0000126 tar_archive->src_fd = ar_archive->src_fd = xopen(argv[0], O_RDONLY);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000127
Denis Vlasenko0381d422008-07-10 23:06:00 +0000128 /* Work out where to extract the files */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000129 /* 2nd argument is a dir name */
Denis Vlasenko0381d422008-07-10 23:06:00 +0000130 if (argv[1]) {
131 extract_dir = argv[1];
Glenn L McGrath1f28b902004-01-07 09:24:06 +0000132 }
133 if (extract_dir) {
Bernhard Reutner-Fischer44e216f2006-06-10 11:29:44 +0000134 mkdir(extract_dir, 0777); /* bb_make_directory(extract_dir, 0777, 0) */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000135 xchdir(extract_dir);
Glenn L McGrath1f28b902004-01-07 09:24:06 +0000136 }
Denis Vlasenko0381d422008-07-10 23:06:00 +0000137
138 /* Do it */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000139 unpack_ar_archive(ar_archive);
Glenn L McGrath9aff9032001-06-13 07:26:39 +0000140
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000141 /* Cleanup */
Denis Vlasenko0381d422008-07-10 23:06:00 +0000142 if (ENABLE_FEATURE_CLEAN_UP)
143 close(ar_archive->src_fd);
Glenn L McGrath445fb952001-04-13 04:02:57 +0000144
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000145 return EXIT_SUCCESS;
Glenn L McGrath58a40852001-01-02 23:49:26 +0000146}