blob: b68882b520b7a1122d3b219e5a160232670642f9 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002/*
Erik Andersen68a9ea42000-04-04 18:39:50 +00003 * Mini tar implementation for busybox
Erik Andersen6acaa402000-03-26 14:03:20 +00004 *
Glenn L McGrath2e772ed2001-10-05 02:58:48 +00005 * Modifed to use common extraction code used by ar, cpio, dpkg-deb, dpkg
6 * Glenn McGrath <bug1@optushome.com.au>
7 *
Erik Andersen61677fe2000-04-13 01:18:56 +00008 * 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 Andersen77d92682001-05-23 20:32:09 +000010 * very different now (i.e., cleaner, less global variables, etc.)
Eric Andersenc4996011999-10-20 22:08:37 +000011 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +000012 * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
Eric Andersen1d1d2f92002-04-13 08:31:59 +000013 * Copyright (C) 1999-2002 by Erik Andersen <andersee@debian.org>
Eric Andersen96bcfd31999-11-12 01:30:18 +000014 *
Erik Andersen6acaa402000-03-26 14:03:20 +000015 * 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 Andersen68a9ea42000-04-04 18:39:50 +000021 * Based in part on the tar implementation from busybox-0.28
Erik Andersen6acaa402000-03-26 14:03:20 +000022 * Copyright (C) 1995 Bruce Perens
23 * This is free software under the GNU General Public License.
24 *
Eric Andersenc4996011999-10-20 22:08:37 +000025 * 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 Andersencc8ed391999-10-05 16:24:54 +000039 */
40
Eric Andersencc8ed391999-10-05 16:24:54 +000041#include <fcntl.h>
Matt Kraai43c8c382000-09-04 16:51:55 +000042#include <getopt.h>
Glenn L McGrath2e772ed2001-10-05 02:58:48 +000043#include <search.h>
44#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000045#include <stdlib.h>
46#include <unistd.h>
Glenn L McGrath2e772ed2001-10-05 02:58:48 +000047#include <fnmatch.h>
48#include <string.h>
49#include <errno.h>
Robert Grieblf2f26e72002-07-23 22:05:47 +000050#include <signal.h>
51#include <sys/wait.h>
52#include <sys/socket.h>
Glenn L McGrathef0eab52001-10-25 14:49:48 +000053#include "unarchive.h"
Eric Andersencbe31da2001-02-20 06:14:08 +000054#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000055
Eric Andersenbdfd0d72001-10-24 05:00:29 +000056#ifdef CONFIG_FEATURE_TAR_CREATE
Eric Andersencc8ed391999-10-05 16:24:54 +000057
Glenn L McGrath2e772ed2001-10-05 02:58:48 +000058/* 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
67static const int TAR_BLOCK_SIZE = 512;
68static const int TAR_MAGIC_LEN = 6;
69static const int TAR_VERSION_LEN = 2;
Eric Andersencc8ed391999-10-05 16:24:54 +000070
Erik Andersen298854f2000-03-23 01:09:18 +000071/* POSIX tar Header Block, from POSIX 1003.1-1990 */
Glenn L McGrath2e772ed2001-10-05 02:58:48 +000072enum { NAME_SIZE = 100 }; /* because gcc won't let me use 'static const int' */
Erik Andersen298854f2000-03-23 01:09:18 +000073struct TarHeader
Glenn L McGrath2e772ed2001-10-05 02:58:48 +000074{ /* byte offset */
Eric Andersen1b1cfde2000-09-24 00:54:37 +000075 char name[NAME_SIZE]; /* 0-99 */
Erik Andersen0817d132000-04-09 15:17:40 +000076 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 Andersen1b1cfde2000-09-24 00:54:37 +000083 char linkname[NAME_SIZE]; /* 157-256 */
Erik Andersen0817d132000-04-09 15:17:40 +000084 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 Andersen298854f2000-03-23 01:09:18 +000092};
93typedef struct TarHeader TarHeader;
Eric Andersencc8ed391999-10-05 16:24:54 +000094
Eric Andersen3d957c82000-12-07 00:34:58 +000095/*
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*/
100typedef struct HardLinkInfo HardLinkInfo;
101struct 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 Andersen68a9ea42000-04-04 18:39:50 +0000110/* Some info to be carried along when creating a new tarball */
111struct 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 Andersenecd51242000-04-08 03:08:21 +0000121 char** excludeList; /* List of files to not include */
Eric Andersen3d957c82000-12-07 00:34:58 +0000122 HardLinkInfo *hlInfoHead; /* Hard Link Tracking Information */
123 HardLinkInfo *hlInfo; /* Hard Link Info for the current file */
Erik Andersen68a9ea42000-04-04 18:39:50 +0000124};
125typedef struct TarBallInfo TarBallInfo;
126
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000127/* A nice enum with all the possible tar file content types */
128enum 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};
142typedef enum TarFileType TarFileType;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000143
Eric Andersen3d957c82000-12-07 00:34:58 +0000144/* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */
Aaron Lehmann8fc5d6d2002-08-21 13:11:34 +0000145extern inline void
Eric Andersen3d957c82000-12-07 00:34:58 +0000146addHardLinkInfo (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
164static void
165freeHardLinkInfo (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 Lehmann8fc5d6d2002-08-21 13:11:34 +0000183extern inline HardLinkInfo *
Eric Andersen3d957c82000-12-07 00:34:58 +0000184findHardLinkInfo (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 Andersen6acaa402000-03-26 14:03:20 +0000194/* 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. */
197static int putOctal (char *cp, int len, long value)
198{
199 int tempLength;
Erik Andersen6acaa402000-03-26 14:03:20 +0000200 char tempBuffer[32];
Erik Andersen5661fe02000-04-05 01:00:52 +0000201 char *tempString = tempBuffer;
Erik Andersen6acaa402000-03-26 14:03:20 +0000202
203 /* Create a string of the specified length with an initial space,
204 * leading zeroes and the octal number, and a trailing null. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000205 sprintf (tempString, "%0*lo", len - 1, value);
Erik Andersen6acaa402000-03-26 14:03:20 +0000206
207 /* If the string is too large, suppress the leading space. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000208 tempLength = strlen (tempString) + 1;
Erik Andersen6acaa402000-03-26 14:03:20 +0000209 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 Andersen68a9ea42000-04-04 18:39:50 +0000228/* Write out a tar header for the specified file/directory/whatever */
Aaron Lehmann8fc5d6d2002-08-21 13:11:34 +0000229extern inline int
Matt Kraaie80a2632000-12-19 20:45:49 +0000230writeTarHeader(struct TarBallInfo *tbInfo, const char *header_name,
231 const char *real_name, struct stat *statbuf)
Erik Andersen6acaa402000-03-26 14:03:20 +0000232{
Erik Andersen5661fe02000-04-05 01:00:52 +0000233 long chksum=0;
234 struct TarHeader header;
235 const unsigned char *cp = (const unsigned char *) &header;
236 ssize_t size = sizeof(struct TarHeader);
Eric Andersenfdd51032000-08-02 18:48:26 +0000237
Erik Andersen5661fe02000-04-05 01:00:52 +0000238 memset( &header, 0, size);
Erik Andersen3364d782000-03-28 00:58:14 +0000239
Matt Kraaie80a2632000-12-19 20:45:49 +0000240 strncpy(header.name, header_name, sizeof(header.name));
Erik Andersenecd51242000-04-08 03:08:21 +0000241
Erik Andersen5661fe02000-04-05 01:00:52 +0000242 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 Andersen68a9ea42000-04-04 18:39:50 +0000249
Erik Andersen84e09e42000-04-08 20:58:35 +0000250 /* Enter the user and group names (default to root if it fails) */
Erik Andersen5661fe02000-04-05 01:00:52 +0000251 my_getpwuid(header.uname, statbuf->st_uid);
Erik Andersen5661fe02000-04-05 01:00:52 +0000252 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000253 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +0000254 my_getgrgid(header.gname, statbuf->st_gid);
255 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000256 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +0000257
Eric Andersen3d957c82000-12-07 00:34:58 +0000258 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 Whitley8a633262001-04-30 18:17:00 +0000263 char *lpath = xreadlink(real_name);
Eric Andersen28355a32001-05-07 17:48:28 +0000264 if (!lpath) /* Already printed err msg inside xreadlink() */
265 return ( FALSE);
Erik Andersen5661fe02000-04-05 01:00:52 +0000266 header.typeflag = SYMTYPE;
Mark Whitley8a633262001-04-30 18:17:00 +0000267 strncpy(header.linkname, lpath, sizeof(header.linkname));
268 free(lpath);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000269 } else if (S_ISDIR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000270 header.typeflag = DIRTYPE;
271 strncat(header.name, "/", sizeof(header.name));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000272 } else if (S_ISCHR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000273 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 Andersen68a9ea42000-04-04 18:39:50 +0000276 } else if (S_ISBLK(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000277 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 Andersen68a9ea42000-04-04 18:39:50 +0000280 } else if (S_ISFIFO(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000281 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 Andersen68a9ea42000-04-04 18:39:50 +0000285 } else {
Matt Kraaidd19c692001-01-31 19:00:21 +0000286 error_msg("%s: Unknown file type", real_name);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000287 return ( FALSE);
288 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000289
Eric Andersen77d92682001-05-23 20:32:09 +0000290 /* Calculate and store the checksum (i.e., the sum of all of the bytes of
Erik Andersen5661fe02000-04-05 01:00:52 +0000291 * 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 Whitleyf57c9442000-12-07 19:56:48 +0000302 if ((size=full_write(tbInfo->tarFd, (char*)&header, sizeof(struct TarHeader))) < 0) {
Matt Kraaie80a2632000-12-19 20:45:49 +0000303 error_msg(io_error, real_name, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +0000304 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 Grieblf2f26e72002-07-23 22:05:47 +0000311
312 if (tbInfo->verboseFlag) {
Eric Andersenfdd51032000-08-02 18:48:26 +0000313 FILE *vbFd = stdout;
Robert Grieblf2f26e72002-07-23 22:05:47 +0000314 if (tbInfo->verboseFlag == 2) // If the archive goes to stdout, verbose to stderr
Eric Andersenfdd51032000-08-02 18:48:26 +0000315 vbFd = stderr;
316 fprintf(vbFd, "%s\n", header.name);
317 }
Erik Andersen3364d782000-03-28 00:58:14 +0000318
319 return ( TRUE);
320}
321
Eric Andersenc265b172001-10-27 03:20:00 +0000322# if defined CONFIG_FEATURE_TAR_EXCLUDE
Aaron Lehmann8fc5d6d2002-08-21 13:11:34 +0000323extern inline int exclude_file(char **excluded_files, const char *file)
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000324{
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 Andersenc265b172001-10-27 03:20:00 +0000349#endif
Erik Andersen3364d782000-03-28 00:58:14 +0000350
Erik Andersen68a9ea42000-04-04 18:39:50 +0000351static int writeFileToTarball(const char *fileName, struct stat *statbuf, void* userData)
Erik Andersen3364d782000-03-28 00:58:14 +0000352{
Erik Andersen68a9ea42000-04-04 18:39:50 +0000353 struct TarBallInfo *tbInfo = (struct TarBallInfo *)userData;
Matt Kraaie80a2632000-12-19 20:45:49 +0000354 const char *header_name;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000355
Eric Andersen3d957c82000-12-07 00:34:58 +0000356 /*
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 Andersen68a9ea42000-04-04 18:39:50 +0000372 /* It is against the rules to archive a socket */
373 if (S_ISSOCK(statbuf->st_mode)) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000374 error_msg("%s: socket ignored", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000375 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 Kraaidd19c692001-01-31 19:00:21 +0000383 error_msg("%s: file is the archive; skipping", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000384 return( TRUE);
385 }
386
Matt Kraaie80a2632000-12-19 20:45:49 +0000387 header_name = fileName;
388 while (header_name[0] == '/') {
Matt Kraaia1f97752000-12-19 06:24:08 +0000389 static int alreadyWarned=FALSE;
390 if (alreadyWarned==FALSE) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000391 error_msg("Removing leading '/' from member names");
Matt Kraaia1f97752000-12-19 06:24:08 +0000392 alreadyWarned=TRUE;
393 }
Matt Kraaie80a2632000-12-19 20:45:49 +0000394 header_name++;
Matt Kraaia1f97752000-12-19 06:24:08 +0000395 }
396
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000397 if (strlen(fileName) >= NAME_SIZE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000398 error_msg(name_longer_than_foo, NAME_SIZE);
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000399 return ( TRUE);
400 }
401
Matt Kraaie80a2632000-12-19 20:45:49 +0000402 if (header_name[0] == '\0')
Matt Kraaia1f97752000-12-19 06:24:08 +0000403 return TRUE;
404
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000405# if defined CONFIG_FEATURE_TAR_EXCLUDE
Matt Kraaibe7499c2001-01-03 17:22:10 +0000406 if (exclude_file(tbInfo->excludeList, header_name)) {
407 return SKIP;
Matt Kraaia1f97752000-12-19 06:24:08 +0000408 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000409# endif //CONFIG_FEATURE_TAR_EXCLUDE
Matt Kraaia1f97752000-12-19 06:24:08 +0000410
Matt Kraaie80a2632000-12-19 20:45:49 +0000411 if (writeTarHeader(tbInfo, header_name, fileName, statbuf)==FALSE) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000412 return( FALSE);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000413 }
Erik Andersen5661fe02000-04-05 01:00:52 +0000414
415 /* Now, if the file is a regular file, copy it out to the tarball */
Eric Andersen3d957c82000-12-07 00:34:58 +0000416 if ((tbInfo->hlInfo == NULL)
417 && (S_ISREG(statbuf->st_mode))) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000418 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 Kraaidd19c692001-01-31 19:00:21 +0000424 error_msg("%s: Cannot open: %s", fileName, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +0000425 return( FALSE);
426 }
427
428 /* write the file to the archive */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000429 while ( (size = full_read(inputFileFd, buffer, sizeof(buffer))) > 0 ) {
430 if (full_write(tbInfo->tarFd, buffer, size) != size ) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000431 /* Output file seems to have a problem */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000432 error_msg(io_error, fileName, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +0000433 return( FALSE);
434 }
435 readSize+=size;
436 }
437 if (size == -1) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000438 error_msg(io_error, fileName, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +0000439 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 Andersen68a9ea42000-04-04 18:39:50 +0000447
448 return( TRUE);
Erik Andersen6acaa402000-03-26 14:03:20 +0000449}
450
Aaron Lehmann8fc5d6d2002-08-21 13:11:34 +0000451extern inline int writeTarFile(const char* tarName, int verboseFlag, char **argv,
Robert Grieblf2f26e72002-07-23 22:05:47 +0000452 char** excludeList, int gzip)
Erik Andersen6acaa402000-03-26 14:03:20 +0000453{
Robert Grieblf2f26e72002-07-23 22:05:47 +0000454#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 Andersen68a9ea42000-04-04 18:39:50 +0000460 int errorFlag=FALSE;
Erik Andersen5661fe02000-04-05 01:00:52 +0000461 ssize_t size;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000462 struct TarBallInfo tbInfo;
Eric Andersen3d957c82000-12-07 00:34:58 +0000463 tbInfo.hlInfoHead = NULL;
Erik Andersen3364d782000-03-28 00:58:14 +0000464
465 /* Make sure there is at least one file to tar up. */
Matt Kraaid8ad76c2000-11-08 02:35:47 +0000466 if (*argv == NULL)
Matt Kraaidd19c692001-01-31 19:00:21 +0000467 error_msg_and_die("Cowardly refusing to create an empty archive");
Erik Andersen6acaa402000-03-26 14:03:20 +0000468
469 /* Open the tar file for writing. */
Robert Grieblf2f26e72002-07-23 22:05:47 +0000470 if (tarName == NULL) {
Erik Andersen68a9ea42000-04-04 18:39:50 +0000471 tbInfo.tarFd = fileno(stdout);
Robert Grieblf2f26e72002-07-23 22:05:47 +0000472 tbInfo.verboseFlag = verboseFlag ? 2 : 0;
473 }
474 else {
Erik Andersen68a9ea42000-04-04 18:39:50 +0000475 tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
Robert Grieblf2f26e72002-07-23 22:05:47 +0000476 tbInfo.verboseFlag = verboseFlag ? 1 : 0;
477 }
478
Erik Andersen68a9ea42000-04-04 18:39:50 +0000479 if (tbInfo.tarFd < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000480 perror_msg( "Error opening '%s'", tarName);
Eric Andersen3d957c82000-12-07 00:34:58 +0000481 freeHardLinkInfo(&tbInfo.hlInfoHead);
Erik Andersen6acaa402000-03-26 14:03:20 +0000482 return ( FALSE);
483 }
Robert Grieblf2f26e72002-07-23 22:05:47 +0000484
Erik Andersen68a9ea42000-04-04 18:39:50 +0000485 /* 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 Whitleyf57c9442000-12-07 19:56:48 +0000488 error_msg_and_die(io_error, tarName, strerror(errno));
Erik Andersen6acaa402000-03-26 14:03:20 +0000489
Robert Grieblf2f26e72002-07-23 22:05:47 +0000490#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 Andersen6acaa402000-03-26 14:03:20 +0000542 /* Read the directory/files and iterate over them one at a time */
Matt Kraaid8ad76c2000-11-08 02:35:47 +0000543 while (*argv != NULL) {
Matt Kraai1f0c4362001-12-20 23:13:26 +0000544 if (! recursive_action(*argv++, TRUE, FALSE, FALSE,
Erik Andersen68a9ea42000-04-04 18:39:50 +0000545 writeFileToTarball, writeFileToTarball,
Matt Kraai1f0c4362001-12-20 23:13:26 +0000546 (void*) &tbInfo)) {
Erik Andersen68a9ea42000-04-04 18:39:50 +0000547 errorFlag = TRUE;
Erik Andersen3364d782000-03-28 00:58:14 +0000548 }
Erik Andersen6acaa402000-03-26 14:03:20 +0000549 }
Erik Andersen5661fe02000-04-05 01:00:52 +0000550 /* 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 Andersen0817d132000-04-09 15:17:40 +0000554
555 /* To be pedantically correct, we would check if the tarball
Eric Andersen3c5ee9a2000-11-14 22:15:48 +0000556 * is smaller than 20 tar blocks, and pad it if it was smaller,
Erik Andersen0817d132000-04-09 15:17:40 +0000557 * but that isn't necessary for GNU tar interoperability, and
558 * so is considered a waste of space */
559
Erik Andersen68a9ea42000-04-04 18:39:50 +0000560 /* Hang up the tools, close up shop, head home */
Robert Grieblf2f26e72002-07-23 22:05:47 +0000561 close(tbInfo.tarFd);
562 if (errorFlag)
Matt Kraaidd19c692001-01-31 19:00:21 +0000563 error_msg("Error exit delayed from previous errors");
Robert Grieblf2f26e72002-07-23 22:05:47 +0000564
Eric Andersen3d957c82000-12-07 00:34:58 +0000565 freeHardLinkInfo(&tbInfo.hlInfoHead);
Robert Grieblf2f26e72002-07-23 22:05:47 +0000566
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 Andersen6acaa402000-03-26 14:03:20 +0000575}
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000576#endif //tar_create
Erik Andersen6acaa402000-03-26 14:03:20 +0000577
Glenn L McGrathd642a672001-10-13 06:54:45 +0000578void 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 McGrath051eee62001-10-13 07:11:03 +0000581 (*list)[*list_count] = xstrdup(new_name);
Glenn L McGrathd642a672001-10-13 06:54:45 +0000582 (*list_count)++;
583 (*list)[*list_count] = NULL;
584}
585
586void append_file_list_to_list(char *filename, char ***name_list, int *num_of_entries)
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000587{
588 FILE *src_stream;
589 char *line;
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000590
591 src_stream = xfopen(filename, "r");
592 while ((line = get_line_from_file(src_stream)) != NULL) {
Matt Kraai39fcb5a2002-01-02 19:01:41 +0000593 chomp (line);
Glenn L McGrathd642a672001-10-13 06:54:45 +0000594 append_file_to_list(line, name_list, num_of_entries);
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000595 free(line);
596 }
597 fclose(src_stream);
598}
Erik Andersen6acaa402000-03-26 14:03:20 +0000599
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000600#ifdef CONFIG_FEATURE_TAR_EXCLUDE
Glenn L McGrath0e766182001-10-13 05:03:29 +0000601/*
602 * Create a list of names that are in the include list AND NOT in the exclude lists
603 */
Aaron Lehmann8fc5d6d2002-08-21 13:11:34 +0000604#if 0 /* this is unused */
Glenn L McGrath0e766182001-10-13 05:03:29 +0000605char **list_and_not_list(char **include_list, char **exclude_list)
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000606{
607 char **new_include_list = NULL;
608 int new_include_count = 0;
609 int include_count = 0;
610 int exclude_count;
Matt Kraaif86bbfa2001-10-12 19:00:15 +0000611
Glenn L McGrath0e766182001-10-13 05:03:29 +0000612 if (include_list == NULL) {
613 return(NULL);
614 }
615
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000616 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 Kraai1f0c4362001-12-20 23:13:26 +0000627 if (! found) {
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000628 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 McGrath2e772ed2001-10-05 02:58:48 +0000631 } else {
632 free(include_list[include_count]);
633 }
634 include_count++;
635 }
Glenn L McGrath0e766182001-10-13 05:03:29 +0000636 new_include_list[new_include_count] = NULL;
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000637 return(new_include_list);
638}
Erik Andersen6acaa402000-03-26 14:03:20 +0000639#endif
Aaron Lehmann8fc5d6d2002-08-21 13:11:34 +0000640#endif
Erik Andersen6acaa402000-03-26 14:03:20 +0000641
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000642int tar_main(int argc, char **argv)
643{
644 enum untar_funct_e {
Matt Kraai2b1effd2001-12-20 22:09:31 +0000645 /* This is optional */
646 untar_unzip = 1,
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000647 /* Require one and only one of these */
Matt Kraai2b1effd2001-12-20 22:09:31 +0000648 untar_list = 2,
649 untar_create = 4,
650 untar_extract = 8
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000651 };
652
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000653 FILE *src_stream = NULL;
654 FILE *uncompressed_stream = NULL;
655 char **include_list = NULL;
Glenn L McGrath4bef7b42001-10-13 19:43:46 +0000656 char **exclude_list = NULL;
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000657 char *src_filename = NULL;
658 char *dst_prefix = NULL;
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000659 int opt;
660 unsigned short untar_funct = 0;
661 unsigned short untar_funct_required = 0;
662 unsigned short extract_function = 0;
Glenn L McGrath0e766182001-10-13 05:03:29 +0000663 int include_list_count = 0;
Eric Andersen9c6b5fc2001-11-17 07:23:46 +0000664#ifdef CONFIG_FEATURE_TAR_EXCLUDE
Glenn L McGrath4bef7b42001-10-13 19:43:46 +0000665 int exclude_list_count = 0;
Eric Andersen9c6b5fc2001-11-17 07:23:46 +0000666#endif
Matt Kraai9fb38f62001-11-12 16:45:23 +0000667#ifdef CONFIG_FEATURE_TAR_GZIP
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000668 int gunzip_pid;
669 int gz_fd = 0;
Matt Kraai9fb38f62001-11-12 16:45:23 +0000670#endif
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000671
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 Andersenbdfd0d72001-10-24 05:00:29 +0000702#ifdef CONFIG_FEATURE_TAR_EXCLUDE
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000703 case 'X':
Glenn L McGrathd642a672001-10-13 06:54:45 +0000704 append_file_list_to_list(optarg, &exclude_list, &exclude_list_count);
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000705 break;
706#endif
707 case 'T':
708 // by default a list is an include list
Glenn L McGrathd642a672001-10-13 06:54:45 +0000709 append_file_list_to_list(optarg, &include_list, &include_list_count);
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000710 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 Kraai2b1effd2001-12-20 22:09:31 +0000718 src_filename = NULL;
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000719 } else {
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000720 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 Grieblf2f26e72002-07-23 22:05:47 +0000729 extract_function |= extract_verbose_list;
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000730 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000731#ifdef CONFIG_FEATURE_TAR_GZIP
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000732 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 McGrathd642a672001-10-13 06:54:45 +0000754 append_file_to_list(argv[optind], &include_list, &include_list_count);
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000755 optind++;
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000756 }
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000757 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 Kraai2b1effd2001-12-20 22:09:31 +0000763 if (src_filename != NULL) {
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000764 src_stream = xfopen(src_filename, "r");
765 } else {
766 src_stream = stdin;
767 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000768#ifdef CONFIG_FEATURE_TAR_GZIP
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000769 /* 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 Andersenbdfd0d72001-10-24 05:00:29 +0000773#endif // CONFIG_FEATURE_TAR_GZIP
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000774 uncompressed_stream = src_stream;
775
776 /* extract or list archive */
Glenn L McGrath4bef7b42001-10-13 19:43:46 +0000777 unarchive(uncompressed_stream, stdout, &get_header_tar, extract_function, dst_prefix, include_list, exclude_list);
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000778 fclose(uncompressed_stream);
779 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000780#ifdef CONFIG_FEATURE_TAR_CREATE
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000781 /* create an archive */
782 else if (untar_funct & untar_create) {
783 int verboseFlag = FALSE;
Robert Grieblf2f26e72002-07-23 22:05:47 +0000784 int gzipFlag = FALSE;
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000785
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000786#ifdef CONFIG_FEATURE_TAR_GZIP
Robert Grieblf2f26e72002-07-23 22:05:47 +0000787 if (untar_funct & untar_unzip)
788 gzipFlag = TRUE;
789
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000790#endif // CONFIG_FEATURE_TAR_GZIP
Robert Grieblf2f26e72002-07-23 22:05:47 +0000791 if (extract_function & extract_verbose_list)
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000792 verboseFlag = TRUE;
Robert Grieblf2f26e72002-07-23 22:05:47 +0000793
794 writeTarFile(src_filename, verboseFlag, include_list, exclude_list, gzipFlag);
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000795 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000796#endif // CONFIG_FEATURE_TAR_CREATE
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000797
798 /* Cleanups */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000799#ifdef CONFIG_FEATURE_TAR_GZIP
Robert Grieblf2f26e72002-07-23 22:05:47 +0000800 if ( !( untar_funct & untar_create ) && ( untar_funct & untar_unzip )) {
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000801 fclose(src_stream);
802 close(gz_fd);
803 gz_close(gunzip_pid);
804 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000805#endif // CONFIG_FEATURE_TAR_GZIP
Matt Kraai31c73af2001-12-20 22:30:14 +0000806#ifdef CONFIG_FEATURE_CLEAN_UP
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000807 if (src_filename) {
808 free(src_filename);
809 }
Matt Kraai31c73af2001-12-20 22:30:14 +0000810#endif
Glenn L McGrath2e772ed2001-10-05 02:58:48 +0000811 return(EXIT_SUCCESS);
812}