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