summaryrefslogtreecommitdiff
path: root/archival/libunarchive/deb_extract.c
blob: b95ac4d1feec474c5b7d950d137fd0f7932a121d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "unarchive.h"
#include "libbb.h"

char *deb_extract(const char *package_filename, FILE *out_stream, 
	const int extract_function, const char *prefix, const char *filename)
{
	FILE *deb_stream;
	FILE *uncompressed_stream = NULL;
	file_header_t *ar_header = NULL;
	char **file_list = NULL;
	char *output_buffer = NULL;
	char *ared_file = NULL;
	char ar_magic[8];
	int gunzip_pid;

	if (filename != NULL) {
		file_list = xmalloc(sizeof(char *) * 2);
		file_list[0] = xstrdup(filename);
		file_list[1] = NULL;
	}
	
	if (extract_function & extract_control_tar_gz) {
		ared_file = xstrdup("control.tar.gz");
	}
	else if (extract_function & extract_data_tar_gz) {		
		ared_file = xstrdup("data.tar.gz");
	}

	/* open the debian package to be worked on */
	deb_stream = wfopen(package_filename, "r");
	if (deb_stream == NULL) {
		return(NULL);
	}
	/* set the buffer size */
	setvbuf(deb_stream, NULL, _IOFBF, 0x8000);

	/* check ar magic */
	fread(ar_magic, 1, 8, deb_stream);
	if (strncmp(ar_magic,"!<arch>",7) != 0) {
		error_msg_and_die("invalid magic");
	}
	archive_offset = 8;

	while ((ar_header = get_header_ar(deb_stream)) != NULL) {
		if (strcmp(ared_file, ar_header->name) == 0) {
			/* open a stream of decompressed data */
			uncompressed_stream = gz_open(deb_stream, &gunzip_pid);
			archive_offset = 0;
			output_buffer = unarchive(uncompressed_stream, out_stream, get_header_tar, extract_function, prefix, file_list, NULL);
		}
		seek_sub_file(deb_stream, ar_header->size);
		free(ar_header->name);
		free(ar_header);
	}
	gz_close(gunzip_pid);
	fclose(deb_stream);
	fclose(uncompressed_stream);
	free(ared_file);
	if (filename != NULL) {
		free(file_list[0]);
		free(file_list);
	}
	return(output_buffer);
}