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