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