blob: 8c2650e2b65a43b2605dade856b70f4ca791de4d [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>
Eric Andersencc8ed391999-10-05 16:24:54 +000054
Glenn L McGrath46f44d22000-12-10 01:57:30 +000055#ifdef BB_FEATURE_TAR_GZIP
56extern int unzip(int in, int out);
57extern int gunzip_init();
58#endif
59
Erik Andersen298854f2000-03-23 01:09:18 +000060/* Tar file constants */
Erik Andersen1ad302a2000-03-24 00:54:46 +000061#ifndef MAJOR
62#define MAJOR(dev) (((dev)>>8)&0xff)
63#define MINOR(dev) ((dev)&0xff)
64#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000065
Eric Andersen1b1cfde2000-09-24 00:54:37 +000066#define NAME_SIZE 100
Eric Andersencc8ed391999-10-05 16:24:54 +000067
Erik Andersen298854f2000-03-23 01:09:18 +000068/* POSIX tar Header Block, from POSIX 1003.1-1990 */
69struct TarHeader
70{
71 /* byte offset */
Eric Andersen1b1cfde2000-09-24 00:54:37 +000072 char name[NAME_SIZE]; /* 0-99 */
Erik Andersen0817d132000-04-09 15:17:40 +000073 char mode[8]; /* 100-107 */
74 char uid[8]; /* 108-115 */
75 char gid[8]; /* 116-123 */
76 char size[12]; /* 124-135 */
77 char mtime[12]; /* 136-147 */
78 char chksum[8]; /* 148-155 */
79 char typeflag; /* 156-156 */
Eric Andersen1b1cfde2000-09-24 00:54:37 +000080 char linkname[NAME_SIZE]; /* 157-256 */
Erik Andersen0817d132000-04-09 15:17:40 +000081 char magic[6]; /* 257-262 */
82 char version[2]; /* 263-264 */
83 char uname[32]; /* 265-296 */
84 char gname[32]; /* 297-328 */
85 char devmajor[8]; /* 329-336 */
86 char devminor[8]; /* 337-344 */
87 char prefix[155]; /* 345-499 */
88 char padding[12]; /* 500-512 (pad to exactly the TAR_BLOCK_SIZE) */
Erik Andersen298854f2000-03-23 01:09:18 +000089};
90typedef struct TarHeader TarHeader;
Eric Andersencc8ed391999-10-05 16:24:54 +000091
Eric Andersencc8ed391999-10-05 16:24:54 +000092
Erik Andersen298854f2000-03-23 01:09:18 +000093/* A few useful constants */
94#define TAR_MAGIC "ustar" /* ustar and a null */
Erik Andersen0817d132000-04-09 15:17:40 +000095#define TAR_VERSION " " /* Be compatable with GNU tar format */
Erik Andersen298854f2000-03-23 01:09:18 +000096#define TAR_MAGIC_LEN 6
97#define TAR_VERSION_LEN 2
Erik Andersen298854f2000-03-23 01:09:18 +000098#define TAR_BLOCK_SIZE 512
99
100/* A nice enum with all the possible tar file content types */
101enum TarFileType
102{
103 REGTYPE = '0', /* regular file */
104 REGTYPE0 = '\0', /* regular file (ancient bug compat)*/
105 LNKTYPE = '1', /* hard link */
106 SYMTYPE = '2', /* symbolic link */
107 CHRTYPE = '3', /* character special */
108 BLKTYPE = '4', /* block special */
109 DIRTYPE = '5', /* directory */
110 FIFOTYPE = '6', /* FIFO special */
111 CONTTYPE = '7', /* reserved */
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000112 GNULONGLINK = 'K', /* GNU long (>100 chars) link name */
113 GNULONGNAME = 'L', /* GNU long (>100 chars) file name */
Erik Andersen298854f2000-03-23 01:09:18 +0000114};
115typedef enum TarFileType TarFileType;
116
117/* This struct ignores magic, non-numeric user name,
118 * non-numeric group name, and the checksum, since
119 * these are all ignored by BusyBox tar. */
120struct TarInfo
121{
122 int tarFd; /* An open file descriptor for reading from the tarball */
123 char * name; /* File name */
124 mode_t mode; /* Unix mode, including device bits. */
125 uid_t uid; /* Numeric UID */
126 gid_t gid; /* Numeric GID */
127 size_t size; /* Size of file */
128 time_t mtime; /* Last-modified time */
129 enum TarFileType type; /* Regular, directory, link, etc */
130 char * linkname; /* Name for symbolic and hard links */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000131 long devmajor; /* Major number for special device */
132 long devminor; /* Minor number for special device */
Erik Andersen298854f2000-03-23 01:09:18 +0000133};
134typedef struct TarInfo TarInfo;
135
Erik Andersene454fb62000-03-23 04:27:58 +0000136/* Local procedures to restore files from a tar file. */
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000137static int readTarFile(int tarFd, int extractFlag, int listFlag,
Matt Kraaib92223b2000-09-04 08:25:42 +0000138 int tostdoutFlag, int verboseFlag, char** extractList,
139 char** excludeList);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000140
Erik Andersen05100cd2000-01-16 01:30:52 +0000141#ifdef BB_FEATURE_TAR_CREATE
Erik Andersen6acaa402000-03-26 14:03:20 +0000142/* Local procedures to save files into a tar file. */
Matt Kraaid8ad76c2000-11-08 02:35:47 +0000143static int writeTarFile(const char* tarName, int verboseFlag, char **argv,
144 char** excludeList);
Erik Andersen05100cd2000-01-16 01:30:52 +0000145#endif
146
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000147#ifdef BB_FEATURE_TAR_GZIP
148/* Signal handler for when child gzip process dies... */
149void child_died()
150{
151 fflush(stdout);
152 fflush(stderr);
153 exit(EXIT_FAILURE);
154}
155
156static int tar_unzip_init(int tarFd)
157{
158 int child_pid;
159 static int unzip_pipe[2];
160 /* Cope if child dies... Otherwise we block forever in read()... */
161 signal(SIGCHLD, child_died);
162
163 if (pipe(unzip_pipe)!=0)
164 error_msg_and_die("pipe error\n");
165
166 if ( (child_pid = fork()) == -1)
167 error_msg_and_die("fork failure\n");
168
169 if (child_pid==0) {
170 /* child process */
171 gunzip_init();
172 unzip(tarFd, unzip_pipe[1]);
173 exit(EXIT_SUCCESS);
174 }
175 else
176 /* return fd of uncompressed data to parent process */
177 return(unzip_pipe[0]);
178}
179#endif
180
Erik Andersene49d5ec2000-02-08 19:58:47 +0000181extern int tar_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000182{
Erik Andersenecd51242000-04-08 03:08:21 +0000183 char** excludeList=NULL;
Matt Kraaic119ab92000-11-30 04:44:54 +0000184 char** extractList=NULL;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000185 const char *tarName="-";
Erik Andersen0817d132000-04-09 15:17:40 +0000186#if defined BB_FEATURE_TAR_EXCLUDE
Erik Andersenecd51242000-04-08 03:08:21 +0000187 int excludeListSize=0;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000188 char *excludeFileName ="-";
189 FILE *fileList;
190 char file[256];
Erik Andersen0817d132000-04-09 15:17:40 +0000191#endif
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000192#if defined BB_FEATURE_TAR_GZIP
193 int unzipFlag = FALSE;
194#endif
Erik Andersen298854f2000-03-23 01:09:18 +0000195 int listFlag = FALSE;
196 int extractFlag = FALSE;
197 int createFlag = FALSE;
198 int verboseFlag = FALSE;
199 int tostdoutFlag = FALSE;
Eric Andersen02f3b2e2000-12-01 19:04:52 +0000200 int status = FALSE;
201 int firstOpt = TRUE;
Eric Andersen46a98df2000-09-19 21:35:14 +0000202 int stopIt;
Eric Andersencc8ed391999-10-05 16:24:54 +0000203
Erik Andersenecd51242000-04-08 03:08:21 +0000204 if (argc <= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000205 usage(tar_usage);
Eric Andersencc8ed391999-10-05 16:24:54 +0000206
Matt Kraaid8ad76c2000-11-08 02:35:47 +0000207 while (*(++argv) && (**argv == '-' || firstOpt == TRUE)) {
Eric Andersen0102a9f2000-09-23 22:36:24 +0000208 firstOpt=FALSE;
Eric Andersen46a98df2000-09-19 21:35:14 +0000209 stopIt=FALSE;
Matt Kraaid8ad76c2000-11-08 02:35:47 +0000210 while (stopIt==FALSE && **argv) {
211 switch (*((*argv)++)) {
Erik Andersenecd51242000-04-08 03:08:21 +0000212 case 'c':
213 if (extractFlag == TRUE || listFlag == TRUE)
214 goto flagError;
215 createFlag = TRUE;
216 break;
Eric Andersenfdd51032000-08-02 18:48:26 +0000217 case 'x':
218 if (listFlag == TRUE || createFlag == TRUE)
219 goto flagError;
220 extractFlag = TRUE;
221 break;
222 case 't':
223 if (extractFlag == TRUE || createFlag == TRUE)
224 goto flagError;
225 listFlag = TRUE;
226 break;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000227#ifdef BB_FEATURE_TAR_GZIP
228 case 'z':
229 unzipFlag = TRUE;
230 break;
231#endif
Erik Andersenecd51242000-04-08 03:08:21 +0000232 case 'v':
233 verboseFlag = TRUE;
234 break;
Erik Andersenecd51242000-04-08 03:08:21 +0000235 case 'O':
236 tostdoutFlag = TRUE;
Eric Andersenfdd51032000-08-02 18:48:26 +0000237 break;
238 case 'f':
239 if (*tarName != '-')
Mark Whitleyf57c9442000-12-07 19:56:48 +0000240 error_msg_and_die( "Only one 'f' option allowed\n");
Eric Andersen46a98df2000-09-19 21:35:14 +0000241 tarName = *(++argv);
242 if (tarName == NULL)
Mark Whitleyf57c9442000-12-07 19:56:48 +0000243 error_msg_and_die( "Option requires an argument: No file specified\n");
Eric Andersen46a98df2000-09-19 21:35:14 +0000244 stopIt=TRUE;
Erik Andersenecd51242000-04-08 03:08:21 +0000245 break;
Erik Andersen0817d132000-04-09 15:17:40 +0000246#if defined BB_FEATURE_TAR_EXCLUDE
Matt Kraai43c8c382000-09-04 16:51:55 +0000247 case 'e':
Eric Andersen8cede002000-12-04 18:51:09 +0000248 if (strcmp(*argv, "xclude")==0) {
Eric Andersen46a98df2000-09-19 21:35:14 +0000249 excludeList=xrealloc( excludeList, sizeof(char**) * (excludeListSize+2));
250 excludeList[excludeListSize] = *(++argv);
Matt Kraaid8ad76c2000-11-08 02:35:47 +0000251 if (excludeList[excludeListSize] == NULL)
Mark Whitleyf57c9442000-12-07 19:56:48 +0000252 error_msg_and_die( "Option requires an argument: No file specified\n");
Eric Andersen46a98df2000-09-19 21:35:14 +0000253 /* Remove leading "/"s */
254 if (*excludeList[excludeListSize] =='/')
255 excludeList[excludeListSize] = (excludeList[excludeListSize])+1;
256 /* Tack a NULL onto the end of the list */
257 excludeList[++excludeListSize] = NULL;
258 stopIt=TRUE;
259 break;
260 }
Eric Andersen8cede002000-12-04 18:51:09 +0000261 case 'X':
262 if (*excludeFileName != '-')
Mark Whitleyf57c9442000-12-07 19:56:48 +0000263 error_msg_and_die("Only one 'X' option allowed\n");
Eric Andersen8cede002000-12-04 18:51:09 +0000264 excludeFileName = *(++argv);
265 if (excludeFileName == NULL)
Mark Whitleyf57c9442000-12-07 19:56:48 +0000266 error_msg_and_die("Option requires an argument: No file specified\n");
Eric Andersen8cede002000-12-04 18:51:09 +0000267 fileList = fopen (excludeFileName, "rt");
268 if (! fileList)
Mark Whitleyf57c9442000-12-07 19:56:48 +0000269 error_msg_and_die("Exclude file: file not found\n");
Eric Andersen8cede002000-12-04 18:51:09 +0000270 while (!feof(fileList)) {
271 fscanf(fileList, "%s", file);
272 excludeList=xrealloc( excludeList, sizeof(char**) * (excludeListSize+2));
273 excludeList[excludeListSize] = malloc(sizeof(char) * (strlen(file)+1));
274 strcpy(excludeList[excludeListSize],file);
275 /* Remove leading "/"s */
276 if (*excludeList[excludeListSize] == '/')
277 excludeList[excludeListSize] = (excludeList[excludeListSize])+1;
278 /* Tack a NULL onto the end of the list */
279 excludeList[++excludeListSize] = NULL;
280 }
281
282 fclose(fileList);
283 stopIt=TRUE;
284 break;
Erik Andersen0817d132000-04-09 15:17:40 +0000285#endif
Eric Andersen46a98df2000-09-19 21:35:14 +0000286 case '-':
287 break;
Erik Andersenecd51242000-04-08 03:08:21 +0000288 default:
Matt Kraai43c8c382000-09-04 16:51:55 +0000289 usage(tar_usage);
Eric Andersen46a98df2000-09-19 21:35:14 +0000290 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000291 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000292 }
293
Erik Andersene49d5ec2000-02-08 19:58:47 +0000294 /*
295 * Do the correct type of action supplying the rest of the
296 * command line arguments as the list of files to process.
297 */
298 if (createFlag == TRUE) {
Erik Andersende552872000-01-23 01:34:05 +0000299#ifndef BB_FEATURE_TAR_CREATE
Mark Whitleyf57c9442000-12-07 19:56:48 +0000300 error_msg_and_die( "This version of tar was not compiled with tar creation support.\n");
Erik Andersende552872000-01-23 01:34:05 +0000301#else
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000302#ifdef BB_FEATURE_TAR_GZIP
303 if (unzipFlag==TRUE)
304 error_msg_and_die("Creation of compressed not internally support by tar, pipe to busybox gunzip\n");
305#endif
Matt Kraai3e856ce2000-12-01 02:55:13 +0000306 status = writeTarFile(tarName, verboseFlag, argv, excludeList);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000307#endif
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000308 }
309 if (listFlag == TRUE || extractFlag == TRUE) {
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000310 int tarFd;
Matt Kraaic119ab92000-11-30 04:44:54 +0000311 if (*argv)
312 extractList = argv;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000313 /* Open the tar file for reading. */
314 if (!strcmp(tarName, "-"))
315 tarFd = fileno(stdin);
316 else
317 tarFd = open(tarName, O_RDONLY);
318 if (tarFd < 0)
319 error_msg_and_die( "Error opening '%s': %s\n", tarName, strerror(errno));
320
321#ifdef BB_FEATURE_TAR_GZIP
322 /* unzip tarFd in a seperate process */
323 if (unzipFlag == TRUE)
324 tarFd = tar_unzip_init(tarFd);
325#endif
326 status = readTarFile(tarFd, extractFlag, listFlag, tostdoutFlag,
Matt Kraai3e856ce2000-12-01 02:55:13 +0000327 verboseFlag, extractList, excludeList);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000328 }
Erik Andersende552872000-01-23 01:34:05 +0000329
Matt Kraai3e856ce2000-12-01 02:55:13 +0000330 if (status == TRUE)
331 return EXIT_SUCCESS;
332 else
333 return EXIT_FAILURE;
334
Erik Andersene49d5ec2000-02-08 19:58:47 +0000335 flagError:
Mark Whitleyf57c9442000-12-07 19:56:48 +0000336 error_msg_and_die( "Exactly one of 'c', 'x' or 't' must be specified\n");
Erik Andersen298854f2000-03-23 01:09:18 +0000337}
338
339static void
Erik Andersen6a34b532000-04-07 06:55:38 +0000340fixUpPermissions(TarInfo *header)
341{
342 struct utimbuf t;
343 /* Now set permissions etc for the new file */
344 chown(header->name, header->uid, header->gid);
345 chmod(header->name, header->mode);
346 /* Reset the time */
347 t.actime = time(0);
348 t.modtime = header->mtime;
349 utime(header->name, &t);
350}
351
352static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000353tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersen298854f2000-03-23 01:09:18 +0000354{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000355 size_t writeSize;
356 size_t readSize;
357 size_t actualWriteSz;
358 char buffer[BUFSIZ];
359 size_t size = header->size;
360 int outFd=fileno(stdout);
361
362 /* Open the file to be written, if a file is supposed to be written */
363 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000364 /* Create the path to the file, just in case it isn't there...
365 * This should not screw up path permissions or anything. */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000366 create_path(header->name, 0777);
Eric Andersen0c6a9702000-06-09 20:51:50 +0000367 if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY,
368 header->mode & ~S_IFMT)) < 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000369 error_msg(io_error, header->name, strerror(errno));
Eric Andersen0c6a9702000-06-09 20:51:50 +0000370 return( FALSE);
371 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000372 }
373
374 /* Write out the file, if we are supposed to be doing that */
375 while ( size > 0 ) {
376 actualWriteSz=0;
377 if ( size > sizeof(buffer) )
378 writeSize = readSize = sizeof(buffer);
379 else {
380 int mod = size % 512;
381 if ( mod != 0 )
382 readSize = size + (512 - mod);
383 else
384 readSize = size;
385 writeSize = size;
386 }
Mark Whitleyf57c9442000-12-07 19:56:48 +0000387 if ( (readSize = full_read(header->tarFd, buffer, readSize)) <= 0 ) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000388 /* Tarball seems to have a problem */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000389 error_msg("Unexpected EOF in archive\n");
Erik Andersen6a34b532000-04-07 06:55:38 +0000390 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000391 }
392 if ( readSize < writeSize )
393 writeSize = readSize;
394
395 /* Write out the file, if we are supposed to be doing that */
396 if (extractFlag==TRUE) {
397
Mark Whitleyf57c9442000-12-07 19:56:48 +0000398 if ((actualWriteSz=full_write(outFd, buffer, writeSize)) != writeSize ) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000399 /* Output file seems to have a problem */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000400 error_msg(io_error, header->name, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000401 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000402 }
Erik Andersen0817d132000-04-09 15:17:40 +0000403 } else {
404 actualWriteSz=writeSize;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000405 }
406
407 size -= actualWriteSz;
408 }
409
410 /* Now we are done writing the file out, so try
411 * and fix up the permissions and whatnot */
412 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000413 close(outFd);
Erik Andersen6a34b532000-04-07 06:55:38 +0000414 fixUpPermissions(header);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000415 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000416 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000417}
418
Erik Andersen6a34b532000-04-07 06:55:38 +0000419static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000420tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000421{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000422
423 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000424 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000425
Mark Whitleyf57c9442000-12-07 19:56:48 +0000426 if (create_path(header->name, header->mode) != TRUE) {
427 error_msg("%s: Cannot mkdir: %s\n",
Erik Andersen6a34b532000-04-07 06:55:38 +0000428 header->name, strerror(errno));
429 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000430 }
431 /* make the final component, just in case it was
Mark Whitleyf57c9442000-12-07 19:56:48 +0000432 * omitted by create_path() (which will skip the
Erik Andersen1ad302a2000-03-24 00:54:46 +0000433 * directory if it doesn't have a terminating '/') */
Erik Andersen6acaa402000-03-26 14:03:20 +0000434 if (mkdir(header->name, header->mode) == 0) {
435 fixUpPermissions(header);
436 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000437 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000438}
439
Erik Andersen6a34b532000-04-07 06:55:38 +0000440static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000441tarExtractHardLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000442{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000443 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000444 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000445
446 if (link(header->linkname, header->name) < 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000447 error_msg("%s: Cannot create hard link to '%s': %s\n",
Erik Andersen5661fe02000-04-05 01:00:52 +0000448 header->name, header->linkname, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000449 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000450 }
451
452 /* Now set permissions etc for the new directory */
453 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000454 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000455}
456
Erik Andersen6a34b532000-04-07 06:55:38 +0000457static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000458tarExtractSymLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000459{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000460 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000461 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000462
463#ifdef S_ISLNK
464 if (symlink(header->linkname, header->name) < 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000465 error_msg("%s: Cannot create symlink to '%s': %s\n",
Erik Andersen5661fe02000-04-05 01:00:52 +0000466 header->name, header->linkname, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000467 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000468 }
469 /* Try to change ownership of the symlink.
470 * If libs doesn't support that, don't bother.
471 * Changing the pointed-to-file is the Wrong Thing(tm).
472 */
473#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
474 lchown(header->name, header->uid, header->gid);
475#endif
476
477 /* Do not change permissions or date on symlink,
478 * since it changes the pointed to file instead. duh. */
479#else
Mark Whitleyf57c9442000-12-07 19:56:48 +0000480 error_msg("%s: Cannot create symlink to '%s': %s\n",
Erik Andersen6a34b532000-04-07 06:55:38 +0000481 header->name, header->linkname,
482 "symlinks not supported");
Erik Andersen1ad302a2000-03-24 00:54:46 +0000483#endif
Erik Andersen6a34b532000-04-07 06:55:38 +0000484 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000485}
486
Erik Andersen6a34b532000-04-07 06:55:38 +0000487static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000488tarExtractSpecial(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000489{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000490 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000491 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000492
493 if (S_ISCHR(header->mode) || S_ISBLK(header->mode) || S_ISSOCK(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000494 if (mknod(header->name, header->mode, makedev(header->devmajor, header->devminor)) < 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000495 error_msg("%s: Cannot mknod: %s\n",
Erik Andersen6a34b532000-04-07 06:55:38 +0000496 header->name, strerror(errno));
497 return( FALSE);
498 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000499 } else if (S_ISFIFO(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000500 if (mkfifo(header->name, header->mode) < 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000501 error_msg("%s: Cannot mkfifo: %s\n",
Erik Andersen6a34b532000-04-07 06:55:38 +0000502 header->name, strerror(errno));
503 return( FALSE);
504 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000505 }
506
507 /* Now set permissions etc for the new directory */
508 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000509 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000510}
511
Erik Andersen1ad302a2000-03-24 00:54:46 +0000512/* Read an octal value in a field of the specified width, with optional
Erik Andersen298854f2000-03-23 01:09:18 +0000513 * spaces on both sides of the number and with an optional null character
Erik Andersen1ad302a2000-03-24 00:54:46 +0000514 * at the end. Returns -1 on an illegal format. */
Erik Andersen298854f2000-03-23 01:09:18 +0000515static long getOctal(const char *cp, int size)
Eric Andersencc8ed391999-10-05 16:24:54 +0000516{
Erik Andersen298854f2000-03-23 01:09:18 +0000517 long val = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000518
Erik Andersen298854f2000-03-23 01:09:18 +0000519 for(;(size > 0) && (*cp == ' '); cp++, size--);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000520 if ((size == 0) || !is_octal(*cp))
Erik Andersen298854f2000-03-23 01:09:18 +0000521 return -1;
Mark Whitleyf57c9442000-12-07 19:56:48 +0000522 for(; (size > 0) && is_octal(*cp); size--) {
Erik Andersen298854f2000-03-23 01:09:18 +0000523 val = val * 8 + *cp++ - '0';
Eric Andersencc8ed391999-10-05 16:24:54 +0000524 }
Erik Andersen298854f2000-03-23 01:09:18 +0000525 for (;(size > 0) && (*cp == ' '); cp++, size--);
526 if ((size > 0) && *cp)
527 return -1;
528 return val;
529}
Eric Andersencc8ed391999-10-05 16:24:54 +0000530
Erik Andersen1ad302a2000-03-24 00:54:46 +0000531
Erik Andersen298854f2000-03-23 01:09:18 +0000532/* Parse the tar header and fill in the nice struct with the details */
533static int
Erik Andersen3364d782000-03-28 00:58:14 +0000534readTarHeader(struct TarHeader *rawHeader, struct TarInfo *header)
Erik Andersen298854f2000-03-23 01:09:18 +0000535{
Erik Andersene454fb62000-03-23 04:27:58 +0000536 int i;
Erik Andersen84e09e42000-04-08 20:58:35 +0000537 long chksum, sum=0;
Erik Andersene454fb62000-03-23 04:27:58 +0000538 unsigned char *s = (unsigned char *)rawHeader;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000539
Erik Andersen298854f2000-03-23 01:09:18 +0000540 header->name = rawHeader->name;
Erik Andersen84e09e42000-04-08 20:58:35 +0000541 /* Check for and relativify any absolute paths */
542 if ( *(header->name) == '/' ) {
543 static int alreadyWarned=FALSE;
544
545 while (*(header->name) == '/')
546 ++*(header->name);
547
548 if (alreadyWarned == FALSE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000549 error_msg("Removing leading '/' from member names\n");
Erik Andersen84e09e42000-04-08 20:58:35 +0000550 alreadyWarned = TRUE;
551 }
552 }
553
Erik Andersen298854f2000-03-23 01:09:18 +0000554 header->mode = getOctal(rawHeader->mode, sizeof(rawHeader->mode));
555 header->uid = getOctal(rawHeader->uid, sizeof(rawHeader->uid));
556 header->gid = getOctal(rawHeader->gid, sizeof(rawHeader->gid));
557 header->size = getOctal(rawHeader->size, sizeof(rawHeader->size));
558 header->mtime = getOctal(rawHeader->mtime, sizeof(rawHeader->mtime));
559 chksum = getOctal(rawHeader->chksum, sizeof(rawHeader->chksum));
560 header->type = rawHeader->typeflag;
561 header->linkname = rawHeader->linkname;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000562 header->devmajor = getOctal(rawHeader->devmajor, sizeof(rawHeader->devmajor));
563 header->devminor = getOctal(rawHeader->devminor, sizeof(rawHeader->devminor));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000564
Erik Andersen298854f2000-03-23 01:09:18 +0000565 /* Check the checksum */
Erik Andersen84e09e42000-04-08 20:58:35 +0000566 for (i = sizeof(*rawHeader); i-- != 0;) {
Erik Andersen298854f2000-03-23 01:09:18 +0000567 sum += *s++;
Erik Andersen84e09e42000-04-08 20:58:35 +0000568 }
569 /* Remove the effects of the checksum field (replace
570 * with blanks for the purposes of the checksum) */
571 s = rawHeader->chksum;
572 for (i = sizeof(rawHeader->chksum) ; i-- != 0;) {
573 sum -= *s++;
574 }
575 sum += ' ' * sizeof(rawHeader->chksum);
Erik Andersene454fb62000-03-23 04:27:58 +0000576 if (sum == chksum )
Erik Andersen298854f2000-03-23 01:09:18 +0000577 return ( TRUE);
578 return( FALSE);
579}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000580
Erik Andersene49d5ec2000-02-08 19:58:47 +0000581
Erik Andersen1ad302a2000-03-24 00:54:46 +0000582/*
583 * Read a tar file and extract or list the specified files within it.
584 * If the list is empty than all files are extracted or listed.
585 */
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000586extern int readTarFile(int tarFd, int extractFlag, int listFlag,
Matt Kraaib92223b2000-09-04 08:25:42 +0000587 int tostdoutFlag, int verboseFlag, char** extractList,
588 char** excludeList)
Erik Andersen1ad302a2000-03-24 00:54:46 +0000589{
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000590 int status;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000591 int errorFlag=FALSE;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000592 int skipNextHeaderFlag=FALSE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000593 TarHeader rawHeader;
594 TarInfo header;
Erik Andersen0817d132000-04-09 15:17:40 +0000595 char** tmpList;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000596
Erik Andersene49d5ec2000-02-08 19:58:47 +0000597 /* Set the umask for this process so it doesn't
Erik Andersen1ad302a2000-03-24 00:54:46 +0000598 * screw up permission setting for us later. */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000599 umask(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000600
Erik Andersen1ad302a2000-03-24 00:54:46 +0000601 /* Read the tar file, and iterate over it one file at a time */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000602 while ( (status = full_read(tarFd, (char*)&rawHeader, TAR_BLOCK_SIZE)) == TAR_BLOCK_SIZE ) {
Erik Andersene2729152000-02-18 21:34:17 +0000603
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000604 /* Try to read the header */
Erik Andersen3364d782000-03-28 00:58:14 +0000605 if ( readTarHeader(&rawHeader, &header) == FALSE ) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000606 if ( *(header.name) == '\0' ) {
607 goto endgame;
608 } else {
609 errorFlag=TRUE;
Mark Whitleyf57c9442000-12-07 19:56:48 +0000610 error_msg("Bad tar header, skipping\n");
Erik Andersen1ad302a2000-03-24 00:54:46 +0000611 continue;
612 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000613 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000614 if ( *(header.name) == '\0' )
615 goto endgame;
Erik Andersen0817d132000-04-09 15:17:40 +0000616 header.tarFd = tarFd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000617
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000618 /* Skip funky extra GNU headers that precede long files */
619 if ( (header.type == GNULONGNAME) || (header.type == GNULONGLINK) ) {
620 skipNextHeaderFlag=TRUE;
Matt Kraaiab8f9e22000-11-18 01:28:57 +0000621 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
622 errorFlag = TRUE;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000623 continue;
624 }
625 if ( skipNextHeaderFlag == TRUE ) {
626 skipNextHeaderFlag=FALSE;
Mark Whitleyf57c9442000-12-07 19:56:48 +0000627 error_msg(name_longer_than_foo, NAME_SIZE);
Matt Kraaiab8f9e22000-11-18 01:28:57 +0000628 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
629 errorFlag = TRUE;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000630 continue;
631 }
632
Erik Andersen0817d132000-04-09 15:17:40 +0000633#if defined BB_FEATURE_TAR_EXCLUDE
634 {
635 int skipFlag=FALSE;
636 /* Check for excluded files.... */
637 for (tmpList=excludeList; tmpList && *tmpList; tmpList++) {
638 /* Do some extra hoop jumping for when directory names
639 * end in '/' but the entry in tmpList doesn't */
640 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
641 header.name[strlen(header.name)-1]=='/'
642 && strncmp( *tmpList, header.name,
643 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
644 /* If it is a regular file, pretend to extract it with
645 * the extractFlag set to FALSE, so the junk in the tarball
646 * is properly skipped over */
647 if ( header.type==REGTYPE || header.type==REGTYPE0 ) {
Matt Kraaiab8f9e22000-11-18 01:28:57 +0000648 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
649 errorFlag = TRUE;
Erik Andersen0817d132000-04-09 15:17:40 +0000650 }
651 skipFlag=TRUE;
652 break;
653 }
654 }
655 /* There are not the droids you're looking for, move along */
656 if (skipFlag==TRUE)
657 continue;
658 }
659#endif
Matt Kraaic119ab92000-11-30 04:44:54 +0000660 if (extractList != NULL) {
Matt Kraaib92223b2000-09-04 08:25:42 +0000661 int skipFlag = TRUE;
662 for (tmpList = extractList; *tmpList != NULL; tmpList++) {
663 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
664 header.name[strlen(header.name)-1]=='/'
665 && strncmp( *tmpList, header.name,
666 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
667 /* If it is a regular file, pretend to extract it with
668 * the extractFlag set to FALSE, so the junk in the tarball
669 * is properly skipped over */
670 skipFlag = FALSE;
Matt Kraaic119ab92000-11-30 04:44:54 +0000671 memmove(extractList+1, extractList,
672 sizeof(*extractList)*(tmpList-extractList));
673 extractList++;
Matt Kraaib92223b2000-09-04 08:25:42 +0000674 break;
675 }
676 }
677 /* There are not the droids you're looking for, move along */
678 if (skipFlag == TRUE) {
679 if ( header.type==REGTYPE || header.type==REGTYPE0 )
Matt Kraaiab8f9e22000-11-18 01:28:57 +0000680 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
681 errorFlag = TRUE;
Matt Kraaib92223b2000-09-04 08:25:42 +0000682 continue;
683 }
684 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000685
Matt Kraai82cfbad2000-09-15 21:18:43 +0000686 if (listFlag == TRUE) {
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000687 /* Special treatment if the list (-t) flag is on */
688 if (verboseFlag == TRUE) {
689 int len, len1;
690 char buf[35];
691 struct tm *tm = localtime (&(header.mtime));
692
Mark Whitleyf57c9442000-12-07 19:56:48 +0000693 len=printf("%s ", mode_string(header.mode));
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000694 memset(buf, 0, 8*sizeof(char));
695 my_getpwuid(buf, header.uid);
696 if (! *buf)
697 len+=printf("%d", header.uid);
698 else
699 len+=printf("%s", buf);
700 memset(buf, 0, 8*sizeof(char));
701 my_getgrgid(buf, header.gid);
702 if (! *buf)
703 len+=printf("/%-d ", header.gid);
704 else
705 len+=printf("/%-s ", buf);
706
707 if (header.type==CHRTYPE || header.type==BLKTYPE) {
708 len1=snprintf(buf, sizeof(buf), "%ld,%-ld ",
709 header.devmajor, header.devminor);
710 } else {
711 len1=snprintf(buf, sizeof(buf), "%lu ", (long)header.size);
712 }
713 /* Jump through some hoops to make the columns match up */
714 for(;(len+len1)<31;len++)
715 printf(" ");
716 printf(buf);
717
718 /* Use ISO 8610 time format */
719 if (tm) {
720 printf ("%04d-%02d-%02d %02d:%02d:%02d ",
721 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
722 tm->tm_hour, tm->tm_min, tm->tm_sec);
723 }
724 }
Eric Andersenfdd51032000-08-02 18:48:26 +0000725 printf("%s", header.name);
Matt Kraai82cfbad2000-09-15 21:18:43 +0000726 if (verboseFlag == TRUE) {
727 if (header.type==LNKTYPE) /* If this is a link, say so */
728 printf(" link to %s", header.linkname);
729 else if (header.type==SYMTYPE)
730 printf(" -> %s", header.linkname);
731 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000732 printf("\n");
733 }
734
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000735 /* List contents if we are supposed to do that */
736 if (verboseFlag == TRUE && extractFlag == TRUE) {
737 /* Now the normal listing */
738 FILE *vbFd = stdout;
739 if (tostdoutFlag == TRUE) // If the archive goes to stdout, verbose to stderr
740 vbFd = stderr;
741 fprintf(vbFd, "%s\n", header.name);
742 }
743
Matt Kraaif4462972000-09-01 02:50:48 +0000744 /* Remove files if we would overwrite them */
Matt Kraaida542f32000-09-01 02:53:01 +0000745 if (extractFlag == TRUE && tostdoutFlag == FALSE)
Matt Kraaif4462972000-09-01 02:50:48 +0000746 unlink(header.name);
Eric Andersencc8ed391999-10-05 16:24:54 +0000747
Erik Andersen1ad302a2000-03-24 00:54:46 +0000748 /* If we got here, we can be certain we have a legitimate
749 * header to work with. So work with it. */
750 switch ( header.type ) {
751 case REGTYPE:
752 case REGTYPE0:
753 /* If the name ends in a '/' then assume it is
754 * supposed to be a directory, and fall through */
755 if (header.name[strlen(header.name)-1] != '/') {
Erik Andersen6a34b532000-04-07 06:55:38 +0000756 if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE)
757 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000758 break;
759 }
760 case DIRTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000761 if (tarExtractDirectory( &header, extractFlag, tostdoutFlag)==FALSE)
762 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000763 break;
764 case LNKTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000765 if (tarExtractHardLink( &header, extractFlag, tostdoutFlag)==FALSE)
766 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000767 break;
768 case SYMTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000769 if (tarExtractSymLink( &header, extractFlag, tostdoutFlag)==FALSE)
770 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000771 break;
772 case CHRTYPE:
773 case BLKTYPE:
774 case FIFOTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000775 if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE)
776 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000777 break;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000778#if 0
779 /* Handled earlier */
780 case GNULONGNAME:
781 case GNULONGLINK:
782 skipNextHeaderFlag=TRUE;
783 break;
784#endif
Erik Andersen1ad302a2000-03-24 00:54:46 +0000785 default:
Mark Whitleyf57c9442000-12-07 19:56:48 +0000786 error_msg("Unknown file type '%c' in tar file\n", header.type);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000787 close( tarFd);
788 return( FALSE);
789 }
790 }
791 close(tarFd);
792 if (status > 0) {
793 /* Bummer - we read a partial header */
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000794 error_msg( "Error reading tar file: %s\n", strerror(errno));
Erik Andersen1ad302a2000-03-24 00:54:46 +0000795 return ( FALSE);
796 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000797 else if (errorFlag==TRUE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000798 error_msg( "Error exit delayed from previous errors\n");
Erik Andersen6a34b532000-04-07 06:55:38 +0000799 return( FALSE);
800 } else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000801 return( status);
802
803 /* Stuff to do when we are done */
804endgame:
805 close( tarFd);
Matt Kraaic119ab92000-11-30 04:44:54 +0000806 if (extractList != NULL) {
807 for (; *extractList != NULL; extractList++) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000808 error_msg("%s: Not found in archive\n", *extractList);
Matt Kraaic119ab92000-11-30 04:44:54 +0000809 errorFlag = TRUE;
810 }
811 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000812 if ( *(header.name) == '\0' ) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000813 if (errorFlag==TRUE)
Mark Whitleyf57c9442000-12-07 19:56:48 +0000814 error_msg( "Error exit delayed from previous errors\n");
Erik Andersen6a34b532000-04-07 06:55:38 +0000815 else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000816 return( TRUE);
817 }
818 return( FALSE);
819}
820
Erik Andersen6acaa402000-03-26 14:03:20 +0000821
822#ifdef BB_FEATURE_TAR_CREATE
823
Eric Andersen3d957c82000-12-07 00:34:58 +0000824/*
825** writeTarFile(), writeFileToTarball(), and writeTarHeader() are
826** the only functions that deal with the HardLinkInfo structure.
827** Even these functions use the xxxHardLinkInfo() functions.
828*/
829typedef struct HardLinkInfo HardLinkInfo;
830struct HardLinkInfo
831{
832 HardLinkInfo *next; /* Next entry in list */
833 dev_t dev; /* Device number */
834 ino_t ino; /* Inode number */
835 short linkCount; /* (Hard) Link Count */
836 char name[1]; /* Start of filename (must be last) */
837};
838
Erik Andersen68a9ea42000-04-04 18:39:50 +0000839/* Some info to be carried along when creating a new tarball */
840struct TarBallInfo
841{
842 char* fileName; /* File name of the tarball */
843 int tarFd; /* Open-for-write file descriptor
844 for the tarball */
845 struct stat statBuf; /* Stat info for the tarball, letting
846 us know the inode and device that the
847 tarball lives, so we can avoid trying
848 to include the tarball into itself */
849 int verboseFlag; /* Whether to print extra stuff or not */
Erik Andersenecd51242000-04-08 03:08:21 +0000850 char** excludeList; /* List of files to not include */
Eric Andersen3d957c82000-12-07 00:34:58 +0000851 HardLinkInfo *hlInfoHead; /* Hard Link Tracking Information */
852 HardLinkInfo *hlInfo; /* Hard Link Info for the current file */
Erik Andersen68a9ea42000-04-04 18:39:50 +0000853};
854typedef struct TarBallInfo TarBallInfo;
855
856
Eric Andersen3d957c82000-12-07 00:34:58 +0000857/* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */
858static void
859addHardLinkInfo (HardLinkInfo **hlInfoHeadPtr, dev_t dev, ino_t ino,
860 short linkCount, const char *name)
861{
862 /* Note: hlInfoHeadPtr can never be NULL! */
863 HardLinkInfo *hlInfo;
864
865 hlInfo = (HardLinkInfo *)xmalloc(sizeof(HardLinkInfo)+strlen(name)+1);
866 if (hlInfo) {
867 hlInfo->next = *hlInfoHeadPtr;
868 *hlInfoHeadPtr = hlInfo;
869 hlInfo->dev = dev;
870 hlInfo->ino = ino;
871 hlInfo->linkCount = linkCount;
872 strcpy(hlInfo->name, name);
873 }
874 return;
875}
876
877static void
878freeHardLinkInfo (HardLinkInfo **hlInfoHeadPtr)
879{
880 HardLinkInfo *hlInfo = NULL;
881 HardLinkInfo *hlInfoNext = NULL;
882
883 if (hlInfoHeadPtr) {
884 hlInfo = *hlInfoHeadPtr;
885 while (hlInfo) {
886 hlInfoNext = hlInfo->next;
887 free(hlInfo);
888 hlInfo = hlInfoNext;
889 }
890 *hlInfoHeadPtr = NULL;
891 }
892 return;
893}
894
895/* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */
896static HardLinkInfo *
897findHardLinkInfo (HardLinkInfo *hlInfo, dev_t dev, ino_t ino)
898{
899 while(hlInfo) {
900 if ((ino == hlInfo->ino) && (dev == hlInfo->dev))
901 break;
902 hlInfo = hlInfo->next;
903 }
904 return(hlInfo);
905}
906
Erik Andersen6acaa402000-03-26 14:03:20 +0000907/* Put an octal string into the specified buffer.
908 * The number is zero and space padded and possibly null padded.
909 * Returns TRUE if successful. */
910static int putOctal (char *cp, int len, long value)
911{
912 int tempLength;
Erik Andersen6acaa402000-03-26 14:03:20 +0000913 char tempBuffer[32];
Erik Andersen5661fe02000-04-05 01:00:52 +0000914 char *tempString = tempBuffer;
Erik Andersen6acaa402000-03-26 14:03:20 +0000915
916 /* Create a string of the specified length with an initial space,
917 * leading zeroes and the octal number, and a trailing null. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000918 sprintf (tempString, "%0*lo", len - 1, value);
Erik Andersen6acaa402000-03-26 14:03:20 +0000919
920 /* If the string is too large, suppress the leading space. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000921 tempLength = strlen (tempString) + 1;
Erik Andersen6acaa402000-03-26 14:03:20 +0000922 if (tempLength > len) {
923 tempLength--;
924 tempString++;
925 }
926
927 /* If the string is still too large, suppress the trailing null. */
928 if (tempLength > len)
929 tempLength--;
930
931 /* If the string is still too large, fail. */
932 if (tempLength > len)
933 return FALSE;
934
935 /* Copy the string to the field. */
936 memcpy (cp, tempString, len);
937
938 return TRUE;
939}
940
Erik Andersen68a9ea42000-04-04 18:39:50 +0000941/* Write out a tar header for the specified file/directory/whatever */
Erik Andersen3364d782000-03-28 00:58:14 +0000942static int
Erik Andersen5661fe02000-04-05 01:00:52 +0000943writeTarHeader(struct TarBallInfo *tbInfo, const char *fileName, struct stat *statbuf)
Erik Andersen6acaa402000-03-26 14:03:20 +0000944{
Erik Andersen5661fe02000-04-05 01:00:52 +0000945 long chksum=0;
946 struct TarHeader header;
Erik Andersen0817d132000-04-09 15:17:40 +0000947#if defined BB_FEATURE_TAR_EXCLUDE
Erik Andersenecd51242000-04-08 03:08:21 +0000948 char** tmpList;
Erik Andersen0817d132000-04-09 15:17:40 +0000949#endif
Erik Andersen5661fe02000-04-05 01:00:52 +0000950 const unsigned char *cp = (const unsigned char *) &header;
951 ssize_t size = sizeof(struct TarHeader);
Eric Andersenfdd51032000-08-02 18:48:26 +0000952
Erik Andersen5661fe02000-04-05 01:00:52 +0000953 memset( &header, 0, size);
Erik Andersen3364d782000-03-28 00:58:14 +0000954
Erik Andersen68a9ea42000-04-04 18:39:50 +0000955 if (*fileName=='/') {
956 static int alreadyWarned=FALSE;
957 if (alreadyWarned==FALSE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000958 error_msg("Removing leading '/' from member names\n");
Erik Andersen68a9ea42000-04-04 18:39:50 +0000959 alreadyWarned=TRUE;
960 }
Erik Andersen84e09e42000-04-08 20:58:35 +0000961 strncpy(header.name, fileName+1, sizeof(header.name));
Erik Andersen3364d782000-03-28 00:58:14 +0000962 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000963 else {
Erik Andersen84e09e42000-04-08 20:58:35 +0000964 strncpy(header.name, fileName, sizeof(header.name));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000965 }
Erik Andersen84e09e42000-04-08 20:58:35 +0000966
Erik Andersen0817d132000-04-09 15:17:40 +0000967#if defined BB_FEATURE_TAR_EXCLUDE
968 /* Check for excluded files.... */
Erik Andersenecd51242000-04-08 03:08:21 +0000969 for (tmpList=tbInfo->excludeList; tmpList && *tmpList; tmpList++) {
Erik Andersen0817d132000-04-09 15:17:40 +0000970 /* Do some extra hoop jumping for when directory names
971 * end in '/' but the entry in tmpList doesn't */
972 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
973 header.name[strlen(header.name)-1]=='/'
974 && strncmp( *tmpList, header.name,
975 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
976 /* Set the mode to something that is not a regular file, thereby
977 * faking out writeTarFile into thinking that nothing further need
978 * be done for this file. Yes, I know this is ugly, but it works. */
979 statbuf->st_mode = 0;
980 return( TRUE);
981 }
Erik Andersenecd51242000-04-08 03:08:21 +0000982 }
Erik Andersen0817d132000-04-09 15:17:40 +0000983#endif
Erik Andersenecd51242000-04-08 03:08:21 +0000984
Erik Andersen5661fe02000-04-05 01:00:52 +0000985 putOctal(header.mode, sizeof(header.mode), statbuf->st_mode);
986 putOctal(header.uid, sizeof(header.uid), statbuf->st_uid);
987 putOctal(header.gid, sizeof(header.gid), statbuf->st_gid);
988 putOctal(header.size, sizeof(header.size), 0); /* Regular file size is handled later */
989 putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime);
990 strncpy(header.magic, TAR_MAGIC TAR_VERSION,
991 TAR_MAGIC_LEN + TAR_VERSION_LEN );
Erik Andersen68a9ea42000-04-04 18:39:50 +0000992
Erik Andersen84e09e42000-04-08 20:58:35 +0000993 /* Enter the user and group names (default to root if it fails) */
Erik Andersen5661fe02000-04-05 01:00:52 +0000994 my_getpwuid(header.uname, statbuf->st_uid);
Erik Andersen5661fe02000-04-05 01:00:52 +0000995 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000996 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +0000997 my_getgrgid(header.gname, statbuf->st_gid);
998 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000999 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +00001000
Eric Andersen3d957c82000-12-07 00:34:58 +00001001 if (tbInfo->hlInfo) {
1002 /* This is a hard link */
1003 header.typeflag = LNKTYPE;
1004 strncpy(header.linkname, tbInfo->hlInfo->name, sizeof(header.linkname));
1005 } else if (S_ISLNK(statbuf->st_mode)) {
Eric Andersen3adffb72000-06-26 10:54:06 +00001006 int link_size=0;
Erik Andersen5661fe02000-04-05 01:00:52 +00001007 char buffer[BUFSIZ];
1008 header.typeflag = SYMTYPE;
Eric Andersen3adffb72000-06-26 10:54:06 +00001009 link_size = readlink(fileName, buffer, sizeof(buffer) - 1);
1010 if ( link_size < 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001011 error_msg("Error reading symlink '%s': %s\n", header.name, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +00001012 return ( FALSE);
1013 }
Eric Andersen3adffb72000-06-26 10:54:06 +00001014 buffer[link_size] = '\0';
Erik Andersen95c1c1e2000-04-14 21:45:29 +00001015 strncpy(header.linkname, buffer, sizeof(header.linkname));
Erik Andersen68a9ea42000-04-04 18:39:50 +00001016 } else if (S_ISDIR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +00001017 header.typeflag = DIRTYPE;
1018 strncat(header.name, "/", sizeof(header.name));
Erik Andersen68a9ea42000-04-04 18:39:50 +00001019 } else if (S_ISCHR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +00001020 header.typeflag = CHRTYPE;
1021 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
1022 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +00001023 } else if (S_ISBLK(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +00001024 header.typeflag = BLKTYPE;
1025 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
1026 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +00001027 } else if (S_ISFIFO(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +00001028 header.typeflag = FIFOTYPE;
1029 } else if (S_ISREG(statbuf->st_mode)) {
1030 header.typeflag = REGTYPE;
1031 putOctal(header.size, sizeof(header.size), statbuf->st_size);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001032 } else {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001033 error_msg("%s: Unknown file type\n", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001034 return ( FALSE);
1035 }
Erik Andersen68a9ea42000-04-04 18:39:50 +00001036
Erik Andersen5661fe02000-04-05 01:00:52 +00001037 /* Calculate and store the checksum (i.e. the sum of all of the bytes of
1038 * the header). The checksum field must be filled with blanks for the
1039 * calculation. The checksum field is formatted differently from the
1040 * other fields: it has [6] digits, a null, then a space -- rather than
1041 * digits, followed by a null like the other fields... */
1042 memset(header.chksum, ' ', sizeof(header.chksum));
1043 cp = (const unsigned char *) &header;
1044 while (size-- > 0)
1045 chksum += *cp++;
1046 putOctal(header.chksum, 7, chksum);
1047
1048 /* Now write the header out to disk */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001049 if ((size=full_write(tbInfo->tarFd, (char*)&header, sizeof(struct TarHeader))) < 0) {
1050 error_msg(io_error, fileName, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +00001051 return ( FALSE);
1052 }
1053 /* Pad the header up to the tar block size */
1054 for (; size<TAR_BLOCK_SIZE; size++) {
1055 write(tbInfo->tarFd, "\0", 1);
1056 }
1057 /* Now do the verbose thing (or not) */
Eric Andersenfdd51032000-08-02 18:48:26 +00001058 if (tbInfo->verboseFlag==TRUE) {
1059 FILE *vbFd = stdout;
1060 if (tbInfo->tarFd == fileno(stdout)) // If the archive goes to stdout, verbose to stderr
1061 vbFd = stderr;
1062 fprintf(vbFd, "%s\n", header.name);
1063 }
Erik Andersen3364d782000-03-28 00:58:14 +00001064
1065 return ( TRUE);
1066}
1067
1068
Erik Andersen68a9ea42000-04-04 18:39:50 +00001069static int writeFileToTarball(const char *fileName, struct stat *statbuf, void* userData)
Erik Andersen3364d782000-03-28 00:58:14 +00001070{
Erik Andersen68a9ea42000-04-04 18:39:50 +00001071 struct TarBallInfo *tbInfo = (struct TarBallInfo *)userData;
Erik Andersen68a9ea42000-04-04 18:39:50 +00001072
Eric Andersen3d957c82000-12-07 00:34:58 +00001073 /*
1074 ** Check to see if we are dealing with a hard link.
1075 ** If so -
1076 ** Treat the first occurance of a given dev/inode as a file while
1077 ** treating any additional occurances as hard links. This is done
1078 ** by adding the file information to the HardLinkInfo linked list.
1079 */
1080 tbInfo->hlInfo = NULL;
1081 if (statbuf->st_nlink > 1) {
1082 tbInfo->hlInfo = findHardLinkInfo(tbInfo->hlInfoHead, statbuf->st_dev,
1083 statbuf->st_ino);
1084 if (tbInfo->hlInfo == NULL)
1085 addHardLinkInfo (&tbInfo->hlInfoHead, statbuf->st_dev,
1086 statbuf->st_ino, statbuf->st_nlink, fileName);
1087 }
1088
Erik Andersen68a9ea42000-04-04 18:39:50 +00001089 /* It is against the rules to archive a socket */
1090 if (S_ISSOCK(statbuf->st_mode)) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001091 error_msg("%s: socket ignored\n", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001092 return( TRUE);
1093 }
1094
1095 /* It is a bad idea to store the archive we are in the process of creating,
1096 * so check the device and inode to be sure that this particular file isn't
1097 * the new tarball */
1098 if (tbInfo->statBuf.st_dev == statbuf->st_dev &&
1099 tbInfo->statBuf.st_ino == statbuf->st_ino) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001100 error_msg("%s: file is the archive; skipping\n", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001101 return( TRUE);
1102 }
1103
Eric Andersen1b1cfde2000-09-24 00:54:37 +00001104 if (strlen(fileName) >= NAME_SIZE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001105 error_msg(name_longer_than_foo, NAME_SIZE);
Eric Andersen1b1cfde2000-09-24 00:54:37 +00001106 return ( TRUE);
1107 }
1108
Erik Andersen5661fe02000-04-05 01:00:52 +00001109 if (writeTarHeader(tbInfo, fileName, statbuf)==FALSE) {
1110 return( FALSE);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001111 }
Erik Andersen5661fe02000-04-05 01:00:52 +00001112
1113 /* Now, if the file is a regular file, copy it out to the tarball */
Eric Andersen3d957c82000-12-07 00:34:58 +00001114 if ((tbInfo->hlInfo == NULL)
1115 && (S_ISREG(statbuf->st_mode))) {
Erik Andersen5661fe02000-04-05 01:00:52 +00001116 int inputFileFd;
1117 char buffer[BUFSIZ];
1118 ssize_t size=0, readSize=0;
1119
1120 /* open the file we want to archive, and make sure all is well */
1121 if ((inputFileFd = open(fileName, O_RDONLY)) < 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001122 error_msg("%s: Cannot open: %s\n", fileName, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +00001123 return( FALSE);
1124 }
1125
1126 /* write the file to the archive */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001127 while ( (size = full_read(inputFileFd, buffer, sizeof(buffer))) > 0 ) {
1128 if (full_write(tbInfo->tarFd, buffer, size) != size ) {
Erik Andersen5661fe02000-04-05 01:00:52 +00001129 /* Output file seems to have a problem */
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 readSize+=size;
1134 }
1135 if (size == -1) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001136 error_msg(io_error, fileName, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +00001137 return( FALSE);
1138 }
1139 /* Pad the file up to the tar block size */
1140 for (; (readSize%TAR_BLOCK_SIZE) != 0; readSize++) {
1141 write(tbInfo->tarFd, "\0", 1);
1142 }
1143 close( inputFileFd);
1144 }
Erik Andersen68a9ea42000-04-04 18:39:50 +00001145
1146 return( TRUE);
Erik Andersen6acaa402000-03-26 14:03:20 +00001147}
1148
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001149static int writeTarFile(const char* tarName, int verboseFlag, char **argv,
1150 char** excludeList)
Erik Andersen6acaa402000-03-26 14:03:20 +00001151{
Erik Andersen3364d782000-03-28 00:58:14 +00001152 int tarFd=-1;
Erik Andersen68a9ea42000-04-04 18:39:50 +00001153 int errorFlag=FALSE;
Erik Andersen5661fe02000-04-05 01:00:52 +00001154 ssize_t size;
Erik Andersen68a9ea42000-04-04 18:39:50 +00001155 struct TarBallInfo tbInfo;
1156 tbInfo.verboseFlag = verboseFlag;
Eric Andersen3d957c82000-12-07 00:34:58 +00001157 tbInfo.hlInfoHead = NULL;
Erik Andersen3364d782000-03-28 00:58:14 +00001158
1159 /* Make sure there is at least one file to tar up. */
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001160 if (*argv == NULL)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001161 error_msg_and_die("Cowardly refusing to create an empty archive\n");
Erik Andersen6acaa402000-03-26 14:03:20 +00001162
1163 /* Open the tar file for writing. */
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001164 if (!strcmp(tarName, "-"))
Erik Andersen68a9ea42000-04-04 18:39:50 +00001165 tbInfo.tarFd = fileno(stdout);
Erik Andersen6acaa402000-03-26 14:03:20 +00001166 else
Erik Andersen68a9ea42000-04-04 18:39:50 +00001167 tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1168 if (tbInfo.tarFd < 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001169 error_msg( "Error opening '%s': %s\n", tarName, strerror(errno));
Eric Andersen3d957c82000-12-07 00:34:58 +00001170 freeHardLinkInfo(&tbInfo.hlInfoHead);
Erik Andersen6acaa402000-03-26 14:03:20 +00001171 return ( FALSE);
1172 }
Erik Andersenecd51242000-04-08 03:08:21 +00001173 tbInfo.excludeList=excludeList;
Erik Andersen68a9ea42000-04-04 18:39:50 +00001174 /* Store the stat info for the tarball's file, so
1175 * can avoid including the tarball into itself.... */
1176 if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001177 error_msg_and_die(io_error, tarName, strerror(errno));
Erik Andersen6acaa402000-03-26 14:03:20 +00001178
1179 /* Set the umask for this process so it doesn't
1180 * screw up permission setting for us later. */
1181 umask(0);
1182
1183 /* Read the directory/files and iterate over them one at a time */
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001184 while (*argv != NULL) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001185 if (recursive_action(*argv++, TRUE, FALSE, FALSE,
Erik Andersen68a9ea42000-04-04 18:39:50 +00001186 writeFileToTarball, writeFileToTarball,
1187 (void*) &tbInfo) == FALSE) {
1188 errorFlag = TRUE;
Erik Andersen3364d782000-03-28 00:58:14 +00001189 }
Erik Andersen6acaa402000-03-26 14:03:20 +00001190 }
Erik Andersen5661fe02000-04-05 01:00:52 +00001191 /* Write two empty blocks to the end of the archive */
1192 for (size=0; size<(2*TAR_BLOCK_SIZE); size++) {
1193 write(tbInfo.tarFd, "\0", 1);
1194 }
Erik Andersen0817d132000-04-09 15:17:40 +00001195
1196 /* To be pedantically correct, we would check if the tarball
Eric Andersen3c5ee9a2000-11-14 22:15:48 +00001197 * is smaller than 20 tar blocks, and pad it if it was smaller,
Erik Andersen0817d132000-04-09 15:17:40 +00001198 * but that isn't necessary for GNU tar interoperability, and
1199 * so is considered a waste of space */
1200
Erik Andersen68a9ea42000-04-04 18:39:50 +00001201 /* Hang up the tools, close up shop, head home */
Erik Andersen6acaa402000-03-26 14:03:20 +00001202 close(tarFd);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001203 if (errorFlag == TRUE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001204 error_msg("Error exit delayed from previous errors\n");
Eric Andersen3d957c82000-12-07 00:34:58 +00001205 freeHardLinkInfo(&tbInfo.hlInfoHead);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001206 return(FALSE);
1207 }
Eric Andersen3d957c82000-12-07 00:34:58 +00001208 freeHardLinkInfo(&tbInfo.hlInfoHead);
Erik Andersen6acaa402000-03-26 14:03:20 +00001209 return( TRUE);
1210}
1211
1212
1213#endif
1214