Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 2 | /* |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 3 | * Mini tar implementation for busybox |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 4 | * |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 5 | * Note, that as of BusyBox-0.43, tar has been completely rewritten from the |
| 6 | * ground up. It still has remnents of the old code lying about, but it is |
| 7 | * very different now (i.e. cleaner, less global variables, etc) |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 8 | * |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 9 | * Copyright (C) 2000 by Lineo, inc. |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 10 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 11 | * |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 12 | * Based in part in the tar implementation in sash |
| 13 | * Copyright (c) 1999 by David I. Bell |
| 14 | * Permission is granted to use, distribute, or modify this source, |
| 15 | * provided that this copyright notice remains intact. |
| 16 | * Permission to distribute sash derived code under the GPL has been granted. |
| 17 | * |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 18 | * Based in part on the tar implementation from busybox-0.28 |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 19 | * Copyright (C) 1995 Bruce Perens |
| 20 | * This is free software under the GNU General Public License. |
| 21 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 22 | * This program is free software; you can redistribute it and/or modify |
| 23 | * it under the terms of the GNU General Public License as published by |
| 24 | * the Free Software Foundation; either version 2 of the License, or |
| 25 | * (at your option) any later version. |
| 26 | * |
| 27 | * This program is distributed in the hope that it will be useful, |
| 28 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 30 | * General Public License for more details. |
| 31 | * |
| 32 | * You should have received a copy of the GNU General Public License |
| 33 | * along with this program; if not, write to the Free Software |
| 34 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 35 | * |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 36 | */ |
| 37 | |
| 38 | |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 39 | #include "busybox.h" |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 40 | #define BB_DECLARE_EXTERN |
| 41 | #define bb_need_io_error |
Eric Andersen | 3c5ee9a | 2000-11-14 22:15:48 +0000 | [diff] [blame] | 42 | #define bb_need_name_longer_than_foo |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 43 | #include "messages.c" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 44 | #include <stdio.h> |
| 45 | #include <dirent.h> |
| 46 | #include <errno.h> |
| 47 | #include <fcntl.h> |
| 48 | #include <signal.h> |
| 49 | #include <time.h> |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 50 | #include <utime.h> |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 51 | #include <sys/types.h> |
Eric Andersen | 08b1034 | 1999-11-19 02:38:58 +0000 | [diff] [blame] | 52 | #include <sys/sysmacros.h> |
Matt Kraai | 43c8c38 | 2000-09-04 16:51:55 +0000 | [diff] [blame] | 53 | #include <getopt.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 54 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 55 | /* Tar file constants */ |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 56 | #ifndef MAJOR |
| 57 | #define MAJOR(dev) (((dev)>>8)&0xff) |
| 58 | #define MINOR(dev) ((dev)&0xff) |
| 59 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 60 | |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 61 | #define NAME_SIZE 100 |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 62 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 63 | /* POSIX tar Header Block, from POSIX 1003.1-1990 */ |
| 64 | struct TarHeader |
| 65 | { |
| 66 | /* byte offset */ |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 67 | char name[NAME_SIZE]; /* 0-99 */ |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 68 | char mode[8]; /* 100-107 */ |
| 69 | char uid[8]; /* 108-115 */ |
| 70 | char gid[8]; /* 116-123 */ |
| 71 | char size[12]; /* 124-135 */ |
| 72 | char mtime[12]; /* 136-147 */ |
| 73 | char chksum[8]; /* 148-155 */ |
| 74 | char typeflag; /* 156-156 */ |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 75 | char linkname[NAME_SIZE]; /* 157-256 */ |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 76 | char magic[6]; /* 257-262 */ |
| 77 | char version[2]; /* 263-264 */ |
| 78 | char uname[32]; /* 265-296 */ |
| 79 | char gname[32]; /* 297-328 */ |
| 80 | char devmajor[8]; /* 329-336 */ |
| 81 | char devminor[8]; /* 337-344 */ |
| 82 | char prefix[155]; /* 345-499 */ |
| 83 | char padding[12]; /* 500-512 (pad to exactly the TAR_BLOCK_SIZE) */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 84 | }; |
| 85 | typedef struct TarHeader TarHeader; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 86 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 87 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 88 | /* A few useful constants */ |
| 89 | #define TAR_MAGIC "ustar" /* ustar and a null */ |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 90 | #define TAR_VERSION " " /* Be compatable with GNU tar format */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 91 | #define TAR_MAGIC_LEN 6 |
| 92 | #define TAR_VERSION_LEN 2 |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 93 | #define TAR_BLOCK_SIZE 512 |
| 94 | |
| 95 | /* A nice enum with all the possible tar file content types */ |
| 96 | enum TarFileType |
| 97 | { |
| 98 | REGTYPE = '0', /* regular file */ |
| 99 | REGTYPE0 = '\0', /* regular file (ancient bug compat)*/ |
| 100 | LNKTYPE = '1', /* hard link */ |
| 101 | SYMTYPE = '2', /* symbolic link */ |
| 102 | CHRTYPE = '3', /* character special */ |
| 103 | BLKTYPE = '4', /* block special */ |
| 104 | DIRTYPE = '5', /* directory */ |
| 105 | FIFOTYPE = '6', /* FIFO special */ |
| 106 | CONTTYPE = '7', /* reserved */ |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 107 | GNULONGLINK = 'K', /* GNU long (>100 chars) link name */ |
| 108 | GNULONGNAME = 'L', /* GNU long (>100 chars) file name */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 109 | }; |
| 110 | typedef enum TarFileType TarFileType; |
| 111 | |
| 112 | /* This struct ignores magic, non-numeric user name, |
| 113 | * non-numeric group name, and the checksum, since |
| 114 | * these are all ignored by BusyBox tar. */ |
| 115 | struct TarInfo |
| 116 | { |
| 117 | int tarFd; /* An open file descriptor for reading from the tarball */ |
| 118 | char * name; /* File name */ |
| 119 | mode_t mode; /* Unix mode, including device bits. */ |
| 120 | uid_t uid; /* Numeric UID */ |
| 121 | gid_t gid; /* Numeric GID */ |
| 122 | size_t size; /* Size of file */ |
| 123 | time_t mtime; /* Last-modified time */ |
| 124 | enum TarFileType type; /* Regular, directory, link, etc */ |
| 125 | char * linkname; /* Name for symbolic and hard links */ |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 126 | long devmajor; /* Major number for special device */ |
| 127 | long devminor; /* Minor number for special device */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 128 | }; |
| 129 | typedef struct TarInfo TarInfo; |
| 130 | |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 131 | /* Local procedures to restore files from a tar file. */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 132 | static int readTarFile(const char* tarName, int extractFlag, int listFlag, |
Matt Kraai | b92223b | 2000-09-04 08:25:42 +0000 | [diff] [blame] | 133 | int tostdoutFlag, int verboseFlag, char** extractList, |
| 134 | char** excludeList); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 135 | |
| 136 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 137 | |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 138 | #ifdef BB_FEATURE_TAR_CREATE |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 139 | /* Local procedures to save files into a tar file. */ |
Matt Kraai | d8ad76c | 2000-11-08 02:35:47 +0000 | [diff] [blame] | 140 | static int writeTarFile(const char* tarName, int verboseFlag, char **argv, |
| 141 | char** excludeList); |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 142 | #endif |
| 143 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 144 | extern int tar_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 145 | { |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 146 | char** excludeList=NULL; |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 147 | #if defined BB_FEATURE_TAR_EXCLUDE |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 148 | int excludeListSize=0; |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 149 | #endif |
Eric Andersen | 108a443 | 2000-05-30 19:05:57 +0000 | [diff] [blame] | 150 | const char *tarName="-"; |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 151 | int listFlag = FALSE; |
| 152 | int extractFlag = FALSE; |
| 153 | int createFlag = FALSE; |
| 154 | int verboseFlag = FALSE; |
| 155 | int tostdoutFlag = FALSE; |
Eric Andersen | 0102a9f | 2000-09-23 22:36:24 +0000 | [diff] [blame] | 156 | int firstOpt = TRUE; |
Eric Andersen | 46a98df | 2000-09-19 21:35:14 +0000 | [diff] [blame] | 157 | int stopIt; |
| 158 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 159 | |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 160 | if (argc <= 1) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 161 | usage(tar_usage); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 162 | |
Matt Kraai | d8ad76c | 2000-11-08 02:35:47 +0000 | [diff] [blame] | 163 | while (*(++argv) && (**argv == '-' || firstOpt == TRUE)) { |
Eric Andersen | 0102a9f | 2000-09-23 22:36:24 +0000 | [diff] [blame] | 164 | firstOpt=FALSE; |
Eric Andersen | 46a98df | 2000-09-19 21:35:14 +0000 | [diff] [blame] | 165 | stopIt=FALSE; |
Matt Kraai | d8ad76c | 2000-11-08 02:35:47 +0000 | [diff] [blame] | 166 | while (stopIt==FALSE && **argv) { |
| 167 | switch (*((*argv)++)) { |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 168 | case 'c': |
| 169 | if (extractFlag == TRUE || listFlag == TRUE) |
| 170 | goto flagError; |
| 171 | createFlag = TRUE; |
| 172 | break; |
Eric Andersen | fdd5103 | 2000-08-02 18:48:26 +0000 | [diff] [blame] | 173 | case 'x': |
| 174 | if (listFlag == TRUE || createFlag == TRUE) |
| 175 | goto flagError; |
| 176 | extractFlag = TRUE; |
| 177 | break; |
| 178 | case 't': |
| 179 | if (extractFlag == TRUE || createFlag == TRUE) |
| 180 | goto flagError; |
| 181 | listFlag = TRUE; |
| 182 | break; |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 183 | case 'v': |
| 184 | verboseFlag = TRUE; |
| 185 | break; |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 186 | case 'O': |
| 187 | tostdoutFlag = TRUE; |
Eric Andersen | fdd5103 | 2000-08-02 18:48:26 +0000 | [diff] [blame] | 188 | break; |
| 189 | case 'f': |
| 190 | if (*tarName != '-') |
| 191 | fatalError( "Only one 'f' option allowed\n"); |
Eric Andersen | 46a98df | 2000-09-19 21:35:14 +0000 | [diff] [blame] | 192 | tarName = *(++argv); |
| 193 | if (tarName == NULL) |
| 194 | fatalError( "Option requires an argument: No file specified\n"); |
Eric Andersen | 46a98df | 2000-09-19 21:35:14 +0000 | [diff] [blame] | 195 | stopIt=TRUE; |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 196 | break; |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 197 | #if defined BB_FEATURE_TAR_EXCLUDE |
Matt Kraai | 43c8c38 | 2000-09-04 16:51:55 +0000 | [diff] [blame] | 198 | case 'e': |
Eric Andersen | 62f987e | 2000-10-09 17:54:38 +0000 | [diff] [blame] | 199 | if (strcmp(*argv, "exclude")==0) { |
Eric Andersen | 46a98df | 2000-09-19 21:35:14 +0000 | [diff] [blame] | 200 | excludeList=xrealloc( excludeList, sizeof(char**) * (excludeListSize+2)); |
| 201 | excludeList[excludeListSize] = *(++argv); |
Matt Kraai | d8ad76c | 2000-11-08 02:35:47 +0000 | [diff] [blame] | 202 | if (excludeList[excludeListSize] == NULL) |
| 203 | fatalError( "Option requires an argument: No file specified\n"); |
Eric Andersen | 46a98df | 2000-09-19 21:35:14 +0000 | [diff] [blame] | 204 | /* Remove leading "/"s */ |
| 205 | if (*excludeList[excludeListSize] =='/') |
| 206 | excludeList[excludeListSize] = (excludeList[excludeListSize])+1; |
| 207 | /* Tack a NULL onto the end of the list */ |
| 208 | excludeList[++excludeListSize] = NULL; |
| 209 | stopIt=TRUE; |
| 210 | break; |
| 211 | } |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 212 | #endif |
Eric Andersen | 46a98df | 2000-09-19 21:35:14 +0000 | [diff] [blame] | 213 | case '-': |
| 214 | break; |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 215 | default: |
Matt Kraai | 43c8c38 | 2000-09-04 16:51:55 +0000 | [diff] [blame] | 216 | usage(tar_usage); |
Eric Andersen | 46a98df | 2000-09-19 21:35:14 +0000 | [diff] [blame] | 217 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 218 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 221 | /* |
| 222 | * Do the correct type of action supplying the rest of the |
| 223 | * command line arguments as the list of files to process. |
| 224 | */ |
| 225 | if (createFlag == TRUE) { |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 226 | #ifndef BB_FEATURE_TAR_CREATE |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 227 | 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] | 228 | #else |
Matt Kraai | d8ad76c | 2000-11-08 02:35:47 +0000 | [diff] [blame] | 229 | exit(writeTarFile(tarName, verboseFlag, argv, excludeList)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 230 | #endif |
Erik Andersen | 8ea7d8c | 2000-05-20 00:40:08 +0000 | [diff] [blame] | 231 | } |
| 232 | if (listFlag == TRUE || extractFlag == TRUE) { |
Matt Kraai | d8ad76c | 2000-11-08 02:35:47 +0000 | [diff] [blame] | 233 | exit(readTarFile(tarName, extractFlag, listFlag, tostdoutFlag, |
| 234 | verboseFlag, argv, excludeList)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 235 | } |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 236 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 237 | flagError: |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 238 | fatalError( "Exactly one of 'c', 'x' or 't' must be specified\n"); |
| 239 | } |
| 240 | |
| 241 | static void |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 242 | fixUpPermissions(TarInfo *header) |
| 243 | { |
| 244 | struct utimbuf t; |
| 245 | /* Now set permissions etc for the new file */ |
| 246 | chown(header->name, header->uid, header->gid); |
| 247 | chmod(header->name, header->mode); |
| 248 | /* Reset the time */ |
| 249 | t.actime = time(0); |
| 250 | t.modtime = header->mtime; |
| 251 | utime(header->name, &t); |
| 252 | } |
| 253 | |
| 254 | static int |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 255 | tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag) |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 256 | { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 257 | size_t writeSize; |
| 258 | size_t readSize; |
| 259 | size_t actualWriteSz; |
| 260 | char buffer[BUFSIZ]; |
| 261 | size_t size = header->size; |
| 262 | int outFd=fileno(stdout); |
| 263 | |
| 264 | /* Open the file to be written, if a file is supposed to be written */ |
| 265 | if (extractFlag==TRUE && tostdoutFlag==FALSE) { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 266 | /* Create the path to the file, just in case it isn't there... |
| 267 | * This should not screw up path permissions or anything. */ |
| 268 | createPath(header->name, 0777); |
Eric Andersen | 0c6a970 | 2000-06-09 20:51:50 +0000 | [diff] [blame] | 269 | if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY, |
| 270 | header->mode & ~S_IFMT)) < 0) { |
| 271 | errorMsg(io_error, header->name, strerror(errno)); |
| 272 | return( FALSE); |
| 273 | } |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* Write out the file, if we are supposed to be doing that */ |
| 277 | while ( size > 0 ) { |
| 278 | actualWriteSz=0; |
| 279 | if ( size > sizeof(buffer) ) |
| 280 | writeSize = readSize = sizeof(buffer); |
| 281 | else { |
| 282 | int mod = size % 512; |
| 283 | if ( mod != 0 ) |
| 284 | readSize = size + (512 - mod); |
| 285 | else |
| 286 | readSize = size; |
| 287 | writeSize = size; |
| 288 | } |
| 289 | if ( (readSize = fullRead(header->tarFd, buffer, readSize)) <= 0 ) { |
| 290 | /* Tarball seems to have a problem */ |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 291 | errorMsg("Unexpected EOF in archive\n"); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 292 | return( FALSE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 293 | } |
| 294 | if ( readSize < writeSize ) |
| 295 | writeSize = readSize; |
| 296 | |
| 297 | /* Write out the file, if we are supposed to be doing that */ |
| 298 | if (extractFlag==TRUE) { |
| 299 | |
| 300 | if ((actualWriteSz=fullWrite(outFd, buffer, writeSize)) != writeSize ) { |
| 301 | /* Output file seems to have a problem */ |
| 302 | errorMsg(io_error, header->name, strerror(errno)); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 303 | return( FALSE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 304 | } |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 305 | } else { |
| 306 | actualWriteSz=writeSize; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | size -= actualWriteSz; |
| 310 | } |
| 311 | |
| 312 | /* Now we are done writing the file out, so try |
| 313 | * and fix up the permissions and whatnot */ |
| 314 | if (extractFlag==TRUE && tostdoutFlag==FALSE) { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 315 | close(outFd); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 316 | fixUpPermissions(header); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 317 | } |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 318 | return( TRUE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 321 | static int |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 322 | tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag) |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 323 | { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 324 | |
| 325 | if (extractFlag==FALSE || tostdoutFlag==TRUE) |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 326 | return( TRUE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 327 | |
| 328 | if (createPath(header->name, header->mode) != TRUE) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 329 | errorMsg("%s: Cannot mkdir: %s\n", |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 330 | header->name, strerror(errno)); |
| 331 | return( FALSE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 332 | } |
| 333 | /* make the final component, just in case it was |
| 334 | * omitted by createPath() (which will skip the |
| 335 | * directory if it doesn't have a terminating '/') */ |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 336 | if (mkdir(header->name, header->mode) == 0) { |
| 337 | fixUpPermissions(header); |
| 338 | } |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 339 | return( TRUE); |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 342 | static int |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 343 | tarExtractHardLink(TarInfo *header, int extractFlag, int tostdoutFlag) |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 344 | { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 345 | if (extractFlag==FALSE || tostdoutFlag==TRUE) |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 346 | return( TRUE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 347 | |
| 348 | if (link(header->linkname, header->name) < 0) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 349 | errorMsg("%s: Cannot create hard link to '%s': %s\n", |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 350 | header->name, header->linkname, strerror(errno)); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 351 | return( FALSE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /* Now set permissions etc for the new directory */ |
| 355 | fixUpPermissions(header); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 356 | return( TRUE); |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 359 | static int |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 360 | tarExtractSymLink(TarInfo *header, int extractFlag, int tostdoutFlag) |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 361 | { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 362 | if (extractFlag==FALSE || tostdoutFlag==TRUE) |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 363 | return( TRUE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 364 | |
| 365 | #ifdef S_ISLNK |
| 366 | if (symlink(header->linkname, header->name) < 0) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 367 | errorMsg("%s: Cannot create symlink to '%s': %s\n", |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 368 | header->name, header->linkname, strerror(errno)); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 369 | return( FALSE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 370 | } |
| 371 | /* Try to change ownership of the symlink. |
| 372 | * If libs doesn't support that, don't bother. |
| 373 | * Changing the pointed-to-file is the Wrong Thing(tm). |
| 374 | */ |
| 375 | #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) |
| 376 | lchown(header->name, header->uid, header->gid); |
| 377 | #endif |
| 378 | |
| 379 | /* Do not change permissions or date on symlink, |
| 380 | * since it changes the pointed to file instead. duh. */ |
| 381 | #else |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 382 | errorMsg("%s: Cannot create symlink to '%s': %s\n", |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 383 | header->name, header->linkname, |
| 384 | "symlinks not supported"); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 385 | #endif |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 386 | return( TRUE); |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 389 | static int |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 390 | tarExtractSpecial(TarInfo *header, int extractFlag, int tostdoutFlag) |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 391 | { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 392 | if (extractFlag==FALSE || tostdoutFlag==TRUE) |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 393 | return( TRUE); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 394 | |
| 395 | if (S_ISCHR(header->mode) || S_ISBLK(header->mode) || S_ISSOCK(header->mode)) { |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 396 | if (mknod(header->name, header->mode, makedev(header->devmajor, header->devminor)) < 0) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 397 | errorMsg("%s: Cannot mknod: %s\n", |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 398 | header->name, strerror(errno)); |
| 399 | return( FALSE); |
| 400 | } |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 401 | } else if (S_ISFIFO(header->mode)) { |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 402 | if (mkfifo(header->name, header->mode) < 0) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 403 | errorMsg("%s: Cannot mkfifo: %s\n", |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 404 | header->name, strerror(errno)); |
| 405 | return( FALSE); |
| 406 | } |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | /* Now set permissions etc for the new directory */ |
| 410 | fixUpPermissions(header); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 411 | return( TRUE); |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 414 | /* 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] | 415 | * 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] | 416 | * at the end. Returns -1 on an illegal format. */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 417 | static long getOctal(const char *cp, int size) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 418 | { |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 419 | long val = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 420 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 421 | for(;(size > 0) && (*cp == ' '); cp++, size--); |
| 422 | if ((size == 0) || !isOctal(*cp)) |
| 423 | return -1; |
| 424 | for(; (size > 0) && isOctal(*cp); size--) { |
| 425 | val = val * 8 + *cp++ - '0'; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 426 | } |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 427 | for (;(size > 0) && (*cp == ' '); cp++, size--); |
| 428 | if ((size > 0) && *cp) |
| 429 | return -1; |
| 430 | return val; |
| 431 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 432 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 433 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 434 | /* Parse the tar header and fill in the nice struct with the details */ |
| 435 | static int |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 436 | readTarHeader(struct TarHeader *rawHeader, struct TarInfo *header) |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 437 | { |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 438 | int i; |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 439 | long chksum, sum=0; |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 440 | unsigned char *s = (unsigned char *)rawHeader; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 441 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 442 | header->name = rawHeader->name; |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 443 | /* Check for and relativify any absolute paths */ |
| 444 | if ( *(header->name) == '/' ) { |
| 445 | static int alreadyWarned=FALSE; |
| 446 | |
| 447 | while (*(header->name) == '/') |
| 448 | ++*(header->name); |
| 449 | |
| 450 | if (alreadyWarned == FALSE) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 451 | errorMsg("Removing leading '/' from member names\n"); |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 452 | alreadyWarned = TRUE; |
| 453 | } |
| 454 | } |
| 455 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 456 | header->mode = getOctal(rawHeader->mode, sizeof(rawHeader->mode)); |
| 457 | header->uid = getOctal(rawHeader->uid, sizeof(rawHeader->uid)); |
| 458 | header->gid = getOctal(rawHeader->gid, sizeof(rawHeader->gid)); |
| 459 | header->size = getOctal(rawHeader->size, sizeof(rawHeader->size)); |
| 460 | header->mtime = getOctal(rawHeader->mtime, sizeof(rawHeader->mtime)); |
| 461 | chksum = getOctal(rawHeader->chksum, sizeof(rawHeader->chksum)); |
| 462 | header->type = rawHeader->typeflag; |
| 463 | header->linkname = rawHeader->linkname; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 464 | header->devmajor = getOctal(rawHeader->devmajor, sizeof(rawHeader->devmajor)); |
| 465 | header->devminor = getOctal(rawHeader->devminor, sizeof(rawHeader->devminor)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 466 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 467 | /* Check the checksum */ |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 468 | for (i = sizeof(*rawHeader); i-- != 0;) { |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 469 | sum += *s++; |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 470 | } |
| 471 | /* Remove the effects of the checksum field (replace |
| 472 | * with blanks for the purposes of the checksum) */ |
| 473 | s = rawHeader->chksum; |
| 474 | for (i = sizeof(rawHeader->chksum) ; i-- != 0;) { |
| 475 | sum -= *s++; |
| 476 | } |
| 477 | sum += ' ' * sizeof(rawHeader->chksum); |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame] | 478 | if (sum == chksum ) |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 479 | return ( TRUE); |
| 480 | return( FALSE); |
| 481 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 482 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 483 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 484 | /* |
| 485 | * Read a tar file and extract or list the specified files within it. |
| 486 | * If the list is empty than all files are extracted or listed. |
| 487 | */ |
| 488 | static int readTarFile(const char* tarName, int extractFlag, int listFlag, |
Matt Kraai | b92223b | 2000-09-04 08:25:42 +0000 | [diff] [blame] | 489 | int tostdoutFlag, int verboseFlag, char** extractList, |
| 490 | char** excludeList) |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 491 | { |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 492 | int status, tarFd=-1; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 493 | int errorFlag=FALSE; |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 494 | int skipNextHeaderFlag=FALSE; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 495 | TarHeader rawHeader; |
| 496 | TarInfo header; |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 497 | char** tmpList; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 498 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 499 | /* Open the tar file for reading. */ |
| 500 | if (!strcmp(tarName, "-")) |
| 501 | tarFd = fileno(stdin); |
| 502 | else |
| 503 | tarFd = open(tarName, O_RDONLY); |
| 504 | if (tarFd < 0) { |
Erik Andersen | deea048 | 2000-03-25 23:09:29 +0000 | [diff] [blame] | 505 | errorMsg( "Error opening '%s': %s\n", tarName, strerror(errno)); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 506 | return ( FALSE); |
Erik Andersen | 06936df | 2000-01-23 02:14:20 +0000 | [diff] [blame] | 507 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 508 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 509 | /* Set the umask for this process so it doesn't |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 510 | * screw up permission setting for us later. */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 511 | umask(0); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 512 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 513 | /* Read the tar file, and iterate over it one file at a time */ |
| 514 | while ( (status = fullRead(tarFd, (char*)&rawHeader, TAR_BLOCK_SIZE)) == TAR_BLOCK_SIZE ) { |
Erik Andersen | e272915 | 2000-02-18 21:34:17 +0000 | [diff] [blame] | 515 | |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 516 | /* Try to read the header */ |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 517 | if ( readTarHeader(&rawHeader, &header) == FALSE ) { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 518 | if ( *(header.name) == '\0' ) { |
| 519 | goto endgame; |
| 520 | } else { |
| 521 | errorFlag=TRUE; |
| 522 | errorMsg("Bad tar header, skipping\n"); |
| 523 | continue; |
| 524 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 525 | } |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 526 | if ( *(header.name) == '\0' ) |
| 527 | goto endgame; |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 528 | header.tarFd = tarFd; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 529 | |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 530 | /* Skip funky extra GNU headers that precede long files */ |
| 531 | if ( (header.type == GNULONGNAME) || (header.type == GNULONGLINK) ) { |
| 532 | skipNextHeaderFlag=TRUE; |
Matt Kraai | ab8f9e2 | 2000-11-18 01:28:57 +0000 | [diff] [blame] | 533 | if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE) |
| 534 | errorFlag = TRUE; |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 535 | continue; |
| 536 | } |
| 537 | if ( skipNextHeaderFlag == TRUE ) { |
| 538 | skipNextHeaderFlag=FALSE; |
Eric Andersen | 3c5ee9a | 2000-11-14 22:15:48 +0000 | [diff] [blame] | 539 | errorMsg(name_longer_than_foo, NAME_SIZE); |
Matt Kraai | ab8f9e2 | 2000-11-18 01:28:57 +0000 | [diff] [blame] | 540 | if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE) |
| 541 | errorFlag = TRUE; |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 542 | continue; |
| 543 | } |
| 544 | |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 545 | #if defined BB_FEATURE_TAR_EXCLUDE |
| 546 | { |
| 547 | int skipFlag=FALSE; |
| 548 | /* Check for excluded files.... */ |
| 549 | for (tmpList=excludeList; tmpList && *tmpList; tmpList++) { |
| 550 | /* Do some extra hoop jumping for when directory names |
| 551 | * end in '/' but the entry in tmpList doesn't */ |
| 552 | if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || ( |
| 553 | header.name[strlen(header.name)-1]=='/' |
| 554 | && strncmp( *tmpList, header.name, |
| 555 | MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) { |
| 556 | /* If it is a regular file, pretend to extract it with |
| 557 | * the extractFlag set to FALSE, so the junk in the tarball |
| 558 | * is properly skipped over */ |
| 559 | if ( header.type==REGTYPE || header.type==REGTYPE0 ) { |
Matt Kraai | ab8f9e2 | 2000-11-18 01:28:57 +0000 | [diff] [blame] | 560 | if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE) |
| 561 | errorFlag = TRUE; |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 562 | } |
| 563 | skipFlag=TRUE; |
| 564 | break; |
| 565 | } |
| 566 | } |
| 567 | /* There are not the droids you're looking for, move along */ |
| 568 | if (skipFlag==TRUE) |
| 569 | continue; |
| 570 | } |
| 571 | #endif |
Matt Kraai | b92223b | 2000-09-04 08:25:42 +0000 | [diff] [blame] | 572 | if (*extractList != NULL) { |
| 573 | int skipFlag = TRUE; |
| 574 | for (tmpList = extractList; *tmpList != NULL; tmpList++) { |
| 575 | if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || ( |
| 576 | header.name[strlen(header.name)-1]=='/' |
| 577 | && strncmp( *tmpList, header.name, |
| 578 | MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) { |
| 579 | /* If it is a regular file, pretend to extract it with |
| 580 | * the extractFlag set to FALSE, so the junk in the tarball |
| 581 | * is properly skipped over */ |
| 582 | skipFlag = FALSE; |
| 583 | break; |
| 584 | } |
| 585 | } |
| 586 | /* There are not the droids you're looking for, move along */ |
| 587 | if (skipFlag == TRUE) { |
| 588 | if ( header.type==REGTYPE || header.type==REGTYPE0 ) |
Matt Kraai | ab8f9e2 | 2000-11-18 01:28:57 +0000 | [diff] [blame] | 589 | if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE) |
| 590 | errorFlag = TRUE; |
Matt Kraai | b92223b | 2000-09-04 08:25:42 +0000 | [diff] [blame] | 591 | continue; |
| 592 | } |
| 593 | } |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 594 | |
Matt Kraai | 82cfbad | 2000-09-15 21:18:43 +0000 | [diff] [blame] | 595 | if (listFlag == TRUE) { |
Matt Kraai | 6fc2a7d | 2000-09-15 22:23:41 +0000 | [diff] [blame] | 596 | /* Special treatment if the list (-t) flag is on */ |
| 597 | if (verboseFlag == TRUE) { |
| 598 | int len, len1; |
| 599 | char buf[35]; |
| 600 | struct tm *tm = localtime (&(header.mtime)); |
| 601 | |
| 602 | len=printf("%s ", modeString(header.mode)); |
| 603 | memset(buf, 0, 8*sizeof(char)); |
| 604 | my_getpwuid(buf, header.uid); |
| 605 | if (! *buf) |
| 606 | len+=printf("%d", header.uid); |
| 607 | else |
| 608 | len+=printf("%s", buf); |
| 609 | memset(buf, 0, 8*sizeof(char)); |
| 610 | my_getgrgid(buf, header.gid); |
| 611 | if (! *buf) |
| 612 | len+=printf("/%-d ", header.gid); |
| 613 | else |
| 614 | len+=printf("/%-s ", buf); |
| 615 | |
| 616 | if (header.type==CHRTYPE || header.type==BLKTYPE) { |
| 617 | len1=snprintf(buf, sizeof(buf), "%ld,%-ld ", |
| 618 | header.devmajor, header.devminor); |
| 619 | } else { |
| 620 | len1=snprintf(buf, sizeof(buf), "%lu ", (long)header.size); |
| 621 | } |
| 622 | /* Jump through some hoops to make the columns match up */ |
| 623 | for(;(len+len1)<31;len++) |
| 624 | printf(" "); |
| 625 | printf(buf); |
| 626 | |
| 627 | /* Use ISO 8610 time format */ |
| 628 | if (tm) { |
| 629 | printf ("%04d-%02d-%02d %02d:%02d:%02d ", |
| 630 | tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, |
| 631 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 632 | } |
| 633 | } |
Eric Andersen | fdd5103 | 2000-08-02 18:48:26 +0000 | [diff] [blame] | 634 | printf("%s", header.name); |
Matt Kraai | 82cfbad | 2000-09-15 21:18:43 +0000 | [diff] [blame] | 635 | if (verboseFlag == TRUE) { |
| 636 | if (header.type==LNKTYPE) /* If this is a link, say so */ |
| 637 | printf(" link to %s", header.linkname); |
| 638 | else if (header.type==SYMTYPE) |
| 639 | printf(" -> %s", header.linkname); |
| 640 | } |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 641 | printf("\n"); |
| 642 | } |
| 643 | |
Matt Kraai | 6fc2a7d | 2000-09-15 22:23:41 +0000 | [diff] [blame] | 644 | /* List contents if we are supposed to do that */ |
| 645 | if (verboseFlag == TRUE && extractFlag == TRUE) { |
| 646 | /* Now the normal listing */ |
| 647 | FILE *vbFd = stdout; |
| 648 | if (tostdoutFlag == TRUE) // If the archive goes to stdout, verbose to stderr |
| 649 | vbFd = stderr; |
| 650 | fprintf(vbFd, "%s\n", header.name); |
| 651 | } |
| 652 | |
Matt Kraai | f446297 | 2000-09-01 02:50:48 +0000 | [diff] [blame] | 653 | /* Remove files if we would overwrite them */ |
Matt Kraai | da542f3 | 2000-09-01 02:53:01 +0000 | [diff] [blame] | 654 | if (extractFlag == TRUE && tostdoutFlag == FALSE) |
Matt Kraai | f446297 | 2000-09-01 02:50:48 +0000 | [diff] [blame] | 655 | unlink(header.name); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 656 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 657 | /* If we got here, we can be certain we have a legitimate |
| 658 | * header to work with. So work with it. */ |
| 659 | switch ( header.type ) { |
| 660 | case REGTYPE: |
| 661 | case REGTYPE0: |
| 662 | /* If the name ends in a '/' then assume it is |
| 663 | * supposed to be a directory, and fall through */ |
| 664 | if (header.name[strlen(header.name)-1] != '/') { |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 665 | if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE) |
| 666 | errorFlag=TRUE; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 667 | break; |
| 668 | } |
| 669 | case DIRTYPE: |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 670 | if (tarExtractDirectory( &header, extractFlag, tostdoutFlag)==FALSE) |
| 671 | errorFlag=TRUE; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 672 | break; |
| 673 | case LNKTYPE: |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 674 | if (tarExtractHardLink( &header, extractFlag, tostdoutFlag)==FALSE) |
| 675 | errorFlag=TRUE; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 676 | break; |
| 677 | case SYMTYPE: |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 678 | if (tarExtractSymLink( &header, extractFlag, tostdoutFlag)==FALSE) |
| 679 | errorFlag=TRUE; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 680 | break; |
| 681 | case CHRTYPE: |
| 682 | case BLKTYPE: |
| 683 | case FIFOTYPE: |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 684 | if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE) |
| 685 | errorFlag=TRUE; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 686 | break; |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 687 | #if 0 |
| 688 | /* Handled earlier */ |
| 689 | case GNULONGNAME: |
| 690 | case GNULONGLINK: |
| 691 | skipNextHeaderFlag=TRUE; |
| 692 | break; |
| 693 | #endif |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 694 | default: |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 695 | errorMsg("Unknown file type '%c' in tar file\n", header.type); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 696 | close( tarFd); |
| 697 | return( FALSE); |
| 698 | } |
| 699 | } |
| 700 | close(tarFd); |
| 701 | if (status > 0) { |
| 702 | /* Bummer - we read a partial header */ |
Erik Andersen | deea048 | 2000-03-25 23:09:29 +0000 | [diff] [blame] | 703 | errorMsg( "Error reading '%s': %s\n", tarName, strerror(errno)); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 704 | return ( FALSE); |
| 705 | } |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 706 | else if (errorFlag==TRUE) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 707 | errorMsg( "Error exit delayed from previous errors\n"); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 708 | return( FALSE); |
| 709 | } else |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 710 | return( status); |
| 711 | |
| 712 | /* Stuff to do when we are done */ |
| 713 | endgame: |
| 714 | close( tarFd); |
| 715 | if ( *(header.name) == '\0' ) { |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 716 | if (errorFlag==TRUE) |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 717 | errorMsg( "Error exit delayed from previous errors\n"); |
Erik Andersen | 6a34b53 | 2000-04-07 06:55:38 +0000 | [diff] [blame] | 718 | else |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 719 | return( TRUE); |
| 720 | } |
| 721 | return( FALSE); |
| 722 | } |
| 723 | |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 724 | |
| 725 | #ifdef BB_FEATURE_TAR_CREATE |
| 726 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 727 | /* Some info to be carried along when creating a new tarball */ |
| 728 | struct TarBallInfo |
| 729 | { |
| 730 | char* fileName; /* File name of the tarball */ |
| 731 | int tarFd; /* Open-for-write file descriptor |
| 732 | for the tarball */ |
| 733 | struct stat statBuf; /* Stat info for the tarball, letting |
| 734 | us know the inode and device that the |
| 735 | tarball lives, so we can avoid trying |
| 736 | to include the tarball into itself */ |
| 737 | int verboseFlag; /* Whether to print extra stuff or not */ |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 738 | char** excludeList; /* List of files to not include */ |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 739 | }; |
| 740 | typedef struct TarBallInfo TarBallInfo; |
| 741 | |
| 742 | |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 743 | /* Put an octal string into the specified buffer. |
| 744 | * The number is zero and space padded and possibly null padded. |
| 745 | * Returns TRUE if successful. */ |
| 746 | static int putOctal (char *cp, int len, long value) |
| 747 | { |
| 748 | int tempLength; |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 749 | char tempBuffer[32]; |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 750 | char *tempString = tempBuffer; |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 751 | |
| 752 | /* Create a string of the specified length with an initial space, |
| 753 | * leading zeroes and the octal number, and a trailing null. */ |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 754 | sprintf (tempString, "%0*lo", len - 1, value); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 755 | |
| 756 | /* If the string is too large, suppress the leading space. */ |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 757 | tempLength = strlen (tempString) + 1; |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 758 | if (tempLength > len) { |
| 759 | tempLength--; |
| 760 | tempString++; |
| 761 | } |
| 762 | |
| 763 | /* If the string is still too large, suppress the trailing null. */ |
| 764 | if (tempLength > len) |
| 765 | tempLength--; |
| 766 | |
| 767 | /* If the string is still too large, fail. */ |
| 768 | if (tempLength > len) |
| 769 | return FALSE; |
| 770 | |
| 771 | /* Copy the string to the field. */ |
| 772 | memcpy (cp, tempString, len); |
| 773 | |
| 774 | return TRUE; |
| 775 | } |
| 776 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 777 | /* Write out a tar header for the specified file/directory/whatever */ |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 778 | static int |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 779 | writeTarHeader(struct TarBallInfo *tbInfo, const char *fileName, struct stat *statbuf) |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 780 | { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 781 | long chksum=0; |
| 782 | struct TarHeader header; |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 783 | #if defined BB_FEATURE_TAR_EXCLUDE |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 784 | char** tmpList; |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 785 | #endif |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 786 | const unsigned char *cp = (const unsigned char *) &header; |
| 787 | ssize_t size = sizeof(struct TarHeader); |
Eric Andersen | fdd5103 | 2000-08-02 18:48:26 +0000 | [diff] [blame] | 788 | |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 789 | memset( &header, 0, size); |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 790 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 791 | if (*fileName=='/') { |
| 792 | static int alreadyWarned=FALSE; |
| 793 | if (alreadyWarned==FALSE) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 794 | errorMsg("Removing leading '/' from member names\n"); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 795 | alreadyWarned=TRUE; |
| 796 | } |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 797 | strncpy(header.name, fileName+1, sizeof(header.name)); |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 798 | } |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 799 | else { |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 800 | strncpy(header.name, fileName, sizeof(header.name)); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 801 | } |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 802 | |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 803 | #if defined BB_FEATURE_TAR_EXCLUDE |
| 804 | /* Check for excluded files.... */ |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 805 | for (tmpList=tbInfo->excludeList; tmpList && *tmpList; tmpList++) { |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 806 | /* Do some extra hoop jumping for when directory names |
| 807 | * end in '/' but the entry in tmpList doesn't */ |
| 808 | if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || ( |
| 809 | header.name[strlen(header.name)-1]=='/' |
| 810 | && strncmp( *tmpList, header.name, |
| 811 | MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) { |
| 812 | /* Set the mode to something that is not a regular file, thereby |
| 813 | * faking out writeTarFile into thinking that nothing further need |
| 814 | * be done for this file. Yes, I know this is ugly, but it works. */ |
| 815 | statbuf->st_mode = 0; |
| 816 | return( TRUE); |
| 817 | } |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 818 | } |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 819 | #endif |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 820 | |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 821 | putOctal(header.mode, sizeof(header.mode), statbuf->st_mode); |
| 822 | putOctal(header.uid, sizeof(header.uid), statbuf->st_uid); |
| 823 | putOctal(header.gid, sizeof(header.gid), statbuf->st_gid); |
| 824 | putOctal(header.size, sizeof(header.size), 0); /* Regular file size is handled later */ |
| 825 | putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime); |
| 826 | strncpy(header.magic, TAR_MAGIC TAR_VERSION, |
| 827 | TAR_MAGIC_LEN + TAR_VERSION_LEN ); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 828 | |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 829 | /* Enter the user and group names (default to root if it fails) */ |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 830 | my_getpwuid(header.uname, statbuf->st_uid); |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 831 | if (! *header.uname) |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 832 | strcpy(header.uname, "root"); |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 833 | my_getgrgid(header.gname, statbuf->st_gid); |
| 834 | if (! *header.uname) |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 835 | strcpy(header.uname, "root"); |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 836 | |
Erik Andersen | 95c1c1e | 2000-04-14 21:45:29 +0000 | [diff] [blame] | 837 | /* WARNING/NOTICE: I break Hard Links */ |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 838 | if (S_ISLNK(statbuf->st_mode)) { |
Eric Andersen | 3adffb7 | 2000-06-26 10:54:06 +0000 | [diff] [blame] | 839 | int link_size=0; |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 840 | char buffer[BUFSIZ]; |
| 841 | header.typeflag = SYMTYPE; |
Eric Andersen | 3adffb7 | 2000-06-26 10:54:06 +0000 | [diff] [blame] | 842 | link_size = readlink(fileName, buffer, sizeof(buffer) - 1); |
| 843 | if ( link_size < 0) { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 844 | errorMsg("Error reading symlink '%s': %s\n", header.name, strerror(errno)); |
| 845 | return ( FALSE); |
| 846 | } |
Eric Andersen | 3adffb7 | 2000-06-26 10:54:06 +0000 | [diff] [blame] | 847 | buffer[link_size] = '\0'; |
Erik Andersen | 95c1c1e | 2000-04-14 21:45:29 +0000 | [diff] [blame] | 848 | strncpy(header.linkname, buffer, sizeof(header.linkname)); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 849 | } else if (S_ISDIR(statbuf->st_mode)) { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 850 | header.typeflag = DIRTYPE; |
| 851 | strncat(header.name, "/", sizeof(header.name)); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 852 | } else if (S_ISCHR(statbuf->st_mode)) { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 853 | header.typeflag = CHRTYPE; |
| 854 | putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev)); |
| 855 | putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev)); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 856 | } else if (S_ISBLK(statbuf->st_mode)) { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 857 | header.typeflag = BLKTYPE; |
| 858 | putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev)); |
| 859 | putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev)); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 860 | } else if (S_ISFIFO(statbuf->st_mode)) { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 861 | header.typeflag = FIFOTYPE; |
| 862 | } else if (S_ISREG(statbuf->st_mode)) { |
| 863 | header.typeflag = REGTYPE; |
| 864 | putOctal(header.size, sizeof(header.size), statbuf->st_size); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 865 | } else { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 866 | errorMsg("%s: Unknown file type\n", fileName); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 867 | return ( FALSE); |
| 868 | } |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 869 | |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 870 | /* Calculate and store the checksum (i.e. the sum of all of the bytes of |
| 871 | * the header). The checksum field must be filled with blanks for the |
| 872 | * calculation. The checksum field is formatted differently from the |
| 873 | * other fields: it has [6] digits, a null, then a space -- rather than |
| 874 | * digits, followed by a null like the other fields... */ |
| 875 | memset(header.chksum, ' ', sizeof(header.chksum)); |
| 876 | cp = (const unsigned char *) &header; |
| 877 | while (size-- > 0) |
| 878 | chksum += *cp++; |
| 879 | putOctal(header.chksum, 7, chksum); |
| 880 | |
| 881 | /* Now write the header out to disk */ |
| 882 | if ((size=fullWrite(tbInfo->tarFd, (char*)&header, sizeof(struct TarHeader))) < 0) { |
| 883 | errorMsg(io_error, fileName, strerror(errno)); |
| 884 | return ( FALSE); |
| 885 | } |
| 886 | /* Pad the header up to the tar block size */ |
| 887 | for (; size<TAR_BLOCK_SIZE; size++) { |
| 888 | write(tbInfo->tarFd, "\0", 1); |
| 889 | } |
| 890 | /* Now do the verbose thing (or not) */ |
Eric Andersen | fdd5103 | 2000-08-02 18:48:26 +0000 | [diff] [blame] | 891 | if (tbInfo->verboseFlag==TRUE) { |
| 892 | FILE *vbFd = stdout; |
| 893 | if (tbInfo->tarFd == fileno(stdout)) // If the archive goes to stdout, verbose to stderr |
| 894 | vbFd = stderr; |
| 895 | fprintf(vbFd, "%s\n", header.name); |
| 896 | } |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 897 | |
| 898 | return ( TRUE); |
| 899 | } |
| 900 | |
| 901 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 902 | static int writeFileToTarball(const char *fileName, struct stat *statbuf, void* userData) |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 903 | { |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 904 | struct TarBallInfo *tbInfo = (struct TarBallInfo *)userData; |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 905 | |
| 906 | /* It is against the rules to archive a socket */ |
| 907 | if (S_ISSOCK(statbuf->st_mode)) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 908 | errorMsg("%s: socket ignored\n", fileName); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 909 | return( TRUE); |
| 910 | } |
| 911 | |
| 912 | /* It is a bad idea to store the archive we are in the process of creating, |
| 913 | * so check the device and inode to be sure that this particular file isn't |
| 914 | * the new tarball */ |
| 915 | if (tbInfo->statBuf.st_dev == statbuf->st_dev && |
| 916 | tbInfo->statBuf.st_ino == statbuf->st_ino) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 917 | errorMsg("%s: file is the archive; skipping\n", fileName); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 918 | return( TRUE); |
| 919 | } |
| 920 | |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 921 | if (strlen(fileName) >= NAME_SIZE) { |
Eric Andersen | 3c5ee9a | 2000-11-14 22:15:48 +0000 | [diff] [blame] | 922 | errorMsg(name_longer_than_foo, NAME_SIZE); |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 923 | return ( TRUE); |
| 924 | } |
| 925 | |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 926 | if (writeTarHeader(tbInfo, fileName, statbuf)==FALSE) { |
| 927 | return( FALSE); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 928 | } |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 929 | |
| 930 | /* Now, if the file is a regular file, copy it out to the tarball */ |
| 931 | if (S_ISREG(statbuf->st_mode)) { |
| 932 | int inputFileFd; |
| 933 | char buffer[BUFSIZ]; |
| 934 | ssize_t size=0, readSize=0; |
| 935 | |
| 936 | /* open the file we want to archive, and make sure all is well */ |
| 937 | if ((inputFileFd = open(fileName, O_RDONLY)) < 0) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 938 | errorMsg("%s: Cannot open: %s\n", fileName, strerror(errno)); |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 939 | return( FALSE); |
| 940 | } |
| 941 | |
| 942 | /* write the file to the archive */ |
| 943 | while ( (size = fullRead(inputFileFd, buffer, sizeof(buffer))) > 0 ) { |
| 944 | if (fullWrite(tbInfo->tarFd, buffer, size) != size ) { |
| 945 | /* Output file seems to have a problem */ |
| 946 | errorMsg(io_error, fileName, strerror(errno)); |
| 947 | return( FALSE); |
| 948 | } |
| 949 | readSize+=size; |
| 950 | } |
| 951 | if (size == -1) { |
| 952 | errorMsg(io_error, fileName, strerror(errno)); |
| 953 | return( FALSE); |
| 954 | } |
| 955 | /* Pad the file up to the tar block size */ |
| 956 | for (; (readSize%TAR_BLOCK_SIZE) != 0; readSize++) { |
| 957 | write(tbInfo->tarFd, "\0", 1); |
| 958 | } |
| 959 | close( inputFileFd); |
| 960 | } |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 961 | |
| 962 | return( TRUE); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Matt Kraai | d8ad76c | 2000-11-08 02:35:47 +0000 | [diff] [blame] | 965 | static int writeTarFile(const char* tarName, int verboseFlag, char **argv, |
| 966 | char** excludeList) |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 967 | { |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 968 | int tarFd=-1; |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 969 | int errorFlag=FALSE; |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 970 | ssize_t size; |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 971 | struct TarBallInfo tbInfo; |
| 972 | tbInfo.verboseFlag = verboseFlag; |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 973 | |
| 974 | /* Make sure there is at least one file to tar up. */ |
Matt Kraai | d8ad76c | 2000-11-08 02:35:47 +0000 | [diff] [blame] | 975 | if (*argv == NULL) |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 976 | fatalError("Cowardly refusing to create an empty archive\n"); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 977 | |
| 978 | /* Open the tar file for writing. */ |
Matt Kraai | d8ad76c | 2000-11-08 02:35:47 +0000 | [diff] [blame] | 979 | if (!strcmp(tarName, "-")) |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 980 | tbInfo.tarFd = fileno(stdout); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 981 | else |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 982 | tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 983 | if (tbInfo.tarFd < 0) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 984 | errorMsg( "Error opening '%s': %s\n", tarName, strerror(errno)); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 985 | return ( FALSE); |
| 986 | } |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 987 | tbInfo.excludeList=excludeList; |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 988 | /* Store the stat info for the tarball's file, so |
| 989 | * can avoid including the tarball into itself.... */ |
| 990 | if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0) |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 991 | fatalError(io_error, tarName, strerror(errno)); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 992 | |
| 993 | /* Set the umask for this process so it doesn't |
| 994 | * screw up permission setting for us later. */ |
| 995 | umask(0); |
| 996 | |
| 997 | /* Read the directory/files and iterate over them one at a time */ |
Matt Kraai | d8ad76c | 2000-11-08 02:35:47 +0000 | [diff] [blame] | 998 | while (*argv != NULL) { |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 999 | if (recursiveAction(*argv++, TRUE, FALSE, FALSE, |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 1000 | writeFileToTarball, writeFileToTarball, |
| 1001 | (void*) &tbInfo) == FALSE) { |
| 1002 | errorFlag = TRUE; |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 1003 | } |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 1004 | } |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 1005 | /* Write two empty blocks to the end of the archive */ |
| 1006 | for (size=0; size<(2*TAR_BLOCK_SIZE); size++) { |
| 1007 | write(tbInfo.tarFd, "\0", 1); |
| 1008 | } |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 1009 | |
| 1010 | /* To be pedantically correct, we would check if the tarball |
Eric Andersen | 3c5ee9a | 2000-11-14 22:15:48 +0000 | [diff] [blame] | 1011 | * is smaller than 20 tar blocks, and pad it if it was smaller, |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 1012 | * but that isn't necessary for GNU tar interoperability, and |
| 1013 | * so is considered a waste of space */ |
| 1014 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 1015 | /* Hang up the tools, close up shop, head home */ |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 1016 | close(tarFd); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 1017 | if (errorFlag == TRUE) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 1018 | errorMsg("Error exit delayed from previous errors\n"); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 1019 | return(FALSE); |
| 1020 | } |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 1021 | return( TRUE); |
| 1022 | } |
| 1023 | |
| 1024 | |
| 1025 | #endif |
| 1026 | |