2 * Copyright (C) 2000 by Glenn McGrath
3 * Copyright (C) 2001 by Laurence Anderson
5 * Based on previous work by busybox developers and others.
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.
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.
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.
30 typedef struct file_headers_s {
42 extern void seek_sub_file(FILE *src_stream, const int count);
43 extern char *extract_archive(FILE *src_stream, FILE *out_stream, const file_header_t *file_entry,
44 const int function, const char *prefix);
47 #ifdef L_archive_offset
50 extern off_t archive_offset;
53 #ifdef L_seek_sub_file
54 void seek_sub_file(FILE *src_stream, const int count)
57 /* Try to fseek as faster */
58 archive_offset += count;
59 if (fseek(src_stream, count, SEEK_CUR) != 0 && errno == ESPIPE) {
60 for (i = 0; i < count; i++) {
70 #ifdef L_extract_archive
71 /* Extract the data postioned at src_stream to either filesystem, stdout or
72 * buffer depending on the value of 'function' which is defined in libbb.h
74 * prefix doesnt have to be just a directory, it may prefix the filename as well.
76 * e.g. '/var/lib/dpkg/info/dpkg.' will extract all files to the base bath
77 * '/var/lib/dpkg/info/' and all files/dirs created in that dir will have
78 * 'dpkg.' as their prefix
80 * For this reason if prefix does point to a dir then it must end with a
81 * trailing '/' or else the last dir will be assumed to be the file prefix
83 char *extract_archive(FILE *src_stream, FILE *out_stream, const file_header_t *file_entry,
84 const int function, const char *prefix)
86 FILE *dst_stream = NULL;
87 char *full_name = NULL;
91 /* prefix doesnt have to be a proper path it may prepend
92 * the filename as well */
94 /* strip leading '/' in filename to extract as prefix may not be dir */
95 /* Cant use concat_path_file here as prefix might not be a directory */
96 char *path = file_entry->name;
100 full_name = xmalloc(strlen(prefix) + strlen(path) + 1);
101 strcpy(full_name, prefix);
102 strcat(full_name, path);
104 full_name = file_entry->name;
107 if (function & extract_to_stdout) {
108 if (S_ISREG(file_entry->mode)) {
109 copy_file_chunk(src_stream, out_stream, file_entry->size);
110 archive_offset += file_entry->size;
113 else if (function & extract_one_to_buffer) {
114 if (S_ISREG(file_entry->mode)) {
115 buffer = (char *) xmalloc(file_entry->size + 1);
116 fread(buffer, 1, file_entry->size, src_stream);
117 archive_offset += file_entry->size;
121 else if (function & extract_all_to_fs) {
124 stat_res = lstat (full_name, &oldfile);
125 if (stat_res == 0) { /* The file already exists */
126 if ((function & extract_unconditional) || (oldfile.st_mtime < file_entry->mtime)) {
127 if (!S_ISDIR(oldfile.st_mode)) {
128 unlink(full_name); /* Directories might not be empty etc */
131 error_msg("%s not created: newer or same age file exists", file_entry->name);
132 if (S_ISREG(file_entry->mode)) {
133 seek_sub_file(src_stream, file_entry->size);
138 switch(file_entry->mode & S_IFMT) {
140 if (file_entry->link_name) { /* Found a cpio hard link */
141 if (link(file_entry->link_name, full_name) != 0) {
142 perror_msg("Cannot link from %s to '%s'",
143 file_entry->name, file_entry->link_name);
146 if ((dst_stream = wfopen(full_name, "w")) == NULL) {
147 seek_sub_file(src_stream, file_entry->size);
150 archive_offset += file_entry->size;
151 copy_file_chunk(src_stream, dst_stream, file_entry->size);
156 if ((function & extract_create_dirs) && (stat_res != 0)) {
157 /* Make sure the prefix component of full_name was create
158 * in applet before getting here*/
159 if (mkdir(full_name, file_entry->mode) < 0) {
160 perror_msg("extract_archive: ");
165 if (symlink(file_entry->link_name, full_name) < 0) {
166 perror_msg("Cannot create symlink from %s to '%s'", file_entry->name, file_entry->link_name);
174 if (mknod(full_name, file_entry->mode, file_entry->device) == -1) {
175 perror_msg("Cannot create node %s", file_entry->name);
181 /* Changing a symlink's properties normally changes the properties of the
182 * file pointed to, so dont try and change the date or mode, lchown does
183 * does the right thing, but isnt available in older versions of libc */
184 if (S_ISLNK(file_entry->mode)) {
185 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
186 lchown(full_name, file_entry->uid, file_entry->gid);
189 if (function & extract_preserve_date) {
190 t.actime = file_entry->mtime;
191 t.modtime = file_entry->mtime;
192 utime(full_name, &t);
194 chmod(full_name, file_entry->mode);
195 chown(full_name, file_entry->uid, file_entry->gid);
198 /* If we arent extracting data we have to skip it,
199 * if data size is 0 then then just do it anyway
200 * (saves testing for it) */
201 seek_sub_file(src_stream, file_entry->size);
204 /* extract_list and extract_verbose_list can be used in conjunction
205 * with one of the above four extraction functions, so do this seperately */
206 if (function & extract_verbose_list) {
207 fprintf(out_stream, "%s %d/%d %8d %s ", mode_string(file_entry->mode),
208 file_entry->uid, file_entry->gid,
209 (int) file_entry->size, time_string(file_entry->mtime));
211 if ((function & extract_list) || (function & extract_verbose_list)){
212 /* fputs doesnt add a trailing \n, so use fprintf */
213 fprintf(out_stream, "%s\n", file_entry->name);
218 return(NULL); /* Maybe we should say if failed */
223 char *unarchive(FILE *src_stream, void *(*get_headers)(FILE *),
224 const int extract_function, const char *prefix, char **extract_names)
226 file_header_t *file_entry;
232 while ((file_entry = (file_header_t *) get_headers(src_stream)) != NULL) {
234 if (extract_names[0] != NULL) {
235 for(i = 0; extract_names[i] != 0; i++) {
236 if (strcmp(extract_names[i], file_entry->name) == 0) {
241 /* seek past the data entry */
242 if (!S_ISLNK(file_entry->mode) && file_entry->link_name && file_entry->size == 0) {
243 error_msg("You should extract %s as other files are hardlinked to it", file_entry->name);
245 seek_sub_file(src_stream, file_entry->size);
249 buffer = extract_archive(src_stream, stdout, file_entry, extract_function, prefix);
255 #ifdef L_get_header_ar
256 void *get_header_ar(FILE *src_stream)
258 file_header_t *typed;
271 static char *ar_long_names;
273 if (fread(ar.raw, 1, 60, src_stream) != 60) {
276 archive_offset += 60;
277 /* align the headers based on the header magic */
278 if ((ar.formated.magic[0] != '`') || (ar.formated.magic[1] != '\n')) {
279 /* some version of ar, have an extra '\n' after each data entry,
280 * this puts the next header out by 1 */
281 if (ar.formated.magic[1] != '`') {
282 error_msg("Invalid magic");
285 /* read the next char out of what would be the data section,
286 * if its a '\n' then it is a valid header offset by 1*/
288 if (fgetc(src_stream) != '\n') {
289 error_msg("Invalid magic");
292 /* fix up the header, we started reading 1 byte too early */
293 /* raw_header[60] wont be '\n' as it should, but it doesnt matter */
294 memmove(ar.raw, &ar.raw[1], 59);
297 typed = (file_header_t *) xcalloc(1, sizeof(file_header_t));
299 typed->size = (size_t) atoi(ar.formated.size);
300 /* long filenames have '/' as the first character */
301 if (ar.formated.name[0] == '/') {
302 if (ar.formated.name[1] == '/') {
303 /* If the second char is a '/' then this entries data section
304 * stores long filename for multiple entries, they are stored
305 * in static variable long_names for use in future entries */
306 ar_long_names = (char *) xrealloc(ar_long_names, typed->size);
307 fread(ar_long_names, 1, typed->size, src_stream);
308 archive_offset += typed->size;
309 /* This ar entries data section only contained filenames for other records
310 * they are stored in the static ar_long_names for future reference */
313 /* The number after the '/' indicates the offset in the ar data section
314 (saved in variable long_name) that conatains the real filename */
315 if (!ar_long_names) {
316 error_msg("Cannot resolve long file name");
319 typed->name = xstrdup(ar_long_names + atoi(&ar.formated.name[1]));
322 /* short filenames */
323 typed->name = xcalloc(1, 16);
324 strncpy(typed->name, ar.formated.name, 16);
326 typed->name[strcspn(typed->name, " /")]='\0';
328 /* convert the rest of the now valid char header to its typed struct */
329 parse_mode(ar.formated.mode, &typed->mode);
330 typed->mtime = atoi(ar.formated.date);
331 typed->uid = atoi(ar.formated.uid);
332 typed->gid = atoi(ar.formated.gid);
338 #ifdef L_get_header_cpio
340 file_header_t *entry;
342 struct hardlinks *next;
345 void *get_header_cpio(FILE *src_stream)
347 file_header_t *cpio_entry = NULL;
348 char cpio_header[110];
351 int major, minor, nlink, inode;
352 static struct hardlinks *saved_hardlinks = NULL;
353 static int pending_hardlinks = 0;
355 if (pending_hardlinks) { /* Deal with any pending hardlinks */
356 struct hardlinks *tmp = saved_hardlinks, *oldtmp = NULL;
358 if (tmp->entry->link_name) { /* Found a hardlink ready to be extracted */
359 cpio_entry = tmp->entry;
360 if (oldtmp) oldtmp->next = tmp->next; /* Remove item from linked list */
361 else saved_hardlinks = tmp->next;
368 pending_hardlinks = 0; /* No more pending hardlinks, read next file entry */
371 /* There can be padding before archive header */
372 seek_sub_file(src_stream, (4 - (archive_offset % 4)) % 4);
373 if (fread(cpio_header, 1, 110, src_stream) == 110) {
374 archive_offset += 110;
375 if (strncmp(cpio_header, "07070", 5) != 0) {
376 error_msg("Unsupported format or invalid magic");
379 switch (cpio_header[5]) {
380 case '2': /* "crc" header format */
381 /* Doesnt do the crc check yet */
382 case '1': /* "newc" header format */
383 cpio_entry = (file_header_t *) xcalloc(1, sizeof(file_header_t));
384 sscanf(cpio_header, "%6c%8x%8x%8x%8x%8x%8lx%8lx%16c%8x%8x%8x%8c",
385 dummy, &inode, &cpio_entry->mode, &cpio_entry->uid, &cpio_entry->gid,
386 &nlink, &cpio_entry->mtime, &cpio_entry->size,
387 dummy, &major, &minor, &namesize, dummy);
389 cpio_entry->name = (char *) xcalloc(1, namesize);
390 fread(cpio_entry->name, 1, namesize, src_stream); /* Read in filename */
391 archive_offset += namesize;
392 /* Skip padding before file contents */
393 seek_sub_file(src_stream, (4 - (archive_offset % 4)) % 4);
394 if (strcmp(cpio_entry->name, "TRAILER!!!") == 0) {
395 printf("%d blocks\n", (int) (archive_offset % 512 ? (archive_offset / 512) + 1 : archive_offset / 512)); /* Always round up */
396 if (saved_hardlinks) { /* Bummer - we still have unresolved hardlinks */
397 struct hardlinks *tmp = saved_hardlinks, *oldtmp = NULL;
399 error_msg("%s not created: cannot resolve hardlink", tmp->entry->name);
402 free (oldtmp->entry->name);
403 free (oldtmp->entry);
406 saved_hardlinks = NULL;
407 pending_hardlinks = 0;
412 if (S_ISLNK(cpio_entry->mode)) {
413 cpio_entry->link_name = (char *) xcalloc(1, cpio_entry->size + 1);
414 fread(cpio_entry->link_name, 1, cpio_entry->size, src_stream);
415 archive_offset += cpio_entry->size;
417 if (nlink > 1 && !S_ISDIR(cpio_entry->mode)) {
418 if (cpio_entry->size == 0) { /* Put file on a linked list for later */
419 struct hardlinks *new = xmalloc(sizeof(struct hardlinks));
420 new->next = saved_hardlinks;
422 new->entry = cpio_entry;
423 saved_hardlinks = new;
424 return(get_header_cpio(src_stream)); /* Recurse to next file */
425 } else { /* Found the file with data in */
426 struct hardlinks *tmp = saved_hardlinks;
427 pending_hardlinks = 1;
429 if (tmp->inode == inode) {
430 tmp->entry->link_name = xstrdup(cpio_entry->name);
435 if (nlink > 1) error_msg("error resolving hardlink: did you create the archive with GNU cpio 2.0-2.2?");
438 cpio_entry->device = (major << 8) | minor;
441 error_msg("Unsupported format");
444 if (ferror(src_stream) || feof(src_stream)) {
445 perror_msg("Stream error");
453 #ifdef L_get_header_tar
454 void *get_header_tar(FILE *tar_stream)
457 unsigned char raw[512];
459 char name[100]; /* 0-99 */
460 char mode[8]; /* 100-107 */
461 char uid[8]; /* 108-115 */
462 char gid[8]; /* 116-123 */
463 char size[12]; /* 124-135 */
464 char mtime[12]; /* 136-147 */
465 char chksum[8]; /* 148-155 */
466 char typeflag; /* 156-156 */
467 char linkname[100]; /* 157-256 */
468 char magic[6]; /* 257-262 */
469 char version[2]; /* 263-264 */
470 char uname[32]; /* 265-296 */
471 char gname[32]; /* 297-328 */
472 char devmajor[8]; /* 329-336 */
473 char devminor[8]; /* 337-344 */
474 char prefix[155]; /* 345-499 */
475 char padding[12]; /* 500-512 */
478 file_header_t *tar_entry = NULL;
482 if (archive_offset % 512 != 0) {
483 seek_sub_file(tar_stream, 512 - (archive_offset % 512));
486 if (fread(tar.raw, 1, 512, tar_stream) != 512) {
487 error_msg("Couldnt read header");
490 archive_offset += 512;
492 /* Check header has valid magic, unfortunately some tar files
493 * have empty (0'ed) tar entries at the end, which will
494 * cause this to fail, so fail silently for now
496 if (strncmp(tar.formated.magic, "ustar", 5) != 0) {
500 /* Do checksum on headers */
501 for (i = 0; i < 148 ; i++) {
505 for (i = 156; i < 512 ; i++) {
508 if (sum != strtol(tar.formated.chksum, NULL, 8)) {
509 error_msg("Invalid tar header checksum");
513 /* convert to type'ed variables */
514 tar_entry = xcalloc(1, sizeof(file_header_t));
515 tar_entry->name = xstrdup(tar.formated.name);
517 parse_mode(tar.formated.mode, &tar_entry->mode);
518 tar_entry->uid = strtol(tar.formated.uid, NULL, 8);
519 tar_entry->gid = strtol(tar.formated.gid, NULL, 8);
520 tar_entry->size = strtol(tar.formated.size, NULL, 8);
521 tar_entry->mtime = strtol(tar.formated.mtime, NULL, 8);
522 tar_entry->link_name = strlen(tar.formated.linkname) ? xstrdup(tar.formated.linkname) : NULL;
523 tar_entry->device = (strtol(tar.formated.devmajor, NULL, 8) << 8) +
524 strtol(tar.formated.devminor, NULL, 8);
531 char *deb_extract(const char *package_filename, FILE *out_stream, const int extract_function,
532 const char *prefix, const char *filename)
535 FILE *uncompressed_stream = NULL;
536 file_header_t *ar_header = NULL;
537 char *output_buffer = NULL;
538 char *ared_file = NULL;
543 file_list = malloc(sizeof(char *));
544 file_list[0] = xstrdup(filename);
547 if (extract_function & extract_control_tar_gz) {
548 ared_file = xstrdup("control.tar.gz");
550 else if (extract_function & extract_data_tar_gz) {
551 ared_file = xstrdup("data.tar.gz");
554 /* open the debian package to be worked on */
555 deb_stream = wfopen(package_filename, "r");
558 fread(ar_magic, 1, 8, deb_stream);
559 if (strncmp(ar_magic,"!<arch>",7) != 0) {
560 error_msg_and_die("invalid magic");
564 while ((ar_header = get_header_ar(deb_stream)) != NULL) {
565 if (strcmp(ared_file, ar_header->name) == 0) {
566 /* open a stream of decompressed data */
567 uncompressed_stream = gz_open(deb_stream, &gunzip_pid);
569 output_buffer = unarchive(uncompressed_stream, get_header_tar, extract_function, prefix, file_list);
571 seek_sub_file(deb_stream, ar_header->size);
573 gz_close(gunzip_pid);
575 fclose(uncompressed_stream);
577 return(output_buffer);