blob: 596574e6af3c7a0f64567a24d9dd645453f05563 [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
Glenn L McGrath4f1b0122000-12-15 06:50:09 +00006 * Written by Glenn McGrath <bug1@optushome.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 *
24 */
Eric Andersen86ab8a32000-06-02 03:21:42 +000025#include <fcntl.h>
Eric Andersen3570a342000-09-25 21:45:58 +000026#include "busybox.h"
Eric Andersen86ab8a32000-06-02 03:21:42 +000027
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000028typedef struct ar_headers_s {
29 char *name;
30 size_t size;
31 uid_t uid;
32 gid_t gid;
33 mode_t mode;
34 time_t mtime;
35 off_t offset;
36 struct ar_headers_s *next;
37} ar_headers_t;
Eric Andersen852ff132000-06-16 04:56:40 +000038
Eric Andersen852ff132000-06-16 04:56:40 +000039/*
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000040 * return the headerL_t struct for the filename descriptor
Glenn L McGrathfca80502000-09-11 05:25:39 +000041 */
Glenn L McGrath7541e3a2001-01-02 23:41:50 +000042extern ar_headers_t get_headers(int srcFd)
Glenn L McGrathfca80502000-09-11 05:25:39 +000043{
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000044 typedef struct raw_ar_header_s { /* Byte Offset */
45 char name[16]; /* 0-15 */
46 char date[12]; /* 16-27 */
47 char uid[6];
48 char gid[6]; /* 28-39 */
49 char mode[8]; /* 40-47 */
50 char size[10]; /* 48-57 */
51 char fmag[2]; /* 58-59 */
52 } raw_ar_header_t;
53 raw_ar_header_t raw_ar_header;
Glenn L McGrathfca80502000-09-11 05:25:39 +000054
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000055 ar_headers_t *head, *entry;
56 char ar_magic[8];
57 char *long_name=NULL;
Glenn L McGrath8324b9f2000-09-09 08:35:45 +000058
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000059 head = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
60 entry = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
Glenn L McGrath8324b9f2000-09-09 08:35:45 +000061
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000062 /* check ar magic */
63 if (full_read(srcFd, ar_magic, 8) != 8)
64 error_msg_and_die("cannot read magic\n");
65 if (strncmp(ar_magic,"!<arch>",7) != 0)
66 error_msg_and_die("invalid magic\n");
Glenn L McGrath8324b9f2000-09-09 08:35:45 +000067
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000068 while (full_read(srcFd, (char *) &raw_ar_header, 60)==60) {
69 /* check the end of header markers are valid */
70 if ((raw_ar_header.fmag[0]!='`') || (raw_ar_header.fmag[1]!='\n')) {
71 char newline[1];
72 if (raw_ar_header.fmag[1]!='`') {
73 break;
74 }
75 /* some version of ar, have an extra '\n' after each entry */
76 read(srcFd, newline, 1);
77 if (newline[0]!='\n') {
78 break;
79 }
80 /* fix up the header, we started reading 1 byte too early due to a '\n' */
81 memmove((char *) &raw_ar_header, (char *)&raw_ar_header+1, 59);
82 /* dont worry about adding the last '\n', we dont need it now */
83 }
Glenn L McGrath8324b9f2000-09-09 08:35:45 +000084
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000085 entry->size = (size_t) atoi(raw_ar_header.size);
86 /* long filenames have '/' as the first character */
87 if (raw_ar_header.name[0] == '/') {
88 if (raw_ar_header.name[1] == '/') {
89 /* multiple long filenames are stored as data in one entry */
90 long_name = (char *) xrealloc(long_name, entry->size);
91 full_read(srcFd, long_name, entry->size);
92 continue;
93 }
Glenn L McGrath8324b9f2000-09-09 08:35:45 +000094 else {
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000095 /* The number after the '/' indicates the offset in the ar data section
96 (saved in variable long_name) that conatains the real filename */
97 const int long_name_offset = (int) atoi((char *) &raw_ar_header.name[1]);
98 entry->name = xmalloc(strlen(&long_name[long_name_offset]));
99 strcpy(entry->name, &long_name[long_name_offset]);
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000100 }
101 }
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000102 else {
103 /* short filenames */
104 entry->name = xmalloc(16);
105 strncpy(entry->name, raw_ar_header.name, 16);
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000106 }
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000107 entry->name[strcspn(entry->name, " /")]='\0';
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000108
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000109 /* convert the rest of the now valid char header to its typed struct */
110 parse_mode(raw_ar_header.mode, &entry->mode);
111 entry->mtime = atoi(raw_ar_header.date);
112 entry->uid = atoi(raw_ar_header.uid);
113 entry->gid = atoi(raw_ar_header.gid);
114 entry->offset = lseek(srcFd, 0, SEEK_CUR);
115
116 /* add this entries header to our combined list */
117 entry->next = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
118 *entry->next = *head;
119 *head = *entry;
120 lseek(srcFd, (off_t) entry->size, SEEK_CUR);
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000121 }
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000122 return(*head);
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000123}
Eric Andersen852ff132000-06-16 04:56:40 +0000124
Eric Andersen86ab8a32000-06-02 03:21:42 +0000125extern int ar_main(int argc, char **argv)
126{
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000127 const int preserve_date = 1; /* preserve original dates */
128 const int verbose = 2; /* be verbose */
129 const int display = 4; /* display contents */
130 const int extract_to_file = 8; /* extract contents of archive */
131 const int extract_to_stdout = 16; /* extract to stdout */
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000132
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000133 int funct = 0, opt=0;
134 int srcFd=0, dstFd=0;
135
136 ar_headers_t head, *extract_list=NULL;
137
138 extract_list = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
139
140 while ((opt = getopt(argc, argv, "ovtpx")) != -1) {
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000141 switch (opt) {
Eric Andersen57ebebf2000-07-05 17:21:58 +0000142 case 'o':
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000143 funct |= preserve_date;
Eric Andersen86ab8a32000-06-02 03:21:42 +0000144 break;
Eric Andersen57ebebf2000-07-05 17:21:58 +0000145 case 'v':
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000146 funct |= verbose;
Eric Andersen86ab8a32000-06-02 03:21:42 +0000147 break;
Eric Andersen57ebebf2000-07-05 17:21:58 +0000148 case 't':
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000149 funct |= display;
Glenn L McGrath437bf722000-09-09 13:38:26 +0000150 break;
Eric Andersen57ebebf2000-07-05 17:21:58 +0000151 case 'p':
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000152 funct |= extract_to_stdout;
Eric Andersen86ab8a32000-06-02 03:21:42 +0000153 break;
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000154 case 'x':
155 funct |= extract_to_file;
Glenn L McGrathe2b345a2000-09-09 14:50:04 +0000156 break;
Eric Andersen86ab8a32000-06-02 03:21:42 +0000157 default:
158 usage(ar_usage);
159 }
160 }
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000161
Matt Kraaid9954932000-09-22 03:36:27 +0000162 /* check the src filename was specified */
163 if (optind == argc)
164 usage(ar_usage);
Eric Andersen852ff132000-06-16 04:56:40 +0000165
Matt Kraaid9954932000-09-22 03:36:27 +0000166 if ( (srcFd = open(argv[optind], O_RDONLY)) < 0)
Mark Whitleyf57c9442000-12-07 19:56:48 +0000167 error_msg_and_die("Cannot read %s\n", argv[optind]);
Matt Kraaid9954932000-09-22 03:36:27 +0000168
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000169 optind++;
170 head = get_headers(srcFd);
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000171
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000172 /* find files to extract or display */
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000173 /* search through argv and build extract list */
174 for (;optind<argc; optind++) {
175 ar_headers_t *ar_entry;
176 ar_entry = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
177 ar_entry = &head;
178 while (ar_entry->next != NULL) {
179 if (strcmp(argv[optind], ar_entry->name) == 0) {
180 ar_headers_t *tmp;
181 tmp = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
182 *tmp = *extract_list;
183 *extract_list = *ar_entry;
184 extract_list->next = tmp;
185 break;
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000186 }
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000187 ar_entry=ar_entry->next;
188 }
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000189 }
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000190
191 /* if individual files not found extract all files */
192 if (extract_list->next==NULL) {
193 extract_list = &head;
194 }
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000195
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000196 /* find files to extract or display */
197 while (extract_list->next != NULL) {
198 if (funct & extract_to_file) {
199 dstFd = open(extract_list->name, O_WRONLY | O_CREAT, extract_list->mode);
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000200 }
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000201 else if (funct & extract_to_stdout) {
202 dstFd = fileno(stdout);
Glenn L McGrathfca80502000-09-11 05:25:39 +0000203 }
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000204 if ((funct & extract_to_file) || (funct & extract_to_stdout)) {
205 lseek(srcFd, extract_list->offset, SEEK_SET);
206 copy_file_chunk(srcFd, dstFd, (size_t) extract_list->size);
Glenn L McGrathfca80502000-09-11 05:25:39 +0000207 }
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000208 if (funct & verbose) {
209 printf("%s %d/%d %8d %s ", mode_string(extract_list->mode),
210 extract_list->uid, extract_list->gid,
211 extract_list->size, time_string(extract_list->mtime));
212 }
213 if ((funct & display) || (funct & verbose)){
214 printf("%s\n", extract_list->name);
215 }
216 extract_list=extract_list->next;
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000217 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000218 return EXIT_SUCCESS;
Eric Andersen86ab8a32000-06-02 03:21:42 +0000219}