blob: ebbc7f035482bebe736ac8cd8542a301bd394a69 [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"
Denys Vlasenkoce3a98a2016-12-23 13:52:53 +010010//config: default y
Denys Vlasenkof6beef62013-11-14 11:39:00 +010011//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.
Denys Vlasenkof6beef62013-11-14 11:39:00 +010019
Denys Vlasenko36184a42013-11-14 09:54:24 +010020//applet:IF_DPKG_DEB(APPLET_ODDNAME(dpkg-deb, dpkg_deb, BB_DIR_USR_BIN, BB_SUID_DROP, dpkg_deb))
Denys Vlasenko6747bda2016-11-27 03:57:08 +010021
Denys Vlasenko66620fa2013-11-14 09:53:52 +010022//kbuild:lib-$(CONFIG_DPKG_DEB) += dpkg_deb.o
23
Pere Orga1f4447b2011-03-27 22:40:30 +020024//usage:#define dpkg_deb_trivial_usage
Denys Vlasenko351ab822016-11-27 04:48:53 +010025//usage: "[-cefxX] FILE [DIR]"
Pere Orga1f4447b2011-03-27 22:40:30 +020026//usage:#define dpkg_deb_full_usage "\n\n"
Denys Vlasenko351ab822016-11-27 04:48:53 +010027//usage: "Perform actions on Debian packages (.deb)\n"
28//usage: "\n -c List files"
29//usage: "\n -f Print control fields"
30//usage: "\n -e Extract control files to DIR (default: ./DEBIAN)"
31//usage: "\n -x Extract files to DIR (no default)"
32//usage: "\n -X Verbose -x"
Pere Orga1f4447b2011-03-27 22:40:30 +020033//usage:
34//usage:#define dpkg_deb_example_usage
35//usage: "$ dpkg-deb -X ./busybox_0.48-1_i386.deb /tmp\n"
36
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000037#include "libbb.h"
Denys Vlasenkod184a722011-09-22 12:45:14 +020038#include "bb_archive.h"
Glenn L McGrath58a40852001-01-02 23:49:26 +000039
Denys Vlasenkofb132e42010-10-29 11:46:52 +020040#define DPKG_DEB_OPT_CONTENTS 1
41#define DPKG_DEB_OPT_CONTROL 2
42#define DPKG_DEB_OPT_FIELD 4
Denys Vlasenko351ab822016-11-27 04:48:53 +010043#define DPKG_DEB_OPT_EXTRACT_VERBOSE 8
44#define DPKG_DEB_OPT_EXTRACT 16
Glenn L McGrath1f28b902004-01-07 09:24:06 +000045
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000046int dpkg_deb_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko351ab822016-11-27 04:48:53 +010047int dpkg_deb_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrathbc9afad2001-02-11 03:32:41 +000048{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000049 archive_handle_t *ar_archive;
Glenn L McGrath18bbca12002-11-05 01:52:23 +000050 archive_handle_t *tar_archive;
Glenn L McGrath66125c82002-12-08 00:54:33 +000051 llist_t *control_tar_llist = NULL;
Denis Vlasenko67b23e62006-10-03 21:00:06 +000052 unsigned opt;
Denis Vlasenko0381d422008-07-10 23:06:00 +000053 const char *extract_dir;
Glenn L McGrath1f28b902004-01-07 09:24:06 +000054
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000055 /* Setup the tar archive handle */
Glenn L McGrath18bbca12002-11-05 01:52:23 +000056 tar_archive = init_handle();
Glenn L McGrath9aff9032001-06-13 07:26:39 +000057
Eric Andersenc7bda1c2004-03-15 08:29:22 +000058 /* Setup an ar archive handle that refers to the gzip sub archive */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000059 ar_archive = init_handle();
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +010060 ar_archive->dpkg__sub_archive = tar_archive;
Glenn L McGrath18bbca12002-11-05 01:52:23 +000061 ar_archive->filter = filter_accept_list_reassign;
62
Denys Vlasenko08f9ffc2015-01-30 15:15:38 +010063 llist_add_to(&ar_archive->accept, (char*)"data.tar");
64 llist_add_to(&control_tar_llist, (char*)"control.tar");
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +000065#if ENABLE_FEATURE_SEAMLESS_GZ
Pascal Bellardc62f2292010-06-07 01:20:41 +020066 llist_add_to(&ar_archive->accept, (char*)"data.tar.gz");
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000067 llist_add_to(&control_tar_llist, (char*)"control.tar.gz");
Glenn L McGrath18bbca12002-11-05 01:52:23 +000068#endif
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +000069#if ENABLE_FEATURE_SEAMLESS_BZ2
Pascal Bellardc62f2292010-06-07 01:20:41 +020070 llist_add_to(&ar_archive->accept, (char*)"data.tar.bz2");
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000071 llist_add_to(&control_tar_llist, (char*)"control.tar.bz2");
Glenn L McGrath18bbca12002-11-05 01:52:23 +000072#endif
Pascal Bellardc62f2292010-06-07 01:20:41 +020073#if ENABLE_FEATURE_SEAMLESS_LZMA
74 llist_add_to(&ar_archive->accept, (char*)"data.tar.lzma");
75 llist_add_to(&control_tar_llist, (char*)"control.tar.lzma");
76#endif
Denys Vlasenko08f9ffc2015-01-30 15:15:38 +010077#if ENABLE_FEATURE_SEAMLESS_XZ
78 llist_add_to(&ar_archive->accept, (char*)"data.tar.xz");
79 llist_add_to(&control_tar_llist, (char*)"control.tar.xz");
80#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000081
Denys Vlasenko351ab822016-11-27 04:48:53 +010082 /* Must have 1 or 2 args */
83 opt_complementary = "-1:?2:c--efXx:e--cfXx:f--ceXx:X--cefx:x--cefX";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000084 opt = getopt32(argv, "cefXx");
Denis Vlasenko0381d422008-07-10 23:06:00 +000085 argv += optind;
Denys Vlasenko351ab822016-11-27 04:48:53 +010086 //argc -= optind;
Glenn L McGrath1f28b902004-01-07 09:24:06 +000087
Denys Vlasenko351ab822016-11-27 04:48:53 +010088 extract_dir = argv[1];
89 if (opt & DPKG_DEB_OPT_CONTENTS) { // -c
Glenn L McGrath1f28b902004-01-07 09:24:06 +000090 tar_archive->action_header = header_verbose_list;
Denys Vlasenko351ab822016-11-27 04:48:53 +010091 if (extract_dir)
92 bb_show_usage();
Glenn L McGrath1f28b902004-01-07 09:24:06 +000093 }
Denys Vlasenko351ab822016-11-27 04:48:53 +010094 if (opt & DPKG_DEB_OPT_FIELD) { // -f
95 /* Print the entire control file */
96//TODO: standard tool accepts an optional list of fields to print
Glenn L McGrath1f28b902004-01-07 09:24:06 +000097 ar_archive->accept = control_tar_llist;
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000098 llist_add_to(&(tar_archive->accept), (char*)"./control");
Glenn L McGrath1f28b902004-01-07 09:24:06 +000099 tar_archive->filter = filter_accept_list;
100 tar_archive->action_data = data_extract_to_stdout;
Denys Vlasenko351ab822016-11-27 04:48:53 +0100101 if (extract_dir)
102 bb_show_usage();
Glenn L McGrath1f28b902004-01-07 09:24:06 +0000103 }
Denys Vlasenko351ab822016-11-27 04:48:53 +0100104 if (opt & DPKG_DEB_OPT_CONTROL) { // -e
105 ar_archive->accept = control_tar_llist;
Glenn L McGrath1f28b902004-01-07 09:24:06 +0000106 tar_archive->action_data = data_extract_all;
Denys Vlasenko351ab822016-11-27 04:48:53 +0100107 if (!extract_dir)
108 extract_dir = "./DEBIAN";
109 }
110 if (opt & (DPKG_DEB_OPT_EXTRACT_VERBOSE | DPKG_DEB_OPT_EXTRACT)) { // -Xx
111 if (opt & DPKG_DEB_OPT_EXTRACT_VERBOSE)
112 tar_archive->action_header = header_list;
113 tar_archive->action_data = data_extract_all;
114 if (!extract_dir)
115 bb_show_usage();
Glenn L McGrath1f28b902004-01-07 09:24:06 +0000116 }
Glenn L McGrath58a40852001-01-02 23:49:26 +0000117
Denys Vlasenko351ab822016-11-27 04:48:53 +0100118 /* Standard tool supports "-" */
119 tar_archive->src_fd = ar_archive->src_fd = xopen_stdin(argv[0]);
Glenn L McGrath6785b512001-04-12 11:48:02 +0000120
Glenn L McGrath1f28b902004-01-07 09:24:06 +0000121 if (extract_dir) {
Bernhard Reutner-Fischer44e216f2006-06-10 11:29:44 +0000122 mkdir(extract_dir, 0777); /* bb_make_directory(extract_dir, 0777, 0) */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000123 xchdir(extract_dir);
Glenn L McGrath1f28b902004-01-07 09:24:06 +0000124 }
Denis Vlasenko0381d422008-07-10 23:06:00 +0000125
126 /* Do it */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000127 unpack_ar_archive(ar_archive);
Glenn L McGrath9aff9032001-06-13 07:26:39 +0000128
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000129 /* Cleanup */
Denis Vlasenko0381d422008-07-10 23:06:00 +0000130 if (ENABLE_FEATURE_CLEAN_UP)
131 close(ar_archive->src_fd);
Glenn L McGrath445fb952001-04-13 04:02:57 +0000132
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000133 return EXIT_SUCCESS;
Glenn L McGrath58a40852001-01-02 23:49:26 +0000134}