opkg: attached patch fixes build failures with gcc 4.3.2. patch by Enrico Scholz...
[project/opkg-lede.git] / libbb / unarchive.c
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 #define CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY 1
31 #define CONFIG_FEATURE_TAR_GNU_EXTENSIONS
32
33 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
34 static char *longname = NULL;
35 static char *linkname = NULL;
36 #endif
37
38 extern void seek_sub_file(FILE *src_stream, const int count);
39 extern char *extract_archive(FILE *src_stream, FILE *out_stream, const file_header_t *file_entry,
40 const int function, const char *prefix);
41
42
43 #ifdef L_archive_offset
44 off_t archive_offset;
45 #else
46 extern off_t archive_offset;
47 #endif
48
49 #ifdef L_seek_sub_file
50 void seek_sub_file(FILE *src_stream, const int count)
51 {
52 int i;
53 /* Try to fseek as faster */
54 archive_offset += count;
55 if (fseek(src_stream, count, SEEK_CUR) != 0 && errno == ESPIPE) {
56 for (i = 0; i < count; i++) {
57 fgetc(src_stream);
58 }
59 }
60 return;
61 }
62 #endif
63
64
65
66 #ifdef L_extract_archive
67 /* Extract the data postioned at src_stream to either filesystem, stdout or
68 * buffer depending on the value of 'function' which is defined in libbb.h
69 *
70 * prefix doesnt have to be just a directory, it may prefix the filename as well.
71 *
72 * e.g. '/var/lib/dpkg/info/dpkg.' will extract all files to the base bath
73 * '/var/lib/dpkg/info/' and all files/dirs created in that dir will have
74 * 'dpkg.' as their prefix
75 *
76 * For this reason if prefix does point to a dir then it must end with a
77 * trailing '/' or else the last dir will be assumed to be the file prefix
78 */
79 char *extract_archive(FILE *src_stream, FILE *out_stream, const file_header_t *file_entry,
80 const int function, const char *prefix)
81 {
82 FILE *dst_stream = NULL;
83 char *full_name = NULL;
84 char *full_link_name = NULL;
85 char *buffer = NULL;
86 struct utimbuf t;
87
88
89 /* prefix doesnt have to be a proper path it may prepend
90 * the filename as well */
91 if (prefix != NULL) {
92 /* strip leading '/' in filename to extract as prefix may not be dir */
93 /* Cant use concat_path_file here as prefix might not be a directory */
94 char *path = file_entry->name;
95 if (strncmp("./", path, 2) == 0) {
96 path += 2;
97 if (strlen(path) == 0) {
98 return(NULL);
99 }
100 }
101 full_name = xmalloc(strlen(prefix) + strlen(path) + 1);
102 strcpy(full_name, prefix);
103 strcat(full_name, path);
104 if ( file_entry->link_name ){
105 full_link_name = xmalloc(strlen(prefix) + strlen(file_entry->link_name) + 1);
106 strcpy(full_link_name, prefix);
107 strcat(full_link_name, file_entry->link_name);
108 }
109 } else {
110 full_name = strdup(file_entry->name);
111 if ( file_entry->link_name )
112 full_link_name = strdup(file_entry->link_name);
113 }
114
115
116 if (function & extract_to_stdout) {
117 if (S_ISREG(file_entry->mode)) {
118 copy_file_chunk(src_stream, out_stream, file_entry->size);
119 archive_offset += file_entry->size;
120 }
121 }
122 else if (function & extract_one_to_buffer) {
123 if (S_ISREG(file_entry->mode)) {
124 buffer = (char *) xmalloc(file_entry->size + 1);
125 fread(buffer, 1, file_entry->size, src_stream);
126 buffer[file_entry->size] = '\0';
127 archive_offset += file_entry->size;
128 return(buffer);
129 }
130 }
131 else if (function & extract_all_to_fs) {
132 struct stat oldfile;
133 int stat_res;
134 stat_res = lstat (full_name, &oldfile);
135 if (stat_res == 0) { /* The file already exists */
136 if ((function & extract_unconditional) || (oldfile.st_mtime < file_entry->mtime)) {
137 if (!S_ISDIR(oldfile.st_mode)) {
138 unlink(full_name); /* Directories might not be empty etc */
139 }
140 } else {
141 if ((function & extract_quiet) != extract_quiet) {
142 error_msg("%s not created: newer or same age file exists", file_entry->name);
143 }
144 seek_sub_file(src_stream, file_entry->size);
145 return (NULL);
146 }
147 }
148 if (function & extract_create_leading_dirs) { /* Create leading directories with default umask */
149 char *buf, *parent;
150 buf = xstrdup(full_name);
151 parent = dirname(buf);
152 if (make_directory (parent, -1, FILEUTILS_RECUR) != 0) {
153 if ((function & extract_quiet) != extract_quiet) {
154 error_msg("couldn't create leading directories");
155 }
156 }
157 free (buf);
158 }
159 switch(file_entry->mode & S_IFMT) {
160 case S_IFREG:
161 if (file_entry->link_name) { /* Found a cpio hard link */
162 if (link(full_link_name, full_name) != 0) {
163 if ((function & extract_quiet) != extract_quiet) {
164 perror_msg("Cannot link from %s to '%s'",
165 file_entry->name, file_entry->link_name);
166 }
167 }
168 } else {
169 if ((dst_stream = wfopen(full_name, "w")) == NULL) {
170 seek_sub_file(src_stream, file_entry->size);
171 return NULL;
172 }
173 archive_offset += file_entry->size;
174 copy_file_chunk(src_stream, dst_stream, file_entry->size);
175 fclose(dst_stream);
176 }
177 break;
178 case S_IFDIR:
179 if (stat_res != 0) {
180 if (mkdir(full_name, file_entry->mode) < 0) {
181 if ((function & extract_quiet) != extract_quiet) {
182 perror_msg("extract_archive: %s", full_name);
183 }
184 }
185 }
186 break;
187 case S_IFLNK:
188 if (symlink(file_entry->link_name, full_name) < 0) {
189 if ((function & extract_quiet) != extract_quiet) {
190 perror_msg("Cannot create symlink from %s to '%s'", file_entry->name, file_entry->link_name);
191 }
192 return NULL;
193 }
194 break;
195 case S_IFSOCK:
196 case S_IFBLK:
197 case S_IFCHR:
198 case S_IFIFO:
199 if (mknod(full_name, file_entry->mode, file_entry->device) == -1) {
200 if ((function & extract_quiet) != extract_quiet) {
201 perror_msg("Cannot create node %s", file_entry->name);
202 }
203 return NULL;
204 }
205 break;
206 default:
207 perror_msg("Don't know how to handle %s", full_name);
208
209 }
210
211 /* Changing a symlink's properties normally changes the properties of the
212 * file pointed to, so dont try and change the date or mode, lchown does
213 * does the right thing, but isnt available in older versions of libc */
214 if (S_ISLNK(file_entry->mode)) {
215 #if (__GLIBC__ > 2) && (__GLIBC_MINOR__ > 1)
216 lchown(full_name, file_entry->uid, file_entry->gid);
217 #endif
218 } else {
219 if (function & extract_preserve_date) {
220 t.actime = file_entry->mtime;
221 t.modtime = file_entry->mtime;
222 utime(full_name, &t);
223 }
224 chown(full_name, file_entry->uid, file_entry->gid);
225 chmod(full_name, file_entry->mode);
226 }
227 } else {
228 /* If we arent extracting data we have to skip it,
229 * if data size is 0 then then just do it anyway
230 * (saves testing for it) */
231 seek_sub_file(src_stream, file_entry->size);
232 }
233
234 /* extract_list and extract_verbose_list can be used in conjunction
235 * with one of the above four extraction functions, so do this seperately */
236 if (function & extract_verbose_list) {
237 fprintf(out_stream, "%s %d/%d %8d %s ", mode_string(file_entry->mode),
238 file_entry->uid, file_entry->gid,
239 (int) file_entry->size, time_string(file_entry->mtime));
240 }
241 if ((function & extract_list) || (function & extract_verbose_list)){
242 /* fputs doesnt add a trailing \n, so use fprintf */
243 fprintf(out_stream, "%s\n", file_entry->name);
244 }
245
246 free(full_name);
247 if ( full_link_name )
248 free(full_link_name);
249
250 return(NULL); /* Maybe we should say if failed */
251 }
252 #endif
253
254 #ifdef L_unarchive
255 char *unarchive(FILE *src_stream, FILE *out_stream,
256 file_header_t *(*get_headers)(FILE *),
257 void (*free_headers)(file_header_t *),
258 const int extract_function, const char *prefix, char **extract_names)
259 {
260 file_header_t *file_entry;
261 int extract_flag;
262 int i;
263 char *buffer = NULL;
264
265 archive_offset = 0;
266 while ((file_entry = get_headers(src_stream)) != NULL) {
267 extract_flag = TRUE;
268 /* fprintf(stderr, __FUNCTION__ " getting headers\n"); */
269 if (extract_names != NULL) {
270 int found_flag = FALSE;
271 for(i = 0; extract_names[i] != 0; i++) {
272 if (strcmp(extract_names[i], file_entry->name) == 0) {
273 found_flag = TRUE;
274 break;
275 }
276 }
277 if (extract_function & extract_exclude_list) {
278 if (found_flag == TRUE) {
279 extract_flag = FALSE;
280 }
281 } else {
282 /* If its not found in the include list dont extract it */
283 if (found_flag == FALSE) {
284 extract_flag = FALSE;
285 }
286 }
287
288 }
289
290 if (extract_flag == TRUE) {
291 /* fprintf(stderr, __FUNCTION__ " extract?\n"); */
292 buffer = extract_archive(src_stream, out_stream, file_entry, extract_function, prefix);
293 /* fprintf(stderr, __FUNCTION__ " extracted\n"); */
294 } else {
295 /* seek past the data entry */
296 seek_sub_file(src_stream, file_entry->size);
297 }
298 free_headers(file_entry);
299 }
300 /*fprintf(stderr, __FUNCTION__ " goin home\n");*/
301
302 return(buffer);
303 }
304 #endif
305
306 #ifdef L_get_header_ar
307 file_header_t *get_header_ar(FILE *src_stream)
308 {
309 file_header_t *typed;
310 union {
311 char raw[60];
312 struct {
313 char name[16];
314 char date[12];
315 char uid[6];
316 char gid[6];
317 char mode[8];
318 char size[10];
319 char magic[2];
320 } formated;
321 } ar;
322 static char *ar_long_names;
323
324 if (fread(ar.raw, 1, 60, src_stream) != 60) {
325 return(NULL);
326 }
327 archive_offset += 60;
328 /* align the headers based on the header magic */
329 if ((ar.formated.magic[0] != '`') || (ar.formated.magic[1] != '\n')) {
330 /* some version of ar, have an extra '\n' after each data entry,
331 * this puts the next header out by 1 */
332 if (ar.formated.magic[1] != '`') {
333 error_msg("Invalid magic");
334 return(NULL);
335 }
336 /* read the next char out of what would be the data section,
337 * if its a '\n' then it is a valid header offset by 1*/
338 archive_offset++;
339 if (fgetc(src_stream) != '\n') {
340 error_msg("Invalid magic");
341 return(NULL);
342 }
343 /* fix up the header, we started reading 1 byte too early */
344 /* raw_header[60] wont be '\n' as it should, but it doesnt matter */
345 memmove(ar.raw, &ar.raw[1], 59);
346 }
347
348 typed = (file_header_t *) xcalloc(1, sizeof(file_header_t));
349
350 typed->size = (size_t) atoi(ar.formated.size);
351 /* long filenames have '/' as the first character */
352 if (ar.formated.name[0] == '/') {
353 if (ar.formated.name[1] == '/') {
354 /* If the second char is a '/' then this entries data section
355 * stores long filename for multiple entries, they are stored
356 * in static variable long_names for use in future entries */
357 ar_long_names = (char *) xrealloc(ar_long_names, typed->size);
358 fread(ar_long_names, 1, typed->size, src_stream);
359 archive_offset += typed->size;
360 /* This ar entries data section only contained filenames for other records
361 * they are stored in the static ar_long_names for future reference */
362 return (get_header_ar(src_stream)); /* Return next header */
363 } else if (ar.formated.name[1] == ' ') {
364 /* This is the index of symbols in the file for compilers */
365 seek_sub_file(src_stream, typed->size);
366 return (get_header_ar(src_stream)); /* Return next header */
367 } else {
368 /* The number after the '/' indicates the offset in the ar data section
369 (saved in variable long_name) that conatains the real filename */
370 if (!ar_long_names) {
371 error_msg("Cannot resolve long file name");
372 return (NULL);
373 }
374 typed->name = xstrdup(ar_long_names + atoi(&ar.formated.name[1]));
375 }
376 } else {
377 /* short filenames */
378 typed->name = xcalloc(1, 16);
379 strncpy(typed->name, ar.formated.name, 16);
380 }
381 typed->name[strcspn(typed->name, " /")]='\0';
382
383 /* convert the rest of the now valid char header to its typed struct */
384 parse_mode(ar.formated.mode, &typed->mode);
385 typed->mtime = atoi(ar.formated.date);
386 typed->uid = atoi(ar.formated.uid);
387 typed->gid = atoi(ar.formated.gid);
388
389 return(typed);
390 }
391
392 void free_header_ar(file_header_t *ar_entry)
393 {
394 if (ar_entry == NULL) {
395 return;
396 }
397
398 free(ar_entry->name);
399 free(ar_entry->link_name);
400
401 free(ar_entry);
402 }
403 #endif
404
405 #ifdef L_get_header_cpio
406 struct hardlinks {
407 file_header_t *entry;
408 int inode;
409 struct hardlinks *next;
410 };
411
412 file_header_t *get_header_cpio(FILE *src_stream)
413 {
414 file_header_t *cpio_entry = NULL;
415 char cpio_header[110];
416 int namesize;
417 char dummy[16];
418 int major, minor, nlink, inode;
419 static struct hardlinks *saved_hardlinks = NULL;
420 static int pending_hardlinks = 0;
421
422
423 if (pending_hardlinks) { /* Deal with any pending hardlinks */
424 struct hardlinks *tmp = saved_hardlinks, *oldtmp = NULL;
425 while (tmp) {
426 if (tmp->entry->link_name) { /* Found a hardlink ready to be extracted */
427 cpio_entry = tmp->entry;
428 if (oldtmp) oldtmp->next = tmp->next; /* Remove item from linked list */
429 else saved_hardlinks = tmp->next;
430 free(tmp);
431 return (cpio_entry);
432 }
433 oldtmp = tmp;
434 tmp = tmp->next;
435 }
436 pending_hardlinks = 0; /* No more pending hardlinks, read next file entry */
437 }
438
439 /* There can be padding before archive header */
440 seek_sub_file(src_stream, (4 - (archive_offset % 4)) % 4);
441 if (fread(cpio_header, 1, 110, src_stream) == 110) {
442 archive_offset += 110;
443 if (strncmp(cpio_header, "07070", 5) != 0) {
444 error_msg("Unsupported format or invalid magic");
445 return(NULL);
446 }
447 switch (cpio_header[5]) {
448 case '2': /* "crc" header format */
449 /* Doesnt do the crc check yet */
450 case '1': /* "newc" header format */
451 cpio_entry = (file_header_t *) xcalloc(1, sizeof(file_header_t));
452 sscanf(cpio_header, "%6c%8x%8x%8x%8x%8x%8lx%8lx%16c%8x%8x%8x%8c",
453 dummy, &inode, (unsigned int*)&cpio_entry->mode,
454 (unsigned int*)&cpio_entry->uid, (unsigned int*)&cpio_entry->gid,
455 &nlink, &cpio_entry->mtime, (unsigned long*)&cpio_entry->size,
456 dummy, &major, &minor, &namesize, dummy);
457
458 cpio_entry->name = (char *) xcalloc(1, namesize);
459 fread(cpio_entry->name, 1, namesize, src_stream); /* Read in filename */
460 archive_offset += namesize;
461 /* Skip padding before file contents */
462 seek_sub_file(src_stream, (4 - (archive_offset % 4)) % 4);
463 if (strcmp(cpio_entry->name, "TRAILER!!!") == 0) {
464 printf("%d blocks\n", (int) (archive_offset % 512 ? (archive_offset / 512) + 1 : archive_offset / 512)); /* Always round up */
465 if (saved_hardlinks) { /* Bummer - we still have unresolved hardlinks */
466 struct hardlinks *tmp = saved_hardlinks, *oldtmp = NULL;
467 while (tmp) {
468 error_msg("%s not created: cannot resolve hardlink", tmp->entry->name);
469 oldtmp = tmp;
470 tmp = tmp->next;
471 free (oldtmp->entry->name);
472 free (oldtmp->entry);
473 free (oldtmp);
474 }
475 saved_hardlinks = NULL;
476 pending_hardlinks = 0;
477 }
478 return(NULL);
479 }
480
481 if (S_ISLNK(cpio_entry->mode)) {
482 cpio_entry->link_name = (char *) xcalloc(1, cpio_entry->size + 1);
483 fread(cpio_entry->link_name, 1, cpio_entry->size, src_stream);
484 archive_offset += cpio_entry->size;
485 cpio_entry->size = 0; /* Stop possiable seeks in future */
486 }
487 if (nlink > 1 && !S_ISDIR(cpio_entry->mode)) {
488 if (cpio_entry->size == 0) { /* Put file on a linked list for later */
489 struct hardlinks *new = xmalloc(sizeof(struct hardlinks));
490 new->next = saved_hardlinks;
491 new->inode = inode;
492 new->entry = cpio_entry;
493 saved_hardlinks = new;
494 return(get_header_cpio(src_stream)); /* Recurse to next file */
495 } else { /* Found the file with data in */
496 struct hardlinks *tmp = saved_hardlinks;
497 pending_hardlinks = 1;
498 while (tmp) {
499 if (tmp->inode == inode) {
500 tmp->entry->link_name = xstrdup(cpio_entry->name);
501 nlink--;
502 }
503 tmp = tmp->next;
504 }
505 if (nlink > 1) error_msg("error resolving hardlink: did you create the archive with GNU cpio 2.0-2.2?");
506 }
507 }
508 cpio_entry->device = (major << 8) | minor;
509 break;
510 default:
511 error_msg("Unsupported format");
512 return(NULL);
513 }
514 if (ferror(src_stream) || feof(src_stream)) {
515 perror_msg("Stream error");
516 return(NULL);
517 }
518 }
519 return(cpio_entry);
520 }
521 #endif
522
523 #ifdef L_get_header_tar
524 file_header_t *get_header_tar(FILE *tar_stream)
525 {
526
527 union {
528 unsigned char raw[512];
529 struct {
530 char name[100]; /* 0-99 */
531 char mode[8]; /* 100-107 */
532 char uid[8]; /* 108-115 */
533 char gid[8]; /* 116-123 */
534 char size[12]; /* 124-135 */
535 char mtime[12]; /* 136-147 */
536 char chksum[8]; /* 148-155 */
537 char typeflag; /* 156-156 */
538 char linkname[100]; /* 157-256 */
539 char magic[6]; /* 257-262 */
540 char version[2]; /* 263-264 */
541 char uname[32]; /* 265-296 */
542 char gname[32]; /* 297-328 */
543 char devmajor[8]; /* 329-336 */
544 char devminor[8]; /* 337-344 */
545 char prefix[155]; /* 345-499 */
546 char padding[12]; /* 500-512 */
547 } formated;
548 } tar;
549 file_header_t *tar_entry = NULL;
550 long i;
551 long sum = 0;
552
553 if (archive_offset % 512 != 0) {
554 seek_sub_file(tar_stream, 512 - (archive_offset % 512));
555 }
556
557 if (fread(tar.raw, 1, 512, tar_stream) != 512) {
558 /* Unfortunatly its common for tar files to have all sorts of
559 * trailing garbage, fail silently */
560 // error_msg("Couldnt read header");
561 return(NULL);
562 }
563 archive_offset += 512;
564
565 /* Check header has valid magic, unfortunately some tar files
566 * have empty (0'ed) tar entries at the end, which will
567 * cause this to fail, so fail silently for now
568 */
569 if (strncmp(tar.formated.magic, "ustar", 5) != 0) {
570 #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
571 if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0)
572 #endif
573 return(NULL);
574 }
575
576 /* Do checksum on headers */
577 for (i = 0; i < 148 ; i++) {
578 sum += tar.raw[i];
579 }
580 sum += ' ' * 8;
581 for (i = 156; i < 512 ; i++) {
582 sum += tar.raw[i];
583 }
584 if (sum != strtol(tar.formated.chksum, NULL, 8)) {
585 if ( strtol(tar.formated.chksum,NULL,8) != 0 )
586 error_msg("Invalid tar header checksum");
587 return(NULL);
588 }
589
590 /* convert to type'ed variables */
591 tar_entry = xcalloc(1, sizeof(file_header_t));
592
593 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
594 if (longname) {
595 tar_entry->name = longname;
596 longname = NULL;
597 }
598 else if (linkname) {
599 tar_entry->name = linkname;
600 linkname = NULL;
601 } else
602 #endif
603 if (tar.formated.prefix[0] == 0) {
604 tar_entry->name = strdup(tar.formated.name);
605 } else {
606 tar_entry->name = concat_path_file(tar.formated.prefix, tar.formated.name);
607 }
608
609 // tar_entry->name = xstrdup(tar.formated.name);
610
611 /*
612 parse_mode(tar.formated.mode, &tar_entry->mode);
613 */
614 tar_entry->mode = 07777 & strtol(tar.formated.mode, NULL, 8);
615
616 tar_entry->uid = strtol(tar.formated.uid, NULL, 8);
617 tar_entry->gid = strtol(tar.formated.gid, NULL, 8);
618 tar_entry->size = strtol(tar.formated.size, NULL, 8);
619 tar_entry->mtime = strtol(tar.formated.mtime, NULL, 8);
620 tar_entry->link_name = strlen(tar.formated.linkname) ?
621 xstrdup(tar.formated.linkname) : NULL;
622 tar_entry->device = (strtol(tar.formated.devmajor, NULL, 8) << 8) +
623 strtol(tar.formated.devminor, NULL, 8);
624
625 /* Fix mode, used by the old format */
626 switch (tar.formated.typeflag) {
627 /* hard links are detected as regular files with 0 size and a link name */
628 case '1':
629 tar_entry->mode |= S_IFREG ;
630 break;
631 case 0:
632 case '0':
633
634 # ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
635 if (last_char_is(tar_entry->name, '/')) {
636 tar_entry->mode |= S_IFDIR;
637 } else
638 # endif
639 tar_entry->mode |= S_IFREG;
640 break;
641 case '2':
642 tar_entry->mode |= S_IFLNK;
643 break;
644 case '3':
645 tar_entry->mode |= S_IFCHR;
646 break;
647 case '4':
648 tar_entry->mode |= S_IFBLK;
649 break;
650 case '5':
651 tar_entry->mode |= S_IFDIR;
652 break;
653 case '6':
654 tar_entry->mode |= S_IFIFO;
655 break;
656 # ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
657 case 'L': {
658 longname = xmalloc(tar_entry->size + 1);
659 if(fread(longname, tar_entry->size, 1, tar_stream) != 1)
660 return NULL;
661 longname[tar_entry->size] = '\0';
662 archive_offset += tar_entry->size;
663
664 return(get_header_tar(tar_stream));
665 }
666 case 'K': {
667 linkname = xmalloc(tar_entry->size + 1);
668 if(fread(linkname, tar_entry->size, 1, tar_stream) != 1)
669 return NULL;
670 linkname[tar_entry->size] = '\0';
671 archive_offset += tar_entry->size;
672
673 tar_entry->name = linkname;
674 return(get_header_tar(tar_stream));
675 }
676 case 'D':
677 case 'M':
678 case 'N':
679 case 'S':
680 case 'V':
681 perror_msg("Ignoring GNU extension type %c", tar.formated.typeflag);
682 # endif
683 default:
684 perror_msg("Unknown typeflag: 0x%x", tar.formated.typeflag);
685 break;
686
687 }
688
689 return(tar_entry);
690 }
691
692 void free_header_tar(file_header_t *tar_entry)
693 {
694 if (tar_entry == NULL) {
695 return;
696 }
697
698 free(tar_entry->name);
699 free(tar_entry->link_name);
700
701 free(tar_entry);
702 }
703
704 #endif
705
706 #ifdef L_deb_extract
707 char *deb_extract(const char *package_filename, FILE *out_stream,
708 const int extract_function, const char *prefix, const char *filename)
709 {
710 FILE *deb_stream;
711 FILE *uncompressed_stream = NULL;
712 file_header_t *ar_header = NULL;
713 char **file_list = NULL;
714 char *output_buffer = NULL;
715 char *ared_file = NULL;
716 char ar_magic[8];
717 int gunzip_pid = 0;
718
719 if (filename != NULL) {
720 file_list = xmalloc(sizeof(char *) * 2);
721 file_list[0] = xstrdup(filename);
722 file_list[1] = NULL;
723 }
724
725 if (extract_function & extract_control_tar_gz) {
726 ared_file = xstrdup("control.tar.gz");
727 }
728 else if (extract_function & extract_data_tar_gz) {
729 ared_file = xstrdup("data.tar.gz");
730 } else {
731 fprintf(stderr, "no file specified to extract -- extract_function=%x\n", extract_function);
732 return NULL;
733 }
734
735 /* open the debian package to be worked on */
736 deb_stream = wfopen(package_filename, "r");
737 if (deb_stream == NULL) {
738 return(NULL);
739 }
740 /* set the buffer size */
741 setvbuf(deb_stream, NULL, _IOFBF, 0x8000);
742
743 /* check ar magic */
744 fread(ar_magic, 1, 8, deb_stream);
745 /*fprintf(stderr, "deb_extract ar_magic=%08x\n", *(long *)ar_magic);*/
746 if (strncmp(ar_magic,"!<arch>",7) == 0) {
747 archive_offset = 8;
748
749 while ((ar_header = get_header_ar(deb_stream)) != NULL) {
750 if (strcmp(ared_file, ar_header->name) == 0) {
751 /* open a stream of decompressed data */
752 uncompressed_stream = gz_open(deb_stream, &gunzip_pid);
753 archive_offset = 0;
754 output_buffer = unarchive(uncompressed_stream, out_stream, get_header_tar, free_header_tar, extract_function, prefix, file_list);
755 }
756 seek_sub_file(deb_stream, ar_header->size);
757 free (ar_header->name);
758 free (ar_header);
759 }
760 gz_close(gunzip_pid);
761 fclose(deb_stream);
762 fclose(uncompressed_stream);
763 free(ared_file);
764 return(output_buffer);
765 } else if (strncmp(ar_magic, "\037\213", 2) == 0) {
766 /* it's a gz file, let's assume it's an opkg */
767 int unzipped_opkg_pid;
768 FILE *unzipped_opkg_stream;
769 file_header_t *tar_header;
770 archive_offset = 0;
771 fseek(deb_stream, 0, SEEK_SET);
772 unzipped_opkg_stream = gz_open(deb_stream, &unzipped_opkg_pid);
773
774 /*fprintf(stderr, __FUNCTION__ ": processing opkg %s -- ared_file=%s\n", package_filename, ared_file);*/
775 /* walk through outer tar file to find ared_file */
776 while ((tar_header = get_header_tar(unzipped_opkg_stream)) != NULL) {
777 int name_offset = 0;
778 if (strncmp(tar_header->name, "./", 2) == 0)
779 name_offset = 2;
780 if (strcmp(ared_file, tar_header->name+name_offset) == 0) {
781 /* open a stream of decompressed data */
782 uncompressed_stream = gz_open(unzipped_opkg_stream, &gunzip_pid);
783 archive_offset = 0;
784 /*fprintf(stderr, __FUNCTION__ ":%d: here -- found file\n", __LINE__);*/
785 output_buffer = unarchive(uncompressed_stream,
786 out_stream,
787 get_header_tar,
788 free_header_tar,
789 extract_function,
790 prefix,
791 file_list);
792 /*fprintf(stderr, __FUNCTION__ ":%d: unarchive complete\n", __LINE__);*/
793 free_header_tar(tar_header);
794 gz_close(gunzip_pid);
795 fclose(uncompressed_stream);
796 break;
797 }
798 seek_sub_file(unzipped_opkg_stream, tar_header->size);
799 free_header_tar(tar_header);
800 }
801 gz_close(unzipped_opkg_pid);
802 fclose(unzipped_opkg_stream);
803 fclose(deb_stream);
804 free(ared_file);
805 /*fprintf(stderr, __FUNCTION__ ":%d: done\n", __LINE__);*/
806 return output_buffer;
807 } else {
808 error_msg_and_die("invalid magic");
809 }
810
811 }
812 #endif