2006-07-02 19:47:05 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2006-05-29 07:42:02 +00:00
|
|
|
/* Copyright 2001 Glenn McGrath.
|
2001-10-25 14:18:08 +00:00
|
|
|
*
|
2006-05-29 07:42:02 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2001-10-25 14:18:08 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libbb.h"
|
2006-08-03 15:41:12 +00:00
|
|
|
#include "unarchive.h"
|
2001-10-25 14:18:08 +00:00
|
|
|
|
2006-03-06 20:47:33 +00:00
|
|
|
char get_header_ar(archive_handle_t *archive_handle)
|
2001-10-25 14:18:08 +00:00
|
|
|
{
|
2006-12-26 18:17:42 +00:00
|
|
|
int err;
|
2002-09-25 02:47:48 +00:00
|
|
|
file_header_t *typed = archive_handle->file_header;
|
2001-10-25 14:18:08 +00:00
|
|
|
union {
|
|
|
|
char raw[60];
|
2006-01-25 00:08:53 +00:00
|
|
|
struct {
|
|
|
|
char name[16];
|
|
|
|
char date[12];
|
|
|
|
char uid[6];
|
|
|
|
char gid[6];
|
|
|
|
char mode[8];
|
|
|
|
char size[10];
|
|
|
|
char magic[2];
|
2006-07-20 19:02:24 +00:00
|
|
|
} formatted;
|
2001-10-25 14:18:08 +00:00
|
|
|
} ar;
|
2007-04-07 00:44:31 +00:00
|
|
|
#if ENABLE_FEATURE_AR_LONG_FILENAMES
|
2001-10-25 14:18:08 +00:00
|
|
|
static char *ar_long_names;
|
2007-04-07 00:44:31 +00:00
|
|
|
static unsigned ar_long_name_size;
|
2002-09-25 02:47:48 +00:00
|
|
|
#endif
|
2001-10-25 14:18:08 +00:00
|
|
|
|
2006-08-03 15:41:12 +00:00
|
|
|
/* dont use xread as we want to handle the error ourself */
|
2002-09-25 02:47:48 +00:00
|
|
|
if (read(archive_handle->src_fd, ar.raw, 60) != 60) {
|
|
|
|
/* End Of File */
|
2006-11-27 16:49:31 +00:00
|
|
|
return EXIT_FAILURE;
|
2003-07-31 01:53:50 +00:00
|
|
|
}
|
2002-09-25 02:47:48 +00:00
|
|
|
|
2004-04-09 06:59:05 +00:00
|
|
|
/* ar header starts on an even byte (2 byte aligned)
|
|
|
|
* '\n' is used for padding
|
|
|
|
*/
|
2002-09-25 02:47:48 +00:00
|
|
|
if (ar.raw[0] == '\n') {
|
2001-10-25 14:18:08 +00:00
|
|
|
/* fix up the header, we started reading 1 byte too early */
|
|
|
|
memmove(ar.raw, &ar.raw[1], 59);
|
2006-07-16 08:14:35 +00:00
|
|
|
ar.raw[59] = xread_char(archive_handle->src_fd);
|
2002-09-25 02:47:48 +00:00
|
|
|
archive_handle->offset++;
|
2001-10-25 14:18:08 +00:00
|
|
|
}
|
2002-09-25 02:47:48 +00:00
|
|
|
archive_handle->offset += 60;
|
2003-07-31 01:53:50 +00:00
|
|
|
|
2002-09-25 02:47:48 +00:00
|
|
|
/* align the headers based on the header magic */
|
2006-12-26 18:17:42 +00:00
|
|
|
if (ar.formatted.magic[0] != '`' || ar.formatted.magic[1] != '\n')
|
2006-10-08 12:49:22 +00:00
|
|
|
bb_error_msg_and_die("invalid ar header");
|
2002-09-25 02:47:48 +00:00
|
|
|
|
2006-12-26 18:17:42 +00:00
|
|
|
/* FIXME: more thorough routine would be in order here */
|
|
|
|
/* (we have something like that in tar) */
|
|
|
|
/* but for now we are lax. This code works because */
|
|
|
|
/* on misformatted numbers bb_strtou returns all-ones */
|
|
|
|
typed->mode = err = bb_strtou(ar.formatted.mode, NULL, 8);
|
|
|
|
if (err == -1) bb_error_msg_and_die("invalid ar header");
|
|
|
|
typed->mtime = err = bb_strtou(ar.formatted.date, NULL, 10);
|
|
|
|
if (err == -1) bb_error_msg_and_die("invalid ar header");
|
|
|
|
typed->uid = err = bb_strtou(ar.formatted.uid, NULL, 10);
|
|
|
|
if (err == -1) bb_error_msg_and_die("invalid ar header");
|
|
|
|
typed->gid = err = bb_strtou(ar.formatted.gid, NULL, 10);
|
|
|
|
if (err == -1) bb_error_msg_and_die("invalid ar header");
|
|
|
|
typed->size = err = bb_strtou(ar.formatted.size, NULL, 10);
|
|
|
|
if (err == -1) bb_error_msg_and_die("invalid ar header");
|
2001-10-25 14:18:08 +00:00
|
|
|
|
|
|
|
/* long filenames have '/' as the first character */
|
2006-07-20 19:02:24 +00:00
|
|
|
if (ar.formatted.name[0] == '/') {
|
2007-03-19 20:59:20 +00:00
|
|
|
#if ENABLE_FEATURE_AR_LONG_FILENAMES
|
2007-03-14 22:08:53 +00:00
|
|
|
unsigned long_offset;
|
|
|
|
|
2006-07-20 19:02:24 +00:00
|
|
|
if (ar.formatted.name[1] == '/') {
|
2001-10-25 14:18:08 +00:00
|
|
|
/* 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 02:47:48 +00:00
|
|
|
ar_long_name_size = typed->size;
|
|
|
|
ar_long_names = xmalloc(ar_long_name_size);
|
2006-07-16 08:14:35 +00:00
|
|
|
xread(archive_handle->src_fd, ar_long_names, ar_long_name_size);
|
2002-09-25 02:47:48 +00:00
|
|
|
archive_handle->offset += ar_long_name_size;
|
2001-10-25 14:18:08 +00:00
|
|
|
/* 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 16:49:55 +00:00
|
|
|
return get_header_ar(archive_handle); /* Return next header */
|
2007-03-14 22:08:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ar.formatted.name[1] == ' ') {
|
2001-10-25 14:18:08 +00:00
|
|
|
/* This is the index of symbols in the file for compilers */
|
2002-09-25 02:47:48 +00:00
|
|
|
data_skip(archive_handle);
|
2004-01-04 11:06:34 +00:00
|
|
|
archive_handle->offset += typed->size;
|
2006-11-27 16:49:55 +00:00
|
|
|
return get_header_ar(archive_handle); /* Return next header */
|
2001-10-25 14:18:08 +00:00
|
|
|
}
|
2007-03-14 22:08:53 +00:00
|
|
|
|
|
|
|
/* The number after the '/' indicates the offset in the ar data section
|
|
|
|
* (saved in variable long_name) that conatains the real filename */
|
|
|
|
long_offset = atoi(&ar.formatted.name[1]);
|
|
|
|
if (long_offset >= ar_long_name_size) {
|
|
|
|
bb_error_msg_and_die("can't resolve long filename");
|
|
|
|
}
|
|
|
|
typed->name = xstrdup(ar_long_names + long_offset);
|
2002-09-25 02:47:48 +00:00
|
|
|
#else
|
2003-03-19 09:13:01 +00:00
|
|
|
bb_error_msg_and_die("long filenames not supported");
|
2002-09-25 02:47:48 +00:00
|
|
|
#endif
|
2001-10-25 14:18:08 +00:00
|
|
|
} else {
|
|
|
|
/* short filenames */
|
2006-11-24 17:21:44 +00:00
|
|
|
typed->name = xstrndup(ar.formatted.name, 16);
|
2001-10-25 14:18:08 +00:00
|
|
|
}
|
|
|
|
|
2002-09-25 02:47:48 +00:00
|
|
|
typed->name[strcspn(typed->name, " /")] = '\0';
|
|
|
|
|
2002-11-04 23:47:31 +00:00
|
|
|
if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
|
2002-09-25 02:47:48 +00:00
|
|
|
archive_handle->action_header(typed);
|
|
|
|
if (archive_handle->sub_archive) {
|
2006-12-22 00:21:07 +00:00
|
|
|
while (archive_handle->action_data_subarchive(archive_handle->sub_archive) == EXIT_SUCCESS)
|
|
|
|
/* repeat */;
|
2002-09-25 02:47:48 +00:00
|
|
|
} else {
|
|
|
|
archive_handle->action_data(archive_handle);
|
|
|
|
}
|
|
|
|
} else {
|
2004-03-15 08:29:22 +00:00
|
|
|
data_skip(archive_handle);
|
2002-09-25 02:47:48 +00:00
|
|
|
}
|
|
|
|
|
2003-07-31 01:53:50 +00:00
|
|
|
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 14:18:08 +00:00
|
|
|
|
2006-11-27 16:49:31 +00:00
|
|
|
return EXIT_SUCCESS;
|
2002-07-11 11:11:56 +00:00
|
|
|
}
|