Glenn L McGrath | d22e560 | 2001-04-11 02:12:08 +0000 | [diff] [blame] | 1 | /* 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 McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame^] | 32 | const int dpkg_deb_contents = 1; |
| 33 | const int dpkg_deb_control = 2; |
| 34 | const int dpkg_deb_info = 4; |
| 35 | const int dpkg_deb_extract = 8; |
| 36 | const int dpkg_deb_verbose_extract = 16; |
| 37 | const int dpkg_deb_list = 32; |
Glenn L McGrath | d22e560 | 2001-04-11 02:12:08 +0000 | [diff] [blame] | 38 | |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame^] | 39 | extern int deb_extract(const char *package_filename, const int function, char *target_dir) |
Glenn L McGrath | d22e560 | 2001-04-11 02:12:08 +0000 | [diff] [blame] | 40 | { |
Glenn L McGrath | d22e560 | 2001-04-11 02:12:08 +0000 | [diff] [blame] | 41 | |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame^] | 42 | FILE *deb_file, *uncompressed_file; |
| 43 | ar_headers_t *headers = NULL; |
| 44 | char *ared_file; |
| 45 | int gunzip_pid; |
Glenn L McGrath | d22e560 | 2001-04-11 02:12:08 +0000 | [diff] [blame] | 46 | |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame^] | 47 | if ((function == dpkg_deb_info) || (function == dpkg_deb_control)) { |
| 48 | ared_file = xstrdup("control.tar.gz"); |
| 49 | } else { |
| 50 | ared_file = xstrdup("data.tar.gz"); |
Glenn L McGrath | d22e560 | 2001-04-11 02:12:08 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame^] | 53 | /* open the debian package to be worked on */ |
| 54 | deb_file = wfopen(package_filename, "r"); |
| 55 | |
| 56 | headers = (ar_headers_t *) xmalloc(sizeof(ar_headers_t)); |
Glenn L McGrath | d22e560 | 2001-04-11 02:12:08 +0000 | [diff] [blame] | 57 | |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame^] | 58 | /* get a linked list of all ar entries */ |
| 59 | if ((headers = get_ar_headers(deb_file)) == NULL) { |
| 60 | error_msg("Couldnt get ar headers\n"); |
| 61 | return(EXIT_FAILURE); |
Glenn L McGrath | d22e560 | 2001-04-11 02:12:08 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame^] | 64 | /* seek to the start of the .tar.gz file within the ar file*/ |
| 65 | if (seek_ared_file(deb_file, headers, ared_file) == EXIT_FAILURE) { |
| 66 | error_msg("Couldnt seek to ar file"); |
Glenn L McGrath | d22e560 | 2001-04-11 02:12:08 +0000 | [diff] [blame] | 67 | } |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame^] | 68 | |
| 69 | /* open a stream of decompressed data */ |
| 70 | uncompressed_file = fdopen(gz_open(deb_file, &gunzip_pid), "r"); |
| 71 | |
| 72 | /* get a list of all tar headers inside the .gz file */ |
| 73 | untar(uncompressed_file, dpkg_deb_extract, target_dir); |
Glenn L McGrath | d22e560 | 2001-04-11 02:12:08 +0000 | [diff] [blame] | 74 | |
| 75 | /* we are deliberately terminating the child so we can safely ignore this */ |
| 76 | signal(SIGTERM, SIG_IGN); |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame^] | 77 | gz_close(gunzip_pid); |
| 78 | fclose(deb_file); |
| 79 | fclose(uncompressed_file); |
| 80 | return(EXIT_SUCCESS); |
Glenn L McGrath | d22e560 | 2001-04-11 02:12:08 +0000 | [diff] [blame] | 81 | } |