blob: fc8b9221d139539f42b97d20e43f9add2e706d56 [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 McGrath7ca04f32002-09-25 02:47:48 +00007#include <fcntl.h>
Glenn L McGrath58a40852001-01-02 23:49:26 +00008#include <stdlib.h>
Glenn L McGrath33431eb2001-04-16 04:52:19 +00009#include <string.h>
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000010#include <unistd.h>
Glenn L McGrath1f28b902004-01-07 09:24:06 +000011
Glenn L McGrathef0eab52001-10-25 14:49:48 +000012#include "unarchive.h"
Glenn L McGrath58a40852001-01-02 23:49:26 +000013#include "busybox.h"
14
Glenn L McGrath1f28b902004-01-07 09:24:06 +000015#define DPKG_DEB_OPT_CONTENTS 1
16#define DPKG_DEB_OPT_CONTROL 2
17#define DPKG_DEB_OPT_FIELD 4
18#define DPKG_DEB_OPT_EXTRACT 8
19#define DPKG_DEB_OPT_EXTRACT_VERBOSE 16
20
Rob Landleydfba7412006-03-06 20:47:33 +000021int dpkg_deb_main(int argc, char **argv)
Glenn L McGrathbc9afad2001-02-11 03:32:41 +000022{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000023 archive_handle_t *ar_archive;
Glenn L McGrath18bbca12002-11-05 01:52:23 +000024 archive_handle_t *tar_archive;
Glenn L McGrath66125c82002-12-08 00:54:33 +000025 llist_t *control_tar_llist = NULL;
Glenn L McGrath1f28b902004-01-07 09:24:06 +000026 unsigned long opt;
27 char *extract_dir = NULL;
28 short argcount = 1;
29
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000030 /* Setup the tar archive handle */
Glenn L McGrath18bbca12002-11-05 01:52:23 +000031 tar_archive = init_handle();
Glenn L McGrath9aff9032001-06-13 07:26:39 +000032
Eric Andersenc7bda1c2004-03-15 08:29:22 +000033 /* Setup an ar archive handle that refers to the gzip sub archive */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000034 ar_archive = init_handle();
Glenn L McGrath18bbca12002-11-05 01:52:23 +000035 ar_archive->sub_archive = tar_archive;
36 ar_archive->filter = filter_accept_list_reassign;
37
38#ifdef CONFIG_FEATURE_DEB_TAR_GZ
Glenn L McGrath66125c82002-12-08 00:54:33 +000039 ar_archive->accept = llist_add_to(NULL, "data.tar.gz");
Glenn L McGrath66125c82002-12-08 00:54:33 +000040 control_tar_llist = llist_add_to(NULL, "control.tar.gz");
Glenn L McGrath18bbca12002-11-05 01:52:23 +000041#endif
42
43#ifdef CONFIG_FEATURE_DEB_TAR_BZ2
Glenn L McGrath66125c82002-12-08 00:54:33 +000044 ar_archive->accept = llist_add_to(ar_archive->accept, "data.tar.bz2");
Glenn L McGrath66125c82002-12-08 00:54:33 +000045 control_tar_llist = llist_add_to(control_tar_llist, "control.tar.bz2");
Glenn L McGrath18bbca12002-11-05 01:52:23 +000046#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000047
"Vladimir N. Oleynik"f704b272005-10-14 09:56:52 +000048 bb_opt_complementally = "?c--efXx:e--cfXx:f--ceXx:X--cefx:x--cefX";
Glenn L McGrath1f28b902004-01-07 09:24:06 +000049 opt = bb_getopt_ulflags(argc, argv, "cefXx");
50
51 if (opt & DPKG_DEB_OPT_CONTENTS) {
52 tar_archive->action_header = header_verbose_list;
53 }
54 if (opt & DPKG_DEB_OPT_CONTROL) {
55 ar_archive->accept = control_tar_llist;
56 tar_archive->action_data = data_extract_all;
57 if (optind + 1 == argc) {
58 extract_dir = "./DEBIAN";
59 } else {
60 argcount++;
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;
Bernhard Reutner-Fischer0b42a6a2005-10-07 11:34:50 +000068 tar_archive->accept = llist_add_to(NULL, "./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;
77 argcount = 2;
78 }
Glenn L McGrath58a40852001-01-02 23:49:26 +000079
"Vladimir N. Oleynik"27421a12005-09-05 14:46:07 +000080 if ((optind + argcount) != 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
Manuel Novoa III cad53642003-03-19 09:13:01 +000084 tar_archive->src_fd = ar_archive->src_fd = bb_xopen(argv[optind++], O_RDONLY);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000085
Glenn L McGrath9aff9032001-06-13 07:26:39 +000086 /* Workout where to extract the files */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000087 /* 2nd argument is a dir name */
Glenn L McGrath1f28b902004-01-07 09:24:06 +000088 if (argv[optind]) {
89 extract_dir = argv[optind];
90 }
91 if (extract_dir) {
92 mkdir(extract_dir, 0777);
Bernhard Reutner-Fischerd9cf7ac2006-04-12 18:39:58 +000093 chdir(extract_dir); /* error check? */
Glenn L McGrath1f28b902004-01-07 09:24:06 +000094 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000095 unpack_ar_archive(ar_archive);
Glenn L McGrath9aff9032001-06-13 07:26:39 +000096
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000097 /* Cleanup */
98 close (ar_archive->src_fd);
Glenn L McGrath445fb952001-04-13 04:02:57 +000099
Glenn L McGrath58a40852001-01-02 23:49:26 +0000100 return(EXIT_SUCCESS);
101}