blob: e5b5c968531617e2578bf4150cb5375de732413e [file] [log] [blame]
Glenn L McGrath95ebf612001-10-25 14:18:08 +00001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 */
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include "unarchive.h"
21#include "libbb.h"
22
23char *deb_extract(const char *package_filename, FILE *out_stream,
24 const int extract_function, const char *prefix, const char *filename)
25{
26 FILE *deb_stream;
27 FILE *uncompressed_stream = NULL;
28 file_header_t *ar_header = NULL;
29 char **file_list = NULL;
30 char *output_buffer = NULL;
31 char *ared_file = NULL;
32 char ar_magic[8];
33 int gunzip_pid;
34
35 if (filename != NULL) {
36 file_list = xmalloc(sizeof(char *) * 2);
37 file_list[0] = xstrdup(filename);
38 file_list[1] = NULL;
39 }
40
41 if (extract_function & extract_control_tar_gz) {
42 ared_file = xstrdup("control.tar.gz");
43 }
44 else if (extract_function & extract_data_tar_gz) {
45 ared_file = xstrdup("data.tar.gz");
46 }
47
48 /* open the debian package to be worked on */
49 deb_stream = wfopen(package_filename, "r");
50 if (deb_stream == NULL) {
51 return(NULL);
52 }
53 /* set the buffer size */
54 setvbuf(deb_stream, NULL, _IOFBF, 0x8000);
55
56 /* check ar magic */
57 fread(ar_magic, 1, 8, deb_stream);
58 if (strncmp(ar_magic,"!<arch>",7) != 0) {
59 error_msg_and_die("invalid magic");
60 }
61 archive_offset = 8;
62
63 while ((ar_header = get_header_ar(deb_stream)) != NULL) {
64 if (strcmp(ared_file, ar_header->name) == 0) {
65 /* open a stream of decompressed data */
66 uncompressed_stream = gz_open(deb_stream, &gunzip_pid);
67 archive_offset = 0;
68 output_buffer = unarchive(uncompressed_stream, out_stream, get_header_tar, extract_function, prefix, file_list, NULL);
69 }
70 seek_sub_file(deb_stream, ar_header->size);
71 free(ar_header->name);
72 free(ar_header);
73 }
74 gz_close(gunzip_pid);
75 fclose(deb_stream);
76 fclose(uncompressed_stream);
77 free(ared_file);
78 if (filename != NULL) {
79 free(file_list[0]);
80 free(file_list);
81 }
82 return(output_buffer);
83}