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 |
| 6 | * Written by Glenn McGrath <bug1@netconnect.com.au> 1 June 2000 |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 7 | * |
| 8 | * Modified 8 August 2000 by Glenn McGrath |
| 9 | * - now uses getopt |
| 10 | * - moved copySubFile function to utilities.c |
| 11 | * - creates linked list of all headers |
| 12 | * - easily accessable to other busybox functions |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 13 | * |
| 14 | * Based in part on BusyBox tar, Debian dpkg-deb and GNU ar. |
| 15 | * |
| 16 | * This program is free software; you can redistribute it and/or modify |
| 17 | * it under the terms of the GNU General Public License as published by |
| 18 | * the Free Software Foundation; either version 2 of the License, or |
| 19 | * (at your option) any later version. |
| 20 | * |
| 21 | * This program is distributed in the hope that it will be useful, |
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 24 | * General Public License for more details. |
| 25 | * |
| 26 | * You should have received a copy of the GNU General Public License |
| 27 | * along with this program; if not, write to the Free Software |
| 28 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 29 | * |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 30 | * Last modified 25 August 2000 |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 31 | */ |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 32 | #include <stdio.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <errno.h> |
| 35 | #include <ctype.h> |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 36 | #include <time.h> |
| 37 | #include <utime.h> |
| 38 | #include <sys/types.h> |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 39 | #include "internal.h" |
| 40 | |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 41 | #define BLOCK_SIZE 60 |
| 42 | #define PRESERVE_DATE 1 /* preserve original dates */ |
| 43 | #define VERBOSE 2 /* be verbose */ |
| 44 | #define DISPLAY 4 /* display contents */ |
| 45 | #define EXT_TO_FILE 8 /* extract contents of archive */ |
| 46 | #define EXT_TO_STDOUT 16 /* extract to stdout */ |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 47 | |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 48 | #define BB_DECLARE_EXTERN |
| 49 | #define bb_need_io_error |
| 50 | #include "messages.c" |
| 51 | |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 52 | typedef struct rawArHeader { /* Byte Offset */ |
| 53 | char name[16]; /* 0-15 */ |
| 54 | char date[12]; /* 16-27 */ |
| 55 | char uid[6], gid[6]; /* 28-39 */ |
| 56 | char mode[8]; /* 40-47 */ |
| 57 | char size[10]; /* 48-57 */ |
| 58 | char fmag[2]; /* 58-59 */ |
| 59 | } rawArHeader_t; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 60 | |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 61 | typedef struct headerL { |
| 62 | char name[16]; |
| 63 | size_t size; |
| 64 | uid_t uid; |
| 65 | gid_t gid; |
| 66 | mode_t mode; |
| 67 | time_t mtime; |
| 68 | off_t offset; |
| 69 | struct headerL *next; |
| 70 | } headerL_t; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 71 | |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 72 | /* |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 73 | * populate linked list with all ar file entries and offset |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 74 | */ |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 75 | static int parseArArchive(int srcFd, headerL_t *current) |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 76 | { |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 77 | off_t lastOffset=0, thisOffset=0; |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 78 | char arVersion[8]; |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 79 | rawArHeader_t rawArHeader; |
| 80 | |
| 81 | lseek(srcFd, 0, SEEK_SET); |
| 82 | if (fullRead(srcFd, arVersion, 8) <= 0) { |
| 83 | errorMsg("Invalid header magic\n"); |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 84 | return (FALSE); |
| 85 | } |
| 86 | if (strncmp(arVersion,"!<arch>",7) != 0) { |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 87 | errorMsg("This doesnt appear to be an ar archive\n"); |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 88 | return(FALSE); |
| 89 | } |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 90 | while (fullRead(srcFd, (char *) &rawArHeader, 60) == 60) { |
| 91 | if ( (rawArHeader.fmag[0] == '`') && |
| 92 | (rawArHeader.fmag[1] == '\n')) { |
| 93 | sscanf(rawArHeader.name, "%s", current->name); |
| 94 | parse_mode(rawArHeader.mode, ¤t->mode); |
| 95 | current->mtime = atoi(rawArHeader.date); |
| 96 | current->uid = atoi(rawArHeader.uid); |
| 97 | current->gid = atoi(rawArHeader.gid); |
| 98 | current->size = (size_t) atoi(rawArHeader.size); |
| 99 | current->offset = lseek(srcFd, 0 , SEEK_CUR); |
| 100 | current->next = (headerL_t *) xmalloc(sizeof(headerL_t)); |
| 101 | lastOffset = lseek(srcFd, (off_t) current->size, SEEK_CUR); |
| 102 | current = current->next; |
| 103 | } |
| 104 | else { /* GNU ar has an extra char after data */ |
| 105 | thisOffset=lseek(srcFd, 0, SEEK_CUR); |
| 106 | if ( (thisOffset - lastOffset) > ((off_t) 61) ) |
| 107 | return(FALSE); |
| 108 | lseek(srcFd, thisOffset - 59, SEEK_SET); |
| 109 | } |
| 110 | } |
| 111 | return(FALSE); |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /* |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 115 | * return the headerL_t struct for the specified filename |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 116 | */ |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 117 | static headerL_t *getSubFileHeader(int srcFd, const char filename[16]) |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 118 | { |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 119 | headerL_t *list; |
| 120 | list = xmalloc(sizeof(headerL_t)); |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 121 | |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 122 | parseArArchive(srcFd, list); |
| 123 | while (list->next != NULL) { |
| 124 | if (strncmp(list->name, filename, strlen(filename))==0) |
| 125 | return(list); |
| 126 | list=list->next; |
| 127 | } |
| 128 | return(NULL); |
| 129 | } |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 130 | |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 131 | /* |
| 132 | * populate linked list with all ar file entries and offset |
| 133 | */ |
| 134 | static int displayEntry(int srcFd, const char filename[16], int funct) |
| 135 | { |
| 136 | headerL_t *file; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 137 | |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 138 | if ((file = getSubFileHeader(srcFd, filename)) == NULL) |
| 139 | return(FALSE); |
| 140 | if ((funct & VERBOSE) == VERBOSE) { |
| 141 | printf("%s %d/%d %8d %s ", modeString(file->mode), file->uid, file->gid, file->size, timeString(file->mtime)); |
| 142 | } |
| 143 | printf("%s\n", file->name); |
| 144 | return(TRUE); |
| 145 | } |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 146 | |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 147 | static int extractAr(int srcFd, int dstFd, const char filename[16]) |
| 148 | { |
| 149 | headerL_t *file; |
| 150 | |
| 151 | if ( (file = getSubFileHeader(srcFd, filename)) == NULL) |
| 152 | return(FALSE); |
| 153 | lseek(srcFd, file->offset, SEEK_SET); |
| 154 | if (copySubFile(srcFd, dstFd, (size_t) file->size) == TRUE) |
| 155 | return(TRUE); |
| 156 | return(FALSE); |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | extern int ar_main(int argc, char **argv) |
| 160 | { |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 161 | int funct = 0, opt=0; |
| 162 | int srcFd=0, dstFd=0; |
| 163 | |
| 164 | while ((opt = getopt(argc, argv, "ovt:p:x:")) != -1) { |
| 165 | switch (opt) { |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 166 | case 'o': |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 167 | funct = funct | PRESERVE_DATE; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 168 | break; |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 169 | case 'v': |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 170 | funct = funct | VERBOSE; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 171 | break; |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 172 | case 't': |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 173 | funct = funct | DISPLAY; |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 174 | case 'x': |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 175 | if (opt=='x') { |
| 176 | funct = funct | EXT_TO_FILE; |
| 177 | } |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 178 | case 'p': |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 179 | if (opt=='p') { |
| 180 | funct = funct | EXT_TO_STDOUT; |
| 181 | } |
| 182 | /* following is common to 't','x' and 'p' */ |
| 183 | if (optarg == NULL) { |
| 184 | printf("expected a filename\n"); |
| 185 | return(FALSE); |
| 186 | } |
| 187 | if ( (srcFd = open(optarg, O_RDONLY)) < 0) { |
| 188 | errorMsg("Cannot read %s\n", optarg); |
| 189 | return (FALSE); |
| 190 | } |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 191 | break; |
| 192 | default: |
| 193 | usage(ar_usage); |
| 194 | } |
| 195 | } |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 196 | |
| 197 | /* check options not just preserve_dates and/or verbose */ |
| 198 | if (funct < 4) { |
| 199 | usage(ar_usage); |
| 200 | return(FALSE); |
| 201 | } |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 202 | |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 203 | /* find files to extract */ |
| 204 | if (optind<argc) |
| 205 | for(; optind < argc; optind++) { |
| 206 | if ( (funct & EXT_TO_FILE) == EXT_TO_FILE) { |
| 207 | dstFd = open(argv[optind], O_WRONLY | O_CREAT); |
| 208 | extractAr(srcFd, dstFd, argv[optind]); |
| 209 | } |
| 210 | if ( (funct & EXT_TO_STDOUT) == EXT_TO_STDOUT) |
| 211 | extractAr(srcFd, fileno(stdout), argv[optind]); |
| 212 | if ( (funct & DISPLAY) == DISPLAY) |
| 213 | displayEntry(srcFd, argv[optind], funct); |
| 214 | } |
| 215 | return (TRUE); |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 216 | } |