blob: eaa15a516b44e2e2bc1a7249154cc46dadcb95fb [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 *
Glenn L McGrath8324b9f2000-09-09 08:35:45 +000024 * Last modified 9 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 Andersen86ab8a32000-06-02 03:21:42 +000038#include "internal.h"
39
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 */
Eric Andersen852ff132000-06-16 04:56:40 +000046
Glenn L McGrath8324b9f2000-09-09 08:35:45 +000047#define MAX_NAME_LENGTH 100
Eric Andersen86ab8a32000-06-02 03:21:42 +000048
Glenn L McGrath8324b9f2000-09-09 08:35:45 +000049//#define BB_DECLARE_EXTERN
50//#define bb_need_io_error
51//#include "messages.c"
52
53typedef struct rawArHeader { /* Byte Offset */
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +000054 char name[16]; /* 0-15 */
55 char date[12]; /* 16-27 */
56 char uid[6], gid[6]; /* 28-39 */
57 char mode[8]; /* 40-47 */
58 char size[10]; /* 48-57 */
59 char fmag[2]; /* 58-59 */
60} rawArHeader_t;
Eric Andersen86ab8a32000-06-02 03:21:42 +000061
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +000062typedef struct headerL {
Glenn L McGrath8324b9f2000-09-09 08:35:45 +000063 char name[MAX_NAME_LENGTH];
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +000064 size_t size;
65 uid_t uid;
66 gid_t gid;
67 mode_t mode;
68 time_t mtime;
69 off_t offset;
70 struct headerL *next;
71} headerL_t;
Eric Andersen86ab8a32000-06-02 03:21:42 +000072
Eric Andersen852ff132000-06-16 04:56:40 +000073/*
Glenn L McGrath8324b9f2000-09-09 08:35:45 +000074 * identify Ar header (magic) and set srcFd to first header entry
Eric Andersen852ff132000-06-16 04:56:40 +000075 */
Glenn L McGrath8324b9f2000-09-09 08:35:45 +000076static int checkArMagic(int srcFd)
Eric Andersen86ab8a32000-06-02 03:21:42 +000077{
Glenn L McGrath8324b9f2000-09-09 08:35:45 +000078 char arMagic[8];
79 if (fullRead(srcFd, arMagic, 8) != 8)
Eric Andersen852ff132000-06-16 04:56:40 +000080 return (FALSE);
Glenn L McGrath8324b9f2000-09-09 08:35:45 +000081
82 if (strncmp(arMagic,"!<arch>",7) != 0)
Eric Andersen852ff132000-06-16 04:56:40 +000083 return(FALSE);
Glenn L McGrath8324b9f2000-09-09 08:35:45 +000084 return(TRUE);
85}
86
87/*
88 * read, convert and check the raw ar header
89 * srcFd should be pointing to the start of header prior to entry
90 * srcFd will be pointing at the start of data after successful exit
91 * if returns FALSE srcFd is reset to initial position
92 */
93static int readRawArHeader(int srcFd, headerL_t *header)
94{
95 rawArHeader_t rawArHeader;
96 off_t initialOffset;
97 size_t nameLength;
98
99 initialOffset = lseek(srcFd, 0, SEEK_CUR);
100 if (fullRead(srcFd, (char *) &rawArHeader, 60) != 60) {
101 lseek(srcFd, initialOffset, SEEK_SET);
102 return(FALSE);
103 }
104 if ((rawArHeader.fmag[0]!='`') || (rawArHeader.fmag[1]!='\n')) {
105 lseek(srcFd, initialOffset, SEEK_SET);
106 return(FALSE);
107 }
108
109 strncpy(header->name, rawArHeader.name, 16);
110 nameLength=strcspn(header->name, " \\");
111 header->name[nameLength]='\0';
112 parse_mode(rawArHeader.mode, &header->mode);
113 header->mtime = atoi(rawArHeader.date);
114 header->uid = atoi(rawArHeader.uid);
115 header->gid = atoi(rawArHeader.gid);
116 header->size = (size_t) atoi(rawArHeader.size);
117 header->offset = initialOffset + (off_t) 60;
118 return(TRUE);
119}
120
121/*
122 * get, check and correct the converted header
123 */
124static int readArEntry(int srcFd, headerL_t *newEntry)
125{
126 size_t nameLength;
127
128 if(readRawArHeader(srcFd, newEntry)==FALSE)
129 return(FALSE);
130
131 nameLength = strcspn(newEntry->name, "/");
132
133 /* handle GNU style short filenames, strip trailing '/' */
134 if (nameLength > 0)
135 newEntry->name[nameLength]='\0';
136
137 /* handle GNU style long filenames */
138 if (nameLength == 0) {
139 /* escape from recursive call */
140 if (newEntry->name[1]=='0')
141 return(TRUE);
142
143 /* the data section contains the real filename */
144 if (newEntry->name[1]=='/') {
145 char tempName[MAX_NAME_LENGTH];
146
147 if (newEntry->size > MAX_NAME_LENGTH)
148 newEntry->size = MAX_NAME_LENGTH;
149 fullRead(srcFd, tempName, newEntry->size);
150 tempName[newEntry->size-3]='\0';
151
152 /* read the second header for this entry */
153 /* be carefull, this is recursive */
154 if (readArEntry(srcFd, newEntry)==FALSE)
155 return(FALSE);
156
157 if ((newEntry->name[0]='/') && (newEntry->name[1]='0'))
158 strcpy(newEntry->name, tempName);
159 else {
160 errorMsg("Invalid long filename\n");
161 return(FALSE);
162 }
163 }
164 }
165 return(TRUE);
Eric Andersen852ff132000-06-16 04:56:40 +0000166}
167
168/*
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000169 * return the headerL_t struct for the specified filename
Eric Andersen852ff132000-06-16 04:56:40 +0000170 */
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000171static headerL_t *getHeaders(int srcFd, headerL_t *head)
Eric Andersen852ff132000-06-16 04:56:40 +0000172{
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000173 int arEntry=FALSE;
174 headerL_t *list;
175 list = (headerL_t *) malloc(sizeof(headerL_t));
Eric Andersen86ab8a32000-06-02 03:21:42 +0000176
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000177 if (checkArMagic(srcFd)==TRUE)
178 arEntry=TRUE;
179 else
180 errorMsg("isnt an ar archive\n");
181
182 if (arEntry==TRUE) {
183 while(readArEntry(srcFd, list) == TRUE) {
184 list->next = (headerL_t *) malloc(sizeof(headerL_t));
185 *list->next = *head;
186 *head = *list;
187
188 /* recursive check for sub-archives */
189 lseek(srcFd, list->size, SEEK_CUR);
190/* printf("checking for sub headers\n");
191 if ((subList = getHeaders(srcFd, list->next)) != NULL) {
192 printf("found a sub archive !\n");
193 }
194 else
195 printf("didnt find a sub header\n"); */
196 }
197 }
198
199 return(head);
200}
201
202/*
203 * find an entry in the linked list matching the filename
204 */
205static headerL_t *findEntry(headerL_t *head, const char *filename)
206{
207 while(head->next != NULL) {
208 if (strcmp(filename, head->name)==0)
209 return(head);
210 head=head->next;
211 }
212 return(NULL);
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000213}
Eric Andersen852ff132000-06-16 04:56:40 +0000214
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000215/*
216 * populate linked list with all ar file entries and offset
217 */
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000218static int displayEntry(headerL_t *head, int funct)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000219{
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000220 if ((funct & VERBOSE) == VERBOSE) {
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000221 printf("%s %d/%d %8d %s ", modeString(head->mode), head->uid, head->gid, head->size, timeString(head->mtime));
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000222 }
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000223 printf("%s\n", head->name);
224 head = head->next;
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000225 return(TRUE);
226}
Eric Andersen852ff132000-06-16 04:56:40 +0000227
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000228static int extractAr(int srcFd, int dstFd, headerL_t *file)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000229{
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000230 lseek(srcFd, file->offset, SEEK_SET);
231 if (copySubFile(srcFd, dstFd, (size_t) file->size) == TRUE)
232 return(TRUE);
233 return(FALSE);
Eric Andersen86ab8a32000-06-02 03:21:42 +0000234}
235
236extern int ar_main(int argc, char **argv)
237{
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000238 int funct = 0, opt=0;
239 int srcFd=0, dstFd=0;
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000240 headerL_t *header, *entry, *extractList;
241
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000242 while ((opt = getopt(argc, argv, "ovt:p:x:")) != -1) {
243 switch (opt) {
Eric Andersen57ebebf2000-07-05 17:21:58 +0000244 case 'o':
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000245 funct = funct | PRESERVE_DATE;
Eric Andersen86ab8a32000-06-02 03:21:42 +0000246 break;
Eric Andersen57ebebf2000-07-05 17:21:58 +0000247 case 'v':
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000248 funct = funct | VERBOSE;
Eric Andersen86ab8a32000-06-02 03:21:42 +0000249 break;
Eric Andersen57ebebf2000-07-05 17:21:58 +0000250 case 't':
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000251 funct = funct | DISPLAY;
Eric Andersen57ebebf2000-07-05 17:21:58 +0000252 case 'x':
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000253 if (opt=='x') {
254 funct = funct | EXT_TO_FILE;
255 }
Eric Andersen57ebebf2000-07-05 17:21:58 +0000256 case 'p':
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000257 if (opt=='p') {
258 funct = funct | EXT_TO_STDOUT;
259 }
260 /* following is common to 't','x' and 'p' */
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000261 if ( (srcFd = open(optarg, O_RDONLY)) < 0) {
262 errorMsg("Cannot read %s\n", optarg);
263 return (FALSE);
264 }
Eric Andersen86ab8a32000-06-02 03:21:42 +0000265 break;
266 default:
267 usage(ar_usage);
268 }
269 }
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000270
271 /* check options not just preserve_dates and/or verbose */
272 if (funct < 4) {
273 usage(ar_usage);
274 return(FALSE);
275 }
Eric Andersen852ff132000-06-16 04:56:40 +0000276
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000277 entry = (headerL_t *) malloc(sizeof(headerL_t));
278 header = (headerL_t *) malloc(sizeof(headerL_t));
279 extractList = (headerL_t *) malloc(sizeof(headerL_t));
280
281 header = getHeaders(srcFd, header);
282
283 /* find files to extract or display */
284 if (optind<argc) {
285 /* only handle specified files */
286 while(optind < argc) {
287 if ( (entry = findEntry(header, argv[optind])) != NULL) {
288 entry->next = (headerL_t *) malloc(sizeof(headerL_t));
289 *entry->next = *extractList;
290 *extractList = *entry;
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000291 }
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000292 optind++;
293 }
294 }
295 else
296 /* extract everything */
297 extractList = header;
298
299 while(extractList->next != NULL) {
300 if ( (funct & EXT_TO_FILE) == EXT_TO_FILE) {
Glenn L McGrath6fb88e72000-09-09 12:48:40 +0000301 dstFd = open(extractList->name, O_WRONLY | O_CREAT, extractList->mode);
302
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000303 extractAr(srcFd, dstFd, extractList);
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000304 }
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000305 if ( (funct & EXT_TO_STDOUT) == EXT_TO_STDOUT)
306 extractAr(srcFd, fileno(stdout), extractList);
307 if ( (funct & DISPLAY) == DISPLAY)
308 displayEntry(extractList, funct);
309 extractList=extractList->next;
310 }
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000311 return (TRUE);
Eric Andersen86ab8a32000-06-02 03:21:42 +0000312}