1 /* vi: set sw=4 ts=4: */
3 * Mini ar implementation for busybox
5 * Copyright (C) 2000 by Glenn McGrath
6 * Written by Glenn McGrath <bug1@netconnect.com.au> 1 June 2000
8 * Based in part on BusyBox tar, Debian dpkg-deb and GNU ar.
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.
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.
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
24 * Last modified 9 September 2000
35 #include <sys/types.h>
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 */
48 #define MAX_NAME_LENGTH 100
50 //#define BB_DECLARE_EXTERN
51 //#define bb_need_io_error
52 //#include "messages.c"
54 //#define BB_AR_EXPERIMENTAL_UNTAR
56 #if defined BB_AR_EXPERIMENTAL_UNTAR
57 typedef 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 */
78 typedef struct rawArHeader { /* Byte Offset */
79 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 */
87 typedef struct headerL {
88 char name[MAX_NAME_LENGTH];
98 #if defined BB_AR_EXPERIMENTAL_UNTAR
100 * identify Tar header (magic field) and reset srcFd to entry position
102 static int checkTarMagic(int srcFd)
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)
117 static int readTarHeader(int srcFd, headerL_t *current)
119 rawTarHeader_t rawTarHeader;
120 unsigned char *temp = (unsigned char *) &rawTarHeader;
125 initialOffset = lseek(srcFd, 0, SEEK_CUR);
126 if (fullRead(srcFd, (char *) &rawTarHeader, 512) != 512) {
127 lseek(srcFd, initialOffset, SEEK_SET);
130 for (i = 0; i < 148 ; i++)
133 for (i = 156; i < 512 ; i++)
135 if (sum!= strtol(rawTarHeader.chksum, NULL, 8))
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);
145 current->next = (headerL_t *) xmalloc(sizeof(headerL_t));
146 current = current->next;
152 * identify Ar header (magic) and reset srcFd to entry position
154 static int checkArMagic(int srcFd)
159 headerStart = lseek(srcFd, 0, SEEK_CUR);
160 if (fullRead(srcFd, arMagic, 8) != 8) {
161 printf("fatal error/n");
164 lseek(srcFd, headerStart, SEEK_SET);
166 if (strncmp(arMagic,"!<arch>",7) != 0)
172 * get, check and correct the converted header
174 static int readArEntry(int srcFd, headerL_t *entry)
177 rawArHeader_t rawArHeader;
180 initialOffset = lseek(srcFd, 0, SEEK_CUR);
181 if (fullRead(srcFd, (char *) &rawArHeader, 60) != 60) {
182 lseek(srcFd, initialOffset, SEEK_SET);
185 if ((rawArHeader.fmag[0]!='`') || (rawArHeader.fmag[1]!='\n')) {
186 lseek(srcFd, initialOffset, SEEK_SET);
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;
200 nameLength = strcspn(entry->name, "/");
202 /* handle GNU style short filenames, strip trailing '/' */
204 entry->name[nameLength]='\0';
206 /* handle GNU style long filenames */
207 if (nameLength == 0) {
208 /* escape from recursive call */
209 if (entry->name[1]=='0')
212 /* the data section contains the real filename */
213 if (entry->name[1]=='/') {
214 char tempName[MAX_NAME_LENGTH];
216 if (entry->size > MAX_NAME_LENGTH)
217 entry->size = MAX_NAME_LENGTH;
218 fullRead(srcFd, tempName, entry->size);
219 tempName[entry->size-3]='\0';
221 /* read the second header for this entry */
222 /* be carefull, this is recursive */
223 if (readArEntry(srcFd, entry)==FALSE)
226 if ((entry->name[0]='/') && (entry->name[1]='0'))
227 strcpy(entry->name, tempName);
229 errorMsg("Invalid long filename\n");
238 * return the headerL_t struct for the specified filename
240 static headerL_t *getHeaders(int srcFd, headerL_t *head, int funct)
242 #if defined BB_AR_EXPERIMENTAL_UNTAR
249 list = (headerL_t *) xmalloc(sizeof(headerL_t));
250 initialOffset=lseek(srcFd, 0, SEEK_CUR);
251 if (checkArMagic(srcFd)==TRUE)
254 #if defined BB_AR_EXPERIMENTAL_UNTAR
255 if (checkTarMagic(srcFd)==TRUE)
259 while(readTarHeader(srcFd, list)==TRUE) {
261 list->next = (headerL_t *) xmalloc(sizeof(headerL_t));
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)
271 tarOffset=tarOffset*512;
272 lseek(srcFd, head->offset + tarOffset, SEEK_SET);
278 lseek(srcFd, 8, SEEK_CUR);
280 if (readArEntry(srcFd, list) == FALSE) {
281 lseek(srcFd, ++initialOffset, SEEK_CUR);
282 if (readArEntry(srcFd, list) == FALSE)
285 list->next = (headerL_t *) xmalloc(sizeof(headerL_t));
288 /* recursive check for sub-archives */
289 if (funct & RECURSIVE)
290 head = getHeaders(srcFd, head, funct);
291 lseek(srcFd, head->offset + head->size, SEEK_SET);
298 * find an entry in the linked list matching the filename
300 static headerL_t *findEntry(headerL_t *head, const char *filename)
302 while(head->next != NULL) {
303 if (strcmp(filename, head->name)==0)
310 extern int ar_main(int argc, char **argv)
312 int funct = 0, opt=0;
313 int srcFd=0, dstFd=0;
314 headerL_t *header, *entry, *extractList;
316 while ((opt = getopt(argc, argv, "ovtpxR")) != -1) {
319 funct |= PRESERVE_DATE;
328 funct |= EXT_TO_FILE;
331 funct |= EXT_TO_STDOUT;
341 /* check the src filename was specified */
342 if (optind == argc) {
347 if ( (srcFd = open(argv[optind], O_RDONLY)) < 0) {
348 errorMsg("Cannot read %s\n", optarg);
352 entry = (headerL_t *) xmalloc(sizeof(headerL_t));
353 header = (headerL_t *) xmalloc(sizeof(headerL_t));
354 extractList = (headerL_t *) xmalloc(sizeof(headerL_t));
356 header = getHeaders(srcFd, header, funct);
357 /* find files to extract or display */
359 /* only handle specified files */
360 while(optind < argc) {
361 if ( (entry = findEntry(header, argv[optind])) != NULL) {
362 entry->next = (headerL_t *) xmalloc(sizeof(headerL_t));
363 *entry->next = *extractList;
364 *extractList = *entry;
370 extractList = header;
372 while(extractList->next != NULL) {
373 if (funct & EXT_TO_FILE) {
374 if (isDirectory(extractList->name, TRUE, NULL)==FALSE)
375 createPath(extractList->name, 0666);
376 dstFd = open(extractList->name, O_WRONLY | O_CREAT, extractList->mode);
377 lseek(srcFd, extractList->offset, SEEK_SET);
378 copySubFile(srcFd, dstFd, (size_t) extractList->size);
380 if (funct & EXT_TO_STDOUT) {
381 lseek(srcFd, extractList->offset, SEEK_SET);
382 copySubFile(srcFd, fileno(stdout), (size_t) extractList->size);
384 if ( (funct & DISPLAY) || (funct & VERBOSE)) {
386 printf("%s %d/%d %8d %s ", modeString(extractList->mode),
387 extractList->uid, extractList->gid,
388 extractList->size, timeString(extractList->mtime));
389 printf("%s\n", extractList->name);
391 extractList=extractList->next;