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 | /* |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 3 | * Mini tar implementation for busybox based on code taken from sash. |
| 4 | * |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 5 | * Copyright (c) 1999 by David I. Bell |
| 6 | * Permission is granted to use, distribute, or modify this source, |
| 7 | * provided that this copyright notice remains intact. |
| 8 | * |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 9 | * Permission to distribute this code under the GPL has been granted. |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 10 | * |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 11 | * Modified for busybox by Erik Andersen <andersee@debian.org> |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 12 | * Adjusted to grok stdin/stdout options. |
| 13 | * |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 14 | * Modified to handle device special files by Matt Porter |
| 15 | * <porter@debian.org> |
| 16 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 17 | * This program is free software; you can redistribute it and/or modify |
| 18 | * it under the terms of the GNU General Public License as published by |
| 19 | * the Free Software Foundation; either version 2 of the License, or |
| 20 | * (at your option) any later version. |
| 21 | * |
| 22 | * This program is distributed in the hope that it will be useful, |
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 25 | * General Public License for more details. |
| 26 | * |
| 27 | * You should have received a copy of the GNU General Public License |
| 28 | * along with this program; if not, write to the Free Software |
| 29 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 30 | * |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 31 | */ |
| 32 | |
| 33 | |
| 34 | #include "internal.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 35 | #include <stdio.h> |
| 36 | #include <dirent.h> |
| 37 | #include <errno.h> |
| 38 | #include <fcntl.h> |
| 39 | #include <signal.h> |
| 40 | #include <time.h> |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 41 | #include <utime.h> |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 42 | #include <sys/types.h> |
Eric Andersen | 08b1034 | 1999-11-19 02:38:58 +0000 | [diff] [blame] | 43 | #include <sys/sysmacros.h> |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 44 | #include <sys/param.h> /* for PATH_MAX */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 45 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 46 | |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 47 | #ifdef BB_FEATURE_TAR_CREATE |
| 48 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 49 | static const char tar_usage[] = |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 50 | "tar -[cxtvOf] [tarFileName] [FILE] ...\n\n" |
| 51 | "Create, extract, or list files from a tar file.\n\n" |
| 52 | "Options:\n" |
| 53 | |
| 54 | "\tc=create, x=extract, t=list contents, v=verbose,\n" |
| 55 | "\tO=extract to stdout, f=tarfile or \"-\" for stdin\n"; |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 56 | |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 57 | #else |
| 58 | |
| 59 | static const char tar_usage[] = |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 60 | "tar -[xtvOf] [tarFileName] [FILE] ...\n\n" |
| 61 | "Extract, or list files stored in a tar file. This\n" |
| 62 | "version of tar does not support creation of tar files.\n\n" |
| 63 | "Options:\n" |
| 64 | |
| 65 | "\tx=extract, t=list contents, v=verbose,\n" |
| 66 | "\tO=extract to stdout, f=tarfile or \"-\" for stdin\n"; |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 67 | |
| 68 | #endif |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 69 | |
| 70 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 71 | /* Tar file constants */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 72 | |
| 73 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 74 | /* POSIX tar Header Block, from POSIX 1003.1-1990 */ |
| 75 | struct TarHeader |
| 76 | { |
| 77 | /* byte offset */ |
| 78 | char name[100]; /* 0 */ |
| 79 | char mode[8]; /* 100 */ |
| 80 | char uid[8]; /* 108 */ |
| 81 | char gid[8]; /* 116 */ |
| 82 | char size[12]; /* 124 */ |
| 83 | char mtime[12]; /* 136 */ |
| 84 | char chksum[8]; /* 148 */ |
| 85 | char typeflag; /* 156 */ |
| 86 | char linkname[100]; /* 157 */ |
| 87 | char magic[6]; /* 257 */ |
| 88 | char version[2]; /* 263 */ |
| 89 | char uname[32]; /* 265 */ |
| 90 | char gname[32]; /* 297 */ |
| 91 | char devmajor[8]; /* 329 */ |
| 92 | char devminor[8]; /* 337 */ |
| 93 | char prefix[155]; /* 345 */ |
| 94 | /* padding 500 */ |
| 95 | }; |
| 96 | typedef struct TarHeader TarHeader; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 97 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 98 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 99 | /* A few useful constants */ |
| 100 | #define TAR_MAGIC "ustar" /* ustar and a null */ |
| 101 | #define TAR_VERSION "00" /* 00 and no null */ |
| 102 | #define TAR_MAGIC_LEN 6 |
| 103 | #define TAR_VERSION_LEN 2 |
| 104 | #define TAR_NAME_LEN 100 |
| 105 | #define TAR_BLOCK_SIZE 512 |
| 106 | |
| 107 | /* A nice enum with all the possible tar file content types */ |
| 108 | enum TarFileType |
| 109 | { |
| 110 | REGTYPE = '0', /* regular file */ |
| 111 | REGTYPE0 = '\0', /* regular file (ancient bug compat)*/ |
| 112 | LNKTYPE = '1', /* hard link */ |
| 113 | SYMTYPE = '2', /* symbolic link */ |
| 114 | CHRTYPE = '3', /* character special */ |
| 115 | BLKTYPE = '4', /* block special */ |
| 116 | DIRTYPE = '5', /* directory */ |
| 117 | FIFOTYPE = '6', /* FIFO special */ |
| 118 | CONTTYPE = '7', /* reserved */ |
| 119 | }; |
| 120 | typedef enum TarFileType TarFileType; |
| 121 | |
| 122 | /* This struct ignores magic, non-numeric user name, |
| 123 | * non-numeric group name, and the checksum, since |
| 124 | * these are all ignored by BusyBox tar. */ |
| 125 | struct TarInfo |
| 126 | { |
| 127 | int tarFd; /* An open file descriptor for reading from the tarball */ |
| 128 | char * name; /* File name */ |
| 129 | mode_t mode; /* Unix mode, including device bits. */ |
| 130 | uid_t uid; /* Numeric UID */ |
| 131 | gid_t gid; /* Numeric GID */ |
| 132 | size_t size; /* Size of file */ |
| 133 | time_t mtime; /* Last-modified time */ |
| 134 | enum TarFileType type; /* Regular, directory, link, etc */ |
| 135 | char * linkname; /* Name for symbolic and hard links */ |
| 136 | dev_t device; /* Special device for mknod() */ |
| 137 | }; |
| 138 | typedef struct TarInfo TarInfo; |
| 139 | |
| 140 | /* Static data */ |
| 141 | static const unsigned long TarChecksumOffset = (const unsigned long)&(((TarHeader *)0)->chksum); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 142 | |
| 143 | |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame^] | 144 | /* Local procedures to restore files from a tar file. */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 145 | static int readTarFile(const char* tarName, int extractFlag, int listFlag, |
| 146 | int tostdoutFlag, int verboseFlag); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 147 | static long getOctal(const char *cp, int len); |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 148 | static int parseTarHeader(struct TarHeader *rawHeader, struct TarInfo *header); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 149 | |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 150 | #ifdef BB_FEATURE_TAR_CREATE |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 151 | /* |
| 152 | * Local procedures to save files into a tar file. |
| 153 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 154 | static void saveFile(const char *fileName, int seeLinks); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 155 | static void saveRegularFile(const char *fileName, |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 156 | const struct stat *statbuf); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 157 | static void saveDirectory(const char *fileName, |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 158 | const struct stat *statbuf); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 159 | static void writeHeader(const char *fileName, const struct stat *statbuf); |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 160 | static void writeTarFile(int argc, char **argv); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 161 | static void writeTarBlock(const char *buf, int len); |
| 162 | static int putOctal(char *cp, int len, long value); |
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 | #endif |
| 165 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 166 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 167 | extern int tar_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 168 | { |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 169 | const char *tarName=NULL; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 170 | const char *options; |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 171 | int listFlag = FALSE; |
| 172 | int extractFlag = FALSE; |
| 173 | int createFlag = FALSE; |
| 174 | int verboseFlag = FALSE; |
| 175 | int tostdoutFlag = FALSE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 176 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 177 | argc--; |
| 178 | argv++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 179 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 180 | if (argc < 1) |
| 181 | usage(tar_usage); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 182 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 183 | /* Parse options */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 184 | if (**argv == '-') |
| 185 | options = (*argv++) + 1; |
| 186 | else |
| 187 | options = (*argv++); |
| 188 | argc--; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 189 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 190 | for (; *options; options++) { |
| 191 | switch (*options) { |
| 192 | case 'f': |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 193 | if (tarName != NULL) |
| 194 | fatalError( "Only one 'f' option allowed\n"); |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 195 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 196 | tarName = *argv++; |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 197 | if (tarName == NULL) |
| 198 | fatalError( "Option requires an argument: No file specified\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 199 | argc--; |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 200 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 201 | break; |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 202 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 203 | case 't': |
| 204 | if (extractFlag == TRUE || createFlag == TRUE) |
| 205 | goto flagError; |
| 206 | listFlag = TRUE; |
| 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 'x': |
| 210 | if (listFlag == TRUE || createFlag == TRUE) |
| 211 | goto flagError; |
| 212 | extractFlag = TRUE; |
| 213 | break; |
| 214 | case 'c': |
| 215 | if (extractFlag == TRUE || listFlag == TRUE) |
| 216 | goto flagError; |
| 217 | createFlag = TRUE; |
| 218 | break; |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 219 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 220 | case 'v': |
| 221 | verboseFlag = TRUE; |
| 222 | break; |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 223 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 224 | case 'O': |
| 225 | tostdoutFlag = TRUE; |
| 226 | break; |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 227 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 228 | case '-': |
| 229 | usage(tar_usage); |
| 230 | break; |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 231 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 232 | default: |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 233 | fatalError( "Unknown tar flag '%c'\n" |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 234 | "Try `tar --help' for more information\n", *options); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 235 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 238 | /* |
| 239 | * Do the correct type of action supplying the rest of the |
| 240 | * command line arguments as the list of files to process. |
| 241 | */ |
| 242 | if (createFlag == TRUE) { |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 243 | #ifndef BB_FEATURE_TAR_CREATE |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 244 | 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] | 245 | #else |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 246 | exit(writeTarFile(argc, argv)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 247 | #endif |
| 248 | } else { |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 249 | exit(readTarFile(tarName, extractFlag, listFlag, tostdoutFlag, verboseFlag)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 250 | } |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 251 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 252 | flagError: |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 253 | fatalError( "Exactly one of 'c', 'x' or 't' must be specified\n"); |
| 254 | } |
| 255 | |
| 256 | static void |
| 257 | tarExtractRegularFile(TarInfo *header, int extractFlag, int listFlag, int tostdoutFlag, int verboseFlag) |
| 258 | { |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame^] | 259 | return; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 260 | } |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame^] | 261 | |
| 262 | static void |
| 263 | tarExtractDirectory(TarInfo *header, int extractFlag, int listFlag, int tostdoutFlag, int verboseFlag) |
| 264 | { |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | static void |
| 269 | tarExtractHardLink(TarInfo *header, int extractFlag, int listFlag, int tostdoutFlag, int verboseFlag) |
| 270 | { |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | static void |
| 275 | tarExtractSymLink(TarInfo *header, int extractFlag, int listFlag, int tostdoutFlag, int verboseFlag) |
| 276 | { |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | static void |
| 281 | tarExtractSpecial(TarInfo *header, int extractFlag, int listFlag, int tostdoutFlag, int verboseFlag) |
| 282 | { |
| 283 | return; |
| 284 | } |
| 285 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 286 | |
| 287 | |
| 288 | /* |
| 289 | * Read a tar file and extract or list the specified files within it. |
| 290 | * If the list is empty than all files are extracted or listed. |
| 291 | */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 292 | static int readTarFile(const char* tarName, int extractFlag, int listFlag, |
| 293 | int tostdoutFlag, int verboseFlag) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 294 | { |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 295 | int status, tarFd=0; |
| 296 | int errorFlag=FALSE; |
| 297 | TarHeader rawHeader; |
| 298 | TarInfo header; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 299 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 300 | /* Open the tar file for reading. */ |
| 301 | if (!strcmp(tarName, "-")) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 302 | tarFd = fileno(stdin); |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 303 | else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 304 | tarFd = open(tarName, O_RDONLY); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 305 | if (tarFd < 0) { |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 306 | errorMsg( "Error opening '%s': %s", tarName, strerror(errno)); |
| 307 | return ( FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 310 | /* Read the tar file */ |
| 311 | while ( (status = fullRead(tarFd, (char*)&rawHeader, TAR_BLOCK_SIZE)) == TAR_BLOCK_SIZE ) { |
| 312 | /* Now see if the header looks ok */ |
| 313 | if ( parseTarHeader(&rawHeader, &header) == FALSE ) { |
| 314 | close( tarFd); |
| 315 | if ( *(header.name) == '\0' ) { |
| 316 | goto endgame; |
| 317 | } else { |
| 318 | errorFlag=TRUE; |
| 319 | errorMsg("Bad tar header, skipping\n"); |
| 320 | continue; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 321 | } |
| 322 | } |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 323 | if ( *(header.name) == '\0' ) |
| 324 | goto endgame; |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame^] | 325 | |
| 326 | if (extractFlag == FALSE) { |
| 327 | if (verboseFlag == TRUE) { |
| 328 | printf("%s %3d/%-d ", modeString(header.mode), header.uid, header.gid); |
| 329 | if (header.type==CHRTYPE || header.type==BLKTYPE) |
| 330 | printf("%4d,%4d %s ", MAJOR(header.device), |
| 331 | MINOR(header.device), timeString(header.mtime)); |
| 332 | else |
| 333 | printf("%9ld %s ", header.size, timeString(header.mtime)); |
| 334 | } |
| 335 | printf("%s", header.name); |
| 336 | |
| 337 | if (header.type==LNKTYPE) |
| 338 | printf(" (link to \"%s\")", hp->linkName); |
| 339 | else if (header.type==SYMTYPE) |
| 340 | printf(" (symlink to \"%s\")", hp->linkName); |
| 341 | printf("\n"); |
| 342 | continue; |
| 343 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 344 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 345 | /* If we got here, we can be certain we have a legitimate |
| 346 | * header to work with. So work with it. */ |
| 347 | switch ( header.type ) { |
| 348 | case REGTYPE: |
| 349 | case REGTYPE0: |
| 350 | /* If the name ends in a '/' then assume it is |
| 351 | * supposed to be a directory, and fall through */ |
| 352 | if (header.name[strlen(header.name)-1] != '/') { |
| 353 | tarExtractRegularFile(&header, extractFlag, listFlag, tostdoutFlag, verboseFlag); |
| 354 | break; |
| 355 | } |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame^] | 356 | case DIRTYPE: |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 357 | tarExtractDirectory( &header, extractFlag, listFlag, tostdoutFlag, verboseFlag); |
| 358 | break; |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame^] | 359 | case LNKTYPE: |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 360 | tarExtractHardLink( &header, extractFlag, listFlag, tostdoutFlag, verboseFlag); |
| 361 | break; |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame^] | 362 | case SYMTYPE: |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 363 | tarExtractSymLink( &header, extractFlag, listFlag, tostdoutFlag, verboseFlag); |
| 364 | break; |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame^] | 365 | case CHRTYPE: |
| 366 | case BLKTYPE: |
| 367 | case FIFOTYPE: |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 368 | tarExtractSpecial( &header, extractFlag, listFlag, tostdoutFlag, verboseFlag); |
| 369 | break; |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 370 | default: |
| 371 | close( tarFd); |
| 372 | return( FALSE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 373 | } |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 374 | } |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 375 | |
| 376 | close(tarFd); |
| 377 | if (status > 0) { |
| 378 | /* Bummer - we read a partial header */ |
| 379 | errorMsg( "Error reading '%s': %s", tarName, strerror(errno)); |
| 380 | return ( FALSE); |
| 381 | } |
| 382 | else |
| 383 | return( status); |
| 384 | |
| 385 | /* Stuff we do when we know we are done with the file */ |
| 386 | endgame: |
| 387 | close( tarFd); |
| 388 | if ( *(header.name) == '\0' ) { |
| 389 | if (errorFlag==FALSE) |
| 390 | return( TRUE); |
| 391 | } |
| 392 | return( FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 395 | /* |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 396 | * Read an octal value in a field of the specified width, with optional |
| 397 | * spaces on both sides of the number and with an optional null character |
| 398 | * at the end. Returns -1 on an illegal format. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 399 | */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 400 | static long getOctal(const char *cp, int size) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 401 | { |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 402 | long val = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 403 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 404 | for(;(size > 0) && (*cp == ' '); cp++, size--); |
| 405 | if ((size == 0) || !isOctal(*cp)) |
| 406 | return -1; |
| 407 | for(; (size > 0) && isOctal(*cp); size--) { |
| 408 | val = val * 8 + *cp++ - '0'; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 409 | } |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 410 | for (;(size > 0) && (*cp == ' '); cp++, size--); |
| 411 | if ((size > 0) && *cp) |
| 412 | return -1; |
| 413 | return val; |
| 414 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 415 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 416 | /* Parse the tar header and fill in the nice struct with the details */ |
| 417 | static int |
| 418 | parseTarHeader(struct TarHeader *rawHeader, struct TarInfo *header) |
| 419 | { |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame^] | 420 | int i; |
| 421 | long chksum, sum; |
| 422 | unsigned char *s = (unsigned char *)rawHeader; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 423 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 424 | header->name = rawHeader->name; |
| 425 | header->mode = getOctal(rawHeader->mode, sizeof(rawHeader->mode)); |
| 426 | header->uid = getOctal(rawHeader->uid, sizeof(rawHeader->uid)); |
| 427 | header->gid = getOctal(rawHeader->gid, sizeof(rawHeader->gid)); |
| 428 | header->size = getOctal(rawHeader->size, sizeof(rawHeader->size)); |
| 429 | header->mtime = getOctal(rawHeader->mtime, sizeof(rawHeader->mtime)); |
| 430 | chksum = getOctal(rawHeader->chksum, sizeof(rawHeader->chksum)); |
| 431 | header->type = rawHeader->typeflag; |
| 432 | header->linkname = rawHeader->linkname; |
| 433 | header->device = MAJOR(getOctal(rawHeader->devmajor, sizeof(rawHeader->devmajor))) | |
| 434 | MINOR(getOctal(rawHeader->devminor, sizeof(rawHeader->devminor))); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 435 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 436 | /* Check the checksum */ |
| 437 | sum = ' ' * sizeof(rawHeader->chksum); |
| 438 | for ( i = TarChecksumOffset; i > 0; i-- ) |
| 439 | sum += *s++; |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame^] | 440 | s += sizeof(rawHeader->chksum); |
| 441 | for ( i = (512 - TarChecksumOffset - sizeof(rawHeader->chksum)); i > 0; i-- ) |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 442 | sum += *s++; |
Erik Andersen | e454fb6 | 2000-03-23 04:27:58 +0000 | [diff] [blame^] | 443 | if (sum == chksum ) |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 444 | return ( TRUE); |
| 445 | return( FALSE); |
| 446 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 447 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 448 | #if 0 |
| 449 | if ((header->mode < 0) || (header->uid < 0) || |
| 450 | (header->gid < 0) || (header->size < 0)) { |
| 451 | errorMsg(stderr, "Bad tar header, skipping\n"); |
| 452 | return( FALSE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | badHeader = FALSE; |
| 456 | skipFileFlag = FALSE; |
| 457 | devFileFlag = FALSE; |
| 458 | |
| 459 | /* |
| 460 | * Check for the file modes. |
| 461 | */ |
| 462 | hardLink = ((hp->typeFlag == TAR_TYPE_HARD_LINK) || |
| 463 | (hp->typeFlag == TAR_TYPE_HARD_LINK - '0')); |
| 464 | |
| 465 | softLink = ((hp->typeFlag == TAR_TYPE_SOFT_LINK) || |
| 466 | (hp->typeFlag == TAR_TYPE_SOFT_LINK - '0')); |
| 467 | |
| 468 | /* |
| 469 | * Check for a directory. |
| 470 | */ |
| 471 | if (outName[strlen(outName) - 1] == '/') |
| 472 | mode |= S_IFDIR; |
| 473 | |
| 474 | /* |
| 475 | * Check for absolute paths in the file. |
| 476 | * If we find any, then warn the user and make them relative. |
| 477 | */ |
| 478 | if (*outName == '/') { |
| 479 | while (*outName == '/') |
| 480 | outName++; |
| 481 | |
| 482 | if (warnedRoot == FALSE) { |
| 483 | fprintf(stderr, |
| 484 | "Absolute path detected, removing leading slashes\n"); |
| 485 | } |
| 486 | |
| 487 | warnedRoot = TRUE; |
| 488 | } |
| 489 | |
| 490 | /* |
| 491 | * See if we want this file to be restored. |
| 492 | * If not, then set up to skip it. |
| 493 | */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 494 | if (wantFileName(outName, argc, argv) == FALSE) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 495 | if (!hardLink && !softLink && (S_ISREG(mode) || S_ISCHR(mode) |
| 496 | || S_ISBLK(mode) || S_ISSOCK(mode) |
| 497 | || S_ISFIFO(mode))) { |
| 498 | inHeader = (size == 0) ? TRUE : FALSE; |
| 499 | dataCc = size; |
| 500 | } |
| 501 | |
| 502 | skipFileFlag = TRUE; |
| 503 | |
| 504 | return; |
| 505 | } |
| 506 | |
| 507 | /* |
| 508 | * This file is to be handled. |
| 509 | * If we aren't extracting then just list information about the file. |
| 510 | */ |
| 511 | if (extractFlag == FALSE) { |
| 512 | if (verboseFlag == TRUE) { |
| 513 | printf("%s %3d/%-d ", modeString(mode), uid, gid); |
| 514 | if (S_ISCHR(mode) || S_ISBLK(mode)) |
| 515 | printf("%4d,%4d %s ", major, minor, timeString(mtime)); |
| 516 | else |
| 517 | printf("%9ld %s ", size, timeString(mtime)); |
| 518 | } |
| 519 | printf("%s", outName); |
| 520 | |
| 521 | if (hardLink) |
| 522 | printf(" (link to \"%s\")", hp->linkName); |
| 523 | else if (softLink) |
| 524 | printf(" (symlink to \"%s\")", hp->linkName); |
| 525 | else if (S_ISREG(mode) || S_ISCHR(mode) || S_ISBLK(mode) || |
| 526 | S_ISSOCK(mode) || S_ISFIFO(mode)) { |
| 527 | inHeader = (size == 0) ? TRUE : FALSE; |
| 528 | dataCc = size; |
| 529 | } |
| 530 | |
| 531 | printf("\n"); |
| 532 | |
| 533 | return; |
| 534 | } |
| 535 | |
| 536 | /* |
| 537 | * We really want to extract the file. |
| 538 | */ |
| 539 | if (verboseFlag == TRUE) |
| 540 | printf("x %s\n", outName); |
| 541 | |
| 542 | if (hardLink) { |
| 543 | if (link(hp->linkName, outName) < 0) { |
| 544 | perror(outName); |
| 545 | return; |
| 546 | } |
| 547 | /* Set the file time */ |
| 548 | utb.actime = mtime; |
| 549 | utb.modtime = mtime; |
| 550 | utime(outName, &utb); |
| 551 | /* Set the file permissions */ |
| 552 | chown(outName, uid, gid); |
| 553 | chmod(outName, mode); |
| 554 | return; |
| 555 | } |
| 556 | |
| 557 | if (softLink) { |
| 558 | #ifdef S_ISLNK |
| 559 | if (symlink(hp->linkName, outName) < 0) { |
| 560 | perror(outName); |
| 561 | return; |
| 562 | } |
| 563 | /* Try to change ownership of the symlink. |
| 564 | * If libs doesn't support that, don't bother. |
| 565 | * Changing the pointed-to file is the Wrong Thing(tm). |
| 566 | */ |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 567 | #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 568 | lchown(outName, uid, gid); |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 569 | #endif |
| 570 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 571 | /* Do not change permissions or date on symlink, |
| 572 | * since it changes the pointed to file instead. duh. */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 573 | #else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 574 | fprintf(stderr, "Cannot create symbolic links\n"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 575 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 576 | return; |
Erik Andersen | 06936df | 2000-01-23 02:14:20 +0000 | [diff] [blame] | 577 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 578 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 579 | /* Set the umask for this process so it doesn't |
| 580 | * screw things up. */ |
| 581 | umask(0); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 582 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 583 | /* |
| 584 | * If the file is a directory, then just create the path. |
| 585 | */ |
| 586 | if (S_ISDIR(mode)) { |
| 587 | if (createPath(outName, mode) == TRUE) { |
Erik Andersen | e272915 | 2000-02-18 21:34:17 +0000 | [diff] [blame] | 588 | /* make the final component, just in case it was |
| 589 | * omitted by createPath() (which will skip the |
| 590 | * directory if it doesn't have a terminating '/') |
| 591 | */ |
| 592 | mkdir(outName, mode); |
| 593 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 594 | /* Set the file time */ |
| 595 | utb.actime = mtime; |
| 596 | utb.modtime = mtime; |
| 597 | utime(outName, &utb); |
| 598 | /* Set the file permissions */ |
| 599 | chown(outName, uid, gid); |
| 600 | chmod(outName, mode); |
| 601 | return; |
| 602 | } |
| 603 | return; |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 604 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 605 | |
| 606 | /* |
| 607 | * There is a file to write. |
| 608 | * First create the path to it if necessary with default permissions. |
| 609 | */ |
| 610 | createPath(outName, 0777); |
| 611 | |
| 612 | inHeader = (size == 0) ? TRUE : FALSE; |
| 613 | dataCc = size; |
| 614 | |
| 615 | /* |
| 616 | * Start the output file. |
| 617 | */ |
| 618 | if (tostdoutFlag == TRUE) |
| 619 | outFd = fileno(stdout); |
| 620 | else { |
| 621 | if (S_ISCHR(mode) || S_ISBLK(mode) || S_ISSOCK(mode)) { |
| 622 | devFileFlag = TRUE; |
| 623 | outFd = mknod(outName, mode, makedev(major, minor)); |
| 624 | } else if (S_ISFIFO(mode)) { |
| 625 | devFileFlag = TRUE; |
| 626 | outFd = mkfifo(outName, mode); |
| 627 | } else { |
| 628 | outFd = open(outName, O_WRONLY | O_CREAT | O_TRUNC, mode); |
| 629 | } |
| 630 | if (outFd < 0) { |
| 631 | perror(outName); |
| 632 | skipFileFlag = TRUE; |
| 633 | return; |
| 634 | } |
| 635 | /* Set the file time */ |
| 636 | utb.actime = mtime; |
| 637 | utb.modtime = mtime; |
| 638 | utime(outName, &utb); |
| 639 | /* Set the file permissions */ |
| 640 | chown(outName, uid, gid); |
| 641 | chmod(outName, mode); |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 642 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 643 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 644 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 645 | /* |
| 646 | * If the file is empty, then that's all we need to do. |
| 647 | */ |
| 648 | if (size == 0 && (tostdoutFlag == FALSE) && (devFileFlag == FALSE)) { |
| 649 | close(outFd); |
| 650 | outFd = -1; |
| 651 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | |
| 655 | /* |
| 656 | * Handle a data block of some specified size that was read. |
| 657 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 658 | static void readData(const char *cp, int count) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 659 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 660 | /* |
| 661 | * Reduce the amount of data left in this file. |
| 662 | * If there is no more data left, then we need to read |
| 663 | * the header again. |
| 664 | */ |
| 665 | dataCc -= count; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 666 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 667 | if (dataCc <= 0) |
| 668 | inHeader = TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 669 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 670 | /* |
| 671 | * If we aren't extracting files or this file is being |
| 672 | * skipped then do nothing more. |
| 673 | */ |
| 674 | if (extractFlag == FALSE || skipFileFlag == TRUE) |
| 675 | return; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 676 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 677 | /* |
| 678 | * Write the data to the output file. |
| 679 | */ |
| 680 | if (fullWrite(outFd, cp, count) < 0) { |
| 681 | perror(outName); |
| 682 | if (tostdoutFlag == FALSE) { |
| 683 | close(outFd); |
| 684 | outFd = -1; |
| 685 | } |
| 686 | skipFileFlag = TRUE; |
| 687 | return; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 690 | /* |
| 691 | * Check if we are done writing to the file now. |
| 692 | */ |
| 693 | if (dataCc <= 0 && tostdoutFlag == FALSE) { |
| 694 | struct utimbuf utb; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 695 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 696 | if (close(outFd)) |
| 697 | perror(outName); |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 698 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 699 | /* Set the file time */ |
| 700 | utb.actime = mtime; |
| 701 | utb.modtime = mtime; |
| 702 | utime(outName, &utb); |
| 703 | /* Set the file permissions */ |
| 704 | chown(outName, uid, gid); |
| 705 | chmod(outName, mode); |
| 706 | |
| 707 | outFd = -1; |
| 708 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | |
| 712 | /* |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 713 | * See if the specified file name belongs to one of the specified list |
| 714 | * of path prefixes. An empty list implies that all files are wanted. |
| 715 | * Returns TRUE if the file is selected. |
| 716 | */ |
| 717 | static int |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 718 | wantFileName(const char *fileName, int argc, char **argv) |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 719 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 720 | const char *pathName; |
| 721 | int fileLength; |
| 722 | int pathLength; |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 723 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 724 | /* |
| 725 | * If there are no files in the list, then the file is wanted. |
| 726 | */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 727 | if (argc == 0) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 728 | return TRUE; |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 729 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 730 | fileLength = strlen(fileName); |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 731 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 732 | /* |
| 733 | * Check each of the test paths. |
| 734 | */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 735 | while (argc-- > 0) { |
| 736 | pathName = *argv++; |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 737 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 738 | pathLength = strlen(pathName); |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 739 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 740 | if (fileLength < pathLength) |
| 741 | continue; |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 742 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 743 | if (memcmp(fileName, pathName, pathLength) != 0) |
| 744 | continue; |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 745 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 746 | if ((fileLength == pathLength) || (fileName[pathLength] == '/')) { |
| 747 | return TRUE; |
| 748 | } |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 749 | } |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 750 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 751 | return FALSE; |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 754 | |
| 755 | /* From here to the end of the file is the tar writing stuff. |
| 756 | * If you do not have BB_FEATURE_TAR_CREATE defined, this will |
| 757 | * not be built. |
| 758 | * */ |
| 759 | #ifdef BB_FEATURE_TAR_CREATE |
| 760 | |
| 761 | /* |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 762 | * Write a tar file containing the specified files. |
| 763 | */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 764 | static void writeTarFile(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 765 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 766 | struct stat statbuf; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 767 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 768 | /* |
| 769 | * Make sure there is at least one file specified. |
| 770 | */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 771 | if (argc <= 0) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 772 | fprintf(stderr, "No files specified to be saved\n"); |
| 773 | errorFlag = TRUE; |
| 774 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 775 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 776 | /* |
| 777 | * Create the tar file for writing. |
| 778 | */ |
| 779 | if ((tarName == NULL) || !strcmp(tarName, "-")) { |
| 780 | tostdoutFlag = TRUE; |
| 781 | tarFd = fileno(stdout); |
| 782 | } else |
| 783 | tarFd = open(tarName, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 784 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 785 | if (tarFd < 0) { |
| 786 | perror(tarName); |
| 787 | errorFlag = TRUE; |
| 788 | return; |
| 789 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 790 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 791 | /* |
| 792 | * Get the device and inode of the tar file for checking later. |
| 793 | */ |
| 794 | if (fstat(tarFd, &statbuf) < 0) { |
| 795 | perror(tarName); |
| 796 | errorFlag = TRUE; |
| 797 | goto done; |
| 798 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 799 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 800 | tarDev = statbuf.st_dev; |
| 801 | tarInode = statbuf.st_ino; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 802 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 803 | /* |
| 804 | * Append each file name into the archive file. |
| 805 | * Follow symbolic links for these top level file names. |
| 806 | */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 807 | while (errorFlag == FALSE && (argc-- > 0)) { |
| 808 | saveFile(*argv++, FALSE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | /* |
| 812 | * Now write an empty block of zeroes to end the archive. |
| 813 | */ |
| 814 | writeTarBlock("", 1); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 815 | |
| 816 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 817 | done: |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 818 | /* |
| 819 | * Close the tar file and check for errors if it was opened. |
| 820 | */ |
| 821 | if ((tostdoutFlag == FALSE) && (tarFd >= 0) && (close(tarFd) < 0)) |
| 822 | perror(tarName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 825 | /* |
| 826 | * Save one file into the tar file. |
| 827 | * If the file is a directory, then this will recursively save all of |
| 828 | * the files and directories within the directory. The seeLinks |
| 829 | * flag indicates whether or not we want to see symbolic links as |
| 830 | * they really are, instead of blindly following them. |
| 831 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 832 | static void saveFile(const char *fileName, int seeLinks) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 833 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 834 | int status; |
| 835 | struct stat statbuf; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 836 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 837 | if (verboseFlag == TRUE) |
| 838 | printf("a %s\n", fileName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 839 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 840 | /* |
| 841 | * Check that the file name will fit in the header. |
| 842 | */ |
| 843 | if (strlen(fileName) >= TAR_NAME_SIZE) { |
| 844 | fprintf(stderr, "%s: File name is too long\n", fileName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 845 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 846 | return; |
| 847 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 848 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 849 | /* |
| 850 | * Find out about the file. |
| 851 | */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 852 | #ifdef S_ISLNK |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 853 | if (seeLinks == TRUE) |
| 854 | status = lstat(fileName, &statbuf); |
| 855 | else |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 856 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 857 | status = stat(fileName, &statbuf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 858 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 859 | if (status < 0) { |
| 860 | perror(fileName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 861 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 862 | return; |
| 863 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 864 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 865 | /* |
| 866 | * Make sure we aren't trying to save our file into itself. |
| 867 | */ |
| 868 | if ((statbuf.st_dev == tarDev) && (statbuf.st_ino == tarInode)) { |
| 869 | fprintf(stderr, "Skipping saving of archive file itself\n"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 870 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 871 | return; |
| 872 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 873 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 874 | /* |
| 875 | * Check the type of file. |
| 876 | */ |
| 877 | mode = statbuf.st_mode; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 878 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 879 | if (S_ISDIR(mode)) { |
| 880 | saveDirectory(fileName, &statbuf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 881 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 882 | return; |
| 883 | } |
| 884 | if (S_ISREG(mode)) { |
| 885 | saveRegularFile(fileName, &statbuf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 886 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 887 | return; |
| 888 | } |
| 889 | |
| 890 | /* Some day add support for tarring these up... but not today. :) */ |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 891 | // if (S_ISLNK(mode) || S_ISFIFO(mode) || S_ISBLK(mode) || S_ISCHR (mode) ) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 892 | // fprintf (stderr, "%s: This version of tar can't store this type of file\n", fileName); |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 893 | // } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 894 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 895 | /* |
| 896 | * The file is a strange type of file, ignore it. |
| 897 | */ |
| 898 | fprintf(stderr, "%s: not a directory or regular file\n", fileName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | |
| 902 | /* |
| 903 | * Save a regular file to the tar file. |
| 904 | */ |
| 905 | static void |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 906 | saveRegularFile(const char *fileName, const struct stat *statbuf) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 907 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 908 | int sawEof; |
| 909 | int fileFd; |
| 910 | int cc; |
| 911 | int dataCount; |
| 912 | long fullDataCount; |
| 913 | char data[TAR_BLOCK_SIZE * 16]; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 914 | |
| 915 | /* |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 916 | * Open the file for reading. |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 917 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 918 | fileFd = open(fileName, O_RDONLY); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 919 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 920 | if (fileFd < 0) { |
| 921 | perror(fileName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 922 | |
| 923 | return; |
| 924 | } |
| 925 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 926 | /* |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 927 | * Write out the header for the file. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 928 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 929 | writeHeader(fileName, statbuf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 930 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 931 | /* |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 932 | * Write the data blocks of the file. |
| 933 | * We must be careful to write the amount of data that the stat |
| 934 | * buffer indicated, even if the file has changed size. Otherwise |
| 935 | * the tar file will be incorrect. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 936 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 937 | fullDataCount = statbuf->st_size; |
| 938 | sawEof = FALSE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 939 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 940 | while (fullDataCount > 0) { |
| 941 | /* |
| 942 | * Get the amount to write this iteration which is |
| 943 | * the minumum of the amount left to write and the |
| 944 | * buffer size. |
| 945 | */ |
| 946 | dataCount = sizeof(data); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 947 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 948 | if (dataCount > fullDataCount) |
| 949 | dataCount = (int) fullDataCount; |
| 950 | |
| 951 | /* |
| 952 | * Read the data from the file if we haven't seen the |
| 953 | * end of file yet. |
| 954 | */ |
| 955 | cc = 0; |
| 956 | |
| 957 | if (sawEof == FALSE) { |
| 958 | cc = fullRead(fileFd, data, dataCount); |
| 959 | |
| 960 | if (cc < 0) { |
| 961 | perror(fileName); |
| 962 | |
| 963 | (void) close(fileFd); |
| 964 | errorFlag = TRUE; |
| 965 | |
| 966 | return; |
| 967 | } |
| 968 | |
| 969 | /* |
| 970 | * If the file ended too soon, complain and set |
| 971 | * a flag so we will zero fill the rest of it. |
| 972 | */ |
| 973 | if (cc < dataCount) { |
| 974 | fprintf(stderr, "%s: Short read - zero filling", fileName); |
| 975 | |
| 976 | sawEof = TRUE; |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | /* |
| 981 | * Zero fill the rest of the data if necessary. |
| 982 | */ |
| 983 | if (cc < dataCount) |
| 984 | memset(data + cc, 0, dataCount - cc); |
| 985 | |
| 986 | /* |
| 987 | * Write the buffer to the TAR file. |
| 988 | */ |
| 989 | writeTarBlock(data, dataCount); |
| 990 | |
| 991 | fullDataCount -= dataCount; |
| 992 | } |
| 993 | |
| 994 | /* |
| 995 | * Close the file. |
| 996 | */ |
| 997 | if ((tostdoutFlag == FALSE) && close(fileFd) < 0) |
| 998 | fprintf(stderr, "%s: close: %s\n", fileName, strerror(errno)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | |
| 1002 | /* |
| 1003 | * Save a directory and all of its files to the tar file. |
| 1004 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1005 | static void saveDirectory(const char *dirName, const struct stat *statbuf) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1006 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1007 | DIR *dir; |
| 1008 | struct dirent *entry; |
| 1009 | int needSlash; |
| 1010 | char fullName[PATH_MAX + 1]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1011 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1012 | /* |
| 1013 | * Construct the directory name as used in the tar file by appending |
| 1014 | * a slash character to it. |
| 1015 | */ |
| 1016 | strcpy(fullName, dirName); |
| 1017 | strcat(fullName, "/"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1018 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1019 | /* |
| 1020 | * Write out the header for the directory entry. |
| 1021 | */ |
| 1022 | writeHeader(fullName, statbuf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1023 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1024 | /* |
| 1025 | * Open the directory. |
| 1026 | */ |
| 1027 | dir = opendir(dirName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1028 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1029 | if (dir == NULL) { |
| 1030 | fprintf(stderr, "Cannot read directory \"%s\": %s\n", |
| 1031 | dirName, strerror(errno)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1032 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1033 | return; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1036 | /* |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1037 | * See if a slash is needed. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1038 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1039 | needSlash = (*dirName && (dirName[strlen(dirName) - 1] != '/')); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1040 | |
| 1041 | /* |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1042 | * Read all of the directory entries and check them, |
| 1043 | * except for the current and parent directory entries. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1044 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1045 | while (errorFlag == FALSE && ((entry = readdir(dir)) != NULL)) { |
| 1046 | if ((strcmp(entry->d_name, ".") == 0) || |
| 1047 | (strcmp(entry->d_name, "..") == 0)) { |
| 1048 | continue; |
| 1049 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1050 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1051 | /* |
| 1052 | * Build the full path name to the file. |
| 1053 | */ |
| 1054 | strcpy(fullName, dirName); |
| 1055 | |
| 1056 | if (needSlash) |
| 1057 | strcat(fullName, "/"); |
| 1058 | |
| 1059 | strcat(fullName, entry->d_name); |
| 1060 | |
| 1061 | /* |
| 1062 | * Write this file to the tar file, noticing whether or not |
| 1063 | * the file is a symbolic link. |
| 1064 | */ |
| 1065 | saveFile(fullName, TRUE); |
| 1066 | } |
| 1067 | |
| 1068 | /* |
| 1069 | * All done, close the directory. |
| 1070 | */ |
| 1071 | closedir(dir); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | |
| 1075 | /* |
| 1076 | * Write a tar header for the specified file name and status. |
| 1077 | * It is assumed that the file name fits. |
| 1078 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1079 | static void writeHeader(const char *fileName, const struct stat *statbuf) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1080 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1081 | long checkSum; |
| 1082 | const unsigned char *cp; |
| 1083 | int len; |
| 1084 | TarHeader header; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1085 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1086 | /* |
| 1087 | * Zero the header block in preparation for filling it in. |
| 1088 | */ |
| 1089 | memset((char *) &header, 0, sizeof(header)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1090 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1091 | /* |
| 1092 | * Fill in the header. |
| 1093 | */ |
| 1094 | strcpy(header.name, fileName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1095 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1096 | strncpy(header.magic, TAR_MAGIC, sizeof(header.magic)); |
| 1097 | strncpy(header.version, TAR_VERSION, sizeof(header.version)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1098 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1099 | putOctal(header.mode, sizeof(header.mode), statbuf->st_mode & 0777); |
| 1100 | putOctal(header.uid, sizeof(header.uid), statbuf->st_uid); |
| 1101 | putOctal(header.gid, sizeof(header.gid), statbuf->st_gid); |
| 1102 | putOctal(header.size, sizeof(header.size), statbuf->st_size); |
| 1103 | putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1104 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1105 | header.typeFlag = TAR_TYPE_REGULAR; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1106 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1107 | /* |
| 1108 | * Calculate and store the checksum. |
| 1109 | * This is the sum of all of the bytes of the header, |
| 1110 | * with the checksum field itself treated as blanks. |
| 1111 | */ |
| 1112 | memset(header.checkSum, ' ', sizeof(header.checkSum)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1113 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1114 | cp = (const unsigned char *) &header; |
| 1115 | len = sizeof(header); |
| 1116 | checkSum = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1117 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1118 | while (len-- > 0) |
| 1119 | checkSum += *cp++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1120 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1121 | putOctal(header.checkSum, sizeof(header.checkSum), checkSum); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1122 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1123 | /* |
| 1124 | * Write the tar header. |
| 1125 | */ |
| 1126 | writeTarBlock((const char *) &header, sizeof(header)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | |
| 1130 | /* |
| 1131 | * Write data to one or more blocks of the tar file. |
| 1132 | * The data is always padded out to a multiple of TAR_BLOCK_SIZE. |
| 1133 | * The errorFlag static variable is set on an error. |
| 1134 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1135 | static void writeTarBlock(const char *buf, int len) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1136 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1137 | int partialLength; |
| 1138 | int completeLength; |
| 1139 | char fullBlock[TAR_BLOCK_SIZE]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1140 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1141 | /* |
| 1142 | * If we had a write error before, then do nothing more. |
| 1143 | */ |
| 1144 | if (errorFlag == TRUE) |
| 1145 | return; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1146 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1147 | /* |
| 1148 | * Get the amount of complete and partial blocks. |
| 1149 | */ |
| 1150 | partialLength = len % TAR_BLOCK_SIZE; |
| 1151 | completeLength = len - partialLength; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1152 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1153 | /* |
| 1154 | * Write all of the complete blocks. |
| 1155 | */ |
| 1156 | if ((completeLength > 0) && !fullWrite(tarFd, buf, completeLength)) { |
| 1157 | perror(tarName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1158 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1159 | errorFlag = TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1160 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1161 | return; |
| 1162 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1163 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1164 | /* |
| 1165 | * If there are no partial blocks left, we are done. |
| 1166 | */ |
| 1167 | if (partialLength == 0) |
| 1168 | return; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1169 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1170 | /* |
| 1171 | * Copy the partial data into a complete block, and pad the rest |
| 1172 | * of it with zeroes. |
| 1173 | */ |
| 1174 | memcpy(fullBlock, buf + completeLength, partialLength); |
| 1175 | memset(fullBlock + partialLength, 0, TAR_BLOCK_SIZE - partialLength); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1176 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1177 | /* |
| 1178 | * Write the last complete block. |
| 1179 | */ |
| 1180 | if (!fullWrite(tarFd, fullBlock, TAR_BLOCK_SIZE)) { |
| 1181 | perror(tarName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1182 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1183 | errorFlag = TRUE; |
| 1184 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | |
| 1188 | /* |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1189 | * Put an octal string into the specified buffer. |
| 1190 | * The number is zero and space padded and possibly null padded. |
| 1191 | * Returns TRUE if successful. |
| 1192 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1193 | static int putOctal(char *cp, int len, long value) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1194 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1195 | int tempLength; |
| 1196 | char *tempString; |
| 1197 | char tempBuffer[32]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1198 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1199 | /* |
| 1200 | * Create a string of the specified length with an initial space, |
| 1201 | * leading zeroes and the octal number, and a trailing null. |
| 1202 | */ |
| 1203 | tempString = tempBuffer; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1204 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1205 | sprintf(tempString, " %0*lo", len - 2, value); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1206 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1207 | tempLength = strlen(tempString) + 1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1208 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1209 | /* |
| 1210 | * If the string is too large, suppress the leading space. |
| 1211 | */ |
| 1212 | if (tempLength > len) { |
| 1213 | tempLength--; |
| 1214 | tempString++; |
| 1215 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1216 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1217 | /* |
| 1218 | * If the string is still too large, suppress the trailing null. |
| 1219 | */ |
| 1220 | if (tempLength > len) |
| 1221 | tempLength--; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1222 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1223 | /* |
| 1224 | * If the string is still too large, fail. |
| 1225 | */ |
| 1226 | if (tempLength > len) |
| 1227 | return FALSE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1228 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1229 | /* |
| 1230 | * Copy the string to the field. |
| 1231 | */ |
| 1232 | memcpy(cp, tempString, len); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1233 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1234 | return TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1235 | } |
Erik Andersen | 05100cd | 2000-01-16 01:30:52 +0000 | [diff] [blame] | 1236 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1237 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1238 | /* END CODE */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 1239 | #endif |