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 | * |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 5 | * Modifed to use common extraction code used by ar, cpio, dpkg-deb, dpkg |
| 6 | * Glenn McGrath <bug1@optushome.com.au> |
| 7 | * |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 8 | * Note, that as of BusyBox-0.43, tar has been completely rewritten from the |
| 9 | * ground up. It still has remnents of the old code lying about, but it is |
Eric Andersen | 77d9268 | 2001-05-23 20:32:09 +0000 | [diff] [blame] | 10 | * very different now (i.e., cleaner, less global variables, etc.) |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 11 | * |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 12 | * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen |
Eric Andersen | 1d1d2f9 | 2002-04-13 08:31:59 +0000 | [diff] [blame] | 13 | * Copyright (C) 1999-2002 by Erik Andersen <andersee@debian.org> |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 14 | * |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 15 | * Based in part in the tar implementation in sash |
| 16 | * Copyright (c) 1999 by David I. Bell |
| 17 | * Permission is granted to use, distribute, or modify this source, |
| 18 | * provided that this copyright notice remains intact. |
| 19 | * Permission to distribute sash derived code under the GPL has been granted. |
| 20 | * |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 21 | * Based in part on the tar implementation from busybox-0.28 |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 22 | * Copyright (C) 1995 Bruce Perens |
| 23 | * This is free software under the GNU General Public License. |
| 24 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 25 | * This program is free software; you can redistribute it and/or modify |
| 26 | * it under the terms of the GNU General Public License as published by |
| 27 | * the Free Software Foundation; either version 2 of the License, or |
| 28 | * (at your option) any later version. |
| 29 | * |
| 30 | * This program is distributed in the hope that it will be useful, |
| 31 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 32 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 33 | * General Public License for more details. |
| 34 | * |
| 35 | * You should have received a copy of the GNU General Public License |
| 36 | * along with this program; if not, write to the Free Software |
| 37 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 38 | * |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 39 | */ |
| 40 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 41 | #include <fcntl.h> |
Matt Kraai | 43c8c38 | 2000-09-04 16:51:55 +0000 | [diff] [blame] | 42 | #include <getopt.h> |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 43 | #include <search.h> |
| 44 | #include <stdio.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 45 | #include <stdlib.h> |
| 46 | #include <unistd.h> |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 47 | #include <fnmatch.h> |
| 48 | #include <string.h> |
| 49 | #include <errno.h> |
Robert Griebl | f2f26e7 | 2002-07-23 22:05:47 +0000 | [diff] [blame] | 50 | #include <signal.h> |
| 51 | #include <sys/wait.h> |
| 52 | #include <sys/socket.h> |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 53 | #include "unarchive.h" |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 54 | #include "busybox.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 55 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 56 | #ifdef CONFIG_FEATURE_TAR_CREATE |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 57 | |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 58 | /* Tar file constants */ |
| 59 | # define TAR_MAGIC "ustar" /* ustar and a null */ |
| 60 | # define TAR_VERSION " " /* Be compatable with GNU tar format */ |
| 61 | |
| 62 | # ifndef MAJOR |
| 63 | # define MAJOR(dev) (((dev)>>8)&0xff) |
| 64 | # define MINOR(dev) ((dev)&0xff) |
| 65 | # endif |
| 66 | |
| 67 | static const int TAR_BLOCK_SIZE = 512; |
| 68 | static const int TAR_MAGIC_LEN = 6; |
| 69 | static const int TAR_VERSION_LEN = 2; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 70 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 71 | /* POSIX tar Header Block, from POSIX 1003.1-1990 */ |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 72 | enum { NAME_SIZE = 100 }; /* because gcc won't let me use 'static const int' */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 73 | struct TarHeader |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 74 | { /* byte offset */ |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 75 | char name[NAME_SIZE]; /* 0-99 */ |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 76 | char mode[8]; /* 100-107 */ |
| 77 | char uid[8]; /* 108-115 */ |
| 78 | char gid[8]; /* 116-123 */ |
| 79 | char size[12]; /* 124-135 */ |
| 80 | char mtime[12]; /* 136-147 */ |
| 81 | char chksum[8]; /* 148-155 */ |
| 82 | char typeflag; /* 156-156 */ |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 83 | char linkname[NAME_SIZE]; /* 157-256 */ |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 84 | char magic[6]; /* 257-262 */ |
| 85 | char version[2]; /* 263-264 */ |
| 86 | char uname[32]; /* 265-296 */ |
| 87 | char gname[32]; /* 297-328 */ |
| 88 | char devmajor[8]; /* 329-336 */ |
| 89 | char devminor[8]; /* 337-344 */ |
| 90 | char prefix[155]; /* 345-499 */ |
| 91 | char padding[12]; /* 500-512 (pad to exactly the TAR_BLOCK_SIZE) */ |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 92 | }; |
| 93 | typedef struct TarHeader TarHeader; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 94 | |
Eric Andersen | 3d957c8 | 2000-12-07 00:34:58 +0000 | [diff] [blame] | 95 | /* |
| 96 | ** writeTarFile(), writeFileToTarball(), and writeTarHeader() are |
| 97 | ** the only functions that deal with the HardLinkInfo structure. |
| 98 | ** Even these functions use the xxxHardLinkInfo() functions. |
| 99 | */ |
| 100 | typedef struct HardLinkInfo HardLinkInfo; |
| 101 | struct HardLinkInfo |
| 102 | { |
| 103 | HardLinkInfo *next; /* Next entry in list */ |
| 104 | dev_t dev; /* Device number */ |
| 105 | ino_t ino; /* Inode number */ |
| 106 | short linkCount; /* (Hard) Link Count */ |
| 107 | char name[1]; /* Start of filename (must be last) */ |
| 108 | }; |
| 109 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 110 | /* Some info to be carried along when creating a new tarball */ |
| 111 | struct TarBallInfo |
| 112 | { |
| 113 | char* fileName; /* File name of the tarball */ |
| 114 | int tarFd; /* Open-for-write file descriptor |
| 115 | for the tarball */ |
| 116 | struct stat statBuf; /* Stat info for the tarball, letting |
| 117 | us know the inode and device that the |
| 118 | tarball lives, so we can avoid trying |
| 119 | to include the tarball into itself */ |
| 120 | int verboseFlag; /* Whether to print extra stuff or not */ |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 121 | char** excludeList; /* List of files to not include */ |
Eric Andersen | 3d957c8 | 2000-12-07 00:34:58 +0000 | [diff] [blame] | 122 | HardLinkInfo *hlInfoHead; /* Hard Link Tracking Information */ |
| 123 | HardLinkInfo *hlInfo; /* Hard Link Info for the current file */ |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 124 | }; |
| 125 | typedef struct TarBallInfo TarBallInfo; |
| 126 | |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 127 | /* A nice enum with all the possible tar file content types */ |
| 128 | enum TarFileType |
| 129 | { |
| 130 | REGTYPE = '0', /* regular file */ |
| 131 | REGTYPE0 = '\0', /* regular file (ancient bug compat)*/ |
| 132 | LNKTYPE = '1', /* hard link */ |
| 133 | SYMTYPE = '2', /* symbolic link */ |
| 134 | CHRTYPE = '3', /* character special */ |
| 135 | BLKTYPE = '4', /* block special */ |
| 136 | DIRTYPE = '5', /* directory */ |
| 137 | FIFOTYPE = '6', /* FIFO special */ |
| 138 | CONTTYPE = '7', /* reserved */ |
| 139 | GNULONGLINK = 'K', /* GNU long (>100 chars) link name */ |
| 140 | GNULONGNAME = 'L', /* GNU long (>100 chars) file name */ |
| 141 | }; |
| 142 | typedef enum TarFileType TarFileType; |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 143 | |
Eric Andersen | 3d957c8 | 2000-12-07 00:34:58 +0000 | [diff] [blame] | 144 | /* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */ |
Aaron Lehmann | 8fc5d6d | 2002-08-21 13:11:34 +0000 | [diff] [blame] | 145 | extern inline void |
Eric Andersen | 3d957c8 | 2000-12-07 00:34:58 +0000 | [diff] [blame] | 146 | addHardLinkInfo (HardLinkInfo **hlInfoHeadPtr, dev_t dev, ino_t ino, |
| 147 | short linkCount, const char *name) |
| 148 | { |
| 149 | /* Note: hlInfoHeadPtr can never be NULL! */ |
| 150 | HardLinkInfo *hlInfo; |
| 151 | |
| 152 | hlInfo = (HardLinkInfo *)xmalloc(sizeof(HardLinkInfo)+strlen(name)+1); |
| 153 | if (hlInfo) { |
| 154 | hlInfo->next = *hlInfoHeadPtr; |
| 155 | *hlInfoHeadPtr = hlInfo; |
| 156 | hlInfo->dev = dev; |
| 157 | hlInfo->ino = ino; |
| 158 | hlInfo->linkCount = linkCount; |
| 159 | strcpy(hlInfo->name, name); |
| 160 | } |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | static void |
| 165 | freeHardLinkInfo (HardLinkInfo **hlInfoHeadPtr) |
| 166 | { |
| 167 | HardLinkInfo *hlInfo = NULL; |
| 168 | HardLinkInfo *hlInfoNext = NULL; |
| 169 | |
| 170 | if (hlInfoHeadPtr) { |
| 171 | hlInfo = *hlInfoHeadPtr; |
| 172 | while (hlInfo) { |
| 173 | hlInfoNext = hlInfo->next; |
| 174 | free(hlInfo); |
| 175 | hlInfo = hlInfoNext; |
| 176 | } |
| 177 | *hlInfoHeadPtr = NULL; |
| 178 | } |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | /* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */ |
Aaron Lehmann | 8fc5d6d | 2002-08-21 13:11:34 +0000 | [diff] [blame] | 183 | extern inline HardLinkInfo * |
Eric Andersen | 3d957c8 | 2000-12-07 00:34:58 +0000 | [diff] [blame] | 184 | findHardLinkInfo (HardLinkInfo *hlInfo, dev_t dev, ino_t ino) |
| 185 | { |
| 186 | while(hlInfo) { |
| 187 | if ((ino == hlInfo->ino) && (dev == hlInfo->dev)) |
| 188 | break; |
| 189 | hlInfo = hlInfo->next; |
| 190 | } |
| 191 | return(hlInfo); |
| 192 | } |
| 193 | |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 194 | /* Put an octal string into the specified buffer. |
| 195 | * The number is zero and space padded and possibly null padded. |
| 196 | * Returns TRUE if successful. */ |
| 197 | static int putOctal (char *cp, int len, long value) |
| 198 | { |
| 199 | int tempLength; |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 200 | char tempBuffer[32]; |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 201 | char *tempString = tempBuffer; |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 202 | |
| 203 | /* Create a string of the specified length with an initial space, |
| 204 | * leading zeroes and the octal number, and a trailing null. */ |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 205 | sprintf (tempString, "%0*lo", len - 1, value); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 206 | |
| 207 | /* If the string is too large, suppress the leading space. */ |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 208 | tempLength = strlen (tempString) + 1; |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 209 | if (tempLength > len) { |
| 210 | tempLength--; |
| 211 | tempString++; |
| 212 | } |
| 213 | |
| 214 | /* If the string is still too large, suppress the trailing null. */ |
| 215 | if (tempLength > len) |
| 216 | tempLength--; |
| 217 | |
| 218 | /* If the string is still too large, fail. */ |
| 219 | if (tempLength > len) |
| 220 | return FALSE; |
| 221 | |
| 222 | /* Copy the string to the field. */ |
| 223 | memcpy (cp, tempString, len); |
| 224 | |
| 225 | return TRUE; |
| 226 | } |
| 227 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 228 | /* Write out a tar header for the specified file/directory/whatever */ |
Aaron Lehmann | 8fc5d6d | 2002-08-21 13:11:34 +0000 | [diff] [blame] | 229 | extern inline int |
Matt Kraai | e80a263 | 2000-12-19 20:45:49 +0000 | [diff] [blame] | 230 | writeTarHeader(struct TarBallInfo *tbInfo, const char *header_name, |
| 231 | const char *real_name, struct stat *statbuf) |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 232 | { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 233 | long chksum=0; |
| 234 | struct TarHeader header; |
| 235 | const unsigned char *cp = (const unsigned char *) &header; |
| 236 | ssize_t size = sizeof(struct TarHeader); |
Eric Andersen | fdd5103 | 2000-08-02 18:48:26 +0000 | [diff] [blame] | 237 | |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 238 | memset( &header, 0, size); |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 239 | |
Matt Kraai | e80a263 | 2000-12-19 20:45:49 +0000 | [diff] [blame] | 240 | strncpy(header.name, header_name, sizeof(header.name)); |
Erik Andersen | ecd5124 | 2000-04-08 03:08:21 +0000 | [diff] [blame] | 241 | |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 242 | putOctal(header.mode, sizeof(header.mode), statbuf->st_mode); |
| 243 | putOctal(header.uid, sizeof(header.uid), statbuf->st_uid); |
| 244 | putOctal(header.gid, sizeof(header.gid), statbuf->st_gid); |
| 245 | putOctal(header.size, sizeof(header.size), 0); /* Regular file size is handled later */ |
| 246 | putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime); |
| 247 | strncpy(header.magic, TAR_MAGIC TAR_VERSION, |
| 248 | TAR_MAGIC_LEN + TAR_VERSION_LEN ); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 249 | |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 250 | /* Enter the user and group names (default to root if it fails) */ |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 251 | my_getpwuid(header.uname, statbuf->st_uid); |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 252 | if (! *header.uname) |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 253 | strcpy(header.uname, "root"); |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 254 | my_getgrgid(header.gname, statbuf->st_gid); |
| 255 | if (! *header.uname) |
Erik Andersen | 84e09e4 | 2000-04-08 20:58:35 +0000 | [diff] [blame] | 256 | strcpy(header.uname, "root"); |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 257 | |
Eric Andersen | 3d957c8 | 2000-12-07 00:34:58 +0000 | [diff] [blame] | 258 | if (tbInfo->hlInfo) { |
| 259 | /* This is a hard link */ |
| 260 | header.typeflag = LNKTYPE; |
| 261 | strncpy(header.linkname, tbInfo->hlInfo->name, sizeof(header.linkname)); |
| 262 | } else if (S_ISLNK(statbuf->st_mode)) { |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 263 | char *lpath = xreadlink(real_name); |
Eric Andersen | 28355a3 | 2001-05-07 17:48:28 +0000 | [diff] [blame] | 264 | if (!lpath) /* Already printed err msg inside xreadlink() */ |
| 265 | return ( FALSE); |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 266 | header.typeflag = SYMTYPE; |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 267 | strncpy(header.linkname, lpath, sizeof(header.linkname)); |
| 268 | free(lpath); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 269 | } else if (S_ISDIR(statbuf->st_mode)) { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 270 | header.typeflag = DIRTYPE; |
| 271 | strncat(header.name, "/", sizeof(header.name)); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 272 | } else if (S_ISCHR(statbuf->st_mode)) { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 273 | header.typeflag = CHRTYPE; |
| 274 | putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev)); |
| 275 | putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev)); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 276 | } else if (S_ISBLK(statbuf->st_mode)) { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 277 | header.typeflag = BLKTYPE; |
| 278 | putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev)); |
| 279 | putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev)); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 280 | } else if (S_ISFIFO(statbuf->st_mode)) { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 281 | header.typeflag = FIFOTYPE; |
| 282 | } else if (S_ISREG(statbuf->st_mode)) { |
| 283 | header.typeflag = REGTYPE; |
| 284 | putOctal(header.size, sizeof(header.size), statbuf->st_size); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 285 | } else { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 286 | error_msg("%s: Unknown file type", real_name); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 287 | return ( FALSE); |
| 288 | } |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 289 | |
Eric Andersen | 77d9268 | 2001-05-23 20:32:09 +0000 | [diff] [blame] | 290 | /* Calculate and store the checksum (i.e., the sum of all of the bytes of |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 291 | * the header). The checksum field must be filled with blanks for the |
| 292 | * calculation. The checksum field is formatted differently from the |
| 293 | * other fields: it has [6] digits, a null, then a space -- rather than |
| 294 | * digits, followed by a null like the other fields... */ |
| 295 | memset(header.chksum, ' ', sizeof(header.chksum)); |
| 296 | cp = (const unsigned char *) &header; |
| 297 | while (size-- > 0) |
| 298 | chksum += *cp++; |
| 299 | putOctal(header.chksum, 7, chksum); |
| 300 | |
| 301 | /* Now write the header out to disk */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 302 | if ((size=full_write(tbInfo->tarFd, (char*)&header, sizeof(struct TarHeader))) < 0) { |
Matt Kraai | e80a263 | 2000-12-19 20:45:49 +0000 | [diff] [blame] | 303 | error_msg(io_error, real_name, strerror(errno)); |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 304 | return ( FALSE); |
| 305 | } |
| 306 | /* Pad the header up to the tar block size */ |
| 307 | for (; size<TAR_BLOCK_SIZE; size++) { |
| 308 | write(tbInfo->tarFd, "\0", 1); |
| 309 | } |
| 310 | /* Now do the verbose thing (or not) */ |
Robert Griebl | f2f26e7 | 2002-07-23 22:05:47 +0000 | [diff] [blame] | 311 | |
| 312 | if (tbInfo->verboseFlag) { |
Eric Andersen | fdd5103 | 2000-08-02 18:48:26 +0000 | [diff] [blame] | 313 | FILE *vbFd = stdout; |
Robert Griebl | f2f26e7 | 2002-07-23 22:05:47 +0000 | [diff] [blame] | 314 | if (tbInfo->verboseFlag == 2) // If the archive goes to stdout, verbose to stderr |
Eric Andersen | fdd5103 | 2000-08-02 18:48:26 +0000 | [diff] [blame] | 315 | vbFd = stderr; |
| 316 | fprintf(vbFd, "%s\n", header.name); |
| 317 | } |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 318 | |
| 319 | return ( TRUE); |
| 320 | } |
| 321 | |
Eric Andersen | c265b17 | 2001-10-27 03:20:00 +0000 | [diff] [blame] | 322 | # if defined CONFIG_FEATURE_TAR_EXCLUDE |
Aaron Lehmann | 8fc5d6d | 2002-08-21 13:11:34 +0000 | [diff] [blame] | 323 | extern inline int exclude_file(char **excluded_files, const char *file) |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 324 | { |
| 325 | int i; |
| 326 | |
| 327 | if (excluded_files == NULL) |
| 328 | return 0; |
| 329 | |
| 330 | for (i = 0; excluded_files[i] != NULL; i++) { |
| 331 | if (excluded_files[i][0] == '/') { |
| 332 | if (fnmatch(excluded_files[i], file, |
| 333 | FNM_PATHNAME | FNM_LEADING_DIR) == 0) |
| 334 | return 1; |
| 335 | } else { |
| 336 | const char *p; |
| 337 | |
| 338 | for (p = file; p[0] != '\0'; p++) { |
| 339 | if ((p == file || p[-1] == '/') && p[0] != '/' && |
| 340 | fnmatch(excluded_files[i], p, |
| 341 | FNM_PATHNAME | FNM_LEADING_DIR) == 0) |
| 342 | return 1; |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | return 0; |
| 348 | } |
Eric Andersen | c265b17 | 2001-10-27 03:20:00 +0000 | [diff] [blame] | 349 | #endif |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 350 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 351 | static int writeFileToTarball(const char *fileName, struct stat *statbuf, void* userData) |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 352 | { |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 353 | struct TarBallInfo *tbInfo = (struct TarBallInfo *)userData; |
Matt Kraai | e80a263 | 2000-12-19 20:45:49 +0000 | [diff] [blame] | 354 | const char *header_name; |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 355 | |
Eric Andersen | 3d957c8 | 2000-12-07 00:34:58 +0000 | [diff] [blame] | 356 | /* |
| 357 | ** Check to see if we are dealing with a hard link. |
| 358 | ** If so - |
| 359 | ** Treat the first occurance of a given dev/inode as a file while |
| 360 | ** treating any additional occurances as hard links. This is done |
| 361 | ** by adding the file information to the HardLinkInfo linked list. |
| 362 | */ |
| 363 | tbInfo->hlInfo = NULL; |
| 364 | if (statbuf->st_nlink > 1) { |
| 365 | tbInfo->hlInfo = findHardLinkInfo(tbInfo->hlInfoHead, statbuf->st_dev, |
| 366 | statbuf->st_ino); |
| 367 | if (tbInfo->hlInfo == NULL) |
| 368 | addHardLinkInfo (&tbInfo->hlInfoHead, statbuf->st_dev, |
| 369 | statbuf->st_ino, statbuf->st_nlink, fileName); |
| 370 | } |
| 371 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 372 | /* It is against the rules to archive a socket */ |
| 373 | if (S_ISSOCK(statbuf->st_mode)) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 374 | error_msg("%s: socket ignored", fileName); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 375 | return( TRUE); |
| 376 | } |
| 377 | |
| 378 | /* It is a bad idea to store the archive we are in the process of creating, |
| 379 | * so check the device and inode to be sure that this particular file isn't |
| 380 | * the new tarball */ |
| 381 | if (tbInfo->statBuf.st_dev == statbuf->st_dev && |
| 382 | tbInfo->statBuf.st_ino == statbuf->st_ino) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 383 | error_msg("%s: file is the archive; skipping", fileName); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 384 | return( TRUE); |
| 385 | } |
| 386 | |
Matt Kraai | e80a263 | 2000-12-19 20:45:49 +0000 | [diff] [blame] | 387 | header_name = fileName; |
| 388 | while (header_name[0] == '/') { |
Matt Kraai | a1f9775 | 2000-12-19 06:24:08 +0000 | [diff] [blame] | 389 | static int alreadyWarned=FALSE; |
| 390 | if (alreadyWarned==FALSE) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 391 | error_msg("Removing leading '/' from member names"); |
Matt Kraai | a1f9775 | 2000-12-19 06:24:08 +0000 | [diff] [blame] | 392 | alreadyWarned=TRUE; |
| 393 | } |
Matt Kraai | e80a263 | 2000-12-19 20:45:49 +0000 | [diff] [blame] | 394 | header_name++; |
Matt Kraai | a1f9775 | 2000-12-19 06:24:08 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 397 | if (strlen(fileName) >= NAME_SIZE) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 398 | error_msg(name_longer_than_foo, NAME_SIZE); |
Eric Andersen | 1b1cfde | 2000-09-24 00:54:37 +0000 | [diff] [blame] | 399 | return ( TRUE); |
| 400 | } |
| 401 | |
Matt Kraai | e80a263 | 2000-12-19 20:45:49 +0000 | [diff] [blame] | 402 | if (header_name[0] == '\0') |
Matt Kraai | a1f9775 | 2000-12-19 06:24:08 +0000 | [diff] [blame] | 403 | return TRUE; |
| 404 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 405 | # if defined CONFIG_FEATURE_TAR_EXCLUDE |
Matt Kraai | be7499c | 2001-01-03 17:22:10 +0000 | [diff] [blame] | 406 | if (exclude_file(tbInfo->excludeList, header_name)) { |
| 407 | return SKIP; |
Matt Kraai | a1f9775 | 2000-12-19 06:24:08 +0000 | [diff] [blame] | 408 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 409 | # endif //CONFIG_FEATURE_TAR_EXCLUDE |
Matt Kraai | a1f9775 | 2000-12-19 06:24:08 +0000 | [diff] [blame] | 410 | |
Matt Kraai | e80a263 | 2000-12-19 20:45:49 +0000 | [diff] [blame] | 411 | if (writeTarHeader(tbInfo, header_name, fileName, statbuf)==FALSE) { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 412 | return( FALSE); |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 413 | } |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 414 | |
| 415 | /* Now, if the file is a regular file, copy it out to the tarball */ |
Eric Andersen | 3d957c8 | 2000-12-07 00:34:58 +0000 | [diff] [blame] | 416 | if ((tbInfo->hlInfo == NULL) |
| 417 | && (S_ISREG(statbuf->st_mode))) { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 418 | int inputFileFd; |
| 419 | char buffer[BUFSIZ]; |
| 420 | ssize_t size=0, readSize=0; |
| 421 | |
| 422 | /* open the file we want to archive, and make sure all is well */ |
| 423 | if ((inputFileFd = open(fileName, O_RDONLY)) < 0) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 424 | error_msg("%s: Cannot open: %s", fileName, strerror(errno)); |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 425 | return( FALSE); |
| 426 | } |
| 427 | |
| 428 | /* write the file to the archive */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 429 | while ( (size = full_read(inputFileFd, buffer, sizeof(buffer))) > 0 ) { |
| 430 | if (full_write(tbInfo->tarFd, buffer, size) != size ) { |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 431 | /* Output file seems to have a problem */ |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 432 | error_msg(io_error, fileName, strerror(errno)); |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 433 | return( FALSE); |
| 434 | } |
| 435 | readSize+=size; |
| 436 | } |
| 437 | if (size == -1) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 438 | error_msg(io_error, fileName, strerror(errno)); |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 439 | return( FALSE); |
| 440 | } |
| 441 | /* Pad the file up to the tar block size */ |
| 442 | for (; (readSize%TAR_BLOCK_SIZE) != 0; readSize++) { |
| 443 | write(tbInfo->tarFd, "\0", 1); |
| 444 | } |
| 445 | close( inputFileFd); |
| 446 | } |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 447 | |
| 448 | return( TRUE); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Aaron Lehmann | 8fc5d6d | 2002-08-21 13:11:34 +0000 | [diff] [blame] | 451 | extern inline int writeTarFile(const char* tarName, int verboseFlag, char **argv, |
Robert Griebl | f2f26e7 | 2002-07-23 22:05:47 +0000 | [diff] [blame] | 452 | char** excludeList, int gzip) |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 453 | { |
Robert Griebl | f2f26e7 | 2002-07-23 22:05:47 +0000 | [diff] [blame] | 454 | #ifdef CONFIG_FEATURE_TAR_GZIP |
| 455 | int gzipDataPipe [2] = { -1, -1 }; |
| 456 | int gzipStatusPipe [2] = { -1, -1 }; |
| 457 | pid_t gzipPid = 0; |
| 458 | #endif |
| 459 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 460 | int errorFlag=FALSE; |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 461 | ssize_t size; |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 462 | struct TarBallInfo tbInfo; |
Eric Andersen | 3d957c8 | 2000-12-07 00:34:58 +0000 | [diff] [blame] | 463 | tbInfo.hlInfoHead = NULL; |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 464 | |
| 465 | /* Make sure there is at least one file to tar up. */ |
Matt Kraai | d8ad76c | 2000-11-08 02:35:47 +0000 | [diff] [blame] | 466 | if (*argv == NULL) |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 467 | error_msg_and_die("Cowardly refusing to create an empty archive"); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 468 | |
| 469 | /* Open the tar file for writing. */ |
Robert Griebl | f2f26e7 | 2002-07-23 22:05:47 +0000 | [diff] [blame] | 470 | if (tarName == NULL) { |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 471 | tbInfo.tarFd = fileno(stdout); |
Robert Griebl | f2f26e7 | 2002-07-23 22:05:47 +0000 | [diff] [blame] | 472 | tbInfo.verboseFlag = verboseFlag ? 2 : 0; |
| 473 | } |
| 474 | else { |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 475 | tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
Robert Griebl | f2f26e7 | 2002-07-23 22:05:47 +0000 | [diff] [blame] | 476 | tbInfo.verboseFlag = verboseFlag ? 1 : 0; |
| 477 | } |
| 478 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 479 | if (tbInfo.tarFd < 0) { |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 480 | perror_msg( "Error opening '%s'", tarName); |
Eric Andersen | 3d957c8 | 2000-12-07 00:34:58 +0000 | [diff] [blame] | 481 | freeHardLinkInfo(&tbInfo.hlInfoHead); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 482 | return ( FALSE); |
| 483 | } |
Robert Griebl | f2f26e7 | 2002-07-23 22:05:47 +0000 | [diff] [blame] | 484 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 485 | /* Store the stat info for the tarball's file, so |
| 486 | * can avoid including the tarball into itself.... */ |
| 487 | if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 488 | error_msg_and_die(io_error, tarName, strerror(errno)); |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 489 | |
Robert Griebl | f2f26e7 | 2002-07-23 22:05:47 +0000 | [diff] [blame] | 490 | #ifdef CONFIG_FEATURE_TAR_GZIP |
| 491 | if ( gzip ) { |
| 492 | if ( socketpair ( AF_UNIX, SOCK_STREAM, 0, gzipDataPipe ) < 0 || pipe ( gzipStatusPipe ) < 0 ) |
| 493 | perror_msg_and_die ( "Failed to create gzip pipe" ); |
| 494 | |
| 495 | signal ( SIGPIPE, SIG_IGN ); // we only want EPIPE on errors |
| 496 | |
| 497 | gzipPid = fork ( ); |
| 498 | |
| 499 | if ( gzipPid == 0 ) { |
| 500 | dup2 ( gzipDataPipe [0], 0 ); |
| 501 | close ( gzipDataPipe [1] ); |
| 502 | |
| 503 | if ( tbInfo. tarFd != 1 ); |
| 504 | dup2 ( tbInfo. tarFd, 1 ); |
| 505 | |
| 506 | close ( gzipStatusPipe [0] ); |
| 507 | fcntl( gzipStatusPipe [1], F_SETFD, FD_CLOEXEC ); // close on exec shows sucess |
| 508 | |
| 509 | execl ( "/bin/gzip", "gzip", "-f", 0 ); |
| 510 | |
| 511 | write ( gzipStatusPipe [1], "", 1 ); |
| 512 | close ( gzipStatusPipe [1] ); |
| 513 | |
| 514 | exit ( -1 ); |
| 515 | } |
| 516 | else if ( gzipPid > 0 ) { |
| 517 | close ( gzipDataPipe [0] ); |
| 518 | close ( gzipStatusPipe [1] ); |
| 519 | |
| 520 | while ( 1 ) { |
| 521 | char buf; |
| 522 | |
| 523 | int n = read ( gzipStatusPipe [0], &buf, 1 ); |
| 524 | if ( n == 1 ) |
| 525 | error_msg_and_die ( "Could not exec gzip process" ); // socket was not closed => error |
| 526 | else if (( n < 0 ) && ( errno==EAGAIN || errno==EINTR )) |
| 527 | continue; // try it again |
| 528 | break; |
| 529 | } |
| 530 | close ( gzipStatusPipe [0] ); |
| 531 | |
| 532 | tbInfo. tarFd = gzipDataPipe [1]; |
| 533 | } |
| 534 | else { |
| 535 | perror_msg_and_die ( "Failed to fork gzip process" ); |
| 536 | } |
| 537 | } |
| 538 | #endif |
| 539 | |
| 540 | tbInfo.excludeList=excludeList; |
| 541 | |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 542 | /* Read the directory/files and iterate over them one at a time */ |
Matt Kraai | d8ad76c | 2000-11-08 02:35:47 +0000 | [diff] [blame] | 543 | while (*argv != NULL) { |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 544 | if (! recursive_action(*argv++, TRUE, FALSE, FALSE, |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 545 | writeFileToTarball, writeFileToTarball, |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 546 | (void*) &tbInfo)) { |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 547 | errorFlag = TRUE; |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 548 | } |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 549 | } |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 550 | /* Write two empty blocks to the end of the archive */ |
| 551 | for (size=0; size<(2*TAR_BLOCK_SIZE); size++) { |
| 552 | write(tbInfo.tarFd, "\0", 1); |
| 553 | } |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 554 | |
| 555 | /* To be pedantically correct, we would check if the tarball |
Eric Andersen | 3c5ee9a | 2000-11-14 22:15:48 +0000 | [diff] [blame] | 556 | * is smaller than 20 tar blocks, and pad it if it was smaller, |
Erik Andersen | 0817d13 | 2000-04-09 15:17:40 +0000 | [diff] [blame] | 557 | * but that isn't necessary for GNU tar interoperability, and |
| 558 | * so is considered a waste of space */ |
| 559 | |
Erik Andersen | 68a9ea4 | 2000-04-04 18:39:50 +0000 | [diff] [blame] | 560 | /* Hang up the tools, close up shop, head home */ |
Robert Griebl | f2f26e7 | 2002-07-23 22:05:47 +0000 | [diff] [blame] | 561 | close(tbInfo.tarFd); |
| 562 | if (errorFlag) |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 563 | error_msg("Error exit delayed from previous errors"); |
Robert Griebl | f2f26e7 | 2002-07-23 22:05:47 +0000 | [diff] [blame] | 564 | |
Eric Andersen | 3d957c8 | 2000-12-07 00:34:58 +0000 | [diff] [blame] | 565 | freeHardLinkInfo(&tbInfo.hlInfoHead); |
Robert Griebl | f2f26e7 | 2002-07-23 22:05:47 +0000 | [diff] [blame] | 566 | |
| 567 | #ifdef CONFIG_FEATURE_TAR_GZIP |
| 568 | if ( gzip && gzipPid ) { |
| 569 | if ( waitpid ( gzipPid, NULL, 0 ) == -1 ) |
| 570 | printf ( "Couldnt wait ?" ); |
| 571 | } |
| 572 | #endif |
| 573 | |
| 574 | return !errorFlag; |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 575 | } |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 576 | #endif //tar_create |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 577 | |
Glenn L McGrath | d642a67 | 2001-10-13 06:54:45 +0000 | [diff] [blame] | 578 | void append_file_to_list(const char *new_name, char ***list, int *list_count) |
| 579 | { |
| 580 | *list = realloc(*list, sizeof(char *) * (*list_count + 2)); |
Glenn L McGrath | 051eee6 | 2001-10-13 07:11:03 +0000 | [diff] [blame] | 581 | (*list)[*list_count] = xstrdup(new_name); |
Glenn L McGrath | d642a67 | 2001-10-13 06:54:45 +0000 | [diff] [blame] | 582 | (*list_count)++; |
| 583 | (*list)[*list_count] = NULL; |
| 584 | } |
| 585 | |
| 586 | void append_file_list_to_list(char *filename, char ***name_list, int *num_of_entries) |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 587 | { |
| 588 | FILE *src_stream; |
| 589 | char *line; |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 590 | |
| 591 | src_stream = xfopen(filename, "r"); |
| 592 | while ((line = get_line_from_file(src_stream)) != NULL) { |
Matt Kraai | 39fcb5a | 2002-01-02 19:01:41 +0000 | [diff] [blame] | 593 | chomp (line); |
Glenn L McGrath | d642a67 | 2001-10-13 06:54:45 +0000 | [diff] [blame] | 594 | append_file_to_list(line, name_list, num_of_entries); |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 595 | free(line); |
| 596 | } |
| 597 | fclose(src_stream); |
| 598 | } |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 599 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 600 | #ifdef CONFIG_FEATURE_TAR_EXCLUDE |
Glenn L McGrath | 0e76618 | 2001-10-13 05:03:29 +0000 | [diff] [blame] | 601 | /* |
| 602 | * Create a list of names that are in the include list AND NOT in the exclude lists |
| 603 | */ |
Aaron Lehmann | 8fc5d6d | 2002-08-21 13:11:34 +0000 | [diff] [blame] | 604 | #if 0 /* this is unused */ |
Glenn L McGrath | 0e76618 | 2001-10-13 05:03:29 +0000 | [diff] [blame] | 605 | char **list_and_not_list(char **include_list, char **exclude_list) |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 606 | { |
| 607 | char **new_include_list = NULL; |
| 608 | int new_include_count = 0; |
| 609 | int include_count = 0; |
| 610 | int exclude_count; |
Matt Kraai | f86bbfa | 2001-10-12 19:00:15 +0000 | [diff] [blame] | 611 | |
Glenn L McGrath | 0e76618 | 2001-10-13 05:03:29 +0000 | [diff] [blame] | 612 | if (include_list == NULL) { |
| 613 | return(NULL); |
| 614 | } |
| 615 | |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 616 | while (include_list[include_count] != NULL) { |
| 617 | int found = FALSE; |
| 618 | exclude_count = 0; |
| 619 | while (exclude_list[exclude_count] != NULL) { |
| 620 | if (strcmp(include_list[include_count], exclude_list[exclude_count]) == 0) { |
| 621 | found = TRUE; |
| 622 | break; |
| 623 | } |
| 624 | exclude_count++; |
| 625 | } |
| 626 | |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 627 | if (! found) { |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 628 | new_include_list = realloc(new_include_list, sizeof(char *) * (include_count + 2)); |
| 629 | new_include_list[new_include_count] = include_list[include_count]; |
| 630 | new_include_count++; |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 631 | } else { |
| 632 | free(include_list[include_count]); |
| 633 | } |
| 634 | include_count++; |
| 635 | } |
Glenn L McGrath | 0e76618 | 2001-10-13 05:03:29 +0000 | [diff] [blame] | 636 | new_include_list[new_include_count] = NULL; |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 637 | return(new_include_list); |
| 638 | } |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 639 | #endif |
Aaron Lehmann | 8fc5d6d | 2002-08-21 13:11:34 +0000 | [diff] [blame] | 640 | #endif |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 641 | |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 642 | int tar_main(int argc, char **argv) |
| 643 | { |
| 644 | enum untar_funct_e { |
Matt Kraai | 2b1effd | 2001-12-20 22:09:31 +0000 | [diff] [blame] | 645 | /* This is optional */ |
| 646 | untar_unzip = 1, |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 647 | /* Require one and only one of these */ |
Matt Kraai | 2b1effd | 2001-12-20 22:09:31 +0000 | [diff] [blame] | 648 | untar_list = 2, |
| 649 | untar_create = 4, |
| 650 | untar_extract = 8 |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 651 | }; |
| 652 | |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 653 | FILE *src_stream = NULL; |
| 654 | FILE *uncompressed_stream = NULL; |
| 655 | char **include_list = NULL; |
Glenn L McGrath | 4bef7b4 | 2001-10-13 19:43:46 +0000 | [diff] [blame] | 656 | char **exclude_list = NULL; |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 657 | char *src_filename = NULL; |
| 658 | char *dst_prefix = NULL; |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 659 | int opt; |
| 660 | unsigned short untar_funct = 0; |
| 661 | unsigned short untar_funct_required = 0; |
| 662 | unsigned short extract_function = 0; |
Glenn L McGrath | 0e76618 | 2001-10-13 05:03:29 +0000 | [diff] [blame] | 663 | int include_list_count = 0; |
Eric Andersen | 9c6b5fc | 2001-11-17 07:23:46 +0000 | [diff] [blame] | 664 | #ifdef CONFIG_FEATURE_TAR_EXCLUDE |
Glenn L McGrath | 4bef7b4 | 2001-10-13 19:43:46 +0000 | [diff] [blame] | 665 | int exclude_list_count = 0; |
Eric Andersen | 9c6b5fc | 2001-11-17 07:23:46 +0000 | [diff] [blame] | 666 | #endif |
Matt Kraai | 9fb38f6 | 2001-11-12 16:45:23 +0000 | [diff] [blame] | 667 | #ifdef CONFIG_FEATURE_TAR_GZIP |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 668 | int gunzip_pid; |
| 669 | int gz_fd = 0; |
Matt Kraai | 9fb38f6 | 2001-11-12 16:45:23 +0000 | [diff] [blame] | 670 | #endif |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 671 | |
| 672 | if (argc < 2) { |
| 673 | show_usage(); |
| 674 | } |
| 675 | |
| 676 | /* Prepend '-' to the first argument if required */ |
| 677 | if (argv[1][0] != '-') { |
| 678 | char *tmp = xmalloc(strlen(argv[1]) + 2); |
| 679 | tmp[0] = '-'; |
| 680 | strcpy(tmp + 1, argv[1]); |
| 681 | argv[1] = tmp; |
| 682 | } |
| 683 | |
| 684 | while ((opt = getopt(argc, argv, "ctxT:X:C:f:Opvz")) != -1) { |
| 685 | switch (opt) { |
| 686 | |
| 687 | /* One and only one of these is required */ |
| 688 | case 'c': |
| 689 | untar_funct_required |= untar_create; |
| 690 | break; |
| 691 | case 't': |
| 692 | untar_funct_required |= untar_list; |
| 693 | extract_function |= extract_list |extract_unconditional; |
| 694 | break; |
| 695 | case 'x': |
| 696 | untar_funct_required |= untar_extract; |
| 697 | extract_function |= (extract_all_to_fs | extract_unconditional | extract_create_leading_dirs); |
| 698 | break; |
| 699 | |
| 700 | /* These are optional */ |
| 701 | /* Exclude or Include files listed in <filename>*/ |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 702 | #ifdef CONFIG_FEATURE_TAR_EXCLUDE |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 703 | case 'X': |
Glenn L McGrath | d642a67 | 2001-10-13 06:54:45 +0000 | [diff] [blame] | 704 | append_file_list_to_list(optarg, &exclude_list, &exclude_list_count); |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 705 | break; |
| 706 | #endif |
| 707 | case 'T': |
| 708 | // by default a list is an include list |
Glenn L McGrath | d642a67 | 2001-10-13 06:54:45 +0000 | [diff] [blame] | 709 | append_file_list_to_list(optarg, &include_list, &include_list_count); |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 710 | break; |
| 711 | |
| 712 | case 'C': // Change to dir <optarg> |
| 713 | /* Make sure dst_prefix ends in a '/' */ |
| 714 | dst_prefix = concat_path_file(optarg, "/"); |
| 715 | break; |
| 716 | case 'f': // archive filename |
| 717 | if (strcmp(optarg, "-") == 0) { |
Matt Kraai | 2b1effd | 2001-12-20 22:09:31 +0000 | [diff] [blame] | 718 | src_filename = NULL; |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 719 | } else { |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 720 | src_filename = xstrdup(optarg); |
| 721 | } |
| 722 | break; |
| 723 | case 'O': |
| 724 | extract_function |= extract_to_stdout; |
| 725 | break; |
| 726 | case 'p': |
| 727 | break; |
| 728 | case 'v': |
Robert Griebl | f2f26e7 | 2002-07-23 22:05:47 +0000 | [diff] [blame] | 729 | extract_function |= extract_verbose_list; |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 730 | break; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 731 | #ifdef CONFIG_FEATURE_TAR_GZIP |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 732 | case 'z': |
| 733 | untar_funct |= untar_unzip; |
| 734 | break; |
| 735 | #endif |
| 736 | default: |
| 737 | show_usage(); |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | /* Make sure the valid arguments were passed */ |
| 742 | if (untar_funct_required == 0) { |
| 743 | error_msg_and_die("You must specify one of the `-ctx' options"); |
| 744 | } |
| 745 | if ((untar_funct_required != untar_create) && |
| 746 | (untar_funct_required != untar_extract) && |
| 747 | (untar_funct_required != untar_list)) { |
| 748 | error_msg_and_die("You may not specify more than one `ctx' option."); |
| 749 | } |
| 750 | untar_funct |= untar_funct_required; |
| 751 | |
| 752 | /* Setup an array of filenames to work with */ |
| 753 | while (optind < argc) { |
Glenn L McGrath | d642a67 | 2001-10-13 06:54:45 +0000 | [diff] [blame] | 754 | append_file_to_list(argv[optind], &include_list, &include_list_count); |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 755 | optind++; |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 756 | } |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 757 | if (extract_function & (extract_list | extract_all_to_fs)) { |
| 758 | if (dst_prefix == NULL) { |
| 759 | dst_prefix = xstrdup("./"); |
| 760 | } |
| 761 | |
| 762 | /* Setup the source of the tar data */ |
Matt Kraai | 2b1effd | 2001-12-20 22:09:31 +0000 | [diff] [blame] | 763 | if (src_filename != NULL) { |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 764 | src_stream = xfopen(src_filename, "r"); |
| 765 | } else { |
| 766 | src_stream = stdin; |
| 767 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 768 | #ifdef CONFIG_FEATURE_TAR_GZIP |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 769 | /* Get a binary tree of all the tar file headers */ |
| 770 | if (untar_funct & untar_unzip) { |
| 771 | uncompressed_stream = gz_open(src_stream, &gunzip_pid); |
| 772 | } else |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 773 | #endif // CONFIG_FEATURE_TAR_GZIP |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 774 | uncompressed_stream = src_stream; |
| 775 | |
| 776 | /* extract or list archive */ |
Glenn L McGrath | 4bef7b4 | 2001-10-13 19:43:46 +0000 | [diff] [blame] | 777 | unarchive(uncompressed_stream, stdout, &get_header_tar, extract_function, dst_prefix, include_list, exclude_list); |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 778 | fclose(uncompressed_stream); |
| 779 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 780 | #ifdef CONFIG_FEATURE_TAR_CREATE |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 781 | /* create an archive */ |
| 782 | else if (untar_funct & untar_create) { |
| 783 | int verboseFlag = FALSE; |
Robert Griebl | f2f26e7 | 2002-07-23 22:05:47 +0000 | [diff] [blame] | 784 | int gzipFlag = FALSE; |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 785 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 786 | #ifdef CONFIG_FEATURE_TAR_GZIP |
Robert Griebl | f2f26e7 | 2002-07-23 22:05:47 +0000 | [diff] [blame] | 787 | if (untar_funct & untar_unzip) |
| 788 | gzipFlag = TRUE; |
| 789 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 790 | #endif // CONFIG_FEATURE_TAR_GZIP |
Robert Griebl | f2f26e7 | 2002-07-23 22:05:47 +0000 | [diff] [blame] | 791 | if (extract_function & extract_verbose_list) |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 792 | verboseFlag = TRUE; |
Robert Griebl | f2f26e7 | 2002-07-23 22:05:47 +0000 | [diff] [blame] | 793 | |
| 794 | writeTarFile(src_filename, verboseFlag, include_list, exclude_list, gzipFlag); |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 795 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 796 | #endif // CONFIG_FEATURE_TAR_CREATE |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 797 | |
| 798 | /* Cleanups */ |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 799 | #ifdef CONFIG_FEATURE_TAR_GZIP |
Robert Griebl | f2f26e7 | 2002-07-23 22:05:47 +0000 | [diff] [blame] | 800 | if ( !( untar_funct & untar_create ) && ( untar_funct & untar_unzip )) { |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 801 | fclose(src_stream); |
| 802 | close(gz_fd); |
| 803 | gz_close(gunzip_pid); |
| 804 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 805 | #endif // CONFIG_FEATURE_TAR_GZIP |
Matt Kraai | 31c73af | 2001-12-20 22:30:14 +0000 | [diff] [blame] | 806 | #ifdef CONFIG_FEATURE_CLEAN_UP |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 807 | if (src_filename) { |
| 808 | free(src_filename); |
| 809 | } |
Matt Kraai | 31c73af | 2001-12-20 22:30:14 +0000 | [diff] [blame] | 810 | #endif |
Glenn L McGrath | 2e772ed | 2001-10-05 02:58:48 +0000 | [diff] [blame] | 811 | return(EXIT_SUCCESS); |
| 812 | } |