blob: f3ba59b6a4978bddb7a75f4aa0e4503851e6505e [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 *
Eric Andersen86ab8a32000-06-02 03:21:42 +00008 * Based in part on BusyBox tar, Debian dpkg-deb and GNU ar.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
Matt Kraaid9954932000-09-22 03:36:27 +000024 * Last modified 20 September 2000
Eric Andersen86ab8a32000-06-02 03:21:42 +000025 */
Eric Andersen86ab8a32000-06-02 03:21:42 +000026#include <stdio.h>
Glenn L McGrath8324b9f2000-09-09 08:35:45 +000027#include <string.h>
Eric Andersen86ab8a32000-06-02 03:21:42 +000028#include <fcntl.h>
29#include <errno.h>
30#include <ctype.h>
Eric Andersen852ff132000-06-16 04:56:40 +000031#include <time.h>
32#include <utime.h>
Glenn L McGrath8324b9f2000-09-09 08:35:45 +000033#include <unistd.h>
34#include <stdlib.h>
Eric Andersen852ff132000-06-16 04:56:40 +000035#include <sys/types.h>
Glenn L McGrath8324b9f2000-09-09 08:35:45 +000036#include <sys/stat.h>
37#include <malloc.h>
Eric Andersen3570a342000-09-25 21:45:58 +000038#include "busybox.h"
Eric Andersen86ab8a32000-06-02 03:21:42 +000039
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +000040#define BLOCK_SIZE 60
41#define PRESERVE_DATE 1 /* preserve original dates */
42#define VERBOSE 2 /* be verbose */
43#define DISPLAY 4 /* display contents */
44#define EXT_TO_FILE 8 /* extract contents of archive */
45#define EXT_TO_STDOUT 16 /* extract to stdout */
Glenn L McGrathe2b345a2000-09-09 14:50:04 +000046#define RECURSIVE 32
Eric Andersen852ff132000-06-16 04:56:40 +000047
Glenn L McGrath8324b9f2000-09-09 08:35:45 +000048#define MAX_NAME_LENGTH 100
Eric Andersen86ab8a32000-06-02 03:21:42 +000049
Glenn L McGrath8324b9f2000-09-09 08:35:45 +000050//#define BB_DECLARE_EXTERN
51//#define bb_need_io_error
52//#include "messages.c"
53
Glenn L McGrathfca80502000-09-11 05:25:39 +000054//#define BB_AR_EXPERIMENTAL_UNTAR
55
56#if defined BB_AR_EXPERIMENTAL_UNTAR
57typedef struct rawTarHeader {
58 char name[100]; /* 0-99 */
59 char mode[8]; /* 100-107 */
60 char uid[8]; /* 108-115 */
61 char gid[8]; /* 116-123 */
62 char size[12]; /* 124-135 */
63 char mtime[12]; /* 136-147 */
64 char chksum[8]; /* 148-155 */
65 char typeflag; /* 156-156 */
66 char linkname[100]; /* 157-256 */
67 char magic[6]; /* 257-262 */
68 char version[2]; /* 263-264 */
69 char uname[32]; /* 265-296 */
70 char gname[32]; /* 297-328 */
71 char devmajor[8]; /* 329-336 */
72 char devminor[8]; /* 337-344 */
73 char prefix[155]; /* 345-499 */
74 char padding[12]; /* 500-512 */
75} rawTarHeader_t;
76#endif
77
Glenn L McGrath8324b9f2000-09-09 08:35:45 +000078typedef struct rawArHeader { /* Byte Offset */
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +000079 char name[16]; /* 0-15 */
80 char date[12]; /* 16-27 */
81 char uid[6], gid[6]; /* 28-39 */
82 char mode[8]; /* 40-47 */
83 char size[10]; /* 48-57 */
84 char fmag[2]; /* 58-59 */
85} rawArHeader_t;
Eric Andersen86ab8a32000-06-02 03:21:42 +000086
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +000087typedef struct headerL {
Glenn L McGrath8324b9f2000-09-09 08:35:45 +000088 char name[MAX_NAME_LENGTH];
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +000089 size_t size;
90 uid_t uid;
91 gid_t gid;
92 mode_t mode;
93 time_t mtime;
94 off_t offset;
95 struct headerL *next;
96} headerL_t;
Eric Andersen86ab8a32000-06-02 03:21:42 +000097
Glenn L McGrathfca80502000-09-11 05:25:39 +000098#if defined BB_AR_EXPERIMENTAL_UNTAR
Eric Andersen852ff132000-06-16 04:56:40 +000099/*
Glenn L McGrathfca80502000-09-11 05:25:39 +0000100 * identify Tar header (magic field) and reset srcFd to entry position
101 */
102static int checkTarMagic(int srcFd)
103{
104 off_t headerStart;
105 char magic[6];
106
107 headerStart = lseek(srcFd, 0, SEEK_CUR);
108 lseek(srcFd, (off_t) 257, SEEK_CUR);
109 fullRead(srcFd, magic, 6);
110 lseek(srcFd, headerStart, SEEK_SET);
111 if (strncmp(magic, "ustar", 5)!=0)
112 return(FALSE);
113 return(TRUE);
114}
115
116
117static int readTarHeader(int srcFd, headerL_t *current)
118{
119 rawTarHeader_t rawTarHeader;
120 unsigned char *temp = (unsigned char *) &rawTarHeader;
121 long sum = 0;
122 int i;
123 off_t initialOffset;
124
125 initialOffset = lseek(srcFd, 0, SEEK_CUR);
126 if (fullRead(srcFd, (char *) &rawTarHeader, 512) != 512) {
127 lseek(srcFd, initialOffset, SEEK_SET);
128 return(FALSE);
129 }
130 for (i = 0; i < 148 ; i++)
131 sum += temp[i];
132 sum += ' ' * 8;
133 for (i = 156; i < 512 ; i++)
134 sum += temp[i];
135 if (sum!= strtol(rawTarHeader.chksum, NULL, 8))
136 return(FALSE);
137 sscanf(rawTarHeader.name, "%s", current->name);
138 current->size = strtol(rawTarHeader.size, NULL, 8);
139 current->uid = strtol(rawTarHeader.uid, NULL, 8);
140 current->gid = strtol(rawTarHeader.gid, NULL, 8);
141 current->mode = strtol(rawTarHeader.mode, NULL, 8);
142 current->mtime = strtol(rawTarHeader.mtime, NULL, 8);
143 current->offset = lseek(srcFd, 0 , SEEK_CUR);
144
145 current->next = (headerL_t *) xmalloc(sizeof(headerL_t));
146 current = current->next;
147 return(TRUE);
148}
149#endif
150
151/*
152 * identify Ar header (magic) and reset srcFd to entry position
Eric Andersen852ff132000-06-16 04:56:40 +0000153 */
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000154static int checkArMagic(int srcFd)
Eric Andersen86ab8a32000-06-02 03:21:42 +0000155{
Glenn L McGrathfca80502000-09-11 05:25:39 +0000156 off_t headerStart;
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000157 char arMagic[8];
Glenn L McGrathfca80502000-09-11 05:25:39 +0000158
159 headerStart = lseek(srcFd, 0, SEEK_CUR);
160 if (fullRead(srcFd, arMagic, 8) != 8) {
Matt Kraaid9954932000-09-22 03:36:27 +0000161 errorMsg("fatal error\n");
Eric Andersen852ff132000-06-16 04:56:40 +0000162 return (FALSE);
Glenn L McGrathfca80502000-09-11 05:25:39 +0000163 }
164 lseek(srcFd, headerStart, SEEK_SET);
165
166 if (strncmp(arMagic,"!<arch>",7) != 0)
Eric Andersen852ff132000-06-16 04:56:40 +0000167 return(FALSE);
Glenn L McGrathfca80502000-09-11 05:25:39 +0000168 return(TRUE);
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000169}
170
171/*
172 * get, check and correct the converted header
173 */
Glenn L McGrathfca80502000-09-11 05:25:39 +0000174static int readArEntry(int srcFd, headerL_t *entry)
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000175{
176 size_t nameLength;
Glenn L McGrathfca80502000-09-11 05:25:39 +0000177 rawArHeader_t rawArHeader;
178 off_t initialOffset;
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000179
Glenn L McGrathfca80502000-09-11 05:25:39 +0000180 initialOffset = lseek(srcFd, 0, SEEK_CUR);
181 if (fullRead(srcFd, (char *) &rawArHeader, 60) != 60) {
182 lseek(srcFd, initialOffset, SEEK_SET);
183 return(FALSE);
184 }
185 if ((rawArHeader.fmag[0]!='`') || (rawArHeader.fmag[1]!='\n')) {
186 lseek(srcFd, initialOffset, SEEK_SET);
187 return(FALSE);
188 }
189
190 strncpy(entry->name, rawArHeader.name, 16);
191 nameLength=strcspn(entry->name, " \\");
192 entry->name[nameLength]='\0';
193 parse_mode(rawArHeader.mode, &entry->mode);
194 entry->mtime = atoi(rawArHeader.date);
195 entry->uid = atoi(rawArHeader.uid);
196 entry->gid = atoi(rawArHeader.gid);
197 entry->size = (size_t) atoi(rawArHeader.size);
198 entry->offset = initialOffset + (off_t) 60;
199
200 nameLength = strcspn(entry->name, "/");
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000201
202 /* handle GNU style short filenames, strip trailing '/' */
203 if (nameLength > 0)
Glenn L McGrathfca80502000-09-11 05:25:39 +0000204 entry->name[nameLength]='\0';
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000205
206 /* handle GNU style long filenames */
207 if (nameLength == 0) {
208 /* escape from recursive call */
Glenn L McGrathfca80502000-09-11 05:25:39 +0000209 if (entry->name[1]=='0')
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000210 return(TRUE);
211
212 /* the data section contains the real filename */
Glenn L McGrathfca80502000-09-11 05:25:39 +0000213 if (entry->name[1]=='/') {
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000214 char tempName[MAX_NAME_LENGTH];
215
Glenn L McGrathfca80502000-09-11 05:25:39 +0000216 if (entry->size > MAX_NAME_LENGTH)
217 entry->size = MAX_NAME_LENGTH;
218 fullRead(srcFd, tempName, entry->size);
219 tempName[entry->size-3]='\0';
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000220
221 /* read the second header for this entry */
222 /* be carefull, this is recursive */
Glenn L McGrathfca80502000-09-11 05:25:39 +0000223 if (readArEntry(srcFd, entry)==FALSE)
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000224 return(FALSE);
225
Glenn L McGrathfca80502000-09-11 05:25:39 +0000226 if ((entry->name[0]='/') && (entry->name[1]='0'))
227 strcpy(entry->name, tempName);
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000228 else {
229 errorMsg("Invalid long filename\n");
230 return(FALSE);
231 }
232 }
233 }
234 return(TRUE);
Eric Andersen852ff132000-06-16 04:56:40 +0000235}
236
237/*
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000238 * return the headerL_t struct for the specified filename
Eric Andersen852ff132000-06-16 04:56:40 +0000239 */
Glenn L McGrathe2b345a2000-09-09 14:50:04 +0000240static headerL_t *getHeaders(int srcFd, headerL_t *head, int funct)
Eric Andersen852ff132000-06-16 04:56:40 +0000241{
Glenn L McGrathfca80502000-09-11 05:25:39 +0000242#if defined BB_AR_EXPERIMENTAL_UNTAR
243 int tar=FALSE;
244#endif
245 int ar=FALSE;
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000246 headerL_t *list;
Glenn L McGrathfca80502000-09-11 05:25:39 +0000247 off_t initialOffset;
Eric Andersen86ab8a32000-06-02 03:21:42 +0000248
Matt Kraai322ae932000-09-13 02:46:14 +0000249 list = (headerL_t *) xmalloc(sizeof(headerL_t));
Glenn L McGrathfca80502000-09-11 05:25:39 +0000250 initialOffset=lseek(srcFd, 0, SEEK_CUR);
251 if (checkArMagic(srcFd)==TRUE)
252 ar=TRUE;
253
254#if defined BB_AR_EXPERIMENTAL_UNTAR
255 if (checkTarMagic(srcFd)==TRUE)
256 tar=TRUE;
257
258 if (tar==TRUE) {
259 while(readTarHeader(srcFd, list)==TRUE) {
260 off_t tarOffset;
Matt Kraai322ae932000-09-13 02:46:14 +0000261 list->next = (headerL_t *) xmalloc(sizeof(headerL_t));
Glenn L McGrathfca80502000-09-11 05:25:39 +0000262 *list->next = *head;
263 *head = *list;
264
265 /* recursive check for sub-archives */
266 if ((funct & RECURSIVE) == RECURSIVE)
267 head = getHeaders(srcFd, head, funct);
268 tarOffset = (off_t) head->size/512;
269 if ( head->size % 512 > 0)
270 tarOffset++;
271 tarOffset=tarOffset*512;
272 lseek(srcFd, head->offset + tarOffset, SEEK_SET);
273 }
274 }
275#endif
276
277 if (ar==TRUE) {
278 lseek(srcFd, 8, SEEK_CUR);
279 while(1) {
280 if (readArEntry(srcFd, list) == FALSE) {
281 lseek(srcFd, ++initialOffset, SEEK_CUR);
282 if (readArEntry(srcFd, list) == FALSE)
283 return(head);
284 }
Matt Kraai322ae932000-09-13 02:46:14 +0000285 list->next = (headerL_t *) xmalloc(sizeof(headerL_t));
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000286 *list->next = *head;
287 *head = *list;
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000288 /* recursive check for sub-archives */
Glenn L McGrathfca80502000-09-11 05:25:39 +0000289 if (funct & RECURSIVE)
Glenn L McGrathe2b345a2000-09-09 14:50:04 +0000290 head = getHeaders(srcFd, head, funct);
291 lseek(srcFd, head->offset + head->size, SEEK_SET);
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000292 }
293 }
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000294 return(head);
295}
296
297/*
298 * find an entry in the linked list matching the filename
299 */
300static headerL_t *findEntry(headerL_t *head, const char *filename)
301{
302 while(head->next != NULL) {
303 if (strcmp(filename, head->name)==0)
304 return(head);
305 head=head->next;
306 }
307 return(NULL);
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000308}
Eric Andersen852ff132000-06-16 04:56:40 +0000309
Eric Andersen86ab8a32000-06-02 03:21:42 +0000310extern int ar_main(int argc, char **argv)
311{
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000312 int funct = 0, opt=0;
313 int srcFd=0, dstFd=0;
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000314 headerL_t *header, *entry, *extractList;
315
Glenn L McGrathe2b345a2000-09-09 14:50:04 +0000316 while ((opt = getopt(argc, argv, "ovtpxR")) != -1) {
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000317 switch (opt) {
Eric Andersen57ebebf2000-07-05 17:21:58 +0000318 case 'o':
Eric Andersen49352ad2000-09-11 04:55:31 +0000319 funct |= PRESERVE_DATE;
Eric Andersen86ab8a32000-06-02 03:21:42 +0000320 break;
Eric Andersen57ebebf2000-07-05 17:21:58 +0000321 case 'v':
Eric Andersen49352ad2000-09-11 04:55:31 +0000322 funct |= VERBOSE;
Eric Andersen86ab8a32000-06-02 03:21:42 +0000323 break;
Eric Andersen57ebebf2000-07-05 17:21:58 +0000324 case 't':
Eric Andersen49352ad2000-09-11 04:55:31 +0000325 funct |= DISPLAY;
Glenn L McGrath437bf722000-09-09 13:38:26 +0000326 break;
Eric Andersen57ebebf2000-07-05 17:21:58 +0000327 case 'x':
Eric Andersen49352ad2000-09-11 04:55:31 +0000328 funct |= EXT_TO_FILE;
Glenn L McGrath437bf722000-09-09 13:38:26 +0000329 break;
Eric Andersen57ebebf2000-07-05 17:21:58 +0000330 case 'p':
Eric Andersen49352ad2000-09-11 04:55:31 +0000331 funct |= EXT_TO_STDOUT;
Eric Andersen86ab8a32000-06-02 03:21:42 +0000332 break;
Glenn L McGrathe2b345a2000-09-09 14:50:04 +0000333 case 'R':
Eric Andersen49352ad2000-09-11 04:55:31 +0000334 funct |= RECURSIVE;
Glenn L McGrathe2b345a2000-09-09 14:50:04 +0000335 break;
Eric Andersen86ab8a32000-06-02 03:21:42 +0000336 default:
337 usage(ar_usage);
338 }
339 }
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000340
Matt Kraaid9954932000-09-22 03:36:27 +0000341 /* check the src filename was specified */
342 if (optind == argc)
343 usage(ar_usage);
Eric Andersen852ff132000-06-16 04:56:40 +0000344
Matt Kraaid9954932000-09-22 03:36:27 +0000345 if ( (srcFd = open(argv[optind], O_RDONLY)) < 0)
346 fatalError("Cannot read %s\n", argv[optind]);
347
Glenn L McGrath437bf722000-09-09 13:38:26 +0000348 optind++;
Matt Kraai322ae932000-09-13 02:46:14 +0000349 entry = (headerL_t *) xmalloc(sizeof(headerL_t));
350 header = (headerL_t *) xmalloc(sizeof(headerL_t));
351 extractList = (headerL_t *) xmalloc(sizeof(headerL_t));
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000352
Glenn L McGrathe2b345a2000-09-09 14:50:04 +0000353 header = getHeaders(srcFd, header, funct);
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000354 /* find files to extract or display */
355 if (optind<argc) {
356 /* only handle specified files */
357 while(optind < argc) {
358 if ( (entry = findEntry(header, argv[optind])) != NULL) {
Matt Kraai322ae932000-09-13 02:46:14 +0000359 entry->next = (headerL_t *) xmalloc(sizeof(headerL_t));
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000360 *entry->next = *extractList;
361 *extractList = *entry;
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000362 }
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000363 optind++;
364 }
365 }
Glenn L McGrathfca80502000-09-11 05:25:39 +0000366 else
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000367 extractList = header;
368
Glenn L McGrathfca80502000-09-11 05:25:39 +0000369 while(extractList->next != NULL) {
370 if (funct & EXT_TO_FILE) {
371 if (isDirectory(extractList->name, TRUE, NULL)==FALSE)
372 createPath(extractList->name, 0666);
Glenn L McGrath6fb88e72000-09-09 12:48:40 +0000373 dstFd = open(extractList->name, O_WRONLY | O_CREAT, extractList->mode);
Glenn L McGrathfca80502000-09-11 05:25:39 +0000374 lseek(srcFd, extractList->offset, SEEK_SET);
375 copySubFile(srcFd, dstFd, (size_t) extractList->size);
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000376 }
Glenn L McGrathfca80502000-09-11 05:25:39 +0000377 if (funct & EXT_TO_STDOUT) {
378 lseek(srcFd, extractList->offset, SEEK_SET);
379 copySubFile(srcFd, fileno(stdout), (size_t) extractList->size);
380 }
381 if ( (funct & DISPLAY) || (funct & VERBOSE)) {
382 if (funct & VERBOSE)
383 printf("%s %d/%d %8d %s ", modeString(extractList->mode),
384 extractList->uid, extractList->gid,
385 extractList->size, timeString(extractList->mtime));
386 printf("%s\n", extractList->name);
387 }
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000388 extractList=extractList->next;
389 }
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000390 return (TRUE);
Eric Andersen86ab8a32000-06-02 03:21:42 +0000391}