2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2006-05-29 13:12:02 +05:30
|
|
|
/* Copyright 2001 Glenn McGrath.
|
2001-10-25 19:48:08 +05:30
|
|
|
*
|
2006-05-29 13:12:02 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2001-10-25 19:48:08 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libbb.h"
|
2006-08-03 21:11:12 +05:30
|
|
|
#include "unarchive.h"
|
2001-10-25 19:48:08 +05:30
|
|
|
|
2006-03-07 02:17:33 +05:30
|
|
|
char get_header_ar(archive_handle_t *archive_handle)
|
2001-10-25 19:48:08 +05:30
|
|
|
{
|
2002-09-25 08:17:48 +05:30
|
|
|
file_header_t *typed = archive_handle->file_header;
|
2001-10-25 19:48:08 +05:30
|
|
|
union {
|
|
|
|
char raw[60];
|
2006-01-25 05:38:53 +05:30
|
|
|
struct {
|
|
|
|
char name[16];
|
|
|
|
char date[12];
|
|
|
|
char uid[6];
|
|
|
|
char gid[6];
|
|
|
|
char mode[8];
|
|
|
|
char size[10];
|
|
|
|
char magic[2];
|
2006-07-21 00:32:24 +05:30
|
|
|
} formatted;
|
2001-10-25 19:48:08 +05:30
|
|
|
} ar;
|
2002-09-25 08:17:48 +05:30
|
|
|
#ifdef CONFIG_FEATURE_AR_LONG_FILENAMES
|
2001-10-25 19:48:08 +05:30
|
|
|
static char *ar_long_names;
|
2002-09-25 08:17:48 +05:30
|
|
|
static unsigned int ar_long_name_size;
|
|
|
|
#endif
|
2001-10-25 19:48:08 +05:30
|
|
|
|
2006-08-03 21:11:12 +05:30
|
|
|
/* dont use xread as we want to handle the error ourself */
|
2002-09-25 08:17:48 +05:30
|
|
|
if (read(archive_handle->src_fd, ar.raw, 60) != 60) {
|
|
|
|
/* End Of File */
|
2006-11-27 22:19:31 +05:30
|
|
|
return EXIT_FAILURE;
|
2003-07-31 07:23:50 +05:30
|
|
|
}
|
2002-09-25 08:17:48 +05:30
|
|
|
|
2004-04-09 12:29:05 +05:30
|
|
|
/* ar header starts on an even byte (2 byte aligned)
|
|
|
|
* '\n' is used for padding
|
|
|
|
*/
|
2002-09-25 08:17:48 +05:30
|
|
|
if (ar.raw[0] == '\n') {
|
2001-10-25 19:48:08 +05:30
|
|
|
/* fix up the header, we started reading 1 byte too early */
|
|
|
|
memmove(ar.raw, &ar.raw[1], 59);
|
2006-07-16 13:44:35 +05:30
|
|
|
ar.raw[59] = xread_char(archive_handle->src_fd);
|
2002-09-25 08:17:48 +05:30
|
|
|
archive_handle->offset++;
|
2001-10-25 19:48:08 +05:30
|
|
|
}
|
2002-09-25 08:17:48 +05:30
|
|
|
archive_handle->offset += 60;
|
2003-07-31 07:23:50 +05:30
|
|
|
|
2002-09-25 08:17:48 +05:30
|
|
|
/* align the headers based on the header magic */
|
2006-07-21 00:32:24 +05:30
|
|
|
if ((ar.formatted.magic[0] != '`') || (ar.formatted.magic[1] != '\n')) {
|
2006-10-08 18:19:22 +05:30
|
|
|
bb_error_msg_and_die("invalid ar header");
|
2002-09-25 08:17:48 +05:30
|
|
|
}
|
|
|
|
|
2006-10-08 18:19:22 +05:30
|
|
|
typed->mode = xstrtoul(ar.formatted.mode, 8);
|
|
|
|
typed->mtime = xatou(ar.formatted.date);
|
|
|
|
typed->uid = xatou(ar.formatted.uid);
|
|
|
|
typed->gid = xatou(ar.formatted.gid);
|
|
|
|
typed->size = xatoul(ar.formatted.size);
|
2001-10-25 19:48:08 +05:30
|
|
|
|
|
|
|
/* long filenames have '/' as the first character */
|
2006-07-21 00:32:24 +05:30
|
|
|
if (ar.formatted.name[0] == '/') {
|
2002-09-25 08:17:48 +05:30
|
|
|
#ifdef CONFIG_FEATURE_AR_LONG_FILENAMES
|
2006-07-21 00:32:24 +05:30
|
|
|
if (ar.formatted.name[1] == '/') {
|
2001-10-25 19:48:08 +05:30
|
|
|
/* If the second char is a '/' then this entries data section
|
|
|
|
* stores long filename for multiple entries, they are stored
|
|
|
|
* in static variable long_names for use in future entries */
|
2002-09-25 08:17:48 +05:30
|
|
|
ar_long_name_size = typed->size;
|
|
|
|
ar_long_names = xmalloc(ar_long_name_size);
|
2006-07-16 13:44:35 +05:30
|
|
|
xread(archive_handle->src_fd, ar_long_names, ar_long_name_size);
|
2002-09-25 08:17:48 +05:30
|
|
|
archive_handle->offset += ar_long_name_size;
|
2001-10-25 19:48:08 +05:30
|
|
|
/* This ar entries data section only contained filenames for other records
|
|
|
|
* they are stored in the static ar_long_names for future reference */
|
2006-11-27 22:19:55 +05:30
|
|
|
return get_header_ar(archive_handle); /* Return next header */
|
2006-07-21 00:32:24 +05:30
|
|
|
} else if (ar.formatted.name[1] == ' ') {
|
2001-10-25 19:48:08 +05:30
|
|
|
/* This is the index of symbols in the file for compilers */
|
2002-09-25 08:17:48 +05:30
|
|
|
data_skip(archive_handle);
|
2004-01-04 16:36:34 +05:30
|
|
|
archive_handle->offset += typed->size;
|
2006-11-27 22:19:55 +05:30
|
|
|
return get_header_ar(archive_handle); /* Return next header */
|
2001-10-25 19:48:08 +05:30
|
|
|
} else {
|
|
|
|
/* The number after the '/' indicates the offset in the ar data section
|
|
|
|
(saved in variable long_name) that conatains the real filename */
|
2006-07-21 00:32:24 +05:30
|
|
|
const unsigned int long_offset = atoi(&ar.formatted.name[1]);
|
2002-09-25 08:17:48 +05:30
|
|
|
if (long_offset >= ar_long_name_size) {
|
2006-10-14 07:53:43 +05:30
|
|
|
bb_error_msg_and_die("can't resolve long filename");
|
2001-10-25 19:48:08 +05:30
|
|
|
}
|
2006-08-03 21:11:12 +05:30
|
|
|
typed->name = xstrdup(ar_long_names + long_offset);
|
2001-10-25 19:48:08 +05:30
|
|
|
}
|
2002-09-25 08:17:48 +05:30
|
|
|
#else
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_error_msg_and_die("long filenames not supported");
|
2002-09-25 08:17:48 +05:30
|
|
|
#endif
|
2001-10-25 19:48:08 +05:30
|
|
|
} else {
|
|
|
|
/* short filenames */
|
2006-11-24 22:51:44 +05:30
|
|
|
typed->name = xstrndup(ar.formatted.name, 16);
|
2001-10-25 19:48:08 +05:30
|
|
|
}
|
|
|
|
|
2002-09-25 08:17:48 +05:30
|
|
|
typed->name[strcspn(typed->name, " /")] = '\0';
|
|
|
|
|
2002-11-05 05:17:31 +05:30
|
|
|
if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
|
2002-09-25 08:17:48 +05:30
|
|
|
archive_handle->action_header(typed);
|
|
|
|
if (archive_handle->sub_archive) {
|
|
|
|
while (archive_handle->action_data_subarchive(archive_handle->sub_archive) == EXIT_SUCCESS);
|
|
|
|
} else {
|
|
|
|
archive_handle->action_data(archive_handle);
|
|
|
|
}
|
|
|
|
} else {
|
2004-03-15 13:59:22 +05:30
|
|
|
data_skip(archive_handle);
|
2002-09-25 08:17:48 +05:30
|
|
|
}
|
|
|
|
|
2003-07-31 07:23:50 +05:30
|
|
|
archive_handle->offset += typed->size;
|
|
|
|
/* Set the file pointer to the correct spot, we may have been reading a compressed file */
|
|
|
|
lseek(archive_handle->src_fd, archive_handle->offset, SEEK_SET);
|
2001-10-25 19:48:08 +05:30
|
|
|
|
2006-11-27 22:19:31 +05:30
|
|
|
return EXIT_SUCCESS;
|
2002-07-11 16:41:56 +05:30
|
|
|
}
|