Fixed the comments to match the code and renamed the function to a (hopefully)
[oweals/busybox.git] / archival / ar.c
1 /* 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
7  *              
8  * 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  *
24  * Last modified 20 September 2000
25  */
26 #include <stdio.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <ctype.h>
31 #include <time.h>
32 #include <utime.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <malloc.h>
38 #include "busybox.h"
39
40 #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 */
46 #define RECURSIVE     32         
47
48 #define MAX_NAME_LENGTH 100
49
50 //#define BB_DECLARE_EXTERN
51 //#define bb_need_io_error
52 //#include "messages.c"
53
54 //#define BB_AR_EXPERIMENTAL_UNTAR
55
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 */
75 } rawTarHeader_t;
76 #endif
77
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 */
85 } rawArHeader_t;
86
87 typedef struct headerL {
88         char name[MAX_NAME_LENGTH];
89         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;
97
98 #if defined BB_AR_EXPERIMENTAL_UNTAR
99 /*
100  * identify Tar header (magic field) and reset srcFd to entry position
101  */
102 static 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
117 static 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
153  */
154 static int checkArMagic(int srcFd)
155 {
156         off_t headerStart;
157         char arMagic[8];
158
159         headerStart = lseek(srcFd, 0, SEEK_CUR);
160         if (fullRead(srcFd, arMagic, 8) != 8) {
161                 errorMsg("fatal error\n");
162                 return (FALSE);
163         }
164         lseek(srcFd, headerStart, SEEK_SET);
165
166         if (strncmp(arMagic,"!<arch>",7) != 0)
167                 return(FALSE);
168         return(TRUE);
169 }
170
171 /*
172  * get, check and correct the converted header
173  */ 
174 static int readArEntry(int srcFd, headerL_t *entry)
175 {
176         size_t nameLength;
177         rawArHeader_t rawArHeader;
178         off_t   initialOffset;
179
180         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, "/");
201         
202         /* handle GNU style short filenames, strip trailing '/' */
203         if (nameLength > 0)
204                 entry->name[nameLength]='\0';
205         
206         /* handle GNU style long filenames */ 
207         if (nameLength == 0) {
208                 /* escape from recursive call */
209                 if (entry->name[1]=='0') 
210                         return(TRUE);
211
212                 /* the data section contains the real filename */
213                 if (entry->name[1]=='/') {
214                         char tempName[MAX_NAME_LENGTH];
215
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';
220                         
221                         /* read the second header for this entry */
222                         /* be carefull, this is recursive */
223                         if (readArEntry(srcFd, entry)==FALSE)
224                                 return(FALSE);
225                 
226                         if ((entry->name[0]='/') && (entry->name[1]='0'))
227                                 strcpy(entry->name, tempName);
228                         else {
229                                 errorMsg("Invalid long filename\n");
230                                 return(FALSE);
231                         }
232                 }
233         }
234         return(TRUE);   
235 }
236
237 /*
238  * return the headerL_t struct for the specified filename
239  */
240 static headerL_t *getHeaders(int srcFd, headerL_t *head, int funct)
241 {
242 #if defined BB_AR_EXPERIMENTAL_UNTAR
243         int tar=FALSE;
244 #endif
245         int ar=FALSE;
246         headerL_t *list;
247         off_t initialOffset;
248
249         list = (headerL_t *) xmalloc(sizeof(headerL_t));
250         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;
261                         list->next = (headerL_t *) xmalloc(sizeof(headerL_t));
262                         *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                         }
285                         list->next = (headerL_t *) xmalloc(sizeof(headerL_t));
286                         *list->next = *head;
287                         *head = *list;
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);
292                 }
293         }
294         return(head);
295 }
296
297 /*
298  * find an entry in the linked list matching the filename
299  */
300 static 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);
308 }
309
310 extern int ar_main(int argc, char **argv)
311 {
312         int funct = 0, opt=0;
313         int srcFd=0, dstFd=0;
314         headerL_t *header, *entry, *extractList;
315
316         while ((opt = getopt(argc, argv, "ovtpxR")) != -1) {
317                 switch (opt) {
318                 case 'o':
319                         funct |= PRESERVE_DATE;
320                         break;
321                 case 'v':
322                         funct |= VERBOSE;
323                         break;
324                 case 't':
325                         funct |= DISPLAY;
326                         break;
327                 case 'x':
328                         funct |= EXT_TO_FILE;
329                         break;
330                 case 'p':
331                         funct |= EXT_TO_STDOUT;
332                         break;
333                 case 'R':
334                         funct |= RECURSIVE;
335                         break;
336                 default:
337                         usage(ar_usage);
338                 }
339         }
340  
341         /* check the src filename was specified */
342         if (optind == argc)
343                 usage(ar_usage);
344         
345         if ( (srcFd = open(argv[optind], O_RDONLY)) < 0)
346                 fatalError("Cannot read %s\n", argv[optind]);
347
348         optind++;       
349         entry = (headerL_t *) xmalloc(sizeof(headerL_t));
350         header = (headerL_t *) xmalloc(sizeof(headerL_t));
351         extractList = (headerL_t *) xmalloc(sizeof(headerL_t)); 
352
353         header = getHeaders(srcFd, header, funct);
354         /* 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) {
359                                 entry->next = (headerL_t *) xmalloc(sizeof(headerL_t));
360                                 *entry->next = *extractList;
361                                 *extractList = *entry;
362                         }
363                         optind++;
364                 }       
365         }
366         else  
367                 extractList = header;
368         
369         while(extractList->next != NULL) {      
370                 if (funct & EXT_TO_FILE) {
371                         if (isDirectory(extractList->name, TRUE, NULL)==FALSE)
372                                 createPath(extractList->name, 0666);
373                         dstFd = open(extractList->name, O_WRONLY | O_CREAT, extractList->mode);
374                         lseek(srcFd, extractList->offset, SEEK_SET);
375                         copy_file_chunk(srcFd, dstFd, (size_t) extractList->size);
376                 }
377                 if (funct & EXT_TO_STDOUT) {    
378                         lseek(srcFd, extractList->offset, SEEK_SET);
379                         copy_file_chunk(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                 }
388                 extractList=extractList->next;
389         }
390         return EXIT_SUCCESS;
391 }