blob: cbacc91f8c4b4da37bc1a0f81ff97a88fbbf711b [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 Vlasenkob6aae0f2007-01-29 22:51:25 +000023 const char *extract_dir = NULL;
Glenn L McGrath1f28b902004-01-07 09:24:06 +000024 short argcount = 1;
25
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 Vlasenkoe3241842007-08-13 10:36:25 +000034#if ENABLE_FEATURE_DEB_TAR_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 Vlasenkoe3241842007-08-13 10:36:25 +000039#if ENABLE_FEATURE_DEB_TAR_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");
Glenn L McGrath1f28b902004-01-07 09:24:06 +000046
47 if (opt & DPKG_DEB_OPT_CONTENTS) {
48 tar_archive->action_header = header_verbose_list;
49 }
50 if (opt & DPKG_DEB_OPT_CONTROL) {
51 ar_archive->accept = control_tar_llist;
52 tar_archive->action_data = data_extract_all;
53 if (optind + 1 == argc) {
54 extract_dir = "./DEBIAN";
55 } else {
56 argcount++;
Glenn L McGrath58a40852001-01-02 23:49:26 +000057 }
58 }
Glenn L McGrath1f28b902004-01-07 09:24:06 +000059 if (opt & DPKG_DEB_OPT_FIELD) {
60 /* Print the entire control file
Eric Andersenc7bda1c2004-03-15 08:29:22 +000061 * it should accept a second argument which specifies a
Glenn L McGrath1f28b902004-01-07 09:24:06 +000062 * specific field to print */
63 ar_archive->accept = control_tar_llist;
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000064 llist_add_to(&(tar_archive->accept), (char*)"./control");
Glenn L McGrath1f28b902004-01-07 09:24:06 +000065 tar_archive->filter = filter_accept_list;
66 tar_archive->action_data = data_extract_to_stdout;
67 }
68 if (opt & DPKG_DEB_OPT_EXTRACT) {
69 tar_archive->action_header = header_list;
70 }
71 if (opt & (DPKG_DEB_OPT_EXTRACT_VERBOSE | DPKG_DEB_OPT_EXTRACT)) {
72 tar_archive->action_data = data_extract_all;
73 argcount = 2;
74 }
Glenn L McGrath58a40852001-01-02 23:49:26 +000075
"Vladimir N. Oleynik"27421a12005-09-05 14:46:07 +000076 if ((optind + argcount) != argc) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000077 bb_show_usage();
Glenn L McGrath58a40852001-01-02 23:49:26 +000078 }
Glenn L McGrath6785b512001-04-12 11:48:02 +000079
Rob Landleyd921b2e2006-08-03 15:41:12 +000080 tar_archive->src_fd = ar_archive->src_fd = xopen(argv[optind++], O_RDONLY);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000081
Glenn L McGrath9aff9032001-06-13 07:26:39 +000082 /* Workout where to extract the files */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000083 /* 2nd argument is a dir name */
Glenn L McGrath1f28b902004-01-07 09:24:06 +000084 if (argv[optind]) {
85 extract_dir = argv[optind];
86 }
87 if (extract_dir) {
Bernhard Reutner-Fischer44e216f2006-06-10 11:29:44 +000088 mkdir(extract_dir, 0777); /* bb_make_directory(extract_dir, 0777, 0) */
Rob Landleyd921b2e2006-08-03 15:41:12 +000089 xchdir(extract_dir);
Glenn L McGrath1f28b902004-01-07 09:24:06 +000090 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000091 unpack_ar_archive(ar_archive);
Glenn L McGrath9aff9032001-06-13 07:26:39 +000092
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000093 /* Cleanup */
Denis Vlasenko079f8af2006-11-27 16:49:31 +000094 close(ar_archive->src_fd);
Glenn L McGrath445fb952001-04-13 04:02:57 +000095
Denis Vlasenko079f8af2006-11-27 16:49:31 +000096 return EXIT_SUCCESS;
Glenn L McGrath58a40852001-01-02 23:49:26 +000097}