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