Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1 | /* |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 2 | * Mini tar implementation for busybox based on code taken from sash. |
| 3 | * |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 4 | * Copyright (c) 1999 by David I. Bell |
| 5 | * Permission is granted to use, distribute, or modify this source, |
| 6 | * provided that this copyright notice remains intact. |
| 7 | * |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 8 | * Permission to distribute this code under the GPL has been granted. |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 9 | * |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 10 | * Modified for busybox by Erik Andersen <andersee@debian.org> |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 11 | * Adjusted to grok stdin/stdout options. |
| 12 | * |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 13 | * Modified to handle device special files by Matt Porter |
| 14 | * <porter@debian.org> |
| 15 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 16 | * This program is free software; you can redistribute it and/or modify |
| 17 | * it under the terms of the GNU General Public License as published by |
| 18 | * the Free Software Foundation; either version 2 of the License, or |
| 19 | * (at your option) any later version. |
| 20 | * |
| 21 | * This program is distributed in the hope that it will be useful, |
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 24 | * General Public License for more details. |
| 25 | * |
| 26 | * You should have received a copy of the GNU General Public License |
| 27 | * along with this program; if not, write to the Free Software |
| 28 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 29 | * |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
| 32 | |
| 33 | #include "internal.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 34 | #include <stdio.h> |
| 35 | #include <dirent.h> |
| 36 | #include <errno.h> |
| 37 | #include <fcntl.h> |
| 38 | #include <signal.h> |
| 39 | #include <time.h> |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 40 | #include <utime.h> |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 41 | #include <sys/types.h> |
Eric Andersen | 08b1034 | 1999-11-19 02:38:58 +0000 | [diff] [blame] | 42 | #include <sys/sysmacros.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 43 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 44 | |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 45 | #ifdef BB_FEATURE_TAR_CREATE |
| 46 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 47 | static const char tar_usage[] = |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 48 | "tar -[cxtvOf] [tarFileName] [FILE] ...\n\n" |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 49 | "Create, extract, or list files from a tar file.\n\n" |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 50 | "Options:\n" |
| 51 | "\tc=create, x=extract, t=list contents, v=verbose,\n" |
| 52 | "\tO=extract to stdout, f=tarfile or \"-\" for stdin\n"; |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 53 | |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 54 | #else |
| 55 | |
| 56 | static const char tar_usage[] = |
| 57 | "tar -[cxtvOf] [tarFileName] [FILE] ...\n\n" |
| 58 | "Extract, or list files stored in a tar file. This\n" |
| 59 | "version of tar does not support creation of tar files.\n\n" |
| 60 | "Options:\n" |
| 61 | "\tx=extract, t=list contents, v=verbose,\n" |
| 62 | "\tO=extract to stdout, f=tarfile or \"-\" for stdin\n"; |
| 63 | |
| 64 | #endif |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 65 | |
| 66 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 67 | /* |
| 68 | * Tar file constants. |
| 69 | */ |
| 70 | #define TAR_BLOCK_SIZE 512 |
| 71 | #define TAR_NAME_SIZE 100 |
| 72 | |
| 73 | |
| 74 | /* |
| 75 | * The POSIX (and basic GNU) tar header format. |
| 76 | * This structure is always embedded in a TAR_BLOCK_SIZE sized block |
| 77 | * with zero padding. We only process this information minimally. |
| 78 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 79 | typedef struct { |
| 80 | char name[TAR_NAME_SIZE]; |
| 81 | char mode[8]; |
| 82 | char uid[8]; |
| 83 | char gid[8]; |
| 84 | char size[12]; |
| 85 | char mtime[12]; |
| 86 | char checkSum[8]; |
| 87 | char typeFlag; |
| 88 | char linkName[TAR_NAME_SIZE]; |
| 89 | char magic[6]; |
| 90 | char version[2]; |
| 91 | char uname[32]; |
| 92 | char gname[32]; |
| 93 | char devMajor[8]; |
| 94 | char devMinor[8]; |
| 95 | char prefix[155]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 96 | } TarHeader; |
| 97 | |
| 98 | #define TAR_MAGIC "ustar" |
| 99 | #define TAR_VERSION "00" |
| 100 | |
| 101 | #define TAR_TYPE_REGULAR '0' |
| 102 | #define TAR_TYPE_HARD_LINK '1' |
| 103 | #define TAR_TYPE_SOFT_LINK '2' |
| 104 | |
| 105 | |
| 106 | /* |
| 107 | * Static data. |
| 108 | */ |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 109 | static int listFlag; |
| 110 | static int extractFlag; |
| 111 | static int createFlag; |
| 112 | static int verboseFlag; |
| 113 | static int tostdoutFlag; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 114 | |
Eric Andersen | 5556c18 | 1999-11-10 19:27:58 +0000 | [diff] [blame] | 115 | static int inHeader; // <- check me |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 116 | static int badHeader; |
| 117 | static int errorFlag; |
| 118 | static int skipFileFlag; |
| 119 | static int warnedRoot; |
| 120 | static int eofFlag; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 121 | static long dataCc; |
| 122 | static int outFd; |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 123 | static const char *outName; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 124 | |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 125 | static int mode; |
| 126 | static int uid; |
| 127 | static int gid; |
| 128 | static time_t mtime; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 129 | |
| 130 | /* |
| 131 | * Static data associated with the tar file. |
| 132 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 133 | static const char *tarName; |
| 134 | static int tarFd; |
| 135 | static dev_t tarDev; |
| 136 | static ino_t tarInode; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 137 | |
| 138 | |
| 139 | /* |
| 140 | * Local procedures to restore files from a tar file. |
| 141 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 142 | static void readTarFile (int fileCount, char **fileTable); |
| 143 | static void readData (const char *cp, int count); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 144 | static long getOctal (const char *cp, int len); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 145 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 146 | static void readHeader (const TarHeader * hp, |
| 147 | int fileCount, char **fileTable); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 148 | |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 149 | static int wantFileName (const char *fileName, |
| 150 | int fileCount, char **fileTable); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 151 | |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 152 | #ifdef BB_FEATURE_TAR_CREATE |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 153 | /* |
| 154 | * Local procedures to save files into a tar file. |
| 155 | */ |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 156 | static void saveFile (const char *fileName, int seeLinks); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 157 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 158 | static void saveRegularFile (const char *fileName, |
| 159 | const struct stat *statbuf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 160 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 161 | static void saveDirectory (const char *fileName, |
| 162 | const struct stat *statbuf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 163 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 164 | static void writeHeader (const char *fileName, const struct stat *statbuf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 165 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 166 | static void writeTarFile (int fileCount, char **fileTable); |
| 167 | static void writeTarBlock (const char *buf, int len); |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 168 | static int putOctal (char *cp, int len, long value); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 169 | |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 170 | #endif |
| 171 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 172 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 173 | extern int tar_main (int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 174 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 175 | const char *options; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 176 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 177 | argc--; |
| 178 | argv++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 179 | |
Eric Andersen | be971d6 | 1999-11-03 16:52:50 +0000 | [diff] [blame] | 180 | if (argc < 1) |
| 181 | usage( tar_usage); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 182 | |
| 183 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 184 | errorFlag = FALSE; |
| 185 | extractFlag = FALSE; |
| 186 | createFlag = FALSE; |
| 187 | listFlag = FALSE; |
| 188 | verboseFlag = FALSE; |
| 189 | tostdoutFlag = FALSE; |
| 190 | tarName = NULL; |
| 191 | tarDev = 0; |
| 192 | tarInode = 0; |
| 193 | tarFd = -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 194 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 195 | /* |
| 196 | * Parse the options. |
| 197 | */ |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame^] | 198 | if (**argv == '-') |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 199 | options = (*argv++) + 1; |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame^] | 200 | else |
| 201 | options = (*argv++); |
| 202 | argc--; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 203 | |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame^] | 204 | for (; *options; options++) { |
| 205 | switch (*options) { |
| 206 | case 'f': |
| 207 | if (tarName != NULL) { |
| 208 | fprintf (stderr, "Only one 'f' option allowed\n"); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 209 | |
| 210 | exit (FALSE); |
| 211 | } |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame^] | 212 | |
| 213 | tarName = *argv++; |
| 214 | argc--; |
| 215 | |
| 216 | break; |
| 217 | |
| 218 | case 't': |
| 219 | if (extractFlag == TRUE || createFlag == TRUE ) |
| 220 | goto flagError; |
| 221 | listFlag = TRUE; |
| 222 | break; |
| 223 | |
| 224 | case 'x': |
| 225 | if (listFlag == TRUE || createFlag == TRUE ) |
| 226 | goto flagError; |
| 227 | extractFlag = TRUE; |
| 228 | break; |
| 229 | case 'c': |
| 230 | if (extractFlag == TRUE || listFlag == TRUE) |
| 231 | goto flagError; |
| 232 | createFlag = TRUE; |
| 233 | break; |
| 234 | |
| 235 | case 'v': |
| 236 | verboseFlag = TRUE; |
| 237 | break; |
| 238 | |
| 239 | case 'O': |
| 240 | tostdoutFlag = TRUE; |
| 241 | break; |
| 242 | |
| 243 | case '-': |
| 244 | usage( tar_usage); |
| 245 | break; |
| 246 | |
| 247 | default: |
| 248 | fprintf (stderr, "Unknown tar flag '%c'\n" |
| 249 | "Try `tar --help' for more information\n", |
| 250 | *options); |
| 251 | exit (FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 252 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 253 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 254 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 255 | /* |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 256 | * Do the correct type of action supplying the rest of the |
| 257 | * command line arguments as the list of files to process. |
| 258 | */ |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame^] | 259 | if (createFlag==TRUE) { |
| 260 | #ifndef BB_FEATURE_TAR_CREATE |
| 261 | fprintf (stderr, "This version of tar was not compiled with tar creation support.\n" ); |
| 262 | exit (FALSE); |
| 263 | #else |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 264 | writeTarFile (argc, argv); |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 265 | #endif |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame^] | 266 | } else { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 267 | readTarFile (argc, argv); |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame^] | 268 | } |
| 269 | if (errorFlag==TRUE) { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 270 | fprintf (stderr, "\n"); |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame^] | 271 | } |
Eric Andersen | 5556c18 | 1999-11-10 19:27:58 +0000 | [diff] [blame] | 272 | exit (!errorFlag); |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame^] | 273 | |
| 274 | flagError: |
| 275 | fprintf (stderr, "Exactly one of 'c', 'x' or 't' must be specified\n"); |
| 276 | exit (FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | |
| 280 | /* |
| 281 | * Read a tar file and extract or list the specified files within it. |
| 282 | * If the list is empty than all files are extracted or listed. |
| 283 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 284 | static void readTarFile (int fileCount, char **fileTable) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 285 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 286 | const char *cp; |
| 287 | int cc; |
| 288 | int inCc; |
| 289 | int blockSize; |
| 290 | char buf[BUF_SIZE]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 291 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 292 | skipFileFlag = FALSE; |
| 293 | badHeader = FALSE; |
| 294 | warnedRoot = FALSE; |
| 295 | eofFlag = FALSE; |
| 296 | inHeader = TRUE; |
| 297 | inCc = 0; |
| 298 | dataCc = 0; |
| 299 | outFd = -1; |
| 300 | blockSize = sizeof (buf); |
| 301 | cp = buf; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 302 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 303 | /* |
| 304 | * Open the tar file for reading. |
| 305 | */ |
| 306 | if ((tarName == NULL) || !strcmp (tarName, "-")) { |
Eric Andersen | 08b1034 | 1999-11-19 02:38:58 +0000 | [diff] [blame] | 307 | tarFd = fileno(stdin); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 308 | } else |
| 309 | tarFd = open (tarName, O_RDONLY); |
| 310 | |
| 311 | if (tarFd < 0) { |
| 312 | perror (tarName); |
| 313 | errorFlag = TRUE; |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * Read blocks from the file until an end of file header block |
| 319 | * has been seen. (A real end of file from a read is an error.) |
| 320 | */ |
Eric Andersen | 5556c18 | 1999-11-10 19:27:58 +0000 | [diff] [blame] | 321 | while (eofFlag==FALSE) { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 322 | /* |
| 323 | * Read the next block of data if necessary. |
| 324 | * This will be a large block if possible, which we will |
| 325 | * then process in the small tar blocks. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 326 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 327 | if (inCc <= 0) { |
| 328 | cp = buf; |
| 329 | inCc = fullRead (tarFd, buf, blockSize); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 330 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 331 | if (inCc < 0) { |
| 332 | perror (tarName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 333 | errorFlag = TRUE; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 334 | goto done; |
| 335 | } |
| 336 | |
| 337 | if (inCc == 0) { |
| 338 | fprintf (stderr, |
| 339 | "Unexpected end of file from \"%s\"", tarName); |
| 340 | errorFlag = TRUE; |
| 341 | goto done; |
| 342 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 345 | /* |
| 346 | * If we are expecting a header block then examine it. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 347 | */ |
Eric Andersen | 5556c18 | 1999-11-10 19:27:58 +0000 | [diff] [blame] | 348 | if (inHeader==TRUE) { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 349 | readHeader ((const TarHeader *) cp, fileCount, fileTable); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 350 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 351 | cp += TAR_BLOCK_SIZE; |
| 352 | inCc -= TAR_BLOCK_SIZE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 353 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 354 | continue; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 357 | /* |
| 358 | * We are currently handling the data for a file. |
| 359 | * Process the minimum of the amount of data we have available |
| 360 | * and the amount left to be processed for the file. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 361 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 362 | cc = inCc; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 363 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 364 | if (cc > dataCc) |
| 365 | cc = dataCc; |
| 366 | |
| 367 | readData (cp, cc); |
| 368 | |
| 369 | /* |
| 370 | * If the amount left isn't an exact multiple of the tar block |
| 371 | * size then round it up to the next block boundary since there |
| 372 | * is padding at the end of the file. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 373 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 374 | if (cc % TAR_BLOCK_SIZE) |
| 375 | cc += TAR_BLOCK_SIZE - (cc % TAR_BLOCK_SIZE); |
| 376 | |
| 377 | cp += cc; |
| 378 | inCc -= cc; |
| 379 | } |
| 380 | |
| 381 | done: |
| 382 | /* |
| 383 | * Close the tar file if needed. |
| 384 | */ |
| 385 | if ((tarFd >= 0) && (close (tarFd) < 0)) |
| 386 | perror (tarName); |
| 387 | |
| 388 | /* |
| 389 | * Close the output file if needed. |
| 390 | * This is only done here on a previous error and so no |
| 391 | * message is required on errors. |
| 392 | */ |
| 393 | if (tostdoutFlag == FALSE) { |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 394 | if (outFd >= 0) { |
| 395 | close (outFd); |
| 396 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 397 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | |
| 401 | /* |
| 402 | * Examine the header block that was just read. |
| 403 | * This can specify the information for another file, or it can mark |
| 404 | * the end of the tar file. |
| 405 | */ |
| 406 | static void |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 407 | readHeader (const TarHeader * hp, int fileCount, char **fileTable) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 408 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 409 | int checkSum; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 410 | int cc; |
| 411 | int hardLink; |
| 412 | int softLink; |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 413 | int devFileFlag; |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 414 | unsigned int major; |
| 415 | unsigned int minor; |
| 416 | long size; |
| 417 | struct utimbuf utb; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 418 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 419 | /* |
| 420 | * If the block is completely empty, then this is the end of the |
| 421 | * archive file. If the name is null, then just skip this header. |
| 422 | */ |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 423 | outName = hp->name; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 424 | |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 425 | if (*outName == '\0') { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 426 | for (cc = TAR_BLOCK_SIZE; cc > 0; cc--) { |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 427 | if (*outName++) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 428 | return; |
| 429 | } |
| 430 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 431 | eofFlag = TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 432 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 433 | return; |
| 434 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 435 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 436 | /* |
| 437 | * There is another file in the archive to examine. |
| 438 | * Extract the encoded information and check it. |
| 439 | */ |
| 440 | mode = getOctal (hp->mode, sizeof (hp->mode)); |
| 441 | uid = getOctal (hp->uid, sizeof (hp->uid)); |
| 442 | gid = getOctal (hp->gid, sizeof (hp->gid)); |
| 443 | size = getOctal (hp->size, sizeof (hp->size)); |
| 444 | mtime = getOctal (hp->mtime, sizeof (hp->mtime)); |
| 445 | checkSum = getOctal (hp->checkSum, sizeof (hp->checkSum)); |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 446 | major = getOctal (hp->devMajor, sizeof (hp->devMajor)); |
| 447 | minor = getOctal (hp->devMinor, sizeof (hp->devMinor)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 448 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 449 | if ((mode < 0) || (uid < 0) || (gid < 0) || (size < 0)) { |
Eric Andersen | 5556c18 | 1999-11-10 19:27:58 +0000 | [diff] [blame] | 450 | if (badHeader==FALSE) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 451 | fprintf (stderr, "Bad tar header, skipping\n"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 452 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 453 | badHeader = TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 454 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 455 | return; |
| 456 | } |
| 457 | |
| 458 | badHeader = FALSE; |
| 459 | skipFileFlag = FALSE; |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 460 | devFileFlag = FALSE; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 461 | |
| 462 | /* |
| 463 | * Check for the file modes. |
| 464 | */ |
| 465 | hardLink = ((hp->typeFlag == TAR_TYPE_HARD_LINK) || |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 466 | (hp->typeFlag == TAR_TYPE_HARD_LINK - '0')); |
| 467 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 468 | softLink = ((hp->typeFlag == TAR_TYPE_SOFT_LINK) || |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 469 | (hp->typeFlag == TAR_TYPE_SOFT_LINK - '0')); |
| 470 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 471 | /* |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 472 | * Check for a directory. |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 473 | */ |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 474 | if (outName[strlen (outName) - 1] == '/') |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 475 | mode |= S_IFDIR; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 476 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 477 | /* |
| 478 | * Check for absolute paths in the file. |
| 479 | * If we find any, then warn the user and make them relative. |
| 480 | */ |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 481 | if (*outName == '/') { |
| 482 | while (*outName == '/') |
| 483 | outName++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 484 | |
Eric Andersen | 5556c18 | 1999-11-10 19:27:58 +0000 | [diff] [blame] | 485 | if (warnedRoot==FALSE) { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 486 | fprintf (stderr, |
| 487 | "Absolute path detected, removing leading slashes\n"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 490 | warnedRoot = TRUE; |
| 491 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 492 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 493 | /* |
| 494 | * See if we want this file to be restored. |
| 495 | * If not, then set up to skip it. |
| 496 | */ |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 497 | if (wantFileName (outName, fileCount, fileTable) == FALSE) { |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 498 | if ( !hardLink && !softLink && (S_ISREG (mode) || S_ISCHR (mode) |
| 499 | || S_ISBLK (mode) || S_ISSOCK(mode) || S_ISFIFO(mode) ) ) { |
Eric Andersen | 5556c18 | 1999-11-10 19:27:58 +0000 | [diff] [blame] | 500 | inHeader = (size == 0)? TRUE : FALSE; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 501 | dataCc = size; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 504 | skipFileFlag = TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 505 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 506 | return; |
| 507 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 508 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 509 | /* |
| 510 | * This file is to be handled. |
| 511 | * If we aren't extracting then just list information about the file. |
| 512 | */ |
Eric Andersen | 5556c18 | 1999-11-10 19:27:58 +0000 | [diff] [blame] | 513 | if (extractFlag==FALSE) { |
| 514 | if (verboseFlag==TRUE) { |
Eric Andersen | 03018f7 | 1999-11-29 04:29:13 +0000 | [diff] [blame] | 515 | printf ("%s %3d/%-d ", modeString (mode), uid, gid); |
| 516 | if( S_ISCHR (mode) || S_ISBLK (mode) ) |
| 517 | printf ("%4d,%4d %s ", major,minor, timeString (mtime)); |
| 518 | else |
| 519 | printf ("%9ld %s ", size, timeString (mtime)); |
| 520 | } |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 521 | printf ("%s", outName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 522 | |
| 523 | if (hardLink) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 524 | printf (" (link to \"%s\")", hp->linkName); |
| 525 | else if (softLink) |
| 526 | printf (" (symlink to \"%s\")", hp->linkName); |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 527 | else if (S_ISREG (mode) || S_ISCHR (mode) || S_ISBLK (mode) || |
| 528 | S_ISSOCK(mode) || S_ISFIFO(mode) ) { |
Eric Andersen | 5556c18 | 1999-11-10 19:27:58 +0000 | [diff] [blame] | 529 | inHeader = (size == 0)? TRUE : FALSE; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 530 | dataCc = size; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 533 | printf ("\n"); |
| 534 | |
| 535 | return; |
| 536 | } |
| 537 | |
| 538 | /* |
| 539 | * We really want to extract the file. |
| 540 | */ |
Eric Andersen | 5556c18 | 1999-11-10 19:27:58 +0000 | [diff] [blame] | 541 | if (verboseFlag==TRUE) |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 542 | printf ("x %s\n", outName); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 543 | |
| 544 | if (hardLink) { |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 545 | if (link (hp->linkName, outName) < 0) |
| 546 | perror (outName); |
| 547 | /* Set the file time */ |
| 548 | utb.actime = mtime; |
| 549 | utb.modtime = mtime; |
| 550 | utime (outName, &utb); |
| 551 | /* Set the file permissions */ |
| 552 | chown(outName, uid, gid); |
| 553 | chmod(outName, mode); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 554 | return; |
| 555 | } |
| 556 | |
| 557 | if (softLink) { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 558 | #ifdef S_ISLNK |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 559 | if (symlink (hp->linkName, outName) < 0) |
| 560 | perror (outName); |
| 561 | /* Try to change ownership of the symlink. |
| 562 | * If libs doesn't support that, don't bother. |
| 563 | * Changing the pointed-to file is the Wrong Thing(tm). |
| 564 | */ |
| 565 | #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) |
| 566 | lchown(outName, uid, gid); |
| 567 | #endif |
| 568 | |
| 569 | /* Do not change permissions or date on symlink, |
| 570 | * since it changes the pointed to file instead. duh. */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 571 | #else |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 572 | fprintf (stderr, "Cannot create symbolic links\n"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 573 | #endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 574 | return; |
| 575 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 576 | |
Eric Andersen | 03018f7 | 1999-11-29 04:29:13 +0000 | [diff] [blame] | 577 | /* Set the umask for this process so it doesn't |
| 578 | * screw things up. */ |
| 579 | umask(0); |
| 580 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 581 | /* |
| 582 | * If the file is a directory, then just create the path. |
| 583 | */ |
| 584 | if (S_ISDIR (mode)) { |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 585 | createPath (outName, mode); |
| 586 | /* Set the file time */ |
| 587 | utb.actime = mtime; |
| 588 | utb.modtime = mtime; |
| 589 | utime (outName, &utb); |
| 590 | /* Set the file permissions */ |
| 591 | chown(outName, uid, gid); |
| 592 | chmod(outName, mode); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 593 | return; |
| 594 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 595 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 596 | /* |
| 597 | * There is a file to write. |
Eric Andersen | 03018f7 | 1999-11-29 04:29:13 +0000 | [diff] [blame] | 598 | * First create the path to it if necessary with default permissions. |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 599 | */ |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 600 | createPath (outName, 0777); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 601 | |
Eric Andersen | 5556c18 | 1999-11-10 19:27:58 +0000 | [diff] [blame] | 602 | inHeader = (size == 0)? TRUE : FALSE; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 603 | dataCc = size; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 604 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 605 | /* |
| 606 | * Start the output file. |
| 607 | */ |
| 608 | if (tostdoutFlag == TRUE) |
Eric Andersen | 08b1034 | 1999-11-19 02:38:58 +0000 | [diff] [blame] | 609 | outFd = fileno(stdout); |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 610 | else { |
| 611 | if ( S_ISCHR(mode) || S_ISBLK(mode) || S_ISSOCK(mode) ) { |
| 612 | devFileFlag = TRUE; |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 613 | outFd = mknod (outName, mode, makedev(major, minor) ); |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 614 | } |
| 615 | else if (S_ISFIFO(mode) ) { |
Eric Andersen | b6a44b8 | 1999-11-13 04:47:09 +0000 | [diff] [blame] | 616 | devFileFlag = TRUE; |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 617 | outFd = mkfifo(outName, mode); |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 618 | } else { |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 619 | outFd = open (outName, O_WRONLY | O_CREAT | O_TRUNC, mode); |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 620 | } |
Erik Andersen | 00266d3 | 1999-12-28 00:17:46 +0000 | [diff] [blame] | 621 | if (outFd < 0) { |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 622 | perror (outName); |
Erik Andersen | 00266d3 | 1999-12-28 00:17:46 +0000 | [diff] [blame] | 623 | skipFileFlag = TRUE; |
| 624 | return; |
| 625 | } |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 626 | /* Set the file time */ |
| 627 | utb.actime = mtime; |
| 628 | utb.modtime = mtime; |
| 629 | utime (outName, &utb); |
| 630 | /* Set the file permissions */ |
| 631 | chown(outName, uid, gid); |
| 632 | chmod(outName, mode); |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 633 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 634 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 635 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 636 | /* |
| 637 | * If the file is empty, then that's all we need to do. |
| 638 | */ |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 639 | if (size == 0 && (tostdoutFlag == FALSE) && (devFileFlag == FALSE)) { |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 640 | close (outFd); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 641 | outFd = -1; |
| 642 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | |
| 646 | /* |
| 647 | * Handle a data block of some specified size that was read. |
| 648 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 649 | static void readData (const char *cp, int count) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 650 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 651 | /* |
| 652 | * Reduce the amount of data left in this file. |
| 653 | * If there is no more data left, then we need to read |
| 654 | * the header again. |
| 655 | */ |
| 656 | dataCc -= count; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 657 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 658 | if (dataCc <= 0) |
| 659 | inHeader = TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 660 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 661 | /* |
| 662 | * If we aren't extracting files or this file is being |
| 663 | * skipped then do nothing more. |
| 664 | */ |
Eric Andersen | 5556c18 | 1999-11-10 19:27:58 +0000 | [diff] [blame] | 665 | if (extractFlag==FALSE || skipFileFlag==TRUE) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 666 | return; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 667 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 668 | /* |
| 669 | * Write the data to the output file. |
| 670 | */ |
| 671 | if (fullWrite (outFd, cp, count) < 0) { |
| 672 | perror (outName); |
| 673 | if (tostdoutFlag == FALSE) { |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 674 | close (outFd); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 675 | outFd = -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 676 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 677 | skipFileFlag = TRUE; |
| 678 | return; |
| 679 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 680 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 681 | /* |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 682 | * Check if we are done writing to the file now. |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 683 | */ |
| 684 | if (dataCc <= 0 && tostdoutFlag == FALSE) { |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 685 | struct utimbuf utb; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 686 | if (close (outFd)) |
| 687 | perror (outName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 688 | |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 689 | /* Set the file time */ |
| 690 | utb.actime = mtime; |
| 691 | utb.modtime = mtime; |
| 692 | utime (outName, &utb); |
| 693 | /* Set the file permissions */ |
| 694 | chown(outName, uid, gid); |
| 695 | chmod(outName, mode); |
| 696 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 697 | outFd = -1; |
| 698 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | |
| 702 | /* |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 703 | * See if the specified file name belongs to one of the specified list |
| 704 | * of path prefixes. An empty list implies that all files are wanted. |
| 705 | * Returns TRUE if the file is selected. |
| 706 | */ |
| 707 | static int |
| 708 | wantFileName (const char *fileName, int fileCount, char **fileTable) |
| 709 | { |
| 710 | const char *pathName; |
| 711 | int fileLength; |
| 712 | int pathLength; |
| 713 | |
| 714 | /* |
| 715 | * If there are no files in the list, then the file is wanted. |
| 716 | */ |
| 717 | if (fileCount == 0) |
| 718 | return TRUE; |
| 719 | |
| 720 | fileLength = strlen (fileName); |
| 721 | |
| 722 | /* |
| 723 | * Check each of the test paths. |
| 724 | */ |
| 725 | while (fileCount-- > 0) { |
| 726 | pathName = *fileTable++; |
| 727 | |
| 728 | pathLength = strlen (pathName); |
| 729 | |
| 730 | if (fileLength < pathLength) |
| 731 | continue; |
| 732 | |
| 733 | if (memcmp (fileName, pathName, pathLength) != 0) |
| 734 | continue; |
| 735 | |
| 736 | if ((fileLength == pathLength) || (fileName[pathLength] == '/')) { |
| 737 | return TRUE; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | return FALSE; |
| 742 | } |
| 743 | |
| 744 | /* |
| 745 | * Read an octal value in a field of the specified width, with optional |
| 746 | * spaces on both sides of the number and with an optional null character |
| 747 | * at the end. Returns -1 on an illegal format. |
| 748 | */ |
| 749 | static long getOctal (const char *cp, int len) |
| 750 | { |
| 751 | long val; |
| 752 | |
| 753 | while ((len > 0) && (*cp == ' ')) { |
| 754 | cp++; |
| 755 | len--; |
| 756 | } |
| 757 | |
| 758 | if ((len == 0) || !isOctal (*cp)) |
| 759 | return -1; |
| 760 | |
| 761 | val = 0; |
| 762 | |
| 763 | while ((len > 0) && isOctal (*cp)) { |
| 764 | val = val * 8 + *cp++ - '0'; |
| 765 | len--; |
| 766 | } |
| 767 | |
| 768 | while ((len > 0) && (*cp == ' ')) { |
| 769 | cp++; |
| 770 | len--; |
| 771 | } |
| 772 | |
| 773 | if ((len > 0) && *cp) |
| 774 | return -1; |
| 775 | |
| 776 | return val; |
| 777 | } |
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | /* From here to the end of the file is the tar writing stuff. |
| 783 | * If you do not have BB_FEATURE_TAR_CREATE defined, this will |
| 784 | * not be built. |
| 785 | * */ |
| 786 | #ifdef BB_FEATURE_TAR_CREATE |
| 787 | |
| 788 | /* |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 789 | * Write a tar file containing the specified files. |
| 790 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 791 | static void writeTarFile (int fileCount, char **fileTable) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 792 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 793 | struct stat statbuf; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 794 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 795 | /* |
| 796 | * Make sure there is at least one file specified. |
| 797 | */ |
| 798 | if (fileCount <= 0) { |
| 799 | fprintf (stderr, "No files specified to be saved\n"); |
| 800 | errorFlag = TRUE; |
| 801 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 802 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 803 | /* |
| 804 | * Create the tar file for writing. |
| 805 | */ |
| 806 | if ((tarName == NULL) || !strcmp (tarName, "-")) { |
| 807 | tostdoutFlag = TRUE; |
Eric Andersen | 08b1034 | 1999-11-19 02:38:58 +0000 | [diff] [blame] | 808 | tarFd = fileno(stdout); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 809 | } else |
| 810 | tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 811 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 812 | if (tarFd < 0) { |
| 813 | perror (tarName); |
| 814 | errorFlag = TRUE; |
| 815 | return; |
| 816 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 817 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 818 | /* |
| 819 | * Get the device and inode of the tar file for checking later. |
| 820 | */ |
| 821 | if (fstat (tarFd, &statbuf) < 0) { |
| 822 | perror (tarName); |
| 823 | errorFlag = TRUE; |
| 824 | goto done; |
| 825 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 826 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 827 | tarDev = statbuf.st_dev; |
| 828 | tarInode = statbuf.st_ino; |
Eric Andersen | 03018f7 | 1999-11-29 04:29:13 +0000 | [diff] [blame] | 829 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 830 | /* |
| 831 | * Append each file name into the archive file. |
| 832 | * Follow symbolic links for these top level file names. |
| 833 | */ |
Eric Andersen | 5556c18 | 1999-11-10 19:27:58 +0000 | [diff] [blame] | 834 | while (errorFlag==FALSE && (fileCount-- > 0)) { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 835 | saveFile (*fileTable++, FALSE); |
| 836 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 837 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 838 | /* |
| 839 | * Now write an empty block of zeroes to end the archive. |
| 840 | */ |
| 841 | writeTarBlock ("", 1); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 842 | |
| 843 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 844 | done: |
| 845 | /* |
| 846 | * Close the tar file and check for errors if it was opened. |
| 847 | */ |
| 848 | if ((tostdoutFlag == FALSE) && (tarFd >= 0) && (close (tarFd) < 0)) |
| 849 | perror (tarName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 852 | /* |
| 853 | * Save one file into the tar file. |
| 854 | * If the file is a directory, then this will recursively save all of |
| 855 | * the files and directories within the directory. The seeLinks |
| 856 | * flag indicates whether or not we want to see symbolic links as |
| 857 | * they really are, instead of blindly following them. |
| 858 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 859 | static void saveFile (const char *fileName, int seeLinks) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 860 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 861 | int status; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 862 | struct stat statbuf; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 863 | |
Eric Andersen | 5556c18 | 1999-11-10 19:27:58 +0000 | [diff] [blame] | 864 | if (verboseFlag==TRUE) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 865 | printf ("a %s\n", fileName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 866 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 867 | /* |
| 868 | * Check that the file name will fit in the header. |
| 869 | */ |
| 870 | if (strlen (fileName) >= TAR_NAME_SIZE) { |
| 871 | fprintf (stderr, "%s: File name is too long\n", fileName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 872 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 873 | return; |
| 874 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 875 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 876 | /* |
| 877 | * Find out about the file. |
| 878 | */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 879 | #ifdef S_ISLNK |
Eric Andersen | 5556c18 | 1999-11-10 19:27:58 +0000 | [diff] [blame] | 880 | if (seeLinks==TRUE) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 881 | status = lstat (fileName, &statbuf); |
| 882 | else |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 883 | #endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 884 | status = stat (fileName, &statbuf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 885 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 886 | if (status < 0) { |
| 887 | perror (fileName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 888 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 889 | return; |
| 890 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 891 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 892 | /* |
| 893 | * Make sure we aren't trying to save our file into itself. |
| 894 | */ |
| 895 | if ((statbuf.st_dev == tarDev) && (statbuf.st_ino == tarInode)) { |
| 896 | fprintf (stderr, "Skipping saving of archive file itself\n"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 897 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 898 | return; |
| 899 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 900 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 901 | /* |
| 902 | * Check the type of file. |
| 903 | */ |
| 904 | mode = statbuf.st_mode; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 905 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 906 | if (S_ISDIR (mode)) { |
| 907 | saveDirectory (fileName, &statbuf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 908 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 909 | return; |
| 910 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 911 | if (S_ISREG (mode)) { |
| 912 | saveRegularFile (fileName, &statbuf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 913 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 914 | return; |
| 915 | } |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 916 | |
| 917 | /* Some day add support for tarring these up... but not today. :) */ |
| 918 | // if (S_ISLNK(mode) || S_ISFIFO(mode) || S_ISBLK(mode) || S_ISCHR (mode) ) { |
| 919 | // fprintf (stderr, "%s: This version of tar can't store this type of file\n", fileName); |
| 920 | // } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 921 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 922 | /* |
| 923 | * The file is a strange type of file, ignore it. |
| 924 | */ |
| 925 | fprintf (stderr, "%s: not a directory or regular file\n", fileName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | |
| 929 | /* |
| 930 | * Save a regular file to the tar file. |
| 931 | */ |
| 932 | static void |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 933 | saveRegularFile (const char *fileName, const struct stat *statbuf) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 934 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 935 | int sawEof; |
| 936 | int fileFd; |
| 937 | int cc; |
| 938 | int dataCount; |
| 939 | long fullDataCount; |
| 940 | char data[TAR_BLOCK_SIZE * 16]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 941 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 942 | /* |
| 943 | * Open the file for reading. |
| 944 | */ |
| 945 | fileFd = open (fileName, O_RDONLY); |
| 946 | |
| 947 | if (fileFd < 0) { |
| 948 | perror (fileName); |
| 949 | |
| 950 | return; |
| 951 | } |
| 952 | |
| 953 | /* |
| 954 | * Write out the header for the file. |
| 955 | */ |
| 956 | writeHeader (fileName, statbuf); |
| 957 | |
| 958 | /* |
| 959 | * Write the data blocks of the file. |
| 960 | * We must be careful to write the amount of data that the stat |
| 961 | * buffer indicated, even if the file has changed size. Otherwise |
| 962 | * the tar file will be incorrect. |
| 963 | */ |
| 964 | fullDataCount = statbuf->st_size; |
| 965 | sawEof = FALSE; |
| 966 | |
| 967 | while (fullDataCount > 0) { |
| 968 | /* |
| 969 | * Get the amount to write this iteration which is |
| 970 | * the minumum of the amount left to write and the |
| 971 | * buffer size. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 972 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 973 | dataCount = sizeof (data); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 974 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 975 | if (dataCount > fullDataCount) |
| 976 | dataCount = (int) fullDataCount; |
| 977 | |
| 978 | /* |
| 979 | * Read the data from the file if we haven't seen the |
| 980 | * end of file yet. |
| 981 | */ |
| 982 | cc = 0; |
| 983 | |
Eric Andersen | 5556c18 | 1999-11-10 19:27:58 +0000 | [diff] [blame] | 984 | if (sawEof==FALSE) { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 985 | cc = fullRead (fileFd, data, dataCount); |
| 986 | |
| 987 | if (cc < 0) { |
| 988 | perror (fileName); |
| 989 | |
| 990 | (void) close (fileFd); |
| 991 | errorFlag = TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 992 | |
| 993 | return; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | /* |
| 997 | * If the file ended too soon, complain and set |
| 998 | * a flag so we will zero fill the rest of it. |
| 999 | */ |
| 1000 | if (cc < dataCount) { |
| 1001 | fprintf (stderr, |
| 1002 | "%s: Short read - zero filling", fileName); |
| 1003 | |
| 1004 | sawEof = TRUE; |
| 1005 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1008 | /* |
| 1009 | * Zero fill the rest of the data if necessary. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1010 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1011 | if (cc < dataCount) |
| 1012 | memset (data + cc, 0, dataCount - cc); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1013 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1014 | /* |
| 1015 | * Write the buffer to the TAR file. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1016 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1017 | writeTarBlock (data, dataCount); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1018 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1019 | fullDataCount -= dataCount; |
| 1020 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1021 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1022 | /* |
| 1023 | * Close the file. |
| 1024 | */ |
| 1025 | if ((tostdoutFlag == FALSE) && close (fileFd) < 0) |
| 1026 | fprintf (stderr, "%s: close: %s\n", fileName, strerror (errno)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | |
| 1030 | /* |
| 1031 | * Save a directory and all of its files to the tar file. |
| 1032 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1033 | static void saveDirectory (const char *dirName, const struct stat *statbuf) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1034 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1035 | DIR *dir; |
| 1036 | struct dirent *entry; |
| 1037 | int needSlash; |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 1038 | char fullName[NAME_MAX]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1039 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1040 | /* |
| 1041 | * Construct the directory name as used in the tar file by appending |
| 1042 | * a slash character to it. |
| 1043 | */ |
| 1044 | strcpy (fullName, dirName); |
| 1045 | strcat (fullName, "/"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1046 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1047 | /* |
| 1048 | * Write out the header for the directory entry. |
| 1049 | */ |
| 1050 | writeHeader (fullName, statbuf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1051 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1052 | /* |
| 1053 | * Open the directory. |
| 1054 | */ |
| 1055 | dir = opendir (dirName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1056 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1057 | if (dir == NULL) { |
| 1058 | fprintf (stderr, "Cannot read directory \"%s\": %s\n", |
| 1059 | dirName, strerror (errno)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1060 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1061 | return; |
| 1062 | } |
| 1063 | |
| 1064 | /* |
| 1065 | * See if a slash is needed. |
| 1066 | */ |
| 1067 | needSlash = (*dirName && (dirName[strlen (dirName) - 1] != '/')); |
| 1068 | |
| 1069 | /* |
| 1070 | * Read all of the directory entries and check them, |
| 1071 | * except for the current and parent directory entries. |
| 1072 | */ |
Eric Andersen | 5556c18 | 1999-11-10 19:27:58 +0000 | [diff] [blame] | 1073 | while (errorFlag==FALSE && ((entry = readdir (dir)) != NULL)) { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1074 | if ((strcmp (entry->d_name, ".") == 0) || |
| 1075 | (strcmp (entry->d_name, "..") == 0)) { |
| 1076 | continue; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1079 | /* |
| 1080 | * Build the full path name to the file. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1081 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1082 | strcpy (fullName, dirName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1083 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1084 | if (needSlash) |
| 1085 | strcat (fullName, "/"); |
| 1086 | |
| 1087 | strcat (fullName, entry->d_name); |
| 1088 | |
| 1089 | /* |
| 1090 | * Write this file to the tar file, noticing whether or not |
| 1091 | * the file is a symbolic link. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1092 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1093 | saveFile (fullName, TRUE); |
| 1094 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1095 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1096 | /* |
| 1097 | * All done, close the directory. |
| 1098 | */ |
| 1099 | closedir (dir); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | |
| 1103 | /* |
| 1104 | * Write a tar header for the specified file name and status. |
| 1105 | * It is assumed that the file name fits. |
| 1106 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1107 | static void writeHeader (const char *fileName, const struct stat *statbuf) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1108 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1109 | long checkSum; |
| 1110 | const unsigned char *cp; |
| 1111 | int len; |
| 1112 | TarHeader header; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1113 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1114 | /* |
| 1115 | * Zero the header block in preparation for filling it in. |
| 1116 | */ |
| 1117 | memset ((char *) &header, 0, sizeof (header)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1118 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1119 | /* |
| 1120 | * Fill in the header. |
| 1121 | */ |
| 1122 | strcpy (header.name, fileName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1123 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1124 | strncpy (header.magic, TAR_MAGIC, sizeof (header.magic)); |
| 1125 | strncpy (header.version, TAR_VERSION, sizeof (header.version)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1126 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1127 | putOctal (header.mode, sizeof (header.mode), statbuf->st_mode & 0777); |
| 1128 | putOctal (header.uid, sizeof (header.uid), statbuf->st_uid); |
| 1129 | putOctal (header.gid, sizeof (header.gid), statbuf->st_gid); |
| 1130 | putOctal (header.size, sizeof (header.size), statbuf->st_size); |
| 1131 | putOctal (header.mtime, sizeof (header.mtime), statbuf->st_mtime); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1132 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1133 | header.typeFlag = TAR_TYPE_REGULAR; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1134 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1135 | /* |
| 1136 | * Calculate and store the checksum. |
| 1137 | * This is the sum of all of the bytes of the header, |
| 1138 | * with the checksum field itself treated as blanks. |
| 1139 | */ |
| 1140 | memset (header.checkSum, ' ', sizeof (header.checkSum)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1141 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1142 | cp = (const unsigned char *) &header; |
| 1143 | len = sizeof (header); |
| 1144 | checkSum = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1145 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1146 | while (len-- > 0) |
| 1147 | checkSum += *cp++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1148 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1149 | putOctal (header.checkSum, sizeof (header.checkSum), checkSum); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1150 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1151 | /* |
| 1152 | * Write the tar header. |
| 1153 | */ |
| 1154 | writeTarBlock ((const char *) &header, sizeof (header)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | |
| 1158 | /* |
| 1159 | * Write data to one or more blocks of the tar file. |
| 1160 | * The data is always padded out to a multiple of TAR_BLOCK_SIZE. |
| 1161 | * The errorFlag static variable is set on an error. |
| 1162 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1163 | static void writeTarBlock (const char *buf, int len) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1164 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1165 | int partialLength; |
| 1166 | int completeLength; |
| 1167 | char fullBlock[TAR_BLOCK_SIZE]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1168 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1169 | /* |
| 1170 | * If we had a write error before, then do nothing more. |
| 1171 | */ |
Eric Andersen | 5556c18 | 1999-11-10 19:27:58 +0000 | [diff] [blame] | 1172 | if (errorFlag==TRUE) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1173 | return; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1174 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1175 | /* |
| 1176 | * Get the amount of complete and partial blocks. |
| 1177 | */ |
| 1178 | partialLength = len % TAR_BLOCK_SIZE; |
| 1179 | completeLength = len - partialLength; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1180 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1181 | /* |
| 1182 | * Write all of the complete blocks. |
| 1183 | */ |
| 1184 | if ((completeLength > 0) && !fullWrite (tarFd, buf, completeLength)) { |
| 1185 | perror (tarName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1186 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1187 | errorFlag = TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1188 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1189 | return; |
| 1190 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1191 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1192 | /* |
| 1193 | * If there are no partial blocks left, we are done. |
| 1194 | */ |
| 1195 | if (partialLength == 0) |
| 1196 | return; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1197 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1198 | /* |
| 1199 | * Copy the partial data into a complete block, and pad the rest |
| 1200 | * of it with zeroes. |
| 1201 | */ |
| 1202 | memcpy (fullBlock, buf + completeLength, partialLength); |
| 1203 | memset (fullBlock + partialLength, 0, TAR_BLOCK_SIZE - partialLength); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1204 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1205 | /* |
| 1206 | * Write the last complete block. |
| 1207 | */ |
| 1208 | if (!fullWrite (tarFd, fullBlock, TAR_BLOCK_SIZE)) { |
| 1209 | perror (tarName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1210 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1211 | errorFlag = TRUE; |
| 1212 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | |
| 1216 | /* |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1217 | * Put an octal string into the specified buffer. |
| 1218 | * The number is zero and space padded and possibly null padded. |
| 1219 | * Returns TRUE if successful. |
| 1220 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1221 | static int putOctal (char *cp, int len, long value) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1222 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1223 | int tempLength; |
| 1224 | char *tempString; |
| 1225 | char tempBuffer[32]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1226 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1227 | /* |
| 1228 | * Create a string of the specified length with an initial space, |
| 1229 | * leading zeroes and the octal number, and a trailing null. |
| 1230 | */ |
| 1231 | tempString = tempBuffer; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1232 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1233 | sprintf (tempString, " %0*lo", len - 2, value); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1234 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1235 | tempLength = strlen (tempString) + 1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1236 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1237 | /* |
| 1238 | * If the string is too large, suppress the leading space. |
| 1239 | */ |
| 1240 | if (tempLength > len) { |
| 1241 | tempLength--; |
| 1242 | tempString++; |
| 1243 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1244 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1245 | /* |
| 1246 | * If the string is still too large, suppress the trailing null. |
| 1247 | */ |
| 1248 | if (tempLength > len) |
| 1249 | tempLength--; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1250 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1251 | /* |
| 1252 | * If the string is still too large, fail. |
| 1253 | */ |
| 1254 | if (tempLength > len) |
| 1255 | return FALSE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1256 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1257 | /* |
| 1258 | * Copy the string to the field. |
| 1259 | */ |
| 1260 | memcpy (cp, tempString, len); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1261 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1262 | return TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1263 | } |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 1264 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1265 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1266 | /* END CODE */ |