1 /* vi: set sw=4 ts=4: */
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.
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.
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.
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
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.
32 int seek_sub_file(FILE *in_file, file_headers_t *headers, const char *tar_gz_file)
34 /* find the headers for the specified .tar.gz file */
35 while (headers != NULL) {
36 if (strcmp(headers->name, tar_gz_file) == 0) {
37 fseek(in_file, headers->offset, SEEK_SET);
40 headers = headers->next;
47 * The contents of argument depend on the value of function.
48 * It is either a dir name or a control file or field name(see dpkg_deb.c)
50 char *deb_extract(const char *package_filename, FILE *out_stream, const int extract_function,
51 const char *prefix, const char *filename)
53 FILE *deb_stream, *uncompressed_stream;
54 file_headers_t *ar_headers = NULL;
55 file_headers_t *tar_headers = NULL;
56 file_headers_t *list_ptr;
57 file_headers_t *deb_extract_list = (file_headers_t *) calloc(1, sizeof(file_headers_t));
59 char *ared_file = NULL;
60 char *output_buffer = NULL;
63 if (extract_function & extract_control_tar_gz) {
64 ared_file = xstrdup("control.tar.gz");
66 else if (extract_function & extract_data_tar_gz) {
67 ared_file = xstrdup("data.tar.gz");
70 /* open the debian package to be worked on */
71 deb_stream = wfopen(package_filename, "r");
73 ar_headers = (file_headers_t *) xmalloc(sizeof(file_headers_t));
75 /* get a linked list of all ar entries */
76 ar_headers = get_ar_headers(deb_stream);
77 if (ar_headers == NULL) {
78 error_msg("Couldnt get ar headers\n");
82 /* seek to the start of the .tar.gz file within the ar file*/
83 fseek(deb_stream, 0, SEEK_SET);
84 if (seek_sub_file(deb_stream, ar_headers, ared_file) == EXIT_FAILURE) {
85 error_msg("Couldnt seek to file %s", ared_file);
88 /* get a linked list of all tar entries */
89 tar_headers = get_tar_gz_headers(deb_stream);
90 if (tar_headers == NULL) {
91 error_msg("Couldnt get tar headers\n");
95 if (extract_function & extract_one_to_buffer) {
96 list_ptr = tar_headers;
97 while (list_ptr != NULL) {
98 if (strcmp(filename, list_ptr->name) == 0) {
99 deb_extract_list = append_archive_list(deb_extract_list, list_ptr);
102 list_ptr = list_ptr->next;
105 deb_extract_list = tar_headers;
108 /* seek to the start of the .tar.gz file within the ar file*/
109 if (seek_sub_file(deb_stream, ar_headers, ared_file) == EXIT_FAILURE) {
110 error_msg("Couldnt seek to ar file");
113 /* open a stream of decompressed data */
114 uncompressed_stream = fdopen(gz_open(deb_stream, &gunzip_pid), "r");
116 output_buffer = extract_archive(uncompressed_stream, out_stream, deb_extract_list, extract_function, prefix);
118 gz_close(gunzip_pid);
120 fclose(uncompressed_stream);
123 return(output_buffer);