blob: f6bf9eb046736357b0780091771297277dbb786d [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
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: dpkg-deb unpacks and provides information about Debian archives.
Denys Vlasenkof6beef62013-11-14 11:39:00 +010014//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020015//config: This implementation of dpkg-deb cannot pack archives.
Denys Vlasenkof6beef62013-11-14 11:39:00 +010016//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020017//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 */
Denys Vlasenko22542ec2017-08-08 21:55:02 +020083 opt = getopt32(argv, "^" "cefXx"
84 "\0" "-1:?2:c--efXx:e--cfXx:f--ceXx:X--cefx:x--cefX"
85 );
Denis Vlasenko0381d422008-07-10 23:06:00 +000086 argv += optind;
Denys Vlasenko351ab822016-11-27 04:48:53 +010087 //argc -= optind;
Glenn L McGrath1f28b902004-01-07 09:24:06 +000088
Denys Vlasenko351ab822016-11-27 04:48:53 +010089 extract_dir = argv[1];
90 if (opt & DPKG_DEB_OPT_CONTENTS) { // -c
Glenn L McGrath1f28b902004-01-07 09:24:06 +000091 tar_archive->action_header = header_verbose_list;
Denys Vlasenko351ab822016-11-27 04:48:53 +010092 if (extract_dir)
93 bb_show_usage();
Glenn L McGrath1f28b902004-01-07 09:24:06 +000094 }
Denys Vlasenko351ab822016-11-27 04:48:53 +010095 if (opt & DPKG_DEB_OPT_FIELD) { // -f
96 /* Print the entire control file */
97//TODO: standard tool accepts an optional list of fields to print
Glenn L McGrath1f28b902004-01-07 09:24:06 +000098 ar_archive->accept = control_tar_llist;
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000099 llist_add_to(&(tar_archive->accept), (char*)"./control");
Glenn L McGrath1f28b902004-01-07 09:24:06 +0000100 tar_archive->filter = filter_accept_list;
101 tar_archive->action_data = data_extract_to_stdout;
Denys Vlasenko351ab822016-11-27 04:48:53 +0100102 if (extract_dir)
103 bb_show_usage();
Glenn L McGrath1f28b902004-01-07 09:24:06 +0000104 }
Denys Vlasenko351ab822016-11-27 04:48:53 +0100105 if (opt & DPKG_DEB_OPT_CONTROL) { // -e
106 ar_archive->accept = control_tar_llist;
Glenn L McGrath1f28b902004-01-07 09:24:06 +0000107 tar_archive->action_data = data_extract_all;
Denys Vlasenko351ab822016-11-27 04:48:53 +0100108 if (!extract_dir)
109 extract_dir = "./DEBIAN";
110 }
111 if (opt & (DPKG_DEB_OPT_EXTRACT_VERBOSE | DPKG_DEB_OPT_EXTRACT)) { // -Xx
112 if (opt & DPKG_DEB_OPT_EXTRACT_VERBOSE)
113 tar_archive->action_header = header_list;
114 tar_archive->action_data = data_extract_all;
115 if (!extract_dir)
116 bb_show_usage();
Glenn L McGrath1f28b902004-01-07 09:24:06 +0000117 }
Glenn L McGrath58a40852001-01-02 23:49:26 +0000118
Denys Vlasenko351ab822016-11-27 04:48:53 +0100119 /* Standard tool supports "-" */
120 tar_archive->src_fd = ar_archive->src_fd = xopen_stdin(argv[0]);
Glenn L McGrath6785b512001-04-12 11:48:02 +0000121
Glenn L McGrath1f28b902004-01-07 09:24:06 +0000122 if (extract_dir) {
Bernhard Reutner-Fischer44e216f2006-06-10 11:29:44 +0000123 mkdir(extract_dir, 0777); /* bb_make_directory(extract_dir, 0777, 0) */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000124 xchdir(extract_dir);
Glenn L McGrath1f28b902004-01-07 09:24:06 +0000125 }
Denis Vlasenko0381d422008-07-10 23:06:00 +0000126
127 /* Do it */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000128 unpack_ar_archive(ar_archive);
Glenn L McGrath9aff9032001-06-13 07:26:39 +0000129
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000130 /* Cleanup */
Denis Vlasenko0381d422008-07-10 23:06:00 +0000131 if (ENABLE_FEATURE_CLEAN_UP)
132 close(ar_archive->src_fd);
Glenn L McGrath445fb952001-04-13 04:02:57 +0000133
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000134 return EXIT_SUCCESS;
Glenn L McGrath58a40852001-01-02 23:49:26 +0000135}