diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2001-10-25 14:53:50 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2001-10-25 14:53:50 +0000 |
commit | 633f153808c3ec316f023cdb72b98c4e5a929bca (patch) | |
tree | be37d276ee997ec744c43796667cf7c1da3f1c5f | |
parent | ef0eab514d87e162f618889209b8520d2a30c73b (diff) | |
download | busybox-w32-633f153808c3ec316f023cdb72b98c4e5a929bca.tar.gz busybox-w32-633f153808c3ec316f023cdb72b98c4e5a929bca.tar.bz2 busybox-w32-633f153808c3ec316f023cdb72b98c4e5a929bca.zip |
Move libbb/unarchive functions to ./archival/libunarchive
-rw-r--r-- | libbb/unarchive.c | 627 |
1 files changed, 0 insertions, 627 deletions
diff --git a/libbb/unarchive.c b/libbb/unarchive.c deleted file mode 100644 index 9c599a415..000000000 --- a/libbb/unarchive.c +++ /dev/null | |||
@@ -1,627 +0,0 @@ | |||
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 <fnmatch.h> | ||
25 | #include <stdlib.h> | ||
26 | #include <string.h> | ||
27 | #include <unistd.h> | ||
28 | #include <utime.h> | ||
29 | #include "libbb.h" | ||
30 | |||
31 | extern void seek_sub_file(FILE *src_stream, const int count); | ||
32 | extern char *extract_archive(FILE *src_stream, FILE *out_stream, const file_header_t *file_entry, | ||
33 | const int function, const char *prefix); | ||
34 | |||
35 | |||
36 | #ifdef L_archive_offset | ||
37 | off_t archive_offset; | ||
38 | #else | ||
39 | extern off_t archive_offset; | ||
40 | #endif | ||
41 | |||
42 | #ifdef L_seek_sub_file | ||
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 | #endif | ||
56 | |||
57 | |||
58 | |||
59 | #ifdef L_extract_archive | ||
60 | /* Extract the data postioned at src_stream to either filesystem, stdout or | ||
61 | * buffer depending on the value of 'function' which is defined in libbb.h | ||
62 | * | ||
63 | * prefix doesnt have to be just a directory, it may prefix the filename as well. | ||
64 | * | ||
65 | * e.g. '/var/lib/dpkg/info/dpkg.' will extract all files to the base bath | ||
66 | * '/var/lib/dpkg/info/' and all files/dirs created in that dir will have | ||
67 | * 'dpkg.' as their prefix | ||
68 | * | ||
69 | * For this reason if prefix does point to a dir then it must end with a | ||
70 | * trailing '/' or else the last dir will be assumed to be the file prefix | ||
71 | */ | ||
72 | char *extract_archive(FILE *src_stream, FILE *out_stream, const file_header_t *file_entry, | ||
73 | const int function, const char *prefix) | ||
74 | { | ||
75 | FILE *dst_stream = NULL; | ||
76 | char *full_name = NULL; | ||
77 | char *buffer = NULL; | ||
78 | struct utimbuf t; | ||
79 | |||
80 | /* prefix doesnt have to be a proper path it may prepend | ||
81 | * the filename as well */ | ||
82 | if (prefix != NULL) { | ||
83 | /* strip leading '/' in filename to extract as prefix may not be dir */ | ||
84 | /* Cant use concat_path_file here as prefix might not be a directory */ | ||
85 | char *path = file_entry->name; | ||
86 | if (strncmp("./", path, 2) == 0) { | ||
87 | path += 2; | ||
88 | if (strlen(path) == 0) { | ||
89 | return(NULL); | ||
90 | } | ||
91 | } | ||
92 | full_name = xmalloc(strlen(prefix) + strlen(path) + 1); | ||
93 | strcpy(full_name, prefix); | ||
94 | strcat(full_name, path); | ||
95 | } else { | ||
96 | full_name = file_entry->name; | ||
97 | } | ||
98 | if (function & extract_to_stdout) { | ||
99 | if (S_ISREG(file_entry->mode)) { | ||
100 | copy_file_chunk(src_stream, out_stream, file_entry->size); | ||
101 | archive_offset += file_entry->size; | ||
102 | } | ||
103 | } | ||
104 | else if (function & extract_one_to_buffer) { | ||
105 | if (S_ISREG(file_entry->mode)) { | ||
106 | buffer = (char *) xmalloc(file_entry->size + 1); | ||
107 | fread(buffer, 1, file_entry->size, src_stream); | ||
108 | buffer[file_entry->size] = '\0'; | ||
109 | archive_offset += file_entry->size; | ||
110 | return(buffer); | ||
111 | } | ||
112 | } | ||
113 | else if (function & extract_all_to_fs) { | ||
114 | struct stat oldfile; | ||
115 | int stat_res; | ||
116 | stat_res = lstat (full_name, &oldfile); | ||
117 | if (stat_res == 0) { /* The file already exists */ | ||
118 | if ((function & extract_unconditional) || (oldfile.st_mtime < file_entry->mtime)) { | ||
119 | if (!S_ISDIR(oldfile.st_mode)) { | ||
120 | unlink(full_name); /* Directories might not be empty etc */ | ||
121 | } | ||
122 | } else { | ||
123 | if ((function & extract_quiet) != extract_quiet) { | ||
124 | error_msg("%s not created: newer or same age file exists", file_entry->name); | ||
125 | } | ||
126 | seek_sub_file(src_stream, file_entry->size); | ||
127 | return (NULL); | ||
128 | } | ||
129 | } | ||
130 | if (function & extract_create_leading_dirs) { /* Create leading directories with default umask */ | ||
131 | char *buf, *parent; | ||
132 | buf = xstrdup(full_name); | ||
133 | parent = dirname(buf); | ||
134 | if (make_directory (parent, -1, FILEUTILS_RECUR) != 0) { | ||
135 | if ((function & extract_quiet) != extract_quiet) { | ||
136 | error_msg("couldn't create leading directories"); | ||
137 | } | ||
138 | } | ||
139 | free (buf); | ||
140 | } | ||
141 | switch(file_entry->mode & S_IFMT) { | ||
142 | case S_IFREG: | ||
143 | if (file_entry->link_name) { /* Found a cpio hard link */ | ||
144 | if (link(file_entry->link_name, full_name) != 0) { | ||
145 | if ((function & extract_quiet) != extract_quiet) { | ||
146 | perror_msg("Cannot link from %s to '%s'", | ||
147 | file_entry->name, file_entry->link_name); | ||
148 | } | ||
149 | } | ||
150 | } else { | ||
151 | if ((dst_stream = wfopen(full_name, "w")) == NULL) { | ||
152 | seek_sub_file(src_stream, file_entry->size); | ||
153 | return NULL; | ||
154 | } | ||
155 | archive_offset += file_entry->size; | ||
156 | copy_file_chunk(src_stream, dst_stream, file_entry->size); | ||
157 | fclose(dst_stream); | ||
158 | } | ||
159 | break; | ||
160 | case S_IFDIR: | ||
161 | if (stat_res != 0) { | ||
162 | if (mkdir(full_name, file_entry->mode) < 0) { | ||
163 | if ((function & extract_quiet) != extract_quiet) { | ||
164 | perror_msg("extract_archive: %s", full_name); | ||
165 | } | ||
166 | } | ||
167 | } | ||
168 | break; | ||
169 | case S_IFLNK: | ||
170 | if (symlink(file_entry->link_name, full_name) < 0) { | ||
171 | if ((function & extract_quiet) != extract_quiet) { | ||
172 | perror_msg("Cannot create symlink from %s to '%s'", file_entry->name, file_entry->link_name); | ||
173 | } | ||
174 | return NULL; | ||
175 | } | ||
176 | break; | ||
177 | case S_IFSOCK: | ||
178 | case S_IFBLK: | ||
179 | case S_IFCHR: | ||
180 | case S_IFIFO: | ||
181 | if (mknod(full_name, file_entry->mode, file_entry->device) == -1) { | ||
182 | if ((function & extract_quiet) != extract_quiet) { | ||
183 | perror_msg("Cannot create node %s", file_entry->name); | ||
184 | } | ||
185 | return NULL; | ||
186 | } | ||
187 | break; | ||
188 | } | ||
189 | |||
190 | /* Changing a symlink's properties normally changes the properties of the | ||
191 | * file pointed to, so dont try and change the date or mode, lchown does | ||
192 | * does the right thing, but isnt available in older versions of libc */ | ||
193 | if (S_ISLNK(file_entry->mode)) { | ||
194 | #if (__GLIBC__ > 2) && (__GLIBC_MINOR__ > 1) | ||
195 | lchown(full_name, file_entry->uid, file_entry->gid); | ||
196 | #endif | ||
197 | } else { | ||
198 | if (function & extract_preserve_date) { | ||
199 | t.actime = file_entry->mtime; | ||
200 | t.modtime = file_entry->mtime; | ||
201 | utime(full_name, &t); | ||
202 | } | ||
203 | chmod(full_name, file_entry->mode); | ||
204 | chown(full_name, file_entry->uid, file_entry->gid); | ||
205 | } | ||
206 | } else { | ||
207 | /* If we arent extracting data we have to skip it, | ||
208 | * if data size is 0 then then just do it anyway | ||
209 | * (saves testing for it) */ | ||
210 | seek_sub_file(src_stream, file_entry->size); | ||
211 | } | ||
212 | |||
213 | /* extract_list and extract_verbose_list can be used in conjunction | ||
214 | * with one of the above four extraction functions, so do this seperately */ | ||
215 | if (function & extract_verbose_list) { | ||
216 | fprintf(out_stream, "%s %d/%d %8d %s ", mode_string(file_entry->mode), | ||
217 | file_entry->uid, file_entry->gid, | ||
218 | (int) file_entry->size, time_string(file_entry->mtime)); | ||
219 | } | ||
220 | if ((function & extract_list) || (function & extract_verbose_list)){ | ||
221 | /* fputs doesnt add a trailing \n, so use fprintf */ | ||
222 | fprintf(out_stream, "%s\n", full_name); | ||
223 | } | ||
224 | |||
225 | free(full_name); | ||
226 | |||
227 | return(NULL); /* Maybe we should say if failed */ | ||
228 | } | ||
229 | #endif | ||
230 | |||
231 | #ifdef L_unarchive | ||
232 | char *unarchive(FILE *src_stream, FILE *out_stream, file_header_t *(*get_headers)(FILE *), | ||
233 | const int extract_function, const char *prefix, char **include_name, char **exclude_name) | ||
234 | { | ||
235 | file_header_t *file_entry; | ||
236 | int extract_flag = TRUE; | ||
237 | int i; | ||
238 | char *buffer = NULL; | ||
239 | |||
240 | archive_offset = 0; | ||
241 | while ((file_entry = get_headers(src_stream)) != NULL) { | ||
242 | |||
243 | if (include_name != NULL) { | ||
244 | extract_flag = FALSE; | ||
245 | for(i = 0; include_name[i] != 0; i++) { | ||
246 | if (fnmatch(include_name[i], file_entry->name, FNM_LEADING_DIR) == 0) { | ||
247 | extract_flag = TRUE; | ||
248 | break; | ||
249 | } | ||
250 | } | ||
251 | } else { | ||
252 | extract_flag = TRUE; | ||
253 | } | ||
254 | |||
255 | /* If the file entry is in the exclude list dont extract it */ | ||
256 | if (exclude_name != NULL) { | ||
257 | for(i = 0; exclude_name[i] != 0; i++) { | ||
258 | if (fnmatch(exclude_name[i], file_entry->name, FNM_LEADING_DIR) == 0) { | ||
259 | extract_flag = FALSE; | ||
260 | break; | ||
261 | } | ||
262 | } | ||
263 | } | ||
264 | |||
265 | if (extract_flag == TRUE) { | ||
266 | buffer = extract_archive(src_stream, out_stream, file_entry, extract_function, prefix); | ||
267 | } else { | ||
268 | /* seek past the data entry */ | ||
269 | seek_sub_file(src_stream, file_entry->size); | ||
270 | } | ||
271 | free(file_entry->name); /* may be null, but doesn't matter */ | ||
272 | if (file_entry->link_name) { | ||
273 | free(file_entry->link_name); | ||
274 | } | ||
275 | free(file_entry); | ||
276 | } | ||
277 | return(buffer); | ||
278 | } | ||
279 | #endif | ||
280 | |||
281 | #ifdef L_get_header_ar | ||
282 | file_header_t *get_header_ar(FILE *src_stream) | ||
283 | { | ||
284 | file_header_t *typed; | ||
285 | union { | ||
286 | char raw[60]; | ||
287 | struct { | ||
288 | char name[16]; | ||
289 | char date[12]; | ||
290 | char uid[6]; | ||
291 | char gid[6]; | ||
292 | char mode[8]; | ||
293 | char size[10]; | ||
294 | char magic[2]; | ||
295 | } formated; | ||
296 | } ar; | ||
297 | static char *ar_long_names; | ||
298 | |||
299 | if (fread(ar.raw, 1, 60, src_stream) != 60) { | ||
300 | return(NULL); | ||
301 | } | ||
302 | archive_offset += 60; | ||
303 | /* align the headers based on the header magic */ | ||
304 | if ((ar.formated.magic[0] != '`') || (ar.formated.magic[1] != '\n')) { | ||
305 | /* some version of ar, have an extra '\n' after each data entry, | ||
306 | * this puts the next header out by 1 */ | ||
307 | if (ar.formated.magic[1] != '`') { | ||
308 | error_msg("Invalid magic"); | ||
309 | return(NULL); | ||
310 | } | ||
311 | /* read the next char out of what would be the data section, | ||
312 | * if its a '\n' then it is a valid header offset by 1*/ | ||
313 | archive_offset++; | ||
314 | if (fgetc(src_stream) != '\n') { | ||
315 | error_msg("Invalid magic"); | ||
316 | return(NULL); | ||
317 | } | ||
318 | /* fix up the header, we started reading 1 byte too early */ | ||
319 | /* raw_header[60] wont be '\n' as it should, but it doesnt matter */ | ||
320 | memmove(ar.raw, &ar.raw[1], 59); | ||
321 | } | ||
322 | |||
323 | typed = (file_header_t *) xcalloc(1, sizeof(file_header_t)); | ||
324 | |||
325 | typed->size = (size_t) atoi(ar.formated.size); | ||
326 | /* long filenames have '/' as the first character */ | ||
327 | if (ar.formated.name[0] == '/') { | ||
328 | if (ar.formated.name[1] == '/') { | ||
329 | /* If the second char is a '/' then this entries data section | ||
330 | * stores long filename for multiple entries, they are stored | ||
331 | * in static variable long_names for use in future entries */ | ||
332 | ar_long_names = (char *) xrealloc(ar_long_names, typed->size); | ||
333 | fread(ar_long_names, 1, typed->size, src_stream); | ||
334 | archive_offset += typed->size; | ||
335 | /* This ar entries data section only contained filenames for other records | ||
336 | * they are stored in the static ar_long_names for future reference */ | ||
337 | return (get_header_ar(src_stream)); /* Return next header */ | ||
338 | } else if (ar.formated.name[1] == ' ') { | ||
339 | /* This is the index of symbols in the file for compilers */ | ||
340 | seek_sub_file(src_stream, typed->size); | ||
341 | return (get_header_ar(src_stream)); /* Return next header */ | ||
342 | } else { | ||
343 | /* The number after the '/' indicates the offset in the ar data section | ||
344 | (saved in variable long_name) that conatains the real filename */ | ||
345 | if (!ar_long_names) { | ||
346 | error_msg("Cannot resolve long file name"); | ||
347 | return (NULL); | ||
348 | } | ||
349 | typed->name = xstrdup(ar_long_names + atoi(&ar.formated.name[1])); | ||
350 | } | ||
351 | } else { | ||
352 | /* short filenames */ | ||
353 | typed->name = xcalloc(1, 16); | ||
354 | strncpy(typed->name, ar.formated.name, 16); | ||
355 | } | ||
356 | typed->name[strcspn(typed->name, " /")]='\0'; | ||
357 | |||
358 | /* convert the rest of the now valid char header to its typed struct */ | ||
359 | parse_mode(ar.formated.mode, &typed->mode); | ||
360 | typed->mtime = atoi(ar.formated.date); | ||
361 | typed->uid = atoi(ar.formated.uid); | ||
362 | typed->gid = atoi(ar.formated.gid); | ||
363 | |||
364 | return(typed); | ||
365 | } | ||
366 | #endif | ||
367 | |||
368 | #ifdef L_get_header_cpio | ||
369 | struct hardlinks { | ||
370 | file_header_t *entry; | ||
371 | int inode; | ||
372 | struct hardlinks *next; | ||
373 | }; | ||
374 | |||
375 | file_header_t *get_header_cpio(FILE *src_stream) | ||
376 | { | ||
377 | file_header_t *cpio_entry = NULL; | ||
378 | char cpio_header[110]; | ||
379 | int namesize; | ||
380 | char dummy[16]; | ||
381 | int major, minor, nlink, inode; | ||
382 | static struct hardlinks *saved_hardlinks = NULL; | ||
383 | static int pending_hardlinks = 0; | ||
384 | |||
385 | if (pending_hardlinks) { /* Deal with any pending hardlinks */ | ||
386 | struct hardlinks *tmp = saved_hardlinks, *oldtmp = NULL; | ||
387 | while (tmp) { | ||
388 | if (tmp->entry->link_name) { /* Found a hardlink ready to be extracted */ | ||
389 | cpio_entry = tmp->entry; | ||
390 | if (oldtmp) oldtmp->next = tmp->next; /* Remove item from linked list */ | ||
391 | else saved_hardlinks = tmp->next; | ||
392 | free(tmp); | ||
393 | return (cpio_entry); | ||
394 | } | ||
395 | oldtmp = tmp; | ||
396 | tmp = tmp->next; | ||
397 | } | ||
398 | pending_hardlinks = 0; /* No more pending hardlinks, read next file entry */ | ||
399 | } | ||
400 | |||
401 | /* There can be padding before archive header */ | ||
402 | seek_sub_file(src_stream, (4 - (archive_offset % 4)) % 4); | ||
403 | if (fread(cpio_header, 1, 110, src_stream) == 110) { | ||
404 | archive_offset += 110; | ||
405 | if (strncmp(cpio_header, "07070", 5) != 0) { | ||
406 | error_msg("Unsupported format or invalid magic"); | ||
407 | return(NULL); | ||
408 | } | ||
409 | switch (cpio_header[5]) { | ||
410 | case '2': /* "crc" header format */ | ||
411 | /* Doesnt do the crc check yet */ | ||
412 | case '1': /* "newc" header format */ | ||
413 | cpio_entry = (file_header_t *) xcalloc(1, sizeof(file_header_t)); | ||
414 | sscanf(cpio_header, "%6c%8x%8x%8x%8x%8x%8lx%8lx%16c%8x%8x%8x%8c", | ||
415 | dummy, &inode, (unsigned int*)&cpio_entry->mode, | ||
416 | (unsigned int*)&cpio_entry->uid, (unsigned int*)&cpio_entry->gid, | ||
417 | &nlink, &cpio_entry->mtime, &cpio_entry->size, | ||
418 | dummy, &major, &minor, &namesize, dummy); | ||
419 | |||
420 | cpio_entry->name = (char *) xcalloc(1, namesize); | ||
421 | fread(cpio_entry->name, 1, namesize, src_stream); /* Read in filename */ | ||
422 | archive_offset += namesize; | ||
423 | /* Skip padding before file contents */ | ||
424 | seek_sub_file(src_stream, (4 - (archive_offset % 4)) % 4); | ||
425 | if (strcmp(cpio_entry->name, "TRAILER!!!") == 0) { | ||
426 | printf("%d blocks\n", (int) (archive_offset % 512 ? (archive_offset / 512) + 1 : archive_offset / 512)); /* Always round up */ | ||
427 | if (saved_hardlinks) { /* Bummer - we still have unresolved hardlinks */ | ||
428 | struct hardlinks *tmp = saved_hardlinks, *oldtmp = NULL; | ||
429 | while (tmp) { | ||
430 | error_msg("%s not created: cannot resolve hardlink", tmp->entry->name); | ||
431 | oldtmp = tmp; | ||
432 | tmp = tmp->next; | ||
433 | free (oldtmp->entry->name); | ||
434 | free (oldtmp->entry); | ||
435 | free (oldtmp); | ||
436 | } | ||
437 | saved_hardlinks = NULL; | ||
438 | pending_hardlinks = 0; | ||
439 | } | ||
440 | return(NULL); | ||
441 | } | ||
442 | |||
443 | if (S_ISLNK(cpio_entry->mode)) { | ||
444 | cpio_entry->link_name = (char *) xcalloc(1, cpio_entry->size + 1); | ||
445 | fread(cpio_entry->link_name, 1, cpio_entry->size, src_stream); | ||
446 | archive_offset += cpio_entry->size; | ||
447 | cpio_entry->size = 0; /* Stop possiable seeks in future */ | ||
448 | } | ||
449 | if (nlink > 1 && !S_ISDIR(cpio_entry->mode)) { | ||
450 | if (cpio_entry->size == 0) { /* Put file on a linked list for later */ | ||
451 | struct hardlinks *new = xmalloc(sizeof(struct hardlinks)); | ||
452 | new->next = saved_hardlinks; | ||
453 | new->inode = inode; | ||
454 | new->entry = cpio_entry; | ||
455 | saved_hardlinks = new; | ||
456 | return(get_header_cpio(src_stream)); /* Recurse to next file */ | ||
457 | } else { /* Found the file with data in */ | ||
458 | struct hardlinks *tmp = saved_hardlinks; | ||
459 | pending_hardlinks = 1; | ||
460 | while (tmp) { | ||
461 | if (tmp->inode == inode) { | ||
462 | tmp->entry->link_name = xstrdup(cpio_entry->name); | ||
463 | nlink--; | ||
464 | } | ||
465 | tmp = tmp->next; | ||
466 | } | ||
467 | if (nlink > 1) error_msg("error resolving hardlink: did you create the archive with GNU cpio 2.0-2.2?"); | ||
468 | } | ||
469 | } | ||
470 | cpio_entry->device = (major << 8) | minor; | ||
471 | break; | ||
472 | default: | ||
473 | error_msg("Unsupported format"); | ||
474 | return(NULL); | ||
475 | } | ||
476 | if (ferror(src_stream) || feof(src_stream)) { | ||
477 | perror_msg("Stream error"); | ||
478 | return(NULL); | ||
479 | } | ||
480 | } | ||
481 | return(cpio_entry); | ||
482 | } | ||
483 | #endif | ||
484 | |||
485 | #ifdef L_get_header_tar | ||
486 | file_header_t *get_header_tar(FILE *tar_stream) | ||
487 | { | ||
488 | union { | ||
489 | unsigned char raw[512]; | ||
490 | struct { | ||
491 | char name[100]; /* 0-99 */ | ||
492 | char mode[8]; /* 100-107 */ | ||
493 | char uid[8]; /* 108-115 */ | ||
494 | char gid[8]; /* 116-123 */ | ||
495 | char size[12]; /* 124-135 */ | ||
496 | char mtime[12]; /* 136-147 */ | ||
497 | char chksum[8]; /* 148-155 */ | ||
498 | char typeflag; /* 156-156 */ | ||
499 | char linkname[100]; /* 157-256 */ | ||
500 | char magic[6]; /* 257-262 */ | ||
501 | char version[2]; /* 263-264 */ | ||
502 | char uname[32]; /* 265-296 */ | ||
503 | char gname[32]; /* 297-328 */ | ||
504 | char devmajor[8]; /* 329-336 */ | ||
505 | char devminor[8]; /* 337-344 */ | ||
506 | char prefix[155]; /* 345-499 */ | ||
507 | char padding[12]; /* 500-512 */ | ||
508 | } formated; | ||
509 | } tar; | ||
510 | file_header_t *tar_entry = NULL; | ||
511 | long sum = 0; | ||
512 | long i; | ||
513 | |||
514 | if (archive_offset % 512 != 0) { | ||
515 | seek_sub_file(tar_stream, 512 - (archive_offset % 512)); | ||
516 | } | ||
517 | |||
518 | if (fread(tar.raw, 1, 512, tar_stream) != 512) { | ||
519 | /* Unfortunatly its common for tar files to have all sorts of | ||
520 | * trailing garbage, fail silently */ | ||
521 | // error_msg("Couldnt read header"); | ||
522 | return(NULL); | ||
523 | } | ||
524 | archive_offset += 512; | ||
525 | |||
526 | /* Check header has valid magic, unfortunately some tar files | ||
527 | * have empty (0'ed) tar entries at the end, which will | ||
528 | * cause this to fail, so fail silently for now | ||
529 | */ | ||
530 | if (strncmp(tar.formated.magic, "ustar", 5) != 0) { | ||
531 | return(NULL); | ||
532 | } | ||
533 | |||
534 | /* Do checksum on headers */ | ||
535 | for (i = 0; i < 148 ; i++) { | ||
536 | sum += tar.raw[i]; | ||
537 | } | ||
538 | sum += ' ' * 8; | ||
539 | for (i = 156; i < 512 ; i++) { | ||
540 | sum += tar.raw[i]; | ||
541 | } | ||
542 | if (sum != strtol(tar.formated.chksum, NULL, 8)) { | ||
543 | error_msg("Invalid tar header checksum"); | ||
544 | return(NULL); | ||
545 | } | ||
546 | |||
547 | /* convert to type'ed variables */ | ||
548 | tar_entry = xcalloc(1, sizeof(file_header_t)); | ||
549 | tar_entry->name = xstrdup(tar.formated.name); | ||
550 | |||
551 | parse_mode(tar.formated.mode, &tar_entry->mode); | ||
552 | tar_entry->uid = strtol(tar.formated.uid, NULL, 8); | ||
553 | tar_entry->gid = strtol(tar.formated.gid, NULL, 8); | ||
554 | tar_entry->size = strtol(tar.formated.size, NULL, 8); | ||
555 | tar_entry->mtime = strtol(tar.formated.mtime, NULL, 8); | ||
556 | tar_entry->link_name = strlen(tar.formated.linkname) ? | ||
557 | xstrdup(tar.formated.linkname) : NULL; | ||
558 | tar_entry->device = (strtol(tar.formated.devmajor, NULL, 8) << 8) + | ||
559 | strtol(tar.formated.devminor, NULL, 8); | ||
560 | |||
561 | return(tar_entry); | ||
562 | } | ||
563 | #endif | ||
564 | |||
565 | #ifdef L_deb_extract | ||
566 | char *deb_extract(const char *package_filename, FILE *out_stream, | ||
567 | const int extract_function, const char *prefix, const char *filename) | ||
568 | { | ||
569 | FILE *deb_stream; | ||
570 | FILE *uncompressed_stream = NULL; | ||
571 | file_header_t *ar_header = NULL; | ||
572 | char **file_list = NULL; | ||
573 | char *output_buffer = NULL; | ||
574 | char *ared_file = NULL; | ||
575 | char ar_magic[8]; | ||
576 | int gunzip_pid; | ||
577 | |||
578 | if (filename != NULL) { | ||
579 | file_list = xmalloc(sizeof(char *) * 2); | ||
580 | file_list[0] = xstrdup(filename); | ||
581 | file_list[1] = NULL; | ||
582 | } | ||
583 | |||
584 | if (extract_function & extract_control_tar_gz) { | ||
585 | ared_file = xstrdup("control.tar.gz"); | ||
586 | } | ||
587 | else if (extract_function & extract_data_tar_gz) { | ||
588 | ared_file = xstrdup("data.tar.gz"); | ||
589 | } | ||
590 | |||
591 | /* open the debian package to be worked on */ | ||
592 | deb_stream = wfopen(package_filename, "r"); | ||
593 | if (deb_stream == NULL) { | ||
594 | return(NULL); | ||
595 | } | ||
596 | /* set the buffer size */ | ||
597 | setvbuf(deb_stream, NULL, _IOFBF, 0x8000); | ||
598 | |||
599 | /* check ar magic */ | ||
600 | fread(ar_magic, 1, 8, deb_stream); | ||
601 | if (strncmp(ar_magic,"!<arch>",7) != 0) { | ||
602 | error_msg_and_die("invalid magic"); | ||
603 | } | ||
604 | archive_offset = 8; | ||
605 | |||
606 | while ((ar_header = get_header_ar(deb_stream)) != NULL) { | ||
607 | if (strcmp(ared_file, ar_header->name) == 0) { | ||
608 | /* open a stream of decompressed data */ | ||
609 | uncompressed_stream = gz_open(deb_stream, &gunzip_pid); | ||
610 | archive_offset = 0; | ||
611 | output_buffer = unarchive(uncompressed_stream, out_stream, get_header_tar, extract_function, prefix, file_list, NULL); | ||
612 | } | ||
613 | seek_sub_file(deb_stream, ar_header->size); | ||
614 | free(ar_header->name); | ||
615 | free(ar_header); | ||
616 | } | ||
617 | gz_close(gunzip_pid); | ||
618 | fclose(deb_stream); | ||
619 | fclose(uncompressed_stream); | ||
620 | free(ared_file); | ||
621 | if (filename != NULL) { | ||
622 | free(file_list[0]); | ||
623 | free(file_list); | ||
624 | } | ||
625 | return(output_buffer); | ||
626 | } | ||
627 | #endif | ||