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