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 | * |
Eric Andersen | 8ec10a9 | 2001-01-27 09:33:39 +0000 | [diff] [blame] | 9 | * Copyright (C) 1999,2000,2001 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 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 39 | #include <stdio.h> |
| 40 | #include <dirent.h> |
| 41 | #include <errno.h> |
| 42 | #include <fcntl.h> |
| 43 | #include <signal.h> |
| 44 | #include <time.h> |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 45 | #include <utime.h> |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 46 | #include <sys/types.h> |
Eric Andersen | 08b1034 | 1999-11-19 02:38:58 +0000 | [diff] [blame] | 47 | #include <sys/sysmacros.h> |
Matt Kraai | 43c8c38 | 2000-09-04 16:51:55 +0000 | [diff] [blame] | 48 | #include <getopt.h> |
Matt Kraai | be7499c | 2001-01-03 17:22:10 +0000 | [diff] [blame] | 49 | #include <fnmatch.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 50 | #include <string.h> |
| 51 | #include <stdlib.h> |
| 52 | #include <unistd.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 53 | #include "busybox.h" |
| 54 | #define BB_DECLARE_EXTERN |
| 55 | #define bb_need_io_error |
| 56 | #define bb_need_name_longer_than_foo |
| 57 | #include "messages.c" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 58 | |
Glenn L McGrath | 46f44d2 | 2000-12-10 01:57:30 +0000 | [diff] [blame] | 59 | #ifdef BB_FEATURE_TAR_GZIP |
| 60 | extern int unzip(int in, int out); |
Glenn L McGrath | 018e9e6 | 2001-03-28 07:27:26 +0000 | [diff] [blame] | 61 | extern int gz_open(FILE *compressed_file, int *pid); |
| 62 | extern void gz_close(int gunzip_pid); |
Glenn L McGrath | 46f44d2 | 2000-12-10 01:57:30 +0000 | [diff] [blame] | 63 | #endif |
| 64 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 65 | /* Tar file constants */ |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 66 | #ifndef MAJOR |
| 67 | #define MAJOR(dev) (((dev)>>8)&0xff) |
| 68 | #define MINOR(dev) ((dev)&0xff) |
| 69 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 70 | |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 71 | enum { NAME_SIZE = 100 }; /* because gcc won't let me use 'static const int' */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 72 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 73 | /* POSIX tar Header Block, from POSIX 1003.1-1990 */ |
| 74 | struct TarHeader |
| 75 | { |
| 76 | /* byte offset */ |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 77 | char name[NAME_SIZE]; /* 0-99 */ |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 78 | char mode[8]; /* 100-107 */ |
| 79 | char uid[8]; /* 108-115 */ |
| 80 | char gid[8]; /* 116-123 */ |
| 81 | char size[12]; /* 124-135 */ |
| 82 | char mtime[12]; /* 136-147 */ |
| 83 | char chksum[8]; /* 148-155 */ |
| 84 | char typeflag; /* 156-156 */ |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 85 | char linkname[NAME_SIZE]; /* 157-256 */ |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 86 | char magic[6]; /* 257-262 */ |
| 87 | char version[2]; /* 263-264 */ |
| 88 | char uname[32]; /* 265-296 */ |
| 89 | char gname[32]; /* 297-328 */ |
| 90 | char devmajor[8]; /* 329-336 */ |
| 91 | char devminor[8]; /* 337-344 */ |
| 92 | char prefix[155]; /* 345-499 */ |
| 93 | char padding[12]; /* 500-512 (pad to exactly the TAR_BLOCK_SIZE) */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 94 | }; |
| 95 | typedef struct TarHeader TarHeader; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 96 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 97 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 98 | /* A few useful constants */ |
| 99 | #define TAR_MAGIC "ustar" /* ustar and a null */ |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 100 | #define TAR_VERSION " " /* Be compatable with GNU tar format */ |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 101 | static const int TAR_MAGIC_LEN = 6; |
| 102 | static const int TAR_VERSION_LEN = 2; |
| 103 | static const int TAR_BLOCK_SIZE = 512; |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 104 | |
| 105 | /* A nice enum with all the possible tar file content types */ |
| 106 | enum TarFileType |
| 107 | { |
| 108 | REGTYPE = '0', /* regular file */ |
| 109 | REGTYPE0 = '\0', /* regular file (ancient bug compat)*/ |
| 110 | LNKTYPE = '1', /* hard link */ |
| 111 | SYMTYPE = '2', /* symbolic link */ |
| 112 | CHRTYPE = '3', /* character special */ |
| 113 | BLKTYPE = '4', /* block special */ |
| 114 | DIRTYPE = '5', /* directory */ |
| 115 | FIFOTYPE = '6', /* FIFO special */ |
| 116 | CONTTYPE = '7', /* reserved */ |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 117 | GNULONGLINK = 'K', /* GNU long (>100 chars) link name */ |
| 118 | GNULONGNAME = 'L', /* GNU long (>100 chars) file name */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 119 | }; |
| 120 | typedef enum TarFileType TarFileType; |
| 121 | |
| 122 | /* This struct ignores magic, non-numeric user name, |
| 123 | * non-numeric group name, and the checksum, since |
| 124 | * these are all ignored by BusyBox tar. */ |
| 125 | struct TarInfo |
| 126 | { |
| 127 | int tarFd; /* An open file descriptor for reading from the tarball */ |
| 128 | char * name; /* File name */ |
| 129 | mode_t mode; /* Unix mode, including device bits. */ |
| 130 | uid_t uid; /* Numeric UID */ |
| 131 | gid_t gid; /* Numeric GID */ |
| 132 | size_t size; /* Size of file */ |
| 133 | time_t mtime; /* Last-modified time */ |
| 134 | enum TarFileType type; /* Regular, directory, link, etc */ |
| 135 | char * linkname; /* Name for symbolic and hard links */ |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 136 | long devmajor; /* Major number for special device */ |
| 137 | long devminor; /* Minor number for special device */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 138 | }; |
| 139 | typedef struct TarInfo TarInfo; |
| 140 | |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 141 | /* Local procedures to restore files from a tar file. */ |
Glenn L McGrath | 7541e3a | 2001-01-02 23:41:50 +0000 | [diff] [blame] | 142 | extern int readTarFile(int tarFd, int extractFlag, int listFlag, |
Matt Kraai | b92223b | 2000-09-04 08:25:42 +0000 | [diff] [blame] | 143 | int tostdoutFlag, int verboseFlag, char** extractList, |
| 144 | char** excludeList); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 145 | |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 146 | #ifdef BB_FEATURE_TAR_CREATE |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 147 | /* Local procedures to save files into a tar file. */ |
Matt Kraai | d8ad76c | 2000-11-08 02:35:47 +0000 | [diff] [blame] | 148 | static int writeTarFile(const char* tarName, int verboseFlag, char **argv, |
| 149 | char** excludeList); |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 150 | #endif |
| 151 | |
Matt Kraai | 3b3f5c3 | 2001-01-22 20:49:00 +0000 | [diff] [blame] | 152 | #if defined BB_FEATURE_TAR_EXCLUDE |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 153 | static struct option longopts[] = { |
Matt Kraai | 3b3f5c3 | 2001-01-22 20:49:00 +0000 | [diff] [blame] | 154 | { "exclude", 1, NULL, 'e' }, |
| 155 | { NULL, 0, NULL, 0 } |
| 156 | }; |
| 157 | #endif |
| 158 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 159 | extern int tar_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 160 | { |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 161 | char** excludeList=NULL; |
Matt Kraai | c119ab9 | 2000-11-30 04:44:54 +0000 | [diff] [blame] | 162 | char** extractList=NULL; |
Glenn L McGrath | 46f44d2 | 2000-12-10 01:57:30 +0000 | [diff] [blame] | 163 | const char *tarName="-"; |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 164 | #if defined BB_FEATURE_TAR_EXCLUDE |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 165 | int excludeListSize=0; |
Glenn L McGrath | 46f44d2 | 2000-12-10 01:57:30 +0000 | [diff] [blame] | 166 | FILE *fileList; |
| 167 | char file[256]; |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 168 | #endif |
Glenn L McGrath | 46f44d2 | 2000-12-10 01:57:30 +0000 | [diff] [blame] | 169 | #if defined BB_FEATURE_TAR_GZIP |
Glenn L McGrath | 018e9e6 | 2001-03-28 07:27:26 +0000 | [diff] [blame] | 170 | FILE *comp_file = NULL; |
Glenn L McGrath | 46f44d2 | 2000-12-10 01:57:30 +0000 | [diff] [blame] | 171 | int unzipFlag = FALSE; |
| 172 | #endif |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 173 | int listFlag = FALSE; |
| 174 | int extractFlag = FALSE; |
| 175 | int createFlag = FALSE; |
| 176 | int verboseFlag = FALSE; |
| 177 | int tostdoutFlag = FALSE; |
Eric Andersen | 02f3b2e | 2000-12-01 19:04:52 +0000 | [diff] [blame] | 178 | int status = FALSE; |
Matt Kraai | 3b3f5c3 | 2001-01-22 20:49:00 +0000 | [diff] [blame] | 179 | int opt; |
Glenn L McGrath | 018e9e6 | 2001-03-28 07:27:26 +0000 | [diff] [blame] | 180 | pid_t pid; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 181 | |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 182 | if (argc <= 1) |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 183 | show_usage(); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 184 | |
Matt Kraai | 3b3f5c3 | 2001-01-22 20:49:00 +0000 | [diff] [blame] | 185 | if (argv[1][0] != '-') { |
| 186 | char *tmp = xmalloc(strlen(argv[1]) + 2); |
| 187 | tmp[0] = '-'; |
| 188 | strcpy(tmp + 1, argv[1]); |
| 189 | argv[1] = tmp; |
| 190 | } |
| 191 | |
| 192 | while ( |
| 193 | #ifndef BB_FEATURE_TAR_EXCLUDE |
| 194 | (opt = getopt(argc, argv, "cxtzvOf:")) |
| 195 | #else |
| 196 | (opt = getopt_long(argc, argv, "cxtzvOf:X:", longopts, NULL)) |
| 197 | #endif |
| 198 | > 0) { |
| 199 | switch (opt) { |
| 200 | case 'c': |
| 201 | if (extractFlag == TRUE || listFlag == TRUE) |
| 202 | goto flagError; |
| 203 | createFlag = TRUE; |
| 204 | break; |
| 205 | case 'x': |
| 206 | if (listFlag == TRUE || createFlag == TRUE) |
| 207 | goto flagError; |
| 208 | extractFlag = TRUE; |
| 209 | break; |
| 210 | case 't': |
| 211 | if (extractFlag == TRUE || createFlag == TRUE) |
| 212 | goto flagError; |
| 213 | listFlag = TRUE; |
| 214 | break; |
Glenn L McGrath | 46f44d2 | 2000-12-10 01:57:30 +0000 | [diff] [blame] | 215 | #ifdef BB_FEATURE_TAR_GZIP |
Matt Kraai | 3b3f5c3 | 2001-01-22 20:49:00 +0000 | [diff] [blame] | 216 | case 'z': |
| 217 | unzipFlag = TRUE; |
| 218 | break; |
Glenn L McGrath | 46f44d2 | 2000-12-10 01:57:30 +0000 | [diff] [blame] | 219 | #endif |
Matt Kraai | 3b3f5c3 | 2001-01-22 20:49:00 +0000 | [diff] [blame] | 220 | case 'v': |
| 221 | verboseFlag = TRUE; |
| 222 | break; |
| 223 | case 'O': |
| 224 | tostdoutFlag = TRUE; |
| 225 | break; |
| 226 | case 'f': |
| 227 | if (*tarName != '-') |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 228 | error_msg_and_die( "Only one 'f' option allowed"); |
Matt Kraai | 3b3f5c3 | 2001-01-22 20:49:00 +0000 | [diff] [blame] | 229 | tarName = optarg; |
| 230 | break; |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 231 | #if defined BB_FEATURE_TAR_EXCLUDE |
Matt Kraai | 3b3f5c3 | 2001-01-22 20:49:00 +0000 | [diff] [blame] | 232 | case 'e': |
| 233 | excludeList=xrealloc( excludeList, |
| 234 | sizeof(char *) * (excludeListSize+2)); |
| 235 | excludeList[excludeListSize] = optarg; |
| 236 | /* Tack a NULL onto the end of the list */ |
| 237 | excludeList[++excludeListSize] = NULL; |
| 238 | case 'X': |
| 239 | fileList = xfopen(optarg, "r"); |
| 240 | while (fgets(file, sizeof(file), fileList) != NULL) { |
| 241 | excludeList = xrealloc(excludeList, |
| 242 | sizeof(char *) * (excludeListSize+2)); |
Matt Kraai | 05e782d | 2001-02-01 16:49:30 +0000 | [diff] [blame] | 243 | chomp(file); |
Matt Kraai | 3b3f5c3 | 2001-01-22 20:49:00 +0000 | [diff] [blame] | 244 | excludeList[excludeListSize] = xstrdup(file); |
| 245 | /* Tack a NULL onto the end of the list */ |
| 246 | excludeList[++excludeListSize] = NULL; |
| 247 | } |
| 248 | fclose(fileList); |
| 249 | break; |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 250 | #endif |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 251 | default: |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 252 | show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 253 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Matt Kraai | 3b3f5c3 | 2001-01-22 20:49:00 +0000 | [diff] [blame] | 256 | /* |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 257 | * Do the correct type of action supplying the rest of the |
| 258 | * command line arguments as the list of files to process. |
| 259 | */ |
| 260 | if (createFlag == TRUE) { |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 261 | #ifndef BB_FEATURE_TAR_CREATE |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 262 | error_msg_and_die( "This version of tar was not compiled with tar creation support."); |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 263 | #else |
Glenn L McGrath | 46f44d2 | 2000-12-10 01:57:30 +0000 | [diff] [blame] | 264 | #ifdef BB_FEATURE_TAR_GZIP |
| 265 | if (unzipFlag==TRUE) |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 266 | error_msg_and_die("Creation of compressed not internally support by tar, pipe to busybox gunzip"); |
Glenn L McGrath | 46f44d2 | 2000-12-10 01:57:30 +0000 | [diff] [blame] | 267 | #endif |
Matt Kraai | 3b3f5c3 | 2001-01-22 20:49:00 +0000 | [diff] [blame] | 268 | status = writeTarFile(tarName, verboseFlag, argv + optind, excludeList); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 269 | #endif |
Erik Andersen | 8ea7d8c | 2000-05-20 00:40:08 +0000 | [diff] [blame] | 270 | } |
| 271 | if (listFlag == TRUE || extractFlag == TRUE) { |
Glenn L McGrath | 46f44d2 | 2000-12-10 01:57:30 +0000 | [diff] [blame] | 272 | int tarFd; |
Matt Kraai | 3b3f5c3 | 2001-01-22 20:49:00 +0000 | [diff] [blame] | 273 | if (argv[optind]) |
| 274 | extractList = argv + optind; |
Glenn L McGrath | 46f44d2 | 2000-12-10 01:57:30 +0000 | [diff] [blame] | 275 | /* Open the tar file for reading. */ |
| 276 | if (!strcmp(tarName, "-")) |
| 277 | tarFd = fileno(stdin); |
| 278 | else |
| 279 | tarFd = open(tarName, O_RDONLY); |
| 280 | if (tarFd < 0) |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 281 | perror_msg_and_die("Error opening '%s'", tarName); |
Glenn L McGrath | 46f44d2 | 2000-12-10 01:57:30 +0000 | [diff] [blame] | 282 | |
| 283 | #ifdef BB_FEATURE_TAR_GZIP |
| 284 | /* unzip tarFd in a seperate process */ |
Glenn L McGrath | 018e9e6 | 2001-03-28 07:27:26 +0000 | [diff] [blame] | 285 | if (unzipFlag == TRUE) { |
| 286 | comp_file = fdopen(tarFd, "r"); |
Glenn L McGrath | 018e9e6 | 2001-03-28 07:27:26 +0000 | [diff] [blame] | 287 | if ((tarFd = gz_open(comp_file, &pid)) == EXIT_FAILURE) { |
| 288 | error_msg_and_die("Couldnt unzip file"); |
| 289 | } |
Glenn L McGrath | 018e9e6 | 2001-03-28 07:27:26 +0000 | [diff] [blame] | 290 | } |
Glenn L McGrath | 46f44d2 | 2000-12-10 01:57:30 +0000 | [diff] [blame] | 291 | #endif |
| 292 | status = readTarFile(tarFd, extractFlag, listFlag, tostdoutFlag, |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 293 | verboseFlag, extractList, excludeList); |
Glenn L McGrath | 018e9e6 | 2001-03-28 07:27:26 +0000 | [diff] [blame] | 294 | close(tarFd); |
Glenn L McGrath | 018e9e6 | 2001-03-28 07:27:26 +0000 | [diff] [blame] | 295 | #ifdef BB_FEATURE_TAR_GZIP |
Glenn L McGrath | ae8ad35 | 2001-03-28 23:57:51 +0000 | [diff] [blame] | 296 | if (unzipFlag == TRUE) { |
| 297 | gz_close(pid); |
| 298 | fclose(comp_file); |
| 299 | } |
Glenn L McGrath | 018e9e6 | 2001-03-28 07:27:26 +0000 | [diff] [blame] | 300 | #endif |
Glenn L McGrath | ae8ad35 | 2001-03-28 23:57:51 +0000 | [diff] [blame] | 301 | } |
Glenn L McGrath | 018e9e6 | 2001-03-28 07:27:26 +0000 | [diff] [blame] | 302 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 303 | if (status == TRUE) |
| 304 | return EXIT_SUCCESS; |
| 305 | else |
| 306 | return EXIT_FAILURE; |
| 307 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 308 | flagError: |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 309 | error_msg_and_die( "Exactly one of 'c', 'x' or 't' must be specified"); |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static void |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 313 | fixUpPermissions(TarInfo *header) |
| 314 | { |
| 315 | struct utimbuf t; |
| 316 | /* Now set permissions etc for the new file */ |
| 317 | chown(header->name, header->uid, header->gid); |
| 318 | chmod(header->name, header->mode); |
| 319 | /* Reset the time */ |
| 320 | t.actime = time(0); |
| 321 | t.modtime = header->mtime; |
| 322 | utime(header->name, &t); |
| 323 | } |
| 324 | |
| 325 | static int |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 326 | tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag) |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 327 | { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 328 | size_t writeSize; |
| 329 | size_t readSize; |
| 330 | size_t actualWriteSz; |
| 331 | char buffer[BUFSIZ]; |
| 332 | size_t size = header->size; |
| 333 | int outFd=fileno(stdout); |
| 334 | |
| 335 | /* Open the file to be written, if a file is supposed to be written */ |
| 336 | if (extractFlag==TRUE && tostdoutFlag==FALSE) { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 337 | /* Create the path to the file, just in case it isn't there... |
| 338 | * This should not screw up path permissions or anything. */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 339 | create_path(header->name, 0777); |
Eric Andersen | 0c6a970 | 2000-06-09 20:51:50 +0000 | [diff] [blame] | 340 | if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY, |
| 341 | header->mode & ~S_IFMT)) < 0) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 342 | error_msg(io_error, header->name, strerror(errno)); |
Eric Andersen | 0c6a970 | 2000-06-09 20:51:50 +0000 | [diff] [blame] | 343 | return( FALSE); |
| 344 | } |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /* Write out the file, if we are supposed to be doing that */ |
| 348 | while ( size > 0 ) { |
| 349 | actualWriteSz=0; |
| 350 | if ( size > sizeof(buffer) ) |
| 351 | writeSize = readSize = sizeof(buffer); |
| 352 | else { |
| 353 | int mod = size % 512; |
| 354 | if ( mod != 0 ) |
| 355 | readSize = size + (512 - mod); |
| 356 | else |
| 357 | readSize = size; |
| 358 | writeSize = size; |
| 359 | } |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 360 | if ( (readSize = full_read(header->tarFd, buffer, readSize)) <= 0 ) { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 361 | /* Tarball seems to have a problem */ |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 362 | error_msg("Unexpected EOF in archive"); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 363 | return( FALSE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 364 | } |
| 365 | if ( readSize < writeSize ) |
| 366 | writeSize = readSize; |
| 367 | |
| 368 | /* Write out the file, if we are supposed to be doing that */ |
| 369 | if (extractFlag==TRUE) { |
| 370 | |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 371 | if ((actualWriteSz=full_write(outFd, buffer, writeSize)) != writeSize ) { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 372 | /* Output file seems to have a problem */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 373 | error_msg(io_error, header->name, strerror(errno)); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 374 | return( FALSE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 375 | } |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 376 | } else { |
| 377 | actualWriteSz=writeSize; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | size -= actualWriteSz; |
| 381 | } |
| 382 | |
| 383 | /* Now we are done writing the file out, so try |
| 384 | * and fix up the permissions and whatnot */ |
| 385 | if (extractFlag==TRUE && tostdoutFlag==FALSE) { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 386 | close(outFd); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 387 | fixUpPermissions(header); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 388 | } |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 389 | return( TRUE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 392 | static int |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 393 | tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag) |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 394 | { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 395 | |
| 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 | |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 399 | if (create_path(header->name, header->mode) != TRUE) { |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 400 | perror_msg("%s: Cannot mkdir", header->name); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 401 | return( FALSE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 402 | } |
| 403 | /* make the final component, just in case it was |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 404 | * omitted by create_path() (which will skip the |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 405 | * directory if it doesn't have a terminating '/') */ |
Matt Kraai | 541ffe3 | 2001-01-13 21:46:25 +0000 | [diff] [blame] | 406 | if (mkdir(header->name, header->mode) < 0 && errno != EEXIST) { |
| 407 | perror_msg("%s", header->name); |
| 408 | return FALSE; |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 409 | } |
Matt Kraai | 541ffe3 | 2001-01-13 21:46:25 +0000 | [diff] [blame] | 410 | |
| 411 | fixUpPermissions(header); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 412 | return( TRUE); |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 415 | static int |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 416 | tarExtractHardLink(TarInfo *header, int extractFlag, int tostdoutFlag) |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 417 | { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 418 | if (extractFlag==FALSE || tostdoutFlag==TRUE) |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 419 | return( TRUE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 420 | |
| 421 | if (link(header->linkname, header->name) < 0) { |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 422 | perror_msg("%s: Cannot create hard link to '%s'", header->name, |
| 423 | header->linkname); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 424 | return( FALSE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /* Now set permissions etc for the new directory */ |
| 428 | fixUpPermissions(header); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 429 | return( TRUE); |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 432 | static int |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 433 | tarExtractSymLink(TarInfo *header, int extractFlag, int tostdoutFlag) |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 434 | { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 435 | if (extractFlag==FALSE || tostdoutFlag==TRUE) |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 436 | return( TRUE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 437 | |
| 438 | #ifdef S_ISLNK |
| 439 | if (symlink(header->linkname, header->name) < 0) { |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 440 | perror_msg("%s: Cannot create symlink to '%s'", header->name, |
| 441 | header->linkname); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 442 | return( FALSE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 443 | } |
| 444 | /* Try to change ownership of the symlink. |
| 445 | * If libs doesn't support that, don't bother. |
| 446 | * Changing the pointed-to-file is the Wrong Thing(tm). |
| 447 | */ |
| 448 | #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) |
| 449 | lchown(header->name, header->uid, header->gid); |
| 450 | #endif |
| 451 | |
| 452 | /* Do not change permissions or date on symlink, |
| 453 | * since it changes the pointed to file instead. duh. */ |
| 454 | #else |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 455 | error_msg("%s: Cannot create symlink to '%s': %s", |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 456 | header->name, header->linkname, |
| 457 | "symlinks not supported"); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 458 | #endif |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 459 | return( TRUE); |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 462 | static int |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 463 | tarExtractSpecial(TarInfo *header, int extractFlag, int tostdoutFlag) |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 464 | { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 465 | if (extractFlag==FALSE || tostdoutFlag==TRUE) |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 466 | return( TRUE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 467 | |
| 468 | 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] | 469 | if (mknod(header->name, header->mode, makedev(header->devmajor, header->devminor)) < 0) { |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 470 | perror_msg("%s: Cannot mknod", header->name); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 471 | return( FALSE); |
| 472 | } |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 473 | } else if (S_ISFIFO(header->mode)) { |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 474 | if (mkfifo(header->name, header->mode) < 0) { |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 475 | perror_msg("%s: Cannot mkfifo", header->name); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 476 | return( FALSE); |
| 477 | } |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | /* Now set permissions etc for the new directory */ |
| 481 | fixUpPermissions(header); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 482 | return( TRUE); |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 485 | /* 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] | 486 | * 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] | 487 | * at the end. Returns -1 on an illegal format. */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 488 | static long getOctal(const char *cp, int size) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 489 | { |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 490 | long val = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 491 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 492 | for(;(size > 0) && (*cp == ' '); cp++, size--); |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 493 | if ((size == 0) || !is_octal(*cp)) |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 494 | return -1; |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 495 | for(; (size > 0) && is_octal(*cp); size--) { |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 496 | val = val * 8 + *cp++ - '0'; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 497 | } |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 498 | for (;(size > 0) && (*cp == ' '); cp++, size--); |
| 499 | if ((size > 0) && *cp) |
| 500 | return -1; |
| 501 | return val; |
| 502 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 503 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 504 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 505 | /* Parse the tar header and fill in the nice struct with the details */ |
| 506 | static int |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 507 | readTarHeader(struct TarHeader *rawHeader, struct TarInfo *header) |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 508 | { |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 509 | int i; |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 510 | long chksum, sum=0; |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 511 | unsigned char *s = (unsigned char *)rawHeader; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 512 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 513 | header->name = rawHeader->name; |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 514 | /* Check for and relativify any absolute paths */ |
| 515 | if ( *(header->name) == '/' ) { |
| 516 | static int alreadyWarned=FALSE; |
| 517 | |
| 518 | while (*(header->name) == '/') |
| 519 | ++*(header->name); |
| 520 | |
| 521 | if (alreadyWarned == FALSE) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 522 | error_msg("Removing leading '/' from member names"); |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 523 | alreadyWarned = TRUE; |
| 524 | } |
| 525 | } |
| 526 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 527 | header->mode = getOctal(rawHeader->mode, sizeof(rawHeader->mode)); |
| 528 | header->uid = getOctal(rawHeader->uid, sizeof(rawHeader->uid)); |
| 529 | header->gid = getOctal(rawHeader->gid, sizeof(rawHeader->gid)); |
| 530 | header->size = getOctal(rawHeader->size, sizeof(rawHeader->size)); |
| 531 | header->mtime = getOctal(rawHeader->mtime, sizeof(rawHeader->mtime)); |
| 532 | chksum = getOctal(rawHeader->chksum, sizeof(rawHeader->chksum)); |
| 533 | header->type = rawHeader->typeflag; |
| 534 | header->linkname = rawHeader->linkname; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 535 | header->devmajor = getOctal(rawHeader->devmajor, sizeof(rawHeader->devmajor)); |
| 536 | header->devminor = getOctal(rawHeader->devminor, sizeof(rawHeader->devminor)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 537 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 538 | /* Check the checksum */ |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 539 | for (i = sizeof(*rawHeader); i-- != 0;) { |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 540 | sum += *s++; |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 541 | } |
| 542 | /* Remove the effects of the checksum field (replace |
| 543 | * with blanks for the purposes of the checksum) */ |
| 544 | s = rawHeader->chksum; |
| 545 | for (i = sizeof(rawHeader->chksum) ; i-- != 0;) { |
| 546 | sum -= *s++; |
| 547 | } |
| 548 | sum += ' ' * sizeof(rawHeader->chksum); |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 549 | if (sum == chksum ) |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 550 | return ( TRUE); |
| 551 | return( FALSE); |
| 552 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 553 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 554 | static int exclude_file(char **excluded_files, const char *file) |
Matt Kraai | be7499c | 2001-01-03 17:22:10 +0000 | [diff] [blame] | 555 | { |
| 556 | int i; |
| 557 | |
| 558 | if (excluded_files == NULL) |
| 559 | return 0; |
| 560 | |
| 561 | for (i = 0; excluded_files[i] != NULL; i++) { |
| 562 | if (excluded_files[i][0] == '/') { |
| 563 | if (fnmatch(excluded_files[i], file, |
| 564 | FNM_PATHNAME | FNM_LEADING_DIR) == 0) |
| 565 | return 1; |
| 566 | } else { |
| 567 | const char *p; |
| 568 | |
| 569 | for (p = file; p[0] != '\0'; p++) { |
| 570 | if ((p == file || p[-1] == '/') && p[0] != '/' && |
| 571 | fnmatch(excluded_files[i], p, |
| 572 | FNM_PATHNAME | FNM_LEADING_DIR) == 0) |
| 573 | return 1; |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | return 0; |
| 579 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 580 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 581 | static int extract_file(char **extract_files, const char *file) |
Matt Kraai | 8f8dab9 | 2001-01-22 05:25:19 +0000 | [diff] [blame] | 582 | { |
| 583 | int i; |
| 584 | |
| 585 | if (extract_files == NULL) |
| 586 | return 1; |
| 587 | |
| 588 | for (i = 0; extract_files[i] != NULL; i++) { |
| 589 | if (fnmatch(extract_files[i], file, FNM_LEADING_DIR) == 0) |
| 590 | return 1; |
| 591 | } |
| 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 596 | /* |
| 597 | * Read a tar file and extract or list the specified files within it. |
| 598 | * If the list is empty than all files are extracted or listed. |
| 599 | */ |
Glenn L McGrath | 46f44d2 | 2000-12-10 01:57:30 +0000 | [diff] [blame] | 600 | extern int readTarFile(int tarFd, int extractFlag, int listFlag, |
Matt Kraai | b92223b | 2000-09-04 08:25:42 +0000 | [diff] [blame] | 601 | int tostdoutFlag, int verboseFlag, char** extractList, |
| 602 | char** excludeList) |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 603 | { |
Glenn L McGrath | 46f44d2 | 2000-12-10 01:57:30 +0000 | [diff] [blame] | 604 | int status; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 605 | int errorFlag=FALSE; |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 606 | int skipNextHeaderFlag=FALSE; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 607 | TarHeader rawHeader; |
| 608 | TarInfo header; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 609 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 610 | /* Set the umask for this process so it doesn't |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 611 | * screw up permission setting for us later. */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 612 | umask(0); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 613 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 614 | /* Read the tar file, and iterate over it one file at a time */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 615 | while ( (status = full_read(tarFd, (char*)&rawHeader, TAR_BLOCK_SIZE)) == TAR_BLOCK_SIZE ) { |
Erik Andersen | e272915 | 2000-02-18 21:34:17 +0000 | [diff] [blame] | 616 | |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 617 | /* Try to read the header */ |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 618 | if ( readTarHeader(&rawHeader, &header) == FALSE ) { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 619 | if ( *(header.name) == '\0' ) { |
| 620 | goto endgame; |
| 621 | } else { |
| 622 | errorFlag=TRUE; |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 623 | error_msg("Bad tar header, skipping"); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 624 | continue; |
| 625 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 626 | } |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 627 | if ( *(header.name) == '\0' ) |
| 628 | goto endgame; |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 629 | header.tarFd = tarFd; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 630 | |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 631 | /* Skip funky extra GNU headers that precede long files */ |
| 632 | if ( (header.type == GNULONGNAME) || (header.type == GNULONGLINK) ) { |
| 633 | skipNextHeaderFlag=TRUE; |
Matt Kraai | ab8f9e2 | 2000-11-18 01:28:57 +0000 | [diff] [blame] | 634 | if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE) |
| 635 | errorFlag = TRUE; |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 636 | continue; |
| 637 | } |
| 638 | if ( skipNextHeaderFlag == TRUE ) { |
| 639 | skipNextHeaderFlag=FALSE; |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 640 | error_msg(name_longer_than_foo, NAME_SIZE); |
Matt Kraai | ab8f9e2 | 2000-11-18 01:28:57 +0000 | [diff] [blame] | 641 | if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE) |
| 642 | errorFlag = TRUE; |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 643 | continue; |
| 644 | } |
| 645 | |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 646 | #if defined BB_FEATURE_TAR_EXCLUDE |
Matt Kraai | be7499c | 2001-01-03 17:22:10 +0000 | [diff] [blame] | 647 | if (exclude_file(excludeList, header.name)) { |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 648 | /* There are not the droids you're looking for, move along */ |
Matt Kraai | be7499c | 2001-01-03 17:22:10 +0000 | [diff] [blame] | 649 | /* If it is a regular file, pretend to extract it with |
| 650 | * the extractFlag set to FALSE, so the junk in the tarball |
| 651 | * is properly skipped over */ |
| 652 | if ( header.type==REGTYPE || header.type==REGTYPE0 ) { |
| 653 | if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE) |
| 654 | errorFlag = TRUE; |
| 655 | } |
| 656 | continue; |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 657 | } |
| 658 | #endif |
Matt Kraai | be7499c | 2001-01-03 17:22:10 +0000 | [diff] [blame] | 659 | |
Matt Kraai | 8f8dab9 | 2001-01-22 05:25:19 +0000 | [diff] [blame] | 660 | if (!extract_file(extractList, header.name)) { |
Matt Kraai | b92223b | 2000-09-04 08:25:42 +0000 | [diff] [blame] | 661 | /* There are not the droids you're looking for, move along */ |
Matt Kraai | 8f8dab9 | 2001-01-22 05:25:19 +0000 | [diff] [blame] | 662 | /* If it is a regular file, pretend to extract it with |
| 663 | * the extractFlag set to FALSE, so the junk in the tarball |
| 664 | * is properly skipped over */ |
| 665 | if ( header.type==REGTYPE || header.type==REGTYPE0 ) { |
| 666 | if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE) |
| 667 | errorFlag = TRUE; |
Matt Kraai | b92223b | 2000-09-04 08:25:42 +0000 | [diff] [blame] | 668 | } |
Matt Kraai | 8f8dab9 | 2001-01-22 05:25:19 +0000 | [diff] [blame] | 669 | continue; |
Matt Kraai | b92223b | 2000-09-04 08:25:42 +0000 | [diff] [blame] | 670 | } |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 671 | |
Matt Kraai | 82cfbad | 2000-09-15 21:18:43 +0000 | [diff] [blame] | 672 | if (listFlag == TRUE) { |
Matt Kraai | 6fc2a7d | 2000-09-15 22:23:41 +0000 | [diff] [blame] | 673 | /* Special treatment if the list (-t) flag is on */ |
| 674 | if (verboseFlag == TRUE) { |
| 675 | int len, len1; |
| 676 | char buf[35]; |
| 677 | struct tm *tm = localtime (&(header.mtime)); |
| 678 | |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 679 | len=printf("%s ", mode_string(header.mode)); |
Matt Kraai | 6fc2a7d | 2000-09-15 22:23:41 +0000 | [diff] [blame] | 680 | my_getpwuid(buf, header.uid); |
| 681 | if (! *buf) |
| 682 | len+=printf("%d", header.uid); |
| 683 | else |
| 684 | len+=printf("%s", buf); |
Matt Kraai | 6fc2a7d | 2000-09-15 22:23:41 +0000 | [diff] [blame] | 685 | my_getgrgid(buf, header.gid); |
| 686 | if (! *buf) |
| 687 | len+=printf("/%-d ", header.gid); |
| 688 | else |
| 689 | len+=printf("/%-s ", buf); |
| 690 | |
| 691 | if (header.type==CHRTYPE || header.type==BLKTYPE) { |
| 692 | len1=snprintf(buf, sizeof(buf), "%ld,%-ld ", |
| 693 | header.devmajor, header.devminor); |
| 694 | } else { |
| 695 | len1=snprintf(buf, sizeof(buf), "%lu ", (long)header.size); |
| 696 | } |
| 697 | /* Jump through some hoops to make the columns match up */ |
| 698 | for(;(len+len1)<31;len++) |
| 699 | printf(" "); |
| 700 | printf(buf); |
| 701 | |
| 702 | /* Use ISO 8610 time format */ |
| 703 | if (tm) { |
| 704 | printf ("%04d-%02d-%02d %02d:%02d:%02d ", |
| 705 | tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, |
| 706 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 707 | } |
| 708 | } |
Eric Andersen | fdd5103 | 2000-08-02 18:48:26 +0000 | [diff] [blame] | 709 | printf("%s", header.name); |
Matt Kraai | 82cfbad | 2000-09-15 21:18:43 +0000 | [diff] [blame] | 710 | if (verboseFlag == TRUE) { |
| 711 | if (header.type==LNKTYPE) /* If this is a link, say so */ |
| 712 | printf(" link to %s", header.linkname); |
| 713 | else if (header.type==SYMTYPE) |
| 714 | printf(" -> %s", header.linkname); |
| 715 | } |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 716 | printf("\n"); |
| 717 | } |
| 718 | |
Matt Kraai | 6fc2a7d | 2000-09-15 22:23:41 +0000 | [diff] [blame] | 719 | /* List contents if we are supposed to do that */ |
| 720 | if (verboseFlag == TRUE && extractFlag == TRUE) { |
| 721 | /* Now the normal listing */ |
| 722 | FILE *vbFd = stdout; |
| 723 | if (tostdoutFlag == TRUE) // If the archive goes to stdout, verbose to stderr |
| 724 | vbFd = stderr; |
| 725 | fprintf(vbFd, "%s\n", header.name); |
| 726 | } |
| 727 | |
Matt Kraai | f446297 | 2000-09-01 02:50:48 +0000 | [diff] [blame] | 728 | /* Remove files if we would overwrite them */ |
Matt Kraai | da542f3 | 2000-09-01 02:53:01 +0000 | [diff] [blame] | 729 | if (extractFlag == TRUE && tostdoutFlag == FALSE) |
Matt Kraai | f446297 | 2000-09-01 02:50:48 +0000 | [diff] [blame] | 730 | unlink(header.name); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 731 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 732 | /* If we got here, we can be certain we have a legitimate |
| 733 | * header to work with. So work with it. */ |
| 734 | switch ( header.type ) { |
| 735 | case REGTYPE: |
| 736 | case REGTYPE0: |
| 737 | /* If the name ends in a '/' then assume it is |
| 738 | * supposed to be a directory, and fall through */ |
| 739 | if (header.name[strlen(header.name)-1] != '/') { |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 740 | if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE) |
| 741 | errorFlag=TRUE; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 742 | break; |
| 743 | } |
| 744 | case DIRTYPE: |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 745 | if (tarExtractDirectory( &header, extractFlag, tostdoutFlag)==FALSE) |
| 746 | errorFlag=TRUE; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 747 | break; |
| 748 | case LNKTYPE: |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 749 | if (tarExtractHardLink( &header, extractFlag, tostdoutFlag)==FALSE) |
| 750 | errorFlag=TRUE; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 751 | break; |
| 752 | case SYMTYPE: |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 753 | if (tarExtractSymLink( &header, extractFlag, tostdoutFlag)==FALSE) |
| 754 | errorFlag=TRUE; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 755 | break; |
| 756 | case CHRTYPE: |
| 757 | case BLKTYPE: |
| 758 | case FIFOTYPE: |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 759 | if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE) |
| 760 | errorFlag=TRUE; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 761 | break; |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 762 | #if 0 |
| 763 | /* Handled earlier */ |
| 764 | case GNULONGNAME: |
| 765 | case GNULONGLINK: |
| 766 | skipNextHeaderFlag=TRUE; |
| 767 | break; |
| 768 | #endif |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 769 | default: |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 770 | error_msg("Unknown file type '%c' in tar file", header.type); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 771 | close( tarFd); |
| 772 | return( FALSE); |
| 773 | } |
| 774 | } |
| 775 | close(tarFd); |
| 776 | if (status > 0) { |
| 777 | /* Bummer - we read a partial header */ |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 778 | perror_msg("Error reading tar file"); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 779 | return ( FALSE); |
| 780 | } |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 781 | else if (errorFlag==TRUE) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 782 | error_msg( "Error exit delayed from previous errors"); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 783 | return( FALSE); |
| 784 | } else |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 785 | return( status); |
| 786 | |
| 787 | /* Stuff to do when we are done */ |
| 788 | endgame: |
| 789 | close( tarFd); |
| 790 | if ( *(header.name) == '\0' ) { |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 791 | if (errorFlag==TRUE) |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 792 | error_msg( "Error exit delayed from previous errors"); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 793 | else |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 794 | return( TRUE); |
| 795 | } |
| 796 | return( FALSE); |
| 797 | } |
| 798 | |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 799 | |
| 800 | #ifdef BB_FEATURE_TAR_CREATE |
| 801 | |
Eric Andersen | 3d957c8 | 2000-12-07 00:34:58 +0000 | [diff] [blame] | 802 | /* |
| 803 | ** writeTarFile(), writeFileToTarball(), and writeTarHeader() are |
| 804 | ** the only functions that deal with the HardLinkInfo structure. |
| 805 | ** Even these functions use the xxxHardLinkInfo() functions. |
| 806 | */ |
| 807 | typedef struct HardLinkInfo HardLinkInfo; |
| 808 | struct HardLinkInfo |
| 809 | { |
| 810 | HardLinkInfo *next; /* Next entry in list */ |
| 811 | dev_t dev; /* Device number */ |
| 812 | ino_t ino; /* Inode number */ |
| 813 | short linkCount; /* (Hard) Link Count */ |
| 814 | char name[1]; /* Start of filename (must be last) */ |
| 815 | }; |
| 816 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 817 | /* Some info to be carried along when creating a new tarball */ |
| 818 | struct TarBallInfo |
| 819 | { |
| 820 | char* fileName; /* File name of the tarball */ |
| 821 | int tarFd; /* Open-for-write file descriptor |
| 822 | for the tarball */ |
| 823 | struct stat statBuf; /* Stat info for the tarball, letting |
| 824 | us know the inode and device that the |
| 825 | tarball lives, so we can avoid trying |
| 826 | to include the tarball into itself */ |
| 827 | int verboseFlag; /* Whether to print extra stuff or not */ |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 828 | char** excludeList; /* List of files to not include */ |
Eric Andersen | 3d957c8 | 2000-12-07 00:34:58 +0000 | [diff] [blame] | 829 | HardLinkInfo *hlInfoHead; /* Hard Link Tracking Information */ |
| 830 | HardLinkInfo *hlInfo; /* Hard Link Info for the current file */ |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 831 | }; |
| 832 | typedef struct TarBallInfo TarBallInfo; |
| 833 | |
| 834 | |
Eric Andersen | 3d957c8 | 2000-12-07 00:34:58 +0000 | [diff] [blame] | 835 | /* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */ |
| 836 | static void |
| 837 | addHardLinkInfo (HardLinkInfo **hlInfoHeadPtr, dev_t dev, ino_t ino, |
| 838 | short linkCount, const char *name) |
| 839 | { |
| 840 | /* Note: hlInfoHeadPtr can never be NULL! */ |
| 841 | HardLinkInfo *hlInfo; |
| 842 | |
| 843 | hlInfo = (HardLinkInfo *)xmalloc(sizeof(HardLinkInfo)+strlen(name)+1); |
| 844 | if (hlInfo) { |
| 845 | hlInfo->next = *hlInfoHeadPtr; |
| 846 | *hlInfoHeadPtr = hlInfo; |
| 847 | hlInfo->dev = dev; |
| 848 | hlInfo->ino = ino; |
| 849 | hlInfo->linkCount = linkCount; |
| 850 | strcpy(hlInfo->name, name); |
| 851 | } |
| 852 | return; |
| 853 | } |
| 854 | |
| 855 | static void |
| 856 | freeHardLinkInfo (HardLinkInfo **hlInfoHeadPtr) |
| 857 | { |
| 858 | HardLinkInfo *hlInfo = NULL; |
| 859 | HardLinkInfo *hlInfoNext = NULL; |
| 860 | |
| 861 | if (hlInfoHeadPtr) { |
| 862 | hlInfo = *hlInfoHeadPtr; |
| 863 | while (hlInfo) { |
| 864 | hlInfoNext = hlInfo->next; |
| 865 | free(hlInfo); |
| 866 | hlInfo = hlInfoNext; |
| 867 | } |
| 868 | *hlInfoHeadPtr = NULL; |
| 869 | } |
| 870 | return; |
| 871 | } |
| 872 | |
| 873 | /* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */ |
| 874 | static HardLinkInfo * |
| 875 | findHardLinkInfo (HardLinkInfo *hlInfo, dev_t dev, ino_t ino) |
| 876 | { |
| 877 | while(hlInfo) { |
| 878 | if ((ino == hlInfo->ino) && (dev == hlInfo->dev)) |
| 879 | break; |
| 880 | hlInfo = hlInfo->next; |
| 881 | } |
| 882 | return(hlInfo); |
| 883 | } |
| 884 | |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 885 | /* Put an octal string into the specified buffer. |
| 886 | * The number is zero and space padded and possibly null padded. |
| 887 | * Returns TRUE if successful. */ |
| 888 | static int putOctal (char *cp, int len, long value) |
| 889 | { |
| 890 | int tempLength; |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 891 | char tempBuffer[32]; |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 892 | char *tempString = tempBuffer; |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 893 | |
| 894 | /* Create a string of the specified length with an initial space, |
| 895 | * leading zeroes and the octal number, and a trailing null. */ |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 896 | sprintf (tempString, "%0*lo", len - 1, value); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 897 | |
| 898 | /* If the string is too large, suppress the leading space. */ |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 899 | tempLength = strlen (tempString) + 1; |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 900 | if (tempLength > len) { |
| 901 | tempLength--; |
| 902 | tempString++; |
| 903 | } |
| 904 | |
| 905 | /* If the string is still too large, suppress the trailing null. */ |
| 906 | if (tempLength > len) |
| 907 | tempLength--; |
| 908 | |
| 909 | /* If the string is still too large, fail. */ |
| 910 | if (tempLength > len) |
| 911 | return FALSE; |
| 912 | |
| 913 | /* Copy the string to the field. */ |
| 914 | memcpy (cp, tempString, len); |
| 915 | |
| 916 | return TRUE; |
| 917 | } |
| 918 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 919 | /* Write out a tar header for the specified file/directory/whatever */ |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 920 | static int |
Matt Kraai | e80a263 | 2000-12-19 20:45:49 +0000 | [diff] [blame] | 921 | writeTarHeader(struct TarBallInfo *tbInfo, const char *header_name, |
| 922 | const char *real_name, struct stat *statbuf) |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 923 | { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 924 | long chksum=0; |
| 925 | struct TarHeader header; |
| 926 | const unsigned char *cp = (const unsigned char *) &header; |
| 927 | ssize_t size = sizeof(struct TarHeader); |
Eric Andersen | fdd5103 | 2000-08-02 18:48:26 +0000 | [diff] [blame] | 928 | |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 929 | memset( &header, 0, size); |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 930 | |
Matt Kraai | e80a263 | 2000-12-19 20:45:49 +0000 | [diff] [blame] | 931 | strncpy(header.name, header_name, sizeof(header.name)); |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 932 | |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 933 | putOctal(header.mode, sizeof(header.mode), statbuf->st_mode); |
| 934 | putOctal(header.uid, sizeof(header.uid), statbuf->st_uid); |
| 935 | putOctal(header.gid, sizeof(header.gid), statbuf->st_gid); |
| 936 | putOctal(header.size, sizeof(header.size), 0); /* Regular file size is handled later */ |
| 937 | putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime); |
| 938 | strncpy(header.magic, TAR_MAGIC TAR_VERSION, |
| 939 | TAR_MAGIC_LEN + TAR_VERSION_LEN ); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 940 | |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 941 | /* Enter the user and group names (default to root if it fails) */ |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 942 | my_getpwuid(header.uname, statbuf->st_uid); |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 943 | if (! *header.uname) |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 944 | strcpy(header.uname, "root"); |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 945 | my_getgrgid(header.gname, statbuf->st_gid); |
| 946 | if (! *header.uname) |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 947 | strcpy(header.uname, "root"); |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 948 | |
Eric Andersen | 3d957c8 | 2000-12-07 00:34:58 +0000 | [diff] [blame] | 949 | if (tbInfo->hlInfo) { |
| 950 | /* This is a hard link */ |
| 951 | header.typeflag = LNKTYPE; |
| 952 | strncpy(header.linkname, tbInfo->hlInfo->name, sizeof(header.linkname)); |
| 953 | } else if (S_ISLNK(statbuf->st_mode)) { |
Eric Andersen | 3adffb7 | 2000-06-26 10:54:06 +0000 | [diff] [blame] | 954 | int link_size=0; |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 955 | char buffer[BUFSIZ]; |
| 956 | header.typeflag = SYMTYPE; |
Matt Kraai | e80a263 | 2000-12-19 20:45:49 +0000 | [diff] [blame] | 957 | link_size = readlink(real_name, buffer, sizeof(buffer) - 1); |
Eric Andersen | 3adffb7 | 2000-06-26 10:54:06 +0000 | [diff] [blame] | 958 | if ( link_size < 0) { |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 959 | perror_msg("Error reading symlink '%s'", header.name); |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 960 | return ( FALSE); |
| 961 | } |
Eric Andersen | 3adffb7 | 2000-06-26 10:54:06 +0000 | [diff] [blame] | 962 | buffer[link_size] = '\0'; |
Erik Andersen | 95c1c1e | 2000-04-14 21:45:29 +0000 | [diff] [blame] | 963 | strncpy(header.linkname, buffer, sizeof(header.linkname)); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 964 | } else if (S_ISDIR(statbuf->st_mode)) { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 965 | header.typeflag = DIRTYPE; |
| 966 | strncat(header.name, "/", sizeof(header.name)); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 967 | } else if (S_ISCHR(statbuf->st_mode)) { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 968 | header.typeflag = CHRTYPE; |
| 969 | putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev)); |
| 970 | putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev)); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 971 | } else if (S_ISBLK(statbuf->st_mode)) { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 972 | header.typeflag = BLKTYPE; |
| 973 | putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev)); |
| 974 | putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev)); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 975 | } else if (S_ISFIFO(statbuf->st_mode)) { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 976 | header.typeflag = FIFOTYPE; |
| 977 | } else if (S_ISREG(statbuf->st_mode)) { |
| 978 | header.typeflag = REGTYPE; |
| 979 | putOctal(header.size, sizeof(header.size), statbuf->st_size); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 980 | } else { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 981 | error_msg("%s: Unknown file type", real_name); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 982 | return ( FALSE); |
| 983 | } |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 984 | |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 985 | /* Calculate and store the checksum (i.e. the sum of all of the bytes of |
| 986 | * the header). The checksum field must be filled with blanks for the |
| 987 | * calculation. The checksum field is formatted differently from the |
| 988 | * other fields: it has [6] digits, a null, then a space -- rather than |
| 989 | * digits, followed by a null like the other fields... */ |
| 990 | memset(header.chksum, ' ', sizeof(header.chksum)); |
| 991 | cp = (const unsigned char *) &header; |
| 992 | while (size-- > 0) |
| 993 | chksum += *cp++; |
| 994 | putOctal(header.chksum, 7, chksum); |
| 995 | |
| 996 | /* Now write the header out to disk */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 997 | if ((size=full_write(tbInfo->tarFd, (char*)&header, sizeof(struct TarHeader))) < 0) { |
Matt Kraai | e80a263 | 2000-12-19 20:45:49 +0000 | [diff] [blame] | 998 | error_msg(io_error, real_name, strerror(errno)); |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 999 | return ( FALSE); |
| 1000 | } |
| 1001 | /* Pad the header up to the tar block size */ |
| 1002 | for (; size<TAR_BLOCK_SIZE; size++) { |
| 1003 | write(tbInfo->tarFd, "\0", 1); |
| 1004 | } |
| 1005 | /* Now do the verbose thing (or not) */ |
Eric Andersen | fdd5103 | 2000-08-02 18:48:26 +0000 | [diff] [blame] | 1006 | if (tbInfo->verboseFlag==TRUE) { |
| 1007 | FILE *vbFd = stdout; |
| 1008 | if (tbInfo->tarFd == fileno(stdout)) // If the archive goes to stdout, verbose to stderr |
| 1009 | vbFd = stderr; |
| 1010 | fprintf(vbFd, "%s\n", header.name); |
| 1011 | } |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 1012 | |
| 1013 | return ( TRUE); |
| 1014 | } |
| 1015 | |
| 1016 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 1017 | static int writeFileToTarball(const char *fileName, struct stat *statbuf, void* userData) |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 1018 | { |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 1019 | struct TarBallInfo *tbInfo = (struct TarBallInfo *)userData; |
Matt Kraai | e80a263 | 2000-12-19 20:45:49 +0000 | [diff] [blame] | 1020 | const char *header_name; |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 1021 | |
Eric Andersen | 3d957c8 | 2000-12-07 00:34:58 +0000 | [diff] [blame] | 1022 | /* |
| 1023 | ** Check to see if we are dealing with a hard link. |
| 1024 | ** If so - |
| 1025 | ** Treat the first occurance of a given dev/inode as a file while |
| 1026 | ** treating any additional occurances as hard links. This is done |
| 1027 | ** by adding the file information to the HardLinkInfo linked list. |
| 1028 | */ |
| 1029 | tbInfo->hlInfo = NULL; |
| 1030 | if (statbuf->st_nlink > 1) { |
| 1031 | tbInfo->hlInfo = findHardLinkInfo(tbInfo->hlInfoHead, statbuf->st_dev, |
| 1032 | statbuf->st_ino); |
| 1033 | if (tbInfo->hlInfo == NULL) |
| 1034 | addHardLinkInfo (&tbInfo->hlInfoHead, statbuf->st_dev, |
| 1035 | statbuf->st_ino, statbuf->st_nlink, fileName); |
| 1036 | } |
| 1037 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 1038 | /* It is against the rules to archive a socket */ |
| 1039 | if (S_ISSOCK(statbuf->st_mode)) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 1040 | error_msg("%s: socket ignored", fileName); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 1041 | return( TRUE); |
| 1042 | } |
| 1043 | |
| 1044 | /* It is a bad idea to store the archive we are in the process of creating, |
| 1045 | * so check the device and inode to be sure that this particular file isn't |
| 1046 | * the new tarball */ |
| 1047 | if (tbInfo->statBuf.st_dev == statbuf->st_dev && |
| 1048 | tbInfo->statBuf.st_ino == statbuf->st_ino) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 1049 | error_msg("%s: file is the archive; skipping", fileName); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 1050 | return( TRUE); |
| 1051 | } |
| 1052 | |
Matt Kraai | e80a263 | 2000-12-19 20:45:49 +0000 | [diff] [blame] | 1053 | header_name = fileName; |
| 1054 | while (header_name[0] == '/') { |
Matt Kraai | a1f9775 | 2000-12-19 06:24:08 +0000 | [diff] [blame] | 1055 | static int alreadyWarned=FALSE; |
| 1056 | if (alreadyWarned==FALSE) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 1057 | error_msg("Removing leading '/' from member names"); |
Matt Kraai | a1f9775 | 2000-12-19 06:24:08 +0000 | [diff] [blame] | 1058 | alreadyWarned=TRUE; |
| 1059 | } |
Matt Kraai | e80a263 | 2000-12-19 20:45:49 +0000 | [diff] [blame] | 1060 | header_name++; |
Matt Kraai | a1f9775 | 2000-12-19 06:24:08 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 1063 | if (strlen(fileName) >= NAME_SIZE) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1064 | error_msg(name_longer_than_foo, NAME_SIZE); |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 1065 | return ( TRUE); |
| 1066 | } |
| 1067 | |
Matt Kraai | e80a263 | 2000-12-19 20:45:49 +0000 | [diff] [blame] | 1068 | if (header_name[0] == '\0') |
Matt Kraai | a1f9775 | 2000-12-19 06:24:08 +0000 | [diff] [blame] | 1069 | return TRUE; |
| 1070 | |
| 1071 | #if defined BB_FEATURE_TAR_EXCLUDE |
Matt Kraai | be7499c | 2001-01-03 17:22:10 +0000 | [diff] [blame] | 1072 | if (exclude_file(tbInfo->excludeList, header_name)) { |
| 1073 | return SKIP; |
Matt Kraai | a1f9775 | 2000-12-19 06:24:08 +0000 | [diff] [blame] | 1074 | } |
| 1075 | #endif |
| 1076 | |
Matt Kraai | e80a263 | 2000-12-19 20:45:49 +0000 | [diff] [blame] | 1077 | if (writeTarHeader(tbInfo, header_name, fileName, statbuf)==FALSE) { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 1078 | return( FALSE); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 1079 | } |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 1080 | |
| 1081 | /* Now, if the file is a regular file, copy it out to the tarball */ |
Eric Andersen | 3d957c8 | 2000-12-07 00:34:58 +0000 | [diff] [blame] | 1082 | if ((tbInfo->hlInfo == NULL) |
| 1083 | && (S_ISREG(statbuf->st_mode))) { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 1084 | int inputFileFd; |
| 1085 | char buffer[BUFSIZ]; |
| 1086 | ssize_t size=0, readSize=0; |
| 1087 | |
| 1088 | /* open the file we want to archive, and make sure all is well */ |
| 1089 | if ((inputFileFd = open(fileName, O_RDONLY)) < 0) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 1090 | error_msg("%s: Cannot open: %s", fileName, strerror(errno)); |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 1091 | return( FALSE); |
| 1092 | } |
| 1093 | |
| 1094 | /* write the file to the archive */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1095 | while ( (size = full_read(inputFileFd, buffer, sizeof(buffer))) > 0 ) { |
| 1096 | if (full_write(tbInfo->tarFd, buffer, size) != size ) { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 1097 | /* Output file seems to have a problem */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1098 | error_msg(io_error, fileName, strerror(errno)); |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 1099 | return( FALSE); |
| 1100 | } |
| 1101 | readSize+=size; |
| 1102 | } |
| 1103 | if (size == -1) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1104 | error_msg(io_error, fileName, strerror(errno)); |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 1105 | return( FALSE); |
| 1106 | } |
| 1107 | /* Pad the file up to the tar block size */ |
| 1108 | for (; (readSize%TAR_BLOCK_SIZE) != 0; readSize++) { |
| 1109 | write(tbInfo->tarFd, "\0", 1); |
| 1110 | } |
| 1111 | close( inputFileFd); |
| 1112 | } |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 1113 | |
| 1114 | return( TRUE); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Matt Kraai | d8ad76c | 2000-11-08 02:35:47 +0000 | [diff] [blame] | 1117 | static int writeTarFile(const char* tarName, int verboseFlag, char **argv, |
| 1118 | char** excludeList) |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 1119 | { |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 1120 | int tarFd=-1; |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 1121 | int errorFlag=FALSE; |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 1122 | ssize_t size; |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 1123 | struct TarBallInfo tbInfo; |
| 1124 | tbInfo.verboseFlag = verboseFlag; |
Eric Andersen | 3d957c8 | 2000-12-07 00:34:58 +0000 | [diff] [blame] | 1125 | tbInfo.hlInfoHead = NULL; |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 1126 | |
| 1127 | /* Make sure there is at least one file to tar up. */ |
Matt Kraai | d8ad76c | 2000-11-08 02:35:47 +0000 | [diff] [blame] | 1128 | if (*argv == NULL) |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 1129 | error_msg_and_die("Cowardly refusing to create an empty archive"); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 1130 | |
| 1131 | /* Open the tar file for writing. */ |
Matt Kraai | d8ad76c | 2000-11-08 02:35:47 +0000 | [diff] [blame] | 1132 | if (!strcmp(tarName, "-")) |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 1133 | tbInfo.tarFd = fileno(stdout); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 1134 | else |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 1135 | tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 1136 | if (tbInfo.tarFd < 0) { |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 1137 | perror_msg( "Error opening '%s'", tarName); |
Eric Andersen | 3d957c8 | 2000-12-07 00:34:58 +0000 | [diff] [blame] | 1138 | freeHardLinkInfo(&tbInfo.hlInfoHead); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 1139 | return ( FALSE); |
| 1140 | } |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 1141 | tbInfo.excludeList=excludeList; |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 1142 | /* Store the stat info for the tarball's file, so |
| 1143 | * can avoid including the tarball into itself.... */ |
| 1144 | if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1145 | error_msg_and_die(io_error, tarName, strerror(errno)); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 1146 | |
| 1147 | /* Set the umask for this process so it doesn't |
| 1148 | * screw up permission setting for us later. */ |
| 1149 | umask(0); |
| 1150 | |
| 1151 | /* Read the directory/files and iterate over them one at a time */ |
Matt Kraai | d8ad76c | 2000-11-08 02:35:47 +0000 | [diff] [blame] | 1152 | while (*argv != NULL) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 1153 | if (recursive_action(*argv++, TRUE, FALSE, FALSE, |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 1154 | writeFileToTarball, writeFileToTarball, |
| 1155 | (void*) &tbInfo) == FALSE) { |
| 1156 | errorFlag = TRUE; |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 1157 | } |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 1158 | } |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 1159 | /* Write two empty blocks to the end of the archive */ |
| 1160 | for (size=0; size<(2*TAR_BLOCK_SIZE); size++) { |
| 1161 | write(tbInfo.tarFd, "\0", 1); |
| 1162 | } |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 1163 | |
| 1164 | /* To be pedantically correct, we would check if the tarball |
Eric Andersen | 3c5ee9a | 2000-11-14 22:15:48 +0000 | [diff] [blame] | 1165 | * is smaller than 20 tar blocks, and pad it if it was smaller, |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 1166 | * but that isn't necessary for GNU tar interoperability, and |
| 1167 | * so is considered a waste of space */ |
| 1168 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 1169 | /* Hang up the tools, close up shop, head home */ |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 1170 | close(tarFd); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 1171 | if (errorFlag == TRUE) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 1172 | error_msg("Error exit delayed from previous errors"); |
Eric Andersen | 3d957c8 | 2000-12-07 00:34:58 +0000 | [diff] [blame] | 1173 | freeHardLinkInfo(&tbInfo.hlInfoHead); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 1174 | return(FALSE); |
| 1175 | } |
Eric Andersen | 3d957c8 | 2000-12-07 00:34:58 +0000 | [diff] [blame] | 1176 | freeHardLinkInfo(&tbInfo.hlInfoHead); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 1177 | return( TRUE); |
| 1178 | } |
| 1179 | |
| 1180 | |
| 1181 | #endif |
| 1182 | |