blob: e982c14bb1248788950f85c4a96658d3cbd1c2d9 [file] [log] [blame]
Glenn L McGrathd22e5602001-04-11 02:12:08 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Copyright (C) tons of folks. Tracking down who wrote what
4 * isn't something I'm going to worry about... If you wrote something
5 * here, please feel free to acknowledge your work.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
22 * Permission has been granted to redistribute this code under the GPL.
23 *
24 */
25
26#include <fcntl.h>
27#include <unistd.h>
28#include <stdlib.h>
29#include <signal.h>
30#include "libbb.h"
31
Glenn L McGrath359c1062001-04-12 10:19:08 +000032extern int deb_extract(const char *package_filename, int function, char *target_dir)
Glenn L McGrathd22e5602001-04-11 02:12:08 +000033{
Glenn L McGrathd22e5602001-04-11 02:12:08 +000034
Glenn L McGrath4949faf2001-04-11 16:23:35 +000035 FILE *deb_file, *uncompressed_file;
36 ar_headers_t *headers = NULL;
Glenn L McGrath359c1062001-04-12 10:19:08 +000037 char *ared_file = NULL;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000038 int gunzip_pid;
Glenn L McGrathd22e5602001-04-11 02:12:08 +000039
Glenn L McGrath359c1062001-04-12 10:19:08 +000040 switch (function) {
41 case (extract_info):
42 case (extract_control):
43 ared_file = xstrdup("control.tar.gz");
44 break;
45 case (extract_contents):
46 case (extract_extract):
47 case (extract_verbose_extract):
48 case (extract_list):
49 ared_file = xstrdup("data.tar.gz");
50 break;
51 default:
52 error_msg("Unknown extraction function");
Glenn L McGrathd22e5602001-04-11 02:12:08 +000053 }
54
Glenn L McGrath4949faf2001-04-11 16:23:35 +000055 /* open the debian package to be worked on */
56 deb_file = wfopen(package_filename, "r");
57
58 headers = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
Glenn L McGrathd22e5602001-04-11 02:12:08 +000059
Glenn L McGrath4949faf2001-04-11 16:23:35 +000060 /* get a linked list of all ar entries */
61 if ((headers = get_ar_headers(deb_file)) == NULL) {
62 error_msg("Couldnt get ar headers\n");
63 return(EXIT_FAILURE);
Glenn L McGrathd22e5602001-04-11 02:12:08 +000064 }
65
Glenn L McGrath4949faf2001-04-11 16:23:35 +000066 /* seek to the start of the .tar.gz file within the ar file*/
67 if (seek_ared_file(deb_file, headers, ared_file) == EXIT_FAILURE) {
68 error_msg("Couldnt seek to ar file");
Glenn L McGrathd22e5602001-04-11 02:12:08 +000069 }
Glenn L McGrath4949faf2001-04-11 16:23:35 +000070
71 /* open a stream of decompressed data */
72 uncompressed_file = fdopen(gz_open(deb_file, &gunzip_pid), "r");
73
74 /* get a list of all tar headers inside the .gz file */
Glenn L McGrath359c1062001-04-12 10:19:08 +000075 untar(uncompressed_file, function, target_dir);
Glenn L McGrathd22e5602001-04-11 02:12:08 +000076
77 /* we are deliberately terminating the child so we can safely ignore this */
Glenn L McGrath4949faf2001-04-11 16:23:35 +000078 gz_close(gunzip_pid);
Glenn L McGrath359c1062001-04-12 10:19:08 +000079
Glenn L McGrath4949faf2001-04-11 16:23:35 +000080 fclose(deb_file);
81 fclose(uncompressed_file);
Glenn L McGrath359c1062001-04-12 10:19:08 +000082 free(ared_file);
83
Glenn L McGrath4949faf2001-04-11 16:23:35 +000084 return(EXIT_SUCCESS);
Glenn L McGrathd22e5602001-04-11 02:12:08 +000085}