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> |
Glenn L McGrath | 3e2ab88 | 2001-04-12 13:49:09 +0000 | [diff] [blame] | 29 | #include <string.h> |
Glenn L McGrath | d22e560 | 2001-04-11 02:12:08 +0000 | [diff] [blame] | 30 | #include <signal.h> |
| 31 | #include "libbb.h" |
| 32 | |
Glenn L McGrath | 445fb95 | 2001-04-13 04:02:57 +0000 | [diff] [blame] | 33 | /* |
| 34 | * The contents of argument depend on the value of function. |
| 35 | * It is either a dir name or a control file or field name(see dpkg_deb.c) |
| 36 | */ |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 37 | extern char *deb_extract(const char *package_filename, const int function, const char *argument, const char *argument2) |
Glenn L McGrath | d22e560 | 2001-04-11 02:12:08 +0000 | [diff] [blame] | 38 | { |
Glenn L McGrath | d22e560 | 2001-04-11 02:12:08 +0000 | [diff] [blame] | 39 | |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 40 | FILE *deb_file, *uncompressed_file; |
| 41 | ar_headers_t *headers = NULL; |
Glenn L McGrath | 359c106 | 2001-04-12 10:19:08 +0000 | [diff] [blame] | 42 | char *ared_file = NULL; |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 43 | char *output_buffer = NULL; |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 44 | int gunzip_pid; |
Glenn L McGrath | d22e560 | 2001-04-11 02:12:08 +0000 | [diff] [blame] | 45 | |
Glenn L McGrath | 359c106 | 2001-04-12 10:19:08 +0000 | [diff] [blame] | 46 | switch (function) { |
| 47 | case (extract_info): |
| 48 | case (extract_control): |
Glenn L McGrath | 445fb95 | 2001-04-13 04:02:57 +0000 | [diff] [blame] | 49 | case (extract_field): |
Glenn L McGrath | 359c106 | 2001-04-12 10:19:08 +0000 | [diff] [blame] | 50 | ared_file = xstrdup("control.tar.gz"); |
| 51 | break; |
Glenn L McGrath | 3e2ab88 | 2001-04-12 13:49:09 +0000 | [diff] [blame] | 52 | default: |
Glenn L McGrath | 359c106 | 2001-04-12 10:19:08 +0000 | [diff] [blame] | 53 | ared_file = xstrdup("data.tar.gz"); |
| 54 | break; |
Glenn L McGrath | d22e560 | 2001-04-11 02:12:08 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 57 | /* open the debian package to be worked on */ |
| 58 | deb_file = wfopen(package_filename, "r"); |
| 59 | |
| 60 | headers = (ar_headers_t *) xmalloc(sizeof(ar_headers_t)); |
Glenn L McGrath | d22e560 | 2001-04-11 02:12:08 +0000 | [diff] [blame] | 61 | |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 62 | /* get a linked list of all ar entries */ |
| 63 | if ((headers = get_ar_headers(deb_file)) == NULL) { |
| 64 | error_msg("Couldnt get ar headers\n"); |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 65 | return(NULL); |
Glenn L McGrath | d22e560 | 2001-04-11 02:12:08 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 68 | /* seek to the start of the .tar.gz file within the ar file*/ |
| 69 | if (seek_ared_file(deb_file, headers, ared_file) == EXIT_FAILURE) { |
| 70 | error_msg("Couldnt seek to ar file"); |
Glenn L McGrath | d22e560 | 2001-04-11 02:12:08 +0000 | [diff] [blame] | 71 | } |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 72 | |
| 73 | /* open a stream of decompressed data */ |
| 74 | uncompressed_file = fdopen(gz_open(deb_file, &gunzip_pid), "r"); |
| 75 | |
Glenn L McGrath | 3e2ab88 | 2001-04-12 13:49:09 +0000 | [diff] [blame] | 76 | if (function & extract_fsys_tarfile) { |
| 77 | copy_file_chunk(uncompressed_file, stdout, -1); |
| 78 | } else { |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 79 | FILE *output; |
| 80 | |
| 81 | if (function & extract_contents_to_file) { |
| 82 | output = wfopen(argument, "w"); |
| 83 | } else { |
| 84 | output = stdout; |
| 85 | } |
| 86 | |
| 87 | output_buffer = untar(uncompressed_file, output, function, argument, argument2); |
| 88 | if (output != stdout) { |
| 89 | fclose(output); |
Glenn L McGrath | 685f5fd | 2001-04-15 12:51:59 +0000 | [diff] [blame] | 90 | } |
Glenn L McGrath | 3e2ab88 | 2001-04-12 13:49:09 +0000 | [diff] [blame] | 91 | } |
Glenn L McGrath | 685f5fd | 2001-04-15 12:51:59 +0000 | [diff] [blame] | 92 | gz_close(gunzip_pid); |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 93 | fclose(deb_file); |
| 94 | fclose(uncompressed_file); |
Glenn L McGrath | 359c106 | 2001-04-12 10:19:08 +0000 | [diff] [blame] | 95 | free(ared_file); |
| 96 | |
Glenn L McGrath | 33431eb | 2001-04-16 04:52:19 +0000 | [diff] [blame] | 97 | return(output_buffer); |
Glenn L McGrath | d22e560 | 2001-04-11 02:12:08 +0000 | [diff] [blame] | 98 | } |