blob: 29e9719cb8350678328481413a4576dffd7020c1 [file] [log] [blame]
Glenn L McGrath58a40852001-01-02 23:49:26 +00001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Glenn L McGrath1f28b902004-01-07 09:24:06 +000015 *
Glenn L McGrath58a40852001-01-02 23:49:26 +000016 */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000017#include <fcntl.h>
Glenn L McGrath58a40852001-01-02 23:49:26 +000018#include <stdlib.h>
Glenn L McGrath33431eb2001-04-16 04:52:19 +000019#include <string.h>
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000020#include <unistd.h>
Glenn L McGrath1f28b902004-01-07 09:24:06 +000021
Glenn L McGrathef0eab52001-10-25 14:49:48 +000022#include "unarchive.h"
Glenn L McGrath58a40852001-01-02 23:49:26 +000023#include "busybox.h"
24
Glenn L McGrath1f28b902004-01-07 09:24:06 +000025#define DPKG_DEB_OPT_CONTENTS 1
26#define DPKG_DEB_OPT_CONTROL 2
27#define DPKG_DEB_OPT_FIELD 4
28#define DPKG_DEB_OPT_EXTRACT 8
29#define DPKG_DEB_OPT_EXTRACT_VERBOSE 16
30
Glenn L McGrathbc9afad2001-02-11 03:32:41 +000031extern int dpkg_deb_main(int argc, char **argv)
32{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000033 archive_handle_t *ar_archive;
Glenn L McGrath18bbca12002-11-05 01:52:23 +000034 archive_handle_t *tar_archive;
Glenn L McGrath66125c82002-12-08 00:54:33 +000035 llist_t *control_tar_llist = NULL;
Glenn L McGrath1f28b902004-01-07 09:24:06 +000036 unsigned long opt;
37 char *extract_dir = NULL;
38 short argcount = 1;
39
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000040 /* Setup the tar archive handle */
Glenn L McGrath18bbca12002-11-05 01:52:23 +000041 tar_archive = init_handle();
Glenn L McGrath9aff9032001-06-13 07:26:39 +000042
Eric Andersenc7bda1c2004-03-15 08:29:22 +000043 /* Setup an ar archive handle that refers to the gzip sub archive */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000044 ar_archive = init_handle();
Glenn L McGrath18bbca12002-11-05 01:52:23 +000045 ar_archive->sub_archive = tar_archive;
46 ar_archive->filter = filter_accept_list_reassign;
47
48#ifdef CONFIG_FEATURE_DEB_TAR_GZ
Glenn L McGrath66125c82002-12-08 00:54:33 +000049 ar_archive->accept = llist_add_to(NULL, "data.tar.gz");
Glenn L McGrath66125c82002-12-08 00:54:33 +000050 control_tar_llist = llist_add_to(NULL, "control.tar.gz");
Glenn L McGrath18bbca12002-11-05 01:52:23 +000051#endif
52
53#ifdef CONFIG_FEATURE_DEB_TAR_BZ2
Glenn L McGrath66125c82002-12-08 00:54:33 +000054 ar_archive->accept = llist_add_to(ar_archive->accept, "data.tar.bz2");
Glenn L McGrath66125c82002-12-08 00:54:33 +000055 control_tar_llist = llist_add_to(control_tar_llist, "control.tar.bz2");
Glenn L McGrath18bbca12002-11-05 01:52:23 +000056#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000057
"Vladimir N. Oleynik"f704b272005-10-14 09:56:52 +000058 bb_opt_complementally = "?c--efXx:e--cfXx:f--ceXx:X--cefx:x--cefX";
Glenn L McGrath1f28b902004-01-07 09:24:06 +000059 opt = bb_getopt_ulflags(argc, argv, "cefXx");
60
61 if (opt & DPKG_DEB_OPT_CONTENTS) {
62 tar_archive->action_header = header_verbose_list;
63 }
64 if (opt & DPKG_DEB_OPT_CONTROL) {
65 ar_archive->accept = control_tar_llist;
66 tar_archive->action_data = data_extract_all;
67 if (optind + 1 == argc) {
68 extract_dir = "./DEBIAN";
69 } else {
70 argcount++;
Glenn L McGrath58a40852001-01-02 23:49:26 +000071 }
72 }
Glenn L McGrath1f28b902004-01-07 09:24:06 +000073 if (opt & DPKG_DEB_OPT_FIELD) {
74 /* Print the entire control file
Eric Andersenc7bda1c2004-03-15 08:29:22 +000075 * it should accept a second argument which specifies a
Glenn L McGrath1f28b902004-01-07 09:24:06 +000076 * specific field to print */
77 ar_archive->accept = control_tar_llist;
Bernhard Reutner-Fischer0b42a6a2005-10-07 11:34:50 +000078 tar_archive->accept = llist_add_to(NULL, "./control");
Glenn L McGrath1f28b902004-01-07 09:24:06 +000079 tar_archive->filter = filter_accept_list;
80 tar_archive->action_data = data_extract_to_stdout;
81 }
82 if (opt & DPKG_DEB_OPT_EXTRACT) {
83 tar_archive->action_header = header_list;
84 }
85 if (opt & (DPKG_DEB_OPT_EXTRACT_VERBOSE | DPKG_DEB_OPT_EXTRACT)) {
86 tar_archive->action_data = data_extract_all;
87 argcount = 2;
88 }
Glenn L McGrath58a40852001-01-02 23:49:26 +000089
"Vladimir N. Oleynik"27421a12005-09-05 14:46:07 +000090 if ((optind + argcount) != argc) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000091 bb_show_usage();
Glenn L McGrath58a40852001-01-02 23:49:26 +000092 }
Glenn L McGrath6785b512001-04-12 11:48:02 +000093
Manuel Novoa III cad53642003-03-19 09:13:01 +000094 tar_archive->src_fd = ar_archive->src_fd = bb_xopen(argv[optind++], O_RDONLY);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000095
Glenn L McGrath9aff9032001-06-13 07:26:39 +000096 /* Workout where to extract the files */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000097 /* 2nd argument is a dir name */
Glenn L McGrath1f28b902004-01-07 09:24:06 +000098 if (argv[optind]) {
99 extract_dir = argv[optind];
100 }
101 if (extract_dir) {
102 mkdir(extract_dir, 0777);
103 chdir(extract_dir);
104 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000105 unpack_ar_archive(ar_archive);
Glenn L McGrath9aff9032001-06-13 07:26:39 +0000106
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000107 /* Cleanup */
108 close (ar_archive->src_fd);
Glenn L McGrath445fb952001-04-13 04:02:57 +0000109
Glenn L McGrath58a40852001-01-02 23:49:26 +0000110 return(EXIT_SUCCESS);
111}