"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame] | 2 | /* Copyright 2001 Glenn McGrath. |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 3 | * |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame] | 4 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 10 | #include <unistd.h> |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 11 | #include "unarchive.h" |
| 12 | #include "libbb.h" |
| 13 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 14 | char get_header_ar(archive_handle_t *archive_handle) |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 15 | { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 16 | file_header_t *typed = archive_handle->file_header; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 17 | union { |
| 18 | char raw[60]; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 19 | struct { |
| 20 | char name[16]; |
| 21 | char date[12]; |
| 22 | char uid[6]; |
| 23 | char gid[6]; |
| 24 | char mode[8]; |
| 25 | char size[10]; |
| 26 | char magic[2]; |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 27 | } formatted; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 28 | } ar; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 29 | #ifdef CONFIG_FEATURE_AR_LONG_FILENAMES |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 30 | static char *ar_long_names; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 31 | static unsigned int ar_long_name_size; |
| 32 | #endif |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 33 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 34 | /* dont use bb_xread as we want to handle the error ourself */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 35 | if (read(archive_handle->src_fd, ar.raw, 60) != 60) { |
| 36 | /* End Of File */ |
| 37 | return(EXIT_FAILURE); |
Glenn L McGrath | 91e4646 | 2003-07-31 01:53:50 +0000 | [diff] [blame] | 38 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 39 | |
Glenn L McGrath | 958ac18 | 2004-04-09 06:59:05 +0000 | [diff] [blame] | 40 | /* ar header starts on an even byte (2 byte aligned) |
| 41 | * '\n' is used for padding |
| 42 | */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 43 | if (ar.raw[0] == '\n') { |
| 44 | /* fix up the header, we started reading 1 byte too early */ |
| 45 | memmove(ar.raw, &ar.raw[1], 59); |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 46 | ar.raw[59] = xread_char(archive_handle->src_fd); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 47 | archive_handle->offset++; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 48 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 49 | archive_handle->offset += 60; |
Glenn L McGrath | 91e4646 | 2003-07-31 01:53:50 +0000 | [diff] [blame] | 50 | |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 51 | /* align the headers based on the header magic */ |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 52 | if ((ar.formatted.magic[0] != '`') || (ar.formatted.magic[1] != '\n')) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 53 | bb_error_msg_and_die("Invalid ar header"); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 54 | } |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 55 | |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 56 | typed->mode = strtol(ar.formatted.mode, NULL, 8); |
| 57 | typed->mtime = atoi(ar.formatted.date); |
| 58 | typed->uid = atoi(ar.formatted.uid); |
| 59 | typed->gid = atoi(ar.formatted.gid); |
| 60 | typed->size = atoi(ar.formatted.size); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 61 | |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 62 | /* long filenames have '/' as the first character */ |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 63 | if (ar.formatted.name[0] == '/') { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 64 | #ifdef CONFIG_FEATURE_AR_LONG_FILENAMES |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 65 | if (ar.formatted.name[1] == '/') { |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 66 | /* If the second char is a '/' then this entries data section |
| 67 | * stores long filename for multiple entries, they are stored |
| 68 | * in static variable long_names for use in future entries */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 69 | ar_long_name_size = typed->size; |
| 70 | ar_long_names = xmalloc(ar_long_name_size); |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 71 | xread(archive_handle->src_fd, ar_long_names, ar_long_name_size); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 72 | archive_handle->offset += ar_long_name_size; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 73 | /* This ar entries data section only contained filenames for other records |
| 74 | * they are stored in the static ar_long_names for future reference */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 75 | return (get_header_ar(archive_handle)); /* Return next header */ |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 76 | } else if (ar.formatted.name[1] == ' ') { |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 77 | /* This is the index of symbols in the file for compilers */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 78 | data_skip(archive_handle); |
Glenn L McGrath | 08ca752 | 2004-01-04 11:06:34 +0000 | [diff] [blame] | 79 | archive_handle->offset += typed->size; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 80 | return (get_header_ar(archive_handle)); /* Return next header */ |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 81 | } else { |
| 82 | /* The number after the '/' indicates the offset in the ar data section |
| 83 | (saved in variable long_name) that conatains the real filename */ |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 84 | const unsigned int long_offset = atoi(&ar.formatted.name[1]); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 85 | if (long_offset >= ar_long_name_size) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 86 | bb_error_msg_and_die("Cant resolve long filename"); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 87 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 88 | typed->name = bb_xstrdup(ar_long_names + long_offset); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 89 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 90 | #else |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 91 | bb_error_msg_and_die("long filenames not supported"); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 92 | #endif |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 93 | } else { |
| 94 | /* short filenames */ |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 95 | typed->name = bb_xstrndup(ar.formatted.name, 16); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 96 | } |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 97 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 98 | typed->name[strcspn(typed->name, " /")] = '\0'; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 99 | |
Glenn L McGrath | 8e94098 | 2002-11-04 23:47:31 +0000 | [diff] [blame] | 100 | if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 101 | archive_handle->action_header(typed); |
| 102 | if (archive_handle->sub_archive) { |
| 103 | while (archive_handle->action_data_subarchive(archive_handle->sub_archive) == EXIT_SUCCESS); |
| 104 | } else { |
| 105 | archive_handle->action_data(archive_handle); |
| 106 | } |
| 107 | } else { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 108 | data_skip(archive_handle); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Glenn L McGrath | 91e4646 | 2003-07-31 01:53:50 +0000 | [diff] [blame] | 111 | archive_handle->offset += typed->size; |
| 112 | /* Set the file pointer to the correct spot, we may have been reading a compressed file */ |
| 113 | lseek(archive_handle->src_fd, archive_handle->offset, SEEK_SET); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 114 | |
| 115 | return(EXIT_SUCCESS); |
Eric Andersen | 2276d83 | 2002-07-11 11:11:56 +0000 | [diff] [blame] | 116 | } |