blob: a5a80439dbf86ba943974f1a15fc60e11aa22567 [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 */
Denys Vlasenkof6beef62013-11-14 11:39:00 +01007//config:config DPKG_DEB
Denys Vlasenkof9b4cc12018-12-28 18:24:45 +01008//config: bool "dpkg-deb (30 kb)"
Denys Vlasenkoce3a98a2016-12-23 13:52:53 +01009//config: default y
Denys Vlasenkof6beef62013-11-14 11:39:00 +010010//config: select FEATURE_SEAMLESS_GZ
11//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020012//config: dpkg-deb unpacks and provides information about Debian archives.
Denys Vlasenkof6beef62013-11-14 11:39:00 +010013//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020014//config: This implementation of dpkg-deb cannot pack archives.
Denys Vlasenkof6beef62013-11-14 11:39:00 +010015//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020016//config: Unless you have a specific application which requires dpkg-deb,
17//config: say N here.
Denys Vlasenkof6beef62013-11-14 11:39:00 +010018
Denys Vlasenko36184a42013-11-14 09:54:24 +010019//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 +010020
Denys Vlasenko66620fa2013-11-14 09:53:52 +010021//kbuild:lib-$(CONFIG_DPKG_DEB) += dpkg_deb.o
22
Pere Orga1f4447b2011-03-27 22:40:30 +020023//usage:#define dpkg_deb_trivial_usage
Denys Vlasenko351ab822016-11-27 04:48:53 +010024//usage: "[-cefxX] FILE [DIR]"
Pere Orga1f4447b2011-03-27 22:40:30 +020025//usage:#define dpkg_deb_full_usage "\n\n"
Denys Vlasenko351ab822016-11-27 04:48:53 +010026//usage: "Perform actions on Debian packages (.deb)\n"
27//usage: "\n -c List files"
28//usage: "\n -f Print control fields"
29//usage: "\n -e Extract control files to DIR (default: ./DEBIAN)"
30//usage: "\n -x Extract files to DIR (no default)"
Denys Vlasenkoa2f18d92020-12-18 04:12:51 +010031//usage: "\n -X Verbose extract"
Pere Orga1f4447b2011-03-27 22:40:30 +020032//usage:
33//usage:#define dpkg_deb_example_usage
34//usage: "$ dpkg-deb -X ./busybox_0.48-1_i386.deb /tmp\n"
35
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000036#include "libbb.h"
Denys Vlasenkod184a722011-09-22 12:45:14 +020037#include "bb_archive.h"
Glenn L McGrath58a40852001-01-02 23:49:26 +000038
Denys Vlasenkofb132e42010-10-29 11:46:52 +020039#define DPKG_DEB_OPT_CONTENTS 1
40#define DPKG_DEB_OPT_CONTROL 2
41#define DPKG_DEB_OPT_FIELD 4
Denys Vlasenko351ab822016-11-27 04:48:53 +010042#define DPKG_DEB_OPT_EXTRACT_VERBOSE 8
43#define DPKG_DEB_OPT_EXTRACT 16
Glenn L McGrath1f28b902004-01-07 09:24:06 +000044
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000045int dpkg_deb_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko351ab822016-11-27 04:48:53 +010046int dpkg_deb_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrathbc9afad2001-02-11 03:32:41 +000047{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000048 archive_handle_t *ar_archive;
Glenn L McGrath18bbca12002-11-05 01:52:23 +000049 archive_handle_t *tar_archive;
Glenn L McGrath66125c82002-12-08 00:54:33 +000050 llist_t *control_tar_llist = NULL;
Denis Vlasenko67b23e62006-10-03 21:00:06 +000051 unsigned opt;
Denis Vlasenko0381d422008-07-10 23:06:00 +000052 const char *extract_dir;
Glenn L McGrath1f28b902004-01-07 09:24:06 +000053
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000054 /* Setup the tar archive handle */
Glenn L McGrath18bbca12002-11-05 01:52:23 +000055 tar_archive = init_handle();
Glenn L McGrath9aff9032001-06-13 07:26:39 +000056
Eric Andersenc7bda1c2004-03-15 08:29:22 +000057 /* Setup an ar archive handle that refers to the gzip sub archive */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000058 ar_archive = init_handle();
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +010059 ar_archive->dpkg__sub_archive = tar_archive;
Glenn L McGrath18bbca12002-11-05 01:52:23 +000060 ar_archive->filter = filter_accept_list_reassign;
61
Denys Vlasenko08f9ffc2015-01-30 15:15:38 +010062 llist_add_to(&ar_archive->accept, (char*)"data.tar");
63 llist_add_to(&control_tar_llist, (char*)"control.tar");
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +000064#if ENABLE_FEATURE_SEAMLESS_GZ
Pascal Bellardc62f2292010-06-07 01:20:41 +020065 llist_add_to(&ar_archive->accept, (char*)"data.tar.gz");
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000066 llist_add_to(&control_tar_llist, (char*)"control.tar.gz");
Glenn L McGrath18bbca12002-11-05 01:52:23 +000067#endif
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +000068#if ENABLE_FEATURE_SEAMLESS_BZ2
Pascal Bellardc62f2292010-06-07 01:20:41 +020069 llist_add_to(&ar_archive->accept, (char*)"data.tar.bz2");
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000070 llist_add_to(&control_tar_llist, (char*)"control.tar.bz2");
Glenn L McGrath18bbca12002-11-05 01:52:23 +000071#endif
Pascal Bellardc62f2292010-06-07 01:20:41 +020072#if ENABLE_FEATURE_SEAMLESS_LZMA
73 llist_add_to(&ar_archive->accept, (char*)"data.tar.lzma");
74 llist_add_to(&control_tar_llist, (char*)"control.tar.lzma");
75#endif
Denys Vlasenko08f9ffc2015-01-30 15:15:38 +010076#if ENABLE_FEATURE_SEAMLESS_XZ
77 llist_add_to(&ar_archive->accept, (char*)"data.tar.xz");
78 llist_add_to(&control_tar_llist, (char*)"control.tar.xz");
79#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000080
Denys Vlasenko351ab822016-11-27 04:48:53 +010081 /* Must have 1 or 2 args */
Denys Vlasenko22542ec2017-08-08 21:55:02 +020082 opt = getopt32(argv, "^" "cefXx"
83 "\0" "-1:?2:c--efXx:e--cfXx:f--ceXx:X--cefx:x--cefX"
84 );
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}