With a bit of care I was able to save about 100 bytes.
[oweals/busybox.git] / libbb / deb_extract.c
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 <string.h>
30 #include "libbb.h"
31
32 int seek_sub_file(FILE *in_file, file_headers_t *headers, const char *tar_gz_file)
33 {
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);
38                         return(EXIT_SUCCESS);
39                 }
40                 headers = headers->next;
41         }
42
43         return(EXIT_FAILURE);
44 }
45
46 /*
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)
49  */
50 char *deb_extract(const char *package_filename, FILE *out_stream, const int extract_function,
51         const char *prefix, const char *filename)
52 {
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));
58
59         char *ared_file = NULL;
60         char *output_buffer = NULL;
61         int gunzip_pid;
62
63         if (extract_function & extract_control_tar_gz) {
64                 ared_file = xstrdup("control.tar.gz");
65         }
66         else if (extract_function & extract_data_tar_gz) {              
67                 ared_file = xstrdup("data.tar.gz");
68         }
69
70         /* open the debian package to be worked on */
71         deb_stream = wfopen(package_filename, "r");
72
73         ar_headers = (file_headers_t *) xmalloc(sizeof(file_headers_t));        
74         
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");
79                 return(NULL);
80         }
81
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);
86         }
87
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");
92                 return(NULL);
93         }
94
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);
100                                 break;
101                         }
102                         list_ptr = list_ptr->next;
103                 }
104         } else {
105                 deb_extract_list = tar_headers;
106         }
107
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");
111         }
112
113         /* open a stream of decompressed data */
114         uncompressed_stream = fdopen(gz_open(deb_stream, &gunzip_pid), "r");
115
116         output_buffer = extract_archive(uncompressed_stream, out_stream, deb_extract_list, extract_function, prefix);
117
118         gz_close(gunzip_pid);
119         fclose(deb_stream);
120         fclose(uncompressed_stream);
121         free(ared_file);
122
123         return(output_buffer);
124 }