Thanks to Antonio
[oweals/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         {
604                 tar_entry->name = xstrndup(tar.formated.name, 100);
605
606                 if (tar.formated.prefix[0]) {
607                         char *temp = tar_entry->name;
608                         tar_entry->name = concat_path_file(tar.formated.prefix, temp);
609                         free(temp);
610                 }
611         }
612
613         // tar_entry->name = xstrdup(tar.formated.name);
614
615 /*
616         parse_mode(tar.formated.mode, &tar_entry->mode);
617 */
618         tar_entry->mode = 07777 & strtol(tar.formated.mode, NULL, 8);
619
620         tar_entry->uid   = strtol(tar.formated.uid, NULL, 8);
621         tar_entry->gid   = strtol(tar.formated.gid, NULL, 8);
622         tar_entry->size  = strtol(tar.formated.size, NULL, 8);
623         tar_entry->mtime = strtol(tar.formated.mtime, NULL, 8);
624         tar_entry->link_name  = strlen(tar.formated.linkname) ? 
625             xstrdup(tar.formated.linkname) : NULL;
626         tar_entry->device = (strtol(tar.formated.devmajor, NULL, 8) << 8) +
627                 strtol(tar.formated.devminor, NULL, 8);
628
629         /* Fix mode, used by the old format */
630         switch (tar.formated.typeflag) {
631         /* hard links are detected as regular files with 0 size and a link name */
632         case '1':
633                 tar_entry->mode |= S_IFREG ;
634                 break;
635         case 0:
636         case '0':
637
638 # ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
639                 if (last_char_is(tar_entry->name, '/')) {
640                         tar_entry->mode |= S_IFDIR;
641                 } else 
642 # endif
643                         tar_entry->mode |= S_IFREG;
644                 break;
645         case '2':
646                 tar_entry->mode |= S_IFLNK;
647                 break;
648         case '3':
649                 tar_entry->mode |= S_IFCHR;
650                 break;
651         case '4':
652                 tar_entry->mode |= S_IFBLK;
653                 break;
654         case '5':
655                 tar_entry->mode |= S_IFDIR;
656                 break;
657         case '6':
658                 tar_entry->mode |= S_IFIFO;
659                 break;
660 # ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
661         case 'L': {
662                         longname = xmalloc(tar_entry->size + 1);
663                         if(fread(longname, tar_entry->size, 1, tar_stream) != 1)
664                                 return NULL;
665                         longname[tar_entry->size] = '\0';
666                         archive_offset += tar_entry->size;
667
668                         return(get_header_tar(tar_stream));
669                 }
670         case 'K': {
671                         linkname = xmalloc(tar_entry->size + 1);
672                         if(fread(linkname, tar_entry->size, 1, tar_stream) != 1)
673                                 return NULL;
674                         linkname[tar_entry->size] = '\0';
675                         archive_offset += tar_entry->size;
676
677                         tar_entry->name = linkname;
678                         return(get_header_tar(tar_stream));
679                 }
680         case 'D':
681         case 'M':
682         case 'N':
683         case 'S':
684         case 'V':
685                 perror_msg("Ignoring GNU extension type %c", tar.formated.typeflag);
686 # endif
687         default:
688                 perror_msg("Unknown typeflag: 0x%x", tar.formated.typeflag);
689                 break;
690
691         }
692
693         return(tar_entry);
694 }
695
696 void free_header_tar(file_header_t *tar_entry)
697 {
698     if (tar_entry == NULL) {
699         return;
700     }
701
702     free(tar_entry->name);
703     free(tar_entry->link_name);
704
705     free(tar_entry);
706 }
707
708 #endif
709
710 #ifdef L_deb_extract
711 char *deb_extract(const char *package_filename, FILE *out_stream, 
712                   const int extract_function, const char *prefix, const char *filename)
713 {
714         FILE *deb_stream;
715         FILE *uncompressed_stream = NULL;
716         file_header_t *ar_header = NULL;
717         char **file_list = NULL;
718         char *output_buffer = NULL;
719         char *ared_file = NULL;
720         char ar_magic[8];
721         int gunzip_pid = 0;
722
723         if (filename != NULL) {
724                 file_list = xmalloc(sizeof(char *) * 2);
725                 file_list[0] = xstrdup(filename);
726                 file_list[1] = NULL;
727         }
728         
729         if (extract_function & extract_control_tar_gz) {
730                 ared_file = xstrdup("control.tar.gz");
731         }
732         else if (extract_function & extract_data_tar_gz) {              
733                 ared_file = xstrdup("data.tar.gz");
734         } else {
735                 fprintf(stderr, "no file specified to extract -- extract_function=%x\n", extract_function);
736                 return NULL;
737         }
738
739         /* open the debian package to be worked on */
740         deb_stream = wfopen(package_filename, "r");
741         if (deb_stream == NULL) {
742                 return(NULL);
743         }
744         /* set the buffer size */
745         setvbuf(deb_stream, NULL, _IOFBF, 0x8000);
746
747         /* check ar magic */
748         fread(ar_magic, 1, 8, deb_stream);
749         /*fprintf(stderr, "deb_extract ar_magic=%08x\n", *(long *)ar_magic);*/
750         if (strncmp(ar_magic,"!<arch>",7) == 0) {
751                 archive_offset = 8;
752
753                 while ((ar_header = get_header_ar(deb_stream)) != NULL) {
754                         if (strcmp(ared_file, ar_header->name) == 0) {
755                                 /* open a stream of decompressed data */
756                                 uncompressed_stream = gz_open(deb_stream, &gunzip_pid);
757                                 if (uncompressed_stream == NULL) {
758                                         return(NULL);
759                                 }
760
761                                 archive_offset = 0;
762                                 output_buffer = unarchive(uncompressed_stream, out_stream, get_header_tar, free_header_tar, extract_function, prefix, file_list);
763                         }
764                         seek_sub_file(deb_stream, ar_header->size);
765                         free (ar_header->name);
766                         free (ar_header);
767                 }
768                 gz_close(gunzip_pid);
769                 fclose(deb_stream);
770                 fclose(uncompressed_stream);
771                 free(ared_file);
772                 return(output_buffer);
773         } else if (strncmp(ar_magic, "\037\213", 2) == 0) {
774                 /* it's a gz file, let's assume it's an opkg */
775                 int unzipped_opkg_pid;
776                 FILE *unzipped_opkg_stream;
777                 file_header_t *tar_header;
778                 archive_offset = 0;
779                 fseek(deb_stream, 0, SEEK_SET);
780                 unzipped_opkg_stream = gz_open(deb_stream, &unzipped_opkg_pid);
781                 if (unzipped_opkg_stream == NULL) {
782                         return(NULL);
783                 }
784                 
785                 /*fprintf(stderr, __FUNCTION__ ": processing opkg %s -- ared_file=%s\n", package_filename, ared_file);*/
786                 /* walk through outer tar file to find ared_file */
787                 while ((tar_header = get_header_tar(unzipped_opkg_stream)) != NULL) {
788                         int name_offset = 0;
789                         if (strncmp(tar_header->name, "./", 2) == 0)
790                                 name_offset = 2;
791                         if (strcmp(ared_file, tar_header->name+name_offset) == 0) {
792                                 /* open a stream of decompressed data */
793                                 uncompressed_stream = gz_open(unzipped_opkg_stream, &gunzip_pid);
794                                 if (uncompressed_stream == NULL) {
795                                         return(NULL);
796                                 }
797                                 archive_offset = 0;
798                                 /*fprintf(stderr, __FUNCTION__ ":%d: here -- found file\n", __LINE__);*/
799                                 output_buffer = unarchive(uncompressed_stream, 
800                                                           out_stream, 
801                                                           get_header_tar,
802                                                           free_header_tar,
803                                                           extract_function, 
804                                                           prefix, 
805                                                           file_list);
806                                 /*fprintf(stderr, __FUNCTION__ ":%d: unarchive complete\n", __LINE__);*/
807                                 free_header_tar(tar_header);
808                                 gz_close(gunzip_pid);
809                                 fclose(uncompressed_stream);
810                                 break;
811                         }
812                         seek_sub_file(unzipped_opkg_stream, tar_header->size);
813                         free_header_tar(tar_header);
814                 }
815                 gz_close(unzipped_opkg_pid);
816                 fclose(unzipped_opkg_stream);
817                 fclose(deb_stream);
818                 free(ared_file);
819                 /*fprintf(stderr, __FUNCTION__ ":%d: done\n", __LINE__);*/
820                 return output_buffer; 
821         } else {
822                 error_msg_and_die("invalid magic");
823         }
824
825 }
826 #endif