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