Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini ar implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2000 by Glenn McGrath |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 6 | * Written by Glenn McGrath <bug1@optushome.com.au> 1 June 2000 |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 7 | * |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 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 | */ |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 25 | #include <fcntl.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 26 | #include <string.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <getopt.h> |
| 29 | #include <unistd.h> |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 30 | #include "busybox.h" |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 31 | |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 32 | typedef struct ar_headers_s { |
| 33 | char *name; |
| 34 | size_t size; |
| 35 | uid_t uid; |
| 36 | gid_t gid; |
| 37 | mode_t mode; |
| 38 | time_t mtime; |
| 39 | off_t offset; |
| 40 | struct ar_headers_s *next; |
| 41 | } ar_headers_t; |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 42 | |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 43 | /* |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 44 | * return the headerL_t struct for the filename descriptor |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame] | 45 | */ |
Glenn L McGrath | 728875f | 2001-02-11 03:15:57 +0000 | [diff] [blame^] | 46 | extern ar_headers_t get_ar_headers(int srcFd) |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame] | 47 | { |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 48 | typedef struct raw_ar_header_s { /* Byte Offset */ |
| 49 | char name[16]; /* 0-15 */ |
| 50 | char date[12]; /* 16-27 */ |
| 51 | char uid[6]; |
| 52 | char gid[6]; /* 28-39 */ |
| 53 | char mode[8]; /* 40-47 */ |
| 54 | char size[10]; /* 48-57 */ |
| 55 | char fmag[2]; /* 58-59 */ |
| 56 | } raw_ar_header_t; |
Glenn L McGrath | 728875f | 2001-02-11 03:15:57 +0000 | [diff] [blame^] | 57 | |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 58 | raw_ar_header_t raw_ar_header; |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame] | 59 | |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 60 | ar_headers_t *head, *entry; |
| 61 | char ar_magic[8]; |
| 62 | char *long_name=NULL; |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 63 | |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 64 | head = (ar_headers_t *) xmalloc(sizeof(ar_headers_t)); |
| 65 | entry = (ar_headers_t *) xmalloc(sizeof(ar_headers_t)); |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 66 | |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 67 | /* check ar magic */ |
Glenn L McGrath | 728875f | 2001-02-11 03:15:57 +0000 | [diff] [blame^] | 68 | if (full_read(srcFd, ar_magic, 8) != 8) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 69 | error_msg_and_die("cannot read magic"); |
Glenn L McGrath | 728875f | 2001-02-11 03:15:57 +0000 | [diff] [blame^] | 70 | } |
| 71 | |
| 72 | if (strncmp(ar_magic,"!<arch>",7) != 0) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 73 | error_msg_and_die("invalid magic"); |
Glenn L McGrath | 728875f | 2001-02-11 03:15:57 +0000 | [diff] [blame^] | 74 | } |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 75 | |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 76 | while (full_read(srcFd, (char *) &raw_ar_header, 60)==60) { |
| 77 | /* check the end of header markers are valid */ |
| 78 | if ((raw_ar_header.fmag[0]!='`') || (raw_ar_header.fmag[1]!='\n')) { |
Glenn L McGrath | 728875f | 2001-02-11 03:15:57 +0000 | [diff] [blame^] | 79 | char newline; |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 80 | if (raw_ar_header.fmag[1]!='`') { |
| 81 | break; |
| 82 | } |
| 83 | /* some version of ar, have an extra '\n' after each entry */ |
Glenn L McGrath | 728875f | 2001-02-11 03:15:57 +0000 | [diff] [blame^] | 84 | read(srcFd, &newline, 1); |
| 85 | if (newline!='\n') { |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 86 | break; |
| 87 | } |
| 88 | /* fix up the header, we started reading 1 byte too early due to a '\n' */ |
| 89 | memmove((char *) &raw_ar_header, (char *)&raw_ar_header+1, 59); |
| 90 | /* dont worry about adding the last '\n', we dont need it now */ |
| 91 | } |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 92 | |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 93 | entry->size = (size_t) atoi(raw_ar_header.size); |
| 94 | /* long filenames have '/' as the first character */ |
| 95 | if (raw_ar_header.name[0] == '/') { |
| 96 | if (raw_ar_header.name[1] == '/') { |
| 97 | /* multiple long filenames are stored as data in one entry */ |
| 98 | long_name = (char *) xrealloc(long_name, entry->size); |
| 99 | full_read(srcFd, long_name, entry->size); |
| 100 | continue; |
| 101 | } |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 102 | else { |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 103 | /* The number after the '/' indicates the offset in the ar data section |
| 104 | (saved in variable long_name) that conatains the real filename */ |
| 105 | const int long_name_offset = (int) atoi((char *) &raw_ar_header.name[1]); |
| 106 | entry->name = xmalloc(strlen(&long_name[long_name_offset])); |
| 107 | strcpy(entry->name, &long_name[long_name_offset]); |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 110 | else { |
| 111 | /* short filenames */ |
| 112 | entry->name = xmalloc(16); |
| 113 | strncpy(entry->name, raw_ar_header.name, 16); |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 114 | } |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 115 | entry->name[strcspn(entry->name, " /")]='\0'; |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 116 | |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 117 | /* convert the rest of the now valid char header to its typed struct */ |
| 118 | parse_mode(raw_ar_header.mode, &entry->mode); |
| 119 | entry->mtime = atoi(raw_ar_header.date); |
| 120 | entry->uid = atoi(raw_ar_header.uid); |
| 121 | entry->gid = atoi(raw_ar_header.gid); |
| 122 | entry->offset = lseek(srcFd, 0, SEEK_CUR); |
| 123 | |
| 124 | /* add this entries header to our combined list */ |
| 125 | entry->next = (ar_headers_t *) xmalloc(sizeof(ar_headers_t)); |
| 126 | *entry->next = *head; |
| 127 | *head = *entry; |
| 128 | lseek(srcFd, (off_t) entry->size, SEEK_CUR); |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 129 | } |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 130 | return(*head); |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 131 | } |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 132 | |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 133 | extern int ar_main(int argc, char **argv) |
| 134 | { |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 135 | const int preserve_date = 1; /* preserve original dates */ |
| 136 | const int verbose = 2; /* be verbose */ |
| 137 | const int display = 4; /* display contents */ |
| 138 | const int extract_to_file = 8; /* extract contents of archive */ |
| 139 | const int extract_to_stdout = 16; /* extract to stdout */ |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 140 | |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 141 | int funct = 0, opt=0; |
| 142 | int srcFd=0, dstFd=0; |
| 143 | |
| 144 | ar_headers_t head, *extract_list=NULL; |
| 145 | |
| 146 | extract_list = (ar_headers_t *) xmalloc(sizeof(ar_headers_t)); |
| 147 | |
| 148 | while ((opt = getopt(argc, argv, "ovtpx")) != -1) { |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 149 | switch (opt) { |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 150 | case 'o': |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 151 | funct |= preserve_date; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 152 | break; |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 153 | case 'v': |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 154 | funct |= verbose; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 155 | break; |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 156 | case 't': |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 157 | funct |= display; |
Glenn L McGrath | 437bf72 | 2000-09-09 13:38:26 +0000 | [diff] [blame] | 158 | break; |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 159 | case 'p': |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 160 | funct |= extract_to_stdout; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 161 | break; |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 162 | case 'x': |
| 163 | funct |= extract_to_file; |
Glenn L McGrath | e2b345a | 2000-09-09 14:50:04 +0000 | [diff] [blame] | 164 | break; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 165 | default: |
| 166 | usage(ar_usage); |
| 167 | } |
| 168 | } |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 169 | |
Matt Kraai | d995493 | 2000-09-22 03:36:27 +0000 | [diff] [blame] | 170 | /* check the src filename was specified */ |
| 171 | if (optind == argc) |
| 172 | usage(ar_usage); |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 173 | |
Matt Kraai | d995493 | 2000-09-22 03:36:27 +0000 | [diff] [blame] | 174 | if ( (srcFd = open(argv[optind], O_RDONLY)) < 0) |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 175 | error_msg_and_die("Cannot read %s", argv[optind]); |
Matt Kraai | d995493 | 2000-09-22 03:36:27 +0000 | [diff] [blame] | 176 | |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 177 | optind++; |
Glenn L McGrath | 728875f | 2001-02-11 03:15:57 +0000 | [diff] [blame^] | 178 | head = get_ar_headers(srcFd); |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 179 | |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 180 | /* find files to extract or display */ |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 181 | /* search through argv and build extract list */ |
| 182 | for (;optind<argc; optind++) { |
| 183 | ar_headers_t *ar_entry; |
| 184 | ar_entry = (ar_headers_t *) xmalloc(sizeof(ar_headers_t)); |
| 185 | ar_entry = &head; |
| 186 | while (ar_entry->next != NULL) { |
| 187 | if (strcmp(argv[optind], ar_entry->name) == 0) { |
| 188 | ar_headers_t *tmp; |
| 189 | tmp = (ar_headers_t *) xmalloc(sizeof(ar_headers_t)); |
| 190 | *tmp = *extract_list; |
| 191 | *extract_list = *ar_entry; |
| 192 | extract_list->next = tmp; |
| 193 | break; |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 194 | } |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 195 | ar_entry=ar_entry->next; |
| 196 | } |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 197 | } |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 198 | |
| 199 | /* if individual files not found extract all files */ |
| 200 | if (extract_list->next==NULL) { |
| 201 | extract_list = &head; |
| 202 | } |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 203 | |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 204 | /* find files to extract or display */ |
| 205 | while (extract_list->next != NULL) { |
| 206 | if (funct & extract_to_file) { |
| 207 | dstFd = open(extract_list->name, O_WRONLY | O_CREAT, extract_list->mode); |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 208 | } |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 209 | else if (funct & extract_to_stdout) { |
| 210 | dstFd = fileno(stdout); |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame] | 211 | } |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 212 | if ((funct & extract_to_file) || (funct & extract_to_stdout)) { |
| 213 | lseek(srcFd, extract_list->offset, SEEK_SET); |
| 214 | copy_file_chunk(srcFd, dstFd, (size_t) extract_list->size); |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame] | 215 | } |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 216 | if (funct & verbose) { |
| 217 | printf("%s %d/%d %8d %s ", mode_string(extract_list->mode), |
| 218 | extract_list->uid, extract_list->gid, |
| 219 | extract_list->size, time_string(extract_list->mtime)); |
| 220 | } |
| 221 | if ((funct & display) || (funct & verbose)){ |
| 222 | printf("%s\n", extract_list->name); |
| 223 | } |
| 224 | extract_list=extract_list->next; |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 225 | } |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 226 | return EXIT_SUCCESS; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 227 | } |