Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 2 | /* |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 3 | * Mini tar implementation for busybox |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 4 | * |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 5 | * Note, that as of BusyBox-0.43, tar has been completely rewritten from the |
| 6 | * ground up. It still has remnents of the old code lying about, but it is |
| 7 | * very different now (i.e. cleaner, less global variables, etc) |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 8 | * |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 9 | * Copyright (C) 2000 by Lineo, inc. |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 10 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 11 | * |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 12 | * Based in part in the tar implementation in sash |
| 13 | * Copyright (c) 1999 by David I. Bell |
| 14 | * Permission is granted to use, distribute, or modify this source, |
| 15 | * provided that this copyright notice remains intact. |
| 16 | * Permission to distribute sash derived code under the GPL has been granted. |
| 17 | * |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 18 | * Based in part on the tar implementation from busybox-0.28 |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 19 | * Copyright (C) 1995 Bruce Perens |
| 20 | * This is free software under the GNU General Public License. |
| 21 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 22 | * This program is free software; you can redistribute it and/or modify |
| 23 | * it under the terms of the GNU General Public License as published by |
| 24 | * the Free Software Foundation; either version 2 of the License, or |
| 25 | * (at your option) any later version. |
| 26 | * |
| 27 | * This program is distributed in the hope that it will be useful, |
| 28 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 30 | * General Public License for more details. |
| 31 | * |
| 32 | * You should have received a copy of the GNU General Public License |
| 33 | * along with this program; if not, write to the Free Software |
| 34 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 35 | * |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 36 | */ |
| 37 | |
| 38 | |
| 39 | #include "internal.h" |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 40 | #define BB_DECLARE_EXTERN |
| 41 | #define bb_need_io_error |
| 42 | #include "messages.c" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 43 | #include <stdio.h> |
| 44 | #include <dirent.h> |
| 45 | #include <errno.h> |
| 46 | #include <fcntl.h> |
| 47 | #include <signal.h> |
| 48 | #include <time.h> |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 49 | #include <utime.h> |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 50 | #include <sys/types.h> |
Eric Andersen | 08b1034 | 1999-11-19 02:38:58 +0000 | [diff] [blame] | 51 | #include <sys/sysmacros.h> |
Matt Kraai | 43c8c38 | 2000-09-04 16:51:55 +0000 | [diff] [blame] | 52 | #include <getopt.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 53 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 54 | /* Tar file constants */ |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 55 | #ifndef MAJOR |
| 56 | #define MAJOR(dev) (((dev)>>8)&0xff) |
| 57 | #define MINOR(dev) ((dev)&0xff) |
| 58 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 59 | |
| 60 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 61 | /* POSIX tar Header Block, from POSIX 1003.1-1990 */ |
| 62 | struct TarHeader |
| 63 | { |
| 64 | /* byte offset */ |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 65 | char name[100]; /* 0-99 */ |
| 66 | char mode[8]; /* 100-107 */ |
| 67 | char uid[8]; /* 108-115 */ |
| 68 | char gid[8]; /* 116-123 */ |
| 69 | char size[12]; /* 124-135 */ |
| 70 | char mtime[12]; /* 136-147 */ |
| 71 | char chksum[8]; /* 148-155 */ |
| 72 | char typeflag; /* 156-156 */ |
| 73 | char linkname[100]; /* 157-256 */ |
| 74 | char magic[6]; /* 257-262 */ |
| 75 | char version[2]; /* 263-264 */ |
| 76 | char uname[32]; /* 265-296 */ |
| 77 | char gname[32]; /* 297-328 */ |
| 78 | char devmajor[8]; /* 329-336 */ |
| 79 | char devminor[8]; /* 337-344 */ |
| 80 | char prefix[155]; /* 345-499 */ |
| 81 | char padding[12]; /* 500-512 (pad to exactly the TAR_BLOCK_SIZE) */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 82 | }; |
| 83 | typedef struct TarHeader TarHeader; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 84 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 85 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 86 | /* A few useful constants */ |
| 87 | #define TAR_MAGIC "ustar" /* ustar and a null */ |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 88 | #define TAR_VERSION " " /* Be compatable with GNU tar format */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 89 | #define TAR_MAGIC_LEN 6 |
| 90 | #define TAR_VERSION_LEN 2 |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 91 | #define TAR_BLOCK_SIZE 512 |
| 92 | |
| 93 | /* A nice enum with all the possible tar file content types */ |
| 94 | enum TarFileType |
| 95 | { |
| 96 | REGTYPE = '0', /* regular file */ |
| 97 | REGTYPE0 = '\0', /* regular file (ancient bug compat)*/ |
| 98 | LNKTYPE = '1', /* hard link */ |
| 99 | SYMTYPE = '2', /* symbolic link */ |
| 100 | CHRTYPE = '3', /* character special */ |
| 101 | BLKTYPE = '4', /* block special */ |
| 102 | DIRTYPE = '5', /* directory */ |
| 103 | FIFOTYPE = '6', /* FIFO special */ |
| 104 | CONTTYPE = '7', /* reserved */ |
| 105 | }; |
| 106 | typedef enum TarFileType TarFileType; |
| 107 | |
| 108 | /* This struct ignores magic, non-numeric user name, |
| 109 | * non-numeric group name, and the checksum, since |
| 110 | * these are all ignored by BusyBox tar. */ |
| 111 | struct TarInfo |
| 112 | { |
| 113 | int tarFd; /* An open file descriptor for reading from the tarball */ |
| 114 | char * name; /* File name */ |
| 115 | mode_t mode; /* Unix mode, including device bits. */ |
| 116 | uid_t uid; /* Numeric UID */ |
| 117 | gid_t gid; /* Numeric GID */ |
| 118 | size_t size; /* Size of file */ |
| 119 | time_t mtime; /* Last-modified time */ |
| 120 | enum TarFileType type; /* Regular, directory, link, etc */ |
| 121 | char * linkname; /* Name for symbolic and hard links */ |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 122 | long devmajor; /* Major number for special device */ |
| 123 | long devminor; /* Minor number for special device */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 124 | }; |
| 125 | typedef struct TarInfo TarInfo; |
| 126 | |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 127 | /* Local procedures to restore files from a tar file. */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 128 | static int readTarFile(const char* tarName, int extractFlag, int listFlag, |
Matt Kraai | b92223b | 2000-09-04 08:25:42 +0000 | [diff] [blame] | 129 | int tostdoutFlag, int verboseFlag, char** extractList, |
| 130 | char** excludeList); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 131 | |
| 132 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 133 | |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 134 | #ifdef BB_FEATURE_TAR_CREATE |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 135 | /* Local procedures to save files into a tar file. */ |
Eric Andersen | 46a98df | 2000-09-19 21:35:14 +0000 | [diff] [blame] | 136 | static int writeTarFile(const char* tarName, int tostdoutFlag, |
| 137 | int verboseFlag, int argc, char **argv, char** excludeList); |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 138 | #endif |
| 139 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 140 | extern int tar_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 141 | { |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 142 | char** excludeList=NULL; |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 143 | #if defined BB_FEATURE_TAR_EXCLUDE |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 144 | int excludeListSize=0; |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 145 | #endif |
Eric Andersen | 108a443 | 2000-05-30 19:05:57 +0000 | [diff] [blame] | 146 | const char *tarName="-"; |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 147 | int listFlag = FALSE; |
| 148 | int extractFlag = FALSE; |
| 149 | int createFlag = FALSE; |
| 150 | int verboseFlag = FALSE; |
| 151 | int tostdoutFlag = FALSE; |
Eric Andersen | 0102a9f | 2000-09-23 22:36:24 +0000 | [diff] [blame^] | 152 | int firstOpt = TRUE; |
Eric Andersen | 46a98df | 2000-09-19 21:35:14 +0000 | [diff] [blame] | 153 | int stopIt; |
| 154 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 155 | |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 156 | if (argc <= 1) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 157 | usage(tar_usage); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 158 | |
Eric Andersen | fdd5103 | 2000-08-02 18:48:26 +0000 | [diff] [blame] | 159 | /* do normal option parsing */ |
Eric Andersen | 0102a9f | 2000-09-23 22:36:24 +0000 | [diff] [blame^] | 160 | while (--argc > 0 && ((*argv && **(++argv) == '-') || |
| 161 | (firstOpt==TRUE && strspn(*argv, "-cxt") ))) { |
| 162 | firstOpt=FALSE; |
Eric Andersen | 46a98df | 2000-09-19 21:35:14 +0000 | [diff] [blame] | 163 | stopIt=FALSE; |
| 164 | while (stopIt==FALSE && *argv && **argv) { |
| 165 | switch (**argv) { |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 166 | case 'c': |
| 167 | if (extractFlag == TRUE || listFlag == TRUE) |
| 168 | goto flagError; |
| 169 | createFlag = TRUE; |
| 170 | break; |
Eric Andersen | fdd5103 | 2000-08-02 18:48:26 +0000 | [diff] [blame] | 171 | case 'x': |
| 172 | if (listFlag == TRUE || createFlag == TRUE) |
| 173 | goto flagError; |
| 174 | extractFlag = TRUE; |
| 175 | break; |
| 176 | case 't': |
| 177 | if (extractFlag == TRUE || createFlag == TRUE) |
| 178 | goto flagError; |
| 179 | listFlag = TRUE; |
| 180 | break; |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 181 | case 'v': |
| 182 | verboseFlag = TRUE; |
| 183 | break; |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 184 | case 'O': |
| 185 | tostdoutFlag = TRUE; |
Eric Andersen | 46a98df | 2000-09-19 21:35:14 +0000 | [diff] [blame] | 186 | tarName = "-"; |
Eric Andersen | fdd5103 | 2000-08-02 18:48:26 +0000 | [diff] [blame] | 187 | break; |
| 188 | case 'f': |
Eric Andersen | 46a98df | 2000-09-19 21:35:14 +0000 | [diff] [blame] | 189 | if (--argc == 0) { |
| 190 | fatalError( "Option requires an argument: No file specified\n"); |
| 191 | } |
Eric Andersen | fdd5103 | 2000-08-02 18:48:26 +0000 | [diff] [blame] | 192 | if (*tarName != '-') |
| 193 | fatalError( "Only one 'f' option allowed\n"); |
Eric Andersen | 46a98df | 2000-09-19 21:35:14 +0000 | [diff] [blame] | 194 | tarName = *(++argv); |
| 195 | if (tarName == NULL) |
| 196 | fatalError( "Option requires an argument: No file specified\n"); |
| 197 | if (!strcmp(tarName, "-") && createFlag == TRUE) |
| 198 | tostdoutFlag = TRUE; |
| 199 | stopIt=TRUE; |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 200 | break; |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 201 | #if defined BB_FEATURE_TAR_EXCLUDE |
Matt Kraai | 43c8c38 | 2000-09-04 16:51:55 +0000 | [diff] [blame] | 202 | case 'e': |
Eric Andersen | 46a98df | 2000-09-19 21:35:14 +0000 | [diff] [blame] | 203 | if (strcmp(*argv, "-exclude")==0) { |
| 204 | if (--argc == 0) { |
| 205 | fatalError( "Option requires an argument: No file specified\n"); |
| 206 | } |
| 207 | excludeList=xrealloc( excludeList, sizeof(char**) * (excludeListSize+2)); |
| 208 | excludeList[excludeListSize] = *(++argv); |
| 209 | /* Remove leading "/"s */ |
| 210 | if (*excludeList[excludeListSize] =='/') |
| 211 | excludeList[excludeListSize] = (excludeList[excludeListSize])+1; |
| 212 | /* Tack a NULL onto the end of the list */ |
| 213 | excludeList[++excludeListSize] = NULL; |
| 214 | stopIt=TRUE; |
| 215 | break; |
| 216 | } |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 217 | #endif |
Eric Andersen | 46a98df | 2000-09-19 21:35:14 +0000 | [diff] [blame] | 218 | case '-': |
| 219 | break; |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 220 | default: |
Matt Kraai | 43c8c38 | 2000-09-04 16:51:55 +0000 | [diff] [blame] | 221 | usage(tar_usage); |
Eric Andersen | 46a98df | 2000-09-19 21:35:14 +0000 | [diff] [blame] | 222 | } |
| 223 | ++(*argv); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 224 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 227 | /* |
| 228 | * Do the correct type of action supplying the rest of the |
| 229 | * command line arguments as the list of files to process. |
| 230 | */ |
| 231 | if (createFlag == TRUE) { |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 232 | #ifndef BB_FEATURE_TAR_CREATE |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 233 | fatalError( "This version of tar was not compiled with tar creation support.\n"); |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 234 | #else |
Eric Andersen | 46a98df | 2000-09-19 21:35:14 +0000 | [diff] [blame] | 235 | exit(writeTarFile(tarName, tostdoutFlag, verboseFlag, argc, argv, excludeList)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 236 | #endif |
Erik Andersen | 8ea7d8c | 2000-05-20 00:40:08 +0000 | [diff] [blame] | 237 | } |
| 238 | if (listFlag == TRUE || extractFlag == TRUE) { |
Matt Kraai | b92223b | 2000-09-04 08:25:42 +0000 | [diff] [blame] | 239 | exit(readTarFile(tarName, extractFlag, listFlag, tostdoutFlag, verboseFlag, &argv[optind], excludeList)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 240 | } |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 241 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 242 | flagError: |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 243 | fatalError( "Exactly one of 'c', 'x' or 't' must be specified\n"); |
| 244 | } |
| 245 | |
| 246 | static void |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 247 | fixUpPermissions(TarInfo *header) |
| 248 | { |
| 249 | struct utimbuf t; |
| 250 | /* Now set permissions etc for the new file */ |
| 251 | chown(header->name, header->uid, header->gid); |
| 252 | chmod(header->name, header->mode); |
| 253 | /* Reset the time */ |
| 254 | t.actime = time(0); |
| 255 | t.modtime = header->mtime; |
| 256 | utime(header->name, &t); |
| 257 | } |
| 258 | |
| 259 | static int |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 260 | tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag) |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 261 | { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 262 | size_t writeSize; |
| 263 | size_t readSize; |
| 264 | size_t actualWriteSz; |
| 265 | char buffer[BUFSIZ]; |
| 266 | size_t size = header->size; |
| 267 | int outFd=fileno(stdout); |
| 268 | |
| 269 | /* Open the file to be written, if a file is supposed to be written */ |
| 270 | if (extractFlag==TRUE && tostdoutFlag==FALSE) { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 271 | /* Create the path to the file, just in case it isn't there... |
| 272 | * This should not screw up path permissions or anything. */ |
| 273 | createPath(header->name, 0777); |
Eric Andersen | 0c6a970 | 2000-06-09 20:51:50 +0000 | [diff] [blame] | 274 | if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY, |
| 275 | header->mode & ~S_IFMT)) < 0) { |
| 276 | errorMsg(io_error, header->name, strerror(errno)); |
| 277 | return( FALSE); |
| 278 | } |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /* Write out the file, if we are supposed to be doing that */ |
| 282 | while ( size > 0 ) { |
| 283 | actualWriteSz=0; |
| 284 | if ( size > sizeof(buffer) ) |
| 285 | writeSize = readSize = sizeof(buffer); |
| 286 | else { |
| 287 | int mod = size % 512; |
| 288 | if ( mod != 0 ) |
| 289 | readSize = size + (512 - mod); |
| 290 | else |
| 291 | readSize = size; |
| 292 | writeSize = size; |
| 293 | } |
| 294 | if ( (readSize = fullRead(header->tarFd, buffer, readSize)) <= 0 ) { |
| 295 | /* Tarball seems to have a problem */ |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 296 | errorMsg("Unexpected EOF in archive\n"); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 297 | return( FALSE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 298 | } |
| 299 | if ( readSize < writeSize ) |
| 300 | writeSize = readSize; |
| 301 | |
| 302 | /* Write out the file, if we are supposed to be doing that */ |
| 303 | if (extractFlag==TRUE) { |
| 304 | |
| 305 | if ((actualWriteSz=fullWrite(outFd, buffer, writeSize)) != writeSize ) { |
| 306 | /* Output file seems to have a problem */ |
| 307 | errorMsg(io_error, header->name, strerror(errno)); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 308 | return( FALSE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 309 | } |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 310 | } else { |
| 311 | actualWriteSz=writeSize; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | size -= actualWriteSz; |
| 315 | } |
| 316 | |
| 317 | /* Now we are done writing the file out, so try |
| 318 | * and fix up the permissions and whatnot */ |
| 319 | if (extractFlag==TRUE && tostdoutFlag==FALSE) { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 320 | close(outFd); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 321 | fixUpPermissions(header); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 322 | } |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 323 | return( TRUE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 326 | static int |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 327 | tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag) |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 328 | { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 329 | |
| 330 | if (extractFlag==FALSE || tostdoutFlag==TRUE) |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 331 | return( TRUE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 332 | |
| 333 | if (createPath(header->name, header->mode) != TRUE) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 334 | errorMsg("%s: Cannot mkdir: %s\n", |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 335 | header->name, strerror(errno)); |
| 336 | return( FALSE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 337 | } |
| 338 | /* make the final component, just in case it was |
| 339 | * omitted by createPath() (which will skip the |
| 340 | * directory if it doesn't have a terminating '/') */ |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 341 | if (mkdir(header->name, header->mode) == 0) { |
| 342 | fixUpPermissions(header); |
| 343 | } |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 344 | return( TRUE); |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 347 | static int |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 348 | tarExtractHardLink(TarInfo *header, int extractFlag, int tostdoutFlag) |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 349 | { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 350 | if (extractFlag==FALSE || tostdoutFlag==TRUE) |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 351 | return( TRUE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 352 | |
| 353 | if (link(header->linkname, header->name) < 0) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 354 | errorMsg("%s: Cannot create hard link to '%s': %s\n", |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 355 | header->name, header->linkname, strerror(errno)); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 356 | return( FALSE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /* Now set permissions etc for the new directory */ |
| 360 | fixUpPermissions(header); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 361 | return( TRUE); |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 364 | static int |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 365 | tarExtractSymLink(TarInfo *header, int extractFlag, int tostdoutFlag) |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 366 | { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 367 | if (extractFlag==FALSE || tostdoutFlag==TRUE) |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 368 | return( TRUE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 369 | |
| 370 | #ifdef S_ISLNK |
| 371 | if (symlink(header->linkname, header->name) < 0) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 372 | errorMsg("%s: Cannot create symlink to '%s': %s\n", |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 373 | header->name, header->linkname, strerror(errno)); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 374 | return( FALSE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 375 | } |
| 376 | /* Try to change ownership of the symlink. |
| 377 | * If libs doesn't support that, don't bother. |
| 378 | * Changing the pointed-to-file is the Wrong Thing(tm). |
| 379 | */ |
| 380 | #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) |
| 381 | lchown(header->name, header->uid, header->gid); |
| 382 | #endif |
| 383 | |
| 384 | /* Do not change permissions or date on symlink, |
| 385 | * since it changes the pointed to file instead. duh. */ |
| 386 | #else |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 387 | errorMsg("%s: Cannot create symlink to '%s': %s\n", |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 388 | header->name, header->linkname, |
| 389 | "symlinks not supported"); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 390 | #endif |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 391 | return( TRUE); |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 394 | static int |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 395 | tarExtractSpecial(TarInfo *header, int extractFlag, int tostdoutFlag) |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 396 | { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 397 | if (extractFlag==FALSE || tostdoutFlag==TRUE) |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 398 | return( TRUE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 399 | |
| 400 | if (S_ISCHR(header->mode) || S_ISBLK(header->mode) || S_ISSOCK(header->mode)) { |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 401 | if (mknod(header->name, header->mode, makedev(header->devmajor, header->devminor)) < 0) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 402 | errorMsg("%s: Cannot mknod: %s\n", |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 403 | header->name, strerror(errno)); |
| 404 | return( FALSE); |
| 405 | } |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 406 | } else if (S_ISFIFO(header->mode)) { |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 407 | if (mkfifo(header->name, header->mode) < 0) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 408 | errorMsg("%s: Cannot mkfifo: %s\n", |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 409 | header->name, strerror(errno)); |
| 410 | return( FALSE); |
| 411 | } |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /* Now set permissions etc for the new directory */ |
| 415 | fixUpPermissions(header); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 416 | return( TRUE); |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 419 | /* Read an octal value in a field of the specified width, with optional |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 420 | * spaces on both sides of the number and with an optional null character |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 421 | * at the end. Returns -1 on an illegal format. */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 422 | static long getOctal(const char *cp, int size) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 423 | { |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 424 | long val = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 425 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 426 | for(;(size > 0) && (*cp == ' '); cp++, size--); |
| 427 | if ((size == 0) || !isOctal(*cp)) |
| 428 | return -1; |
| 429 | for(; (size > 0) && isOctal(*cp); size--) { |
| 430 | val = val * 8 + *cp++ - '0'; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 431 | } |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 432 | for (;(size > 0) && (*cp == ' '); cp++, size--); |
| 433 | if ((size > 0) && *cp) |
| 434 | return -1; |
| 435 | return val; |
| 436 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 437 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 438 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 439 | /* Parse the tar header and fill in the nice struct with the details */ |
| 440 | static int |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 441 | readTarHeader(struct TarHeader *rawHeader, struct TarInfo *header) |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 442 | { |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 443 | int i; |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 444 | long chksum, sum=0; |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 445 | unsigned char *s = (unsigned char *)rawHeader; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 446 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 447 | header->name = rawHeader->name; |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 448 | /* Check for and relativify any absolute paths */ |
| 449 | if ( *(header->name) == '/' ) { |
| 450 | static int alreadyWarned=FALSE; |
| 451 | |
| 452 | while (*(header->name) == '/') |
| 453 | ++*(header->name); |
| 454 | |
| 455 | if (alreadyWarned == FALSE) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 456 | errorMsg("Removing leading '/' from member names\n"); |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 457 | alreadyWarned = TRUE; |
| 458 | } |
| 459 | } |
| 460 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 461 | header->mode = getOctal(rawHeader->mode, sizeof(rawHeader->mode)); |
| 462 | header->uid = getOctal(rawHeader->uid, sizeof(rawHeader->uid)); |
| 463 | header->gid = getOctal(rawHeader->gid, sizeof(rawHeader->gid)); |
| 464 | header->size = getOctal(rawHeader->size, sizeof(rawHeader->size)); |
| 465 | header->mtime = getOctal(rawHeader->mtime, sizeof(rawHeader->mtime)); |
| 466 | chksum = getOctal(rawHeader->chksum, sizeof(rawHeader->chksum)); |
| 467 | header->type = rawHeader->typeflag; |
| 468 | header->linkname = rawHeader->linkname; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 469 | header->devmajor = getOctal(rawHeader->devmajor, sizeof(rawHeader->devmajor)); |
| 470 | header->devminor = getOctal(rawHeader->devminor, sizeof(rawHeader->devminor)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 471 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 472 | /* Check the checksum */ |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 473 | for (i = sizeof(*rawHeader); i-- != 0;) { |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 474 | sum += *s++; |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 475 | } |
| 476 | /* Remove the effects of the checksum field (replace |
| 477 | * with blanks for the purposes of the checksum) */ |
| 478 | s = rawHeader->chksum; |
| 479 | for (i = sizeof(rawHeader->chksum) ; i-- != 0;) { |
| 480 | sum -= *s++; |
| 481 | } |
| 482 | sum += ' ' * sizeof(rawHeader->chksum); |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 483 | if (sum == chksum ) |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 484 | return ( TRUE); |
| 485 | return( FALSE); |
| 486 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 487 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 488 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 489 | /* |
| 490 | * Read a tar file and extract or list the specified files within it. |
| 491 | * If the list is empty than all files are extracted or listed. |
| 492 | */ |
| 493 | static int readTarFile(const char* tarName, int extractFlag, int listFlag, |
Matt Kraai | b92223b | 2000-09-04 08:25:42 +0000 | [diff] [blame] | 494 | int tostdoutFlag, int verboseFlag, char** extractList, |
| 495 | char** excludeList) |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 496 | { |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 497 | int status, tarFd=-1; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 498 | int errorFlag=FALSE; |
| 499 | TarHeader rawHeader; |
| 500 | TarInfo header; |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 501 | char** tmpList; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 502 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 503 | /* Open the tar file for reading. */ |
| 504 | if (!strcmp(tarName, "-")) |
| 505 | tarFd = fileno(stdin); |
| 506 | else |
| 507 | tarFd = open(tarName, O_RDONLY); |
| 508 | if (tarFd < 0) { |
Erik Andersen | deea048 | 2000-03-25 23:09:29 +0000 | [diff] [blame] | 509 | errorMsg( "Error opening '%s': %s\n", tarName, strerror(errno)); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 510 | return ( FALSE); |
Erik Andersen | 06936df | 2000-01-23 02:14:20 +0000 | [diff] [blame] | 511 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 512 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 513 | /* Set the umask for this process so it doesn't |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 514 | * screw up permission setting for us later. */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 515 | umask(0); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 516 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 517 | /* Read the tar file, and iterate over it one file at a time */ |
| 518 | while ( (status = fullRead(tarFd, (char*)&rawHeader, TAR_BLOCK_SIZE)) == TAR_BLOCK_SIZE ) { |
Erik Andersen | e272915 | 2000-02-18 21:34:17 +0000 | [diff] [blame] | 519 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 520 | /* First, try to read the header */ |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 521 | if ( readTarHeader(&rawHeader, &header) == FALSE ) { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 522 | if ( *(header.name) == '\0' ) { |
| 523 | goto endgame; |
| 524 | } else { |
| 525 | errorFlag=TRUE; |
| 526 | errorMsg("Bad tar header, skipping\n"); |
| 527 | continue; |
| 528 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 529 | } |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 530 | if ( *(header.name) == '\0' ) |
| 531 | goto endgame; |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 532 | header.tarFd = tarFd; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 533 | |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 534 | #if defined BB_FEATURE_TAR_EXCLUDE |
| 535 | { |
| 536 | int skipFlag=FALSE; |
| 537 | /* Check for excluded files.... */ |
| 538 | for (tmpList=excludeList; tmpList && *tmpList; tmpList++) { |
| 539 | /* Do some extra hoop jumping for when directory names |
| 540 | * end in '/' but the entry in tmpList doesn't */ |
| 541 | if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || ( |
| 542 | header.name[strlen(header.name)-1]=='/' |
| 543 | && strncmp( *tmpList, header.name, |
| 544 | MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) { |
| 545 | /* If it is a regular file, pretend to extract it with |
| 546 | * the extractFlag set to FALSE, so the junk in the tarball |
| 547 | * is properly skipped over */ |
| 548 | if ( header.type==REGTYPE || header.type==REGTYPE0 ) { |
| 549 | tarExtractRegularFile(&header, FALSE, FALSE); |
| 550 | } |
| 551 | skipFlag=TRUE; |
| 552 | break; |
| 553 | } |
| 554 | } |
| 555 | /* There are not the droids you're looking for, move along */ |
| 556 | if (skipFlag==TRUE) |
| 557 | continue; |
| 558 | } |
| 559 | #endif |
Matt Kraai | b92223b | 2000-09-04 08:25:42 +0000 | [diff] [blame] | 560 | if (*extractList != NULL) { |
| 561 | int skipFlag = TRUE; |
| 562 | for (tmpList = extractList; *tmpList != NULL; tmpList++) { |
| 563 | if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || ( |
| 564 | header.name[strlen(header.name)-1]=='/' |
| 565 | && strncmp( *tmpList, header.name, |
| 566 | MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) { |
| 567 | /* If it is a regular file, pretend to extract it with |
| 568 | * the extractFlag set to FALSE, so the junk in the tarball |
| 569 | * is properly skipped over */ |
| 570 | skipFlag = FALSE; |
| 571 | break; |
| 572 | } |
| 573 | } |
| 574 | /* There are not the droids you're looking for, move along */ |
| 575 | if (skipFlag == TRUE) { |
| 576 | if ( header.type==REGTYPE || header.type==REGTYPE0 ) |
| 577 | tarExtractRegularFile(&header, FALSE, FALSE); |
| 578 | continue; |
| 579 | } |
| 580 | } |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 581 | |
Matt Kraai | 82cfbad | 2000-09-15 21:18:43 +0000 | [diff] [blame] | 582 | if (listFlag == TRUE) { |
Matt Kraai | 6fc2a7d | 2000-09-15 22:23:41 +0000 | [diff] [blame] | 583 | /* Special treatment if the list (-t) flag is on */ |
| 584 | if (verboseFlag == TRUE) { |
| 585 | int len, len1; |
| 586 | char buf[35]; |
| 587 | struct tm *tm = localtime (&(header.mtime)); |
| 588 | |
| 589 | len=printf("%s ", modeString(header.mode)); |
| 590 | memset(buf, 0, 8*sizeof(char)); |
| 591 | my_getpwuid(buf, header.uid); |
| 592 | if (! *buf) |
| 593 | len+=printf("%d", header.uid); |
| 594 | else |
| 595 | len+=printf("%s", buf); |
| 596 | memset(buf, 0, 8*sizeof(char)); |
| 597 | my_getgrgid(buf, header.gid); |
| 598 | if (! *buf) |
| 599 | len+=printf("/%-d ", header.gid); |
| 600 | else |
| 601 | len+=printf("/%-s ", buf); |
| 602 | |
| 603 | if (header.type==CHRTYPE || header.type==BLKTYPE) { |
| 604 | len1=snprintf(buf, sizeof(buf), "%ld,%-ld ", |
| 605 | header.devmajor, header.devminor); |
| 606 | } else { |
| 607 | len1=snprintf(buf, sizeof(buf), "%lu ", (long)header.size); |
| 608 | } |
| 609 | /* Jump through some hoops to make the columns match up */ |
| 610 | for(;(len+len1)<31;len++) |
| 611 | printf(" "); |
| 612 | printf(buf); |
| 613 | |
| 614 | /* Use ISO 8610 time format */ |
| 615 | if (tm) { |
| 616 | printf ("%04d-%02d-%02d %02d:%02d:%02d ", |
| 617 | tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, |
| 618 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 619 | } |
| 620 | } |
Eric Andersen | fdd5103 | 2000-08-02 18:48:26 +0000 | [diff] [blame] | 621 | printf("%s", header.name); |
Matt Kraai | 82cfbad | 2000-09-15 21:18:43 +0000 | [diff] [blame] | 622 | if (verboseFlag == TRUE) { |
| 623 | if (header.type==LNKTYPE) /* If this is a link, say so */ |
| 624 | printf(" link to %s", header.linkname); |
| 625 | else if (header.type==SYMTYPE) |
| 626 | printf(" -> %s", header.linkname); |
| 627 | } |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 628 | printf("\n"); |
| 629 | } |
| 630 | |
Matt Kraai | 6fc2a7d | 2000-09-15 22:23:41 +0000 | [diff] [blame] | 631 | /* List contents if we are supposed to do that */ |
| 632 | if (verboseFlag == TRUE && extractFlag == TRUE) { |
| 633 | /* Now the normal listing */ |
| 634 | FILE *vbFd = stdout; |
| 635 | if (tostdoutFlag == TRUE) // If the archive goes to stdout, verbose to stderr |
| 636 | vbFd = stderr; |
| 637 | fprintf(vbFd, "%s\n", header.name); |
| 638 | } |
| 639 | |
Matt Kraai | f446297 | 2000-09-01 02:50:48 +0000 | [diff] [blame] | 640 | /* Remove files if we would overwrite them */ |
Matt Kraai | da542f3 | 2000-09-01 02:53:01 +0000 | [diff] [blame] | 641 | if (extractFlag == TRUE && tostdoutFlag == FALSE) |
Matt Kraai | f446297 | 2000-09-01 02:50:48 +0000 | [diff] [blame] | 642 | unlink(header.name); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 643 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 644 | /* If we got here, we can be certain we have a legitimate |
| 645 | * header to work with. So work with it. */ |
| 646 | switch ( header.type ) { |
| 647 | case REGTYPE: |
| 648 | case REGTYPE0: |
| 649 | /* If the name ends in a '/' then assume it is |
| 650 | * supposed to be a directory, and fall through */ |
| 651 | if (header.name[strlen(header.name)-1] != '/') { |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 652 | if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE) |
| 653 | errorFlag=TRUE; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 654 | break; |
| 655 | } |
| 656 | case DIRTYPE: |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 657 | if (tarExtractDirectory( &header, extractFlag, tostdoutFlag)==FALSE) |
| 658 | errorFlag=TRUE; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 659 | break; |
| 660 | case LNKTYPE: |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 661 | if (tarExtractHardLink( &header, extractFlag, tostdoutFlag)==FALSE) |
| 662 | errorFlag=TRUE; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 663 | break; |
| 664 | case SYMTYPE: |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 665 | if (tarExtractSymLink( &header, extractFlag, tostdoutFlag)==FALSE) |
| 666 | errorFlag=TRUE; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 667 | break; |
| 668 | case CHRTYPE: |
| 669 | case BLKTYPE: |
| 670 | case FIFOTYPE: |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 671 | if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE) |
| 672 | errorFlag=TRUE; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 673 | break; |
| 674 | default: |
| 675 | close( tarFd); |
| 676 | return( FALSE); |
| 677 | } |
| 678 | } |
| 679 | close(tarFd); |
| 680 | if (status > 0) { |
| 681 | /* Bummer - we read a partial header */ |
Erik Andersen | deea048 | 2000-03-25 23:09:29 +0000 | [diff] [blame] | 682 | errorMsg( "Error reading '%s': %s\n", tarName, strerror(errno)); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 683 | return ( FALSE); |
| 684 | } |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 685 | else if (errorFlag==TRUE) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 686 | errorMsg( "Error exit delayed from previous errors\n"); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 687 | return( FALSE); |
| 688 | } else |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 689 | return( status); |
| 690 | |
| 691 | /* Stuff to do when we are done */ |
| 692 | endgame: |
| 693 | close( tarFd); |
| 694 | if ( *(header.name) == '\0' ) { |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 695 | if (errorFlag==TRUE) |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 696 | errorMsg( "Error exit delayed from previous errors\n"); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 697 | else |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 698 | return( TRUE); |
| 699 | } |
| 700 | return( FALSE); |
| 701 | } |
| 702 | |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 703 | |
| 704 | #ifdef BB_FEATURE_TAR_CREATE |
| 705 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 706 | /* Some info to be carried along when creating a new tarball */ |
| 707 | struct TarBallInfo |
| 708 | { |
| 709 | char* fileName; /* File name of the tarball */ |
| 710 | int tarFd; /* Open-for-write file descriptor |
| 711 | for the tarball */ |
| 712 | struct stat statBuf; /* Stat info for the tarball, letting |
| 713 | us know the inode and device that the |
| 714 | tarball lives, so we can avoid trying |
| 715 | to include the tarball into itself */ |
| 716 | int verboseFlag; /* Whether to print extra stuff or not */ |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 717 | char** excludeList; /* List of files to not include */ |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 718 | }; |
| 719 | typedef struct TarBallInfo TarBallInfo; |
| 720 | |
| 721 | |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 722 | /* Put an octal string into the specified buffer. |
| 723 | * The number is zero and space padded and possibly null padded. |
| 724 | * Returns TRUE if successful. */ |
| 725 | static int putOctal (char *cp, int len, long value) |
| 726 | { |
| 727 | int tempLength; |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 728 | char tempBuffer[32]; |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 729 | char *tempString = tempBuffer; |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 730 | |
| 731 | /* Create a string of the specified length with an initial space, |
| 732 | * leading zeroes and the octal number, and a trailing null. */ |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 733 | sprintf (tempString, "%0*lo", len - 1, value); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 734 | |
| 735 | /* If the string is too large, suppress the leading space. */ |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 736 | tempLength = strlen (tempString) + 1; |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 737 | if (tempLength > len) { |
| 738 | tempLength--; |
| 739 | tempString++; |
| 740 | } |
| 741 | |
| 742 | /* If the string is still too large, suppress the trailing null. */ |
| 743 | if (tempLength > len) |
| 744 | tempLength--; |
| 745 | |
| 746 | /* If the string is still too large, fail. */ |
| 747 | if (tempLength > len) |
| 748 | return FALSE; |
| 749 | |
| 750 | /* Copy the string to the field. */ |
| 751 | memcpy (cp, tempString, len); |
| 752 | |
| 753 | return TRUE; |
| 754 | } |
| 755 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 756 | /* Write out a tar header for the specified file/directory/whatever */ |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 757 | static int |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 758 | writeTarHeader(struct TarBallInfo *tbInfo, const char *fileName, struct stat *statbuf) |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 759 | { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 760 | long chksum=0; |
| 761 | struct TarHeader header; |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 762 | #if defined BB_FEATURE_TAR_EXCLUDE |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 763 | char** tmpList; |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 764 | #endif |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 765 | const unsigned char *cp = (const unsigned char *) &header; |
| 766 | ssize_t size = sizeof(struct TarHeader); |
Eric Andersen | fdd5103 | 2000-08-02 18:48:26 +0000 | [diff] [blame] | 767 | |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 768 | memset( &header, 0, size); |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 769 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 770 | if (*fileName=='/') { |
| 771 | static int alreadyWarned=FALSE; |
| 772 | if (alreadyWarned==FALSE) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 773 | errorMsg("Removing leading '/' from member names\n"); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 774 | alreadyWarned=TRUE; |
| 775 | } |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 776 | strncpy(header.name, fileName+1, sizeof(header.name)); |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 777 | } |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 778 | else { |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 779 | strncpy(header.name, fileName, sizeof(header.name)); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 780 | } |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 781 | |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 782 | #if defined BB_FEATURE_TAR_EXCLUDE |
| 783 | /* Check for excluded files.... */ |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 784 | for (tmpList=tbInfo->excludeList; tmpList && *tmpList; tmpList++) { |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 785 | /* Do some extra hoop jumping for when directory names |
| 786 | * end in '/' but the entry in tmpList doesn't */ |
| 787 | if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || ( |
| 788 | header.name[strlen(header.name)-1]=='/' |
| 789 | && strncmp( *tmpList, header.name, |
| 790 | MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) { |
| 791 | /* Set the mode to something that is not a regular file, thereby |
| 792 | * faking out writeTarFile into thinking that nothing further need |
| 793 | * be done for this file. Yes, I know this is ugly, but it works. */ |
| 794 | statbuf->st_mode = 0; |
| 795 | return( TRUE); |
| 796 | } |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 797 | } |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 798 | #endif |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 799 | |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 800 | putOctal(header.mode, sizeof(header.mode), statbuf->st_mode); |
| 801 | putOctal(header.uid, sizeof(header.uid), statbuf->st_uid); |
| 802 | putOctal(header.gid, sizeof(header.gid), statbuf->st_gid); |
| 803 | putOctal(header.size, sizeof(header.size), 0); /* Regular file size is handled later */ |
| 804 | putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime); |
| 805 | strncpy(header.magic, TAR_MAGIC TAR_VERSION, |
| 806 | TAR_MAGIC_LEN + TAR_VERSION_LEN ); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 807 | |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 808 | /* Enter the user and group names (default to root if it fails) */ |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 809 | my_getpwuid(header.uname, statbuf->st_uid); |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 810 | if (! *header.uname) |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 811 | strcpy(header.uname, "root"); |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 812 | my_getgrgid(header.gname, statbuf->st_gid); |
| 813 | if (! *header.uname) |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 814 | strcpy(header.uname, "root"); |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 815 | |
Erik Andersen | 95c1c1e | 2000-04-14 21:45:29 +0000 | [diff] [blame] | 816 | /* WARNING/NOTICE: I break Hard Links */ |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 817 | if (S_ISLNK(statbuf->st_mode)) { |
Eric Andersen | 3adffb7 | 2000-06-26 10:54:06 +0000 | [diff] [blame] | 818 | int link_size=0; |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 819 | char buffer[BUFSIZ]; |
| 820 | header.typeflag = SYMTYPE; |
Eric Andersen | 3adffb7 | 2000-06-26 10:54:06 +0000 | [diff] [blame] | 821 | link_size = readlink(fileName, buffer, sizeof(buffer) - 1); |
| 822 | if ( link_size < 0) { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 823 | errorMsg("Error reading symlink '%s': %s\n", header.name, strerror(errno)); |
| 824 | return ( FALSE); |
| 825 | } |
Eric Andersen | 3adffb7 | 2000-06-26 10:54:06 +0000 | [diff] [blame] | 826 | buffer[link_size] = '\0'; |
Erik Andersen | 95c1c1e | 2000-04-14 21:45:29 +0000 | [diff] [blame] | 827 | strncpy(header.linkname, buffer, sizeof(header.linkname)); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 828 | } else if (S_ISDIR(statbuf->st_mode)) { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 829 | header.typeflag = DIRTYPE; |
| 830 | strncat(header.name, "/", sizeof(header.name)); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 831 | } else if (S_ISCHR(statbuf->st_mode)) { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 832 | header.typeflag = CHRTYPE; |
| 833 | putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev)); |
| 834 | putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev)); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 835 | } else if (S_ISBLK(statbuf->st_mode)) { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 836 | header.typeflag = BLKTYPE; |
| 837 | putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev)); |
| 838 | putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev)); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 839 | } else if (S_ISFIFO(statbuf->st_mode)) { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 840 | header.typeflag = FIFOTYPE; |
| 841 | } else if (S_ISREG(statbuf->st_mode)) { |
| 842 | header.typeflag = REGTYPE; |
| 843 | putOctal(header.size, sizeof(header.size), statbuf->st_size); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 844 | } else { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 845 | errorMsg("%s: Unknown file type\n", fileName); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 846 | return ( FALSE); |
| 847 | } |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 848 | |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 849 | /* Calculate and store the checksum (i.e. the sum of all of the bytes of |
| 850 | * the header). The checksum field must be filled with blanks for the |
| 851 | * calculation. The checksum field is formatted differently from the |
| 852 | * other fields: it has [6] digits, a null, then a space -- rather than |
| 853 | * digits, followed by a null like the other fields... */ |
| 854 | memset(header.chksum, ' ', sizeof(header.chksum)); |
| 855 | cp = (const unsigned char *) &header; |
| 856 | while (size-- > 0) |
| 857 | chksum += *cp++; |
| 858 | putOctal(header.chksum, 7, chksum); |
| 859 | |
| 860 | /* Now write the header out to disk */ |
| 861 | if ((size=fullWrite(tbInfo->tarFd, (char*)&header, sizeof(struct TarHeader))) < 0) { |
| 862 | errorMsg(io_error, fileName, strerror(errno)); |
| 863 | return ( FALSE); |
| 864 | } |
| 865 | /* Pad the header up to the tar block size */ |
| 866 | for (; size<TAR_BLOCK_SIZE; size++) { |
| 867 | write(tbInfo->tarFd, "\0", 1); |
| 868 | } |
| 869 | /* Now do the verbose thing (or not) */ |
Eric Andersen | fdd5103 | 2000-08-02 18:48:26 +0000 | [diff] [blame] | 870 | if (tbInfo->verboseFlag==TRUE) { |
| 871 | FILE *vbFd = stdout; |
| 872 | if (tbInfo->tarFd == fileno(stdout)) // If the archive goes to stdout, verbose to stderr |
| 873 | vbFd = stderr; |
| 874 | fprintf(vbFd, "%s\n", header.name); |
| 875 | } |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 876 | |
| 877 | return ( TRUE); |
| 878 | } |
| 879 | |
| 880 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 881 | static int writeFileToTarball(const char *fileName, struct stat *statbuf, void* userData) |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 882 | { |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 883 | struct TarBallInfo *tbInfo = (struct TarBallInfo *)userData; |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 884 | |
| 885 | /* It is against the rules to archive a socket */ |
| 886 | if (S_ISSOCK(statbuf->st_mode)) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 887 | errorMsg("%s: socket ignored\n", fileName); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 888 | return( TRUE); |
| 889 | } |
| 890 | |
| 891 | /* It is a bad idea to store the archive we are in the process of creating, |
| 892 | * so check the device and inode to be sure that this particular file isn't |
| 893 | * the new tarball */ |
| 894 | if (tbInfo->statBuf.st_dev == statbuf->st_dev && |
| 895 | tbInfo->statBuf.st_ino == statbuf->st_ino) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 896 | errorMsg("%s: file is the archive; skipping\n", fileName); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 897 | return( TRUE); |
| 898 | } |
| 899 | |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 900 | if (writeTarHeader(tbInfo, fileName, statbuf)==FALSE) { |
| 901 | return( FALSE); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 902 | } |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 903 | |
| 904 | /* Now, if the file is a regular file, copy it out to the tarball */ |
| 905 | if (S_ISREG(statbuf->st_mode)) { |
| 906 | int inputFileFd; |
| 907 | char buffer[BUFSIZ]; |
| 908 | ssize_t size=0, readSize=0; |
| 909 | |
| 910 | /* open the file we want to archive, and make sure all is well */ |
| 911 | if ((inputFileFd = open(fileName, O_RDONLY)) < 0) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 912 | errorMsg("%s: Cannot open: %s\n", fileName, strerror(errno)); |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 913 | return( FALSE); |
| 914 | } |
| 915 | |
| 916 | /* write the file to the archive */ |
| 917 | while ( (size = fullRead(inputFileFd, buffer, sizeof(buffer))) > 0 ) { |
| 918 | if (fullWrite(tbInfo->tarFd, buffer, size) != size ) { |
| 919 | /* Output file seems to have a problem */ |
| 920 | errorMsg(io_error, fileName, strerror(errno)); |
| 921 | return( FALSE); |
| 922 | } |
| 923 | readSize+=size; |
| 924 | } |
| 925 | if (size == -1) { |
| 926 | errorMsg(io_error, fileName, strerror(errno)); |
| 927 | return( FALSE); |
| 928 | } |
| 929 | /* Pad the file up to the tar block size */ |
| 930 | for (; (readSize%TAR_BLOCK_SIZE) != 0; readSize++) { |
| 931 | write(tbInfo->tarFd, "\0", 1); |
| 932 | } |
| 933 | close( inputFileFd); |
| 934 | } |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 935 | |
| 936 | return( TRUE); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 937 | } |
| 938 | |
Eric Andersen | 46a98df | 2000-09-19 21:35:14 +0000 | [diff] [blame] | 939 | static int writeTarFile(const char* tarName, int tostdoutFlag, |
| 940 | int verboseFlag, int argc, char **argv, char** excludeList) |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 941 | { |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 942 | int tarFd=-1; |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 943 | int errorFlag=FALSE; |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 944 | ssize_t size; |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 945 | struct TarBallInfo tbInfo; |
| 946 | tbInfo.verboseFlag = verboseFlag; |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 947 | |
| 948 | /* Make sure there is at least one file to tar up. */ |
| 949 | if (argc <= 0) |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 950 | fatalError("Cowardly refusing to create an empty archive\n"); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 951 | |
| 952 | /* Open the tar file for writing. */ |
Eric Andersen | 46a98df | 2000-09-19 21:35:14 +0000 | [diff] [blame] | 953 | if (tostdoutFlag == TRUE) |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 954 | tbInfo.tarFd = fileno(stdout); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 955 | else |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 956 | tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 957 | if (tbInfo.tarFd < 0) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 958 | errorMsg( "Error opening '%s': %s\n", tarName, strerror(errno)); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 959 | return ( FALSE); |
| 960 | } |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 961 | tbInfo.excludeList=excludeList; |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 962 | /* Store the stat info for the tarball's file, so |
| 963 | * can avoid including the tarball into itself.... */ |
| 964 | if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0) |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 965 | fatalError(io_error, tarName, strerror(errno)); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 966 | |
| 967 | /* Set the umask for this process so it doesn't |
| 968 | * screw up permission setting for us later. */ |
| 969 | umask(0); |
| 970 | |
| 971 | /* Read the directory/files and iterate over them one at a time */ |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 972 | while (argc-- > 0) { |
| 973 | if (recursiveAction(*argv++, TRUE, FALSE, FALSE, |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 974 | writeFileToTarball, writeFileToTarball, |
| 975 | (void*) &tbInfo) == FALSE) { |
| 976 | errorFlag = TRUE; |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 977 | } |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 978 | } |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 979 | /* Write two empty blocks to the end of the archive */ |
| 980 | for (size=0; size<(2*TAR_BLOCK_SIZE); size++) { |
| 981 | write(tbInfo.tarFd, "\0", 1); |
| 982 | } |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 983 | |
| 984 | /* To be pedantically correct, we would check if the tarball |
| 985 | * is smaller then 20 tar blocks, and pad it if it was smaller, |
| 986 | * but that isn't necessary for GNU tar interoperability, and |
| 987 | * so is considered a waste of space */ |
| 988 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 989 | /* Hang up the tools, close up shop, head home */ |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 990 | close(tarFd); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 991 | if (errorFlag == TRUE) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 992 | errorMsg("Error exit delayed from previous errors\n"); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 993 | return(FALSE); |
| 994 | } |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 995 | return( TRUE); |
| 996 | } |
| 997 | |
| 998 | |
| 999 | #endif |
| 1000 | |