blob: 80a5f3250c84293dd58f9635bf4808077f0b0b2e [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 */
Glenn L McGrath58a40852001-01-02 23:49:26 +00007#include "busybox.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
Rob Landleydfba7412006-03-06 20:47:33 +000016int dpkg_deb_main(int argc, char **argv)
Glenn L McGrathbc9afad2001-02-11 03:32:41 +000017{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000018 archive_handle_t *ar_archive;
Glenn L McGrath18bbca12002-11-05 01:52:23 +000019 archive_handle_t *tar_archive;
Glenn L McGrath66125c82002-12-08 00:54:33 +000020 llist_t *control_tar_llist = NULL;
Denis Vlasenko67b23e62006-10-03 21:00:06 +000021 unsigned opt;
Glenn L McGrath1f28b902004-01-07 09:24:06 +000022 char *extract_dir = NULL;
23 short argcount = 1;
24
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000025 /* Setup the tar archive handle */
Glenn L McGrath18bbca12002-11-05 01:52:23 +000026 tar_archive = init_handle();
Glenn L McGrath9aff9032001-06-13 07:26:39 +000027
Eric Andersenc7bda1c2004-03-15 08:29:22 +000028 /* Setup an ar archive handle that refers to the gzip sub archive */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000029 ar_archive = init_handle();
Glenn L McGrath18bbca12002-11-05 01:52:23 +000030 ar_archive->sub_archive = tar_archive;
31 ar_archive->filter = filter_accept_list_reassign;
32
33#ifdef CONFIG_FEATURE_DEB_TAR_GZ
Rob Landley8bb50782006-05-26 23:44:51 +000034 llist_add_to(&(ar_archive->accept), "data.tar.gz");
35 llist_add_to(&control_tar_llist, "control.tar.gz");
Glenn L McGrath18bbca12002-11-05 01:52:23 +000036#endif
37
38#ifdef CONFIG_FEATURE_DEB_TAR_BZ2
Rob Landley8bb50782006-05-26 23:44:51 +000039 llist_add_to(&(ar_archive->accept), "data.tar.bz2");
40 llist_add_to(&control_tar_llist, "control.tar.bz2");
Glenn L McGrath18bbca12002-11-05 01:52:23 +000041#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000042
Denis Vlasenko67b23e62006-10-03 21:00:06 +000043 opt_complementary = "?c--efXx:e--cfXx:f--ceXx:X--cefx:x--cefX";
44 opt = getopt32(argc, argv, "cefXx");
Glenn L McGrath1f28b902004-01-07 09:24:06 +000045
46 if (opt & DPKG_DEB_OPT_CONTENTS) {
47 tar_archive->action_header = header_verbose_list;
48 }
49 if (opt & DPKG_DEB_OPT_CONTROL) {
50 ar_archive->accept = control_tar_llist;
51 tar_archive->action_data = data_extract_all;
52 if (optind + 1 == argc) {
53 extract_dir = "./DEBIAN";
54 } else {
55 argcount++;
Glenn L McGrath58a40852001-01-02 23:49:26 +000056 }
57 }
Glenn L McGrath1f28b902004-01-07 09:24:06 +000058 if (opt & DPKG_DEB_OPT_FIELD) {
59 /* Print the entire control file
Eric Andersenc7bda1c2004-03-15 08:29:22 +000060 * it should accept a second argument which specifies a
Glenn L McGrath1f28b902004-01-07 09:24:06 +000061 * specific field to print */
62 ar_archive->accept = control_tar_llist;
Rob Landley8bb50782006-05-26 23:44:51 +000063 llist_add_to(&(tar_archive->accept), "./control");
Glenn L McGrath1f28b902004-01-07 09:24:06 +000064 tar_archive->filter = filter_accept_list;
65 tar_archive->action_data = data_extract_to_stdout;
66 }
67 if (opt & DPKG_DEB_OPT_EXTRACT) {
68 tar_archive->action_header = header_list;
69 }
70 if (opt & (DPKG_DEB_OPT_EXTRACT_VERBOSE | DPKG_DEB_OPT_EXTRACT)) {
71 tar_archive->action_data = data_extract_all;
72 argcount = 2;
73 }
Glenn L McGrath58a40852001-01-02 23:49:26 +000074
"Vladimir N. Oleynik"27421a12005-09-05 14:46:07 +000075 if ((optind + argcount) != argc) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000076 bb_show_usage();
Glenn L McGrath58a40852001-01-02 23:49:26 +000077 }
Glenn L McGrath6785b512001-04-12 11:48:02 +000078
Rob Landleyd921b2e2006-08-03 15:41:12 +000079 tar_archive->src_fd = ar_archive->src_fd = xopen(argv[optind++], O_RDONLY);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000080
Glenn L McGrath9aff9032001-06-13 07:26:39 +000081 /* Workout where to extract the files */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000082 /* 2nd argument is a dir name */
Glenn L McGrath1f28b902004-01-07 09:24:06 +000083 if (argv[optind]) {
84 extract_dir = argv[optind];
85 }
86 if (extract_dir) {
Bernhard Reutner-Fischer44e216f2006-06-10 11:29:44 +000087 mkdir(extract_dir, 0777); /* bb_make_directory(extract_dir, 0777, 0) */
Rob Landleyd921b2e2006-08-03 15:41:12 +000088 xchdir(extract_dir);
Glenn L McGrath1f28b902004-01-07 09:24:06 +000089 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000090 unpack_ar_archive(ar_archive);
Glenn L McGrath9aff9032001-06-13 07:26:39 +000091
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000092 /* Cleanup */
93 close (ar_archive->src_fd);
Glenn L McGrath445fb952001-04-13 04:02:57 +000094
Glenn L McGrath58a40852001-01-02 23:49:26 +000095 return(EXIT_SUCCESS);
96}