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 | 68a9ea4 | 2000-04-04 18:39:50 +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 pretty |
| 7 | * different (i.e. cleaner, less global variables, etc) |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 8 | * |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 9 | * Copyright (C) 2000 by Lineo, inc. |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 10 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 11 | * |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 12 | * Based in part in the tar implementation in sash |
| 13 | * Copyright (c) 1999 by David I. Bell |
| 14 | * Permission is granted to use, distribute, or modify this source, |
| 15 | * provided that this copyright notice remains intact. |
| 16 | * Permission to distribute sash derived code under the GPL has been granted. |
| 17 | * |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 18 | * Based in part on the tar implementation from busybox-0.28 |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 19 | * Copyright (C) 1995 Bruce Perens |
| 20 | * This is free software under the GNU General Public License. |
| 21 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 22 | * This program is free software; you can redistribute it and/or modify |
| 23 | * it under the terms of the GNU General Public License as published by |
| 24 | * the Free Software Foundation; either version 2 of the License, or |
| 25 | * (at your option) any later version. |
| 26 | * |
| 27 | * This program is distributed in the hope that it will be useful, |
| 28 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 30 | * General Public License for more details. |
| 31 | * |
| 32 | * You should have received a copy of the GNU General Public License |
| 33 | * along with this program; if not, write to the Free Software |
| 34 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 35 | * |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 36 | */ |
| 37 | |
| 38 | |
| 39 | #include "internal.h" |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 40 | #define BB_DECLARE_EXTERN |
| 41 | #define bb_need_io_error |
| 42 | #include "messages.c" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 43 | #include <stdio.h> |
| 44 | #include <dirent.h> |
| 45 | #include <errno.h> |
| 46 | #include <fcntl.h> |
| 47 | #include <signal.h> |
| 48 | #include <time.h> |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 49 | #include <utime.h> |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 50 | #include <sys/types.h> |
Eric Andersen | 08b1034 | 1999-11-19 02:38:58 +0000 | [diff] [blame] | 51 | #include <sys/sysmacros.h> |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 52 | #include <sys/param.h> /* for PATH_MAX */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 53 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 54 | |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 55 | #ifdef BB_FEATURE_TAR_CREATE |
| 56 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 57 | static const char tar_usage[] = |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 58 | "tar -[cxtvOf] [tarFileName] [FILE] ...\n\n" |
| 59 | "Create, extract, or list files from a tar file.\n\n" |
| 60 | "Options:\n" |
| 61 | |
| 62 | "\tc=create, x=extract, t=list contents, v=verbose,\n" |
| 63 | "\tO=extract to stdout, f=tarfile or \"-\" for stdin\n"; |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 64 | |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 65 | #else |
| 66 | |
| 67 | static const char tar_usage[] = |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 68 | "tar -[xtvOf] [tarFileName] [FILE] ...\n\n" |
| 69 | "Extract, or list files stored in a tar file. This\n" |
| 70 | "version of tar does not support creation of tar files.\n\n" |
| 71 | "Options:\n" |
| 72 | |
| 73 | "\tx=extract, t=list contents, v=verbose,\n" |
| 74 | "\tO=extract to stdout, f=tarfile or \"-\" for stdin\n"; |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 75 | |
| 76 | #endif |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 77 | |
| 78 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 79 | /* Tar file constants */ |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 80 | #ifndef MAJOR |
| 81 | #define MAJOR(dev) (((dev)>>8)&0xff) |
| 82 | #define MINOR(dev) ((dev)&0xff) |
| 83 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 84 | |
| 85 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 86 | /* POSIX tar Header Block, from POSIX 1003.1-1990 */ |
| 87 | struct TarHeader |
| 88 | { |
| 89 | /* byte offset */ |
| 90 | char name[100]; /* 0 */ |
| 91 | char mode[8]; /* 100 */ |
| 92 | char uid[8]; /* 108 */ |
| 93 | char gid[8]; /* 116 */ |
| 94 | char size[12]; /* 124 */ |
| 95 | char mtime[12]; /* 136 */ |
| 96 | char chksum[8]; /* 148 */ |
| 97 | char typeflag; /* 156 */ |
| 98 | char linkname[100]; /* 157 */ |
| 99 | char magic[6]; /* 257 */ |
| 100 | char version[2]; /* 263 */ |
| 101 | char uname[32]; /* 265 */ |
| 102 | char gname[32]; /* 297 */ |
| 103 | char devmajor[8]; /* 329 */ |
| 104 | char devminor[8]; /* 337 */ |
| 105 | char prefix[155]; /* 345 */ |
| 106 | /* padding 500 */ |
| 107 | }; |
| 108 | typedef struct TarHeader TarHeader; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 109 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 110 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 111 | /* A few useful constants */ |
| 112 | #define TAR_MAGIC "ustar" /* ustar and a null */ |
| 113 | #define TAR_VERSION "00" /* 00 and no null */ |
| 114 | #define TAR_MAGIC_LEN 6 |
| 115 | #define TAR_VERSION_LEN 2 |
| 116 | #define TAR_NAME_LEN 100 |
| 117 | #define TAR_BLOCK_SIZE 512 |
| 118 | |
| 119 | /* A nice enum with all the possible tar file content types */ |
| 120 | enum TarFileType |
| 121 | { |
| 122 | REGTYPE = '0', /* regular file */ |
| 123 | REGTYPE0 = '\0', /* regular file (ancient bug compat)*/ |
| 124 | LNKTYPE = '1', /* hard link */ |
| 125 | SYMTYPE = '2', /* symbolic link */ |
| 126 | CHRTYPE = '3', /* character special */ |
| 127 | BLKTYPE = '4', /* block special */ |
| 128 | DIRTYPE = '5', /* directory */ |
| 129 | FIFOTYPE = '6', /* FIFO special */ |
| 130 | CONTTYPE = '7', /* reserved */ |
| 131 | }; |
| 132 | typedef enum TarFileType TarFileType; |
| 133 | |
| 134 | /* This struct ignores magic, non-numeric user name, |
| 135 | * non-numeric group name, and the checksum, since |
| 136 | * these are all ignored by BusyBox tar. */ |
| 137 | struct TarInfo |
| 138 | { |
| 139 | int tarFd; /* An open file descriptor for reading from the tarball */ |
| 140 | char * name; /* File name */ |
| 141 | mode_t mode; /* Unix mode, including device bits. */ |
| 142 | uid_t uid; /* Numeric UID */ |
| 143 | gid_t gid; /* Numeric GID */ |
| 144 | size_t size; /* Size of file */ |
| 145 | time_t mtime; /* Last-modified time */ |
| 146 | enum TarFileType type; /* Regular, directory, link, etc */ |
| 147 | char * linkname; /* Name for symbolic and hard links */ |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 148 | long devmajor; /* Major number for special device */ |
| 149 | long devminor; /* Minor number for special device */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 150 | }; |
| 151 | typedef struct TarInfo TarInfo; |
| 152 | |
| 153 | /* Static data */ |
| 154 | static const unsigned long TarChecksumOffset = (const unsigned long)&(((TarHeader *)0)->chksum); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 155 | |
| 156 | |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 157 | /* Local procedures to restore files from a tar file. */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 158 | static int readTarFile(const char* tarName, int extractFlag, int listFlag, |
| 159 | int tostdoutFlag, int verboseFlag); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 160 | |
| 161 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 162 | |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 163 | #ifdef BB_FEATURE_TAR_CREATE |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 164 | /* Local procedures to save files into a tar file. */ |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 165 | static int writeTarFile(const char* tarName, int tostdoutFlag, |
| 166 | int verboseFlag, int argc, char **argv); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 167 | static int putOctal(char *cp, int len, long value); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 168 | |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 169 | #endif |
| 170 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 171 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 172 | extern int tar_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 173 | { |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 174 | const char *tarName=NULL; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 175 | const char *options; |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 176 | int listFlag = FALSE; |
| 177 | int extractFlag = FALSE; |
| 178 | int createFlag = FALSE; |
| 179 | int verboseFlag = FALSE; |
| 180 | int tostdoutFlag = FALSE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 181 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 182 | argc--; |
| 183 | argv++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 184 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 185 | if (argc < 1) |
| 186 | usage(tar_usage); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 187 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 188 | /* Parse options */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 189 | if (**argv == '-') |
| 190 | options = (*argv++) + 1; |
| 191 | else |
| 192 | options = (*argv++); |
| 193 | argc--; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 194 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 195 | for (; *options; options++) { |
| 196 | switch (*options) { |
| 197 | case 'f': |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 198 | if (tarName != NULL) |
| 199 | fatalError( "Only one 'f' option allowed\n"); |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 200 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 201 | tarName = *argv++; |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 202 | if (tarName == NULL) |
| 203 | fatalError( "Option requires an argument: No file specified\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 204 | argc--; |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 205 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 206 | break; |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 207 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 208 | case 't': |
| 209 | if (extractFlag == TRUE || createFlag == TRUE) |
| 210 | goto flagError; |
| 211 | listFlag = TRUE; |
| 212 | break; |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 213 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 214 | case 'x': |
| 215 | if (listFlag == TRUE || createFlag == TRUE) |
| 216 | goto flagError; |
| 217 | extractFlag = TRUE; |
| 218 | break; |
| 219 | case 'c': |
| 220 | if (extractFlag == TRUE || listFlag == TRUE) |
| 221 | goto flagError; |
| 222 | createFlag = TRUE; |
| 223 | break; |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 224 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 225 | case 'v': |
| 226 | verboseFlag = TRUE; |
| 227 | break; |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 228 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 229 | case 'O': |
| 230 | tostdoutFlag = TRUE; |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 231 | tarName = "-"; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 232 | break; |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 233 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 234 | case '-': |
| 235 | usage(tar_usage); |
| 236 | break; |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 237 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 238 | default: |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 239 | fatalError( "Unknown tar flag '%c'\n" |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 240 | "Try `tar --help' for more information\n", *options); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 241 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 244 | /* |
| 245 | * Do the correct type of action supplying the rest of the |
| 246 | * command line arguments as the list of files to process. |
| 247 | */ |
| 248 | if (createFlag == TRUE) { |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 249 | #ifndef BB_FEATURE_TAR_CREATE |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 250 | fatalError( "This version of tar was not compiled with tar creation support.\n"); |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 251 | #else |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 252 | exit(writeTarFile(tarName, tostdoutFlag, verboseFlag, argc, argv)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 253 | #endif |
| 254 | } else { |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 255 | exit(readTarFile(tarName, extractFlag, listFlag, tostdoutFlag, verboseFlag)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 256 | } |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 257 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 258 | flagError: |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 259 | fatalError( "Exactly one of 'c', 'x' or 't' must be specified\n"); |
| 260 | } |
| 261 | |
| 262 | static void |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 263 | tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag) |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 264 | { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 265 | size_t writeSize; |
| 266 | size_t readSize; |
| 267 | size_t actualWriteSz; |
| 268 | char buffer[BUFSIZ]; |
| 269 | size_t size = header->size; |
| 270 | int outFd=fileno(stdout); |
| 271 | |
| 272 | /* Open the file to be written, if a file is supposed to be written */ |
| 273 | if (extractFlag==TRUE && tostdoutFlag==FALSE) { |
| 274 | if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY, header->mode & ~S_IFMT)) < 0) |
| 275 | errorMsg(io_error, header->name, strerror(errno)); |
| 276 | /* Create the path to the file, just in case it isn't there... |
| 277 | * This should not screw up path permissions or anything. */ |
| 278 | createPath(header->name, 0777); |
| 279 | } |
| 280 | |
| 281 | /* Write out the file, if we are supposed to be doing that */ |
| 282 | while ( size > 0 ) { |
| 283 | actualWriteSz=0; |
| 284 | if ( size > sizeof(buffer) ) |
| 285 | writeSize = readSize = sizeof(buffer); |
| 286 | else { |
| 287 | int mod = size % 512; |
| 288 | if ( mod != 0 ) |
| 289 | readSize = size + (512 - mod); |
| 290 | else |
| 291 | readSize = size; |
| 292 | writeSize = size; |
| 293 | } |
| 294 | if ( (readSize = fullRead(header->tarFd, buffer, readSize)) <= 0 ) { |
| 295 | /* Tarball seems to have a problem */ |
Erik Andersen | deea048 | 2000-03-25 23:09:29 +0000 | [diff] [blame] | 296 | errorMsg("tar: Unexpected EOF in archive\n"); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 297 | return; |
| 298 | } |
| 299 | if ( readSize < writeSize ) |
| 300 | writeSize = readSize; |
| 301 | |
| 302 | /* Write out the file, if we are supposed to be doing that */ |
| 303 | if (extractFlag==TRUE) { |
| 304 | |
| 305 | if ((actualWriteSz=fullWrite(outFd, buffer, writeSize)) != writeSize ) { |
| 306 | /* Output file seems to have a problem */ |
| 307 | errorMsg(io_error, header->name, strerror(errno)); |
| 308 | return; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | size -= actualWriteSz; |
| 313 | } |
| 314 | |
| 315 | /* Now we are done writing the file out, so try |
| 316 | * and fix up the permissions and whatnot */ |
| 317 | if (extractFlag==TRUE && tostdoutFlag==FALSE) { |
| 318 | struct utimbuf t; |
| 319 | /* Now set permissions etc for the new file */ |
| 320 | fchown(outFd, header->uid, header->gid); |
| 321 | fchmod(outFd, header->mode & ~S_IFMT); |
| 322 | close(outFd); |
| 323 | /* File must be closed before trying to change the date */ |
| 324 | t.actime = time(0); |
| 325 | t.modtime = header->mtime; |
| 326 | utime(header->name, &t); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | static void |
| 331 | fixUpPermissions(TarInfo *header) |
| 332 | { |
| 333 | struct utimbuf t; |
| 334 | /* Now set permissions etc for the new file */ |
| 335 | chown(header->name, header->uid, header->gid); |
| 336 | chmod(header->name, header->mode); |
| 337 | /* Reset the time */ |
| 338 | t.actime = time(0); |
| 339 | t.modtime = header->mtime; |
| 340 | utime(header->name, &t); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 341 | } |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 342 | |
| 343 | static void |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 344 | tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag) |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 345 | { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 346 | |
| 347 | if (extractFlag==FALSE || tostdoutFlag==TRUE) |
| 348 | return; |
| 349 | |
| 350 | if (createPath(header->name, header->mode) != TRUE) { |
| 351 | errorMsg("Error creating directory '%s': %s", header->name, strerror(errno)); |
| 352 | return; |
| 353 | } |
| 354 | /* make the final component, just in case it was |
| 355 | * omitted by createPath() (which will skip the |
| 356 | * directory if it doesn't have a terminating '/') */ |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 357 | if (mkdir(header->name, header->mode) == 0) { |
| 358 | fixUpPermissions(header); |
| 359 | } |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | static void |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 363 | tarExtractHardLink(TarInfo *header, int extractFlag, int tostdoutFlag) |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 364 | { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 365 | if (extractFlag==FALSE || tostdoutFlag==TRUE) |
| 366 | return; |
| 367 | |
| 368 | if (link(header->linkname, header->name) < 0) { |
Erik Andersen | deea048 | 2000-03-25 23:09:29 +0000 | [diff] [blame] | 369 | errorMsg("Error creating hard link '%s': %s\n", header->linkname, strerror(errno)); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 370 | return; |
| 371 | } |
| 372 | |
| 373 | /* Now set permissions etc for the new directory */ |
| 374 | fixUpPermissions(header); |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | static void |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 378 | tarExtractSymLink(TarInfo *header, int extractFlag, int tostdoutFlag) |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 379 | { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 380 | if (extractFlag==FALSE || tostdoutFlag==TRUE) |
| 381 | return; |
| 382 | |
| 383 | #ifdef S_ISLNK |
| 384 | if (symlink(header->linkname, header->name) < 0) { |
Erik Andersen | deea048 | 2000-03-25 23:09:29 +0000 | [diff] [blame] | 385 | errorMsg("Error creating symlink '%s': %s\n", header->linkname, strerror(errno)); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 386 | return; |
| 387 | } |
| 388 | /* Try to change ownership of the symlink. |
| 389 | * If libs doesn't support that, don't bother. |
| 390 | * Changing the pointed-to-file is the Wrong Thing(tm). |
| 391 | */ |
| 392 | #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) |
| 393 | lchown(header->name, header->uid, header->gid); |
| 394 | #endif |
| 395 | |
| 396 | /* Do not change permissions or date on symlink, |
| 397 | * since it changes the pointed to file instead. duh. */ |
| 398 | #else |
| 399 | fprintf(stderr, "Cannot create symbolic links\n"); |
| 400 | #endif |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | static void |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 404 | tarExtractSpecial(TarInfo *header, int extractFlag, int tostdoutFlag) |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 405 | { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 406 | if (extractFlag==FALSE || tostdoutFlag==TRUE) |
| 407 | return; |
| 408 | |
| 409 | if (S_ISCHR(header->mode) || S_ISBLK(header->mode) || S_ISSOCK(header->mode)) { |
| 410 | mknod(header->name, header->mode, makedev(header->devmajor, header->devminor)); |
| 411 | } else if (S_ISFIFO(header->mode)) { |
| 412 | mkfifo(header->name, header->mode); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /* Now set permissions etc for the new directory */ |
| 416 | fixUpPermissions(header); |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 419 | /* Read an octal value in a field of the specified width, with optional |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 420 | * spaces on both sides of the number and with an optional null character |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 421 | * at the end. Returns -1 on an illegal format. */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 422 | static long getOctal(const char *cp, int size) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 423 | { |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 424 | long val = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 425 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 426 | for(;(size > 0) && (*cp == ' '); cp++, size--); |
| 427 | if ((size == 0) || !isOctal(*cp)) |
| 428 | return -1; |
| 429 | for(; (size > 0) && isOctal(*cp); size--) { |
| 430 | val = val * 8 + *cp++ - '0'; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 431 | } |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 432 | for (;(size > 0) && (*cp == ' '); cp++, size--); |
| 433 | if ((size > 0) && *cp) |
| 434 | return -1; |
| 435 | return val; |
| 436 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 437 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 438 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 439 | /* Parse the tar header and fill in the nice struct with the details */ |
| 440 | static int |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 441 | readTarHeader(struct TarHeader *rawHeader, struct TarInfo *header) |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 442 | { |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 443 | int i; |
| 444 | long chksum, sum; |
| 445 | unsigned char *s = (unsigned char *)rawHeader; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 446 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 447 | header->name = rawHeader->name; |
| 448 | header->mode = getOctal(rawHeader->mode, sizeof(rawHeader->mode)); |
| 449 | header->uid = getOctal(rawHeader->uid, sizeof(rawHeader->uid)); |
| 450 | header->gid = getOctal(rawHeader->gid, sizeof(rawHeader->gid)); |
| 451 | header->size = getOctal(rawHeader->size, sizeof(rawHeader->size)); |
| 452 | header->mtime = getOctal(rawHeader->mtime, sizeof(rawHeader->mtime)); |
| 453 | chksum = getOctal(rawHeader->chksum, sizeof(rawHeader->chksum)); |
| 454 | header->type = rawHeader->typeflag; |
| 455 | header->linkname = rawHeader->linkname; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 456 | header->devmajor = getOctal(rawHeader->devmajor, sizeof(rawHeader->devmajor)); |
| 457 | header->devminor = getOctal(rawHeader->devminor, sizeof(rawHeader->devminor)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 458 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 459 | /* Check the checksum */ |
| 460 | sum = ' ' * sizeof(rawHeader->chksum); |
| 461 | for ( i = TarChecksumOffset; i > 0; i-- ) |
| 462 | sum += *s++; |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 463 | s += sizeof(rawHeader->chksum); |
| 464 | for ( i = (512 - TarChecksumOffset - sizeof(rawHeader->chksum)); i > 0; i-- ) |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 465 | sum += *s++; |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 466 | if (sum == chksum ) |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 467 | return ( TRUE); |
| 468 | return( FALSE); |
| 469 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 470 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 471 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 472 | /* |
| 473 | * Read a tar file and extract or list the specified files within it. |
| 474 | * If the list is empty than all files are extracted or listed. |
| 475 | */ |
| 476 | static int readTarFile(const char* tarName, int extractFlag, int listFlag, |
| 477 | int tostdoutFlag, int verboseFlag) |
| 478 | { |
| 479 | int status, tarFd=0; |
| 480 | int errorFlag=FALSE; |
| 481 | TarHeader rawHeader; |
| 482 | TarInfo header; |
| 483 | int alreadyWarned=FALSE; |
| 484 | //int skipFileFlag=FALSE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 485 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 486 | /* Open the tar file for reading. */ |
| 487 | if (!strcmp(tarName, "-")) |
| 488 | tarFd = fileno(stdin); |
| 489 | else |
| 490 | tarFd = open(tarName, O_RDONLY); |
| 491 | if (tarFd < 0) { |
Erik Andersen | deea048 | 2000-03-25 23:09:29 +0000 | [diff] [blame] | 492 | errorMsg( "Error opening '%s': %s\n", tarName, strerror(errno)); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 493 | return ( FALSE); |
Erik Andersen | 06936df | 2000-01-23 02:14:20 +0000 | [diff] [blame] | 494 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 495 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 496 | /* Set the umask for this process so it doesn't |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 497 | * screw up permission setting for us later. */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 498 | umask(0); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 499 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 500 | /* Read the tar file, and iterate over it one file at a time */ |
| 501 | while ( (status = fullRead(tarFd, (char*)&rawHeader, TAR_BLOCK_SIZE)) == TAR_BLOCK_SIZE ) { |
Erik Andersen | e272915 | 2000-02-18 21:34:17 +0000 | [diff] [blame] | 502 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 503 | /* First, try to read the header */ |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 504 | if ( readTarHeader(&rawHeader, &header) == FALSE ) { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 505 | close( tarFd); |
| 506 | if ( *(header.name) == '\0' ) { |
| 507 | goto endgame; |
| 508 | } else { |
| 509 | errorFlag=TRUE; |
| 510 | errorMsg("Bad tar header, skipping\n"); |
| 511 | continue; |
| 512 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 513 | } |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 514 | if ( *(header.name) == '\0' ) |
| 515 | goto endgame; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 516 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 517 | /* Check for and relativify any absolute paths */ |
| 518 | if ( *(header.name) == '/' ) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 519 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 520 | while (*(header.name) == '/') |
| 521 | ++*(header.name); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 522 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 523 | if (alreadyWarned == FALSE) { |
| 524 | errorMsg("Absolute path detected, removing leading slashes\n"); |
| 525 | alreadyWarned = TRUE; |
| 526 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 527 | } |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 528 | |
| 529 | /* Special treatment if the list (-t) flag is on */ |
| 530 | if (verboseFlag == TRUE && extractFlag == FALSE) { |
| 531 | int len, len1; |
| 532 | char buf[35]; |
| 533 | struct tm *tm = localtime (&(header.mtime)); |
| 534 | |
| 535 | len=printf("%s %d/%-d ", modeString(header.mode), header.uid, header.gid); |
| 536 | if (header.type==CHRTYPE || header.type==BLKTYPE) { |
| 537 | len1=snprintf(buf, sizeof(buf), "%ld,%-ld ", |
| 538 | header.devmajor, header.devminor); |
| 539 | } else { |
| 540 | len1=snprintf(buf, sizeof(buf), "%d ", header.size); |
| 541 | } |
| 542 | /* Jump through some hoops to make the columns match up */ |
| 543 | for(;(len+len1)<31;len++) |
| 544 | printf(" "); |
| 545 | printf(buf); |
| 546 | |
| 547 | /* Use ISO 8610 time format */ |
| 548 | if (tm) { |
| 549 | printf ("%04d-%02d-%02d %02d:%02d:%02d ", |
| 550 | tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, |
| 551 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 552 | } |
| 553 | } |
| 554 | /* List contents if we are supposed to do that */ |
| 555 | if (verboseFlag == TRUE || listFlag == TRUE) { |
| 556 | /* Now the normal listing */ |
| 557 | printf("%s", header.name); |
| 558 | /* If this is a link, say so */ |
| 559 | if (header.type==LNKTYPE) |
| 560 | printf(" link to %s", header.linkname); |
| 561 | else if (header.type==SYMTYPE) |
| 562 | printf(" -> %s", header.linkname); |
| 563 | printf("\n"); |
| 564 | } |
| 565 | |
| 566 | #if 0 |
| 567 | /* See if we want to restore this file or not */ |
| 568 | skipFileFlag=FALSE; |
| 569 | if (wantFileName(outName) == FALSE) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 570 | skipFileFlag = TRUE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 571 | } |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 572 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 573 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 574 | /* If we got here, we can be certain we have a legitimate |
| 575 | * header to work with. So work with it. */ |
| 576 | switch ( header.type ) { |
| 577 | case REGTYPE: |
| 578 | case REGTYPE0: |
| 579 | /* If the name ends in a '/' then assume it is |
| 580 | * supposed to be a directory, and fall through */ |
| 581 | if (header.name[strlen(header.name)-1] != '/') { |
| 582 | tarExtractRegularFile(&header, extractFlag, tostdoutFlag); |
| 583 | break; |
| 584 | } |
| 585 | case DIRTYPE: |
| 586 | tarExtractDirectory( &header, extractFlag, tostdoutFlag); |
| 587 | break; |
| 588 | case LNKTYPE: |
| 589 | tarExtractHardLink( &header, extractFlag, tostdoutFlag); |
| 590 | break; |
| 591 | case SYMTYPE: |
| 592 | tarExtractSymLink( &header, extractFlag, tostdoutFlag); |
| 593 | break; |
| 594 | case CHRTYPE: |
| 595 | case BLKTYPE: |
| 596 | case FIFOTYPE: |
| 597 | tarExtractSpecial( &header, extractFlag, tostdoutFlag); |
| 598 | break; |
| 599 | default: |
| 600 | close( tarFd); |
| 601 | return( FALSE); |
| 602 | } |
| 603 | } |
| 604 | close(tarFd); |
| 605 | if (status > 0) { |
| 606 | /* Bummer - we read a partial header */ |
Erik Andersen | deea048 | 2000-03-25 23:09:29 +0000 | [diff] [blame] | 607 | errorMsg( "Error reading '%s': %s\n", tarName, strerror(errno)); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 608 | return ( FALSE); |
| 609 | } |
| 610 | else |
| 611 | return( status); |
| 612 | |
| 613 | /* Stuff to do when we are done */ |
| 614 | endgame: |
| 615 | close( tarFd); |
| 616 | if ( *(header.name) == '\0' ) { |
| 617 | if (errorFlag==FALSE) |
| 618 | return( TRUE); |
| 619 | } |
| 620 | return( FALSE); |
| 621 | } |
| 622 | |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 623 | |
| 624 | #ifdef BB_FEATURE_TAR_CREATE |
| 625 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 626 | /* Some info to be carried along when creating a new tarball */ |
| 627 | struct TarBallInfo |
| 628 | { |
| 629 | char* fileName; /* File name of the tarball */ |
| 630 | int tarFd; /* Open-for-write file descriptor |
| 631 | for the tarball */ |
| 632 | struct stat statBuf; /* Stat info for the tarball, letting |
| 633 | us know the inode and device that the |
| 634 | tarball lives, so we can avoid trying |
| 635 | to include the tarball into itself */ |
| 636 | int verboseFlag; /* Whether to print extra stuff or not */ |
| 637 | }; |
| 638 | typedef struct TarBallInfo TarBallInfo; |
| 639 | |
| 640 | |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 641 | /* Put an octal string into the specified buffer. |
| 642 | * The number is zero and space padded and possibly null padded. |
| 643 | * Returns TRUE if successful. */ |
| 644 | static int putOctal (char *cp, int len, long value) |
| 645 | { |
| 646 | int tempLength; |
| 647 | char *tempString; |
| 648 | char tempBuffer[32]; |
| 649 | |
| 650 | /* Create a string of the specified length with an initial space, |
| 651 | * leading zeroes and the octal number, and a trailing null. */ |
| 652 | tempString = tempBuffer; |
| 653 | |
| 654 | sprintf (tempString, " %0*lo", len - 2, value); |
| 655 | |
| 656 | tempLength = strlen (tempString) + 1; |
| 657 | |
| 658 | /* If the string is too large, suppress the leading space. */ |
| 659 | if (tempLength > len) { |
| 660 | tempLength--; |
| 661 | tempString++; |
| 662 | } |
| 663 | |
| 664 | /* If the string is still too large, suppress the trailing null. */ |
| 665 | if (tempLength > len) |
| 666 | tempLength--; |
| 667 | |
| 668 | /* If the string is still too large, fail. */ |
| 669 | if (tempLength > len) |
| 670 | return FALSE; |
| 671 | |
| 672 | /* Copy the string to the field. */ |
| 673 | memcpy (cp, tempString, len); |
| 674 | |
| 675 | return TRUE; |
| 676 | } |
| 677 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 678 | /* Write out a tar header for the specified file/directory/whatever */ |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 679 | static int |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 680 | writeTarHeader(struct TarHeader *header, const char *fileName, struct stat *statbuf) |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 681 | { |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 682 | //int i; |
| 683 | //long chksum, sum; |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 684 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 685 | if (*fileName=='/') { |
| 686 | static int alreadyWarned=FALSE; |
| 687 | if (alreadyWarned==FALSE) { |
| 688 | errorMsg("tar: Removing leading '/' from member names\n"); |
| 689 | alreadyWarned=TRUE; |
| 690 | } |
| 691 | strcpy(header->name, fileName+1); |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 692 | } |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 693 | else { |
| 694 | strcpy(header->name, fileName); |
| 695 | } |
| 696 | putOctal(header->mode, sizeof(header->mode), statbuf->st_mode & 0777); |
| 697 | putOctal(header->uid, sizeof(header->uid), statbuf->st_uid); |
| 698 | putOctal(header->gid, sizeof(header->gid), statbuf->st_gid); |
| 699 | putOctal(header->size, sizeof(header->size), statbuf->st_size); |
| 700 | putOctal(header->mtime, sizeof(header->mtime), statbuf->st_mtime); |
| 701 | |
| 702 | if (S_ISLNK(statbuf->st_mode)) { |
| 703 | header->typeflag = LNKTYPE; |
| 704 | // TODO -- Handle SYMTYPE |
| 705 | } else if (S_ISDIR(statbuf->st_mode)) { |
| 706 | header->typeflag = DIRTYPE; |
| 707 | strncat(header->name, "/", sizeof(header->name)); |
| 708 | } else if (S_ISCHR(statbuf->st_mode)) { |
| 709 | header->typeflag = CHRTYPE; |
| 710 | putOctal(header->devmajor, sizeof(header->devmajor), MAJOR(statbuf->st_rdev)); |
| 711 | putOctal(header->devminor, sizeof(header->devminor), MINOR(statbuf->st_rdev)); |
| 712 | } else if (S_ISBLK(statbuf->st_mode)) { |
| 713 | header->typeflag = BLKTYPE; |
| 714 | putOctal(header->devmajor, sizeof(header->devmajor), MAJOR(statbuf->st_rdev)); |
| 715 | putOctal(header->devminor, sizeof(header->devminor), MINOR(statbuf->st_rdev)); |
| 716 | } else if (S_ISFIFO(statbuf->st_mode)) { |
| 717 | header->typeflag = FIFOTYPE; |
| 718 | } else if (S_ISLNK(statbuf->st_mode)) { |
| 719 | header->typeflag = LNKTYPE; |
| 720 | } else if (S_ISLNK(statbuf->st_mode)) { |
| 721 | header->typeflag = REGTYPE; |
| 722 | } else { |
| 723 | return ( FALSE); |
| 724 | } |
| 725 | return ( TRUE); |
| 726 | |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 727 | #if 0 |
| 728 | header->linkname = rawHeader->linkname; |
| 729 | header->devmajor = getOctal(rawHeader->devmajor, sizeof(rawHeader->devmajor)); |
| 730 | header->devminor = getOctal(rawHeader->devminor, sizeof(rawHeader->devminor)); |
| 731 | |
| 732 | /* Write out the checksum */ |
| 733 | chksum = getOctal(rawHeader->chksum, sizeof(rawHeader->chksum)); |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 734 | |
| 735 | return ( TRUE); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 736 | #endif |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 740 | static int writeFileToTarball(const char *fileName, struct stat *statbuf, void* userData) |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 741 | { |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 742 | int inputFileFd; |
| 743 | struct TarBallInfo *tbInfo = (struct TarBallInfo *)userData; |
| 744 | char header[sizeof(struct TarHeader)]; |
| 745 | |
| 746 | /* First open the file we want to archive, and make sure all is well */ |
| 747 | if ((inputFileFd = open(fileName, O_RDONLY)) < 0) { |
| 748 | errorMsg("tar: %s: Cannot open: %s\n", fileName, strerror(errno)); |
| 749 | return( TRUE); |
| 750 | } |
| 751 | |
| 752 | /* It is against the rules to archive a socket */ |
| 753 | if (S_ISSOCK(statbuf->st_mode)) { |
| 754 | errorMsg("tar: %s: socket ignored\n", fileName); |
| 755 | return( TRUE); |
| 756 | } |
| 757 | |
| 758 | /* It is a bad idea to store the archive we are in the process of creating, |
| 759 | * so check the device and inode to be sure that this particular file isn't |
| 760 | * the new tarball */ |
| 761 | if (tbInfo->statBuf.st_dev == statbuf->st_dev && |
| 762 | tbInfo->statBuf.st_ino == statbuf->st_ino) { |
| 763 | errorMsg("tar: %s: file is the archive; skipping\n", fileName); |
| 764 | return( TRUE); |
| 765 | } |
| 766 | |
| 767 | memset( header, 0, sizeof(struct TarHeader)); |
| 768 | if (writeTarHeader((struct TarHeader *)header, fileName, statbuf)==FALSE) { |
| 769 | dprintf(tbInfo->tarFd, "%s", header); |
| 770 | } |
| 771 | /* Now do the verbose thing (or not) */ |
| 772 | if (tbInfo->verboseFlag==TRUE) |
| 773 | fprintf(stdout, "%s\n", ((struct TarHeader *)header)->name); |
| 774 | |
| 775 | return( TRUE); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 778 | static int writeTarFile(const char* tarName, int tostdoutFlag, |
| 779 | int verboseFlag, int argc, char **argv) |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 780 | { |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 781 | int tarFd=-1; |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 782 | int errorFlag=FALSE; |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 783 | //int skipFileFlag=FALSE; |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 784 | struct TarBallInfo tbInfo; |
| 785 | tbInfo.verboseFlag = verboseFlag; |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 786 | |
| 787 | /* Make sure there is at least one file to tar up. */ |
| 788 | if (argc <= 0) |
| 789 | fatalError("tar: Cowardly refusing to create an empty archive\n"); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 790 | |
| 791 | /* Open the tar file for writing. */ |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 792 | if (tostdoutFlag == TRUE) |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 793 | tbInfo.tarFd = fileno(stdout); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 794 | else |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 795 | tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 796 | if (tbInfo.tarFd < 0) { |
| 797 | errorMsg( "tar: Error opening '%s': %s\n", tarName, strerror(errno)); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 798 | return ( FALSE); |
| 799 | } |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 800 | /* Store the stat info for the tarball's file, so |
| 801 | * can avoid including the tarball into itself.... */ |
| 802 | if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0) |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 803 | fatalError(io_error, tarName, strerror(errno)); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 804 | |
| 805 | /* Set the umask for this process so it doesn't |
| 806 | * screw up permission setting for us later. */ |
| 807 | umask(0); |
| 808 | |
| 809 | /* Read the directory/files and iterate over them one at a time */ |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 810 | while (argc-- > 0) { |
| 811 | if (recursiveAction(*argv++, TRUE, FALSE, FALSE, |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 812 | writeFileToTarball, writeFileToTarball, |
| 813 | (void*) &tbInfo) == FALSE) { |
| 814 | errorFlag = TRUE; |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 815 | } |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 816 | } |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 817 | /* Hang up the tools, close up shop, head home */ |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 818 | close(tarFd); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 819 | if (errorFlag == TRUE) { |
| 820 | errorMsg("tar: Error exit delayed from previous errors\n"); |
| 821 | return(FALSE); |
| 822 | } |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 823 | return( TRUE); |
| 824 | } |
| 825 | |
| 826 | |
| 827 | #endif |
| 828 | |