blob: 45a791bba0716aea17132c9b11489e6b2e4f3c4e [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 *
Bernhard Reutner-Fischerd9cf7ac2006-04-12 18:39:58 +00005 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath58a40852001-01-02 23:49:26 +00006 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +00007#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +00008#include "unarchive.h"
Glenn L McGrath58a40852001-01-02 23:49:26 +00009
Glenn L McGrath1f28b902004-01-07 09:24:06 +000010#define DPKG_DEB_OPT_CONTENTS 1
11#define DPKG_DEB_OPT_CONTROL 2
12#define DPKG_DEB_OPT_FIELD 4
13#define DPKG_DEB_OPT_EXTRACT 8
14#define DPKG_DEB_OPT_EXTRACT_VERBOSE 16
15
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000016int dpkg_deb_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +000017int dpkg_deb_main(int argc, char **argv)
Glenn L McGrathbc9afad2001-02-11 03:32:41 +000018{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000019 archive_handle_t *ar_archive;
Glenn L McGrath18bbca12002-11-05 01:52:23 +000020 archive_handle_t *tar_archive;
Glenn L McGrath66125c82002-12-08 00:54:33 +000021 llist_t *control_tar_llist = NULL;
Denis Vlasenko67b23e62006-10-03 21:00:06 +000022 unsigned opt;
Denis Vlasenko0381d422008-07-10 23:06:00 +000023 const char *extract_dir;
24 int need_args;
Glenn L McGrath1f28b902004-01-07 09:24:06 +000025
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000026 /* Setup the tar archive handle */
Glenn L McGrath18bbca12002-11-05 01:52:23 +000027 tar_archive = init_handle();
Glenn L McGrath9aff9032001-06-13 07:26:39 +000028
Eric Andersenc7bda1c2004-03-15 08:29:22 +000029 /* Setup an ar archive handle that refers to the gzip sub archive */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000030 ar_archive = init_handle();
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +010031 ar_archive->dpkg__sub_archive = tar_archive;
Glenn L McGrath18bbca12002-11-05 01:52:23 +000032 ar_archive->filter = filter_accept_list_reassign;
33
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +000034#if ENABLE_FEATURE_SEAMLESS_GZ
Pascal Bellardc62f2292010-06-07 01:20:41 +020035 llist_add_to(&ar_archive->accept, (char*)"data.tar.gz");
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000036 llist_add_to(&control_tar_llist, (char*)"control.tar.gz");
Glenn L McGrath18bbca12002-11-05 01:52:23 +000037#endif
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +000038#if ENABLE_FEATURE_SEAMLESS_BZ2
Pascal Bellardc62f2292010-06-07 01:20:41 +020039 llist_add_to(&ar_archive->accept, (char*)"data.tar.bz2");
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000040 llist_add_to(&control_tar_llist, (char*)"control.tar.bz2");
Glenn L McGrath18bbca12002-11-05 01:52:23 +000041#endif
Pascal Bellardc62f2292010-06-07 01:20:41 +020042#if ENABLE_FEATURE_SEAMLESS_LZMA
43 llist_add_to(&ar_archive->accept, (char*)"data.tar.lzma");
44 llist_add_to(&control_tar_llist, (char*)"control.tar.lzma");
45#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000046
Denis Vlasenkoe7fca512007-11-10 01:32:18 +000047 opt_complementary = "c--efXx:e--cfXx:f--ceXx:X--cefx:x--cefX";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000048 opt = getopt32(argv, "cefXx");
Denis Vlasenko0381d422008-07-10 23:06:00 +000049 argv += optind;
50 argc -= optind;
Glenn L McGrath1f28b902004-01-07 09:24:06 +000051
52 if (opt & DPKG_DEB_OPT_CONTENTS) {
53 tar_archive->action_header = header_verbose_list;
54 }
Denis Vlasenko0381d422008-07-10 23:06:00 +000055 extract_dir = NULL;
56 need_args = 1;
Glenn L McGrath1f28b902004-01-07 09:24:06 +000057 if (opt & DPKG_DEB_OPT_CONTROL) {
58 ar_archive->accept = control_tar_llist;
59 tar_archive->action_data = data_extract_all;
Denis Vlasenko0381d422008-07-10 23:06:00 +000060 if (1 == argc) {
Glenn L McGrath1f28b902004-01-07 09:24:06 +000061 extract_dir = "./DEBIAN";
62 } else {
Denis Vlasenko0381d422008-07-10 23:06:00 +000063 need_args++;
Glenn L McGrath58a40852001-01-02 23:49:26 +000064 }
65 }
Glenn L McGrath1f28b902004-01-07 09:24:06 +000066 if (opt & DPKG_DEB_OPT_FIELD) {
67 /* Print the entire control file
Eric Andersenc7bda1c2004-03-15 08:29:22 +000068 * it should accept a second argument which specifies a
Glenn L McGrath1f28b902004-01-07 09:24:06 +000069 * specific field to print */
70 ar_archive->accept = control_tar_llist;
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000071 llist_add_to(&(tar_archive->accept), (char*)"./control");
Glenn L McGrath1f28b902004-01-07 09:24:06 +000072 tar_archive->filter = filter_accept_list;
73 tar_archive->action_data = data_extract_to_stdout;
74 }
75 if (opt & DPKG_DEB_OPT_EXTRACT) {
76 tar_archive->action_header = header_list;
77 }
78 if (opt & (DPKG_DEB_OPT_EXTRACT_VERBOSE | DPKG_DEB_OPT_EXTRACT)) {
79 tar_archive->action_data = data_extract_all;
Denis Vlasenko0381d422008-07-10 23:06:00 +000080 need_args = 2;
Glenn L McGrath1f28b902004-01-07 09:24:06 +000081 }
Glenn L McGrath58a40852001-01-02 23:49:26 +000082
Denis Vlasenko0381d422008-07-10 23:06:00 +000083 if (need_args != argc) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000084 bb_show_usage();
Glenn L McGrath58a40852001-01-02 23:49:26 +000085 }
Glenn L McGrath6785b512001-04-12 11:48:02 +000086
Denis Vlasenko0381d422008-07-10 23:06:00 +000087 tar_archive->src_fd = ar_archive->src_fd = xopen(argv[0], O_RDONLY);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000088
Denis Vlasenko0381d422008-07-10 23:06:00 +000089 /* Work out where to extract the files */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000090 /* 2nd argument is a dir name */
Denis Vlasenko0381d422008-07-10 23:06:00 +000091 if (argv[1]) {
92 extract_dir = argv[1];
Glenn L McGrath1f28b902004-01-07 09:24:06 +000093 }
94 if (extract_dir) {
Bernhard Reutner-Fischer44e216f2006-06-10 11:29:44 +000095 mkdir(extract_dir, 0777); /* bb_make_directory(extract_dir, 0777, 0) */
Rob Landleyd921b2e2006-08-03 15:41:12 +000096 xchdir(extract_dir);
Glenn L McGrath1f28b902004-01-07 09:24:06 +000097 }
Denis Vlasenko0381d422008-07-10 23:06:00 +000098
99 /* Do it */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000100 unpack_ar_archive(ar_archive);
Glenn L McGrath9aff9032001-06-13 07:26:39 +0000101
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000102 /* Cleanup */
Denis Vlasenko0381d422008-07-10 23:06:00 +0000103 if (ENABLE_FEATURE_CLEAN_UP)
104 close(ar_archive->src_fd);
Glenn L McGrath445fb952001-04-13 04:02:57 +0000105
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000106 return EXIT_SUCCESS;
Glenn L McGrath58a40852001-01-02 23:49:26 +0000107}