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