blob: fb0fcc614dce0f987db583c943c99104d315ee2c [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 *
Eric Andersen8ec10a92001-01-27 09:33:39 +00009 * Copyright (C) 1999,2000,2001 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 Andersened3ef502001-01-27 08:24:39 +000055#include <string.h>
56#include <stdlib.h>
57#include <unistd.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000058
Glenn L McGrath46f44d22000-12-10 01:57:30 +000059#ifdef BB_FEATURE_TAR_GZIP
60extern int unzip(int in, int out);
61extern int gunzip_init();
62#endif
63
Erik Andersen298854f2000-03-23 01:09:18 +000064/* Tar file constants */
Erik Andersen1ad302a2000-03-24 00:54:46 +000065#ifndef MAJOR
66#define MAJOR(dev) (((dev)>>8)&0xff)
67#define MINOR(dev) ((dev)&0xff)
68#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000069
Mark Whitley59ab0252001-01-23 22:30:04 +000070enum { NAME_SIZE = 100 }; /* because gcc won't let me use 'static const int' */
Eric Andersencc8ed391999-10-05 16:24:54 +000071
Erik Andersen298854f2000-03-23 01:09:18 +000072/* POSIX tar Header Block, from POSIX 1003.1-1990 */
73struct TarHeader
74{
75 /* byte offset */
Eric Andersen1b1cfde2000-09-24 00:54:37 +000076 char name[NAME_SIZE]; /* 0-99 */
Erik Andersen0817d132000-04-09 15:17:40 +000077 char mode[8]; /* 100-107 */
78 char uid[8]; /* 108-115 */
79 char gid[8]; /* 116-123 */
80 char size[12]; /* 124-135 */
81 char mtime[12]; /* 136-147 */
82 char chksum[8]; /* 148-155 */
83 char typeflag; /* 156-156 */
Eric Andersen1b1cfde2000-09-24 00:54:37 +000084 char linkname[NAME_SIZE]; /* 157-256 */
Erik Andersen0817d132000-04-09 15:17:40 +000085 char magic[6]; /* 257-262 */
86 char version[2]; /* 263-264 */
87 char uname[32]; /* 265-296 */
88 char gname[32]; /* 297-328 */
89 char devmajor[8]; /* 329-336 */
90 char devminor[8]; /* 337-344 */
91 char prefix[155]; /* 345-499 */
92 char padding[12]; /* 500-512 (pad to exactly the TAR_BLOCK_SIZE) */
Erik Andersen298854f2000-03-23 01:09:18 +000093};
94typedef struct TarHeader TarHeader;
Eric Andersencc8ed391999-10-05 16:24:54 +000095
Eric Andersencc8ed391999-10-05 16:24:54 +000096
Erik Andersen298854f2000-03-23 01:09:18 +000097/* A few useful constants */
98#define TAR_MAGIC "ustar" /* ustar and a null */
Erik Andersen0817d132000-04-09 15:17:40 +000099#define TAR_VERSION " " /* Be compatable with GNU tar format */
Mark Whitley59ab0252001-01-23 22:30:04 +0000100static const int TAR_MAGIC_LEN = 6;
101static const int TAR_VERSION_LEN = 2;
102static const int TAR_BLOCK_SIZE = 512;
Erik Andersen298854f2000-03-23 01:09:18 +0000103
104/* A nice enum with all the possible tar file content types */
105enum TarFileType
106{
107 REGTYPE = '0', /* regular file */
108 REGTYPE0 = '\0', /* regular file (ancient bug compat)*/
109 LNKTYPE = '1', /* hard link */
110 SYMTYPE = '2', /* symbolic link */
111 CHRTYPE = '3', /* character special */
112 BLKTYPE = '4', /* block special */
113 DIRTYPE = '5', /* directory */
114 FIFOTYPE = '6', /* FIFO special */
115 CONTTYPE = '7', /* reserved */
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000116 GNULONGLINK = 'K', /* GNU long (>100 chars) link name */
117 GNULONGNAME = 'L', /* GNU long (>100 chars) file name */
Erik Andersen298854f2000-03-23 01:09:18 +0000118};
119typedef enum TarFileType TarFileType;
120
121/* This struct ignores magic, non-numeric user name,
122 * non-numeric group name, and the checksum, since
123 * these are all ignored by BusyBox tar. */
124struct TarInfo
125{
126 int tarFd; /* An open file descriptor for reading from the tarball */
127 char * name; /* File name */
128 mode_t mode; /* Unix mode, including device bits. */
129 uid_t uid; /* Numeric UID */
130 gid_t gid; /* Numeric GID */
131 size_t size; /* Size of file */
132 time_t mtime; /* Last-modified time */
133 enum TarFileType type; /* Regular, directory, link, etc */
134 char * linkname; /* Name for symbolic and hard links */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000135 long devmajor; /* Major number for special device */
136 long devminor; /* Minor number for special device */
Erik Andersen298854f2000-03-23 01:09:18 +0000137};
138typedef struct TarInfo TarInfo;
139
Erik Andersene454fb62000-03-23 04:27:58 +0000140/* Local procedures to restore files from a tar file. */
Glenn L McGrath7541e3a2001-01-02 23:41:50 +0000141extern int readTarFile(int tarFd, int extractFlag, int listFlag,
Matt Kraaib92223b2000-09-04 08:25:42 +0000142 int tostdoutFlag, int verboseFlag, char** extractList,
143 char** excludeList);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000144
Erik Andersen05100cd2000-01-16 01:30:52 +0000145#ifdef BB_FEATURE_TAR_CREATE
Erik Andersen6acaa402000-03-26 14:03:20 +0000146/* Local procedures to save files into a tar file. */
Matt Kraaid8ad76c2000-11-08 02:35:47 +0000147static int writeTarFile(const char* tarName, int verboseFlag, char **argv,
148 char** excludeList);
Erik Andersen05100cd2000-01-16 01:30:52 +0000149#endif
150
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000151#ifdef BB_FEATURE_TAR_GZIP
152/* Signal handler for when child gzip process dies... */
153void child_died()
154{
155 fflush(stdout);
156 fflush(stderr);
157 exit(EXIT_FAILURE);
158}
159
Glenn L McGrath7541e3a2001-01-02 23:41:50 +0000160extern int tar_unzip_init(int tarFd)
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000161{
162 int child_pid;
163 static int unzip_pipe[2];
164 /* Cope if child dies... Otherwise we block forever in read()... */
165 signal(SIGCHLD, child_died);
166
167 if (pipe(unzip_pipe)!=0)
Matt Kraaidd19c692001-01-31 19:00:21 +0000168 error_msg_and_die("pipe error");
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000169
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000170 if ( (child_pid = fork()) == -1)
Matt Kraaidd19c692001-01-31 19:00:21 +0000171 error_msg_and_die("fork failure");
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000172
173 if (child_pid==0) {
174 /* child process */
Glenn L McGrath1d269432001-01-20 00:12:21 +0000175 close(unzip_pipe[0]);
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000176 gunzip_init();
177 unzip(tarFd, unzip_pipe[1]);
178 exit(EXIT_SUCCESS);
179 }
Glenn L McGrath1d269432001-01-20 00:12:21 +0000180 else {
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000181 /* return fd of uncompressed data to parent process */
Glenn L McGrath1d269432001-01-20 00:12:21 +0000182 close(unzip_pipe[1]);
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000183 return(unzip_pipe[0]);
Glenn L McGrath1d269432001-01-20 00:12:21 +0000184 }
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000185}
186#endif
187
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000188#if defined BB_FEATURE_TAR_EXCLUDE
189struct option longopts[] = {
190 { "exclude", 1, NULL, 'e' },
191 { NULL, 0, NULL, 0 }
192};
193#endif
194
Erik Andersene49d5ec2000-02-08 19:58:47 +0000195extern int tar_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000196{
Erik Andersenecd51242000-04-08 03:08:21 +0000197 char** excludeList=NULL;
Matt Kraaic119ab92000-11-30 04:44:54 +0000198 char** extractList=NULL;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000199 const char *tarName="-";
Erik Andersen0817d132000-04-09 15:17:40 +0000200#if defined BB_FEATURE_TAR_EXCLUDE
Erik Andersenecd51242000-04-08 03:08:21 +0000201 int excludeListSize=0;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000202 FILE *fileList;
203 char file[256];
Erik Andersen0817d132000-04-09 15:17:40 +0000204#endif
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000205#if defined BB_FEATURE_TAR_GZIP
206 int unzipFlag = FALSE;
207#endif
Erik Andersen298854f2000-03-23 01:09:18 +0000208 int listFlag = FALSE;
209 int extractFlag = FALSE;
210 int createFlag = FALSE;
211 int verboseFlag = FALSE;
212 int tostdoutFlag = FALSE;
Eric Andersen02f3b2e2000-12-01 19:04:52 +0000213 int status = FALSE;
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000214 int opt;
Eric Andersencc8ed391999-10-05 16:24:54 +0000215
Erik Andersenecd51242000-04-08 03:08:21 +0000216 if (argc <= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000217 usage(tar_usage);
Eric Andersencc8ed391999-10-05 16:24:54 +0000218
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000219 if (argv[1][0] != '-') {
220 char *tmp = xmalloc(strlen(argv[1]) + 2);
221 tmp[0] = '-';
222 strcpy(tmp + 1, argv[1]);
223 argv[1] = tmp;
224 }
225
226 while (
227#ifndef BB_FEATURE_TAR_EXCLUDE
228 (opt = getopt(argc, argv, "cxtzvOf:"))
229#else
230 (opt = getopt_long(argc, argv, "cxtzvOf:X:", longopts, NULL))
231#endif
232 > 0) {
233 switch (opt) {
234 case 'c':
235 if (extractFlag == TRUE || listFlag == TRUE)
236 goto flagError;
237 createFlag = TRUE;
238 break;
239 case 'x':
240 if (listFlag == TRUE || createFlag == TRUE)
241 goto flagError;
242 extractFlag = TRUE;
243 break;
244 case 't':
245 if (extractFlag == TRUE || createFlag == TRUE)
246 goto flagError;
247 listFlag = TRUE;
248 break;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000249#ifdef BB_FEATURE_TAR_GZIP
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000250 case 'z':
251 unzipFlag = TRUE;
252 break;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000253#endif
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000254 case 'v':
255 verboseFlag = TRUE;
256 break;
257 case 'O':
258 tostdoutFlag = TRUE;
259 break;
260 case 'f':
261 if (*tarName != '-')
Matt Kraaidd19c692001-01-31 19:00:21 +0000262 error_msg_and_die( "Only one 'f' option allowed");
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000263 tarName = optarg;
264 break;
Erik Andersen0817d132000-04-09 15:17:40 +0000265#if defined BB_FEATURE_TAR_EXCLUDE
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000266 case 'e':
267 excludeList=xrealloc( excludeList,
268 sizeof(char *) * (excludeListSize+2));
269 excludeList[excludeListSize] = optarg;
270 /* Tack a NULL onto the end of the list */
271 excludeList[++excludeListSize] = NULL;
272 case 'X':
273 fileList = xfopen(optarg, "r");
274 while (fgets(file, sizeof(file), fileList) != NULL) {
275 excludeList = xrealloc(excludeList,
276 sizeof(char *) * (excludeListSize+2));
Matt Kraai05e782d2001-02-01 16:49:30 +0000277 chomp(file);
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000278 excludeList[excludeListSize] = xstrdup(file);
279 /* Tack a NULL onto the end of the list */
280 excludeList[++excludeListSize] = NULL;
281 }
282 fclose(fileList);
283 break;
Erik Andersen0817d132000-04-09 15:17:40 +0000284#endif
Erik Andersenecd51242000-04-08 03:08:21 +0000285 default:
Matt Kraai43c8c382000-09-04 16:51:55 +0000286 usage(tar_usage);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000287 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000288 }
289
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000290 /*
Erik Andersene49d5ec2000-02-08 19:58:47 +0000291 * 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
Matt Kraaidd19c692001-01-31 19:00:21 +0000296 error_msg_and_die( "This version of tar was not compiled with tar creation support.");
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)
Matt Kraaidd19c692001-01-31 19:00:21 +0000300 error_msg_and_die("Creation of compressed not internally support by tar, pipe to busybox gunzip");
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000301#endif
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000302 status = writeTarFile(tarName, verboseFlag, argv + optind, 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 Kraai3b3f5c32001-01-22 20:49:00 +0000307 if (argv[optind])
308 extractList = argv + optind;
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:
Matt Kraaidd19c692001-01-31 19:00:21 +0000332 error_msg_and_die( "Exactly one of 'c', 'x' or 't' must be specified");
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 */
Matt Kraaidd19c692001-01-31 19:00:21 +0000385 error_msg("Unexpected EOF in archive");
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 '/') */
Matt Kraai541ffe32001-01-13 21:46:25 +0000429 if (mkdir(header->name, header->mode) < 0 && errno != EEXIST) {
430 perror_msg("%s", header->name);
431 return FALSE;
Erik Andersen6acaa402000-03-26 14:03:20 +0000432 }
Matt Kraai541ffe32001-01-13 21:46:25 +0000433
434 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000435 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000436}
437
Erik Andersen6a34b532000-04-07 06:55:38 +0000438static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000439tarExtractHardLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000440{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000441 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000442 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000443
444 if (link(header->linkname, header->name) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000445 perror_msg("%s: Cannot create hard link to '%s'", header->name,
446 header->linkname);
Erik Andersen6a34b532000-04-07 06:55:38 +0000447 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000448 }
449
450 /* Now set permissions etc for the new directory */
451 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000452 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000453}
454
Erik Andersen6a34b532000-04-07 06:55:38 +0000455static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000456tarExtractSymLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000457{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000458 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000459 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000460
461#ifdef S_ISLNK
462 if (symlink(header->linkname, header->name) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000463 perror_msg("%s: Cannot create symlink to '%s'", header->name,
464 header->linkname);
Erik Andersen6a34b532000-04-07 06:55:38 +0000465 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000466 }
467 /* Try to change ownership of the symlink.
468 * If libs doesn't support that, don't bother.
469 * Changing the pointed-to-file is the Wrong Thing(tm).
470 */
471#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
472 lchown(header->name, header->uid, header->gid);
473#endif
474
475 /* Do not change permissions or date on symlink,
476 * since it changes the pointed to file instead. duh. */
477#else
Matt Kraaidd19c692001-01-31 19:00:21 +0000478 error_msg("%s: Cannot create symlink to '%s': %s",
Erik Andersen6a34b532000-04-07 06:55:38 +0000479 header->name, header->linkname,
480 "symlinks not supported");
Erik Andersen1ad302a2000-03-24 00:54:46 +0000481#endif
Erik Andersen6a34b532000-04-07 06:55:38 +0000482 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000483}
484
Erik Andersen6a34b532000-04-07 06:55:38 +0000485static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000486tarExtractSpecial(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000487{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000488 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000489 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000490
491 if (S_ISCHR(header->mode) || S_ISBLK(header->mode) || S_ISSOCK(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000492 if (mknod(header->name, header->mode, makedev(header->devmajor, header->devminor)) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000493 perror_msg("%s: Cannot mknod", header->name);
Erik Andersen6a34b532000-04-07 06:55:38 +0000494 return( FALSE);
495 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000496 } else if (S_ISFIFO(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000497 if (mkfifo(header->name, header->mode) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000498 perror_msg("%s: Cannot mkfifo", header->name);
Erik Andersen6a34b532000-04-07 06:55:38 +0000499 return( FALSE);
500 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000501 }
502
503 /* Now set permissions etc for the new directory */
504 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000505 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000506}
507
Erik Andersen1ad302a2000-03-24 00:54:46 +0000508/* Read an octal value in a field of the specified width, with optional
Erik Andersen298854f2000-03-23 01:09:18 +0000509 * spaces on both sides of the number and with an optional null character
Erik Andersen1ad302a2000-03-24 00:54:46 +0000510 * at the end. Returns -1 on an illegal format. */
Erik Andersen298854f2000-03-23 01:09:18 +0000511static long getOctal(const char *cp, int size)
Eric Andersencc8ed391999-10-05 16:24:54 +0000512{
Erik Andersen298854f2000-03-23 01:09:18 +0000513 long val = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000514
Erik Andersen298854f2000-03-23 01:09:18 +0000515 for(;(size > 0) && (*cp == ' '); cp++, size--);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000516 if ((size == 0) || !is_octal(*cp))
Erik Andersen298854f2000-03-23 01:09:18 +0000517 return -1;
Mark Whitleyf57c9442000-12-07 19:56:48 +0000518 for(; (size > 0) && is_octal(*cp); size--) {
Erik Andersen298854f2000-03-23 01:09:18 +0000519 val = val * 8 + *cp++ - '0';
Eric Andersencc8ed391999-10-05 16:24:54 +0000520 }
Erik Andersen298854f2000-03-23 01:09:18 +0000521 for (;(size > 0) && (*cp == ' '); cp++, size--);
522 if ((size > 0) && *cp)
523 return -1;
524 return val;
525}
Eric Andersencc8ed391999-10-05 16:24:54 +0000526
Erik Andersen1ad302a2000-03-24 00:54:46 +0000527
Erik Andersen298854f2000-03-23 01:09:18 +0000528/* Parse the tar header and fill in the nice struct with the details */
529static int
Erik Andersen3364d782000-03-28 00:58:14 +0000530readTarHeader(struct TarHeader *rawHeader, struct TarInfo *header)
Erik Andersen298854f2000-03-23 01:09:18 +0000531{
Erik Andersene454fb62000-03-23 04:27:58 +0000532 int i;
Erik Andersen84e09e42000-04-08 20:58:35 +0000533 long chksum, sum=0;
Erik Andersene454fb62000-03-23 04:27:58 +0000534 unsigned char *s = (unsigned char *)rawHeader;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000535
Erik Andersen298854f2000-03-23 01:09:18 +0000536 header->name = rawHeader->name;
Erik Andersen84e09e42000-04-08 20:58:35 +0000537 /* Check for and relativify any absolute paths */
538 if ( *(header->name) == '/' ) {
539 static int alreadyWarned=FALSE;
540
541 while (*(header->name) == '/')
542 ++*(header->name);
543
544 if (alreadyWarned == FALSE) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000545 error_msg("Removing leading '/' from member names");
Erik Andersen84e09e42000-04-08 20:58:35 +0000546 alreadyWarned = TRUE;
547 }
548 }
549
Erik Andersen298854f2000-03-23 01:09:18 +0000550 header->mode = getOctal(rawHeader->mode, sizeof(rawHeader->mode));
551 header->uid = getOctal(rawHeader->uid, sizeof(rawHeader->uid));
552 header->gid = getOctal(rawHeader->gid, sizeof(rawHeader->gid));
553 header->size = getOctal(rawHeader->size, sizeof(rawHeader->size));
554 header->mtime = getOctal(rawHeader->mtime, sizeof(rawHeader->mtime));
555 chksum = getOctal(rawHeader->chksum, sizeof(rawHeader->chksum));
556 header->type = rawHeader->typeflag;
557 header->linkname = rawHeader->linkname;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000558 header->devmajor = getOctal(rawHeader->devmajor, sizeof(rawHeader->devmajor));
559 header->devminor = getOctal(rawHeader->devminor, sizeof(rawHeader->devminor));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000560
Erik Andersen298854f2000-03-23 01:09:18 +0000561 /* Check the checksum */
Erik Andersen84e09e42000-04-08 20:58:35 +0000562 for (i = sizeof(*rawHeader); i-- != 0;) {
Erik Andersen298854f2000-03-23 01:09:18 +0000563 sum += *s++;
Erik Andersen84e09e42000-04-08 20:58:35 +0000564 }
565 /* Remove the effects of the checksum field (replace
566 * with blanks for the purposes of the checksum) */
567 s = rawHeader->chksum;
568 for (i = sizeof(rawHeader->chksum) ; i-- != 0;) {
569 sum -= *s++;
570 }
571 sum += ' ' * sizeof(rawHeader->chksum);
Erik Andersene454fb62000-03-23 04:27:58 +0000572 if (sum == chksum )
Erik Andersen298854f2000-03-23 01:09:18 +0000573 return ( TRUE);
574 return( FALSE);
575}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000576
Matt Kraaibe7499c2001-01-03 17:22:10 +0000577int exclude_file(char **excluded_files, const char *file)
578{
579 int i;
580
581 if (excluded_files == NULL)
582 return 0;
583
584 for (i = 0; excluded_files[i] != NULL; i++) {
585 if (excluded_files[i][0] == '/') {
586 if (fnmatch(excluded_files[i], file,
587 FNM_PATHNAME | FNM_LEADING_DIR) == 0)
588 return 1;
589 } else {
590 const char *p;
591
592 for (p = file; p[0] != '\0'; p++) {
593 if ((p == file || p[-1] == '/') && p[0] != '/' &&
594 fnmatch(excluded_files[i], p,
595 FNM_PATHNAME | FNM_LEADING_DIR) == 0)
596 return 1;
597 }
598 }
599 }
600
601 return 0;
602}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000603
Matt Kraai8f8dab92001-01-22 05:25:19 +0000604int extract_file(char **extract_files, const char *file)
605{
606 int i;
607
608 if (extract_files == NULL)
609 return 1;
610
611 for (i = 0; extract_files[i] != NULL; i++) {
612 if (fnmatch(extract_files[i], file, FNM_LEADING_DIR) == 0)
613 return 1;
614 }
615
616 return 0;
617}
618
Erik Andersen1ad302a2000-03-24 00:54:46 +0000619/*
620 * Read a tar file and extract or list the specified files within it.
621 * If the list is empty than all files are extracted or listed.
622 */
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000623extern int readTarFile(int tarFd, int extractFlag, int listFlag,
Matt Kraaib92223b2000-09-04 08:25:42 +0000624 int tostdoutFlag, int verboseFlag, char** extractList,
625 char** excludeList)
Erik Andersen1ad302a2000-03-24 00:54:46 +0000626{
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000627 int status;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000628 int errorFlag=FALSE;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000629 int skipNextHeaderFlag=FALSE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000630 TarHeader rawHeader;
631 TarInfo header;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000632
Erik Andersene49d5ec2000-02-08 19:58:47 +0000633 /* Set the umask for this process so it doesn't
Erik Andersen1ad302a2000-03-24 00:54:46 +0000634 * screw up permission setting for us later. */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000635 umask(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000636
Erik Andersen1ad302a2000-03-24 00:54:46 +0000637 /* Read the tar file, and iterate over it one file at a time */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000638 while ( (status = full_read(tarFd, (char*)&rawHeader, TAR_BLOCK_SIZE)) == TAR_BLOCK_SIZE ) {
Erik Andersene2729152000-02-18 21:34:17 +0000639
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000640 /* Try to read the header */
Erik Andersen3364d782000-03-28 00:58:14 +0000641 if ( readTarHeader(&rawHeader, &header) == FALSE ) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000642 if ( *(header.name) == '\0' ) {
643 goto endgame;
644 } else {
645 errorFlag=TRUE;
Matt Kraaidd19c692001-01-31 19:00:21 +0000646 error_msg("Bad tar header, skipping");
Erik Andersen1ad302a2000-03-24 00:54:46 +0000647 continue;
648 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000649 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000650 if ( *(header.name) == '\0' )
651 goto endgame;
Erik Andersen0817d132000-04-09 15:17:40 +0000652 header.tarFd = tarFd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000653
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000654 /* Skip funky extra GNU headers that precede long files */
655 if ( (header.type == GNULONGNAME) || (header.type == GNULONGLINK) ) {
656 skipNextHeaderFlag=TRUE;
Matt Kraaiab8f9e22000-11-18 01:28:57 +0000657 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
658 errorFlag = TRUE;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000659 continue;
660 }
661 if ( skipNextHeaderFlag == TRUE ) {
662 skipNextHeaderFlag=FALSE;
Mark Whitleyf57c9442000-12-07 19:56:48 +0000663 error_msg(name_longer_than_foo, NAME_SIZE);
Matt Kraaiab8f9e22000-11-18 01:28:57 +0000664 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
665 errorFlag = TRUE;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000666 continue;
667 }
668
Erik Andersen0817d132000-04-09 15:17:40 +0000669#if defined BB_FEATURE_TAR_EXCLUDE
Matt Kraaibe7499c2001-01-03 17:22:10 +0000670 if (exclude_file(excludeList, header.name)) {
Erik Andersen0817d132000-04-09 15:17:40 +0000671 /* There are not the droids you're looking for, move along */
Matt Kraaibe7499c2001-01-03 17:22:10 +0000672 /* If it is a regular file, pretend to extract it with
673 * the extractFlag set to FALSE, so the junk in the tarball
674 * is properly skipped over */
675 if ( header.type==REGTYPE || header.type==REGTYPE0 ) {
676 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
677 errorFlag = TRUE;
678 }
679 continue;
Erik Andersen0817d132000-04-09 15:17:40 +0000680 }
681#endif
Matt Kraaibe7499c2001-01-03 17:22:10 +0000682
Matt Kraai8f8dab92001-01-22 05:25:19 +0000683 if (!extract_file(extractList, header.name)) {
Matt Kraaib92223b2000-09-04 08:25:42 +0000684 /* There are not the droids you're looking for, move along */
Matt Kraai8f8dab92001-01-22 05:25:19 +0000685 /* If it is a regular file, pretend to extract it with
686 * the extractFlag set to FALSE, so the junk in the tarball
687 * is properly skipped over */
688 if ( header.type==REGTYPE || header.type==REGTYPE0 ) {
689 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
690 errorFlag = TRUE;
Matt Kraaib92223b2000-09-04 08:25:42 +0000691 }
Matt Kraai8f8dab92001-01-22 05:25:19 +0000692 continue;
Matt Kraaib92223b2000-09-04 08:25:42 +0000693 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000694
Matt Kraai82cfbad2000-09-15 21:18:43 +0000695 if (listFlag == TRUE) {
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000696 /* Special treatment if the list (-t) flag is on */
697 if (verboseFlag == TRUE) {
698 int len, len1;
699 char buf[35];
700 struct tm *tm = localtime (&(header.mtime));
701
Mark Whitleyf57c9442000-12-07 19:56:48 +0000702 len=printf("%s ", mode_string(header.mode));
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000703 my_getpwuid(buf, header.uid);
704 if (! *buf)
705 len+=printf("%d", header.uid);
706 else
707 len+=printf("%s", buf);
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000708 my_getgrgid(buf, header.gid);
709 if (! *buf)
710 len+=printf("/%-d ", header.gid);
711 else
712 len+=printf("/%-s ", buf);
713
714 if (header.type==CHRTYPE || header.type==BLKTYPE) {
715 len1=snprintf(buf, sizeof(buf), "%ld,%-ld ",
716 header.devmajor, header.devminor);
717 } else {
718 len1=snprintf(buf, sizeof(buf), "%lu ", (long)header.size);
719 }
720 /* Jump through some hoops to make the columns match up */
721 for(;(len+len1)<31;len++)
722 printf(" ");
723 printf(buf);
724
725 /* Use ISO 8610 time format */
726 if (tm) {
727 printf ("%04d-%02d-%02d %02d:%02d:%02d ",
728 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
729 tm->tm_hour, tm->tm_min, tm->tm_sec);
730 }
731 }
Eric Andersenfdd51032000-08-02 18:48:26 +0000732 printf("%s", header.name);
Matt Kraai82cfbad2000-09-15 21:18:43 +0000733 if (verboseFlag == TRUE) {
734 if (header.type==LNKTYPE) /* If this is a link, say so */
735 printf(" link to %s", header.linkname);
736 else if (header.type==SYMTYPE)
737 printf(" -> %s", header.linkname);
738 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000739 printf("\n");
740 }
741
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000742 /* List contents if we are supposed to do that */
743 if (verboseFlag == TRUE && extractFlag == TRUE) {
744 /* Now the normal listing */
745 FILE *vbFd = stdout;
746 if (tostdoutFlag == TRUE) // If the archive goes to stdout, verbose to stderr
747 vbFd = stderr;
748 fprintf(vbFd, "%s\n", header.name);
749 }
750
Matt Kraaif4462972000-09-01 02:50:48 +0000751 /* Remove files if we would overwrite them */
Matt Kraaida542f32000-09-01 02:53:01 +0000752 if (extractFlag == TRUE && tostdoutFlag == FALSE)
Matt Kraaif4462972000-09-01 02:50:48 +0000753 unlink(header.name);
Eric Andersencc8ed391999-10-05 16:24:54 +0000754
Erik Andersen1ad302a2000-03-24 00:54:46 +0000755 /* If we got here, we can be certain we have a legitimate
756 * header to work with. So work with it. */
757 switch ( header.type ) {
758 case REGTYPE:
759 case REGTYPE0:
760 /* If the name ends in a '/' then assume it is
761 * supposed to be a directory, and fall through */
762 if (header.name[strlen(header.name)-1] != '/') {
Erik Andersen6a34b532000-04-07 06:55:38 +0000763 if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE)
764 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000765 break;
766 }
767 case DIRTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000768 if (tarExtractDirectory( &header, extractFlag, tostdoutFlag)==FALSE)
769 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000770 break;
771 case LNKTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000772 if (tarExtractHardLink( &header, extractFlag, tostdoutFlag)==FALSE)
773 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000774 break;
775 case SYMTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000776 if (tarExtractSymLink( &header, extractFlag, tostdoutFlag)==FALSE)
777 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000778 break;
779 case CHRTYPE:
780 case BLKTYPE:
781 case FIFOTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000782 if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE)
783 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000784 break;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000785#if 0
786 /* Handled earlier */
787 case GNULONGNAME:
788 case GNULONGLINK:
789 skipNextHeaderFlag=TRUE;
790 break;
791#endif
Erik Andersen1ad302a2000-03-24 00:54:46 +0000792 default:
Matt Kraaidd19c692001-01-31 19:00:21 +0000793 error_msg("Unknown file type '%c' in tar file", header.type);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000794 close( tarFd);
795 return( FALSE);
796 }
797 }
798 close(tarFd);
799 if (status > 0) {
800 /* Bummer - we read a partial header */
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000801 perror_msg("Error reading tar file");
Erik Andersen1ad302a2000-03-24 00:54:46 +0000802 return ( FALSE);
803 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000804 else if (errorFlag==TRUE) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000805 error_msg( "Error exit delayed from previous errors");
Erik Andersen6a34b532000-04-07 06:55:38 +0000806 return( FALSE);
807 } else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000808 return( status);
809
810 /* Stuff to do when we are done */
811endgame:
812 close( tarFd);
813 if ( *(header.name) == '\0' ) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000814 if (errorFlag==TRUE)
Matt Kraaidd19c692001-01-31 19:00:21 +0000815 error_msg( "Error exit delayed from previous errors");
Erik Andersen6a34b532000-04-07 06:55:38 +0000816 else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000817 return( TRUE);
818 }
819 return( FALSE);
820}
821
Erik Andersen6acaa402000-03-26 14:03:20 +0000822
823#ifdef BB_FEATURE_TAR_CREATE
824
Eric Andersen3d957c82000-12-07 00:34:58 +0000825/*
826** writeTarFile(), writeFileToTarball(), and writeTarHeader() are
827** the only functions that deal with the HardLinkInfo structure.
828** Even these functions use the xxxHardLinkInfo() functions.
829*/
830typedef struct HardLinkInfo HardLinkInfo;
831struct HardLinkInfo
832{
833 HardLinkInfo *next; /* Next entry in list */
834 dev_t dev; /* Device number */
835 ino_t ino; /* Inode number */
836 short linkCount; /* (Hard) Link Count */
837 char name[1]; /* Start of filename (must be last) */
838};
839
Erik Andersen68a9ea42000-04-04 18:39:50 +0000840/* Some info to be carried along when creating a new tarball */
841struct TarBallInfo
842{
843 char* fileName; /* File name of the tarball */
844 int tarFd; /* Open-for-write file descriptor
845 for the tarball */
846 struct stat statBuf; /* Stat info for the tarball, letting
847 us know the inode and device that the
848 tarball lives, so we can avoid trying
849 to include the tarball into itself */
850 int verboseFlag; /* Whether to print extra stuff or not */
Erik Andersenecd51242000-04-08 03:08:21 +0000851 char** excludeList; /* List of files to not include */
Eric Andersen3d957c82000-12-07 00:34:58 +0000852 HardLinkInfo *hlInfoHead; /* Hard Link Tracking Information */
853 HardLinkInfo *hlInfo; /* Hard Link Info for the current file */
Erik Andersen68a9ea42000-04-04 18:39:50 +0000854};
855typedef struct TarBallInfo TarBallInfo;
856
857
Eric Andersen3d957c82000-12-07 00:34:58 +0000858/* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */
859static void
860addHardLinkInfo (HardLinkInfo **hlInfoHeadPtr, dev_t dev, ino_t ino,
861 short linkCount, const char *name)
862{
863 /* Note: hlInfoHeadPtr can never be NULL! */
864 HardLinkInfo *hlInfo;
865
866 hlInfo = (HardLinkInfo *)xmalloc(sizeof(HardLinkInfo)+strlen(name)+1);
867 if (hlInfo) {
868 hlInfo->next = *hlInfoHeadPtr;
869 *hlInfoHeadPtr = hlInfo;
870 hlInfo->dev = dev;
871 hlInfo->ino = ino;
872 hlInfo->linkCount = linkCount;
873 strcpy(hlInfo->name, name);
874 }
875 return;
876}
877
878static void
879freeHardLinkInfo (HardLinkInfo **hlInfoHeadPtr)
880{
881 HardLinkInfo *hlInfo = NULL;
882 HardLinkInfo *hlInfoNext = NULL;
883
884 if (hlInfoHeadPtr) {
885 hlInfo = *hlInfoHeadPtr;
886 while (hlInfo) {
887 hlInfoNext = hlInfo->next;
888 free(hlInfo);
889 hlInfo = hlInfoNext;
890 }
891 *hlInfoHeadPtr = NULL;
892 }
893 return;
894}
895
896/* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */
897static HardLinkInfo *
898findHardLinkInfo (HardLinkInfo *hlInfo, dev_t dev, ino_t ino)
899{
900 while(hlInfo) {
901 if ((ino == hlInfo->ino) && (dev == hlInfo->dev))
902 break;
903 hlInfo = hlInfo->next;
904 }
905 return(hlInfo);
906}
907
Erik Andersen6acaa402000-03-26 14:03:20 +0000908/* Put an octal string into the specified buffer.
909 * The number is zero and space padded and possibly null padded.
910 * Returns TRUE if successful. */
911static int putOctal (char *cp, int len, long value)
912{
913 int tempLength;
Erik Andersen6acaa402000-03-26 14:03:20 +0000914 char tempBuffer[32];
Erik Andersen5661fe02000-04-05 01:00:52 +0000915 char *tempString = tempBuffer;
Erik Andersen6acaa402000-03-26 14:03:20 +0000916
917 /* Create a string of the specified length with an initial space,
918 * leading zeroes and the octal number, and a trailing null. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000919 sprintf (tempString, "%0*lo", len - 1, value);
Erik Andersen6acaa402000-03-26 14:03:20 +0000920
921 /* If the string is too large, suppress the leading space. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000922 tempLength = strlen (tempString) + 1;
Erik Andersen6acaa402000-03-26 14:03:20 +0000923 if (tempLength > len) {
924 tempLength--;
925 tempString++;
926 }
927
928 /* If the string is still too large, suppress the trailing null. */
929 if (tempLength > len)
930 tempLength--;
931
932 /* If the string is still too large, fail. */
933 if (tempLength > len)
934 return FALSE;
935
936 /* Copy the string to the field. */
937 memcpy (cp, tempString, len);
938
939 return TRUE;
940}
941
Erik Andersen68a9ea42000-04-04 18:39:50 +0000942/* Write out a tar header for the specified file/directory/whatever */
Erik Andersen3364d782000-03-28 00:58:14 +0000943static int
Matt Kraaie80a2632000-12-19 20:45:49 +0000944writeTarHeader(struct TarBallInfo *tbInfo, const char *header_name,
945 const char *real_name, struct stat *statbuf)
Erik Andersen6acaa402000-03-26 14:03:20 +0000946{
Erik Andersen5661fe02000-04-05 01:00:52 +0000947 long chksum=0;
948 struct TarHeader header;
949 const unsigned char *cp = (const unsigned char *) &header;
950 ssize_t size = sizeof(struct TarHeader);
Eric Andersenfdd51032000-08-02 18:48:26 +0000951
Erik Andersen5661fe02000-04-05 01:00:52 +0000952 memset( &header, 0, size);
Erik Andersen3364d782000-03-28 00:58:14 +0000953
Matt Kraaie80a2632000-12-19 20:45:49 +0000954 strncpy(header.name, header_name, sizeof(header.name));
Erik Andersenecd51242000-04-08 03:08:21 +0000955
Erik Andersen5661fe02000-04-05 01:00:52 +0000956 putOctal(header.mode, sizeof(header.mode), statbuf->st_mode);
957 putOctal(header.uid, sizeof(header.uid), statbuf->st_uid);
958 putOctal(header.gid, sizeof(header.gid), statbuf->st_gid);
959 putOctal(header.size, sizeof(header.size), 0); /* Regular file size is handled later */
960 putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime);
961 strncpy(header.magic, TAR_MAGIC TAR_VERSION,
962 TAR_MAGIC_LEN + TAR_VERSION_LEN );
Erik Andersen68a9ea42000-04-04 18:39:50 +0000963
Erik Andersen84e09e42000-04-08 20:58:35 +0000964 /* Enter the user and group names (default to root if it fails) */
Erik Andersen5661fe02000-04-05 01:00:52 +0000965 my_getpwuid(header.uname, statbuf->st_uid);
Erik Andersen5661fe02000-04-05 01:00:52 +0000966 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000967 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +0000968 my_getgrgid(header.gname, statbuf->st_gid);
969 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000970 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +0000971
Eric Andersen3d957c82000-12-07 00:34:58 +0000972 if (tbInfo->hlInfo) {
973 /* This is a hard link */
974 header.typeflag = LNKTYPE;
975 strncpy(header.linkname, tbInfo->hlInfo->name, sizeof(header.linkname));
976 } else if (S_ISLNK(statbuf->st_mode)) {
Eric Andersen3adffb72000-06-26 10:54:06 +0000977 int link_size=0;
Erik Andersen5661fe02000-04-05 01:00:52 +0000978 char buffer[BUFSIZ];
979 header.typeflag = SYMTYPE;
Matt Kraaie80a2632000-12-19 20:45:49 +0000980 link_size = readlink(real_name, buffer, sizeof(buffer) - 1);
Eric Andersen3adffb72000-06-26 10:54:06 +0000981 if ( link_size < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000982 perror_msg("Error reading symlink '%s'", header.name);
Erik Andersen5661fe02000-04-05 01:00:52 +0000983 return ( FALSE);
984 }
Eric Andersen3adffb72000-06-26 10:54:06 +0000985 buffer[link_size] = '\0';
Erik Andersen95c1c1e2000-04-14 21:45:29 +0000986 strncpy(header.linkname, buffer, sizeof(header.linkname));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000987 } else if (S_ISDIR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000988 header.typeflag = DIRTYPE;
989 strncat(header.name, "/", sizeof(header.name));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000990 } else if (S_ISCHR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000991 header.typeflag = CHRTYPE;
992 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
993 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000994 } else if (S_ISBLK(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000995 header.typeflag = BLKTYPE;
996 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
997 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000998 } else if (S_ISFIFO(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000999 header.typeflag = FIFOTYPE;
1000 } else if (S_ISREG(statbuf->st_mode)) {
1001 header.typeflag = REGTYPE;
1002 putOctal(header.size, sizeof(header.size), statbuf->st_size);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001003 } else {
Matt Kraaidd19c692001-01-31 19:00:21 +00001004 error_msg("%s: Unknown file type", real_name);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001005 return ( FALSE);
1006 }
Erik Andersen68a9ea42000-04-04 18:39:50 +00001007
Erik Andersen5661fe02000-04-05 01:00:52 +00001008 /* Calculate and store the checksum (i.e. the sum of all of the bytes of
1009 * the header). The checksum field must be filled with blanks for the
1010 * calculation. The checksum field is formatted differently from the
1011 * other fields: it has [6] digits, a null, then a space -- rather than
1012 * digits, followed by a null like the other fields... */
1013 memset(header.chksum, ' ', sizeof(header.chksum));
1014 cp = (const unsigned char *) &header;
1015 while (size-- > 0)
1016 chksum += *cp++;
1017 putOctal(header.chksum, 7, chksum);
1018
1019 /* Now write the header out to disk */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001020 if ((size=full_write(tbInfo->tarFd, (char*)&header, sizeof(struct TarHeader))) < 0) {
Matt Kraaie80a2632000-12-19 20:45:49 +00001021 error_msg(io_error, real_name, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +00001022 return ( FALSE);
1023 }
1024 /* Pad the header up to the tar block size */
1025 for (; size<TAR_BLOCK_SIZE; size++) {
1026 write(tbInfo->tarFd, "\0", 1);
1027 }
1028 /* Now do the verbose thing (or not) */
Eric Andersenfdd51032000-08-02 18:48:26 +00001029 if (tbInfo->verboseFlag==TRUE) {
1030 FILE *vbFd = stdout;
1031 if (tbInfo->tarFd == fileno(stdout)) // If the archive goes to stdout, verbose to stderr
1032 vbFd = stderr;
1033 fprintf(vbFd, "%s\n", header.name);
1034 }
Erik Andersen3364d782000-03-28 00:58:14 +00001035
1036 return ( TRUE);
1037}
1038
1039
Erik Andersen68a9ea42000-04-04 18:39:50 +00001040static int writeFileToTarball(const char *fileName, struct stat *statbuf, void* userData)
Erik Andersen3364d782000-03-28 00:58:14 +00001041{
Erik Andersen68a9ea42000-04-04 18:39:50 +00001042 struct TarBallInfo *tbInfo = (struct TarBallInfo *)userData;
Matt Kraaie80a2632000-12-19 20:45:49 +00001043 const char *header_name;
Erik Andersen68a9ea42000-04-04 18:39:50 +00001044
Eric Andersen3d957c82000-12-07 00:34:58 +00001045 /*
1046 ** Check to see if we are dealing with a hard link.
1047 ** If so -
1048 ** Treat the first occurance of a given dev/inode as a file while
1049 ** treating any additional occurances as hard links. This is done
1050 ** by adding the file information to the HardLinkInfo linked list.
1051 */
1052 tbInfo->hlInfo = NULL;
1053 if (statbuf->st_nlink > 1) {
1054 tbInfo->hlInfo = findHardLinkInfo(tbInfo->hlInfoHead, statbuf->st_dev,
1055 statbuf->st_ino);
1056 if (tbInfo->hlInfo == NULL)
1057 addHardLinkInfo (&tbInfo->hlInfoHead, statbuf->st_dev,
1058 statbuf->st_ino, statbuf->st_nlink, fileName);
1059 }
1060
Erik Andersen68a9ea42000-04-04 18:39:50 +00001061 /* It is against the rules to archive a socket */
1062 if (S_ISSOCK(statbuf->st_mode)) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001063 error_msg("%s: socket ignored", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001064 return( TRUE);
1065 }
1066
1067 /* It is a bad idea to store the archive we are in the process of creating,
1068 * so check the device and inode to be sure that this particular file isn't
1069 * the new tarball */
1070 if (tbInfo->statBuf.st_dev == statbuf->st_dev &&
1071 tbInfo->statBuf.st_ino == statbuf->st_ino) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001072 error_msg("%s: file is the archive; skipping", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001073 return( TRUE);
1074 }
1075
Matt Kraaie80a2632000-12-19 20:45:49 +00001076 header_name = fileName;
1077 while (header_name[0] == '/') {
Matt Kraaia1f97752000-12-19 06:24:08 +00001078 static int alreadyWarned=FALSE;
1079 if (alreadyWarned==FALSE) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001080 error_msg("Removing leading '/' from member names");
Matt Kraaia1f97752000-12-19 06:24:08 +00001081 alreadyWarned=TRUE;
1082 }
Matt Kraaie80a2632000-12-19 20:45:49 +00001083 header_name++;
Matt Kraaia1f97752000-12-19 06:24:08 +00001084 }
1085
Eric Andersen1b1cfde2000-09-24 00:54:37 +00001086 if (strlen(fileName) >= NAME_SIZE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001087 error_msg(name_longer_than_foo, NAME_SIZE);
Eric Andersen1b1cfde2000-09-24 00:54:37 +00001088 return ( TRUE);
1089 }
1090
Matt Kraaie80a2632000-12-19 20:45:49 +00001091 if (header_name[0] == '\0')
Matt Kraaia1f97752000-12-19 06:24:08 +00001092 return TRUE;
1093
1094#if defined BB_FEATURE_TAR_EXCLUDE
Matt Kraaibe7499c2001-01-03 17:22:10 +00001095 if (exclude_file(tbInfo->excludeList, header_name)) {
1096 return SKIP;
Matt Kraaia1f97752000-12-19 06:24:08 +00001097 }
1098#endif
1099
Matt Kraaie80a2632000-12-19 20:45:49 +00001100 if (writeTarHeader(tbInfo, header_name, fileName, statbuf)==FALSE) {
Erik Andersen5661fe02000-04-05 01:00:52 +00001101 return( FALSE);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001102 }
Erik Andersen5661fe02000-04-05 01:00:52 +00001103
1104 /* Now, if the file is a regular file, copy it out to the tarball */
Eric Andersen3d957c82000-12-07 00:34:58 +00001105 if ((tbInfo->hlInfo == NULL)
1106 && (S_ISREG(statbuf->st_mode))) {
Erik Andersen5661fe02000-04-05 01:00:52 +00001107 int inputFileFd;
1108 char buffer[BUFSIZ];
1109 ssize_t size=0, readSize=0;
1110
1111 /* open the file we want to archive, and make sure all is well */
1112 if ((inputFileFd = open(fileName, O_RDONLY)) < 0) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001113 error_msg("%s: Cannot open: %s", fileName, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +00001114 return( FALSE);
1115 }
1116
1117 /* write the file to the archive */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001118 while ( (size = full_read(inputFileFd, buffer, sizeof(buffer))) > 0 ) {
1119 if (full_write(tbInfo->tarFd, buffer, size) != size ) {
Erik Andersen5661fe02000-04-05 01:00:52 +00001120 /* Output file seems to have a problem */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001121 error_msg(io_error, fileName, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +00001122 return( FALSE);
1123 }
1124 readSize+=size;
1125 }
1126 if (size == -1) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001127 error_msg(io_error, fileName, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +00001128 return( FALSE);
1129 }
1130 /* Pad the file up to the tar block size */
1131 for (; (readSize%TAR_BLOCK_SIZE) != 0; readSize++) {
1132 write(tbInfo->tarFd, "\0", 1);
1133 }
1134 close( inputFileFd);
1135 }
Erik Andersen68a9ea42000-04-04 18:39:50 +00001136
1137 return( TRUE);
Erik Andersen6acaa402000-03-26 14:03:20 +00001138}
1139
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001140static int writeTarFile(const char* tarName, int verboseFlag, char **argv,
1141 char** excludeList)
Erik Andersen6acaa402000-03-26 14:03:20 +00001142{
Erik Andersen3364d782000-03-28 00:58:14 +00001143 int tarFd=-1;
Erik Andersen68a9ea42000-04-04 18:39:50 +00001144 int errorFlag=FALSE;
Erik Andersen5661fe02000-04-05 01:00:52 +00001145 ssize_t size;
Erik Andersen68a9ea42000-04-04 18:39:50 +00001146 struct TarBallInfo tbInfo;
1147 tbInfo.verboseFlag = verboseFlag;
Eric Andersen3d957c82000-12-07 00:34:58 +00001148 tbInfo.hlInfoHead = NULL;
Erik Andersen3364d782000-03-28 00:58:14 +00001149
1150 /* Make sure there is at least one file to tar up. */
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001151 if (*argv == NULL)
Matt Kraaidd19c692001-01-31 19:00:21 +00001152 error_msg_and_die("Cowardly refusing to create an empty archive");
Erik Andersen6acaa402000-03-26 14:03:20 +00001153
1154 /* Open the tar file for writing. */
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001155 if (!strcmp(tarName, "-"))
Erik Andersen68a9ea42000-04-04 18:39:50 +00001156 tbInfo.tarFd = fileno(stdout);
Erik Andersen6acaa402000-03-26 14:03:20 +00001157 else
Erik Andersen68a9ea42000-04-04 18:39:50 +00001158 tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1159 if (tbInfo.tarFd < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +00001160 perror_msg( "Error opening '%s'", tarName);
Eric Andersen3d957c82000-12-07 00:34:58 +00001161 freeHardLinkInfo(&tbInfo.hlInfoHead);
Erik Andersen6acaa402000-03-26 14:03:20 +00001162 return ( FALSE);
1163 }
Erik Andersenecd51242000-04-08 03:08:21 +00001164 tbInfo.excludeList=excludeList;
Erik Andersen68a9ea42000-04-04 18:39:50 +00001165 /* Store the stat info for the tarball's file, so
1166 * can avoid including the tarball into itself.... */
1167 if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001168 error_msg_and_die(io_error, tarName, strerror(errno));
Erik Andersen6acaa402000-03-26 14:03:20 +00001169
1170 /* Set the umask for this process so it doesn't
1171 * screw up permission setting for us later. */
1172 umask(0);
1173
1174 /* Read the directory/files and iterate over them one at a time */
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001175 while (*argv != NULL) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001176 if (recursive_action(*argv++, TRUE, FALSE, FALSE,
Erik Andersen68a9ea42000-04-04 18:39:50 +00001177 writeFileToTarball, writeFileToTarball,
1178 (void*) &tbInfo) == FALSE) {
1179 errorFlag = TRUE;
Erik Andersen3364d782000-03-28 00:58:14 +00001180 }
Erik Andersen6acaa402000-03-26 14:03:20 +00001181 }
Erik Andersen5661fe02000-04-05 01:00:52 +00001182 /* Write two empty blocks to the end of the archive */
1183 for (size=0; size<(2*TAR_BLOCK_SIZE); size++) {
1184 write(tbInfo.tarFd, "\0", 1);
1185 }
Erik Andersen0817d132000-04-09 15:17:40 +00001186
1187 /* To be pedantically correct, we would check if the tarball
Eric Andersen3c5ee9a2000-11-14 22:15:48 +00001188 * is smaller than 20 tar blocks, and pad it if it was smaller,
Erik Andersen0817d132000-04-09 15:17:40 +00001189 * but that isn't necessary for GNU tar interoperability, and
1190 * so is considered a waste of space */
1191
Erik Andersen68a9ea42000-04-04 18:39:50 +00001192 /* Hang up the tools, close up shop, head home */
Erik Andersen6acaa402000-03-26 14:03:20 +00001193 close(tarFd);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001194 if (errorFlag == TRUE) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001195 error_msg("Error exit delayed from previous errors");
Eric Andersen3d957c82000-12-07 00:34:58 +00001196 freeHardLinkInfo(&tbInfo.hlInfoHead);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001197 return(FALSE);
1198 }
Eric Andersen3d957c82000-12-07 00:34:58 +00001199 freeHardLinkInfo(&tbInfo.hlInfoHead);
Erik Andersen6acaa402000-03-26 14:03:20 +00001200 return( TRUE);
1201}
1202
1203
1204#endif
1205