blob: b91c6f97d76e99a618c3ef5cd3aeaaa7967c220f [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Rob Landley1ec5b292006-05-29 07:42:02 +00002/* Copyright 2001 Glenn McGrath.
Glenn L McGrath95ebf612001-10-25 14:18:08 +00003 *
Rob Landley1ec5b292006-05-29 07:42:02 +00004 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath95ebf612001-10-25 14:18:08 +00005 */
6
Glenn L McGrath95ebf612001-10-25 14:18:08 +00007#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +00008#include "unarchive.h"
Glenn L McGrath95ebf612001-10-25 14:18:08 +00009
Rob Landleydfba7412006-03-06 20:47:33 +000010char get_header_ar(archive_handle_t *archive_handle)
Glenn L McGrath95ebf612001-10-25 14:18:08 +000011{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000012 file_header_t *typed = archive_handle->file_header;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000013 union {
14 char raw[60];
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000015 struct {
16 char name[16];
17 char date[12];
18 char uid[6];
19 char gid[6];
20 char mode[8];
21 char size[10];
22 char magic[2];
"Robert P. J. Day"eea56182006-07-20 19:02:24 +000023 } formatted;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000024 } ar;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000025#ifdef CONFIG_FEATURE_AR_LONG_FILENAMES
Glenn L McGrath95ebf612001-10-25 14:18:08 +000026 static char *ar_long_names;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000027 static unsigned int ar_long_name_size;
28#endif
Glenn L McGrath95ebf612001-10-25 14:18:08 +000029
Rob Landleyd921b2e2006-08-03 15:41:12 +000030 /* dont use xread as we want to handle the error ourself */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000031 if (read(archive_handle->src_fd, ar.raw, 60) != 60) {
32 /* End Of File */
33 return(EXIT_FAILURE);
Glenn L McGrath91e46462003-07-31 01:53:50 +000034 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000035
Glenn L McGrath958ac182004-04-09 06:59:05 +000036 /* ar header starts on an even byte (2 byte aligned)
37 * '\n' is used for padding
38 */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000039 if (ar.raw[0] == '\n') {
40 /* fix up the header, we started reading 1 byte too early */
41 memmove(ar.raw, &ar.raw[1], 59);
Rob Landley53437472006-07-16 08:14:35 +000042 ar.raw[59] = xread_char(archive_handle->src_fd);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000043 archive_handle->offset++;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000044 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000045 archive_handle->offset += 60;
Glenn L McGrath91e46462003-07-31 01:53:50 +000046
Glenn L McGrath95ebf612001-10-25 14:18:08 +000047 /* align the headers based on the header magic */
"Robert P. J. Day"eea56182006-07-20 19:02:24 +000048 if ((ar.formatted.magic[0] != '`') || (ar.formatted.magic[1] != '\n')) {
Denis Vlasenko13858992006-10-08 12:49:22 +000049 bb_error_msg_and_die("invalid ar header");
Glenn L McGrath95ebf612001-10-25 14:18:08 +000050 }
Glenn L McGrath95ebf612001-10-25 14:18:08 +000051
Denis Vlasenko13858992006-10-08 12:49:22 +000052 typed->mode = xstrtoul(ar.formatted.mode, 8);
53 typed->mtime = xatou(ar.formatted.date);
54 typed->uid = xatou(ar.formatted.uid);
55 typed->gid = xatou(ar.formatted.gid);
56 typed->size = xatoul(ar.formatted.size);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000057
Glenn L McGrath95ebf612001-10-25 14:18:08 +000058 /* long filenames have '/' as the first character */
"Robert P. J. Day"eea56182006-07-20 19:02:24 +000059 if (ar.formatted.name[0] == '/') {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000060#ifdef CONFIG_FEATURE_AR_LONG_FILENAMES
"Robert P. J. Day"eea56182006-07-20 19:02:24 +000061 if (ar.formatted.name[1] == '/') {
Glenn L McGrath95ebf612001-10-25 14:18:08 +000062 /* If the second char is a '/' then this entries data section
63 * stores long filename for multiple entries, they are stored
64 * in static variable long_names for use in future entries */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000065 ar_long_name_size = typed->size;
66 ar_long_names = xmalloc(ar_long_name_size);
Rob Landley53437472006-07-16 08:14:35 +000067 xread(archive_handle->src_fd, ar_long_names, ar_long_name_size);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000068 archive_handle->offset += ar_long_name_size;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000069 /* This ar entries data section only contained filenames for other records
70 * they are stored in the static ar_long_names for future reference */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000071 return (get_header_ar(archive_handle)); /* Return next header */
"Robert P. J. Day"eea56182006-07-20 19:02:24 +000072 } else if (ar.formatted.name[1] == ' ') {
Glenn L McGrath95ebf612001-10-25 14:18:08 +000073 /* This is the index of symbols in the file for compilers */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000074 data_skip(archive_handle);
Glenn L McGrath08ca7522004-01-04 11:06:34 +000075 archive_handle->offset += typed->size;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000076 return (get_header_ar(archive_handle)); /* Return next header */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000077 } else {
78 /* The number after the '/' indicates the offset in the ar data section
79 (saved in variable long_name) that conatains the real filename */
"Robert P. J. Day"eea56182006-07-20 19:02:24 +000080 const unsigned int long_offset = atoi(&ar.formatted.name[1]);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000081 if (long_offset >= ar_long_name_size) {
Denis Vlasenkoea620772006-10-14 02:23:43 +000082 bb_error_msg_and_die("can't resolve long filename");
Glenn L McGrath95ebf612001-10-25 14:18:08 +000083 }
Rob Landleyd921b2e2006-08-03 15:41:12 +000084 typed->name = xstrdup(ar_long_names + long_offset);
Glenn L McGrath95ebf612001-10-25 14:18:08 +000085 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000086#else
Manuel Novoa III cad53642003-03-19 09:13:01 +000087 bb_error_msg_and_die("long filenames not supported");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000088#endif
Glenn L McGrath95ebf612001-10-25 14:18:08 +000089 } else {
90 /* short filenames */
Rob Landleyd921b2e2006-08-03 15:41:12 +000091 typed->name = xstrndup(ar.formatted.name, 16);
Glenn L McGrath95ebf612001-10-25 14:18:08 +000092 }
Glenn L McGrath95ebf612001-10-25 14:18:08 +000093
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000094 typed->name[strcspn(typed->name, " /")] = '\0';
Glenn L McGrath95ebf612001-10-25 14:18:08 +000095
Glenn L McGrath8e940982002-11-04 23:47:31 +000096 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000097 archive_handle->action_header(typed);
98 if (archive_handle->sub_archive) {
99 while (archive_handle->action_data_subarchive(archive_handle->sub_archive) == EXIT_SUCCESS);
100 } else {
101 archive_handle->action_data(archive_handle);
102 }
103 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000104 data_skip(archive_handle);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000105 }
106
Glenn L McGrath91e46462003-07-31 01:53:50 +0000107 archive_handle->offset += typed->size;
108 /* Set the file pointer to the correct spot, we may have been reading a compressed file */
109 lseek(archive_handle->src_fd, archive_handle->offset, SEEK_SET);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000110
111 return(EXIT_SUCCESS);
Eric Andersen2276d832002-07-11 11:11:56 +0000112}