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 |
| 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 | * |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 24 | * Last modified 10 June 2000 |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 25 | */ |
| 26 | |
| 27 | |
| 28 | #include <stdio.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <errno.h> |
| 31 | #include <ctype.h> |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 32 | #include <time.h> |
| 33 | #include <utime.h> |
| 34 | #include <sys/types.h> |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 35 | #include "internal.h" |
| 36 | |
| 37 | #define AR_BLOCK_SIZE 60 |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 38 | #define AR_PRESERVE_DATE 1 /* preserve original dates */ |
| 39 | #define AR_VERBOSE 2 /* be verbose */ |
| 40 | #define AR_DISPLAY 4 /* display contents */ |
| 41 | #define AR_EXT_TO_FILE 8 /* extract contents of archive */ |
| 42 | #define AR_EXT_TO_STDOUT 16 /* extract to stdout */ |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 43 | |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 44 | #define BB_DECLARE_EXTERN |
| 45 | #define bb_need_io_error |
| 46 | #include "messages.c" |
| 47 | |
| 48 | struct ArHeader { /* Byte Offset */ |
| 49 | char ar_name[16]; /* 0-15 */ |
| 50 | char ar_date[12]; /* 16-27 */ |
| 51 | char ar_uid[6], ar_gid[6]; /* 28-39 */ |
| 52 | char ar_mode[8]; /* 40-47 */ |
| 53 | char ar_size[10]; /* 48-57 */ |
| 54 | char ar_fmag[2]; /* 58-59 */ |
| 55 | }; |
| 56 | typedef struct ArHeader ArHeader; |
| 57 | |
| 58 | struct ArInfo { |
| 59 | char name[17]; /* File name */ |
| 60 | time_t date; /* long int, No of seconds since epoch */ |
| 61 | uid_t uid; /* unsigned int, Numeric UID */ |
| 62 | gid_t gid; /* unsigned int, Numeric GID */ |
| 63 | mode_t mode; /* unsigned int, Unix mode */ |
| 64 | size_t size; /* int, Size of the file */ |
| 65 | }; |
| 66 | typedef struct ArInfo ArInfo; |
| 67 | |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 68 | /* |
| 69 | * Display details of a file, verbosly if funct=2 |
| 70 | */ |
| 71 | static void displayEntry(struct ArInfo *entry, int funct) |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 72 | { |
| 73 | /* TODO convert mode to string */ |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 74 | if ((funct & AR_VERBOSE) == AR_VERBOSE) |
| 75 | printf("%i %i/%i %8i %s ", entry->mode, entry->uid, entry->gid, |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 76 | entry->size, timeString(entry->date)); |
| 77 | printf("%s\n", entry->name); |
| 78 | } |
| 79 | |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 80 | /* this is from tar.c remove later*/ |
| 81 | static long getOctal(const char *cp, int size) |
| 82 | { |
| 83 | long val = 0; |
| 84 | |
| 85 | for(;(size > 0) && (*cp == ' '); cp++, size--); |
| 86 | if ((size == 0) || !isOctal(*cp)) |
| 87 | return -1; |
| 88 | for(; (size > 0) && isOctal(*cp); size--) { |
| 89 | val = val * 8 + *cp++ - '0'; |
| 90 | } |
| 91 | for (;(size > 0) && (*cp == ' '); cp++, size--); |
| 92 | if ((size > 0) && *cp) |
| 93 | return -1; |
| 94 | return val; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * Converts from the char based struct to a new struct with stricter types |
| 99 | */ |
| 100 | static int processArHeader(struct ArHeader *rawHeader, struct ArInfo *header) |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 101 | { |
| 102 | int count2; |
| 103 | int count; |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 104 | |
| 105 | /* check end of header marker is valid */ |
| 106 | if ((rawHeader->ar_fmag[0]!='`') || (rawHeader->ar_fmag[1]!='\n')) |
| 107 | return(FALSE); |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 108 | |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 109 | /* convert filename */ |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 110 | for (count = 0; count < 16; count++) { |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 111 | /* allow spaces in filename except at the end */ |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 112 | if (rawHeader->ar_name[count] == ' ') { |
| 113 | for (count2 = count; count2 < 16; count2++) |
| 114 | if (!isspace(rawHeader->ar_name[count2])) |
| 115 | break; |
| 116 | if (count2 >= 16) |
| 117 | break; |
| 118 | } |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 119 | /* GNU ar uses '/' as an end of filename marker */ |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 120 | if (rawHeader->ar_name[count] == '/') |
| 121 | break; |
| 122 | header->name[count] = rawHeader->ar_name[count]; |
| 123 | } |
| 124 | header->name[count] = '\0'; |
| 125 | header->date = atoi(rawHeader->ar_date); |
| 126 | header->uid = atoi(rawHeader->ar_uid); |
| 127 | header->gid = atoi(rawHeader->ar_gid); |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 128 | header->mode = getOctal(rawHeader->ar_mode, sizeof(rawHeader->ar_mode)); |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 129 | header->size = atoi(rawHeader->ar_size); |
| 130 | return (TRUE); |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Copy size bytes from current position if srcFd to current position in dstFd |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 135 | * taken from tarExtractRegularFile in tar.c, remove later |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 136 | */ |
| 137 | static int copySubFile(int srcFd, int dstFd, int copySize) |
| 138 | { |
| 139 | int readSize, writeSize, doneSize; |
| 140 | char buffer[BUFSIZ]; |
| 141 | |
| 142 | while (copySize > 0) { |
| 143 | if (copySize > BUFSIZ) |
| 144 | readSize = BUFSIZ; |
| 145 | else |
| 146 | readSize = copySize; |
| 147 | writeSize = fullRead(srcFd, buffer, readSize); |
| 148 | if (writeSize <= 0) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 149 | errorMsg(io_error, "copySubFile", strerror(errno)); |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 150 | return (FALSE); |
| 151 | } |
| 152 | doneSize = fullWrite(dstFd, buffer, writeSize); |
| 153 | if (doneSize <= 0) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 154 | errorMsg(io_error, "copySubFile", strerror(errno)); |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 155 | return (FALSE); |
| 156 | } |
| 157 | copySize -= doneSize; |
| 158 | } |
| 159 | return (TRUE); |
| 160 | } |
| 161 | |
| 162 | /* |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 163 | * Extract the file described in ArInfo to the specified path |
| 164 | * set the new files uid, gid and mode |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 165 | */ |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 166 | static int extractToFile(struct ArInfo *file, int funct, int srcFd, const char *path) |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 167 | { |
| 168 | int dstFd, temp; |
| 169 | struct stat tmpStat; |
| 170 | char *pathname = NULL; |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 171 | struct utimbuf newtime; |
| 172 | |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 173 | if ((temp = isDirectory(path, TRUE, &tmpStat)) != TRUE) { |
| 174 | if (!createPath(path, 0777)) { |
| 175 | fatalError("Cannot extract to specified path"); |
| 176 | return (FALSE); |
| 177 | } |
| 178 | } |
| 179 | temp = (strlen(path) + 16); |
| 180 | pathname = (char *) xmalloc(temp); |
| 181 | pathname = strcpy(pathname, path); |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 182 | pathname = strcat(pathname, file->name); |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 183 | dstFd = device_open(pathname, O_WRONLY | O_CREAT); |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 184 | temp = copySubFile(srcFd, dstFd, file->size); |
| 185 | fchown(dstFd, file->uid, file->gid); |
| 186 | fchmod(dstFd, file->mode); |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 187 | close(dstFd); |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 188 | if ((funct&AR_PRESERVE_DATE)==AR_PRESERVE_DATE) |
| 189 | newtime.modtime=file->date; |
| 190 | else |
| 191 | newtime.modtime=time(0); |
| 192 | newtime.actime=time(0); |
| 193 | temp = utime(pathname, &newtime); |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 194 | return (TRUE); |
| 195 | } |
| 196 | |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 197 | /* |
| 198 | * Return a file descriptor for the specified file and do error checks |
| 199 | */ |
| 200 | static int getArFd(char *filename) |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 201 | { |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 202 | int arFd; |
| 203 | char arVersion[8]; |
| 204 | |
| 205 | arFd = open(filename, O_RDONLY); |
| 206 | if (arFd < 0) { |
| 207 | errorMsg("Error opening '%s': %s\n", filename, strerror(errno)); |
| 208 | return (FALSE); |
| 209 | } |
| 210 | if (fullRead(arFd, arVersion, 8) <= 0) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 211 | errorMsg( "Unexpected EOF in archive\n"); |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 212 | return (FALSE); |
| 213 | } |
| 214 | if (strncmp(arVersion,"!<arch>",7) != 0) { |
| 215 | errorMsg("ar header fails check "); |
| 216 | return(FALSE); |
| 217 | } |
| 218 | return arFd; |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Step through the ar file and process it one entry at a time |
| 223 | * fileList[0] is the name of the ar archive |
| 224 | * fileList[1] and up are filenames to extract from the archive |
| 225 | * funct contains flags to specify the actions to be performed |
| 226 | */ |
| 227 | static int readArFile(char *fileList[16], int fileListSize, int funct) |
| 228 | { |
| 229 | int arFd, status, extFileFlag, i, lastOffset=0; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 230 | ArHeader rawArHeader; |
| 231 | ArInfo arEntry; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 232 | |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 233 | /* open the ar archive */ |
| 234 | arFd=getArFd(fileList[0]); |
| 235 | |
| 236 | /* read the first header, then loop until ono more headers */ |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 237 | while ((status = fullRead(arFd, (char *) &rawArHeader, AR_BLOCK_SIZE)) |
| 238 | == AR_BLOCK_SIZE) { |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 239 | |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 240 | /* check the header is valid, if not try reading the header |
| 241 | agian with an offset of 1, needed as some ar archive end |
| 242 | with a '\n' which isnt counted in specified file size */ |
| 243 | if ((status=processArHeader(&rawArHeader, &arEntry))==FALSE ) { |
| 244 | if ((i=lseek(arFd, 0, SEEK_CUR))==(lastOffset+60)) |
| 245 | lseek(arFd, lastOffset+1, SEEK_SET); |
| 246 | else |
| 247 | return(FALSE); |
| 248 | } |
| 249 | else { |
| 250 | extFileFlag=0; |
| 251 | |
Eric Andersen | 6ac18a3 | 2000-08-11 20:11:35 +0000 | [diff] [blame^] | 252 | if ((funct&AR_DISPLAY) || (funct&AR_VERBOSE)) |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 253 | displayEntry(&arEntry, funct); |
| 254 | |
| 255 | /* check file was specified to be extracted only if |
| 256 | some file were specified */ |
| 257 | if ((funct&AR_EXT_TO_FILE) || (funct&AR_EXT_TO_STDOUT)){ |
| 258 | if (fileListSize==1) |
| 259 | extFileFlag=1; |
| 260 | else { |
| 261 | for( i=1; i<=fileListSize; i++) |
| 262 | if ((status=(strcmp(fileList[i],arEntry.name)))==0) |
| 263 | extFileFlag=1; |
| 264 | } |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 265 | } |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 266 | if (extFileFlag==1) { |
| 267 | if (funct&AR_EXT_TO_FILE) |
| 268 | extractToFile(&arEntry, funct, arFd, "./"); |
| 269 | else |
| 270 | copySubFile(arFd,fileno(stdout),arEntry.size); |
| 271 | } |
| 272 | else |
| 273 | lseek(arFd, arEntry.size, SEEK_CUR); |
| 274 | lastOffset=lseek(arFd, 0, SEEK_CUR); |
| 275 | } /* if processArHeader */ |
| 276 | } /* while */ |
| 277 | return (TRUE); |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | extern int ar_main(int argc, char **argv) |
| 281 | { |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 282 | int funct = 0, ret=0, i=0; |
| 283 | char *fileList[16], c, *opt_ptr; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 284 | |
| 285 | if (argc < 2) |
| 286 | usage(ar_usage); |
| 287 | |
| 288 | opt_ptr = argv[1]; |
| 289 | if (*opt_ptr == '-') |
| 290 | ++opt_ptr; |
| 291 | while ((c = *opt_ptr++) != '\0') { |
| 292 | switch (c) { |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 293 | case 'o': |
| 294 | funct = funct | AR_PRESERVE_DATE; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 295 | break; |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 296 | case 'v': |
| 297 | funct = funct | AR_VERBOSE; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 298 | break; |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 299 | case 't': |
| 300 | funct = funct | AR_DISPLAY; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 301 | break; |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 302 | case 'x': |
| 303 | funct = funct | AR_EXT_TO_FILE; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 304 | break; |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 305 | case 'p': |
| 306 | funct = funct | AR_EXT_TO_STDOUT; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 307 | break; |
| 308 | default: |
| 309 | usage(ar_usage); |
| 310 | } |
| 311 | } |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 312 | |
| 313 | for(i=0; i<(argc-2); i++) |
| 314 | fileList[i]=argv[i+2]; |
| 315 | |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 316 | if (funct > 3) |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 317 | ret = readArFile(fileList, (argc-2), funct); |
| 318 | |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 319 | return (ret); |
| 320 | } |