blob: 1c598fa164654feea3e3d0104d34d99affb57545 [file] [log] [blame]
Eric Andersen86ab8a32000-06-02 03:21:42 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini ar implementation for busybox
4 *
5 * Copyright (C) 2000 by Glenn McGrath
6 * Written by Glenn McGrath <bug1@netconnect.com.au> 1 June 2000
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +00007 *
8 * Modified 8 August 2000 by Glenn McGrath
9 * - now uses getopt
10 * - moved copySubFile function to utilities.c
11 * - creates linked list of all headers
12 * - easily accessable to other busybox functions
Eric Andersen86ab8a32000-06-02 03:21:42 +000013 *
14 * Based in part on BusyBox tar, Debian dpkg-deb and GNU ar.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +000030 * Last modified 25 August 2000
Eric Andersen86ab8a32000-06-02 03:21:42 +000031 */
Eric Andersen86ab8a32000-06-02 03:21:42 +000032#include <stdio.h>
33#include <fcntl.h>
34#include <errno.h>
35#include <ctype.h>
Eric Andersen852ff132000-06-16 04:56:40 +000036#include <time.h>
37#include <utime.h>
38#include <sys/types.h>
Eric Andersen86ab8a32000-06-02 03:21:42 +000039#include "internal.h"
40
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +000041#define BLOCK_SIZE 60
42#define PRESERVE_DATE 1 /* preserve original dates */
43#define VERBOSE 2 /* be verbose */
44#define DISPLAY 4 /* display contents */
45#define EXT_TO_FILE 8 /* extract contents of archive */
46#define EXT_TO_STDOUT 16 /* extract to stdout */
Eric Andersen852ff132000-06-16 04:56:40 +000047
Eric Andersen86ab8a32000-06-02 03:21:42 +000048#define BB_DECLARE_EXTERN
49#define bb_need_io_error
50#include "messages.c"
51
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +000052typedef struct rawArHeader { /* Byte Offset */
53 char name[16]; /* 0-15 */
54 char date[12]; /* 16-27 */
55 char uid[6], gid[6]; /* 28-39 */
56 char mode[8]; /* 40-47 */
57 char size[10]; /* 48-57 */
58 char fmag[2]; /* 58-59 */
59} rawArHeader_t;
Eric Andersen86ab8a32000-06-02 03:21:42 +000060
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +000061typedef struct headerL {
62 char name[16];
63 size_t size;
64 uid_t uid;
65 gid_t gid;
66 mode_t mode;
67 time_t mtime;
68 off_t offset;
69 struct headerL *next;
70} headerL_t;
Eric Andersen86ab8a32000-06-02 03:21:42 +000071
Eric Andersen852ff132000-06-16 04:56:40 +000072/*
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +000073 * populate linked list with all ar file entries and offset
Eric Andersen852ff132000-06-16 04:56:40 +000074 */
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +000075static int parseArArchive(int srcFd, headerL_t *current)
Eric Andersen86ab8a32000-06-02 03:21:42 +000076{
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +000077 off_t lastOffset=0, thisOffset=0;
Eric Andersen852ff132000-06-16 04:56:40 +000078 char arVersion[8];
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +000079 rawArHeader_t rawArHeader;
80
81 lseek(srcFd, 0, SEEK_SET);
82 if (fullRead(srcFd, arVersion, 8) <= 0) {
83 errorMsg("Invalid header magic\n");
Eric Andersen852ff132000-06-16 04:56:40 +000084 return (FALSE);
85 }
86 if (strncmp(arVersion,"!<arch>",7) != 0) {
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +000087 errorMsg("This doesnt appear to be an ar archive\n");
Eric Andersen852ff132000-06-16 04:56:40 +000088 return(FALSE);
89 }
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +000090 while (fullRead(srcFd, (char *) &rawArHeader, 60) == 60) {
91 if ( (rawArHeader.fmag[0] == '`') &&
92 (rawArHeader.fmag[1] == '\n')) {
93 sscanf(rawArHeader.name, "%s", current->name);
94 parse_mode(rawArHeader.mode, &current->mode);
95 current->mtime = atoi(rawArHeader.date);
96 current->uid = atoi(rawArHeader.uid);
97 current->gid = atoi(rawArHeader.gid);
98 current->size = (size_t) atoi(rawArHeader.size);
99 current->offset = lseek(srcFd, 0 , SEEK_CUR);
100 current->next = (headerL_t *) xmalloc(sizeof(headerL_t));
101 lastOffset = lseek(srcFd, (off_t) current->size, SEEK_CUR);
102 current = current->next;
103 }
104 else { /* GNU ar has an extra char after data */
105 thisOffset=lseek(srcFd, 0, SEEK_CUR);
106 if ( (thisOffset - lastOffset) > ((off_t) 61) )
107 return(FALSE);
108 lseek(srcFd, thisOffset - 59, SEEK_SET);
109 }
110 }
111 return(FALSE);
Eric Andersen852ff132000-06-16 04:56:40 +0000112}
113
114/*
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000115 * return the headerL_t struct for the specified filename
Eric Andersen852ff132000-06-16 04:56:40 +0000116 */
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000117static headerL_t *getSubFileHeader(int srcFd, const char filename[16])
Eric Andersen852ff132000-06-16 04:56:40 +0000118{
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000119 headerL_t *list;
120 list = xmalloc(sizeof(headerL_t));
Eric Andersen86ab8a32000-06-02 03:21:42 +0000121
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000122 parseArArchive(srcFd, list);
123 while (list->next != NULL) {
124 if (strncmp(list->name, filename, strlen(filename))==0)
125 return(list);
126 list=list->next;
127 }
128 return(NULL);
129}
Eric Andersen852ff132000-06-16 04:56:40 +0000130
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000131/*
132 * populate linked list with all ar file entries and offset
133 */
134static int displayEntry(int srcFd, const char filename[16], int funct)
135{
136 headerL_t *file;
Eric Andersen86ab8a32000-06-02 03:21:42 +0000137
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000138 if ((file = getSubFileHeader(srcFd, filename)) == NULL)
139 return(FALSE);
140 if ((funct & VERBOSE) == VERBOSE) {
141 printf("%s %d/%d %8d %s ", modeString(file->mode), file->uid, file->gid, file->size, timeString(file->mtime));
142 }
143 printf("%s\n", file->name);
144 return(TRUE);
145}
Eric Andersen852ff132000-06-16 04:56:40 +0000146
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000147static int extractAr(int srcFd, int dstFd, const char filename[16])
148{
149 headerL_t *file;
150
151 if ( (file = getSubFileHeader(srcFd, filename)) == NULL)
152 return(FALSE);
153 lseek(srcFd, file->offset, SEEK_SET);
154 if (copySubFile(srcFd, dstFd, (size_t) file->size) == TRUE)
155 return(TRUE);
156 return(FALSE);
Eric Andersen86ab8a32000-06-02 03:21:42 +0000157}
158
159extern int ar_main(int argc, char **argv)
160{
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000161 int funct = 0, opt=0;
162 int srcFd=0, dstFd=0;
163
164 while ((opt = getopt(argc, argv, "ovt:p:x:")) != -1) {
165 switch (opt) {
Eric Andersen57ebebf2000-07-05 17:21:58 +0000166 case 'o':
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000167 funct = funct | PRESERVE_DATE;
Eric Andersen86ab8a32000-06-02 03:21:42 +0000168 break;
Eric Andersen57ebebf2000-07-05 17:21:58 +0000169 case 'v':
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000170 funct = funct | VERBOSE;
Eric Andersen86ab8a32000-06-02 03:21:42 +0000171 break;
Eric Andersen57ebebf2000-07-05 17:21:58 +0000172 case 't':
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000173 funct = funct | DISPLAY;
Eric Andersen57ebebf2000-07-05 17:21:58 +0000174 case 'x':
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000175 if (opt=='x') {
176 funct = funct | EXT_TO_FILE;
177 }
Eric Andersen57ebebf2000-07-05 17:21:58 +0000178 case 'p':
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000179 if (opt=='p') {
180 funct = funct | EXT_TO_STDOUT;
181 }
182 /* following is common to 't','x' and 'p' */
183 if (optarg == NULL) {
184 printf("expected a filename\n");
185 return(FALSE);
186 }
187 if ( (srcFd = open(optarg, O_RDONLY)) < 0) {
188 errorMsg("Cannot read %s\n", optarg);
189 return (FALSE);
190 }
Eric Andersen86ab8a32000-06-02 03:21:42 +0000191 break;
192 default:
193 usage(ar_usage);
194 }
195 }
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000196
197 /* check options not just preserve_dates and/or verbose */
198 if (funct < 4) {
199 usage(ar_usage);
200 return(FALSE);
201 }
Eric Andersen852ff132000-06-16 04:56:40 +0000202
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000203 /* find files to extract */
204 if (optind<argc)
205 for(; optind < argc; optind++) {
206 if ( (funct & EXT_TO_FILE) == EXT_TO_FILE) {
207 dstFd = open(argv[optind], O_WRONLY | O_CREAT);
208 extractAr(srcFd, dstFd, argv[optind]);
209 }
210 if ( (funct & EXT_TO_STDOUT) == EXT_TO_STDOUT)
211 extractAr(srcFd, fileno(stdout), argv[optind]);
212 if ( (funct & DISPLAY) == DISPLAY)
213 displayEntry(srcFd, argv[optind], funct);
214 }
215 return (TRUE);
Eric Andersen86ab8a32000-06-02 03:21:42 +0000216}