blob: 9a3cff361841e56972f13b7aac5d431222df636a [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 *
Erik Andersen61677fe2000-04-13 01:18:56 +00005 * Note, that as of BusyBox-0.43, tar has been completely rewritten from the
6 * ground up. It still has remnents of the old code lying about, but it is
7 * very different now (i.e. cleaner, less global variables, etc)
Eric Andersenc4996011999-10-20 22:08:37 +00008 *
Erik Andersen68a9ea42000-04-04 18:39:50 +00009 * Copyright (C) 2000 by Lineo, inc.
Erik Andersen1ad302a2000-03-24 00:54:46 +000010 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
Eric Andersen96bcfd31999-11-12 01:30:18 +000011 *
Erik Andersen6acaa402000-03-26 14:03:20 +000012 * Based in part in the tar implementation in sash
13 * Copyright (c) 1999 by David I. Bell
14 * Permission is granted to use, distribute, or modify this source,
15 * provided that this copyright notice remains intact.
16 * Permission to distribute sash derived code under the GPL has been granted.
17 *
Erik Andersen68a9ea42000-04-04 18:39:50 +000018 * Based in part on the tar implementation from busybox-0.28
Erik Andersen6acaa402000-03-26 14:03:20 +000019 * Copyright (C) 1995 Bruce Perens
20 * This is free software under the GNU General Public License.
21 *
Eric Andersenc4996011999-10-20 22:08:37 +000022 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 2 of the License, or
25 * (at your option) any later version.
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 * General Public License for more details.
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 *
Eric Andersencc8ed391999-10-05 16:24:54 +000036 */
37
38
Eric Andersen3570a342000-09-25 21:45:58 +000039#include "busybox.h"
Erik Andersen1ad302a2000-03-24 00:54:46 +000040#define BB_DECLARE_EXTERN
41#define bb_need_io_error
Eric Andersen3c5ee9a2000-11-14 22:15:48 +000042#define bb_need_name_longer_than_foo
Erik Andersen1ad302a2000-03-24 00:54:46 +000043#include "messages.c"
Eric Andersencc8ed391999-10-05 16:24:54 +000044#include <stdio.h>
45#include <dirent.h>
46#include <errno.h>
47#include <fcntl.h>
48#include <signal.h>
49#include <time.h>
Erik Andersen7dc16072000-01-04 01:10:25 +000050#include <utime.h>
Eric Andersen96bcfd31999-11-12 01:30:18 +000051#include <sys/types.h>
Eric Andersen08b10341999-11-19 02:38:58 +000052#include <sys/sysmacros.h>
Matt Kraai43c8c382000-09-04 16:51:55 +000053#include <getopt.h>
Matt Kraaibe7499c2001-01-03 17:22:10 +000054#include <fnmatch.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000055
Glenn L McGrath46f44d22000-12-10 01:57:30 +000056#ifdef BB_FEATURE_TAR_GZIP
57extern int unzip(int in, int out);
58extern int gunzip_init();
59#endif
60
Erik Andersen298854f2000-03-23 01:09:18 +000061/* Tar file constants */
Erik Andersen1ad302a2000-03-24 00:54:46 +000062#ifndef MAJOR
63#define MAJOR(dev) (((dev)>>8)&0xff)
64#define MINOR(dev) ((dev)&0xff)
65#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000066
Eric Andersen1b1cfde2000-09-24 00:54:37 +000067#define NAME_SIZE 100
Eric Andersencc8ed391999-10-05 16:24:54 +000068
Erik Andersen298854f2000-03-23 01:09:18 +000069/* POSIX tar Header Block, from POSIX 1003.1-1990 */
70struct TarHeader
71{
72 /* byte offset */
Eric Andersen1b1cfde2000-09-24 00:54:37 +000073 char name[NAME_SIZE]; /* 0-99 */
Erik Andersen0817d132000-04-09 15:17:40 +000074 char mode[8]; /* 100-107 */
75 char uid[8]; /* 108-115 */
76 char gid[8]; /* 116-123 */
77 char size[12]; /* 124-135 */
78 char mtime[12]; /* 136-147 */
79 char chksum[8]; /* 148-155 */
80 char typeflag; /* 156-156 */
Eric Andersen1b1cfde2000-09-24 00:54:37 +000081 char linkname[NAME_SIZE]; /* 157-256 */
Erik Andersen0817d132000-04-09 15:17:40 +000082 char magic[6]; /* 257-262 */
83 char version[2]; /* 263-264 */
84 char uname[32]; /* 265-296 */
85 char gname[32]; /* 297-328 */
86 char devmajor[8]; /* 329-336 */
87 char devminor[8]; /* 337-344 */
88 char prefix[155]; /* 345-499 */
89 char padding[12]; /* 500-512 (pad to exactly the TAR_BLOCK_SIZE) */
Erik Andersen298854f2000-03-23 01:09:18 +000090};
91typedef struct TarHeader TarHeader;
Eric Andersencc8ed391999-10-05 16:24:54 +000092
Eric Andersencc8ed391999-10-05 16:24:54 +000093
Erik Andersen298854f2000-03-23 01:09:18 +000094/* A few useful constants */
95#define TAR_MAGIC "ustar" /* ustar and a null */
Erik Andersen0817d132000-04-09 15:17:40 +000096#define TAR_VERSION " " /* Be compatable with GNU tar format */
Erik Andersen298854f2000-03-23 01:09:18 +000097#define TAR_MAGIC_LEN 6
98#define TAR_VERSION_LEN 2
Erik Andersen298854f2000-03-23 01:09:18 +000099#define TAR_BLOCK_SIZE 512
100
101/* A nice enum with all the possible tar file content types */
102enum TarFileType
103{
104 REGTYPE = '0', /* regular file */
105 REGTYPE0 = '\0', /* regular file (ancient bug compat)*/
106 LNKTYPE = '1', /* hard link */
107 SYMTYPE = '2', /* symbolic link */
108 CHRTYPE = '3', /* character special */
109 BLKTYPE = '4', /* block special */
110 DIRTYPE = '5', /* directory */
111 FIFOTYPE = '6', /* FIFO special */
112 CONTTYPE = '7', /* reserved */
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000113 GNULONGLINK = 'K', /* GNU long (>100 chars) link name */
114 GNULONGNAME = 'L', /* GNU long (>100 chars) file name */
Erik Andersen298854f2000-03-23 01:09:18 +0000115};
116typedef enum TarFileType TarFileType;
117
118/* This struct ignores magic, non-numeric user name,
119 * non-numeric group name, and the checksum, since
120 * these are all ignored by BusyBox tar. */
121struct TarInfo
122{
123 int tarFd; /* An open file descriptor for reading from the tarball */
124 char * name; /* File name */
125 mode_t mode; /* Unix mode, including device bits. */
126 uid_t uid; /* Numeric UID */
127 gid_t gid; /* Numeric GID */
128 size_t size; /* Size of file */
129 time_t mtime; /* Last-modified time */
130 enum TarFileType type; /* Regular, directory, link, etc */
131 char * linkname; /* Name for symbolic and hard links */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000132 long devmajor; /* Major number for special device */
133 long devminor; /* Minor number for special device */
Erik Andersen298854f2000-03-23 01:09:18 +0000134};
135typedef struct TarInfo TarInfo;
136
Erik Andersene454fb62000-03-23 04:27:58 +0000137/* Local procedures to restore files from a tar file. */
Glenn L McGrath7541e3a2001-01-02 23:41:50 +0000138extern int readTarFile(int tarFd, int extractFlag, int listFlag,
Matt Kraaib92223b2000-09-04 08:25:42 +0000139 int tostdoutFlag, int verboseFlag, char** extractList,
140 char** excludeList);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000141
Erik Andersen05100cd2000-01-16 01:30:52 +0000142#ifdef BB_FEATURE_TAR_CREATE
Erik Andersen6acaa402000-03-26 14:03:20 +0000143/* Local procedures to save files into a tar file. */
Matt Kraaid8ad76c2000-11-08 02:35:47 +0000144static int writeTarFile(const char* tarName, int verboseFlag, char **argv,
145 char** excludeList);
Erik Andersen05100cd2000-01-16 01:30:52 +0000146#endif
147
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000148#ifdef BB_FEATURE_TAR_GZIP
149/* Signal handler for when child gzip process dies... */
150void child_died()
151{
152 fflush(stdout);
153 fflush(stderr);
154 exit(EXIT_FAILURE);
155}
156
Glenn L McGrath7541e3a2001-01-02 23:41:50 +0000157extern int tar_unzip_init(int tarFd)
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000158{
159 int child_pid;
160 static int unzip_pipe[2];
161 /* Cope if child dies... Otherwise we block forever in read()... */
162 signal(SIGCHLD, child_died);
163
164 if (pipe(unzip_pipe)!=0)
165 error_msg_and_die("pipe error\n");
166
167 if ( (child_pid = fork()) == -1)
168 error_msg_and_die("fork failure\n");
169
170 if (child_pid==0) {
171 /* child process */
172 gunzip_init();
173 unzip(tarFd, unzip_pipe[1]);
174 exit(EXIT_SUCCESS);
175 }
176 else
177 /* return fd of uncompressed data to parent process */
178 return(unzip_pipe[0]);
179}
180#endif
181
Erik Andersene49d5ec2000-02-08 19:58:47 +0000182extern int tar_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000183{
Erik Andersenecd51242000-04-08 03:08:21 +0000184 char** excludeList=NULL;
Matt Kraaic119ab92000-11-30 04:44:54 +0000185 char** extractList=NULL;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000186 const char *tarName="-";
Erik Andersen0817d132000-04-09 15:17:40 +0000187#if defined BB_FEATURE_TAR_EXCLUDE
Erik Andersenecd51242000-04-08 03:08:21 +0000188 int excludeListSize=0;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000189 char *excludeFileName ="-";
190 FILE *fileList;
191 char file[256];
Erik Andersen0817d132000-04-09 15:17:40 +0000192#endif
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000193#if defined BB_FEATURE_TAR_GZIP
194 int unzipFlag = FALSE;
195#endif
Erik Andersen298854f2000-03-23 01:09:18 +0000196 int listFlag = FALSE;
197 int extractFlag = FALSE;
198 int createFlag = FALSE;
199 int verboseFlag = FALSE;
200 int tostdoutFlag = FALSE;
Eric Andersen02f3b2e2000-12-01 19:04:52 +0000201 int status = FALSE;
202 int firstOpt = TRUE;
Eric Andersen46a98df2000-09-19 21:35:14 +0000203 int stopIt;
Eric Andersencc8ed391999-10-05 16:24:54 +0000204
Erik Andersenecd51242000-04-08 03:08:21 +0000205 if (argc <= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000206 usage(tar_usage);
Eric Andersencc8ed391999-10-05 16:24:54 +0000207
Matt Kraaid8ad76c2000-11-08 02:35:47 +0000208 while (*(++argv) && (**argv == '-' || firstOpt == TRUE)) {
Eric Andersen0102a9f2000-09-23 22:36:24 +0000209 firstOpt=FALSE;
Eric Andersen46a98df2000-09-19 21:35:14 +0000210 stopIt=FALSE;
Matt Kraaid8ad76c2000-11-08 02:35:47 +0000211 while (stopIt==FALSE && **argv) {
212 switch (*((*argv)++)) {
Erik Andersenecd51242000-04-08 03:08:21 +0000213 case 'c':
214 if (extractFlag == TRUE || listFlag == TRUE)
215 goto flagError;
216 createFlag = TRUE;
217 break;
Eric Andersenfdd51032000-08-02 18:48:26 +0000218 case 'x':
219 if (listFlag == TRUE || createFlag == TRUE)
220 goto flagError;
221 extractFlag = TRUE;
222 break;
223 case 't':
224 if (extractFlag == TRUE || createFlag == TRUE)
225 goto flagError;
226 listFlag = TRUE;
227 break;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000228#ifdef BB_FEATURE_TAR_GZIP
229 case 'z':
230 unzipFlag = TRUE;
231 break;
232#endif
Erik Andersenecd51242000-04-08 03:08:21 +0000233 case 'v':
234 verboseFlag = TRUE;
235 break;
Erik Andersenecd51242000-04-08 03:08:21 +0000236 case 'O':
237 tostdoutFlag = TRUE;
Eric Andersenfdd51032000-08-02 18:48:26 +0000238 break;
239 case 'f':
240 if (*tarName != '-')
Mark Whitleyf57c9442000-12-07 19:56:48 +0000241 error_msg_and_die( "Only one 'f' option allowed\n");
Eric Andersen46a98df2000-09-19 21:35:14 +0000242 tarName = *(++argv);
243 if (tarName == NULL)
Mark Whitleyf57c9442000-12-07 19:56:48 +0000244 error_msg_and_die( "Option requires an argument: No file specified\n");
Eric Andersen46a98df2000-09-19 21:35:14 +0000245 stopIt=TRUE;
Erik Andersenecd51242000-04-08 03:08:21 +0000246 break;
Erik Andersen0817d132000-04-09 15:17:40 +0000247#if defined BB_FEATURE_TAR_EXCLUDE
Matt Kraai43c8c382000-09-04 16:51:55 +0000248 case 'e':
Eric Andersen8cede002000-12-04 18:51:09 +0000249 if (strcmp(*argv, "xclude")==0) {
Eric Andersen4836fd42000-12-13 15:28:48 +0000250 excludeList=xrealloc( excludeList,
251 sizeof(char *) * (excludeListSize+2));
Eric Andersen46a98df2000-09-19 21:35:14 +0000252 excludeList[excludeListSize] = *(++argv);
Matt Kraaid8ad76c2000-11-08 02:35:47 +0000253 if (excludeList[excludeListSize] == NULL)
Mark Whitleyf57c9442000-12-07 19:56:48 +0000254 error_msg_and_die( "Option requires an argument: No file specified\n");
Eric Andersen46a98df2000-09-19 21:35:14 +0000255 /* Tack a NULL onto the end of the list */
256 excludeList[++excludeListSize] = NULL;
257 stopIt=TRUE;
258 break;
259 }
Eric Andersen4836fd42000-12-13 15:28:48 +0000260 case 'X':
261 if (*excludeFileName != '-')
262 error_msg_and_die("Only one 'X' option allowed\n");
263 excludeFileName = *(++argv);
264 if (excludeFileName == NULL)
265 error_msg_and_die("Option requires an argument: No file specified\n");
266 fileList = fopen (excludeFileName, "r");
267 if (! fileList)
268 error_msg_and_die("Exclude file: file not found\n");
269 while (fgets(file, sizeof(file), fileList) != NULL) {
270 excludeList = xrealloc(excludeList,
271 sizeof(char *) * (excludeListSize+2));
272 if (file[strlen(file)-1] == '\n')
273 file[strlen(file)-1] = '\0';
274 excludeList[excludeListSize] = xstrdup(file);
Eric Andersen4836fd42000-12-13 15:28:48 +0000275 /* Tack a NULL onto the end of the list */
276 excludeList[++excludeListSize] = NULL;
277 }
278 fclose(fileList);
279 stopIt=TRUE;
280 break;
Erik Andersen0817d132000-04-09 15:17:40 +0000281#endif
Eric Andersen46a98df2000-09-19 21:35:14 +0000282 case '-':
283 break;
Erik Andersenecd51242000-04-08 03:08:21 +0000284 default:
Matt Kraai43c8c382000-09-04 16:51:55 +0000285 usage(tar_usage);
Eric Andersen46a98df2000-09-19 21:35:14 +0000286 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000287 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000288 }
289
Erik Andersene49d5ec2000-02-08 19:58:47 +0000290 /*
291 * Do the correct type of action supplying the rest of the
292 * command line arguments as the list of files to process.
293 */
294 if (createFlag == TRUE) {
Erik Andersende552872000-01-23 01:34:05 +0000295#ifndef BB_FEATURE_TAR_CREATE
Mark Whitleyf57c9442000-12-07 19:56:48 +0000296 error_msg_and_die( "This version of tar was not compiled with tar creation support.\n");
Erik Andersende552872000-01-23 01:34:05 +0000297#else
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000298#ifdef BB_FEATURE_TAR_GZIP
299 if (unzipFlag==TRUE)
300 error_msg_and_die("Creation of compressed not internally support by tar, pipe to busybox gunzip\n");
301#endif
Matt Kraai3e856ce2000-12-01 02:55:13 +0000302 status = writeTarFile(tarName, verboseFlag, argv, excludeList);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000303#endif
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000304 }
305 if (listFlag == TRUE || extractFlag == TRUE) {
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000306 int tarFd;
Matt Kraaic119ab92000-11-30 04:44:54 +0000307 if (*argv)
308 extractList = argv;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000309 /* Open the tar file for reading. */
310 if (!strcmp(tarName, "-"))
311 tarFd = fileno(stdin);
312 else
313 tarFd = open(tarName, O_RDONLY);
314 if (tarFd < 0)
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000315 perror_msg_and_die("Error opening '%s'", tarName);
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000316
317#ifdef BB_FEATURE_TAR_GZIP
318 /* unzip tarFd in a seperate process */
319 if (unzipFlag == TRUE)
320 tarFd = tar_unzip_init(tarFd);
321#endif
322 status = readTarFile(tarFd, extractFlag, listFlag, tostdoutFlag,
Matt Kraai3e856ce2000-12-01 02:55:13 +0000323 verboseFlag, extractList, excludeList);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000324 }
Erik Andersende552872000-01-23 01:34:05 +0000325
Matt Kraai3e856ce2000-12-01 02:55:13 +0000326 if (status == TRUE)
327 return EXIT_SUCCESS;
328 else
329 return EXIT_FAILURE;
330
Erik Andersene49d5ec2000-02-08 19:58:47 +0000331 flagError:
Mark Whitleyf57c9442000-12-07 19:56:48 +0000332 error_msg_and_die( "Exactly one of 'c', 'x' or 't' must be specified\n");
Erik Andersen298854f2000-03-23 01:09:18 +0000333}
334
335static void
Erik Andersen6a34b532000-04-07 06:55:38 +0000336fixUpPermissions(TarInfo *header)
337{
338 struct utimbuf t;
339 /* Now set permissions etc for the new file */
340 chown(header->name, header->uid, header->gid);
341 chmod(header->name, header->mode);
342 /* Reset the time */
343 t.actime = time(0);
344 t.modtime = header->mtime;
345 utime(header->name, &t);
346}
347
348static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000349tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersen298854f2000-03-23 01:09:18 +0000350{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000351 size_t writeSize;
352 size_t readSize;
353 size_t actualWriteSz;
354 char buffer[BUFSIZ];
355 size_t size = header->size;
356 int outFd=fileno(stdout);
357
358 /* Open the file to be written, if a file is supposed to be written */
359 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000360 /* Create the path to the file, just in case it isn't there...
361 * This should not screw up path permissions or anything. */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000362 create_path(header->name, 0777);
Eric Andersen0c6a9702000-06-09 20:51:50 +0000363 if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY,
364 header->mode & ~S_IFMT)) < 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000365 error_msg(io_error, header->name, strerror(errno));
Eric Andersen0c6a9702000-06-09 20:51:50 +0000366 return( FALSE);
367 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000368 }
369
370 /* Write out the file, if we are supposed to be doing that */
371 while ( size > 0 ) {
372 actualWriteSz=0;
373 if ( size > sizeof(buffer) )
374 writeSize = readSize = sizeof(buffer);
375 else {
376 int mod = size % 512;
377 if ( mod != 0 )
378 readSize = size + (512 - mod);
379 else
380 readSize = size;
381 writeSize = size;
382 }
Mark Whitleyf57c9442000-12-07 19:56:48 +0000383 if ( (readSize = full_read(header->tarFd, buffer, readSize)) <= 0 ) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000384 /* Tarball seems to have a problem */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000385 error_msg("Unexpected EOF in archive\n");
Erik Andersen6a34b532000-04-07 06:55:38 +0000386 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000387 }
388 if ( readSize < writeSize )
389 writeSize = readSize;
390
391 /* Write out the file, if we are supposed to be doing that */
392 if (extractFlag==TRUE) {
393
Mark Whitleyf57c9442000-12-07 19:56:48 +0000394 if ((actualWriteSz=full_write(outFd, buffer, writeSize)) != writeSize ) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000395 /* Output file seems to have a problem */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000396 error_msg(io_error, header->name, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000397 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000398 }
Erik Andersen0817d132000-04-09 15:17:40 +0000399 } else {
400 actualWriteSz=writeSize;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000401 }
402
403 size -= actualWriteSz;
404 }
405
406 /* Now we are done writing the file out, so try
407 * and fix up the permissions and whatnot */
408 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000409 close(outFd);
Erik Andersen6a34b532000-04-07 06:55:38 +0000410 fixUpPermissions(header);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000411 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000412 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000413}
414
Erik Andersen6a34b532000-04-07 06:55:38 +0000415static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000416tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000417{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000418
419 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000420 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000421
Mark Whitleyf57c9442000-12-07 19:56:48 +0000422 if (create_path(header->name, header->mode) != TRUE) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000423 perror_msg("%s: Cannot mkdir", header->name);
Erik Andersen6a34b532000-04-07 06:55:38 +0000424 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000425 }
426 /* make the final component, just in case it was
Mark Whitleyf57c9442000-12-07 19:56:48 +0000427 * omitted by create_path() (which will skip the
Erik Andersen1ad302a2000-03-24 00:54:46 +0000428 * directory if it doesn't have a terminating '/') */
Erik Andersen6acaa402000-03-26 14:03:20 +0000429 if (mkdir(header->name, header->mode) == 0) {
430 fixUpPermissions(header);
431 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000432 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000433}
434
Erik Andersen6a34b532000-04-07 06:55:38 +0000435static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000436tarExtractHardLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000437{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000438 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000439 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000440
441 if (link(header->linkname, header->name) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000442 perror_msg("%s: Cannot create hard link to '%s'", header->name,
443 header->linkname);
Erik Andersen6a34b532000-04-07 06:55:38 +0000444 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000445 }
446
447 /* Now set permissions etc for the new directory */
448 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000449 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000450}
451
Erik Andersen6a34b532000-04-07 06:55:38 +0000452static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000453tarExtractSymLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000454{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000455 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000456 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000457
458#ifdef S_ISLNK
459 if (symlink(header->linkname, header->name) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000460 perror_msg("%s: Cannot create symlink to '%s'", header->name,
461 header->linkname);
Erik Andersen6a34b532000-04-07 06:55:38 +0000462 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000463 }
464 /* Try to change ownership of the symlink.
465 * If libs doesn't support that, don't bother.
466 * Changing the pointed-to-file is the Wrong Thing(tm).
467 */
468#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
469 lchown(header->name, header->uid, header->gid);
470#endif
471
472 /* Do not change permissions or date on symlink,
473 * since it changes the pointed to file instead. duh. */
474#else
Mark Whitleyf57c9442000-12-07 19:56:48 +0000475 error_msg("%s: Cannot create symlink to '%s': %s\n",
Erik Andersen6a34b532000-04-07 06:55:38 +0000476 header->name, header->linkname,
477 "symlinks not supported");
Erik Andersen1ad302a2000-03-24 00:54:46 +0000478#endif
Erik Andersen6a34b532000-04-07 06:55:38 +0000479 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000480}
481
Erik Andersen6a34b532000-04-07 06:55:38 +0000482static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000483tarExtractSpecial(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000484{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000485 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000486 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000487
488 if (S_ISCHR(header->mode) || S_ISBLK(header->mode) || S_ISSOCK(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000489 if (mknod(header->name, header->mode, makedev(header->devmajor, header->devminor)) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000490 perror_msg("%s: Cannot mknod", header->name);
Erik Andersen6a34b532000-04-07 06:55:38 +0000491 return( FALSE);
492 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000493 } else if (S_ISFIFO(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000494 if (mkfifo(header->name, header->mode) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000495 perror_msg("%s: Cannot mkfifo", header->name);
Erik Andersen6a34b532000-04-07 06:55:38 +0000496 return( FALSE);
497 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000498 }
499
500 /* Now set permissions etc for the new directory */
501 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000502 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000503}
504
Erik Andersen1ad302a2000-03-24 00:54:46 +0000505/* Read an octal value in a field of the specified width, with optional
Erik Andersen298854f2000-03-23 01:09:18 +0000506 * spaces on both sides of the number and with an optional null character
Erik Andersen1ad302a2000-03-24 00:54:46 +0000507 * at the end. Returns -1 on an illegal format. */
Erik Andersen298854f2000-03-23 01:09:18 +0000508static long getOctal(const char *cp, int size)
Eric Andersencc8ed391999-10-05 16:24:54 +0000509{
Erik Andersen298854f2000-03-23 01:09:18 +0000510 long val = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000511
Erik Andersen298854f2000-03-23 01:09:18 +0000512 for(;(size > 0) && (*cp == ' '); cp++, size--);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000513 if ((size == 0) || !is_octal(*cp))
Erik Andersen298854f2000-03-23 01:09:18 +0000514 return -1;
Mark Whitleyf57c9442000-12-07 19:56:48 +0000515 for(; (size > 0) && is_octal(*cp); size--) {
Erik Andersen298854f2000-03-23 01:09:18 +0000516 val = val * 8 + *cp++ - '0';
Eric Andersencc8ed391999-10-05 16:24:54 +0000517 }
Erik Andersen298854f2000-03-23 01:09:18 +0000518 for (;(size > 0) && (*cp == ' '); cp++, size--);
519 if ((size > 0) && *cp)
520 return -1;
521 return val;
522}
Eric Andersencc8ed391999-10-05 16:24:54 +0000523
Erik Andersen1ad302a2000-03-24 00:54:46 +0000524
Erik Andersen298854f2000-03-23 01:09:18 +0000525/* Parse the tar header and fill in the nice struct with the details */
526static int
Erik Andersen3364d782000-03-28 00:58:14 +0000527readTarHeader(struct TarHeader *rawHeader, struct TarInfo *header)
Erik Andersen298854f2000-03-23 01:09:18 +0000528{
Erik Andersene454fb62000-03-23 04:27:58 +0000529 int i;
Erik Andersen84e09e42000-04-08 20:58:35 +0000530 long chksum, sum=0;
Erik Andersene454fb62000-03-23 04:27:58 +0000531 unsigned char *s = (unsigned char *)rawHeader;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000532
Erik Andersen298854f2000-03-23 01:09:18 +0000533 header->name = rawHeader->name;
Erik Andersen84e09e42000-04-08 20:58:35 +0000534 /* Check for and relativify any absolute paths */
535 if ( *(header->name) == '/' ) {
536 static int alreadyWarned=FALSE;
537
538 while (*(header->name) == '/')
539 ++*(header->name);
540
541 if (alreadyWarned == FALSE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000542 error_msg("Removing leading '/' from member names\n");
Erik Andersen84e09e42000-04-08 20:58:35 +0000543 alreadyWarned = TRUE;
544 }
545 }
546
Erik Andersen298854f2000-03-23 01:09:18 +0000547 header->mode = getOctal(rawHeader->mode, sizeof(rawHeader->mode));
548 header->uid = getOctal(rawHeader->uid, sizeof(rawHeader->uid));
549 header->gid = getOctal(rawHeader->gid, sizeof(rawHeader->gid));
550 header->size = getOctal(rawHeader->size, sizeof(rawHeader->size));
551 header->mtime = getOctal(rawHeader->mtime, sizeof(rawHeader->mtime));
552 chksum = getOctal(rawHeader->chksum, sizeof(rawHeader->chksum));
553 header->type = rawHeader->typeflag;
554 header->linkname = rawHeader->linkname;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000555 header->devmajor = getOctal(rawHeader->devmajor, sizeof(rawHeader->devmajor));
556 header->devminor = getOctal(rawHeader->devminor, sizeof(rawHeader->devminor));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000557
Erik Andersen298854f2000-03-23 01:09:18 +0000558 /* Check the checksum */
Erik Andersen84e09e42000-04-08 20:58:35 +0000559 for (i = sizeof(*rawHeader); i-- != 0;) {
Erik Andersen298854f2000-03-23 01:09:18 +0000560 sum += *s++;
Erik Andersen84e09e42000-04-08 20:58:35 +0000561 }
562 /* Remove the effects of the checksum field (replace
563 * with blanks for the purposes of the checksum) */
564 s = rawHeader->chksum;
565 for (i = sizeof(rawHeader->chksum) ; i-- != 0;) {
566 sum -= *s++;
567 }
568 sum += ' ' * sizeof(rawHeader->chksum);
Erik Andersene454fb62000-03-23 04:27:58 +0000569 if (sum == chksum )
Erik Andersen298854f2000-03-23 01:09:18 +0000570 return ( TRUE);
571 return( FALSE);
572}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000573
Matt Kraaibe7499c2001-01-03 17:22:10 +0000574int exclude_file(char **excluded_files, const char *file)
575{
576 int i;
577
578 if (excluded_files == NULL)
579 return 0;
580
581 for (i = 0; excluded_files[i] != NULL; i++) {
582 if (excluded_files[i][0] == '/') {
583 if (fnmatch(excluded_files[i], file,
584 FNM_PATHNAME | FNM_LEADING_DIR) == 0)
585 return 1;
586 } else {
587 const char *p;
588
589 for (p = file; p[0] != '\0'; p++) {
590 if ((p == file || p[-1] == '/') && p[0] != '/' &&
591 fnmatch(excluded_files[i], p,
592 FNM_PATHNAME | FNM_LEADING_DIR) == 0)
593 return 1;
594 }
595 }
596 }
597
598 return 0;
599}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000600
Erik Andersen1ad302a2000-03-24 00:54:46 +0000601/*
602 * Read a tar file and extract or list the specified files within it.
603 * If the list is empty than all files are extracted or listed.
604 */
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000605extern int readTarFile(int tarFd, int extractFlag, int listFlag,
Matt Kraaib92223b2000-09-04 08:25:42 +0000606 int tostdoutFlag, int verboseFlag, char** extractList,
607 char** excludeList)
Erik Andersen1ad302a2000-03-24 00:54:46 +0000608{
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000609 int status;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000610 int errorFlag=FALSE;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000611 int skipNextHeaderFlag=FALSE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000612 TarHeader rawHeader;
613 TarInfo header;
Erik Andersen0817d132000-04-09 15:17:40 +0000614 char** tmpList;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000615
Erik Andersene49d5ec2000-02-08 19:58:47 +0000616 /* Set the umask for this process so it doesn't
Erik Andersen1ad302a2000-03-24 00:54:46 +0000617 * screw up permission setting for us later. */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000618 umask(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000619
Erik Andersen1ad302a2000-03-24 00:54:46 +0000620 /* Read the tar file, and iterate over it one file at a time */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000621 while ( (status = full_read(tarFd, (char*)&rawHeader, TAR_BLOCK_SIZE)) == TAR_BLOCK_SIZE ) {
Erik Andersene2729152000-02-18 21:34:17 +0000622
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000623 /* Try to read the header */
Erik Andersen3364d782000-03-28 00:58:14 +0000624 if ( readTarHeader(&rawHeader, &header) == FALSE ) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000625 if ( *(header.name) == '\0' ) {
626 goto endgame;
627 } else {
628 errorFlag=TRUE;
Mark Whitleyf57c9442000-12-07 19:56:48 +0000629 error_msg("Bad tar header, skipping\n");
Erik Andersen1ad302a2000-03-24 00:54:46 +0000630 continue;
631 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000632 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000633 if ( *(header.name) == '\0' )
634 goto endgame;
Erik Andersen0817d132000-04-09 15:17:40 +0000635 header.tarFd = tarFd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000636
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000637 /* Skip funky extra GNU headers that precede long files */
638 if ( (header.type == GNULONGNAME) || (header.type == GNULONGLINK) ) {
639 skipNextHeaderFlag=TRUE;
Matt Kraaiab8f9e22000-11-18 01:28:57 +0000640 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
641 errorFlag = TRUE;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000642 continue;
643 }
644 if ( skipNextHeaderFlag == TRUE ) {
645 skipNextHeaderFlag=FALSE;
Mark Whitleyf57c9442000-12-07 19:56:48 +0000646 error_msg(name_longer_than_foo, NAME_SIZE);
Matt Kraaiab8f9e22000-11-18 01:28:57 +0000647 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
648 errorFlag = TRUE;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000649 continue;
650 }
651
Erik Andersen0817d132000-04-09 15:17:40 +0000652#if defined BB_FEATURE_TAR_EXCLUDE
Matt Kraaibe7499c2001-01-03 17:22:10 +0000653 if (exclude_file(excludeList, header.name)) {
Erik Andersen0817d132000-04-09 15:17:40 +0000654 /* There are not the droids you're looking for, move along */
Matt Kraaibe7499c2001-01-03 17:22:10 +0000655 /* If it is a regular file, pretend to extract it with
656 * the extractFlag set to FALSE, so the junk in the tarball
657 * is properly skipped over */
658 if ( header.type==REGTYPE || header.type==REGTYPE0 ) {
659 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
660 errorFlag = TRUE;
661 }
662 continue;
Erik Andersen0817d132000-04-09 15:17:40 +0000663 }
664#endif
Matt Kraaibe7499c2001-01-03 17:22:10 +0000665
Matt Kraaic119ab92000-11-30 04:44:54 +0000666 if (extractList != NULL) {
Matt Kraaib92223b2000-09-04 08:25:42 +0000667 int skipFlag = TRUE;
668 for (tmpList = extractList; *tmpList != NULL; tmpList++) {
669 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
670 header.name[strlen(header.name)-1]=='/'
671 && strncmp( *tmpList, header.name,
672 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
673 /* If it is a regular file, pretend to extract it with
674 * the extractFlag set to FALSE, so the junk in the tarball
675 * is properly skipped over */
676 skipFlag = FALSE;
Matt Kraaic119ab92000-11-30 04:44:54 +0000677 memmove(extractList+1, extractList,
678 sizeof(*extractList)*(tmpList-extractList));
679 extractList++;
Matt Kraaib92223b2000-09-04 08:25:42 +0000680 break;
681 }
682 }
683 /* There are not the droids you're looking for, move along */
684 if (skipFlag == TRUE) {
685 if ( header.type==REGTYPE || header.type==REGTYPE0 )
Matt Kraaiab8f9e22000-11-18 01:28:57 +0000686 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
687 errorFlag = TRUE;
Matt Kraaib92223b2000-09-04 08:25:42 +0000688 continue;
689 }
690 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000691
Matt Kraai82cfbad2000-09-15 21:18:43 +0000692 if (listFlag == TRUE) {
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000693 /* Special treatment if the list (-t) flag is on */
694 if (verboseFlag == TRUE) {
695 int len, len1;
696 char buf[35];
697 struct tm *tm = localtime (&(header.mtime));
698
Mark Whitleyf57c9442000-12-07 19:56:48 +0000699 len=printf("%s ", mode_string(header.mode));
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000700 my_getpwuid(buf, header.uid);
701 if (! *buf)
702 len+=printf("%d", header.uid);
703 else
704 len+=printf("%s", buf);
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000705 my_getgrgid(buf, header.gid);
706 if (! *buf)
707 len+=printf("/%-d ", header.gid);
708 else
709 len+=printf("/%-s ", buf);
710
711 if (header.type==CHRTYPE || header.type==BLKTYPE) {
712 len1=snprintf(buf, sizeof(buf), "%ld,%-ld ",
713 header.devmajor, header.devminor);
714 } else {
715 len1=snprintf(buf, sizeof(buf), "%lu ", (long)header.size);
716 }
717 /* Jump through some hoops to make the columns match up */
718 for(;(len+len1)<31;len++)
719 printf(" ");
720 printf(buf);
721
722 /* Use ISO 8610 time format */
723 if (tm) {
724 printf ("%04d-%02d-%02d %02d:%02d:%02d ",
725 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
726 tm->tm_hour, tm->tm_min, tm->tm_sec);
727 }
728 }
Eric Andersenfdd51032000-08-02 18:48:26 +0000729 printf("%s", header.name);
Matt Kraai82cfbad2000-09-15 21:18:43 +0000730 if (verboseFlag == TRUE) {
731 if (header.type==LNKTYPE) /* If this is a link, say so */
732 printf(" link to %s", header.linkname);
733 else if (header.type==SYMTYPE)
734 printf(" -> %s", header.linkname);
735 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000736 printf("\n");
737 }
738
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000739 /* List contents if we are supposed to do that */
740 if (verboseFlag == TRUE && extractFlag == TRUE) {
741 /* Now the normal listing */
742 FILE *vbFd = stdout;
743 if (tostdoutFlag == TRUE) // If the archive goes to stdout, verbose to stderr
744 vbFd = stderr;
745 fprintf(vbFd, "%s\n", header.name);
746 }
747
Matt Kraaif4462972000-09-01 02:50:48 +0000748 /* Remove files if we would overwrite them */
Matt Kraaida542f32000-09-01 02:53:01 +0000749 if (extractFlag == TRUE && tostdoutFlag == FALSE)
Matt Kraaif4462972000-09-01 02:50:48 +0000750 unlink(header.name);
Eric Andersencc8ed391999-10-05 16:24:54 +0000751
Erik Andersen1ad302a2000-03-24 00:54:46 +0000752 /* If we got here, we can be certain we have a legitimate
753 * header to work with. So work with it. */
754 switch ( header.type ) {
755 case REGTYPE:
756 case REGTYPE0:
757 /* If the name ends in a '/' then assume it is
758 * supposed to be a directory, and fall through */
759 if (header.name[strlen(header.name)-1] != '/') {
Erik Andersen6a34b532000-04-07 06:55:38 +0000760 if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE)
761 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000762 break;
763 }
764 case DIRTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000765 if (tarExtractDirectory( &header, extractFlag, tostdoutFlag)==FALSE)
766 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000767 break;
768 case LNKTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000769 if (tarExtractHardLink( &header, extractFlag, tostdoutFlag)==FALSE)
770 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000771 break;
772 case SYMTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000773 if (tarExtractSymLink( &header, extractFlag, tostdoutFlag)==FALSE)
774 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000775 break;
776 case CHRTYPE:
777 case BLKTYPE:
778 case FIFOTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000779 if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE)
780 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000781 break;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000782#if 0
783 /* Handled earlier */
784 case GNULONGNAME:
785 case GNULONGLINK:
786 skipNextHeaderFlag=TRUE;
787 break;
788#endif
Erik Andersen1ad302a2000-03-24 00:54:46 +0000789 default:
Mark Whitleyf57c9442000-12-07 19:56:48 +0000790 error_msg("Unknown file type '%c' in tar file\n", header.type);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000791 close( tarFd);
792 return( FALSE);
793 }
794 }
795 close(tarFd);
796 if (status > 0) {
797 /* Bummer - we read a partial header */
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000798 perror_msg("Error reading tar file");
Erik Andersen1ad302a2000-03-24 00:54:46 +0000799 return ( FALSE);
800 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000801 else if (errorFlag==TRUE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000802 error_msg( "Error exit delayed from previous errors\n");
Erik Andersen6a34b532000-04-07 06:55:38 +0000803 return( FALSE);
804 } else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000805 return( status);
806
807 /* Stuff to do when we are done */
808endgame:
809 close( tarFd);
Matt Kraaic119ab92000-11-30 04:44:54 +0000810 if (extractList != NULL) {
811 for (; *extractList != NULL; extractList++) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000812 error_msg("%s: Not found in archive\n", *extractList);
Matt Kraaic119ab92000-11-30 04:44:54 +0000813 errorFlag = TRUE;
814 }
815 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000816 if ( *(header.name) == '\0' ) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000817 if (errorFlag==TRUE)
Mark Whitleyf57c9442000-12-07 19:56:48 +0000818 error_msg( "Error exit delayed from previous errors\n");
Erik Andersen6a34b532000-04-07 06:55:38 +0000819 else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000820 return( TRUE);
821 }
822 return( FALSE);
823}
824
Erik Andersen6acaa402000-03-26 14:03:20 +0000825
826#ifdef BB_FEATURE_TAR_CREATE
827
Eric Andersen3d957c82000-12-07 00:34:58 +0000828/*
829** writeTarFile(), writeFileToTarball(), and writeTarHeader() are
830** the only functions that deal with the HardLinkInfo structure.
831** Even these functions use the xxxHardLinkInfo() functions.
832*/
833typedef struct HardLinkInfo HardLinkInfo;
834struct HardLinkInfo
835{
836 HardLinkInfo *next; /* Next entry in list */
837 dev_t dev; /* Device number */
838 ino_t ino; /* Inode number */
839 short linkCount; /* (Hard) Link Count */
840 char name[1]; /* Start of filename (must be last) */
841};
842
Erik Andersen68a9ea42000-04-04 18:39:50 +0000843/* Some info to be carried along when creating a new tarball */
844struct TarBallInfo
845{
846 char* fileName; /* File name of the tarball */
847 int tarFd; /* Open-for-write file descriptor
848 for the tarball */
849 struct stat statBuf; /* Stat info for the tarball, letting
850 us know the inode and device that the
851 tarball lives, so we can avoid trying
852 to include the tarball into itself */
853 int verboseFlag; /* Whether to print extra stuff or not */
Erik Andersenecd51242000-04-08 03:08:21 +0000854 char** excludeList; /* List of files to not include */
Eric Andersen3d957c82000-12-07 00:34:58 +0000855 HardLinkInfo *hlInfoHead; /* Hard Link Tracking Information */
856 HardLinkInfo *hlInfo; /* Hard Link Info for the current file */
Erik Andersen68a9ea42000-04-04 18:39:50 +0000857};
858typedef struct TarBallInfo TarBallInfo;
859
860
Eric Andersen3d957c82000-12-07 00:34:58 +0000861/* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */
862static void
863addHardLinkInfo (HardLinkInfo **hlInfoHeadPtr, dev_t dev, ino_t ino,
864 short linkCount, const char *name)
865{
866 /* Note: hlInfoHeadPtr can never be NULL! */
867 HardLinkInfo *hlInfo;
868
869 hlInfo = (HardLinkInfo *)xmalloc(sizeof(HardLinkInfo)+strlen(name)+1);
870 if (hlInfo) {
871 hlInfo->next = *hlInfoHeadPtr;
872 *hlInfoHeadPtr = hlInfo;
873 hlInfo->dev = dev;
874 hlInfo->ino = ino;
875 hlInfo->linkCount = linkCount;
876 strcpy(hlInfo->name, name);
877 }
878 return;
879}
880
881static void
882freeHardLinkInfo (HardLinkInfo **hlInfoHeadPtr)
883{
884 HardLinkInfo *hlInfo = NULL;
885 HardLinkInfo *hlInfoNext = NULL;
886
887 if (hlInfoHeadPtr) {
888 hlInfo = *hlInfoHeadPtr;
889 while (hlInfo) {
890 hlInfoNext = hlInfo->next;
891 free(hlInfo);
892 hlInfo = hlInfoNext;
893 }
894 *hlInfoHeadPtr = NULL;
895 }
896 return;
897}
898
899/* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */
900static HardLinkInfo *
901findHardLinkInfo (HardLinkInfo *hlInfo, dev_t dev, ino_t ino)
902{
903 while(hlInfo) {
904 if ((ino == hlInfo->ino) && (dev == hlInfo->dev))
905 break;
906 hlInfo = hlInfo->next;
907 }
908 return(hlInfo);
909}
910
Erik Andersen6acaa402000-03-26 14:03:20 +0000911/* Put an octal string into the specified buffer.
912 * The number is zero and space padded and possibly null padded.
913 * Returns TRUE if successful. */
914static int putOctal (char *cp, int len, long value)
915{
916 int tempLength;
Erik Andersen6acaa402000-03-26 14:03:20 +0000917 char tempBuffer[32];
Erik Andersen5661fe02000-04-05 01:00:52 +0000918 char *tempString = tempBuffer;
Erik Andersen6acaa402000-03-26 14:03:20 +0000919
920 /* Create a string of the specified length with an initial space,
921 * leading zeroes and the octal number, and a trailing null. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000922 sprintf (tempString, "%0*lo", len - 1, value);
Erik Andersen6acaa402000-03-26 14:03:20 +0000923
924 /* If the string is too large, suppress the leading space. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000925 tempLength = strlen (tempString) + 1;
Erik Andersen6acaa402000-03-26 14:03:20 +0000926 if (tempLength > len) {
927 tempLength--;
928 tempString++;
929 }
930
931 /* If the string is still too large, suppress the trailing null. */
932 if (tempLength > len)
933 tempLength--;
934
935 /* If the string is still too large, fail. */
936 if (tempLength > len)
937 return FALSE;
938
939 /* Copy the string to the field. */
940 memcpy (cp, tempString, len);
941
942 return TRUE;
943}
944
Erik Andersen68a9ea42000-04-04 18:39:50 +0000945/* Write out a tar header for the specified file/directory/whatever */
Erik Andersen3364d782000-03-28 00:58:14 +0000946static int
Matt Kraaie80a2632000-12-19 20:45:49 +0000947writeTarHeader(struct TarBallInfo *tbInfo, const char *header_name,
948 const char *real_name, struct stat *statbuf)
Erik Andersen6acaa402000-03-26 14:03:20 +0000949{
Erik Andersen5661fe02000-04-05 01:00:52 +0000950 long chksum=0;
951 struct TarHeader header;
952 const unsigned char *cp = (const unsigned char *) &header;
953 ssize_t size = sizeof(struct TarHeader);
Eric Andersenfdd51032000-08-02 18:48:26 +0000954
Erik Andersen5661fe02000-04-05 01:00:52 +0000955 memset( &header, 0, size);
Erik Andersen3364d782000-03-28 00:58:14 +0000956
Matt Kraaie80a2632000-12-19 20:45:49 +0000957 strncpy(header.name, header_name, sizeof(header.name));
Erik Andersenecd51242000-04-08 03:08:21 +0000958
Erik Andersen5661fe02000-04-05 01:00:52 +0000959 putOctal(header.mode, sizeof(header.mode), statbuf->st_mode);
960 putOctal(header.uid, sizeof(header.uid), statbuf->st_uid);
961 putOctal(header.gid, sizeof(header.gid), statbuf->st_gid);
962 putOctal(header.size, sizeof(header.size), 0); /* Regular file size is handled later */
963 putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime);
964 strncpy(header.magic, TAR_MAGIC TAR_VERSION,
965 TAR_MAGIC_LEN + TAR_VERSION_LEN );
Erik Andersen68a9ea42000-04-04 18:39:50 +0000966
Erik Andersen84e09e42000-04-08 20:58:35 +0000967 /* Enter the user and group names (default to root if it fails) */
Erik Andersen5661fe02000-04-05 01:00:52 +0000968 my_getpwuid(header.uname, statbuf->st_uid);
Erik Andersen5661fe02000-04-05 01:00:52 +0000969 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000970 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +0000971 my_getgrgid(header.gname, statbuf->st_gid);
972 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000973 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +0000974
Eric Andersen3d957c82000-12-07 00:34:58 +0000975 if (tbInfo->hlInfo) {
976 /* This is a hard link */
977 header.typeflag = LNKTYPE;
978 strncpy(header.linkname, tbInfo->hlInfo->name, sizeof(header.linkname));
979 } else if (S_ISLNK(statbuf->st_mode)) {
Eric Andersen3adffb72000-06-26 10:54:06 +0000980 int link_size=0;
Erik Andersen5661fe02000-04-05 01:00:52 +0000981 char buffer[BUFSIZ];
982 header.typeflag = SYMTYPE;
Matt Kraaie80a2632000-12-19 20:45:49 +0000983 link_size = readlink(real_name, buffer, sizeof(buffer) - 1);
Eric Andersen3adffb72000-06-26 10:54:06 +0000984 if ( link_size < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000985 perror_msg("Error reading symlink '%s'", header.name);
Erik Andersen5661fe02000-04-05 01:00:52 +0000986 return ( FALSE);
987 }
Eric Andersen3adffb72000-06-26 10:54:06 +0000988 buffer[link_size] = '\0';
Erik Andersen95c1c1e2000-04-14 21:45:29 +0000989 strncpy(header.linkname, buffer, sizeof(header.linkname));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000990 } else if (S_ISDIR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000991 header.typeflag = DIRTYPE;
992 strncat(header.name, "/", sizeof(header.name));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000993 } else if (S_ISCHR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000994 header.typeflag = CHRTYPE;
995 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
996 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000997 } else if (S_ISBLK(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000998 header.typeflag = BLKTYPE;
999 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
1000 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +00001001 } else if (S_ISFIFO(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +00001002 header.typeflag = FIFOTYPE;
1003 } else if (S_ISREG(statbuf->st_mode)) {
1004 header.typeflag = REGTYPE;
1005 putOctal(header.size, sizeof(header.size), statbuf->st_size);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001006 } else {
Matt Kraaie80a2632000-12-19 20:45:49 +00001007 error_msg("%s: Unknown file type\n", real_name);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001008 return ( FALSE);
1009 }
Erik Andersen68a9ea42000-04-04 18:39:50 +00001010
Erik Andersen5661fe02000-04-05 01:00:52 +00001011 /* Calculate and store the checksum (i.e. the sum of all of the bytes of
1012 * the header). The checksum field must be filled with blanks for the
1013 * calculation. The checksum field is formatted differently from the
1014 * other fields: it has [6] digits, a null, then a space -- rather than
1015 * digits, followed by a null like the other fields... */
1016 memset(header.chksum, ' ', sizeof(header.chksum));
1017 cp = (const unsigned char *) &header;
1018 while (size-- > 0)
1019 chksum += *cp++;
1020 putOctal(header.chksum, 7, chksum);
1021
1022 /* Now write the header out to disk */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001023 if ((size=full_write(tbInfo->tarFd, (char*)&header, sizeof(struct TarHeader))) < 0) {
Matt Kraaie80a2632000-12-19 20:45:49 +00001024 error_msg(io_error, real_name, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +00001025 return ( FALSE);
1026 }
1027 /* Pad the header up to the tar block size */
1028 for (; size<TAR_BLOCK_SIZE; size++) {
1029 write(tbInfo->tarFd, "\0", 1);
1030 }
1031 /* Now do the verbose thing (or not) */
Eric Andersenfdd51032000-08-02 18:48:26 +00001032 if (tbInfo->verboseFlag==TRUE) {
1033 FILE *vbFd = stdout;
1034 if (tbInfo->tarFd == fileno(stdout)) // If the archive goes to stdout, verbose to stderr
1035 vbFd = stderr;
1036 fprintf(vbFd, "%s\n", header.name);
1037 }
Erik Andersen3364d782000-03-28 00:58:14 +00001038
1039 return ( TRUE);
1040}
1041
1042
Erik Andersen68a9ea42000-04-04 18:39:50 +00001043static int writeFileToTarball(const char *fileName, struct stat *statbuf, void* userData)
Erik Andersen3364d782000-03-28 00:58:14 +00001044{
Erik Andersen68a9ea42000-04-04 18:39:50 +00001045 struct TarBallInfo *tbInfo = (struct TarBallInfo *)userData;
Matt Kraaie80a2632000-12-19 20:45:49 +00001046 const char *header_name;
Erik Andersen68a9ea42000-04-04 18:39:50 +00001047
Eric Andersen3d957c82000-12-07 00:34:58 +00001048 /*
1049 ** Check to see if we are dealing with a hard link.
1050 ** If so -
1051 ** Treat the first occurance of a given dev/inode as a file while
1052 ** treating any additional occurances as hard links. This is done
1053 ** by adding the file information to the HardLinkInfo linked list.
1054 */
1055 tbInfo->hlInfo = NULL;
1056 if (statbuf->st_nlink > 1) {
1057 tbInfo->hlInfo = findHardLinkInfo(tbInfo->hlInfoHead, statbuf->st_dev,
1058 statbuf->st_ino);
1059 if (tbInfo->hlInfo == NULL)
1060 addHardLinkInfo (&tbInfo->hlInfoHead, statbuf->st_dev,
1061 statbuf->st_ino, statbuf->st_nlink, fileName);
1062 }
1063
Erik Andersen68a9ea42000-04-04 18:39:50 +00001064 /* It is against the rules to archive a socket */
1065 if (S_ISSOCK(statbuf->st_mode)) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001066 error_msg("%s: socket ignored\n", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001067 return( TRUE);
1068 }
1069
1070 /* It is a bad idea to store the archive we are in the process of creating,
1071 * so check the device and inode to be sure that this particular file isn't
1072 * the new tarball */
1073 if (tbInfo->statBuf.st_dev == statbuf->st_dev &&
1074 tbInfo->statBuf.st_ino == statbuf->st_ino) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001075 error_msg("%s: file is the archive; skipping\n", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001076 return( TRUE);
1077 }
1078
Matt Kraaie80a2632000-12-19 20:45:49 +00001079 header_name = fileName;
1080 while (header_name[0] == '/') {
Matt Kraaia1f97752000-12-19 06:24:08 +00001081 static int alreadyWarned=FALSE;
1082 if (alreadyWarned==FALSE) {
1083 error_msg("Removing leading '/' from member names\n");
1084 alreadyWarned=TRUE;
1085 }
Matt Kraaie80a2632000-12-19 20:45:49 +00001086 header_name++;
Matt Kraaia1f97752000-12-19 06:24:08 +00001087 }
1088
Eric Andersen1b1cfde2000-09-24 00:54:37 +00001089 if (strlen(fileName) >= NAME_SIZE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001090 error_msg(name_longer_than_foo, NAME_SIZE);
Eric Andersen1b1cfde2000-09-24 00:54:37 +00001091 return ( TRUE);
1092 }
1093
Matt Kraaie80a2632000-12-19 20:45:49 +00001094 if (header_name[0] == '\0')
Matt Kraaia1f97752000-12-19 06:24:08 +00001095 return TRUE;
1096
1097#if defined BB_FEATURE_TAR_EXCLUDE
Matt Kraaibe7499c2001-01-03 17:22:10 +00001098 if (exclude_file(tbInfo->excludeList, header_name)) {
1099 return SKIP;
Matt Kraaia1f97752000-12-19 06:24:08 +00001100 }
1101#endif
1102
Matt Kraaie80a2632000-12-19 20:45:49 +00001103 if (writeTarHeader(tbInfo, header_name, fileName, statbuf)==FALSE) {
Erik Andersen5661fe02000-04-05 01:00:52 +00001104 return( FALSE);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001105 }
Erik Andersen5661fe02000-04-05 01:00:52 +00001106
1107 /* Now, if the file is a regular file, copy it out to the tarball */
Eric Andersen3d957c82000-12-07 00:34:58 +00001108 if ((tbInfo->hlInfo == NULL)
1109 && (S_ISREG(statbuf->st_mode))) {
Erik Andersen5661fe02000-04-05 01:00:52 +00001110 int inputFileFd;
1111 char buffer[BUFSIZ];
1112 ssize_t size=0, readSize=0;
1113
1114 /* open the file we want to archive, and make sure all is well */
1115 if ((inputFileFd = open(fileName, O_RDONLY)) < 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001116 error_msg("%s: Cannot open: %s\n", fileName, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +00001117 return( FALSE);
1118 }
1119
1120 /* write the file to the archive */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001121 while ( (size = full_read(inputFileFd, buffer, sizeof(buffer))) > 0 ) {
1122 if (full_write(tbInfo->tarFd, buffer, size) != size ) {
Erik Andersen5661fe02000-04-05 01:00:52 +00001123 /* Output file seems to have a problem */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001124 error_msg(io_error, fileName, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +00001125 return( FALSE);
1126 }
1127 readSize+=size;
1128 }
1129 if (size == -1) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001130 error_msg(io_error, fileName, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +00001131 return( FALSE);
1132 }
1133 /* Pad the file up to the tar block size */
1134 for (; (readSize%TAR_BLOCK_SIZE) != 0; readSize++) {
1135 write(tbInfo->tarFd, "\0", 1);
1136 }
1137 close( inputFileFd);
1138 }
Erik Andersen68a9ea42000-04-04 18:39:50 +00001139
1140 return( TRUE);
Erik Andersen6acaa402000-03-26 14:03:20 +00001141}
1142
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001143static int writeTarFile(const char* tarName, int verboseFlag, char **argv,
1144 char** excludeList)
Erik Andersen6acaa402000-03-26 14:03:20 +00001145{
Erik Andersen3364d782000-03-28 00:58:14 +00001146 int tarFd=-1;
Erik Andersen68a9ea42000-04-04 18:39:50 +00001147 int errorFlag=FALSE;
Erik Andersen5661fe02000-04-05 01:00:52 +00001148 ssize_t size;
Erik Andersen68a9ea42000-04-04 18:39:50 +00001149 struct TarBallInfo tbInfo;
1150 tbInfo.verboseFlag = verboseFlag;
Eric Andersen3d957c82000-12-07 00:34:58 +00001151 tbInfo.hlInfoHead = NULL;
Erik Andersen3364d782000-03-28 00:58:14 +00001152
1153 /* Make sure there is at least one file to tar up. */
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001154 if (*argv == NULL)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001155 error_msg_and_die("Cowardly refusing to create an empty archive\n");
Erik Andersen6acaa402000-03-26 14:03:20 +00001156
1157 /* Open the tar file for writing. */
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001158 if (!strcmp(tarName, "-"))
Erik Andersen68a9ea42000-04-04 18:39:50 +00001159 tbInfo.tarFd = fileno(stdout);
Erik Andersen6acaa402000-03-26 14:03:20 +00001160 else
Erik Andersen68a9ea42000-04-04 18:39:50 +00001161 tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1162 if (tbInfo.tarFd < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +00001163 perror_msg( "Error opening '%s'", tarName);
Eric Andersen3d957c82000-12-07 00:34:58 +00001164 freeHardLinkInfo(&tbInfo.hlInfoHead);
Erik Andersen6acaa402000-03-26 14:03:20 +00001165 return ( FALSE);
1166 }
Erik Andersenecd51242000-04-08 03:08:21 +00001167 tbInfo.excludeList=excludeList;
Erik Andersen68a9ea42000-04-04 18:39:50 +00001168 /* Store the stat info for the tarball's file, so
1169 * can avoid including the tarball into itself.... */
1170 if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001171 error_msg_and_die(io_error, tarName, strerror(errno));
Erik Andersen6acaa402000-03-26 14:03:20 +00001172
1173 /* Set the umask for this process so it doesn't
1174 * screw up permission setting for us later. */
1175 umask(0);
1176
1177 /* Read the directory/files and iterate over them one at a time */
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001178 while (*argv != NULL) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001179 if (recursive_action(*argv++, TRUE, FALSE, FALSE,
Erik Andersen68a9ea42000-04-04 18:39:50 +00001180 writeFileToTarball, writeFileToTarball,
1181 (void*) &tbInfo) == FALSE) {
1182 errorFlag = TRUE;
Erik Andersen3364d782000-03-28 00:58:14 +00001183 }
Erik Andersen6acaa402000-03-26 14:03:20 +00001184 }
Erik Andersen5661fe02000-04-05 01:00:52 +00001185 /* Write two empty blocks to the end of the archive */
1186 for (size=0; size<(2*TAR_BLOCK_SIZE); size++) {
1187 write(tbInfo.tarFd, "\0", 1);
1188 }
Erik Andersen0817d132000-04-09 15:17:40 +00001189
1190 /* To be pedantically correct, we would check if the tarball
Eric Andersen3c5ee9a2000-11-14 22:15:48 +00001191 * is smaller than 20 tar blocks, and pad it if it was smaller,
Erik Andersen0817d132000-04-09 15:17:40 +00001192 * but that isn't necessary for GNU tar interoperability, and
1193 * so is considered a waste of space */
1194
Erik Andersen68a9ea42000-04-04 18:39:50 +00001195 /* Hang up the tools, close up shop, head home */
Erik Andersen6acaa402000-03-26 14:03:20 +00001196 close(tarFd);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001197 if (errorFlag == TRUE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001198 error_msg("Error exit delayed from previous errors\n");
Eric Andersen3d957c82000-12-07 00:34:58 +00001199 freeHardLinkInfo(&tbInfo.hlInfoHead);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001200 return(FALSE);
1201 }
Eric Andersen3d957c82000-12-07 00:34:58 +00001202 freeHardLinkInfo(&tbInfo.hlInfoHead);
Erik Andersen6acaa402000-03-26 14:03:20 +00001203 return( TRUE);
1204}
1205
1206
1207#endif
1208