blob: 6c576a8da3a6dc67231fa9e0e1950f38d9dd40b1 [file] [log] [blame]
Glenn L McGrath95ebf612001-10-25 14:18:08 +00001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 */
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000020#include <unistd.h>
Glenn L McGrath95ebf612001-10-25 14:18:08 +000021#include "unarchive.h"
22#include "libbb.h"
23
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000024extern char get_header_ar(archive_handle_t *archive_handle)
Glenn L McGrath95ebf612001-10-25 14:18:08 +000025{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000026 file_header_t *typed = archive_handle->file_header;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000027 union {
28 char raw[60];
29 struct {
30 char name[16];
31 char date[12];
32 char uid[6];
33 char gid[6];
34 char mode[8];
35 char size[10];
36 char magic[2];
37 } formated;
38 } ar;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000039#ifdef CONFIG_FEATURE_AR_LONG_FILENAMES
Glenn L McGrath95ebf612001-10-25 14:18:08 +000040 static char *ar_long_names;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000041 static unsigned int ar_long_name_size;
42#endif
Glenn L McGrath95ebf612001-10-25 14:18:08 +000043
Manuel Novoa III cad53642003-03-19 09:13:01 +000044 /* dont use bb_xread as we want to handle the error ourself */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000045 if (read(archive_handle->src_fd, ar.raw, 60) != 60) {
46 /* End Of File */
47 return(EXIT_FAILURE);
48 }
49
50 /* Some ar entries have a trailing '\n' after the previous data entry */
51 if (ar.raw[0] == '\n') {
52 /* fix up the header, we started reading 1 byte too early */
53 memmove(ar.raw, &ar.raw[1], 59);
Manuel Novoa III cad53642003-03-19 09:13:01 +000054 ar.raw[59] = bb_xread_char(archive_handle->src_fd);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000055 archive_handle->offset++;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000056 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000057 archive_handle->offset += 60;
58
Glenn L McGrath95ebf612001-10-25 14:18:08 +000059 /* align the headers based on the header magic */
60 if ((ar.formated.magic[0] != '`') || (ar.formated.magic[1] != '\n')) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000061 bb_error_msg_and_die("Invalid ar header");
Glenn L McGrath95ebf612001-10-25 14:18:08 +000062 }
Glenn L McGrath95ebf612001-10-25 14:18:08 +000063
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000064 typed->mode = strtol(ar.formated.mode, NULL, 8);
65 typed->mtime = atoi(ar.formated.date);
66 typed->uid = atoi(ar.formated.uid);
67 typed->gid = atoi(ar.formated.gid);
68 typed->size = atoi(ar.formated.size);
69
Glenn L McGrath95ebf612001-10-25 14:18:08 +000070 /* long filenames have '/' as the first character */
71 if (ar.formated.name[0] == '/') {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000072#ifdef CONFIG_FEATURE_AR_LONG_FILENAMES
Glenn L McGrath95ebf612001-10-25 14:18:08 +000073 if (ar.formated.name[1] == '/') {
74 /* If the second char is a '/' then this entries data section
75 * stores long filename for multiple entries, they are stored
76 * in static variable long_names for use in future entries */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000077 ar_long_name_size = typed->size;
78 ar_long_names = xmalloc(ar_long_name_size);
Manuel Novoa III cad53642003-03-19 09:13:01 +000079 bb_xread_all(archive_handle->src_fd, ar_long_names, ar_long_name_size);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000080 archive_handle->offset += ar_long_name_size;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000081 /* This ar entries data section only contained filenames for other records
82 * they are stored in the static ar_long_names for future reference */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000083 return (get_header_ar(archive_handle)); /* Return next header */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000084 } else if (ar.formated.name[1] == ' ') {
85 /* This is the index of symbols in the file for compilers */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000086 data_skip(archive_handle);
87 return (get_header_ar(archive_handle)); /* Return next header */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000088 } else {
89 /* The number after the '/' indicates the offset in the ar data section
90 (saved in variable long_name) that conatains the real filename */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000091 const unsigned int long_offset = atoi(&ar.formated.name[1]);
92 if (long_offset >= ar_long_name_size) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000093 bb_error_msg_and_die("Cant resolve long filename");
Glenn L McGrath95ebf612001-10-25 14:18:08 +000094 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000095 typed->name = bb_xstrdup(ar_long_names + long_offset);
Glenn L McGrath95ebf612001-10-25 14:18:08 +000096 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000097#else
Manuel Novoa III cad53642003-03-19 09:13:01 +000098 bb_error_msg_and_die("long filenames not supported");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000099#endif
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000100 } else {
101 /* short filenames */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000102 typed->name = bb_xstrndup(ar.formated.name, 16);
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000103 }
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000104
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000105 typed->name[strcspn(typed->name, " /")] = '\0';
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000106
Glenn L McGrath8e940982002-11-04 23:47:31 +0000107 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000108 archive_handle->action_header(typed);
109 if (archive_handle->sub_archive) {
110 while (archive_handle->action_data_subarchive(archive_handle->sub_archive) == EXIT_SUCCESS);
111 } else {
112 archive_handle->action_data(archive_handle);
113 }
114 } else {
115 data_skip(archive_handle);
116 }
117
118 archive_handle->offset += typed->size + 1;
119
120 return(EXIT_SUCCESS);
Eric Andersen2276d832002-07-11 11:11:56 +0000121}
122