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