blob: 2d171b4ce0e836032f74d22fb9caf4745858224f [file] [log] [blame]
Glenn L McGratheb1c9402001-06-20 07:48:00 +00001/*
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
Eric Andersen30f1eaf2001-06-22 03:00:21 +000030extern void seek_sub_file(FILE *src_stream, const int count);
31extern char *extract_archive(FILE *src_stream, FILE *out_stream, const file_header_t *file_entry,
32 const int function, const char *prefix);
33
34
35#ifdef L_archive_offset
36off_t archive_offset;
37#else
38extern off_t archive_offset;
39#endif
40
41#ifdef L_seek_sub_file
Glenn L McGratheb1c9402001-06-20 07:48:00 +000042void seek_sub_file(FILE *src_stream, const int count)
43{
44 int i;
45 /* Try to fseek as faster */
46 archive_offset += count;
47 if (fseek(src_stream, count, SEEK_CUR) != 0 && errno == ESPIPE) {
48 for (i = 0; i < count; i++) {
49 fgetc(src_stream);
50 }
51 }
52 return;
53}
Eric Andersen30f1eaf2001-06-22 03:00:21 +000054#endif
Glenn L McGratheb1c9402001-06-20 07:48:00 +000055
56
Eric Andersen30f1eaf2001-06-22 03:00:21 +000057
58#ifdef L_extract_archive
Glenn L McGratheb1c9402001-06-20 07:48:00 +000059/* Extract the data postioned at src_stream to either filesystem, stdout or
60 * buffer depending on the value of 'function' which is defined in libbb.h
61 *
62 * prefix doesnt have to be just a directory, it may prefix the filename as well.
63 *
64 * e.g. '/var/lib/dpkg/info/dpkg.' will extract all files to the base bath
65 * '/var/lib/dpkg/info/' and all files/dirs created in that dir will have
66 * 'dpkg.' as their prefix
67 *
68 * For this reason if prefix does point to a dir then it must end with a
69 * trailing '/' or else the last dir will be assumed to be the file prefix
70 */
71char *extract_archive(FILE *src_stream, FILE *out_stream, const file_header_t *file_entry,
72 const int function, const char *prefix)
73{
74 FILE *dst_stream = NULL;
75 char *full_name = NULL;
76 char *buffer = NULL;
77 struct utimbuf t;
78
79 /* prefix doesnt have to be a proper path it may prepend
80 * the filename as well */
81 if (prefix != NULL) {
82 /* strip leading '/' in filename to extract as prefix may not be dir */
83 /* Cant use concat_path_file here as prefix might not be a directory */
84 char *path = file_entry->name;
Glenn L McGrath8d3b0492001-07-18 13:22:44 +000085 if (strncmp("./", path, 2) == 0) {
86 path += 2;
87 if (strlen(path) == 0) {
88 return(NULL);
89 }
Glenn L McGratheb1c9402001-06-20 07:48:00 +000090 }
91 full_name = xmalloc(strlen(prefix) + strlen(path) + 1);
92 strcpy(full_name, prefix);
93 strcat(full_name, path);
94 } else {
95 full_name = file_entry->name;
96 }
Glenn L McGratheb1c9402001-06-20 07:48:00 +000097 if (function & extract_to_stdout) {
98 if (S_ISREG(file_entry->mode)) {
99 copy_file_chunk(src_stream, out_stream, file_entry->size);
100 archive_offset += file_entry->size;
101 }
102 }
103 else if (function & extract_one_to_buffer) {
104 if (S_ISREG(file_entry->mode)) {
105 buffer = (char *) xmalloc(file_entry->size + 1);
106 fread(buffer, 1, file_entry->size, src_stream);
Glenn L McGrathcfeb08a2001-07-13 17:59:48 +0000107 buffer[file_entry->size] = '\0';
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000108 archive_offset += file_entry->size;
109 return(buffer);
110 }
111 }
112 else if (function & extract_all_to_fs) {
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000113 struct stat oldfile;
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +0000114 int stat_res;
115 stat_res = lstat (full_name, &oldfile);
116 if (stat_res == 0) { /* The file already exists */
117 if ((function & extract_unconditional) || (oldfile.st_mtime < file_entry->mtime)) {
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000118 if (!S_ISDIR(oldfile.st_mode)) {
119 unlink(full_name); /* Directories might not be empty etc */
120 }
121 } else {
Glenn L McGrath3e94f722001-07-12 10:24:57 +0000122 if ((function & extract_quiet) != extract_quiet) {
123 error_msg("%s not created: newer or same age file exists", file_entry->name);
124 }
125 seek_sub_file(src_stream, file_entry->size);
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000126 return (NULL);
127 }
128 }
Glenn L McGrath2e6d3cf2001-06-24 12:36:54 +0000129 if (function & extract_create_leading_dirs) { /* Create leading directories with default umask */
Matt Kraaiac20ce12001-08-24 19:51:54 +0000130 char *buf, *parent;
131 buf = xstrdup(full_name);
132 parent = dirname(full_name);
Glenn L McGrath2e6d3cf2001-06-24 12:36:54 +0000133 if (make_directory (parent, -1, FILEUTILS_RECUR) != 0) {
Glenn L McGrath3e94f722001-07-12 10:24:57 +0000134 if ((function & extract_quiet) != extract_quiet) {
135 error_msg("couldn't create leading directories");
136 }
Glenn L McGrath2e6d3cf2001-06-24 12:36:54 +0000137 }
Matt Kraaiac20ce12001-08-24 19:51:54 +0000138 free (buf);
Glenn L McGrath2e6d3cf2001-06-24 12:36:54 +0000139 }
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000140 switch(file_entry->mode & S_IFMT) {
141 case S_IFREG:
142 if (file_entry->link_name) { /* Found a cpio hard link */
143 if (link(file_entry->link_name, full_name) != 0) {
Glenn L McGrath3e94f722001-07-12 10:24:57 +0000144 if ((function & extract_quiet) != extract_quiet) {
145 perror_msg("Cannot link from %s to '%s'",
146 file_entry->name, file_entry->link_name);
147 }
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000148 }
149 } else {
150 if ((dst_stream = wfopen(full_name, "w")) == NULL) {
151 seek_sub_file(src_stream, file_entry->size);
152 return NULL;
153 }
154 archive_offset += file_entry->size;
155 copy_file_chunk(src_stream, dst_stream, file_entry->size);
156 fclose(dst_stream);
157 }
158 break;
159 case S_IFDIR:
Glenn L McGrath2e6d3cf2001-06-24 12:36:54 +0000160 if (stat_res != 0) {
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +0000161 if (mkdir(full_name, file_entry->mode) < 0) {
Glenn L McGrath3e94f722001-07-12 10:24:57 +0000162 if ((function & extract_quiet) != extract_quiet) {
163 perror_msg("extract_archive: ");
164 }
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000165 }
166 }
167 break;
168 case S_IFLNK:
169 if (symlink(file_entry->link_name, full_name) < 0) {
Glenn L McGrath3e94f722001-07-12 10:24:57 +0000170 if ((function & extract_quiet) != extract_quiet) {
171 perror_msg("Cannot create symlink from %s to '%s'", file_entry->name, file_entry->link_name);
172 }
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000173 return NULL;
174 }
175 break;
176 case S_IFSOCK:
177 case S_IFBLK:
178 case S_IFCHR:
179 case S_IFIFO:
180 if (mknod(full_name, file_entry->mode, file_entry->device) == -1) {
Glenn L McGrath3e94f722001-07-12 10:24:57 +0000181 if ((function & extract_quiet) != extract_quiet) {
182 perror_msg("Cannot create node %s", file_entry->name);
183 }
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000184 return NULL;
185 }
186 break;
187 }
Glenn L McGrath76ce7542001-06-22 02:55:16 +0000188
189 /* Changing a symlink's properties normally changes the properties of the
190 * file pointed to, so dont try and change the date or mode, lchown does
191 * does the right thing, but isnt available in older versions of libc */
192 if (S_ISLNK(file_entry->mode)) {
Eric Andersen23b1e5c2001-08-02 09:58:41 +0000193#if (__GLIBC__ > 2) && (__GLIBC_MINOR__ > 1)
Glenn L McGrath76ce7542001-06-22 02:55:16 +0000194 lchown(full_name, file_entry->uid, file_entry->gid);
195#endif
196 } else {
197 if (function & extract_preserve_date) {
198 t.actime = file_entry->mtime;
199 t.modtime = file_entry->mtime;
200 utime(full_name, &t);
201 }
202 chmod(full_name, file_entry->mode);
203 chown(full_name, file_entry->uid, file_entry->gid);
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000204 }
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000205 } else {
206 /* If we arent extracting data we have to skip it,
207 * if data size is 0 then then just do it anyway
208 * (saves testing for it) */
209 seek_sub_file(src_stream, file_entry->size);
210 }
211
212 /* extract_list and extract_verbose_list can be used in conjunction
213 * with one of the above four extraction functions, so do this seperately */
214 if (function & extract_verbose_list) {
215 fprintf(out_stream, "%s %d/%d %8d %s ", mode_string(file_entry->mode),
216 file_entry->uid, file_entry->gid,
217 (int) file_entry->size, time_string(file_entry->mtime));
218 }
219 if ((function & extract_list) || (function & extract_verbose_list)){
220 /* fputs doesnt add a trailing \n, so use fprintf */
221 fprintf(out_stream, "%s\n", file_entry->name);
222 }
223
224 free(full_name);
225
226 return(NULL); /* Maybe we should say if failed */
227}
Eric Andersen30f1eaf2001-06-22 03:00:21 +0000228#endif
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000229
Eric Andersen30f1eaf2001-06-22 03:00:21 +0000230#ifdef L_unarchive
Glenn L McGrathc1270082001-07-11 17:32:14 +0000231char *unarchive(FILE *src_stream, FILE *out_stream, file_header_t *(*get_headers)(FILE *),
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000232 const int extract_function, const char *prefix, char **extract_names)
233{
234 file_header_t *file_entry;
Glenn L McGrathb373a8d2001-08-04 05:28:29 +0000235 int extract_flag;
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000236 int i;
237 char *buffer = NULL;
238
239 archive_offset = 0;
Eric Andersen5ef56142001-06-26 16:36:26 +0000240 while ((file_entry = get_headers(src_stream)) != NULL) {
Glenn L McGrathb373a8d2001-08-04 05:28:29 +0000241 extract_flag = TRUE;
242 if (extract_names != NULL) {
243 int found_flag = FALSE;
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000244 for(i = 0; extract_names[i] != 0; i++) {
245 if (strcmp(extract_names[i], file_entry->name) == 0) {
Glenn L McGrathb373a8d2001-08-04 05:28:29 +0000246 found_flag = TRUE;
247 break;
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000248 }
249 }
Glenn L McGrathb373a8d2001-08-04 05:28:29 +0000250 if (extract_function & extract_exclude_list) {
251 if (found_flag == TRUE) {
252 extract_flag = FALSE;
253 }
254 } else {
255 /* If its not found in the include list dont extract it */
256 if (found_flag == FALSE) {
257 extract_flag = FALSE;
258 }
259 }
260
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000261 }
Glenn L McGrath8d3b0492001-07-18 13:22:44 +0000262
Glenn L McGrathb373a8d2001-08-04 05:28:29 +0000263 if (extract_flag == TRUE) {
Glenn L McGratha868ec82001-07-14 08:49:53 +0000264 buffer = extract_archive(src_stream, out_stream, file_entry, extract_function, prefix);
265 } else {
266 /* seek past the data entry */
267 seek_sub_file(src_stream, file_entry->size);
268 }
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000269 }
270 return(buffer);
271}
Eric Andersen30f1eaf2001-06-22 03:00:21 +0000272#endif
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000273
Eric Andersen30f1eaf2001-06-22 03:00:21 +0000274#ifdef L_get_header_ar
Eric Andersen5ef56142001-06-26 16:36:26 +0000275file_header_t *get_header_ar(FILE *src_stream)
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000276{
277 file_header_t *typed;
278 union {
279 char raw[60];
280 struct {
281 char name[16];
282 char date[12];
283 char uid[6];
284 char gid[6];
285 char mode[8];
286 char size[10];
287 char magic[2];
288 } formated;
289 } ar;
290 static char *ar_long_names;
291
292 if (fread(ar.raw, 1, 60, src_stream) != 60) {
293 return(NULL);
294 }
295 archive_offset += 60;
296 /* align the headers based on the header magic */
297 if ((ar.formated.magic[0] != '`') || (ar.formated.magic[1] != '\n')) {
298 /* some version of ar, have an extra '\n' after each data entry,
299 * this puts the next header out by 1 */
300 if (ar.formated.magic[1] != '`') {
301 error_msg("Invalid magic");
302 return(NULL);
303 }
304 /* read the next char out of what would be the data section,
305 * if its a '\n' then it is a valid header offset by 1*/
306 archive_offset++;
307 if (fgetc(src_stream) != '\n') {
308 error_msg("Invalid magic");
309 return(NULL);
310 }
311 /* fix up the header, we started reading 1 byte too early */
312 /* raw_header[60] wont be '\n' as it should, but it doesnt matter */
313 memmove(ar.raw, &ar.raw[1], 59);
314 }
315
316 typed = (file_header_t *) xcalloc(1, sizeof(file_header_t));
317
318 typed->size = (size_t) atoi(ar.formated.size);
319 /* long filenames have '/' as the first character */
320 if (ar.formated.name[0] == '/') {
321 if (ar.formated.name[1] == '/') {
322 /* If the second char is a '/' then this entries data section
323 * stores long filename for multiple entries, they are stored
324 * in static variable long_names for use in future entries */
325 ar_long_names = (char *) xrealloc(ar_long_names, typed->size);
326 fread(ar_long_names, 1, typed->size, src_stream);
327 archive_offset += typed->size;
328 /* This ar entries data section only contained filenames for other records
329 * they are stored in the static ar_long_names for future reference */
Glenn L McGrath2e6d3cf2001-06-24 12:36:54 +0000330 return (get_header_ar(src_stream)); /* Return next header */
331 } else if (ar.formated.name[1] == ' ') {
332 /* This is the index of symbols in the file for compilers */
333 seek_sub_file(src_stream, typed->size);
334 return (get_header_ar(src_stream)); /* Return next header */
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000335 } else {
336 /* The number after the '/' indicates the offset in the ar data section
337 (saved in variable long_name) that conatains the real filename */
338 if (!ar_long_names) {
339 error_msg("Cannot resolve long file name");
340 return (NULL);
341 }
342 typed->name = xstrdup(ar_long_names + atoi(&ar.formated.name[1]));
343 }
344 } else {
345 /* short filenames */
346 typed->name = xcalloc(1, 16);
347 strncpy(typed->name, ar.formated.name, 16);
348 }
349 typed->name[strcspn(typed->name, " /")]='\0';
350
351 /* convert the rest of the now valid char header to its typed struct */
352 parse_mode(ar.formated.mode, &typed->mode);
353 typed->mtime = atoi(ar.formated.date);
354 typed->uid = atoi(ar.formated.uid);
355 typed->gid = atoi(ar.formated.gid);
356
357 return(typed);
358}
359#endif
360
Eric Andersen30f1eaf2001-06-22 03:00:21 +0000361#ifdef L_get_header_cpio
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +0000362struct hardlinks {
363 file_header_t *entry;
364 int inode;
365 struct hardlinks *next;
366};
367
Eric Andersen5ef56142001-06-26 16:36:26 +0000368file_header_t *get_header_cpio(FILE *src_stream)
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000369{
370 file_header_t *cpio_entry = NULL;
371 char cpio_header[110];
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000372 int namesize;
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +0000373 char dummy[16];
374 int major, minor, nlink, inode;
375 static struct hardlinks *saved_hardlinks = NULL;
376 static int pending_hardlinks = 0;
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000377
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +0000378 if (pending_hardlinks) { /* Deal with any pending hardlinks */
379 struct hardlinks *tmp = saved_hardlinks, *oldtmp = NULL;
380 while (tmp) {
381 if (tmp->entry->link_name) { /* Found a hardlink ready to be extracted */
382 cpio_entry = tmp->entry;
383 if (oldtmp) oldtmp->next = tmp->next; /* Remove item from linked list */
384 else saved_hardlinks = tmp->next;
385 free(tmp);
386 return (cpio_entry);
387 }
388 oldtmp = tmp;
389 tmp = tmp->next;
390 }
391 pending_hardlinks = 0; /* No more pending hardlinks, read next file entry */
392 }
393
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000394 /* There can be padding before archive header */
395 seek_sub_file(src_stream, (4 - (archive_offset % 4)) % 4);
396 if (fread(cpio_header, 1, 110, src_stream) == 110) {
397 archive_offset += 110;
398 if (strncmp(cpio_header, "07070", 5) != 0) {
399 error_msg("Unsupported format or invalid magic");
400 return(NULL);
401 }
402 switch (cpio_header[5]) {
403 case '2': /* "crc" header format */
404 /* Doesnt do the crc check yet */
405 case '1': /* "newc" header format */
406 cpio_entry = (file_header_t *) xcalloc(1, sizeof(file_header_t));
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +0000407 sscanf(cpio_header, "%6c%8x%8x%8x%8x%8x%8lx%8lx%16c%8x%8x%8x%8c",
Eric Andersen23b1e5c2001-08-02 09:58:41 +0000408 dummy, &inode, (unsigned int*)&cpio_entry->mode,
409 (unsigned int*)&cpio_entry->uid, (unsigned int*)&cpio_entry->gid,
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000410 &nlink, &cpio_entry->mtime, &cpio_entry->size,
411 dummy, &major, &minor, &namesize, dummy);
412
413 cpio_entry->name = (char *) xcalloc(1, namesize);
414 fread(cpio_entry->name, 1, namesize, src_stream); /* Read in filename */
415 archive_offset += namesize;
416 /* Skip padding before file contents */
417 seek_sub_file(src_stream, (4 - (archive_offset % 4)) % 4);
418 if (strcmp(cpio_entry->name, "TRAILER!!!") == 0) {
419 printf("%d blocks\n", (int) (archive_offset % 512 ? (archive_offset / 512) + 1 : archive_offset / 512)); /* Always round up */
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +0000420 if (saved_hardlinks) { /* Bummer - we still have unresolved hardlinks */
421 struct hardlinks *tmp = saved_hardlinks, *oldtmp = NULL;
422 while (tmp) {
423 error_msg("%s not created: cannot resolve hardlink", tmp->entry->name);
424 oldtmp = tmp;
425 tmp = tmp->next;
426 free (oldtmp->entry->name);
427 free (oldtmp->entry);
428 free (oldtmp);
429 }
430 saved_hardlinks = NULL;
431 pending_hardlinks = 0;
432 }
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000433 return(NULL);
434 }
435
436 if (S_ISLNK(cpio_entry->mode)) {
437 cpio_entry->link_name = (char *) xcalloc(1, cpio_entry->size + 1);
438 fread(cpio_entry->link_name, 1, cpio_entry->size, src_stream);
439 archive_offset += cpio_entry->size;
Glenn L McGrath2e6d3cf2001-06-24 12:36:54 +0000440 cpio_entry->size = 0; /* Stop possiable seeks in future */
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000441 }
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +0000442 if (nlink > 1 && !S_ISDIR(cpio_entry->mode)) {
443 if (cpio_entry->size == 0) { /* Put file on a linked list for later */
444 struct hardlinks *new = xmalloc(sizeof(struct hardlinks));
445 new->next = saved_hardlinks;
446 new->inode = inode;
447 new->entry = cpio_entry;
448 saved_hardlinks = new;
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000449 return(get_header_cpio(src_stream)); /* Recurse to next file */
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +0000450 } else { /* Found the file with data in */
451 struct hardlinks *tmp = saved_hardlinks;
452 pending_hardlinks = 1;
453 while (tmp) {
454 if (tmp->inode == inode) {
455 tmp->entry->link_name = xstrdup(cpio_entry->name);
456 nlink--;
457 }
458 tmp = tmp->next;
459 }
460 if (nlink > 1) error_msg("error resolving hardlink: did you create the archive with GNU cpio 2.0-2.2?");
461 }
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000462 }
463 cpio_entry->device = (major << 8) | minor;
464 break;
465 default:
466 error_msg("Unsupported format");
467 return(NULL);
468 }
469 if (ferror(src_stream) || feof(src_stream)) {
470 perror_msg("Stream error");
471 return(NULL);
472 }
473 }
474 return(cpio_entry);
475}
476#endif
477
Eric Andersen30f1eaf2001-06-22 03:00:21 +0000478#ifdef L_get_header_tar
Eric Andersen5ef56142001-06-26 16:36:26 +0000479file_header_t *get_header_tar(FILE *tar_stream)
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000480{
481 union {
482 unsigned char raw[512];
483 struct {
484 char name[100]; /* 0-99 */
485 char mode[8]; /* 100-107 */
486 char uid[8]; /* 108-115 */
487 char gid[8]; /* 116-123 */
488 char size[12]; /* 124-135 */
489 char mtime[12]; /* 136-147 */
490 char chksum[8]; /* 148-155 */
491 char typeflag; /* 156-156 */
492 char linkname[100]; /* 157-256 */
493 char magic[6]; /* 257-262 */
494 char version[2]; /* 263-264 */
495 char uname[32]; /* 265-296 */
496 char gname[32]; /* 297-328 */
497 char devmajor[8]; /* 329-336 */
498 char devminor[8]; /* 337-344 */
499 char prefix[155]; /* 345-499 */
500 char padding[12]; /* 500-512 */
501 } formated;
502 } tar;
503 file_header_t *tar_entry = NULL;
504 long i;
505 long sum = 0;
506
507 if (archive_offset % 512 != 0) {
508 seek_sub_file(tar_stream, 512 - (archive_offset % 512));
509 }
510
511 if (fread(tar.raw, 1, 512, tar_stream) != 512) {
Glenn L McGrath6fc92a52001-07-18 03:23:10 +0000512 /* Unfortunatly its common for tar files to have all sorts of
513 * trailing garbage, fail silently */
514// error_msg("Couldnt read header");
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000515 return(NULL);
516 }
517 archive_offset += 512;
518
519 /* Check header has valid magic, unfortunately some tar files
520 * have empty (0'ed) tar entries at the end, which will
521 * cause this to fail, so fail silently for now
522 */
523 if (strncmp(tar.formated.magic, "ustar", 5) != 0) {
524 return(NULL);
525 }
526
527 /* Do checksum on headers */
528 for (i = 0; i < 148 ; i++) {
529 sum += tar.raw[i];
530 }
531 sum += ' ' * 8;
532 for (i = 156; i < 512 ; i++) {
533 sum += tar.raw[i];
534 }
535 if (sum != strtol(tar.formated.chksum, NULL, 8)) {
536 error_msg("Invalid tar header checksum");
537 return(NULL);
538 }
539
540 /* convert to type'ed variables */
541 tar_entry = xcalloc(1, sizeof(file_header_t));
542 tar_entry->name = xstrdup(tar.formated.name);
543
544 parse_mode(tar.formated.mode, &tar_entry->mode);
545 tar_entry->uid = strtol(tar.formated.uid, NULL, 8);
546 tar_entry->gid = strtol(tar.formated.gid, NULL, 8);
547 tar_entry->size = strtol(tar.formated.size, NULL, 8);
548 tar_entry->mtime = strtol(tar.formated.mtime, NULL, 8);
Eric Andersen5ef56142001-06-26 16:36:26 +0000549 tar_entry->link_name = strlen(tar.formated.linkname) ?
550 xstrdup(tar.formated.linkname) : NULL;
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000551 tar_entry->device = (strtol(tar.formated.devmajor, NULL, 8) << 8) +
552 strtol(tar.formated.devminor, NULL, 8);
553
554 return(tar_entry);
555}
556#endif
557
Eric Andersen30f1eaf2001-06-22 03:00:21 +0000558#ifdef L_deb_extract
Eric Andersen5ef56142001-06-26 16:36:26 +0000559char *deb_extract(const char *package_filename, FILE *out_stream,
560 const int extract_function, const char *prefix, const char *filename)
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000561{
562 FILE *deb_stream;
563 FILE *uncompressed_stream = NULL;
564 file_header_t *ar_header = NULL;
Glenn L McGratha868ec82001-07-14 08:49:53 +0000565 char **file_list = NULL;
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000566 char *output_buffer = NULL;
567 char *ared_file = NULL;
568 char ar_magic[8];
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000569 int gunzip_pid;
570
Glenn L McGratha868ec82001-07-14 08:49:53 +0000571 if (filename != NULL) {
572 file_list = xmalloc(sizeof(char *) * 2);
Glenn L McGrath248c57c2001-07-11 07:22:17 +0000573 file_list[0] = xstrdup(filename);
574 file_list[1] = NULL;
575 }
576
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000577 if (extract_function & extract_control_tar_gz) {
578 ared_file = xstrdup("control.tar.gz");
579 }
580 else if (extract_function & extract_data_tar_gz) {
581 ared_file = xstrdup("data.tar.gz");
582 }
583
584 /* open the debian package to be worked on */
585 deb_stream = wfopen(package_filename, "r");
Glenn L McGrath3d462242001-07-13 18:16:57 +0000586 if (deb_stream == NULL) {
587 return(NULL);
588 }
Glenn L McGrathb028e082001-07-13 06:43:03 +0000589 /* set the buffer size */
590 setvbuf(deb_stream, NULL, _IOFBF, 0x8000);
591
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000592 /* check ar magic */
593 fread(ar_magic, 1, 8, deb_stream);
594 if (strncmp(ar_magic,"!<arch>",7) != 0) {
595 error_msg_and_die("invalid magic");
596 }
597 archive_offset = 8;
598
599 while ((ar_header = get_header_ar(deb_stream)) != NULL) {
600 if (strcmp(ared_file, ar_header->name) == 0) {
601 /* open a stream of decompressed data */
602 uncompressed_stream = gz_open(deb_stream, &gunzip_pid);
603 archive_offset = 0;
Glenn L McGrathc1270082001-07-11 17:32:14 +0000604 output_buffer = unarchive(uncompressed_stream, out_stream, get_header_tar, extract_function, prefix, file_list);
Glenn L McGratheb1c9402001-06-20 07:48:00 +0000605 }
606 seek_sub_file(deb_stream, ar_header->size);
607 }
608 gz_close(gunzip_pid);
609 fclose(deb_stream);
610 fclose(uncompressed_stream);
611 free(ared_file);
612 return(output_buffer);
613}
614#endif