diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2001-06-13 07:34:03 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2001-06-13 07:34:03 +0000 |
commit | 17822cd60aaf9333a9895494edcf03a0037de54c (patch) | |
tree | 3bf667fdf5b78df26acfc662955d7e38f3c5c4d5 | |
parent | 9aff9036035fbed074e8e711b96c5c934e668884 (diff) | |
download | busybox-w32-17822cd60aaf9333a9895494edcf03a0037de54c.tar.gz busybox-w32-17822cd60aaf9333a9895494edcf03a0037de54c.tar.bz2 busybox-w32-17822cd60aaf9333a9895494edcf03a0037de54c.zip |
Reorganise unarchive functions, new files, removed some
-rw-r--r-- | libbb/add_from_archive_list.c | 31 | ||||
-rw-r--r-- | libbb/append_archive_list.c | 39 | ||||
-rw-r--r-- | libbb/extract_archive.c | 132 | ||||
-rw-r--r-- | libbb/fgets_str.c | 64 | ||||
-rw-r--r-- | libbb/get_tar_gz_headers.c | 39 | ||||
-rw-r--r-- | libbb/get_tar_headers.c | 111 | ||||
-rw-r--r-- | libbb/read_text_file_to_buffer.c | 38 | ||||
-rw-r--r-- | libbb/untar.c | 225 |
8 files changed, 416 insertions, 263 deletions
diff --git a/libbb/add_from_archive_list.c b/libbb/add_from_archive_list.c new file mode 100644 index 000000000..7e4e07159 --- /dev/null +++ b/libbb/add_from_archive_list.c | |||
@@ -0,0 +1,31 @@ | |||
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 new file mode 100644 index 000000000..c955d08d2 --- /dev/null +++ b/libbb/append_archive_list.c | |||
@@ -0,0 +1,39 @@ | |||
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/extract_archive.c b/libbb/extract_archive.c new file mode 100644 index 000000000..f22cbb619 --- /dev/null +++ b/libbb/extract_archive.c | |||
@@ -0,0 +1,132 @@ | |||
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/fgets_str.c b/libbb/fgets_str.c new file mode 100644 index 000000000..33d8d00cc --- /dev/null +++ b/libbb/fgets_str.c | |||
@@ -0,0 +1,64 @@ | |||
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 | |||
18 | #include <stdio.h> | ||
19 | #include <stdlib.h> | ||
20 | #include <string.h> | ||
21 | |||
22 | /* | ||
23 | * Continue reading from file until the terminating string is encountered. | ||
24 | * Return data as string. | ||
25 | * e.g. fgets_str(file, "\n"); will read till end of file | ||
26 | */ | ||
27 | |||
28 | char *fgets_str(FILE *file, const char *terminating_string) | ||
29 | { | ||
30 | char *linebuf = NULL; | ||
31 | const int term_length = strlen(terminating_string); | ||
32 | int end_string_offset; | ||
33 | int linebufsz = 0; | ||
34 | int idx = 0; | ||
35 | int ch; | ||
36 | |||
37 | while (1) { | ||
38 | ch = fgetc(file); | ||
39 | if (ch == EOF) { | ||
40 | break; | ||
41 | } | ||
42 | |||
43 | /* grow the line buffer as necessary */ | ||
44 | while (idx > linebufsz - 2) { | ||
45 | linebuf = realloc(linebuf, linebufsz += 1000); /* GROWBY */ | ||
46 | } | ||
47 | |||
48 | linebuf[idx] = ch; | ||
49 | idx++; | ||
50 | |||
51 | /* Check for terminating string */ | ||
52 | end_string_offset = idx - term_length; | ||
53 | if ((end_string_offset > 0) && (memcmp(&linebuf[end_string_offset], terminating_string, term_length) == 0)) { | ||
54 | idx -= term_length; | ||
55 | break; | ||
56 | } | ||
57 | } | ||
58 | if (idx == 0) { | ||
59 | return NULL; | ||
60 | } | ||
61 | linebuf[idx] = '\0'; | ||
62 | return(linebuf); | ||
63 | } | ||
64 | |||
diff --git a/libbb/get_tar_gz_headers.c b/libbb/get_tar_gz_headers.c new file mode 100644 index 000000000..fb9b7b63f --- /dev/null +++ b/libbb/get_tar_gz_headers.c | |||
@@ -0,0 +1,39 @@ | |||
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 new file mode 100644 index 000000000..5ddf4d9c3 --- /dev/null +++ b/libbb/get_tar_headers.c | |||
@@ -0,0 +1,111 @@ | |||
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/read_text_file_to_buffer.c b/libbb/read_text_file_to_buffer.c deleted file mode 100644 index ef64ad712..000000000 --- a/libbb/read_text_file_to_buffer.c +++ /dev/null | |||
@@ -1,38 +0,0 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <stdlib.h> | ||
3 | #include <string.h> | ||
4 | #include "libbb.h" | ||
5 | |||
6 | /* | ||
7 | * Reads consecutive lines from file line that start with end_string | ||
8 | * read finishes at an empty line or eof | ||
9 | */ | ||
10 | extern char *read_text_file_to_buffer(FILE *src_file) | ||
11 | { | ||
12 | char *line = NULL; | ||
13 | char *buffer = NULL; | ||
14 | int buffer_length = 0; | ||
15 | int line_length = 0; | ||
16 | |||
17 | buffer = xmalloc(1); | ||
18 | strcpy(buffer, ""); | ||
19 | |||
20 | /* Loop until line is empty, or just one char, which will be '\n' */ | ||
21 | do { | ||
22 | line = get_line_from_file(src_file); | ||
23 | if (line == NULL) { | ||
24 | break; | ||
25 | } | ||
26 | line_length = strlen(line); | ||
27 | buffer_length += line_length + 1; | ||
28 | buffer = (char *) xrealloc(buffer, buffer_length + 1); | ||
29 | strcat(buffer, line); | ||
30 | free(line); | ||
31 | } while (line_length > 1); | ||
32 | |||
33 | if (strlen(buffer) == 0) { | ||
34 | return(NULL); | ||
35 | } else { | ||
36 | return(strdup(buffer)); | ||
37 | } | ||
38 | } \ No newline at end of file | ||
diff --git a/libbb/untar.c b/libbb/untar.c deleted file mode 100644 index b6b44967f..000000000 --- a/libbb/untar.c +++ /dev/null | |||
@@ -1,225 +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 | * NOTE: This is used by deb_extract to avoid calling the whole tar applet. | ||
17 | * Its functionality should be merged with tar to avoid duplication | ||
18 | */ | ||
19 | |||
20 | #include <stdlib.h> | ||
21 | #include <string.h> | ||
22 | #include <unistd.h> | ||
23 | #include "libbb.h" | ||
24 | |||
25 | /* | ||
26 | * stdout can be redircted to FILE *output | ||
27 | * If const char *file_prefix isnt NULL is prepended to all the extracted filenames. | ||
28 | * The purpose of const char *argument depends on the value of const int untar_function | ||
29 | */ | ||
30 | |||
31 | extern char *untar(FILE *src_tar_file, FILE *output, const int untar_function, | ||
32 | const char *argument, const char *file_prefix) | ||
33 | { | ||
34 | typedef struct raw_tar_header { | ||
35 | char name[100]; /* 0-99 */ | ||
36 | char mode[8]; /* 100-107 */ | ||
37 | char uid[8]; /* 108-115 */ | ||
38 | char gid[8]; /* 116-123 */ | ||
39 | char size[12]; /* 124-135 */ | ||
40 | char mtime[12]; /* 136-147 */ | ||
41 | char chksum[8]; /* 148-155 */ | ||
42 | char typeflag; /* 156-156 */ | ||
43 | char linkname[100]; /* 157-256 */ | ||
44 | char magic[6]; /* 257-262 */ | ||
45 | char version[2]; /* 263-264 */ | ||
46 | char uname[32]; /* 265-296 */ | ||
47 | char gname[32]; /* 297-328 */ | ||
48 | char devmajor[8]; /* 329-336 */ | ||
49 | char devminor[8]; /* 337-344 */ | ||
50 | char prefix[155]; /* 345-499 */ | ||
51 | char padding[12]; /* 500-512 */ | ||
52 | } raw_tar_header_t; | ||
53 | |||
54 | raw_tar_header_t raw_tar_header; | ||
55 | unsigned char *temp = (unsigned char *) &raw_tar_header; | ||
56 | long i; | ||
57 | long next_header_offset = 0; | ||
58 | long uncompressed_count = 0; | ||
59 | size_t size; | ||
60 | mode_t mode; | ||
61 | |||
62 | while (fread((char *) &raw_tar_header, 1, 512, src_tar_file) == 512) { | ||
63 | long sum = 0; | ||
64 | |||
65 | if (ferror(src_tar_file) || feof(src_tar_file)) { | ||
66 | perror_msg("untar: "); | ||
67 | break; | ||
68 | } | ||
69 | |||
70 | uncompressed_count += 512; | ||
71 | |||
72 | /* Check header has valid magic */ | ||
73 | if (strncmp(raw_tar_header.magic, "ustar", 5) != 0) { | ||
74 | /* | ||
75 | * FIXME, Need HELP with this | ||
76 | * | ||
77 | * This has to fail silently or it incorrectly reports errors when called from | ||
78 | * deb_extract. | ||
79 | * The problem is deb_extract decompresses the .gz file in a child process and | ||
80 | * untar reads from the child proccess. The child process finishes and exits, | ||
81 | * but fread reads 0's from the src_tar_file even though the child | ||
82 | * closed its handle. | ||
83 | */ | ||
84 | // error_msg("Invalid tar magic"); | ||
85 | break; | ||
86 | } | ||
87 | |||
88 | /* Do checksum on headers */ | ||
89 | for (i = 0; i < 148 ; i++) { | ||
90 | sum += temp[i]; | ||
91 | } | ||
92 | sum += ' ' * 8; | ||
93 | for (i = 156; i < 512 ; i++) { | ||
94 | sum += temp[i]; | ||
95 | } | ||
96 | if (sum != strtol(raw_tar_header.chksum, NULL, 8)) { | ||
97 | error_msg("Invalid tar header checksum"); | ||
98 | break; | ||
99 | } | ||
100 | |||
101 | /* convert to type'ed variables */ | ||
102 | size = strtol(raw_tar_header.size, NULL, 8); | ||
103 | parse_mode(raw_tar_header.mode, &mode); | ||
104 | |||
105 | /* start of next header is at */ | ||
106 | next_header_offset = uncompressed_count + size; | ||
107 | if (size % 512 != 0) { | ||
108 | next_header_offset += (512 - size % 512); | ||
109 | } | ||
110 | |||
111 | /* If an exclude list is specified check current file against list */ | ||
112 | #if 0 | ||
113 | if (*exclude_list != NULL) { | ||
114 | i = 0; | ||
115 | while (exclude_list[i] != 0) { | ||
116 | if (strncmp(exclude_list[i], raw_tar_header.name, strlen(raw_tar_header.name)) == 0) { | ||
117 | break; | ||
118 | } | ||
119 | i++; | ||
120 | } | ||
121 | if (exclude_list[i] != 0) { | ||
122 | continue; | ||
123 | } | ||
124 | } | ||
125 | #endif | ||
126 | if (untar_function & (extract_contents | extract_verbose_extract | extract_contents_to_file)) { | ||
127 | fprintf(output, "%s\n", raw_tar_header.name); | ||
128 | } | ||
129 | |||
130 | switch (raw_tar_header.typeflag ) { | ||
131 | case '0': | ||
132 | case '\0': { | ||
133 | /* If the name ends in a '/' then assume it is | ||
134 | * supposed to be a directory, and fall through | ||
135 | */ | ||
136 | int name_length = strlen(raw_tar_header.name); | ||
137 | if (raw_tar_header.name[name_length - 1] != '/') { | ||
138 | switch (untar_function) { | ||
139 | case (extract_extract): | ||
140 | case (extract_verbose_extract): | ||
141 | case (extract_control): { | ||
142 | FILE *dst_file = NULL; | ||
143 | char *full_name; | ||
144 | |||
145 | if (file_prefix != NULL) { | ||
146 | full_name = xmalloc(strlen(argument) + strlen(file_prefix) + name_length + 3); | ||
147 | sprintf(full_name, "%s/%s.%s", argument, file_prefix, strrchr(raw_tar_header.name, '/') + 1); | ||
148 | } else { | ||
149 | full_name = concat_path_file(argument, raw_tar_header.name); | ||
150 | } | ||
151 | dst_file = wfopen(full_name, "w"); | ||
152 | copy_file_chunk(src_tar_file, dst_file, (unsigned long long) size); | ||
153 | uncompressed_count += size; | ||
154 | fclose(dst_file); | ||
155 | chmod(full_name, mode); | ||
156 | free(full_name); | ||
157 | } | ||
158 | break; | ||
159 | case (extract_info): | ||
160 | if (strstr(raw_tar_header.name, argument) != NULL) { | ||
161 | copy_file_chunk(src_tar_file, stdout, (unsigned long long) size); | ||
162 | uncompressed_count += size; | ||
163 | } | ||
164 | break; | ||
165 | case (extract_field): | ||
166 | if (strstr(raw_tar_header.name, "./control") != NULL) { | ||
167 | return(read_text_file_to_buffer(src_tar_file)); | ||
168 | } | ||
169 | break; | ||
170 | } | ||
171 | break; | ||
172 | } | ||
173 | } | ||
174 | case '5': | ||
175 | if (untar_function & (extract_extract | extract_verbose_extract | extract_control)) { | ||
176 | int ret; | ||
177 | char *dir; | ||
178 | dir = concat_path_file(argument, raw_tar_header.name); | ||
179 | ret = create_path(dir, mode); | ||
180 | free(dir); | ||
181 | if (ret == FALSE) { | ||
182 | perror_msg("%s: Cannot mkdir", raw_tar_header.name); | ||
183 | return NULL; | ||
184 | } | ||
185 | chmod(dir, mode); | ||
186 | } | ||
187 | break; | ||
188 | case '1': | ||
189 | if (untar_function & (extract_extract | extract_verbose_extract | extract_control)) { | ||
190 | if (link(raw_tar_header.linkname, raw_tar_header.name) < 0) { | ||
191 | perror_msg("%s: Cannot create hard link to '%s'", raw_tar_header.name, raw_tar_header.linkname); | ||
192 | return NULL; | ||
193 | } | ||
194 | } | ||
195 | break; | ||
196 | case '2': | ||
197 | if (untar_function & (extract_extract | extract_verbose_extract | extract_control)) { | ||
198 | if (symlink(raw_tar_header.linkname, raw_tar_header.name) < 0) { | ||
199 | perror_msg("%s: Cannot create symlink to '%s'", raw_tar_header.name, raw_tar_header.linkname); | ||
200 | return NULL; | ||
201 | } | ||
202 | } | ||
203 | break; | ||
204 | case '3': | ||
205 | case '4': | ||
206 | case '6': | ||
207 | // if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE) | ||
208 | // errorFlag=TRUE; | ||
209 | // break; | ||
210 | default: | ||
211 | error_msg("Unknown file type '%c' in tar file", raw_tar_header.typeflag); | ||
212 | return NULL; | ||
213 | } | ||
214 | /* | ||
215 | * Seek to start of next block, cant use fseek as unzip() does support it | ||
216 | */ | ||
217 | while (uncompressed_count < next_header_offset) { | ||
218 | if (fgetc(src_tar_file) == EOF) { | ||
219 | break; | ||
220 | } | ||
221 | uncompressed_count++; | ||
222 | } | ||
223 | } | ||
224 | return NULL; | ||
225 | } | ||