aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2001-04-23 13:52:02 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2001-04-23 13:52:02 +0000
commitf5c358bc9832733fcba16ec24b38c67c7b62ab85 (patch)
treef633e0218cb95919b55663bc49c227620bc963ee
parent782ab3ccf85360e86e4559aff51bd1e8ca38b36a (diff)
downloadbusybox-w32-f5c358bc9832733fcba16ec24b38c67c7b62ab85.tar.gz
busybox-w32-f5c358bc9832733fcba16ec24b38c67c7b62ab85.tar.bz2
busybox-w32-f5c358bc9832733fcba16ec24b38c67c7b62ab85.zip
Simplify file_prefix handling, and use of *dir variable.
-rw-r--r--libbb/untar.c50
1 files changed, 21 insertions, 29 deletions
diff --git a/libbb/untar.c b/libbb/untar.c
index 70f7eb6a6..b6b44967f 100644
--- a/libbb/untar.c
+++ b/libbb/untar.c
@@ -22,6 +22,12 @@
22#include <unistd.h> 22#include <unistd.h>
23#include "libbb.h" 23#include "libbb.h"
24 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
25extern char *untar(FILE *src_tar_file, FILE *output, const int untar_function, 31extern char *untar(FILE *src_tar_file, FILE *output, const int untar_function,
26 const char *argument, const char *file_prefix) 32 const char *argument, const char *file_prefix)
27{ 33{
@@ -55,7 +61,6 @@ extern char *untar(FILE *src_tar_file, FILE *output, const int untar_function,
55 61
56 while (fread((char *) &raw_tar_header, 1, 512, src_tar_file) == 512) { 62 while (fread((char *) &raw_tar_header, 1, 512, src_tar_file) == 512) {
57 long sum = 0; 63 long sum = 0;
58 char *dir = NULL;
59 64
60 if (ferror(src_tar_file) || feof(src_tar_file)) { 65 if (ferror(src_tar_file) || feof(src_tar_file)) {
61 perror_msg("untar: "); 66 perror_msg("untar: ");
@@ -122,19 +127,14 @@ extern char *untar(FILE *src_tar_file, FILE *output, const int untar_function,
122 fprintf(output, "%s\n", raw_tar_header.name); 127 fprintf(output, "%s\n", raw_tar_header.name);
123 } 128 }
124 129
125 /* extract files */
126 if (untar_function & (extract_extract | extract_verbose_extract | extract_control)) {
127 dir = concat_path_file(argument, raw_tar_header.name);
128 create_path(dir, 0777);
129 }
130
131 switch (raw_tar_header.typeflag ) { 130 switch (raw_tar_header.typeflag ) {
132 case '0': 131 case '0':
133 case '\0': 132 case '\0': {
134 /* If the name ends in a '/' then assume it is 133 /* If the name ends in a '/' then assume it is
135 * supposed to be a directory, and fall through 134 * supposed to be a directory, and fall through
136 */ 135 */
137 if (raw_tar_header.name[strlen(raw_tar_header.name)-1] != '/') { 136 int name_length = strlen(raw_tar_header.name);
137 if (raw_tar_header.name[name_length - 1] != '/') {
138 switch (untar_function) { 138 switch (untar_function) {
139 case (extract_extract): 139 case (extract_extract):
140 case (extract_verbose_extract): 140 case (extract_verbose_extract):
@@ -143,25 +143,17 @@ extern char *untar(FILE *src_tar_file, FILE *output, const int untar_function,
143 char *full_name; 143 char *full_name;
144 144
145 if (file_prefix != NULL) { 145 if (file_prefix != NULL) {
146 char *file_name = NULL, *file_extension = NULL; 146 full_name = xmalloc(strlen(argument) + strlen(file_prefix) + name_length + 3);
147 147 sprintf(full_name, "%s/%s.%s", argument, file_prefix, strrchr(raw_tar_header.name, '/') + 1);
148 file_extension = xmalloc(strlen(raw_tar_header.name) + 1);
149 file_extension = strrchr(raw_tar_header.name, '/');
150 file_extension++;
151 file_name = xmalloc(strlen(file_prefix) + strlen(file_extension) + 2);
152 strcpy(file_name, file_prefix);
153 strcat(file_name, ".");
154 strcat(file_name, file_extension);
155
156 full_name = concat_path_file(xstrndup(dir, strlen(dir) - strlen(strrchr(dir, '/'))), file_name);
157 } else { 148 } else {
158 full_name = xstrdup(dir); 149 full_name = concat_path_file(argument, raw_tar_header.name);
159 } 150 }
160 dst_file = wfopen(full_name, "w"); 151 dst_file = wfopen(full_name, "w");
161 copy_file_chunk(src_tar_file, dst_file, (unsigned long long) size); 152 copy_file_chunk(src_tar_file, dst_file, (unsigned long long) size);
162 uncompressed_count += size; 153 uncompressed_count += size;
163 fclose(dst_file); 154 fclose(dst_file);
164 chmod(full_name, mode); 155 chmod(full_name, mode);
156 free(full_name);
165 } 157 }
166 break; 158 break;
167 case (extract_info): 159 case (extract_info):
@@ -178,19 +170,24 @@ extern char *untar(FILE *src_tar_file, FILE *output, const int untar_function,
178 } 170 }
179 break; 171 break;
180 } 172 }
173 }
181 case '5': 174 case '5':
182 if (untar_function & (extract_extract | extract_verbose_extract | extract_control)) { 175 if (untar_function & (extract_extract | extract_verbose_extract | extract_control)) {
183 if (create_path(dir, mode) != TRUE) { 176 int ret;
184 free(dir); 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) {
185 perror_msg("%s: Cannot mkdir", raw_tar_header.name); 182 perror_msg("%s: Cannot mkdir", raw_tar_header.name);
186 return NULL; 183 return NULL;
187 } 184 }
185 chmod(dir, mode);
188 } 186 }
189 break; 187 break;
190 case '1': 188 case '1':
191 if (untar_function & (extract_extract | extract_verbose_extract | extract_control)) { 189 if (untar_function & (extract_extract | extract_verbose_extract | extract_control)) {
192 if (link(raw_tar_header.linkname, raw_tar_header.name) < 0) { 190 if (link(raw_tar_header.linkname, raw_tar_header.name) < 0) {
193 free(dir);
194 perror_msg("%s: Cannot create hard link to '%s'", raw_tar_header.name, raw_tar_header.linkname); 191 perror_msg("%s: Cannot create hard link to '%s'", raw_tar_header.name, raw_tar_header.linkname);
195 return NULL; 192 return NULL;
196 } 193 }
@@ -199,7 +196,6 @@ extern char *untar(FILE *src_tar_file, FILE *output, const int untar_function,
199 case '2': 196 case '2':
200 if (untar_function & (extract_extract | extract_verbose_extract | extract_control)) { 197 if (untar_function & (extract_extract | extract_verbose_extract | extract_control)) {
201 if (symlink(raw_tar_header.linkname, raw_tar_header.name) < 0) { 198 if (symlink(raw_tar_header.linkname, raw_tar_header.name) < 0) {
202 free(dir);
203 perror_msg("%s: Cannot create symlink to '%s'", raw_tar_header.name, raw_tar_header.linkname); 199 perror_msg("%s: Cannot create symlink to '%s'", raw_tar_header.name, raw_tar_header.linkname);
204 return NULL; 200 return NULL;
205 } 201 }
@@ -213,10 +209,8 @@ extern char *untar(FILE *src_tar_file, FILE *output, const int untar_function,
213// break; 209// break;
214 default: 210 default:
215 error_msg("Unknown file type '%c' in tar file", raw_tar_header.typeflag); 211 error_msg("Unknown file type '%c' in tar file", raw_tar_header.typeflag);
216 free(dir);
217 return NULL; 212 return NULL;
218 } 213 }
219
220 /* 214 /*
221 * Seek to start of next block, cant use fseek as unzip() does support it 215 * Seek to start of next block, cant use fseek as unzip() does support it
222 */ 216 */
@@ -226,8 +220,6 @@ extern char *untar(FILE *src_tar_file, FILE *output, const int untar_function,
226 } 220 }
227 uncompressed_count++; 221 uncompressed_count++;
228 } 222 }
229
230// free(dir);
231 } 223 }
232 return NULL; 224 return NULL;
233} 225}