diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2001-06-20 07:48:00 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2001-06-20 07:48:00 +0000 |
commit | eb1c94078f35d1b6e48741122c8bb9426befc5eb (patch) | |
tree | 48535c1f250f98707e595cfb83f47c83332c4fc9 | |
parent | b4a26e6fc09e9dd14ca206f8d9ed2c6e8976df1b (diff) | |
download | busybox-w32-eb1c94078f35d1b6e48741122c8bb9426befc5eb.tar.gz busybox-w32-eb1c94078f35d1b6e48741122c8bb9426befc5eb.tar.bz2 busybox-w32-eb1c94078f35d1b6e48741122c8bb9426befc5eb.zip |
Reorganise unarchiving functions, more code re-use, only does single pass(no more linked lists), basis for supporting a cpio (and cheaper untar) applet, but cpio applet isnt included in this.
It effects ar, dpkg-deb applets only
-rw-r--r-- | Makefile | 24 | ||||
-rw-r--r-- | ar.c | 42 | ||||
-rw-r--r-- | archival/ar.c | 42 | ||||
-rw-r--r-- | archival/dpkg_deb.c | 2 | ||||
-rw-r--r-- | dpkg_deb.c | 2 | ||||
-rw-r--r-- | include/libbb.h | 32 | ||||
-rw-r--r-- | libbb/add_from_archive_list.c | 31 | ||||
-rw-r--r-- | libbb/append_archive_list.c | 39 | ||||
-rw-r--r-- | libbb/deb_extract.c | 124 | ||||
-rw-r--r-- | libbb/extract_archive.c | 132 | ||||
-rw-r--r-- | libbb/get_ar_headers.c | 119 | ||||
-rw-r--r-- | libbb/get_tar_gz_headers.c | 39 | ||||
-rw-r--r-- | libbb/get_tar_headers.c | 111 | ||||
-rw-r--r-- | libbb/gz_open.c | 14 | ||||
-rw-r--r-- | libbb/libbb.h | 32 | ||||
-rw-r--r-- | libbb/unarchive.c | 500 |
16 files changed, 583 insertions, 702 deletions
@@ -236,19 +236,17 @@ endif | |||
236 | 236 | ||
237 | LIBBB = libbb | 237 | LIBBB = libbb |
238 | LIBBB_LIB = libbb.a | 238 | LIBBB_LIB = libbb.a |
239 | LIBBB_CSRC= append_archive_list.c add_from_archive_list.c ask_confirmation.c \ | 239 | LIBBB_CSRC= ask_confirmation.c chomp.c concat_path_file.c copy_file.c \ |
240 | chomp.c concat_path_file.c copy_file.c copy_file_chunk.c create_path.c \ | 240 | copy_file_chunk.c create_path.c daemon.c device_open.c error_msg.c \ |
241 | daemon.c deb_extract.c device_open.c error_msg.c error_msg_and_die.c \ | 241 | error_msg_and_die.c fgets_str.c find_mount_point.c find_pid_by_name.c \ |
242 | extract_archive.c fgets_str.c find_mount_point.c find_pid_by_name.c \ | 242 | find_root_device.c full_read.c full_write.c get_console.c \ |
243 | find_root_device.c full_read.c full_write.c get_ar_headers.c get_console.c \ | 243 | get_last_path_component.c get_line_from_file.c gz_open.c human_readable.c \ |
244 | get_last_path_component.c get_line_from_file.c get_tar_headers.c \ | 244 | isdirectory.c kernel_version.c loop.c mode_string.c module_syscalls.c mtab.c \ |
245 | get_tar_gz_headers.c gz_open.c human_readable.c isdirectory.c kernel_version.c \ | 245 | mtab_file.c my_getgrnam.c my_getgrgid.c my_getpwnam.c my_getpwnamegid.c \ |
246 | loop.c mode_string.c module_syscalls.c mtab.c mtab_file.c my_getgrnam.c \ | 246 | my_getpwuid.c parse_mode.c parse_number.c perror_msg.c perror_msg_and_die.c \ |
247 | my_getgrgid.c my_getpwnam.c my_getpwnamegid.c my_getpwuid.c parse_mode.c \ | 247 | print_file.c process_escape_sequence.c read_package_field.c recursive_action.c \ |
248 | parse_number.c perror_msg.c perror_msg_and_die.c print_file.c \ | 248 | safe_read.c safe_strncpy.c syscalls.c syslog_msg_with_name.c time_string.c \ |
249 | process_escape_sequence.c read_package_field.c recursive_action.c safe_read.c \ | 249 | trim.c unarchive.c unzip.c vdprintf.c verror_msg.c vperror_msg.c wfopen.c xfuncs.c \ |
250 | safe_strncpy.c syscalls.c syslog_msg_with_name.c time_string.c \ | ||
251 | trim.c unzip.c vdprintf.c verror_msg.c vperror_msg.c wfopen.c xfuncs.c \ | ||
252 | xgetcwd.c xreadlink.c xregcomp.c interface.c remove_file.c last_char_is.c \ | 250 | xgetcwd.c xreadlink.c xregcomp.c interface.c remove_file.c last_char_is.c \ |
253 | copyfd.c vherror_msg.c herror_msg.c herror_msg_and_die.c xgethostbyname.c | 251 | copyfd.c vherror_msg.c herror_msg.c herror_msg_and_die.c xgethostbyname.c |
254 | LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC)) | 252 | LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC)) |
@@ -24,6 +24,7 @@ | |||
24 | */ | 24 | */ |
25 | #include <fcntl.h> | 25 | #include <fcntl.h> |
26 | #include <stdlib.h> | 26 | #include <stdlib.h> |
27 | #include <string.h> | ||
27 | #include <getopt.h> | 28 | #include <getopt.h> |
28 | #include <unistd.h> | 29 | #include <unistd.h> |
29 | #include "busybox.h" | 30 | #include "busybox.h" |
@@ -31,9 +32,12 @@ | |||
31 | extern int ar_main(int argc, char **argv) | 32 | extern int ar_main(int argc, char **argv) |
32 | { | 33 | { |
33 | FILE *src_stream = NULL; | 34 | FILE *src_stream = NULL; |
34 | int extract_function = 0, opt = 0; | 35 | char **extract_names = NULL; |
35 | file_headers_t *head; | 36 | char ar_magic[8]; |
36 | file_headers_t *ar_extract_list = NULL; | 37 | int extract_function = 0; |
38 | int opt; | ||
39 | int num_of_entries = 0; | ||
40 | extern off_t archive_offset; | ||
37 | 41 | ||
38 | while ((opt = getopt(argc, argv, "ovtpx")) != -1) { | 42 | while ((opt = getopt(argc, argv, "ovtpx")) != -1) { |
39 | switch (opt) { | 43 | switch (opt) { |
@@ -63,27 +67,23 @@ extern int ar_main(int argc, char **argv) | |||
63 | } | 67 | } |
64 | 68 | ||
65 | src_stream = xfopen(argv[optind++], "r"); | 69 | src_stream = xfopen(argv[optind++], "r"); |
66 | head = get_ar_headers(src_stream); | ||
67 | 70 | ||
68 | /* find files to extract or display */ | 71 | /* check ar magic */ |
69 | /* search through argv and build extract list */ | 72 | fread(ar_magic, 1, 8, src_stream); |
70 | ar_extract_list = (file_headers_t *) xcalloc(1, sizeof(file_headers_t)); | 73 | if (strncmp(ar_magic,"!<arch>",7) != 0) { |
71 | if (optind < argc) { | 74 | error_msg_and_die("invalid magic"); |
72 | while (optind < argc) { | ||
73 | ar_extract_list = add_from_archive_list(head, ar_extract_list, argv[optind]); | ||
74 | optind++; | ||
75 | } | ||
76 | } else { | ||
77 | ar_extract_list = head; | ||
78 | } | 75 | } |
76 | archive_offset = 8; | ||
79 | 77 | ||
80 | /* If there isnt even one possible entry then abort */ | 78 | extract_names = malloc(sizeof(char *)); |
81 | if (ar_extract_list->name == NULL) { | 79 | extract_names[0] = NULL; |
82 | error_msg_and_die("No files to extract"); | 80 | while (optind < argc) { |
83 | } | 81 | num_of_entries++; |
84 | 82 | *extract_names = realloc(*extract_names, num_of_entries); | |
85 | fseek(src_stream, 0, SEEK_SET); | 83 | extract_names[num_of_entries - 1] = xstrdup(argv[optind]); |
86 | extract_archive(src_stream, stdout, ar_extract_list, extract_function, "./"); | 84 | optind++; |
85 | } | ||
87 | 86 | ||
87 | unarchive(src_stream, &get_header_ar, extract_function, "./", extract_names); | ||
88 | return EXIT_SUCCESS; | 88 | return EXIT_SUCCESS; |
89 | } | 89 | } |
diff --git a/archival/ar.c b/archival/ar.c index 7359f4910..fd98d86f5 100644 --- a/archival/ar.c +++ b/archival/ar.c | |||
@@ -24,6 +24,7 @@ | |||
24 | */ | 24 | */ |
25 | #include <fcntl.h> | 25 | #include <fcntl.h> |
26 | #include <stdlib.h> | 26 | #include <stdlib.h> |
27 | #include <string.h> | ||
27 | #include <getopt.h> | 28 | #include <getopt.h> |
28 | #include <unistd.h> | 29 | #include <unistd.h> |
29 | #include "busybox.h" | 30 | #include "busybox.h" |
@@ -31,9 +32,12 @@ | |||
31 | extern int ar_main(int argc, char **argv) | 32 | extern int ar_main(int argc, char **argv) |
32 | { | 33 | { |
33 | FILE *src_stream = NULL; | 34 | FILE *src_stream = NULL; |
34 | int extract_function = 0, opt = 0; | 35 | char **extract_names = NULL; |
35 | file_headers_t *head; | 36 | char ar_magic[8]; |
36 | file_headers_t *ar_extract_list = NULL; | 37 | int extract_function = 0; |
38 | int opt; | ||
39 | int num_of_entries = 0; | ||
40 | extern off_t archive_offset; | ||
37 | 41 | ||
38 | while ((opt = getopt(argc, argv, "ovtpx")) != -1) { | 42 | while ((opt = getopt(argc, argv, "ovtpx")) != -1) { |
39 | switch (opt) { | 43 | switch (opt) { |
@@ -63,27 +67,23 @@ extern int ar_main(int argc, char **argv) | |||
63 | } | 67 | } |
64 | 68 | ||
65 | src_stream = xfopen(argv[optind++], "r"); | 69 | src_stream = xfopen(argv[optind++], "r"); |
66 | head = get_ar_headers(src_stream); | ||
67 | 70 | ||
68 | /* find files to extract or display */ | 71 | /* check ar magic */ |
69 | /* search through argv and build extract list */ | 72 | fread(ar_magic, 1, 8, src_stream); |
70 | ar_extract_list = (file_headers_t *) xcalloc(1, sizeof(file_headers_t)); | 73 | if (strncmp(ar_magic,"!<arch>",7) != 0) { |
71 | if (optind < argc) { | 74 | error_msg_and_die("invalid magic"); |
72 | while (optind < argc) { | ||
73 | ar_extract_list = add_from_archive_list(head, ar_extract_list, argv[optind]); | ||
74 | optind++; | ||
75 | } | ||
76 | } else { | ||
77 | ar_extract_list = head; | ||
78 | } | 75 | } |
76 | archive_offset = 8; | ||
79 | 77 | ||
80 | /* If there isnt even one possible entry then abort */ | 78 | extract_names = malloc(sizeof(char *)); |
81 | if (ar_extract_list->name == NULL) { | 79 | extract_names[0] = NULL; |
82 | error_msg_and_die("No files to extract"); | 80 | while (optind < argc) { |
83 | } | 81 | num_of_entries++; |
84 | 82 | *extract_names = realloc(*extract_names, num_of_entries); | |
85 | fseek(src_stream, 0, SEEK_SET); | 83 | extract_names[num_of_entries - 1] = xstrdup(argv[optind]); |
86 | extract_archive(src_stream, stdout, ar_extract_list, extract_function, "./"); | 84 | optind++; |
85 | } | ||
87 | 86 | ||
87 | unarchive(src_stream, &get_header_ar, extract_function, "./", extract_names); | ||
88 | return EXIT_SUCCESS; | 88 | return EXIT_SUCCESS; |
89 | } | 89 | } |
diff --git a/archival/dpkg_deb.c b/archival/dpkg_deb.c index 400f2385a..77172b0f6 100644 --- a/archival/dpkg_deb.c +++ b/archival/dpkg_deb.c | |||
@@ -26,7 +26,7 @@ extern int dpkg_deb_main(int argc, char **argv) | |||
26 | char *output_buffer = NULL; | 26 | char *output_buffer = NULL; |
27 | int opt = 0; | 27 | int opt = 0; |
28 | int arg_type = 0; | 28 | int arg_type = 0; |
29 | int deb_extract_funct = 0; | 29 | int deb_extract_funct = extract_create_dirs; |
30 | 30 | ||
31 | const int arg_type_prefix = 1; | 31 | const int arg_type_prefix = 1; |
32 | const int arg_type_field = 2; | 32 | const int arg_type_field = 2; |
diff --git a/dpkg_deb.c b/dpkg_deb.c index 400f2385a..77172b0f6 100644 --- a/dpkg_deb.c +++ b/dpkg_deb.c | |||
@@ -26,7 +26,7 @@ extern int dpkg_deb_main(int argc, char **argv) | |||
26 | char *output_buffer = NULL; | 26 | char *output_buffer = NULL; |
27 | int opt = 0; | 27 | int opt = 0; |
28 | int arg_type = 0; | 28 | int arg_type = 0; |
29 | int deb_extract_funct = 0; | 29 | int deb_extract_funct = extract_create_dirs; |
30 | 30 | ||
31 | const int arg_type_prefix = 1; | 31 | const int arg_type_prefix = 1; |
32 | const int arg_type_field = 2; | 32 | const int arg_type_field = 2; |
diff --git a/include/libbb.h b/include/libbb.h index 893a9f07f..e42ca9f2b 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -213,23 +213,9 @@ char *xreadlink(const char *path); | |||
213 | char *concat_path_file(const char *path, const char *filename); | 213 | char *concat_path_file(const char *path, const char *filename); |
214 | char *last_char_is(const char *s, int c); | 214 | char *last_char_is(const char *s, int c); |
215 | 215 | ||
216 | typedef struct file_headers_s { | 216 | void *get_header_ar(FILE *in_file); |
217 | char *name; | 217 | void *get_header_cpio(FILE *src_stream); |
218 | char *link_name; | 218 | void *get_header_tar(FILE *tar_stream); |
219 | off_t size; | ||
220 | uid_t uid; | ||
221 | gid_t gid; | ||
222 | mode_t mode; | ||
223 | time_t mtime; | ||
224 | off_t offset; | ||
225 | dev_t device; | ||
226 | struct file_headers_s *next; | ||
227 | } file_headers_t; | ||
228 | file_headers_t *get_ar_headers(FILE *in_file); | ||
229 | file_headers_t *get_tar_headers(FILE *tar_stream); | ||
230 | file_headers_t *get_tar_gz_headers(FILE *compressed_stream); | ||
231 | file_headers_t *append_archive_list(file_headers_t *head, file_headers_t *tail_entry); | ||
232 | file_headers_t *add_from_archive_list(file_headers_t *master_list, file_headers_t *new_list, const char *name); | ||
233 | 219 | ||
234 | enum extract_functions_e { | 220 | enum extract_functions_e { |
235 | extract_verbose_list = 1, | 221 | extract_verbose_list = 1, |
@@ -240,18 +226,20 @@ enum extract_functions_e { | |||
240 | extract_preserve_date = 32, | 226 | extract_preserve_date = 32, |
241 | extract_data_tar_gz = 64, | 227 | extract_data_tar_gz = 64, |
242 | extract_control_tar_gz = 128, | 228 | extract_control_tar_gz = 128, |
243 | extract_unzip_only = 256 | 229 | extract_unzip_only = 256, |
230 | extract_unconditional = 512, | ||
231 | extract_create_dirs = 1024 | ||
244 | }; | 232 | }; |
245 | char *extract_archive(FILE *src_stream, FILE *out_stream, file_headers_t *extract_headers, int function, const char *prefix); | 233 | char *unarchive(FILE *src_stream, void *(*get_header)(FILE *), |
246 | char *deb_extract(const char *package_filename, FILE *out_stream, const int function, | 234 | const int extract_function, const char *prefix, char **extract_names); |
235 | char *deb_extract(const char *package_filename, FILE *out_stream, const int extract_function, | ||
247 | const char *prefix, const char *filename); | 236 | const char *prefix, const char *filename); |
248 | char *read_package_field(const char *package_buffer); | 237 | char *read_package_field(const char *package_buffer); |
249 | int seek_sub_file(FILE *in_file, file_headers_t *headers, const char *tar_gz_file); | ||
250 | char *fgets_str(FILE *file, const char *terminating_string); | 238 | char *fgets_str(FILE *file, const char *terminating_string); |
251 | 239 | ||
252 | extern int unzip(FILE *l_in_file, FILE *l_out_file); | 240 | extern int unzip(FILE *l_in_file, FILE *l_out_file); |
253 | extern void gz_close(int gunzip_pid); | 241 | extern void gz_close(int gunzip_pid); |
254 | extern int gz_open(FILE *compressed_file, int *pid); | 242 | extern FILE *gz_open(FILE *compressed_file, int *pid); |
255 | 243 | ||
256 | extern struct hostent *xgethostbyname(const char *name); | 244 | extern struct hostent *xgethostbyname(const char *name); |
257 | 245 | ||
diff --git a/libbb/add_from_archive_list.c b/libbb/add_from_archive_list.c deleted file mode 100644 index 7e4e07159..000000000 --- a/libbb/add_from_archive_list.c +++ /dev/null | |||
@@ -1,31 +0,0 @@ | |||
1 | /* | ||
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 "libbb.h" | ||
18 | |||
19 | file_headers_t *add_from_archive_list(file_headers_t *master_list, file_headers_t *new_list, const char *filename) | ||
20 | { | ||
21 | file_headers_t *list_ptr; | ||
22 | |||
23 | list_ptr = master_list; | ||
24 | while (list_ptr != NULL) { | ||
25 | if (strcmp(filename, list_ptr->name) == 0) { | ||
26 | return(append_archive_list(new_list, list_ptr)); | ||
27 | } | ||
28 | list_ptr = list_ptr->next; | ||
29 | } | ||
30 | return(NULL); | ||
31 | } \ No newline at end of file | ||
diff --git a/libbb/append_archive_list.c b/libbb/append_archive_list.c deleted file mode 100644 index c955d08d2..000000000 --- a/libbb/append_archive_list.c +++ /dev/null | |||
@@ -1,39 +0,0 @@ | |||
1 | /* | ||
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 <stdlib.h> | ||
18 | #include "libbb.h" | ||
19 | |||
20 | file_headers_t *append_archive_list(file_headers_t *head, file_headers_t *tail_entry) | ||
21 | { | ||
22 | file_headers_t *tail_ptr; | ||
23 | file_headers_t *new_node = (file_headers_t *) malloc(sizeof(file_headers_t)); | ||
24 | |||
25 | *new_node = *tail_entry; | ||
26 | new_node->next = NULL; | ||
27 | |||
28 | if (head->name == NULL) { | ||
29 | head = new_node; | ||
30 | } else { | ||
31 | tail_ptr = head; | ||
32 | while(tail_ptr->next != NULL) { | ||
33 | tail_ptr = tail_ptr->next; | ||
34 | } | ||
35 | tail_ptr->next = new_node; | ||
36 | } | ||
37 | |||
38 | return(head); | ||
39 | } \ No newline at end of file | ||
diff --git a/libbb/deb_extract.c b/libbb/deb_extract.c deleted file mode 100644 index f64193970..000000000 --- a/libbb/deb_extract.c +++ /dev/null | |||
@@ -1,124 +0,0 @@ | |||
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 | } \ No newline at end of file | ||
diff --git a/libbb/extract_archive.c b/libbb/extract_archive.c deleted file mode 100644 index f22cbb619..000000000 --- a/libbb/extract_archive.c +++ /dev/null | |||
@@ -1,132 +0,0 @@ | |||
1 | /* | ||
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 <stdlib.h> | ||
18 | #include <string.h> | ||
19 | #include <time.h> | ||
20 | #include <unistd.h> | ||
21 | #include <utime.h> | ||
22 | #include "libbb.h" | ||
23 | |||
24 | /* Extract files in the linear linked list 'extract_headers' to either | ||
25 | * filesystem, stdout or buffer depending on the value of 'function' which is | ||
26 | * described in extract_files.h | ||
27 | * | ||
28 | * prefix doesnt have to be just a directory, it may prefix the filename as well. | ||
29 | * | ||
30 | * e.g. '/var/lib/dpkg/info/dpkg.' will extract all files to the base bath | ||
31 | * '/var/lib/dpkg/info/' and all files/dirs created in that dir will have | ||
32 | * 'dpkg.' as their prefix | ||
33 | * | ||
34 | * For this reason if prefix does point to a dir then it must end with a | ||
35 | * trailing '/' or else the last dir will be assumed to be the file prefix | ||
36 | */ | ||
37 | |||
38 | char *extract_archive(FILE *src_stream, FILE *out_stream, file_headers_t *extract_headers, int function, const char *prefix) | ||
39 | { | ||
40 | FILE *dst_stream = NULL; | ||
41 | char *full_name = NULL; | ||
42 | char *buffer = NULL; | ||
43 | size_t uncompressed_count = 0; | ||
44 | struct utimbuf t; | ||
45 | |||
46 | /* find files to extract or display */ | ||
47 | while (extract_headers != NULL) { | ||
48 | /* prefix doesnt have to be a proper path it may prepend | ||
49 | * the filename as well */ | ||
50 | if (prefix != NULL) { | ||
51 | /* strip leading '/' in filename to extract as prefix may not be dir */ | ||
52 | char *tmp_str = strchr(extract_headers->name, '/'); | ||
53 | if (tmp_str == NULL) { | ||
54 | tmp_str = extract_headers->name; | ||
55 | } else { | ||
56 | tmp_str++; | ||
57 | } | ||
58 | /* Cant use concat_path_file here as prefix might not be a directory */ | ||
59 | full_name = xmalloc(strlen(prefix) + strlen(tmp_str) + 1); | ||
60 | strcpy(full_name, prefix); | ||
61 | strcat(full_name, tmp_str); | ||
62 | } else { | ||
63 | full_name = extract_headers->name; | ||
64 | } | ||
65 | |||
66 | /* Seek to start of file, have to use fgetc as src_stream may be a pipe | ||
67 | * you cant [lf]seek on pipes */ | ||
68 | while (uncompressed_count < extract_headers->offset) { | ||
69 | fgetc(src_stream); | ||
70 | uncompressed_count++; | ||
71 | } | ||
72 | |||
73 | if (S_ISREG(extract_headers->mode)) { | ||
74 | if (function & extract_to_stdout) { | ||
75 | copy_file_chunk(src_stream, out_stream, extract_headers->size); | ||
76 | uncompressed_count += extract_headers->size; | ||
77 | } | ||
78 | else if (function & extract_all_to_fs) { | ||
79 | dst_stream = wfopen(full_name, "w"); | ||
80 | copy_file_chunk(src_stream, dst_stream, extract_headers->size); | ||
81 | uncompressed_count += extract_headers->size; | ||
82 | fclose(dst_stream); | ||
83 | } | ||
84 | else if (function & extract_one_to_buffer) { | ||
85 | buffer = (char *) xmalloc(extract_headers->size + 1); | ||
86 | fread(buffer, 1, extract_headers->size, src_stream); | ||
87 | break; | ||
88 | } | ||
89 | } | ||
90 | #if defined BB_DPKG | defined BB_DPKG_DEB | ||
91 | else if (S_ISDIR(extract_headers->mode)) { | ||
92 | if (function & extract_all_to_fs) { | ||
93 | if (create_path(full_name, extract_headers->mode) == FALSE) { | ||
94 | perror_msg("Cannot mkdir %s", extract_headers->name); | ||
95 | return NULL; | ||
96 | } | ||
97 | } | ||
98 | } | ||
99 | else if (S_ISLNK(extract_headers->mode)) { | ||
100 | if (function & extract_all_to_fs) { | ||
101 | if (symlink(extract_headers->link_name, full_name) < 0) { | ||
102 | perror_msg("Cannot create hard link from %s to '%s'", extract_headers->name, extract_headers->link_name); | ||
103 | return NULL; | ||
104 | } | ||
105 | } | ||
106 | } | ||
107 | #endif | ||
108 | if (function & extract_verbose_list) { | ||
109 | fprintf(out_stream, "%s %d/%d %8d %s ", mode_string(extract_headers->mode), | ||
110 | extract_headers->uid, extract_headers->gid, | ||
111 | (int) extract_headers->size, time_string(extract_headers->mtime)); | ||
112 | } | ||
113 | |||
114 | if ((function & extract_list) || (function & extract_verbose_list)){ | ||
115 | /* fputs doesnt add a trailing \n, so use fprintf */ | ||
116 | fprintf(out_stream, "%s\n", extract_headers->name); | ||
117 | } | ||
118 | |||
119 | if (function & extract_preserve_date) { | ||
120 | t.actime = extract_headers->mtime; | ||
121 | t.modtime = extract_headers->mtime; | ||
122 | utime(full_name, &t); | ||
123 | } | ||
124 | chmod(full_name, extract_headers->mode); | ||
125 | free(full_name); | ||
126 | |||
127 | extract_headers = extract_headers->next; | ||
128 | } | ||
129 | |||
130 | return(buffer); | ||
131 | } | ||
132 | |||
diff --git a/libbb/get_ar_headers.c b/libbb/get_ar_headers.c deleted file mode 100644 index 414498b4f..000000000 --- a/libbb/get_ar_headers.c +++ /dev/null | |||
@@ -1,119 +0,0 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdlib.h> | ||
29 | #include <string.h> | ||
30 | #include <unistd.h> | ||
31 | #include "libbb.h" | ||
32 | |||
33 | file_headers_t *get_ar_headers(FILE *in_file) | ||
34 | { | ||
35 | typedef struct raw_ar_header_s { /* Byte Offset */ | ||
36 | char name[16]; /* 0-15 */ | ||
37 | char date[12]; /* 16-27 */ | ||
38 | char uid[6]; | ||
39 | char gid[6]; /* 28-39 */ | ||
40 | char mode[8]; /* 40-47 */ | ||
41 | char size[10]; /* 48-57 */ | ||
42 | char fmag[2]; /* 58-59 */ | ||
43 | } raw_ar_header_t; | ||
44 | |||
45 | raw_ar_header_t raw_ar_header; | ||
46 | |||
47 | file_headers_t *ar_list, *ar_entry; | ||
48 | char ar_magic[8]; | ||
49 | char *long_name=NULL; | ||
50 | |||
51 | /* check ar magic */ | ||
52 | if (fread(ar_magic, 1, 8, in_file) != 8) { | ||
53 | error_msg("cannot read magic"); | ||
54 | return(NULL); | ||
55 | } | ||
56 | |||
57 | if (strncmp(ar_magic,"!<arch>",7) != 0) { | ||
58 | error_msg("invalid magic"); | ||
59 | return(NULL); | ||
60 | } | ||
61 | |||
62 | ar_list = (file_headers_t *) xcalloc(1, sizeof(file_headers_t)); | ||
63 | |||
64 | while (fread((char *) &raw_ar_header, 1, 60, in_file) == 60) { | ||
65 | ar_entry = (file_headers_t *) xcalloc(1, sizeof(file_headers_t)); | ||
66 | /* check the end of header markers are valid */ | ||
67 | if ((raw_ar_header.fmag[0] != '`') || (raw_ar_header.fmag[1] != '\n')) { | ||
68 | char newline; | ||
69 | if (raw_ar_header.fmag[1] != '`') { | ||
70 | break; | ||
71 | } | ||
72 | /* some version of ar, have an extra '\n' after each entry */ | ||
73 | fread(&newline, 1, 1, in_file); | ||
74 | if (newline!='\n') { | ||
75 | break; | ||
76 | } | ||
77 | /* fix up the header, we started reading 1 byte too early due to a '\n' */ | ||
78 | memmove((char *) &raw_ar_header, (char *)&raw_ar_header+1, 59); | ||
79 | /* dont worry about adding the last '\n', we dont need it now */ | ||
80 | } | ||
81 | |||
82 | ar_entry->size = (size_t) atoi(raw_ar_header.size); | ||
83 | /* long filenames have '/' as the first character */ | ||
84 | if (raw_ar_header.name[0] == '/') { | ||
85 | if (raw_ar_header.name[1] == '/') { | ||
86 | /* multiple long filenames are stored as data in one entry */ | ||
87 | long_name = (char *) xrealloc(long_name, ar_entry->size); | ||
88 | fread(long_name, 1, ar_entry->size, in_file); | ||
89 | continue; | ||
90 | } | ||
91 | else { | ||
92 | /* The number after the '/' indicates the offset in the ar data section | ||
93 | (saved in variable long_name) that conatains the real filename */ | ||
94 | const int long_name_offset = (int) atoi((char *) &raw_ar_header.name[1]); | ||
95 | ar_entry->name = xmalloc(strlen(&long_name[long_name_offset])); | ||
96 | strcpy(ar_entry->name, &long_name[long_name_offset]); | ||
97 | } | ||
98 | } | ||
99 | else { | ||
100 | /* short filenames */ | ||
101 | ar_entry->name = xmalloc(16); | ||
102 | ar_entry->name = strncpy(ar_entry->name, raw_ar_header.name, 16); | ||
103 | } | ||
104 | ar_entry->name[strcspn(ar_entry->name, " /")]='\0'; | ||
105 | |||
106 | /* convert the rest of the now valid char header to its typed struct */ | ||
107 | parse_mode(raw_ar_header.mode, &ar_entry->mode); | ||
108 | ar_entry->mtime = atoi(raw_ar_header.date); | ||
109 | ar_entry->uid = atoi(raw_ar_header.uid); | ||
110 | ar_entry->gid = atoi(raw_ar_header.gid); | ||
111 | ar_entry->offset = ftell(in_file); | ||
112 | ar_entry->next = NULL; | ||
113 | |||
114 | fseek(in_file, (off_t) ar_entry->size, SEEK_CUR); | ||
115 | |||
116 | ar_list = append_archive_list(ar_list, ar_entry); | ||
117 | } | ||
118 | return(ar_list); | ||
119 | } | ||
diff --git a/libbb/get_tar_gz_headers.c b/libbb/get_tar_gz_headers.c deleted file mode 100644 index fb9b7b63f..000000000 --- a/libbb/get_tar_gz_headers.c +++ /dev/null | |||
@@ -1,39 +0,0 @@ | |||
1 | /* | ||
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 | #include <stdio.h> | ||
17 | #include <unistd.h> | ||
18 | #include "libbb.h" | ||
19 | |||
20 | file_headers_t *get_tar_gz_headers(FILE *compressed_stream) | ||
21 | { | ||
22 | FILE *uncompressed_stream; | ||
23 | file_headers_t *tar_headers_tree; | ||
24 | int gunzip_pid; | ||
25 | int gz_fd ; | ||
26 | |||
27 | /* open a stream of decompressed data */ | ||
28 | gz_fd = gz_open(compressed_stream, &gunzip_pid); | ||
29 | if (gz_fd == -1) { | ||
30 | error_msg("Couldnt initialise gzip stream"); | ||
31 | } | ||
32 | uncompressed_stream = fdopen(gz_fd, "r"); | ||
33 | tar_headers_tree = get_tar_headers(uncompressed_stream); | ||
34 | fclose(uncompressed_stream); | ||
35 | close(gz_fd); | ||
36 | gz_close(gunzip_pid); | ||
37 | |||
38 | return(tar_headers_tree); | ||
39 | } \ No newline at end of file | ||
diff --git a/libbb/get_tar_headers.c b/libbb/get_tar_headers.c deleted file mode 100644 index 5ddf4d9c3..000000000 --- a/libbb/get_tar_headers.c +++ /dev/null | |||
@@ -1,111 +0,0 @@ | |||
1 | /* | ||
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 | #include <stdlib.h> | ||
17 | #include <string.h> | ||
18 | #include <unistd.h> | ||
19 | #include "libbb.h" | ||
20 | |||
21 | file_headers_t *get_tar_headers(FILE *tar_stream) | ||
22 | { | ||
23 | typedef struct raw_tar_header { | ||
24 | char name[100]; /* 0-99 */ | ||
25 | char mode[8]; /* 100-107 */ | ||
26 | char uid[8]; /* 108-115 */ | ||
27 | char gid[8]; /* 116-123 */ | ||
28 | char size[12]; /* 124-135 */ | ||
29 | char mtime[12]; /* 136-147 */ | ||
30 | char chksum[8]; /* 148-155 */ | ||
31 | char typeflag; /* 156-156 */ | ||
32 | char linkname[100]; /* 157-256 */ | ||
33 | char magic[6]; /* 257-262 */ | ||
34 | char version[2]; /* 263-264 */ | ||
35 | char uname[32]; /* 265-296 */ | ||
36 | char gname[32]; /* 297-328 */ | ||
37 | char devmajor[8]; /* 329-336 */ | ||
38 | char devminor[8]; /* 337-344 */ | ||
39 | char prefix[155]; /* 345-499 */ | ||
40 | char padding[12]; /* 500-512 */ | ||
41 | } raw_tar_header_t; | ||
42 | |||
43 | raw_tar_header_t raw_tar_header; | ||
44 | unsigned char *temp = (unsigned char *) &raw_tar_header; | ||
45 | file_headers_t *tar_headers_list = (file_headers_t *) calloc(1, sizeof(file_headers_t)); | ||
46 | file_headers_t *tar_entry; | ||
47 | long i; | ||
48 | long next_header_offset = 0; | ||
49 | long current_offset = 0; | ||
50 | |||
51 | while (fread((char *) &raw_tar_header, 1, 512, tar_stream) == 512) { | ||
52 | long sum = 0; | ||
53 | current_offset += 512; | ||
54 | |||
55 | /* Check header has valid magic, unfortunately some tar files | ||
56 | * have empty (0'ed) tar entries at the end, which will | ||
57 | * cause this to fail, so fail silently for now | ||
58 | */ | ||
59 | if (strncmp(raw_tar_header.magic, "ustar", 5) != 0) { | ||
60 | // error_msg("invalid magic, [%s]\n", raw_tar_header.magic); | ||
61 | break; | ||
62 | } | ||
63 | |||
64 | /* Do checksum on headers */ | ||
65 | for (i = 0; i < 148 ; i++) { | ||
66 | sum += temp[i]; | ||
67 | } | ||
68 | sum += ' ' * 8; | ||
69 | for (i = 156; i < 512 ; i++) { | ||
70 | sum += temp[i]; | ||
71 | } | ||
72 | if (sum != strtol(raw_tar_header.chksum, NULL, 8)) { | ||
73 | error_msg("Invalid tar header checksum"); | ||
74 | break; | ||
75 | } | ||
76 | |||
77 | /* convert to type'ed variables */ | ||
78 | tar_entry = xcalloc(1, sizeof(file_headers_t)); | ||
79 | tar_entry->name = xstrdup(raw_tar_header.name); | ||
80 | |||
81 | tar_entry->offset = (off_t) current_offset; | ||
82 | parse_mode(raw_tar_header.mode, &tar_entry->mode); | ||
83 | tar_entry->uid = strtol(raw_tar_header.uid, NULL, 8); | ||
84 | tar_entry->gid = strtol(raw_tar_header.gid, NULL, 8); | ||
85 | tar_entry->size = strtol(raw_tar_header.size, NULL, 8); | ||
86 | tar_entry->mtime = strtol(raw_tar_header.mtime, NULL, 8); | ||
87 | tar_entry->link_name = xstrdup(raw_tar_header.linkname); | ||
88 | // tar_entry->devmajor = strtol(rawHeader->devmajor, NULL, 8); | ||
89 | // tar_entry->devminor = strtol(rawHeader->devminor, NULL, 8); | ||
90 | |||
91 | tar_headers_list = append_archive_list(tar_headers_list, tar_entry); | ||
92 | |||
93 | /* start of next header is at */ | ||
94 | next_header_offset = current_offset + tar_entry->size; | ||
95 | |||
96 | if (tar_entry->size % 512 != 0) { | ||
97 | next_header_offset += (512 - tar_entry->size % 512); | ||
98 | } | ||
99 | /* | ||
100 | * Seek to start of next block, cant use fseek as unzip() does support it | ||
101 | */ | ||
102 | while (current_offset < next_header_offset) { | ||
103 | if (fgetc(tar_stream) == EOF) { | ||
104 | break; | ||
105 | } | ||
106 | current_offset++; | ||
107 | } | ||
108 | } | ||
109 | return(tar_headers_list); | ||
110 | } | ||
111 | |||
diff --git a/libbb/gz_open.c b/libbb/gz_open.c index 19ec0a066..b23920b16 100644 --- a/libbb/gz_open.c +++ b/libbb/gz_open.c | |||
@@ -6,17 +6,17 @@ | |||
6 | #include <unistd.h> | 6 | #include <unistd.h> |
7 | #include "libbb.h" | 7 | #include "libbb.h" |
8 | 8 | ||
9 | extern int gz_open(FILE *compressed_file, int *pid) | 9 | extern FILE *gz_open(FILE *compressed_file, int *pid) |
10 | { | 10 | { |
11 | int unzip_pipe[2]; | 11 | int unzip_pipe[2]; |
12 | 12 | ||
13 | if (pipe(unzip_pipe)!=0) { | 13 | if (pipe(unzip_pipe)!=0) { |
14 | error_msg("pipe error"); | 14 | error_msg("pipe error"); |
15 | return(EXIT_FAILURE); | 15 | return(NULL); |
16 | } | 16 | } |
17 | if ((*pid = fork()) == -1) { | 17 | if ((*pid = fork()) == -1) { |
18 | error_msg("fork failured"); | 18 | error_msg("fork failured"); |
19 | return(EXIT_FAILURE); | 19 | return(NULL); |
20 | } | 20 | } |
21 | if (*pid==0) { | 21 | if (*pid==0) { |
22 | /* child process */ | 22 | /* child process */ |
@@ -27,7 +27,9 @@ extern int gz_open(FILE *compressed_file, int *pid) | |||
27 | close(unzip_pipe[1]); | 27 | close(unzip_pipe[1]); |
28 | exit(EXIT_SUCCESS); | 28 | exit(EXIT_SUCCESS); |
29 | } | 29 | } |
30 | |||
31 | close(unzip_pipe[1]); | 30 | close(unzip_pipe[1]); |
32 | return(unzip_pipe[0]); | 31 | if (unzip_pipe[0] == -1) { |
33 | } \ No newline at end of file | 32 | error_msg("Couldnt initialise gzip stream"); |
33 | } | ||
34 | return(fdopen(unzip_pipe[0], "r")); | ||
35 | } | ||
diff --git a/libbb/libbb.h b/libbb/libbb.h index 893a9f07f..e42ca9f2b 100644 --- a/libbb/libbb.h +++ b/libbb/libbb.h | |||
@@ -213,23 +213,9 @@ char *xreadlink(const char *path); | |||
213 | char *concat_path_file(const char *path, const char *filename); | 213 | char *concat_path_file(const char *path, const char *filename); |
214 | char *last_char_is(const char *s, int c); | 214 | char *last_char_is(const char *s, int c); |
215 | 215 | ||
216 | typedef struct file_headers_s { | 216 | void *get_header_ar(FILE *in_file); |
217 | char *name; | 217 | void *get_header_cpio(FILE *src_stream); |
218 | char *link_name; | 218 | void *get_header_tar(FILE *tar_stream); |
219 | off_t size; | ||
220 | uid_t uid; | ||
221 | gid_t gid; | ||
222 | mode_t mode; | ||
223 | time_t mtime; | ||
224 | off_t offset; | ||
225 | dev_t device; | ||
226 | struct file_headers_s *next; | ||
227 | } file_headers_t; | ||
228 | file_headers_t *get_ar_headers(FILE *in_file); | ||
229 | file_headers_t *get_tar_headers(FILE *tar_stream); | ||
230 | file_headers_t *get_tar_gz_headers(FILE *compressed_stream); | ||
231 | file_headers_t *append_archive_list(file_headers_t *head, file_headers_t *tail_entry); | ||
232 | file_headers_t *add_from_archive_list(file_headers_t *master_list, file_headers_t *new_list, const char *name); | ||
233 | 219 | ||
234 | enum extract_functions_e { | 220 | enum extract_functions_e { |
235 | extract_verbose_list = 1, | 221 | extract_verbose_list = 1, |
@@ -240,18 +226,20 @@ enum extract_functions_e { | |||
240 | extract_preserve_date = 32, | 226 | extract_preserve_date = 32, |
241 | extract_data_tar_gz = 64, | 227 | extract_data_tar_gz = 64, |
242 | extract_control_tar_gz = 128, | 228 | extract_control_tar_gz = 128, |
243 | extract_unzip_only = 256 | 229 | extract_unzip_only = 256, |
230 | extract_unconditional = 512, | ||
231 | extract_create_dirs = 1024 | ||
244 | }; | 232 | }; |
245 | char *extract_archive(FILE *src_stream, FILE *out_stream, file_headers_t *extract_headers, int function, const char *prefix); | 233 | char *unarchive(FILE *src_stream, void *(*get_header)(FILE *), |
246 | char *deb_extract(const char *package_filename, FILE *out_stream, const int function, | 234 | const int extract_function, const char *prefix, char **extract_names); |
235 | char *deb_extract(const char *package_filename, FILE *out_stream, const int extract_function, | ||
247 | const char *prefix, const char *filename); | 236 | const char *prefix, const char *filename); |
248 | char *read_package_field(const char *package_buffer); | 237 | char *read_package_field(const char *package_buffer); |
249 | int seek_sub_file(FILE *in_file, file_headers_t *headers, const char *tar_gz_file); | ||
250 | char *fgets_str(FILE *file, const char *terminating_string); | 238 | char *fgets_str(FILE *file, const char *terminating_string); |
251 | 239 | ||
252 | extern int unzip(FILE *l_in_file, FILE *l_out_file); | 240 | extern int unzip(FILE *l_in_file, FILE *l_out_file); |
253 | extern void gz_close(int gunzip_pid); | 241 | extern void gz_close(int gunzip_pid); |
254 | extern int gz_open(FILE *compressed_file, int *pid); | 242 | extern FILE *gz_open(FILE *compressed_file, int *pid); |
255 | 243 | ||
256 | extern struct hostent *xgethostbyname(const char *name); | 244 | extern struct hostent *xgethostbyname(const char *name); |
257 | 245 | ||
diff --git a/libbb/unarchive.c b/libbb/unarchive.c new file mode 100644 index 000000000..a0cc28eca --- /dev/null +++ b/libbb/unarchive.c | |||
@@ -0,0 +1,500 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2000 by Glenn McGrath | ||
3 | * Copyright (C) 2001 by Laurence Anderson | ||
4 | * | ||
5 | * Based on previous work by busybox developers and others. | ||
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 | ||
15 | * GNU Library 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 | |||
22 | #include <stdio.h> | ||
23 | #include <errno.h> | ||
24 | #include <stdlib.h> | ||
25 | #include <string.h> | ||
26 | #include <unistd.h> | ||
27 | #include <utime.h> | ||
28 | #include "libbb.h" | ||
29 | |||
30 | typedef struct file_headers_s { | ||
31 | char *name; | ||
32 | char *link_name; | ||
33 | off_t size; | ||
34 | uid_t uid; | ||
35 | gid_t gid; | ||
36 | mode_t mode; | ||
37 | time_t mtime; | ||
38 | dev_t device; | ||
39 | } file_header_t; | ||
40 | |||
41 | off_t archive_offset; | ||
42 | |||
43 | void seek_sub_file(FILE *src_stream, const int count) | ||
44 | { | ||
45 | int i; | ||
46 | /* Try to fseek as faster */ | ||
47 | archive_offset += count; | ||
48 | if (fseek(src_stream, count, SEEK_CUR) != 0 && errno == ESPIPE) { | ||
49 | for (i = 0; i < count; i++) { | ||
50 | fgetc(src_stream); | ||
51 | } | ||
52 | } | ||
53 | return; | ||
54 | } | ||
55 | |||
56 | |||
57 | /* Extract the data postioned at src_stream to either filesystem, stdout or | ||
58 | * buffer depending on the value of 'function' which is defined in libbb.h | ||
59 | * | ||
60 | * prefix doesnt have to be just a directory, it may prefix the filename as well. | ||
61 | * | ||
62 | * e.g. '/var/lib/dpkg/info/dpkg.' will extract all files to the base bath | ||
63 | * '/var/lib/dpkg/info/' and all files/dirs created in that dir will have | ||
64 | * 'dpkg.' as their prefix | ||
65 | * | ||
66 | * For this reason if prefix does point to a dir then it must end with a | ||
67 | * trailing '/' or else the last dir will be assumed to be the file prefix | ||
68 | */ | ||
69 | char *extract_archive(FILE *src_stream, FILE *out_stream, const file_header_t *file_entry, | ||
70 | const int function, const char *prefix) | ||
71 | { | ||
72 | FILE *dst_stream = NULL; | ||
73 | char *full_name = NULL; | ||
74 | char *buffer = NULL; | ||
75 | struct utimbuf t; | ||
76 | |||
77 | /* prefix doesnt have to be a proper path it may prepend | ||
78 | * the filename as well */ | ||
79 | if (prefix != NULL) { | ||
80 | /* strip leading '/' in filename to extract as prefix may not be dir */ | ||
81 | /* Cant use concat_path_file here as prefix might not be a directory */ | ||
82 | char *path = file_entry->name; | ||
83 | if (*path == '/') { | ||
84 | path++; | ||
85 | } | ||
86 | full_name = xmalloc(strlen(prefix) + strlen(path) + 1); | ||
87 | strcpy(full_name, prefix); | ||
88 | strcat(full_name, path); | ||
89 | } else { | ||
90 | full_name = file_entry->name; | ||
91 | } | ||
92 | |||
93 | if (function & extract_to_stdout) { | ||
94 | if (S_ISREG(file_entry->mode)) { | ||
95 | copy_file_chunk(src_stream, out_stream, file_entry->size); | ||
96 | archive_offset += file_entry->size; | ||
97 | } | ||
98 | } | ||
99 | else if (function & extract_one_to_buffer) { | ||
100 | if (S_ISREG(file_entry->mode)) { | ||
101 | buffer = (char *) xmalloc(file_entry->size + 1); | ||
102 | fread(buffer, 1, file_entry->size, src_stream); | ||
103 | archive_offset += file_entry->size; | ||
104 | return(buffer); | ||
105 | } | ||
106 | } | ||
107 | else if (function & extract_all_to_fs) { | ||
108 | #if 0 | ||
109 | struct stat oldfile; | ||
110 | if ( (S_ISLNK(file_entry->mode) ? lstat (full_name, &oldfile) : stat (full_name, &oldfile)) == 0) { /* The file already exists */ | ||
111 | if (function & extract_unconditional || oldfile.st_mtime < file_entry->mtime) { | ||
112 | if (!S_ISDIR(oldfile.st_mode)) { | ||
113 | unlink(full_name); /* Directories might not be empty etc */ | ||
114 | } | ||
115 | } else { | ||
116 | error_msg("%s not created: newer or same age file exists", file_entry->name); | ||
117 | if (S_ISREG(file_entry->mode)) { | ||
118 | seek_sub_file(src_stream, file_entry->size); | ||
119 | } | ||
120 | return (NULL); | ||
121 | } | ||
122 | } | ||
123 | #endif | ||
124 | switch(file_entry->mode & S_IFMT) { | ||
125 | case S_IFREG: | ||
126 | if (file_entry->link_name) { /* Found a cpio hard link */ | ||
127 | if (link(file_entry->link_name, full_name) != 0) { | ||
128 | perror_msg("Cannot link from %s to '%s'", | ||
129 | file_entry->name, file_entry->link_name); | ||
130 | } | ||
131 | } else { | ||
132 | if ((dst_stream = wfopen(full_name, "w")) == NULL) { | ||
133 | seek_sub_file(src_stream, file_entry->size); | ||
134 | return NULL; | ||
135 | } | ||
136 | archive_offset += file_entry->size; | ||
137 | copy_file_chunk(src_stream, dst_stream, file_entry->size); | ||
138 | fclose(dst_stream); | ||
139 | } | ||
140 | break; | ||
141 | case S_IFDIR: | ||
142 | /* Use create_path instead of mkdir incase prefix path | ||
143 | * hasnt been created */ | ||
144 | if (function & extract_create_dirs) { | ||
145 | if (create_path(full_name, file_entry->mode) == FALSE) { | ||
146 | return NULL; | ||
147 | } | ||
148 | } | ||
149 | break; | ||
150 | case S_IFLNK: | ||
151 | if (symlink(file_entry->link_name, full_name) < 0) { | ||
152 | perror_msg("Cannot create symlink from %s to '%s'", file_entry->name, file_entry->link_name); | ||
153 | return NULL; | ||
154 | } | ||
155 | break; | ||
156 | case S_IFSOCK: | ||
157 | case S_IFBLK: | ||
158 | case S_IFCHR: | ||
159 | case S_IFIFO: | ||
160 | if (mknod(full_name, file_entry->mode, file_entry->device) == -1) { | ||
161 | perror_msg("Cannot create node %s", file_entry->name); | ||
162 | return NULL; | ||
163 | } | ||
164 | break; | ||
165 | } | ||
166 | if (function & extract_preserve_date) { | ||
167 | t.actime = file_entry->mtime; | ||
168 | t.modtime = file_entry->mtime; | ||
169 | utime(full_name, &t); | ||
170 | } | ||
171 | chmod(full_name, file_entry->mode); | ||
172 | lchown(full_name, file_entry->uid, file_entry->gid); | ||
173 | } else { | ||
174 | /* If we arent extracting data we have to skip it, | ||
175 | * if data size is 0 then then just do it anyway | ||
176 | * (saves testing for it) */ | ||
177 | seek_sub_file(src_stream, file_entry->size); | ||
178 | } | ||
179 | |||
180 | /* extract_list and extract_verbose_list can be used in conjunction | ||
181 | * with one of the above four extraction functions, so do this seperately */ | ||
182 | if (function & extract_verbose_list) { | ||
183 | fprintf(out_stream, "%s %d/%d %8d %s ", mode_string(file_entry->mode), | ||
184 | file_entry->uid, file_entry->gid, | ||
185 | (int) file_entry->size, time_string(file_entry->mtime)); | ||
186 | } | ||
187 | if ((function & extract_list) || (function & extract_verbose_list)){ | ||
188 | /* fputs doesnt add a trailing \n, so use fprintf */ | ||
189 | fprintf(out_stream, "%s\n", file_entry->name); | ||
190 | } | ||
191 | |||
192 | free(full_name); | ||
193 | |||
194 | return(NULL); /* Maybe we should say if failed */ | ||
195 | } | ||
196 | |||
197 | #if defined BB_AR || defined BB_CPIO || defined BB_UNTAR | ||
198 | char *unarchive(FILE *src_stream, void *(*get_headers)(FILE *), | ||
199 | const int extract_function, const char *prefix, char **extract_names) | ||
200 | { | ||
201 | file_header_t *file_entry; | ||
202 | int found; | ||
203 | int i; | ||
204 | char *buffer = NULL; | ||
205 | |||
206 | archive_offset = 0; | ||
207 | while ((file_entry = (file_header_t *) get_headers(src_stream)) != NULL) { | ||
208 | found = FALSE; | ||
209 | if (extract_names[0] != NULL) { | ||
210 | for(i = 0; extract_names[i] != 0; i++) { | ||
211 | if (strcmp(extract_names[i], file_entry->name) == 0) { | ||
212 | found = TRUE; | ||
213 | } | ||
214 | } | ||
215 | if (!found) { | ||
216 | /* seek past the data entry */ | ||
217 | if (!S_ISLNK(file_entry->mode) && file_entry->link_name && file_entry->size == 0) { | ||
218 | error_msg("You should extract %s as other files are hardlinked to it", file_entry->name); | ||
219 | } | ||
220 | seek_sub_file(src_stream, file_entry->size); | ||
221 | continue; | ||
222 | } | ||
223 | } | ||
224 | buffer = extract_archive(src_stream, stdout, file_entry, extract_function, prefix); | ||
225 | } | ||
226 | return(buffer); | ||
227 | } | ||
228 | #endif | ||
229 | |||
230 | #if defined BB_AR || defined BB_DPKG_DEB || defined BB_DPKG | ||
231 | void *get_header_ar(FILE *src_stream) | ||
232 | { | ||
233 | file_header_t *typed; | ||
234 | union { | ||
235 | char raw[60]; | ||
236 | struct { | ||
237 | char name[16]; | ||
238 | char date[12]; | ||
239 | char uid[6]; | ||
240 | char gid[6]; | ||
241 | char mode[8]; | ||
242 | char size[10]; | ||
243 | char magic[2]; | ||
244 | } formated; | ||
245 | } ar; | ||
246 | static char *ar_long_names; | ||
247 | |||
248 | if (fread(ar.raw, 1, 60, src_stream) != 60) { | ||
249 | return(NULL); | ||
250 | } | ||
251 | archive_offset += 60; | ||
252 | /* align the headers based on the header magic */ | ||
253 | if ((ar.formated.magic[0] != '`') || (ar.formated.magic[1] != '\n')) { | ||
254 | /* some version of ar, have an extra '\n' after each data entry, | ||
255 | * this puts the next header out by 1 */ | ||
256 | if (ar.formated.magic[1] != '`') { | ||
257 | error_msg("Invalid magic"); | ||
258 | return(NULL); | ||
259 | } | ||
260 | /* read the next char out of what would be the data section, | ||
261 | * if its a '\n' then it is a valid header offset by 1*/ | ||
262 | archive_offset++; | ||
263 | if (fgetc(src_stream) != '\n') { | ||
264 | error_msg("Invalid magic"); | ||
265 | return(NULL); | ||
266 | } | ||
267 | /* fix up the header, we started reading 1 byte too early */ | ||
268 | /* raw_header[60] wont be '\n' as it should, but it doesnt matter */ | ||
269 | memmove(ar.raw, &ar.raw[1], 59); | ||
270 | } | ||
271 | |||
272 | typed = (file_header_t *) xcalloc(1, sizeof(file_header_t)); | ||
273 | |||
274 | typed->size = (size_t) atoi(ar.formated.size); | ||
275 | /* long filenames have '/' as the first character */ | ||
276 | if (ar.formated.name[0] == '/') { | ||
277 | if (ar.formated.name[1] == '/') { | ||
278 | /* If the second char is a '/' then this entries data section | ||
279 | * stores long filename for multiple entries, they are stored | ||
280 | * in static variable long_names for use in future entries */ | ||
281 | ar_long_names = (char *) xrealloc(ar_long_names, typed->size); | ||
282 | fread(ar_long_names, 1, typed->size, src_stream); | ||
283 | archive_offset += typed->size; | ||
284 | /* This ar entries data section only contained filenames for other records | ||
285 | * they are stored in the static ar_long_names for future reference */ | ||
286 | return(NULL); | ||
287 | } else { | ||
288 | /* The number after the '/' indicates the offset in the ar data section | ||
289 | (saved in variable long_name) that conatains the real filename */ | ||
290 | if (!ar_long_names) { | ||
291 | error_msg("Cannot resolve long file name"); | ||
292 | return (NULL); | ||
293 | } | ||
294 | typed->name = xstrdup(ar_long_names + atoi(&ar.formated.name[1])); | ||
295 | } | ||
296 | } else { | ||
297 | /* short filenames */ | ||
298 | typed->name = xcalloc(1, 16); | ||
299 | strncpy(typed->name, ar.formated.name, 16); | ||
300 | } | ||
301 | typed->name[strcspn(typed->name, " /")]='\0'; | ||
302 | |||
303 | /* convert the rest of the now valid char header to its typed struct */ | ||
304 | parse_mode(ar.formated.mode, &typed->mode); | ||
305 | typed->mtime = atoi(ar.formated.date); | ||
306 | typed->uid = atoi(ar.formated.uid); | ||
307 | typed->gid = atoi(ar.formated.gid); | ||
308 | |||
309 | return(typed); | ||
310 | } | ||
311 | #endif | ||
312 | |||
313 | #if defined BB_CPIO | ||
314 | void *get_header_cpio(FILE *src_stream) | ||
315 | { | ||
316 | file_header_t *cpio_entry = NULL; | ||
317 | char cpio_header[110]; | ||
318 | char dummy[14]; | ||
319 | int namesize; | ||
320 | int major, minor, nlink; | ||
321 | |||
322 | /* There can be padding before archive header */ | ||
323 | seek_sub_file(src_stream, (4 - (archive_offset % 4)) % 4); | ||
324 | if (fread(cpio_header, 1, 110, src_stream) == 110) { | ||
325 | archive_offset += 110; | ||
326 | if (strncmp(cpio_header, "07070", 5) != 0) { | ||
327 | error_msg("Unsupported format or invalid magic"); | ||
328 | return(NULL); | ||
329 | } | ||
330 | switch (cpio_header[5]) { | ||
331 | case '2': /* "crc" header format */ | ||
332 | /* Doesnt do the crc check yet */ | ||
333 | case '1': /* "newc" header format */ | ||
334 | cpio_entry = (file_header_t *) xcalloc(1, sizeof(file_header_t)); | ||
335 | sscanf(cpio_header, "%14c%8x%8x%8x%8x%8lx%8lx%16c%8x%8x%8x%8c", | ||
336 | dummy, &cpio_entry->mode, &cpio_entry->uid, &cpio_entry->gid, | ||
337 | &nlink, &cpio_entry->mtime, &cpio_entry->size, | ||
338 | dummy, &major, &minor, &namesize, dummy); | ||
339 | |||
340 | cpio_entry->name = (char *) xcalloc(1, namesize); | ||
341 | fread(cpio_entry->name, 1, namesize, src_stream); /* Read in filename */ | ||
342 | archive_offset += namesize; | ||
343 | /* Skip padding before file contents */ | ||
344 | seek_sub_file(src_stream, (4 - (archive_offset % 4)) % 4); | ||
345 | if (strcmp(cpio_entry->name, "TRAILER!!!") == 0) { | ||
346 | printf("%d blocks\n", (int) (archive_offset % 512 ? (archive_offset / 512) + 1 : archive_offset / 512)); /* Always round up */ | ||
347 | return(NULL); | ||
348 | } | ||
349 | |||
350 | if (S_ISLNK(cpio_entry->mode)) { | ||
351 | cpio_entry->link_name = (char *) xcalloc(1, cpio_entry->size + 1); | ||
352 | fread(cpio_entry->link_name, 1, cpio_entry->size, src_stream); | ||
353 | archive_offset += cpio_entry->size; | ||
354 | } | ||
355 | if (nlink > 1 && !S_ISDIR(cpio_entry->mode) && cpio_entry->size == 0) { | ||
356 | error_msg("%s not extracted: Cannot handle hard links yet", cpio_entry->name); | ||
357 | return(get_header_cpio(src_stream)); /* Recurse to next file */ | ||
358 | } | ||
359 | cpio_entry->device = (major << 8) | minor; | ||
360 | break; | ||
361 | default: | ||
362 | error_msg("Unsupported format"); | ||
363 | return(NULL); | ||
364 | } | ||
365 | if (ferror(src_stream) || feof(src_stream)) { | ||
366 | perror_msg("Stream error"); | ||
367 | return(NULL); | ||
368 | } | ||
369 | } | ||
370 | return(cpio_entry); | ||
371 | } | ||
372 | #endif | ||
373 | |||
374 | #if defined BB_UNTAR || defined BB_DPKG_DEB || defined BB_DPKG | ||
375 | void *get_header_tar(FILE *tar_stream) | ||
376 | { | ||
377 | union { | ||
378 | unsigned char raw[512]; | ||
379 | struct { | ||
380 | char name[100]; /* 0-99 */ | ||
381 | char mode[8]; /* 100-107 */ | ||
382 | char uid[8]; /* 108-115 */ | ||
383 | char gid[8]; /* 116-123 */ | ||
384 | char size[12]; /* 124-135 */ | ||
385 | char mtime[12]; /* 136-147 */ | ||
386 | char chksum[8]; /* 148-155 */ | ||
387 | char typeflag; /* 156-156 */ | ||
388 | char linkname[100]; /* 157-256 */ | ||
389 | char magic[6]; /* 257-262 */ | ||
390 | char version[2]; /* 263-264 */ | ||
391 | char uname[32]; /* 265-296 */ | ||
392 | char gname[32]; /* 297-328 */ | ||
393 | char devmajor[8]; /* 329-336 */ | ||
394 | char devminor[8]; /* 337-344 */ | ||
395 | char prefix[155]; /* 345-499 */ | ||
396 | char padding[12]; /* 500-512 */ | ||
397 | } formated; | ||
398 | } tar; | ||
399 | file_header_t *tar_entry = NULL; | ||
400 | long i; | ||
401 | long sum = 0; | ||
402 | |||
403 | if (archive_offset % 512 != 0) { | ||
404 | seek_sub_file(tar_stream, 512 - (archive_offset % 512)); | ||
405 | } | ||
406 | |||
407 | if (fread(tar.raw, 1, 512, tar_stream) != 512) { | ||
408 | error_msg("Couldnt read header"); | ||
409 | return(NULL); | ||
410 | } | ||
411 | archive_offset += 512; | ||
412 | |||
413 | /* Check header has valid magic, unfortunately some tar files | ||
414 | * have empty (0'ed) tar entries at the end, which will | ||
415 | * cause this to fail, so fail silently for now | ||
416 | */ | ||
417 | if (strncmp(tar.formated.magic, "ustar", 5) != 0) { | ||
418 | return(NULL); | ||
419 | } | ||
420 | |||
421 | /* Do checksum on headers */ | ||
422 | for (i = 0; i < 148 ; i++) { | ||
423 | sum += tar.raw[i]; | ||
424 | } | ||
425 | sum += ' ' * 8; | ||
426 | for (i = 156; i < 512 ; i++) { | ||
427 | sum += tar.raw[i]; | ||
428 | } | ||
429 | if (sum != strtol(tar.formated.chksum, NULL, 8)) { | ||
430 | error_msg("Invalid tar header checksum"); | ||
431 | return(NULL); | ||
432 | } | ||
433 | |||
434 | /* convert to type'ed variables */ | ||
435 | tar_entry = xcalloc(1, sizeof(file_header_t)); | ||
436 | tar_entry->name = xstrdup(tar.formated.name); | ||
437 | |||
438 | parse_mode(tar.formated.mode, &tar_entry->mode); | ||
439 | tar_entry->uid = strtol(tar.formated.uid, NULL, 8); | ||
440 | tar_entry->gid = strtol(tar.formated.gid, NULL, 8); | ||
441 | tar_entry->size = strtol(tar.formated.size, NULL, 8); | ||
442 | tar_entry->mtime = strtol(tar.formated.mtime, NULL, 8); | ||
443 | tar_entry->link_name = strlen(tar.formated.linkname) ? xstrdup(tar.formated.linkname) : NULL; | ||
444 | tar_entry->device = (strtol(tar.formated.devmajor, NULL, 8) << 8) + | ||
445 | strtol(tar.formated.devminor, NULL, 8); | ||
446 | |||
447 | return(tar_entry); | ||
448 | } | ||
449 | #endif | ||
450 | |||
451 | #if defined BB_DPKG || defined BB_DPKG_DEB | ||
452 | char *deb_extract(const char *package_filename, FILE *out_stream, const int extract_function, | ||
453 | const char *prefix, const char *filename) | ||
454 | { | ||
455 | FILE *deb_stream; | ||
456 | FILE *uncompressed_stream = NULL; | ||
457 | file_header_t *ar_header = NULL; | ||
458 | char *output_buffer = NULL; | ||
459 | char *ared_file = NULL; | ||
460 | char ar_magic[8]; | ||
461 | char **file_list; | ||
462 | int gunzip_pid; | ||
463 | |||
464 | file_list = malloc(sizeof(char *)); | ||
465 | file_list[0] = xstrdup(filename); | ||
466 | file_list[1] = NULL; | ||
467 | |||
468 | if (extract_function & extract_control_tar_gz) { | ||
469 | ared_file = xstrdup("control.tar.gz"); | ||
470 | } | ||
471 | else if (extract_function & extract_data_tar_gz) { | ||
472 | ared_file = xstrdup("data.tar.gz"); | ||
473 | } | ||
474 | |||
475 | /* open the debian package to be worked on */ | ||
476 | deb_stream = wfopen(package_filename, "r"); | ||
477 | |||
478 | /* check ar magic */ | ||
479 | fread(ar_magic, 1, 8, deb_stream); | ||
480 | if (strncmp(ar_magic,"!<arch>",7) != 0) { | ||
481 | error_msg_and_die("invalid magic"); | ||
482 | } | ||
483 | archive_offset = 8; | ||
484 | |||
485 | while ((ar_header = get_header_ar(deb_stream)) != NULL) { | ||
486 | if (strcmp(ared_file, ar_header->name) == 0) { | ||
487 | /* open a stream of decompressed data */ | ||
488 | uncompressed_stream = gz_open(deb_stream, &gunzip_pid); | ||
489 | archive_offset = 0; | ||
490 | output_buffer = unarchive(uncompressed_stream, get_header_tar, extract_function, prefix, file_list); | ||
491 | } | ||
492 | seek_sub_file(deb_stream, ar_header->size); | ||
493 | } | ||
494 | gz_close(gunzip_pid); | ||
495 | fclose(deb_stream); | ||
496 | fclose(uncompressed_stream); | ||
497 | free(ared_file); | ||
498 | return(output_buffer); | ||
499 | } | ||
500 | #endif | ||