blob: e4bdd0a6c092a5e8fe5eaf638d915b9060a2e443 [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
39#include "internal.h"
Erik Andersen1ad302a2000-03-24 00:54:46 +000040#define BB_DECLARE_EXTERN
41#define bb_need_io_error
Eric Andersen1b1cfde2000-09-24 00:54:37 +000042#define bb_need_name_longer_then_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
Erik Andersen298854f2000-03-23 01:09:18 +000055/* Tar file constants */
Erik Andersen1ad302a2000-03-24 00:54:46 +000056#ifndef MAJOR
57#define MAJOR(dev) (((dev)>>8)&0xff)
58#define MINOR(dev) ((dev)&0xff)
59#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000060
Eric Andersen1b1cfde2000-09-24 00:54:37 +000061#define NAME_SIZE 100
Eric Andersencc8ed391999-10-05 16:24:54 +000062
Erik Andersen298854f2000-03-23 01:09:18 +000063/* POSIX tar Header Block, from POSIX 1003.1-1990 */
64struct TarHeader
65{
66 /* byte offset */
Eric Andersen1b1cfde2000-09-24 00:54:37 +000067 char name[NAME_SIZE]; /* 0-99 */
Erik Andersen0817d132000-04-09 15:17:40 +000068 char mode[8]; /* 100-107 */
69 char uid[8]; /* 108-115 */
70 char gid[8]; /* 116-123 */
71 char size[12]; /* 124-135 */
72 char mtime[12]; /* 136-147 */
73 char chksum[8]; /* 148-155 */
74 char typeflag; /* 156-156 */
Eric Andersen1b1cfde2000-09-24 00:54:37 +000075 char linkname[NAME_SIZE]; /* 157-256 */
Erik Andersen0817d132000-04-09 15:17:40 +000076 char magic[6]; /* 257-262 */
77 char version[2]; /* 263-264 */
78 char uname[32]; /* 265-296 */
79 char gname[32]; /* 297-328 */
80 char devmajor[8]; /* 329-336 */
81 char devminor[8]; /* 337-344 */
82 char prefix[155]; /* 345-499 */
83 char padding[12]; /* 500-512 (pad to exactly the TAR_BLOCK_SIZE) */
Erik Andersen298854f2000-03-23 01:09:18 +000084};
85typedef struct TarHeader TarHeader;
Eric Andersencc8ed391999-10-05 16:24:54 +000086
Eric Andersencc8ed391999-10-05 16:24:54 +000087
Erik Andersen298854f2000-03-23 01:09:18 +000088/* A few useful constants */
89#define TAR_MAGIC "ustar" /* ustar and a null */
Erik Andersen0817d132000-04-09 15:17:40 +000090#define TAR_VERSION " " /* Be compatable with GNU tar format */
Erik Andersen298854f2000-03-23 01:09:18 +000091#define TAR_MAGIC_LEN 6
92#define TAR_VERSION_LEN 2
Erik Andersen298854f2000-03-23 01:09:18 +000093#define TAR_BLOCK_SIZE 512
94
95/* A nice enum with all the possible tar file content types */
96enum TarFileType
97{
98 REGTYPE = '0', /* regular file */
99 REGTYPE0 = '\0', /* regular file (ancient bug compat)*/
100 LNKTYPE = '1', /* hard link */
101 SYMTYPE = '2', /* symbolic link */
102 CHRTYPE = '3', /* character special */
103 BLKTYPE = '4', /* block special */
104 DIRTYPE = '5', /* directory */
105 FIFOTYPE = '6', /* FIFO special */
106 CONTTYPE = '7', /* reserved */
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000107 GNULONGLINK = 'K', /* GNU long (>100 chars) link name */
108 GNULONGNAME = 'L', /* GNU long (>100 chars) file name */
Erik Andersen298854f2000-03-23 01:09:18 +0000109};
110typedef enum TarFileType TarFileType;
111
112/* This struct ignores magic, non-numeric user name,
113 * non-numeric group name, and the checksum, since
114 * these are all ignored by BusyBox tar. */
115struct TarInfo
116{
117 int tarFd; /* An open file descriptor for reading from the tarball */
118 char * name; /* File name */
119 mode_t mode; /* Unix mode, including device bits. */
120 uid_t uid; /* Numeric UID */
121 gid_t gid; /* Numeric GID */
122 size_t size; /* Size of file */
123 time_t mtime; /* Last-modified time */
124 enum TarFileType type; /* Regular, directory, link, etc */
125 char * linkname; /* Name for symbolic and hard links */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000126 long devmajor; /* Major number for special device */
127 long devminor; /* Minor number for special device */
Erik Andersen298854f2000-03-23 01:09:18 +0000128};
129typedef struct TarInfo TarInfo;
130
Erik Andersene454fb62000-03-23 04:27:58 +0000131/* Local procedures to restore files from a tar file. */
Erik Andersen298854f2000-03-23 01:09:18 +0000132static int readTarFile(const char* tarName, int extractFlag, int listFlag,
Matt Kraaib92223b2000-09-04 08:25:42 +0000133 int tostdoutFlag, int verboseFlag, char** extractList,
134 char** excludeList);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000135
136
Eric Andersencc8ed391999-10-05 16:24:54 +0000137
Erik Andersen05100cd2000-01-16 01:30:52 +0000138#ifdef BB_FEATURE_TAR_CREATE
Erik Andersen6acaa402000-03-26 14:03:20 +0000139/* Local procedures to save files into a tar file. */
Eric Andersen46a98df2000-09-19 21:35:14 +0000140static int writeTarFile(const char* tarName, int tostdoutFlag,
141 int verboseFlag, int argc, char **argv, char** excludeList);
Erik Andersen05100cd2000-01-16 01:30:52 +0000142#endif
143
Erik Andersene49d5ec2000-02-08 19:58:47 +0000144extern int tar_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000145{
Erik Andersenecd51242000-04-08 03:08:21 +0000146 char** excludeList=NULL;
Erik Andersen0817d132000-04-09 15:17:40 +0000147#if defined BB_FEATURE_TAR_EXCLUDE
Erik Andersenecd51242000-04-08 03:08:21 +0000148 int excludeListSize=0;
Erik Andersen0817d132000-04-09 15:17:40 +0000149#endif
Eric Andersen108a4432000-05-30 19:05:57 +0000150 const char *tarName="-";
Erik Andersen298854f2000-03-23 01:09:18 +0000151 int listFlag = FALSE;
152 int extractFlag = FALSE;
153 int createFlag = FALSE;
154 int verboseFlag = FALSE;
155 int tostdoutFlag = FALSE;
Eric Andersen0102a9f2000-09-23 22:36:24 +0000156 int firstOpt = TRUE;
Eric Andersen46a98df2000-09-19 21:35:14 +0000157 int stopIt;
158
Eric Andersencc8ed391999-10-05 16:24:54 +0000159
Erik Andersenecd51242000-04-08 03:08:21 +0000160 if (argc <= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000161 usage(tar_usage);
Eric Andersencc8ed391999-10-05 16:24:54 +0000162
Eric Andersenfdd51032000-08-02 18:48:26 +0000163 /* do normal option parsing */
Eric Andersen0102a9f2000-09-23 22:36:24 +0000164 while (--argc > 0 && ((*argv && **(++argv) == '-') ||
165 (firstOpt==TRUE && strspn(*argv, "-cxt") ))) {
166 firstOpt=FALSE;
Eric Andersen46a98df2000-09-19 21:35:14 +0000167 stopIt=FALSE;
168 while (stopIt==FALSE && *argv && **argv) {
169 switch (**argv) {
Erik Andersenecd51242000-04-08 03:08:21 +0000170 case 'c':
171 if (extractFlag == TRUE || listFlag == TRUE)
172 goto flagError;
173 createFlag = TRUE;
174 break;
Eric Andersenfdd51032000-08-02 18:48:26 +0000175 case 'x':
176 if (listFlag == TRUE || createFlag == TRUE)
177 goto flagError;
178 extractFlag = TRUE;
179 break;
180 case 't':
181 if (extractFlag == TRUE || createFlag == TRUE)
182 goto flagError;
183 listFlag = TRUE;
184 break;
Erik Andersenecd51242000-04-08 03:08:21 +0000185 case 'v':
186 verboseFlag = TRUE;
187 break;
Erik Andersenecd51242000-04-08 03:08:21 +0000188 case 'O':
189 tostdoutFlag = TRUE;
Eric Andersen46a98df2000-09-19 21:35:14 +0000190 tarName = "-";
Eric Andersenfdd51032000-08-02 18:48:26 +0000191 break;
192 case 'f':
Eric Andersen46a98df2000-09-19 21:35:14 +0000193 if (--argc == 0) {
194 fatalError( "Option requires an argument: No file specified\n");
195 }
Eric Andersenfdd51032000-08-02 18:48:26 +0000196 if (*tarName != '-')
197 fatalError( "Only one 'f' option allowed\n");
Eric Andersen46a98df2000-09-19 21:35:14 +0000198 tarName = *(++argv);
199 if (tarName == NULL)
200 fatalError( "Option requires an argument: No file specified\n");
201 if (!strcmp(tarName, "-") && createFlag == TRUE)
202 tostdoutFlag = TRUE;
203 stopIt=TRUE;
Erik Andersenecd51242000-04-08 03:08:21 +0000204 break;
Erik Andersen0817d132000-04-09 15:17:40 +0000205#if defined BB_FEATURE_TAR_EXCLUDE
Matt Kraai43c8c382000-09-04 16:51:55 +0000206 case 'e':
Eric Andersen46a98df2000-09-19 21:35:14 +0000207 if (strcmp(*argv, "-exclude")==0) {
208 if (--argc == 0) {
209 fatalError( "Option requires an argument: No file specified\n");
210 }
211 excludeList=xrealloc( excludeList, sizeof(char**) * (excludeListSize+2));
212 excludeList[excludeListSize] = *(++argv);
213 /* Remove leading "/"s */
214 if (*excludeList[excludeListSize] =='/')
215 excludeList[excludeListSize] = (excludeList[excludeListSize])+1;
216 /* Tack a NULL onto the end of the list */
217 excludeList[++excludeListSize] = NULL;
218 stopIt=TRUE;
219 break;
220 }
Erik Andersen0817d132000-04-09 15:17:40 +0000221#endif
Eric Andersen46a98df2000-09-19 21:35:14 +0000222 case '-':
223 break;
Erik Andersenecd51242000-04-08 03:08:21 +0000224 default:
Matt Kraai43c8c382000-09-04 16:51:55 +0000225 usage(tar_usage);
Eric Andersen46a98df2000-09-19 21:35:14 +0000226 }
227 ++(*argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000228 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000229 }
230
Erik Andersene49d5ec2000-02-08 19:58:47 +0000231 /*
232 * Do the correct type of action supplying the rest of the
233 * command line arguments as the list of files to process.
234 */
235 if (createFlag == TRUE) {
Erik Andersende552872000-01-23 01:34:05 +0000236#ifndef BB_FEATURE_TAR_CREATE
Erik Andersen298854f2000-03-23 01:09:18 +0000237 fatalError( "This version of tar was not compiled with tar creation support.\n");
Erik Andersende552872000-01-23 01:34:05 +0000238#else
Eric Andersen46a98df2000-09-19 21:35:14 +0000239 exit(writeTarFile(tarName, tostdoutFlag, verboseFlag, argc, argv, excludeList));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000240#endif
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000241 }
242 if (listFlag == TRUE || extractFlag == TRUE) {
Matt Kraaib92223b2000-09-04 08:25:42 +0000243 exit(readTarFile(tarName, extractFlag, listFlag, tostdoutFlag, verboseFlag, &argv[optind], excludeList));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000244 }
Erik Andersende552872000-01-23 01:34:05 +0000245
Erik Andersene49d5ec2000-02-08 19:58:47 +0000246 flagError:
Erik Andersen298854f2000-03-23 01:09:18 +0000247 fatalError( "Exactly one of 'c', 'x' or 't' must be specified\n");
248}
249
250static void
Erik Andersen6a34b532000-04-07 06:55:38 +0000251fixUpPermissions(TarInfo *header)
252{
253 struct utimbuf t;
254 /* Now set permissions etc for the new file */
255 chown(header->name, header->uid, header->gid);
256 chmod(header->name, header->mode);
257 /* Reset the time */
258 t.actime = time(0);
259 t.modtime = header->mtime;
260 utime(header->name, &t);
261}
262
263static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000264tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersen298854f2000-03-23 01:09:18 +0000265{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000266 size_t writeSize;
267 size_t readSize;
268 size_t actualWriteSz;
269 char buffer[BUFSIZ];
270 size_t size = header->size;
271 int outFd=fileno(stdout);
272
273 /* Open the file to be written, if a file is supposed to be written */
274 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000275 /* Create the path to the file, just in case it isn't there...
276 * This should not screw up path permissions or anything. */
277 createPath(header->name, 0777);
Eric Andersen0c6a9702000-06-09 20:51:50 +0000278 if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY,
279 header->mode & ~S_IFMT)) < 0) {
280 errorMsg(io_error, header->name, strerror(errno));
281 return( FALSE);
282 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000283 }
284
285 /* Write out the file, if we are supposed to be doing that */
286 while ( size > 0 ) {
287 actualWriteSz=0;
288 if ( size > sizeof(buffer) )
289 writeSize = readSize = sizeof(buffer);
290 else {
291 int mod = size % 512;
292 if ( mod != 0 )
293 readSize = size + (512 - mod);
294 else
295 readSize = size;
296 writeSize = size;
297 }
298 if ( (readSize = fullRead(header->tarFd, buffer, readSize)) <= 0 ) {
299 /* Tarball seems to have a problem */
Matt Kraaibe84cd42000-07-12 17:02:35 +0000300 errorMsg("Unexpected EOF in archive\n");
Erik Andersen6a34b532000-04-07 06:55:38 +0000301 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000302 }
303 if ( readSize < writeSize )
304 writeSize = readSize;
305
306 /* Write out the file, if we are supposed to be doing that */
307 if (extractFlag==TRUE) {
308
309 if ((actualWriteSz=fullWrite(outFd, buffer, writeSize)) != writeSize ) {
310 /* Output file seems to have a problem */
311 errorMsg(io_error, header->name, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000312 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000313 }
Erik Andersen0817d132000-04-09 15:17:40 +0000314 } else {
315 actualWriteSz=writeSize;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000316 }
317
318 size -= actualWriteSz;
319 }
320
321 /* Now we are done writing the file out, so try
322 * and fix up the permissions and whatnot */
323 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000324 close(outFd);
Erik Andersen6a34b532000-04-07 06:55:38 +0000325 fixUpPermissions(header);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000326 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000327 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000328}
329
Erik Andersen6a34b532000-04-07 06:55:38 +0000330static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000331tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000332{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000333
334 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000335 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000336
337 if (createPath(header->name, header->mode) != TRUE) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000338 errorMsg("%s: Cannot mkdir: %s\n",
Erik Andersen6a34b532000-04-07 06:55:38 +0000339 header->name, strerror(errno));
340 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000341 }
342 /* make the final component, just in case it was
343 * omitted by createPath() (which will skip the
344 * directory if it doesn't have a terminating '/') */
Erik Andersen6acaa402000-03-26 14:03:20 +0000345 if (mkdir(header->name, header->mode) == 0) {
346 fixUpPermissions(header);
347 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000348 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000349}
350
Erik Andersen6a34b532000-04-07 06:55:38 +0000351static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000352tarExtractHardLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000353{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000354 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000355 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000356
357 if (link(header->linkname, header->name) < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000358 errorMsg("%s: Cannot create hard link to '%s': %s\n",
Erik Andersen5661fe02000-04-05 01:00:52 +0000359 header->name, header->linkname, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000360 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000361 }
362
363 /* Now set permissions etc for the new directory */
364 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000365 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000366}
367
Erik Andersen6a34b532000-04-07 06:55:38 +0000368static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000369tarExtractSymLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000370{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000371 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000372 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000373
374#ifdef S_ISLNK
375 if (symlink(header->linkname, header->name) < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000376 errorMsg("%s: Cannot create symlink to '%s': %s\n",
Erik Andersen5661fe02000-04-05 01:00:52 +0000377 header->name, header->linkname, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000378 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000379 }
380 /* Try to change ownership of the symlink.
381 * If libs doesn't support that, don't bother.
382 * Changing the pointed-to-file is the Wrong Thing(tm).
383 */
384#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
385 lchown(header->name, header->uid, header->gid);
386#endif
387
388 /* Do not change permissions or date on symlink,
389 * since it changes the pointed to file instead. duh. */
390#else
Matt Kraaibe84cd42000-07-12 17:02:35 +0000391 errorMsg("%s: Cannot create symlink to '%s': %s\n",
Erik Andersen6a34b532000-04-07 06:55:38 +0000392 header->name, header->linkname,
393 "symlinks not supported");
Erik Andersen1ad302a2000-03-24 00:54:46 +0000394#endif
Erik Andersen6a34b532000-04-07 06:55:38 +0000395 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000396}
397
Erik Andersen6a34b532000-04-07 06:55:38 +0000398static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000399tarExtractSpecial(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000400{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000401 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000402 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000403
404 if (S_ISCHR(header->mode) || S_ISBLK(header->mode) || S_ISSOCK(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000405 if (mknod(header->name, header->mode, makedev(header->devmajor, header->devminor)) < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000406 errorMsg("%s: Cannot mknod: %s\n",
Erik Andersen6a34b532000-04-07 06:55:38 +0000407 header->name, strerror(errno));
408 return( FALSE);
409 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000410 } else if (S_ISFIFO(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000411 if (mkfifo(header->name, header->mode) < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000412 errorMsg("%s: Cannot mkfifo: %s\n",
Erik Andersen6a34b532000-04-07 06:55:38 +0000413 header->name, strerror(errno));
414 return( FALSE);
415 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000416 }
417
418 /* Now set permissions etc for the new directory */
419 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000420 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000421}
422
Erik Andersen1ad302a2000-03-24 00:54:46 +0000423/* Read an octal value in a field of the specified width, with optional
Erik Andersen298854f2000-03-23 01:09:18 +0000424 * spaces on both sides of the number and with an optional null character
Erik Andersen1ad302a2000-03-24 00:54:46 +0000425 * at the end. Returns -1 on an illegal format. */
Erik Andersen298854f2000-03-23 01:09:18 +0000426static long getOctal(const char *cp, int size)
Eric Andersencc8ed391999-10-05 16:24:54 +0000427{
Erik Andersen298854f2000-03-23 01:09:18 +0000428 long val = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000429
Erik Andersen298854f2000-03-23 01:09:18 +0000430 for(;(size > 0) && (*cp == ' '); cp++, size--);
431 if ((size == 0) || !isOctal(*cp))
432 return -1;
433 for(; (size > 0) && isOctal(*cp); size--) {
434 val = val * 8 + *cp++ - '0';
Eric Andersencc8ed391999-10-05 16:24:54 +0000435 }
Erik Andersen298854f2000-03-23 01:09:18 +0000436 for (;(size > 0) && (*cp == ' '); cp++, size--);
437 if ((size > 0) && *cp)
438 return -1;
439 return val;
440}
Eric Andersencc8ed391999-10-05 16:24:54 +0000441
Erik Andersen1ad302a2000-03-24 00:54:46 +0000442
Erik Andersen298854f2000-03-23 01:09:18 +0000443/* Parse the tar header and fill in the nice struct with the details */
444static int
Erik Andersen3364d782000-03-28 00:58:14 +0000445readTarHeader(struct TarHeader *rawHeader, struct TarInfo *header)
Erik Andersen298854f2000-03-23 01:09:18 +0000446{
Erik Andersene454fb62000-03-23 04:27:58 +0000447 int i;
Erik Andersen84e09e42000-04-08 20:58:35 +0000448 long chksum, sum=0;
Erik Andersene454fb62000-03-23 04:27:58 +0000449 unsigned char *s = (unsigned char *)rawHeader;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000450
Erik Andersen298854f2000-03-23 01:09:18 +0000451 header->name = rawHeader->name;
Erik Andersen84e09e42000-04-08 20:58:35 +0000452 /* Check for and relativify any absolute paths */
453 if ( *(header->name) == '/' ) {
454 static int alreadyWarned=FALSE;
455
456 while (*(header->name) == '/')
457 ++*(header->name);
458
459 if (alreadyWarned == FALSE) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000460 errorMsg("Removing leading '/' from member names\n");
Erik Andersen84e09e42000-04-08 20:58:35 +0000461 alreadyWarned = TRUE;
462 }
463 }
464
Erik Andersen298854f2000-03-23 01:09:18 +0000465 header->mode = getOctal(rawHeader->mode, sizeof(rawHeader->mode));
466 header->uid = getOctal(rawHeader->uid, sizeof(rawHeader->uid));
467 header->gid = getOctal(rawHeader->gid, sizeof(rawHeader->gid));
468 header->size = getOctal(rawHeader->size, sizeof(rawHeader->size));
469 header->mtime = getOctal(rawHeader->mtime, sizeof(rawHeader->mtime));
470 chksum = getOctal(rawHeader->chksum, sizeof(rawHeader->chksum));
471 header->type = rawHeader->typeflag;
472 header->linkname = rawHeader->linkname;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000473 header->devmajor = getOctal(rawHeader->devmajor, sizeof(rawHeader->devmajor));
474 header->devminor = getOctal(rawHeader->devminor, sizeof(rawHeader->devminor));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000475
Erik Andersen298854f2000-03-23 01:09:18 +0000476 /* Check the checksum */
Erik Andersen84e09e42000-04-08 20:58:35 +0000477 for (i = sizeof(*rawHeader); i-- != 0;) {
Erik Andersen298854f2000-03-23 01:09:18 +0000478 sum += *s++;
Erik Andersen84e09e42000-04-08 20:58:35 +0000479 }
480 /* Remove the effects of the checksum field (replace
481 * with blanks for the purposes of the checksum) */
482 s = rawHeader->chksum;
483 for (i = sizeof(rawHeader->chksum) ; i-- != 0;) {
484 sum -= *s++;
485 }
486 sum += ' ' * sizeof(rawHeader->chksum);
Erik Andersene454fb62000-03-23 04:27:58 +0000487 if (sum == chksum )
Erik Andersen298854f2000-03-23 01:09:18 +0000488 return ( TRUE);
489 return( FALSE);
490}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000491
Erik Andersene49d5ec2000-02-08 19:58:47 +0000492
Erik Andersen1ad302a2000-03-24 00:54:46 +0000493/*
494 * Read a tar file and extract or list the specified files within it.
495 * If the list is empty than all files are extracted or listed.
496 */
497static int readTarFile(const char* tarName, int extractFlag, int listFlag,
Matt Kraaib92223b2000-09-04 08:25:42 +0000498 int tostdoutFlag, int verboseFlag, char** extractList,
499 char** excludeList)
Erik Andersen1ad302a2000-03-24 00:54:46 +0000500{
Erik Andersen0817d132000-04-09 15:17:40 +0000501 int status, tarFd=-1;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000502 int errorFlag=FALSE;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000503 int skipNextHeaderFlag=FALSE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000504 TarHeader rawHeader;
505 TarInfo header;
Erik Andersen0817d132000-04-09 15:17:40 +0000506 char** tmpList;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000507
Erik Andersen1ad302a2000-03-24 00:54:46 +0000508 /* Open the tar file for reading. */
509 if (!strcmp(tarName, "-"))
510 tarFd = fileno(stdin);
511 else
512 tarFd = open(tarName, O_RDONLY);
513 if (tarFd < 0) {
Erik Andersendeea0482000-03-25 23:09:29 +0000514 errorMsg( "Error opening '%s': %s\n", tarName, strerror(errno));
Erik Andersen1ad302a2000-03-24 00:54:46 +0000515 return ( FALSE);
Erik Andersen06936df2000-01-23 02:14:20 +0000516 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000517
Erik Andersene49d5ec2000-02-08 19:58:47 +0000518 /* Set the umask for this process so it doesn't
Erik Andersen1ad302a2000-03-24 00:54:46 +0000519 * screw up permission setting for us later. */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000520 umask(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000521
Erik Andersen1ad302a2000-03-24 00:54:46 +0000522 /* Read the tar file, and iterate over it one file at a time */
523 while ( (status = fullRead(tarFd, (char*)&rawHeader, TAR_BLOCK_SIZE)) == TAR_BLOCK_SIZE ) {
Erik Andersene2729152000-02-18 21:34:17 +0000524
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000525 /* Try to read the header */
Erik Andersen3364d782000-03-28 00:58:14 +0000526 if ( readTarHeader(&rawHeader, &header) == FALSE ) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000527 if ( *(header.name) == '\0' ) {
528 goto endgame;
529 } else {
530 errorFlag=TRUE;
531 errorMsg("Bad tar header, skipping\n");
532 continue;
533 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000534 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000535 if ( *(header.name) == '\0' )
536 goto endgame;
Erik Andersen0817d132000-04-09 15:17:40 +0000537 header.tarFd = tarFd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000538
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000539 /* Skip funky extra GNU headers that precede long files */
540 if ( (header.type == GNULONGNAME) || (header.type == GNULONGLINK) ) {
541 skipNextHeaderFlag=TRUE;
542 tarExtractRegularFile(&header, FALSE, FALSE);
543 continue;
544 }
545 if ( skipNextHeaderFlag == TRUE ) {
546 skipNextHeaderFlag=FALSE;
547 errorMsg(name_longer_then_foo, NAME_SIZE);
548 tarExtractRegularFile(&header, FALSE, FALSE);
549 continue;
550 }
551
Erik Andersen0817d132000-04-09 15:17:40 +0000552#if defined BB_FEATURE_TAR_EXCLUDE
553 {
554 int skipFlag=FALSE;
555 /* Check for excluded files.... */
556 for (tmpList=excludeList; tmpList && *tmpList; tmpList++) {
557 /* Do some extra hoop jumping for when directory names
558 * end in '/' but the entry in tmpList doesn't */
559 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
560 header.name[strlen(header.name)-1]=='/'
561 && strncmp( *tmpList, header.name,
562 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
563 /* If it is a regular file, pretend to extract it with
564 * the extractFlag set to FALSE, so the junk in the tarball
565 * is properly skipped over */
566 if ( header.type==REGTYPE || header.type==REGTYPE0 ) {
567 tarExtractRegularFile(&header, FALSE, FALSE);
568 }
569 skipFlag=TRUE;
570 break;
571 }
572 }
573 /* There are not the droids you're looking for, move along */
574 if (skipFlag==TRUE)
575 continue;
576 }
577#endif
Matt Kraaib92223b2000-09-04 08:25:42 +0000578 if (*extractList != NULL) {
579 int skipFlag = TRUE;
580 for (tmpList = extractList; *tmpList != NULL; tmpList++) {
581 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
582 header.name[strlen(header.name)-1]=='/'
583 && strncmp( *tmpList, header.name,
584 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
585 /* If it is a regular file, pretend to extract it with
586 * the extractFlag set to FALSE, so the junk in the tarball
587 * is properly skipped over */
588 skipFlag = FALSE;
589 break;
590 }
591 }
592 /* There are not the droids you're looking for, move along */
593 if (skipFlag == TRUE) {
594 if ( header.type==REGTYPE || header.type==REGTYPE0 )
595 tarExtractRegularFile(&header, FALSE, FALSE);
596 continue;
597 }
598 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000599
Matt Kraai82cfbad2000-09-15 21:18:43 +0000600 if (listFlag == TRUE) {
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000601 /* Special treatment if the list (-t) flag is on */
602 if (verboseFlag == TRUE) {
603 int len, len1;
604 char buf[35];
605 struct tm *tm = localtime (&(header.mtime));
606
607 len=printf("%s ", modeString(header.mode));
608 memset(buf, 0, 8*sizeof(char));
609 my_getpwuid(buf, header.uid);
610 if (! *buf)
611 len+=printf("%d", header.uid);
612 else
613 len+=printf("%s", buf);
614 memset(buf, 0, 8*sizeof(char));
615 my_getgrgid(buf, header.gid);
616 if (! *buf)
617 len+=printf("/%-d ", header.gid);
618 else
619 len+=printf("/%-s ", buf);
620
621 if (header.type==CHRTYPE || header.type==BLKTYPE) {
622 len1=snprintf(buf, sizeof(buf), "%ld,%-ld ",
623 header.devmajor, header.devminor);
624 } else {
625 len1=snprintf(buf, sizeof(buf), "%lu ", (long)header.size);
626 }
627 /* Jump through some hoops to make the columns match up */
628 for(;(len+len1)<31;len++)
629 printf(" ");
630 printf(buf);
631
632 /* Use ISO 8610 time format */
633 if (tm) {
634 printf ("%04d-%02d-%02d %02d:%02d:%02d ",
635 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
636 tm->tm_hour, tm->tm_min, tm->tm_sec);
637 }
638 }
Eric Andersenfdd51032000-08-02 18:48:26 +0000639 printf("%s", header.name);
Matt Kraai82cfbad2000-09-15 21:18:43 +0000640 if (verboseFlag == TRUE) {
641 if (header.type==LNKTYPE) /* If this is a link, say so */
642 printf(" link to %s", header.linkname);
643 else if (header.type==SYMTYPE)
644 printf(" -> %s", header.linkname);
645 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000646 printf("\n");
647 }
648
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000649 /* List contents if we are supposed to do that */
650 if (verboseFlag == TRUE && extractFlag == TRUE) {
651 /* Now the normal listing */
652 FILE *vbFd = stdout;
653 if (tostdoutFlag == TRUE) // If the archive goes to stdout, verbose to stderr
654 vbFd = stderr;
655 fprintf(vbFd, "%s\n", header.name);
656 }
657
Matt Kraaif4462972000-09-01 02:50:48 +0000658 /* Remove files if we would overwrite them */
Matt Kraaida542f32000-09-01 02:53:01 +0000659 if (extractFlag == TRUE && tostdoutFlag == FALSE)
Matt Kraaif4462972000-09-01 02:50:48 +0000660 unlink(header.name);
Eric Andersencc8ed391999-10-05 16:24:54 +0000661
Erik Andersen1ad302a2000-03-24 00:54:46 +0000662 /* If we got here, we can be certain we have a legitimate
663 * header to work with. So work with it. */
664 switch ( header.type ) {
665 case REGTYPE:
666 case REGTYPE0:
667 /* If the name ends in a '/' then assume it is
668 * supposed to be a directory, and fall through */
669 if (header.name[strlen(header.name)-1] != '/') {
Erik Andersen6a34b532000-04-07 06:55:38 +0000670 if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE)
671 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000672 break;
673 }
674 case DIRTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000675 if (tarExtractDirectory( &header, extractFlag, tostdoutFlag)==FALSE)
676 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000677 break;
678 case LNKTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000679 if (tarExtractHardLink( &header, extractFlag, tostdoutFlag)==FALSE)
680 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000681 break;
682 case SYMTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000683 if (tarExtractSymLink( &header, extractFlag, tostdoutFlag)==FALSE)
684 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000685 break;
686 case CHRTYPE:
687 case BLKTYPE:
688 case FIFOTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000689 if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE)
690 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000691 break;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000692#if 0
693 /* Handled earlier */
694 case GNULONGNAME:
695 case GNULONGLINK:
696 skipNextHeaderFlag=TRUE;
697 break;
698#endif
Erik Andersen1ad302a2000-03-24 00:54:46 +0000699 default:
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000700 errorMsg("Unknown file type '%c' in tar file\n", header.type);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000701 close( tarFd);
702 return( FALSE);
703 }
704 }
705 close(tarFd);
706 if (status > 0) {
707 /* Bummer - we read a partial header */
Erik Andersendeea0482000-03-25 23:09:29 +0000708 errorMsg( "Error reading '%s': %s\n", tarName, strerror(errno));
Erik Andersen1ad302a2000-03-24 00:54:46 +0000709 return ( FALSE);
710 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000711 else if (errorFlag==TRUE) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000712 errorMsg( "Error exit delayed from previous errors\n");
Erik Andersen6a34b532000-04-07 06:55:38 +0000713 return( FALSE);
714 } else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000715 return( status);
716
717 /* Stuff to do when we are done */
718endgame:
719 close( tarFd);
720 if ( *(header.name) == '\0' ) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000721 if (errorFlag==TRUE)
Matt Kraaibe84cd42000-07-12 17:02:35 +0000722 errorMsg( "Error exit delayed from previous errors\n");
Erik Andersen6a34b532000-04-07 06:55:38 +0000723 else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000724 return( TRUE);
725 }
726 return( FALSE);
727}
728
Erik Andersen6acaa402000-03-26 14:03:20 +0000729
730#ifdef BB_FEATURE_TAR_CREATE
731
Erik Andersen68a9ea42000-04-04 18:39:50 +0000732/* Some info to be carried along when creating a new tarball */
733struct TarBallInfo
734{
735 char* fileName; /* File name of the tarball */
736 int tarFd; /* Open-for-write file descriptor
737 for the tarball */
738 struct stat statBuf; /* Stat info for the tarball, letting
739 us know the inode and device that the
740 tarball lives, so we can avoid trying
741 to include the tarball into itself */
742 int verboseFlag; /* Whether to print extra stuff or not */
Erik Andersenecd51242000-04-08 03:08:21 +0000743 char** excludeList; /* List of files to not include */
Erik Andersen68a9ea42000-04-04 18:39:50 +0000744};
745typedef struct TarBallInfo TarBallInfo;
746
747
Erik Andersen6acaa402000-03-26 14:03:20 +0000748/* Put an octal string into the specified buffer.
749 * The number is zero and space padded and possibly null padded.
750 * Returns TRUE if successful. */
751static int putOctal (char *cp, int len, long value)
752{
753 int tempLength;
Erik Andersen6acaa402000-03-26 14:03:20 +0000754 char tempBuffer[32];
Erik Andersen5661fe02000-04-05 01:00:52 +0000755 char *tempString = tempBuffer;
Erik Andersen6acaa402000-03-26 14:03:20 +0000756
757 /* Create a string of the specified length with an initial space,
758 * leading zeroes and the octal number, and a trailing null. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000759 sprintf (tempString, "%0*lo", len - 1, value);
Erik Andersen6acaa402000-03-26 14:03:20 +0000760
761 /* If the string is too large, suppress the leading space. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000762 tempLength = strlen (tempString) + 1;
Erik Andersen6acaa402000-03-26 14:03:20 +0000763 if (tempLength > len) {
764 tempLength--;
765 tempString++;
766 }
767
768 /* If the string is still too large, suppress the trailing null. */
769 if (tempLength > len)
770 tempLength--;
771
772 /* If the string is still too large, fail. */
773 if (tempLength > len)
774 return FALSE;
775
776 /* Copy the string to the field. */
777 memcpy (cp, tempString, len);
778
779 return TRUE;
780}
781
Erik Andersen68a9ea42000-04-04 18:39:50 +0000782/* Write out a tar header for the specified file/directory/whatever */
Erik Andersen3364d782000-03-28 00:58:14 +0000783static int
Erik Andersen5661fe02000-04-05 01:00:52 +0000784writeTarHeader(struct TarBallInfo *tbInfo, const char *fileName, struct stat *statbuf)
Erik Andersen6acaa402000-03-26 14:03:20 +0000785{
Erik Andersen5661fe02000-04-05 01:00:52 +0000786 long chksum=0;
787 struct TarHeader header;
Erik Andersen0817d132000-04-09 15:17:40 +0000788#if defined BB_FEATURE_TAR_EXCLUDE
Erik Andersenecd51242000-04-08 03:08:21 +0000789 char** tmpList;
Erik Andersen0817d132000-04-09 15:17:40 +0000790#endif
Erik Andersen5661fe02000-04-05 01:00:52 +0000791 const unsigned char *cp = (const unsigned char *) &header;
792 ssize_t size = sizeof(struct TarHeader);
Eric Andersenfdd51032000-08-02 18:48:26 +0000793
Erik Andersen5661fe02000-04-05 01:00:52 +0000794 memset( &header, 0, size);
Erik Andersen3364d782000-03-28 00:58:14 +0000795
Erik Andersen68a9ea42000-04-04 18:39:50 +0000796 if (*fileName=='/') {
797 static int alreadyWarned=FALSE;
798 if (alreadyWarned==FALSE) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000799 errorMsg("Removing leading '/' from member names\n");
Erik Andersen68a9ea42000-04-04 18:39:50 +0000800 alreadyWarned=TRUE;
801 }
Erik Andersen84e09e42000-04-08 20:58:35 +0000802 strncpy(header.name, fileName+1, sizeof(header.name));
Erik Andersen3364d782000-03-28 00:58:14 +0000803 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000804 else {
Erik Andersen84e09e42000-04-08 20:58:35 +0000805 strncpy(header.name, fileName, sizeof(header.name));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000806 }
Erik Andersen84e09e42000-04-08 20:58:35 +0000807
Erik Andersen0817d132000-04-09 15:17:40 +0000808#if defined BB_FEATURE_TAR_EXCLUDE
809 /* Check for excluded files.... */
Erik Andersenecd51242000-04-08 03:08:21 +0000810 for (tmpList=tbInfo->excludeList; tmpList && *tmpList; tmpList++) {
Erik Andersen0817d132000-04-09 15:17:40 +0000811 /* Do some extra hoop jumping for when directory names
812 * end in '/' but the entry in tmpList doesn't */
813 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
814 header.name[strlen(header.name)-1]=='/'
815 && strncmp( *tmpList, header.name,
816 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
817 /* Set the mode to something that is not a regular file, thereby
818 * faking out writeTarFile into thinking that nothing further need
819 * be done for this file. Yes, I know this is ugly, but it works. */
820 statbuf->st_mode = 0;
821 return( TRUE);
822 }
Erik Andersenecd51242000-04-08 03:08:21 +0000823 }
Erik Andersen0817d132000-04-09 15:17:40 +0000824#endif
Erik Andersenecd51242000-04-08 03:08:21 +0000825
Erik Andersen5661fe02000-04-05 01:00:52 +0000826 putOctal(header.mode, sizeof(header.mode), statbuf->st_mode);
827 putOctal(header.uid, sizeof(header.uid), statbuf->st_uid);
828 putOctal(header.gid, sizeof(header.gid), statbuf->st_gid);
829 putOctal(header.size, sizeof(header.size), 0); /* Regular file size is handled later */
830 putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime);
831 strncpy(header.magic, TAR_MAGIC TAR_VERSION,
832 TAR_MAGIC_LEN + TAR_VERSION_LEN );
Erik Andersen68a9ea42000-04-04 18:39:50 +0000833
Erik Andersen84e09e42000-04-08 20:58:35 +0000834 /* Enter the user and group names (default to root if it fails) */
Erik Andersen5661fe02000-04-05 01:00:52 +0000835 my_getpwuid(header.uname, statbuf->st_uid);
Erik Andersen5661fe02000-04-05 01:00:52 +0000836 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000837 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +0000838 my_getgrgid(header.gname, statbuf->st_gid);
839 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000840 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +0000841
Erik Andersen95c1c1e2000-04-14 21:45:29 +0000842 /* WARNING/NOTICE: I break Hard Links */
Erik Andersen68a9ea42000-04-04 18:39:50 +0000843 if (S_ISLNK(statbuf->st_mode)) {
Eric Andersen3adffb72000-06-26 10:54:06 +0000844 int link_size=0;
Erik Andersen5661fe02000-04-05 01:00:52 +0000845 char buffer[BUFSIZ];
846 header.typeflag = SYMTYPE;
Eric Andersen3adffb72000-06-26 10:54:06 +0000847 link_size = readlink(fileName, buffer, sizeof(buffer) - 1);
848 if ( link_size < 0) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000849 errorMsg("Error reading symlink '%s': %s\n", header.name, strerror(errno));
850 return ( FALSE);
851 }
Eric Andersen3adffb72000-06-26 10:54:06 +0000852 buffer[link_size] = '\0';
Erik Andersen95c1c1e2000-04-14 21:45:29 +0000853 strncpy(header.linkname, buffer, sizeof(header.linkname));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000854 } else if (S_ISDIR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000855 header.typeflag = DIRTYPE;
856 strncat(header.name, "/", sizeof(header.name));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000857 } else if (S_ISCHR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000858 header.typeflag = CHRTYPE;
859 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
860 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000861 } else if (S_ISBLK(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000862 header.typeflag = BLKTYPE;
863 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
864 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000865 } else if (S_ISFIFO(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000866 header.typeflag = FIFOTYPE;
867 } else if (S_ISREG(statbuf->st_mode)) {
868 header.typeflag = REGTYPE;
869 putOctal(header.size, sizeof(header.size), statbuf->st_size);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000870 } else {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000871 errorMsg("%s: Unknown file type\n", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000872 return ( FALSE);
873 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000874
Erik Andersen5661fe02000-04-05 01:00:52 +0000875 /* Calculate and store the checksum (i.e. the sum of all of the bytes of
876 * the header). The checksum field must be filled with blanks for the
877 * calculation. The checksum field is formatted differently from the
878 * other fields: it has [6] digits, a null, then a space -- rather than
879 * digits, followed by a null like the other fields... */
880 memset(header.chksum, ' ', sizeof(header.chksum));
881 cp = (const unsigned char *) &header;
882 while (size-- > 0)
883 chksum += *cp++;
884 putOctal(header.chksum, 7, chksum);
885
886 /* Now write the header out to disk */
887 if ((size=fullWrite(tbInfo->tarFd, (char*)&header, sizeof(struct TarHeader))) < 0) {
888 errorMsg(io_error, fileName, strerror(errno));
889 return ( FALSE);
890 }
891 /* Pad the header up to the tar block size */
892 for (; size<TAR_BLOCK_SIZE; size++) {
893 write(tbInfo->tarFd, "\0", 1);
894 }
895 /* Now do the verbose thing (or not) */
Eric Andersenfdd51032000-08-02 18:48:26 +0000896 if (tbInfo->verboseFlag==TRUE) {
897 FILE *vbFd = stdout;
898 if (tbInfo->tarFd == fileno(stdout)) // If the archive goes to stdout, verbose to stderr
899 vbFd = stderr;
900 fprintf(vbFd, "%s\n", header.name);
901 }
Erik Andersen3364d782000-03-28 00:58:14 +0000902
903 return ( TRUE);
904}
905
906
Erik Andersen68a9ea42000-04-04 18:39:50 +0000907static int writeFileToTarball(const char *fileName, struct stat *statbuf, void* userData)
Erik Andersen3364d782000-03-28 00:58:14 +0000908{
Erik Andersen68a9ea42000-04-04 18:39:50 +0000909 struct TarBallInfo *tbInfo = (struct TarBallInfo *)userData;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000910
911 /* It is against the rules to archive a socket */
912 if (S_ISSOCK(statbuf->st_mode)) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000913 errorMsg("%s: socket ignored\n", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000914 return( TRUE);
915 }
916
917 /* It is a bad idea to store the archive we are in the process of creating,
918 * so check the device and inode to be sure that this particular file isn't
919 * the new tarball */
920 if (tbInfo->statBuf.st_dev == statbuf->st_dev &&
921 tbInfo->statBuf.st_ino == statbuf->st_ino) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000922 errorMsg("%s: file is the archive; skipping\n", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000923 return( TRUE);
924 }
925
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000926 if (strlen(fileName) >= NAME_SIZE) {
927 errorMsg(name_longer_then_foo, NAME_SIZE);
928 return ( TRUE);
929 }
930
Erik Andersen5661fe02000-04-05 01:00:52 +0000931 if (writeTarHeader(tbInfo, fileName, statbuf)==FALSE) {
932 return( FALSE);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000933 }
Erik Andersen5661fe02000-04-05 01:00:52 +0000934
935 /* Now, if the file is a regular file, copy it out to the tarball */
936 if (S_ISREG(statbuf->st_mode)) {
937 int inputFileFd;
938 char buffer[BUFSIZ];
939 ssize_t size=0, readSize=0;
940
941 /* open the file we want to archive, and make sure all is well */
942 if ((inputFileFd = open(fileName, O_RDONLY)) < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000943 errorMsg("%s: Cannot open: %s\n", fileName, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +0000944 return( FALSE);
945 }
946
947 /* write the file to the archive */
948 while ( (size = fullRead(inputFileFd, buffer, sizeof(buffer))) > 0 ) {
949 if (fullWrite(tbInfo->tarFd, buffer, size) != size ) {
950 /* Output file seems to have a problem */
951 errorMsg(io_error, fileName, strerror(errno));
952 return( FALSE);
953 }
954 readSize+=size;
955 }
956 if (size == -1) {
957 errorMsg(io_error, fileName, strerror(errno));
958 return( FALSE);
959 }
960 /* Pad the file up to the tar block size */
961 for (; (readSize%TAR_BLOCK_SIZE) != 0; readSize++) {
962 write(tbInfo->tarFd, "\0", 1);
963 }
964 close( inputFileFd);
965 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000966
967 return( TRUE);
Erik Andersen6acaa402000-03-26 14:03:20 +0000968}
969
Eric Andersen46a98df2000-09-19 21:35:14 +0000970static int writeTarFile(const char* tarName, int tostdoutFlag,
971 int verboseFlag, int argc, char **argv, char** excludeList)
Erik Andersen6acaa402000-03-26 14:03:20 +0000972{
Erik Andersen3364d782000-03-28 00:58:14 +0000973 int tarFd=-1;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000974 int errorFlag=FALSE;
Erik Andersen5661fe02000-04-05 01:00:52 +0000975 ssize_t size;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000976 struct TarBallInfo tbInfo;
977 tbInfo.verboseFlag = verboseFlag;
Erik Andersen3364d782000-03-28 00:58:14 +0000978
979 /* Make sure there is at least one file to tar up. */
980 if (argc <= 0)
Matt Kraaibe84cd42000-07-12 17:02:35 +0000981 fatalError("Cowardly refusing to create an empty archive\n");
Erik Andersen6acaa402000-03-26 14:03:20 +0000982
983 /* Open the tar file for writing. */
Eric Andersen46a98df2000-09-19 21:35:14 +0000984 if (tostdoutFlag == TRUE)
Erik Andersen68a9ea42000-04-04 18:39:50 +0000985 tbInfo.tarFd = fileno(stdout);
Erik Andersen6acaa402000-03-26 14:03:20 +0000986 else
Erik Andersen68a9ea42000-04-04 18:39:50 +0000987 tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
988 if (tbInfo.tarFd < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000989 errorMsg( "Error opening '%s': %s\n", tarName, strerror(errno));
Erik Andersen6acaa402000-03-26 14:03:20 +0000990 return ( FALSE);
991 }
Erik Andersenecd51242000-04-08 03:08:21 +0000992 tbInfo.excludeList=excludeList;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000993 /* Store the stat info for the tarball's file, so
994 * can avoid including the tarball into itself.... */
995 if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0)
Erik Andersen3364d782000-03-28 00:58:14 +0000996 fatalError(io_error, tarName, strerror(errno));
Erik Andersen6acaa402000-03-26 14:03:20 +0000997
998 /* Set the umask for this process so it doesn't
999 * screw up permission setting for us later. */
1000 umask(0);
1001
1002 /* Read the directory/files and iterate over them one at a time */
Erik Andersen3364d782000-03-28 00:58:14 +00001003 while (argc-- > 0) {
1004 if (recursiveAction(*argv++, TRUE, FALSE, FALSE,
Erik Andersen68a9ea42000-04-04 18:39:50 +00001005 writeFileToTarball, writeFileToTarball,
1006 (void*) &tbInfo) == FALSE) {
1007 errorFlag = TRUE;
Erik Andersen3364d782000-03-28 00:58:14 +00001008 }
Erik Andersen6acaa402000-03-26 14:03:20 +00001009 }
Erik Andersen5661fe02000-04-05 01:00:52 +00001010 /* Write two empty blocks to the end of the archive */
1011 for (size=0; size<(2*TAR_BLOCK_SIZE); size++) {
1012 write(tbInfo.tarFd, "\0", 1);
1013 }
Erik Andersen0817d132000-04-09 15:17:40 +00001014
1015 /* To be pedantically correct, we would check if the tarball
1016 * is smaller then 20 tar blocks, and pad it if it was smaller,
1017 * but that isn't necessary for GNU tar interoperability, and
1018 * so is considered a waste of space */
1019
Erik Andersen68a9ea42000-04-04 18:39:50 +00001020 /* Hang up the tools, close up shop, head home */
Erik Andersen6acaa402000-03-26 14:03:20 +00001021 close(tarFd);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001022 if (errorFlag == TRUE) {
Matt Kraaibe84cd42000-07-12 17:02:35 +00001023 errorMsg("Error exit delayed from previous errors\n");
Erik Andersen68a9ea42000-04-04 18:39:50 +00001024 return(FALSE);
1025 }
Erik Andersen6acaa402000-03-26 14:03:20 +00001026 return( TRUE);
1027}
1028
1029
1030#endif
1031