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