blob: f94c90c55f5ba2564c3783b3ff306eda3f877250 [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();
Glenn L McGrath18bbca12002-11-05 01:52:23 +000031 ar_archive->sub_archive = tar_archive;
32 ar_archive->filter = filter_accept_list_reassign;
33
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +000034#if ENABLE_FEATURE_SEAMLESS_GZ
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000035 llist_add_to(&(ar_archive->accept), (char*)"data.tar.gz");
36 llist_add_to(&control_tar_llist, (char*)"control.tar.gz");
Glenn L McGrath18bbca12002-11-05 01:52:23 +000037#endif
38
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +000039#if ENABLE_FEATURE_SEAMLESS_BZ2
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000040 llist_add_to(&(ar_archive->accept), (char*)"data.tar.bz2");
41 llist_add_to(&control_tar_llist, (char*)"control.tar.bz2");
Glenn L McGrath18bbca12002-11-05 01:52:23 +000042#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000043
Denis Vlasenkoe7fca512007-11-10 01:32:18 +000044 opt_complementary = "c--efXx:e--cfXx:f--ceXx:X--cefx:x--cefX";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000045 opt = getopt32(argv, "cefXx");
Denis Vlasenko0381d422008-07-10 23:06:00 +000046 argv += optind;
47 argc -= optind;
Glenn L McGrath1f28b902004-01-07 09:24:06 +000048
49 if (opt & DPKG_DEB_OPT_CONTENTS) {
50 tar_archive->action_header = header_verbose_list;
51 }
Denis Vlasenko0381d422008-07-10 23:06:00 +000052 extract_dir = NULL;
53 need_args = 1;
Glenn L McGrath1f28b902004-01-07 09:24:06 +000054 if (opt & DPKG_DEB_OPT_CONTROL) {
55 ar_archive->accept = control_tar_llist;
56 tar_archive->action_data = data_extract_all;
Denis Vlasenko0381d422008-07-10 23:06:00 +000057 if (1 == argc) {
Glenn L McGrath1f28b902004-01-07 09:24:06 +000058 extract_dir = "./DEBIAN";
59 } else {
Denis Vlasenko0381d422008-07-10 23:06:00 +000060 need_args++;
Glenn L McGrath58a40852001-01-02 23:49:26 +000061 }
62 }
Glenn L McGrath1f28b902004-01-07 09:24:06 +000063 if (opt & DPKG_DEB_OPT_FIELD) {
64 /* Print the entire control file
Eric Andersenc7bda1c2004-03-15 08:29:22 +000065 * it should accept a second argument which specifies a
Glenn L McGrath1f28b902004-01-07 09:24:06 +000066 * specific field to print */
67 ar_archive->accept = control_tar_llist;
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000068 llist_add_to(&(tar_archive->accept), (char*)"./control");
Glenn L McGrath1f28b902004-01-07 09:24:06 +000069 tar_archive->filter = filter_accept_list;
70 tar_archive->action_data = data_extract_to_stdout;
71 }
72 if (opt & DPKG_DEB_OPT_EXTRACT) {
73 tar_archive->action_header = header_list;
74 }
75 if (opt & (DPKG_DEB_OPT_EXTRACT_VERBOSE | DPKG_DEB_OPT_EXTRACT)) {
76 tar_archive->action_data = data_extract_all;
Denis Vlasenko0381d422008-07-10 23:06:00 +000077 need_args = 2;
Glenn L McGrath1f28b902004-01-07 09:24:06 +000078 }
Glenn L McGrath58a40852001-01-02 23:49:26 +000079
Denis Vlasenko0381d422008-07-10 23:06:00 +000080 if (need_args != argc) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000081 bb_show_usage();
Glenn L McGrath58a40852001-01-02 23:49:26 +000082 }
Glenn L McGrath6785b512001-04-12 11:48:02 +000083
Denis Vlasenko0381d422008-07-10 23:06:00 +000084 tar_archive->src_fd = ar_archive->src_fd = xopen(argv[0], O_RDONLY);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000085
Denis Vlasenko0381d422008-07-10 23:06:00 +000086 /* Work out where to extract the files */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000087 /* 2nd argument is a dir name */
Denis Vlasenko0381d422008-07-10 23:06:00 +000088 if (argv[1]) {
89 extract_dir = argv[1];
Glenn L McGrath1f28b902004-01-07 09:24:06 +000090 }
91 if (extract_dir) {
Bernhard Reutner-Fischer44e216f2006-06-10 11:29:44 +000092 mkdir(extract_dir, 0777); /* bb_make_directory(extract_dir, 0777, 0) */
Rob Landleyd921b2e2006-08-03 15:41:12 +000093 xchdir(extract_dir);
Glenn L McGrath1f28b902004-01-07 09:24:06 +000094 }
Denis Vlasenko0381d422008-07-10 23:06:00 +000095
96 /* Do it */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000097 unpack_ar_archive(ar_archive);
Glenn L McGrath9aff9032001-06-13 07:26:39 +000098
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000099 /* Cleanup */
Denis Vlasenko0381d422008-07-10 23:06:00 +0000100 if (ENABLE_FEATURE_CLEAN_UP)
101 close(ar_archive->src_fd);
Glenn L McGrath445fb952001-04-13 04:02:57 +0000102
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000103 return EXIT_SUCCESS;
Glenn L McGrath58a40852001-01-02 23:49:26 +0000104}