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