blob: ade77a59dd75a242c8b7aa726d07f3880f1b8f9c [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
42#include "messages.c"
Eric Andersencc8ed391999-10-05 16:24:54 +000043#include <stdio.h>
44#include <dirent.h>
45#include <errno.h>
46#include <fcntl.h>
47#include <signal.h>
48#include <time.h>
Erik Andersen7dc16072000-01-04 01:10:25 +000049#include <utime.h>
Eric Andersen96bcfd31999-11-12 01:30:18 +000050#include <sys/types.h>
Eric Andersen08b10341999-11-19 02:38:58 +000051#include <sys/sysmacros.h>
Matt Kraai43c8c382000-09-04 16:51:55 +000052#include <getopt.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000053
Erik Andersen298854f2000-03-23 01:09:18 +000054/* Tar file constants */
Erik Andersen1ad302a2000-03-24 00:54:46 +000055#ifndef MAJOR
56#define MAJOR(dev) (((dev)>>8)&0xff)
57#define MINOR(dev) ((dev)&0xff)
58#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000059
60
Erik Andersen298854f2000-03-23 01:09:18 +000061/* POSIX tar Header Block, from POSIX 1003.1-1990 */
62struct TarHeader
63{
64 /* byte offset */
Erik Andersen0817d132000-04-09 15:17:40 +000065 char name[100]; /* 0-99 */
66 char mode[8]; /* 100-107 */
67 char uid[8]; /* 108-115 */
68 char gid[8]; /* 116-123 */
69 char size[12]; /* 124-135 */
70 char mtime[12]; /* 136-147 */
71 char chksum[8]; /* 148-155 */
72 char typeflag; /* 156-156 */
73 char linkname[100]; /* 157-256 */
74 char magic[6]; /* 257-262 */
75 char version[2]; /* 263-264 */
76 char uname[32]; /* 265-296 */
77 char gname[32]; /* 297-328 */
78 char devmajor[8]; /* 329-336 */
79 char devminor[8]; /* 337-344 */
80 char prefix[155]; /* 345-499 */
81 char padding[12]; /* 500-512 (pad to exactly the TAR_BLOCK_SIZE) */
Erik Andersen298854f2000-03-23 01:09:18 +000082};
83typedef struct TarHeader TarHeader;
Eric Andersencc8ed391999-10-05 16:24:54 +000084
Eric Andersencc8ed391999-10-05 16:24:54 +000085
Erik Andersen298854f2000-03-23 01:09:18 +000086/* A few useful constants */
87#define TAR_MAGIC "ustar" /* ustar and a null */
Erik Andersen0817d132000-04-09 15:17:40 +000088#define TAR_VERSION " " /* Be compatable with GNU tar format */
Erik Andersen298854f2000-03-23 01:09:18 +000089#define TAR_MAGIC_LEN 6
90#define TAR_VERSION_LEN 2
Erik Andersen298854f2000-03-23 01:09:18 +000091#define TAR_BLOCK_SIZE 512
92
93/* A nice enum with all the possible tar file content types */
94enum TarFileType
95{
96 REGTYPE = '0', /* regular file */
97 REGTYPE0 = '\0', /* regular file (ancient bug compat)*/
98 LNKTYPE = '1', /* hard link */
99 SYMTYPE = '2', /* symbolic link */
100 CHRTYPE = '3', /* character special */
101 BLKTYPE = '4', /* block special */
102 DIRTYPE = '5', /* directory */
103 FIFOTYPE = '6', /* FIFO special */
104 CONTTYPE = '7', /* reserved */
105};
106typedef enum TarFileType TarFileType;
107
108/* This struct ignores magic, non-numeric user name,
109 * non-numeric group name, and the checksum, since
110 * these are all ignored by BusyBox tar. */
111struct TarInfo
112{
113 int tarFd; /* An open file descriptor for reading from the tarball */
114 char * name; /* File name */
115 mode_t mode; /* Unix mode, including device bits. */
116 uid_t uid; /* Numeric UID */
117 gid_t gid; /* Numeric GID */
118 size_t size; /* Size of file */
119 time_t mtime; /* Last-modified time */
120 enum TarFileType type; /* Regular, directory, link, etc */
121 char * linkname; /* Name for symbolic and hard links */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000122 long devmajor; /* Major number for special device */
123 long devminor; /* Minor number for special device */
Erik Andersen298854f2000-03-23 01:09:18 +0000124};
125typedef struct TarInfo TarInfo;
126
Erik Andersene454fb62000-03-23 04:27:58 +0000127/* Local procedures to restore files from a tar file. */
Erik Andersen298854f2000-03-23 01:09:18 +0000128static int readTarFile(const char* tarName, int extractFlag, int listFlag,
Matt Kraaib92223b2000-09-04 08:25:42 +0000129 int tostdoutFlag, int verboseFlag, char** extractList,
130 char** excludeList);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000131
132
Eric Andersencc8ed391999-10-05 16:24:54 +0000133
Erik Andersen05100cd2000-01-16 01:30:52 +0000134#ifdef BB_FEATURE_TAR_CREATE
Erik Andersen6acaa402000-03-26 14:03:20 +0000135/* Local procedures to save files into a tar file. */
Eric Andersen46a98df2000-09-19 21:35:14 +0000136static int writeTarFile(const char* tarName, int tostdoutFlag,
137 int verboseFlag, int argc, char **argv, char** excludeList);
Erik Andersen05100cd2000-01-16 01:30:52 +0000138#endif
139
Erik Andersene49d5ec2000-02-08 19:58:47 +0000140extern int tar_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000141{
Erik Andersenecd51242000-04-08 03:08:21 +0000142 char** excludeList=NULL;
Erik Andersen0817d132000-04-09 15:17:40 +0000143#if defined BB_FEATURE_TAR_EXCLUDE
Erik Andersenecd51242000-04-08 03:08:21 +0000144 int excludeListSize=0;
Erik Andersen0817d132000-04-09 15:17:40 +0000145#endif
Eric Andersen108a4432000-05-30 19:05:57 +0000146 const char *tarName="-";
Erik Andersen298854f2000-03-23 01:09:18 +0000147 int listFlag = FALSE;
148 int extractFlag = FALSE;
149 int createFlag = FALSE;
150 int verboseFlag = FALSE;
151 int tostdoutFlag = FALSE;
Eric Andersen46a98df2000-09-19 21:35:14 +0000152 int stopIt;
153
Eric Andersencc8ed391999-10-05 16:24:54 +0000154
Erik Andersenecd51242000-04-08 03:08:21 +0000155 if (argc <= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000156 usage(tar_usage);
Eric Andersencc8ed391999-10-05 16:24:54 +0000157
Eric Andersenfdd51032000-08-02 18:48:26 +0000158 /* do normal option parsing */
Eric Andersen46a98df2000-09-19 21:35:14 +0000159 while (--argc > 0 && strspn(*(++argv), "-cxt") >0 ) {
160 stopIt=FALSE;
161 while (stopIt==FALSE && *argv && **argv) {
162 switch (**argv) {
Erik Andersenecd51242000-04-08 03:08:21 +0000163 case 'c':
164 if (extractFlag == TRUE || listFlag == TRUE)
165 goto flagError;
166 createFlag = TRUE;
167 break;
Eric Andersenfdd51032000-08-02 18:48:26 +0000168 case 'x':
169 if (listFlag == TRUE || createFlag == TRUE)
170 goto flagError;
171 extractFlag = TRUE;
172 break;
173 case 't':
174 if (extractFlag == TRUE || createFlag == TRUE)
175 goto flagError;
176 listFlag = TRUE;
177 break;
Erik Andersenecd51242000-04-08 03:08:21 +0000178 case 'v':
179 verboseFlag = TRUE;
180 break;
Erik Andersenecd51242000-04-08 03:08:21 +0000181 case 'O':
182 tostdoutFlag = TRUE;
Eric Andersen46a98df2000-09-19 21:35:14 +0000183 tarName = "-";
Eric Andersenfdd51032000-08-02 18:48:26 +0000184 break;
185 case 'f':
Eric Andersen46a98df2000-09-19 21:35:14 +0000186 if (--argc == 0) {
187 fatalError( "Option requires an argument: No file specified\n");
188 }
Eric Andersenfdd51032000-08-02 18:48:26 +0000189 if (*tarName != '-')
190 fatalError( "Only one 'f' option allowed\n");
Eric Andersen46a98df2000-09-19 21:35:14 +0000191 tarName = *(++argv);
192 if (tarName == NULL)
193 fatalError( "Option requires an argument: No file specified\n");
194 if (!strcmp(tarName, "-") && createFlag == TRUE)
195 tostdoutFlag = TRUE;
196 stopIt=TRUE;
Erik Andersenecd51242000-04-08 03:08:21 +0000197 break;
Erik Andersen0817d132000-04-09 15:17:40 +0000198#if defined BB_FEATURE_TAR_EXCLUDE
Matt Kraai43c8c382000-09-04 16:51:55 +0000199 case 'e':
Eric Andersen46a98df2000-09-19 21:35:14 +0000200 if (strcmp(*argv, "-exclude")==0) {
201 if (--argc == 0) {
202 fatalError( "Option requires an argument: No file specified\n");
203 }
204 excludeList=xrealloc( excludeList, sizeof(char**) * (excludeListSize+2));
205 excludeList[excludeListSize] = *(++argv);
206 /* Remove leading "/"s */
207 if (*excludeList[excludeListSize] =='/')
208 excludeList[excludeListSize] = (excludeList[excludeListSize])+1;
209 /* Tack a NULL onto the end of the list */
210 excludeList[++excludeListSize] = NULL;
211 stopIt=TRUE;
212 break;
213 }
Erik Andersen0817d132000-04-09 15:17:40 +0000214#endif
Eric Andersen46a98df2000-09-19 21:35:14 +0000215 case '-':
216 break;
Erik Andersenecd51242000-04-08 03:08:21 +0000217 default:
Matt Kraai43c8c382000-09-04 16:51:55 +0000218 usage(tar_usage);
Eric Andersen46a98df2000-09-19 21:35:14 +0000219 }
220 ++(*argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000221 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000222 }
223
Erik Andersene49d5ec2000-02-08 19:58:47 +0000224 /*
225 * Do the correct type of action supplying the rest of the
226 * command line arguments as the list of files to process.
227 */
228 if (createFlag == TRUE) {
Erik Andersende552872000-01-23 01:34:05 +0000229#ifndef BB_FEATURE_TAR_CREATE
Erik Andersen298854f2000-03-23 01:09:18 +0000230 fatalError( "This version of tar was not compiled with tar creation support.\n");
Erik Andersende552872000-01-23 01:34:05 +0000231#else
Eric Andersen46a98df2000-09-19 21:35:14 +0000232 exit(writeTarFile(tarName, tostdoutFlag, verboseFlag, argc, argv, excludeList));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000233#endif
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000234 }
235 if (listFlag == TRUE || extractFlag == TRUE) {
Matt Kraaib92223b2000-09-04 08:25:42 +0000236 exit(readTarFile(tarName, extractFlag, listFlag, tostdoutFlag, verboseFlag, &argv[optind], excludeList));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000237 }
Erik Andersende552872000-01-23 01:34:05 +0000238
Erik Andersene49d5ec2000-02-08 19:58:47 +0000239 flagError:
Erik Andersen298854f2000-03-23 01:09:18 +0000240 fatalError( "Exactly one of 'c', 'x' or 't' must be specified\n");
241}
242
243static void
Erik Andersen6a34b532000-04-07 06:55:38 +0000244fixUpPermissions(TarInfo *header)
245{
246 struct utimbuf t;
247 /* Now set permissions etc for the new file */
248 chown(header->name, header->uid, header->gid);
249 chmod(header->name, header->mode);
250 /* Reset the time */
251 t.actime = time(0);
252 t.modtime = header->mtime;
253 utime(header->name, &t);
254}
255
256static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000257tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersen298854f2000-03-23 01:09:18 +0000258{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000259 size_t writeSize;
260 size_t readSize;
261 size_t actualWriteSz;
262 char buffer[BUFSIZ];
263 size_t size = header->size;
264 int outFd=fileno(stdout);
265
266 /* Open the file to be written, if a file is supposed to be written */
267 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000268 /* Create the path to the file, just in case it isn't there...
269 * This should not screw up path permissions or anything. */
270 createPath(header->name, 0777);
Eric Andersen0c6a9702000-06-09 20:51:50 +0000271 if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY,
272 header->mode & ~S_IFMT)) < 0) {
273 errorMsg(io_error, header->name, strerror(errno));
274 return( FALSE);
275 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000276 }
277
278 /* Write out the file, if we are supposed to be doing that */
279 while ( size > 0 ) {
280 actualWriteSz=0;
281 if ( size > sizeof(buffer) )
282 writeSize = readSize = sizeof(buffer);
283 else {
284 int mod = size % 512;
285 if ( mod != 0 )
286 readSize = size + (512 - mod);
287 else
288 readSize = size;
289 writeSize = size;
290 }
291 if ( (readSize = fullRead(header->tarFd, buffer, readSize)) <= 0 ) {
292 /* Tarball seems to have a problem */
Matt Kraaibe84cd42000-07-12 17:02:35 +0000293 errorMsg("Unexpected EOF in archive\n");
Erik Andersen6a34b532000-04-07 06:55:38 +0000294 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000295 }
296 if ( readSize < writeSize )
297 writeSize = readSize;
298
299 /* Write out the file, if we are supposed to be doing that */
300 if (extractFlag==TRUE) {
301
302 if ((actualWriteSz=fullWrite(outFd, buffer, writeSize)) != writeSize ) {
303 /* Output file seems to have a problem */
304 errorMsg(io_error, header->name, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000305 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000306 }
Erik Andersen0817d132000-04-09 15:17:40 +0000307 } else {
308 actualWriteSz=writeSize;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000309 }
310
311 size -= actualWriteSz;
312 }
313
314 /* Now we are done writing the file out, so try
315 * and fix up the permissions and whatnot */
316 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000317 close(outFd);
Erik Andersen6a34b532000-04-07 06:55:38 +0000318 fixUpPermissions(header);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000319 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000320 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000321}
322
Erik Andersen6a34b532000-04-07 06:55:38 +0000323static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000324tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000325{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000326
327 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000328 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000329
330 if (createPath(header->name, header->mode) != TRUE) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000331 errorMsg("%s: Cannot mkdir: %s\n",
Erik Andersen6a34b532000-04-07 06:55:38 +0000332 header->name, strerror(errno));
333 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000334 }
335 /* make the final component, just in case it was
336 * omitted by createPath() (which will skip the
337 * directory if it doesn't have a terminating '/') */
Erik Andersen6acaa402000-03-26 14:03:20 +0000338 if (mkdir(header->name, header->mode) == 0) {
339 fixUpPermissions(header);
340 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000341 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000342}
343
Erik Andersen6a34b532000-04-07 06:55:38 +0000344static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000345tarExtractHardLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000346{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000347 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000348 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000349
350 if (link(header->linkname, header->name) < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000351 errorMsg("%s: Cannot create hard link to '%s': %s\n",
Erik Andersen5661fe02000-04-05 01:00:52 +0000352 header->name, header->linkname, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000353 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000354 }
355
356 /* Now set permissions etc for the new directory */
357 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000358 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000359}
360
Erik Andersen6a34b532000-04-07 06:55:38 +0000361static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000362tarExtractSymLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000363{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000364 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000365 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000366
367#ifdef S_ISLNK
368 if (symlink(header->linkname, header->name) < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000369 errorMsg("%s: Cannot create symlink to '%s': %s\n",
Erik Andersen5661fe02000-04-05 01:00:52 +0000370 header->name, header->linkname, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000371 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000372 }
373 /* Try to change ownership of the symlink.
374 * If libs doesn't support that, don't bother.
375 * Changing the pointed-to-file is the Wrong Thing(tm).
376 */
377#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
378 lchown(header->name, header->uid, header->gid);
379#endif
380
381 /* Do not change permissions or date on symlink,
382 * since it changes the pointed to file instead. duh. */
383#else
Matt Kraaibe84cd42000-07-12 17:02:35 +0000384 errorMsg("%s: Cannot create symlink to '%s': %s\n",
Erik Andersen6a34b532000-04-07 06:55:38 +0000385 header->name, header->linkname,
386 "symlinks not supported");
Erik Andersen1ad302a2000-03-24 00:54:46 +0000387#endif
Erik Andersen6a34b532000-04-07 06:55:38 +0000388 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000389}
390
Erik Andersen6a34b532000-04-07 06:55:38 +0000391static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000392tarExtractSpecial(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000393{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000394 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000395 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000396
397 if (S_ISCHR(header->mode) || S_ISBLK(header->mode) || S_ISSOCK(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000398 if (mknod(header->name, header->mode, makedev(header->devmajor, header->devminor)) < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000399 errorMsg("%s: Cannot mknod: %s\n",
Erik Andersen6a34b532000-04-07 06:55:38 +0000400 header->name, strerror(errno));
401 return( FALSE);
402 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000403 } else if (S_ISFIFO(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000404 if (mkfifo(header->name, header->mode) < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000405 errorMsg("%s: Cannot mkfifo: %s\n",
Erik Andersen6a34b532000-04-07 06:55:38 +0000406 header->name, strerror(errno));
407 return( FALSE);
408 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000409 }
410
411 /* Now set permissions etc for the new directory */
412 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000413 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000414}
415
Erik Andersen1ad302a2000-03-24 00:54:46 +0000416/* Read an octal value in a field of the specified width, with optional
Erik Andersen298854f2000-03-23 01:09:18 +0000417 * spaces on both sides of the number and with an optional null character
Erik Andersen1ad302a2000-03-24 00:54:46 +0000418 * at the end. Returns -1 on an illegal format. */
Erik Andersen298854f2000-03-23 01:09:18 +0000419static long getOctal(const char *cp, int size)
Eric Andersencc8ed391999-10-05 16:24:54 +0000420{
Erik Andersen298854f2000-03-23 01:09:18 +0000421 long val = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000422
Erik Andersen298854f2000-03-23 01:09:18 +0000423 for(;(size > 0) && (*cp == ' '); cp++, size--);
424 if ((size == 0) || !isOctal(*cp))
425 return -1;
426 for(; (size > 0) && isOctal(*cp); size--) {
427 val = val * 8 + *cp++ - '0';
Eric Andersencc8ed391999-10-05 16:24:54 +0000428 }
Erik Andersen298854f2000-03-23 01:09:18 +0000429 for (;(size > 0) && (*cp == ' '); cp++, size--);
430 if ((size > 0) && *cp)
431 return -1;
432 return val;
433}
Eric Andersencc8ed391999-10-05 16:24:54 +0000434
Erik Andersen1ad302a2000-03-24 00:54:46 +0000435
Erik Andersen298854f2000-03-23 01:09:18 +0000436/* Parse the tar header and fill in the nice struct with the details */
437static int
Erik Andersen3364d782000-03-28 00:58:14 +0000438readTarHeader(struct TarHeader *rawHeader, struct TarInfo *header)
Erik Andersen298854f2000-03-23 01:09:18 +0000439{
Erik Andersene454fb62000-03-23 04:27:58 +0000440 int i;
Erik Andersen84e09e42000-04-08 20:58:35 +0000441 long chksum, sum=0;
Erik Andersene454fb62000-03-23 04:27:58 +0000442 unsigned char *s = (unsigned char *)rawHeader;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000443
Erik Andersen298854f2000-03-23 01:09:18 +0000444 header->name = rawHeader->name;
Erik Andersen84e09e42000-04-08 20:58:35 +0000445 /* Check for and relativify any absolute paths */
446 if ( *(header->name) == '/' ) {
447 static int alreadyWarned=FALSE;
448
449 while (*(header->name) == '/')
450 ++*(header->name);
451
452 if (alreadyWarned == FALSE) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000453 errorMsg("Removing leading '/' from member names\n");
Erik Andersen84e09e42000-04-08 20:58:35 +0000454 alreadyWarned = TRUE;
455 }
456 }
457
Erik Andersen298854f2000-03-23 01:09:18 +0000458 header->mode = getOctal(rawHeader->mode, sizeof(rawHeader->mode));
459 header->uid = getOctal(rawHeader->uid, sizeof(rawHeader->uid));
460 header->gid = getOctal(rawHeader->gid, sizeof(rawHeader->gid));
461 header->size = getOctal(rawHeader->size, sizeof(rawHeader->size));
462 header->mtime = getOctal(rawHeader->mtime, sizeof(rawHeader->mtime));
463 chksum = getOctal(rawHeader->chksum, sizeof(rawHeader->chksum));
464 header->type = rawHeader->typeflag;
465 header->linkname = rawHeader->linkname;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000466 header->devmajor = getOctal(rawHeader->devmajor, sizeof(rawHeader->devmajor));
467 header->devminor = getOctal(rawHeader->devminor, sizeof(rawHeader->devminor));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000468
Erik Andersen298854f2000-03-23 01:09:18 +0000469 /* Check the checksum */
Erik Andersen84e09e42000-04-08 20:58:35 +0000470 for (i = sizeof(*rawHeader); i-- != 0;) {
Erik Andersen298854f2000-03-23 01:09:18 +0000471 sum += *s++;
Erik Andersen84e09e42000-04-08 20:58:35 +0000472 }
473 /* Remove the effects of the checksum field (replace
474 * with blanks for the purposes of the checksum) */
475 s = rawHeader->chksum;
476 for (i = sizeof(rawHeader->chksum) ; i-- != 0;) {
477 sum -= *s++;
478 }
479 sum += ' ' * sizeof(rawHeader->chksum);
Erik Andersene454fb62000-03-23 04:27:58 +0000480 if (sum == chksum )
Erik Andersen298854f2000-03-23 01:09:18 +0000481 return ( TRUE);
482 return( FALSE);
483}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000484
Erik Andersene49d5ec2000-02-08 19:58:47 +0000485
Erik Andersen1ad302a2000-03-24 00:54:46 +0000486/*
487 * Read a tar file and extract or list the specified files within it.
488 * If the list is empty than all files are extracted or listed.
489 */
490static int readTarFile(const char* tarName, int extractFlag, int listFlag,
Matt Kraaib92223b2000-09-04 08:25:42 +0000491 int tostdoutFlag, int verboseFlag, char** extractList,
492 char** excludeList)
Erik Andersen1ad302a2000-03-24 00:54:46 +0000493{
Erik Andersen0817d132000-04-09 15:17:40 +0000494 int status, tarFd=-1;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000495 int errorFlag=FALSE;
496 TarHeader rawHeader;
497 TarInfo header;
Erik Andersen0817d132000-04-09 15:17:40 +0000498 char** tmpList;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000499
Erik Andersen1ad302a2000-03-24 00:54:46 +0000500 /* Open the tar file for reading. */
501 if (!strcmp(tarName, "-"))
502 tarFd = fileno(stdin);
503 else
504 tarFd = open(tarName, O_RDONLY);
505 if (tarFd < 0) {
Erik Andersendeea0482000-03-25 23:09:29 +0000506 errorMsg( "Error opening '%s': %s\n", tarName, strerror(errno));
Erik Andersen1ad302a2000-03-24 00:54:46 +0000507 return ( FALSE);
Erik Andersen06936df2000-01-23 02:14:20 +0000508 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000509
Erik Andersene49d5ec2000-02-08 19:58:47 +0000510 /* Set the umask for this process so it doesn't
Erik Andersen1ad302a2000-03-24 00:54:46 +0000511 * screw up permission setting for us later. */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000512 umask(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000513
Erik Andersen1ad302a2000-03-24 00:54:46 +0000514 /* Read the tar file, and iterate over it one file at a time */
515 while ( (status = fullRead(tarFd, (char*)&rawHeader, TAR_BLOCK_SIZE)) == TAR_BLOCK_SIZE ) {
Erik Andersene2729152000-02-18 21:34:17 +0000516
Erik Andersen1ad302a2000-03-24 00:54:46 +0000517 /* First, try to read the header */
Erik Andersen3364d782000-03-28 00:58:14 +0000518 if ( readTarHeader(&rawHeader, &header) == FALSE ) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000519 if ( *(header.name) == '\0' ) {
520 goto endgame;
521 } else {
522 errorFlag=TRUE;
523 errorMsg("Bad tar header, skipping\n");
524 continue;
525 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000526 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000527 if ( *(header.name) == '\0' )
528 goto endgame;
Erik Andersen0817d132000-04-09 15:17:40 +0000529 header.tarFd = tarFd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000530
Erik Andersen0817d132000-04-09 15:17:40 +0000531#if defined BB_FEATURE_TAR_EXCLUDE
532 {
533 int skipFlag=FALSE;
534 /* Check for excluded files.... */
535 for (tmpList=excludeList; tmpList && *tmpList; tmpList++) {
536 /* Do some extra hoop jumping for when directory names
537 * end in '/' but the entry in tmpList doesn't */
538 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
539 header.name[strlen(header.name)-1]=='/'
540 && strncmp( *tmpList, header.name,
541 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
542 /* If it is a regular file, pretend to extract it with
543 * the extractFlag set to FALSE, so the junk in the tarball
544 * is properly skipped over */
545 if ( header.type==REGTYPE || header.type==REGTYPE0 ) {
546 tarExtractRegularFile(&header, FALSE, FALSE);
547 }
548 skipFlag=TRUE;
549 break;
550 }
551 }
552 /* There are not the droids you're looking for, move along */
553 if (skipFlag==TRUE)
554 continue;
555 }
556#endif
Matt Kraaib92223b2000-09-04 08:25:42 +0000557 if (*extractList != NULL) {
558 int skipFlag = TRUE;
559 for (tmpList = extractList; *tmpList != NULL; tmpList++) {
560 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
561 header.name[strlen(header.name)-1]=='/'
562 && strncmp( *tmpList, header.name,
563 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
564 /* If it is a regular file, pretend to extract it with
565 * the extractFlag set to FALSE, so the junk in the tarball
566 * is properly skipped over */
567 skipFlag = FALSE;
568 break;
569 }
570 }
571 /* There are not the droids you're looking for, move along */
572 if (skipFlag == TRUE) {
573 if ( header.type==REGTYPE || header.type==REGTYPE0 )
574 tarExtractRegularFile(&header, FALSE, FALSE);
575 continue;
576 }
577 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000578
Matt Kraai82cfbad2000-09-15 21:18:43 +0000579 if (listFlag == TRUE) {
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000580 /* Special treatment if the list (-t) flag is on */
581 if (verboseFlag == TRUE) {
582 int len, len1;
583 char buf[35];
584 struct tm *tm = localtime (&(header.mtime));
585
586 len=printf("%s ", modeString(header.mode));
587 memset(buf, 0, 8*sizeof(char));
588 my_getpwuid(buf, header.uid);
589 if (! *buf)
590 len+=printf("%d", header.uid);
591 else
592 len+=printf("%s", buf);
593 memset(buf, 0, 8*sizeof(char));
594 my_getgrgid(buf, header.gid);
595 if (! *buf)
596 len+=printf("/%-d ", header.gid);
597 else
598 len+=printf("/%-s ", buf);
599
600 if (header.type==CHRTYPE || header.type==BLKTYPE) {
601 len1=snprintf(buf, sizeof(buf), "%ld,%-ld ",
602 header.devmajor, header.devminor);
603 } else {
604 len1=snprintf(buf, sizeof(buf), "%lu ", (long)header.size);
605 }
606 /* Jump through some hoops to make the columns match up */
607 for(;(len+len1)<31;len++)
608 printf(" ");
609 printf(buf);
610
611 /* Use ISO 8610 time format */
612 if (tm) {
613 printf ("%04d-%02d-%02d %02d:%02d:%02d ",
614 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
615 tm->tm_hour, tm->tm_min, tm->tm_sec);
616 }
617 }
Eric Andersenfdd51032000-08-02 18:48:26 +0000618 printf("%s", header.name);
Matt Kraai82cfbad2000-09-15 21:18:43 +0000619 if (verboseFlag == TRUE) {
620 if (header.type==LNKTYPE) /* If this is a link, say so */
621 printf(" link to %s", header.linkname);
622 else if (header.type==SYMTYPE)
623 printf(" -> %s", header.linkname);
624 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000625 printf("\n");
626 }
627
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000628 /* List contents if we are supposed to do that */
629 if (verboseFlag == TRUE && extractFlag == TRUE) {
630 /* Now the normal listing */
631 FILE *vbFd = stdout;
632 if (tostdoutFlag == TRUE) // If the archive goes to stdout, verbose to stderr
633 vbFd = stderr;
634 fprintf(vbFd, "%s\n", header.name);
635 }
636
Matt Kraaif4462972000-09-01 02:50:48 +0000637 /* Remove files if we would overwrite them */
Matt Kraaida542f32000-09-01 02:53:01 +0000638 if (extractFlag == TRUE && tostdoutFlag == FALSE)
Matt Kraaif4462972000-09-01 02:50:48 +0000639 unlink(header.name);
Eric Andersencc8ed391999-10-05 16:24:54 +0000640
Erik Andersen1ad302a2000-03-24 00:54:46 +0000641 /* If we got here, we can be certain we have a legitimate
642 * header to work with. So work with it. */
643 switch ( header.type ) {
644 case REGTYPE:
645 case REGTYPE0:
646 /* If the name ends in a '/' then assume it is
647 * supposed to be a directory, and fall through */
648 if (header.name[strlen(header.name)-1] != '/') {
Erik Andersen6a34b532000-04-07 06:55:38 +0000649 if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE)
650 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000651 break;
652 }
653 case DIRTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000654 if (tarExtractDirectory( &header, extractFlag, tostdoutFlag)==FALSE)
655 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000656 break;
657 case LNKTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000658 if (tarExtractHardLink( &header, extractFlag, tostdoutFlag)==FALSE)
659 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000660 break;
661 case SYMTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000662 if (tarExtractSymLink( &header, extractFlag, tostdoutFlag)==FALSE)
663 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000664 break;
665 case CHRTYPE:
666 case BLKTYPE:
667 case FIFOTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000668 if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE)
669 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000670 break;
671 default:
672 close( tarFd);
673 return( FALSE);
674 }
675 }
676 close(tarFd);
677 if (status > 0) {
678 /* Bummer - we read a partial header */
Erik Andersendeea0482000-03-25 23:09:29 +0000679 errorMsg( "Error reading '%s': %s\n", tarName, strerror(errno));
Erik Andersen1ad302a2000-03-24 00:54:46 +0000680 return ( FALSE);
681 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000682 else if (errorFlag==TRUE) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000683 errorMsg( "Error exit delayed from previous errors\n");
Erik Andersen6a34b532000-04-07 06:55:38 +0000684 return( FALSE);
685 } else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000686 return( status);
687
688 /* Stuff to do when we are done */
689endgame:
690 close( tarFd);
691 if ( *(header.name) == '\0' ) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000692 if (errorFlag==TRUE)
Matt Kraaibe84cd42000-07-12 17:02:35 +0000693 errorMsg( "Error exit delayed from previous errors\n");
Erik Andersen6a34b532000-04-07 06:55:38 +0000694 else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000695 return( TRUE);
696 }
697 return( FALSE);
698}
699
Erik Andersen6acaa402000-03-26 14:03:20 +0000700
701#ifdef BB_FEATURE_TAR_CREATE
702
Erik Andersen68a9ea42000-04-04 18:39:50 +0000703/* Some info to be carried along when creating a new tarball */
704struct TarBallInfo
705{
706 char* fileName; /* File name of the tarball */
707 int tarFd; /* Open-for-write file descriptor
708 for the tarball */
709 struct stat statBuf; /* Stat info for the tarball, letting
710 us know the inode and device that the
711 tarball lives, so we can avoid trying
712 to include the tarball into itself */
713 int verboseFlag; /* Whether to print extra stuff or not */
Erik Andersenecd51242000-04-08 03:08:21 +0000714 char** excludeList; /* List of files to not include */
Erik Andersen68a9ea42000-04-04 18:39:50 +0000715};
716typedef struct TarBallInfo TarBallInfo;
717
718
Erik Andersen6acaa402000-03-26 14:03:20 +0000719/* Put an octal string into the specified buffer.
720 * The number is zero and space padded and possibly null padded.
721 * Returns TRUE if successful. */
722static int putOctal (char *cp, int len, long value)
723{
724 int tempLength;
Erik Andersen6acaa402000-03-26 14:03:20 +0000725 char tempBuffer[32];
Erik Andersen5661fe02000-04-05 01:00:52 +0000726 char *tempString = tempBuffer;
Erik Andersen6acaa402000-03-26 14:03:20 +0000727
728 /* Create a string of the specified length with an initial space,
729 * leading zeroes and the octal number, and a trailing null. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000730 sprintf (tempString, "%0*lo", len - 1, value);
Erik Andersen6acaa402000-03-26 14:03:20 +0000731
732 /* If the string is too large, suppress the leading space. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000733 tempLength = strlen (tempString) + 1;
Erik Andersen6acaa402000-03-26 14:03:20 +0000734 if (tempLength > len) {
735 tempLength--;
736 tempString++;
737 }
738
739 /* If the string is still too large, suppress the trailing null. */
740 if (tempLength > len)
741 tempLength--;
742
743 /* If the string is still too large, fail. */
744 if (tempLength > len)
745 return FALSE;
746
747 /* Copy the string to the field. */
748 memcpy (cp, tempString, len);
749
750 return TRUE;
751}
752
Erik Andersen68a9ea42000-04-04 18:39:50 +0000753/* Write out a tar header for the specified file/directory/whatever */
Erik Andersen3364d782000-03-28 00:58:14 +0000754static int
Erik Andersen5661fe02000-04-05 01:00:52 +0000755writeTarHeader(struct TarBallInfo *tbInfo, const char *fileName, struct stat *statbuf)
Erik Andersen6acaa402000-03-26 14:03:20 +0000756{
Erik Andersen5661fe02000-04-05 01:00:52 +0000757 long chksum=0;
758 struct TarHeader header;
Erik Andersen0817d132000-04-09 15:17:40 +0000759#if defined BB_FEATURE_TAR_EXCLUDE
Erik Andersenecd51242000-04-08 03:08:21 +0000760 char** tmpList;
Erik Andersen0817d132000-04-09 15:17:40 +0000761#endif
Erik Andersen5661fe02000-04-05 01:00:52 +0000762 const unsigned char *cp = (const unsigned char *) &header;
763 ssize_t size = sizeof(struct TarHeader);
Eric Andersenfdd51032000-08-02 18:48:26 +0000764
Erik Andersen5661fe02000-04-05 01:00:52 +0000765 memset( &header, 0, size);
Erik Andersen3364d782000-03-28 00:58:14 +0000766
Erik Andersen68a9ea42000-04-04 18:39:50 +0000767 if (*fileName=='/') {
768 static int alreadyWarned=FALSE;
769 if (alreadyWarned==FALSE) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000770 errorMsg("Removing leading '/' from member names\n");
Erik Andersen68a9ea42000-04-04 18:39:50 +0000771 alreadyWarned=TRUE;
772 }
Erik Andersen84e09e42000-04-08 20:58:35 +0000773 strncpy(header.name, fileName+1, sizeof(header.name));
Erik Andersen3364d782000-03-28 00:58:14 +0000774 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000775 else {
Erik Andersen84e09e42000-04-08 20:58:35 +0000776 strncpy(header.name, fileName, sizeof(header.name));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000777 }
Erik Andersen84e09e42000-04-08 20:58:35 +0000778
Erik Andersen0817d132000-04-09 15:17:40 +0000779#if defined BB_FEATURE_TAR_EXCLUDE
780 /* Check for excluded files.... */
Erik Andersenecd51242000-04-08 03:08:21 +0000781 for (tmpList=tbInfo->excludeList; tmpList && *tmpList; tmpList++) {
Erik Andersen0817d132000-04-09 15:17:40 +0000782 /* Do some extra hoop jumping for when directory names
783 * end in '/' but the entry in tmpList doesn't */
784 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
785 header.name[strlen(header.name)-1]=='/'
786 && strncmp( *tmpList, header.name,
787 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
788 /* Set the mode to something that is not a regular file, thereby
789 * faking out writeTarFile into thinking that nothing further need
790 * be done for this file. Yes, I know this is ugly, but it works. */
791 statbuf->st_mode = 0;
792 return( TRUE);
793 }
Erik Andersenecd51242000-04-08 03:08:21 +0000794 }
Erik Andersen0817d132000-04-09 15:17:40 +0000795#endif
Erik Andersenecd51242000-04-08 03:08:21 +0000796
Erik Andersen5661fe02000-04-05 01:00:52 +0000797 putOctal(header.mode, sizeof(header.mode), statbuf->st_mode);
798 putOctal(header.uid, sizeof(header.uid), statbuf->st_uid);
799 putOctal(header.gid, sizeof(header.gid), statbuf->st_gid);
800 putOctal(header.size, sizeof(header.size), 0); /* Regular file size is handled later */
801 putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime);
802 strncpy(header.magic, TAR_MAGIC TAR_VERSION,
803 TAR_MAGIC_LEN + TAR_VERSION_LEN );
Erik Andersen68a9ea42000-04-04 18:39:50 +0000804
Erik Andersen84e09e42000-04-08 20:58:35 +0000805 /* Enter the user and group names (default to root if it fails) */
Erik Andersen5661fe02000-04-05 01:00:52 +0000806 my_getpwuid(header.uname, statbuf->st_uid);
Erik Andersen5661fe02000-04-05 01:00:52 +0000807 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000808 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +0000809 my_getgrgid(header.gname, statbuf->st_gid);
810 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000811 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +0000812
Erik Andersen95c1c1e2000-04-14 21:45:29 +0000813 /* WARNING/NOTICE: I break Hard Links */
Erik Andersen68a9ea42000-04-04 18:39:50 +0000814 if (S_ISLNK(statbuf->st_mode)) {
Eric Andersen3adffb72000-06-26 10:54:06 +0000815 int link_size=0;
Erik Andersen5661fe02000-04-05 01:00:52 +0000816 char buffer[BUFSIZ];
817 header.typeflag = SYMTYPE;
Eric Andersen3adffb72000-06-26 10:54:06 +0000818 link_size = readlink(fileName, buffer, sizeof(buffer) - 1);
819 if ( link_size < 0) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000820 errorMsg("Error reading symlink '%s': %s\n", header.name, strerror(errno));
821 return ( FALSE);
822 }
Eric Andersen3adffb72000-06-26 10:54:06 +0000823 buffer[link_size] = '\0';
Erik Andersen95c1c1e2000-04-14 21:45:29 +0000824 strncpy(header.linkname, buffer, sizeof(header.linkname));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000825 } else if (S_ISDIR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000826 header.typeflag = DIRTYPE;
827 strncat(header.name, "/", sizeof(header.name));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000828 } else if (S_ISCHR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000829 header.typeflag = CHRTYPE;
830 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
831 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000832 } else if (S_ISBLK(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000833 header.typeflag = BLKTYPE;
834 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
835 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000836 } else if (S_ISFIFO(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000837 header.typeflag = FIFOTYPE;
838 } else if (S_ISREG(statbuf->st_mode)) {
839 header.typeflag = REGTYPE;
840 putOctal(header.size, sizeof(header.size), statbuf->st_size);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000841 } else {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000842 errorMsg("%s: Unknown file type\n", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000843 return ( FALSE);
844 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000845
Erik Andersen5661fe02000-04-05 01:00:52 +0000846 /* Calculate and store the checksum (i.e. the sum of all of the bytes of
847 * the header). The checksum field must be filled with blanks for the
848 * calculation. The checksum field is formatted differently from the
849 * other fields: it has [6] digits, a null, then a space -- rather than
850 * digits, followed by a null like the other fields... */
851 memset(header.chksum, ' ', sizeof(header.chksum));
852 cp = (const unsigned char *) &header;
853 while (size-- > 0)
854 chksum += *cp++;
855 putOctal(header.chksum, 7, chksum);
856
857 /* Now write the header out to disk */
858 if ((size=fullWrite(tbInfo->tarFd, (char*)&header, sizeof(struct TarHeader))) < 0) {
859 errorMsg(io_error, fileName, strerror(errno));
860 return ( FALSE);
861 }
862 /* Pad the header up to the tar block size */
863 for (; size<TAR_BLOCK_SIZE; size++) {
864 write(tbInfo->tarFd, "\0", 1);
865 }
866 /* Now do the verbose thing (or not) */
Eric Andersenfdd51032000-08-02 18:48:26 +0000867 if (tbInfo->verboseFlag==TRUE) {
868 FILE *vbFd = stdout;
869 if (tbInfo->tarFd == fileno(stdout)) // If the archive goes to stdout, verbose to stderr
870 vbFd = stderr;
871 fprintf(vbFd, "%s\n", header.name);
872 }
Erik Andersen3364d782000-03-28 00:58:14 +0000873
874 return ( TRUE);
875}
876
877
Erik Andersen68a9ea42000-04-04 18:39:50 +0000878static int writeFileToTarball(const char *fileName, struct stat *statbuf, void* userData)
Erik Andersen3364d782000-03-28 00:58:14 +0000879{
Erik Andersen68a9ea42000-04-04 18:39:50 +0000880 struct TarBallInfo *tbInfo = (struct TarBallInfo *)userData;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000881
882 /* It is against the rules to archive a socket */
883 if (S_ISSOCK(statbuf->st_mode)) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000884 errorMsg("%s: socket ignored\n", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000885 return( TRUE);
886 }
887
888 /* It is a bad idea to store the archive we are in the process of creating,
889 * so check the device and inode to be sure that this particular file isn't
890 * the new tarball */
891 if (tbInfo->statBuf.st_dev == statbuf->st_dev &&
892 tbInfo->statBuf.st_ino == statbuf->st_ino) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000893 errorMsg("%s: file is the archive; skipping\n", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000894 return( TRUE);
895 }
896
Erik Andersen5661fe02000-04-05 01:00:52 +0000897 if (writeTarHeader(tbInfo, fileName, statbuf)==FALSE) {
898 return( FALSE);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000899 }
Erik Andersen5661fe02000-04-05 01:00:52 +0000900
901 /* Now, if the file is a regular file, copy it out to the tarball */
902 if (S_ISREG(statbuf->st_mode)) {
903 int inputFileFd;
904 char buffer[BUFSIZ];
905 ssize_t size=0, readSize=0;
906
907 /* open the file we want to archive, and make sure all is well */
908 if ((inputFileFd = open(fileName, O_RDONLY)) < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000909 errorMsg("%s: Cannot open: %s\n", fileName, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +0000910 return( FALSE);
911 }
912
913 /* write the file to the archive */
914 while ( (size = fullRead(inputFileFd, buffer, sizeof(buffer))) > 0 ) {
915 if (fullWrite(tbInfo->tarFd, buffer, size) != size ) {
916 /* Output file seems to have a problem */
917 errorMsg(io_error, fileName, strerror(errno));
918 return( FALSE);
919 }
920 readSize+=size;
921 }
922 if (size == -1) {
923 errorMsg(io_error, fileName, strerror(errno));
924 return( FALSE);
925 }
926 /* Pad the file up to the tar block size */
927 for (; (readSize%TAR_BLOCK_SIZE) != 0; readSize++) {
928 write(tbInfo->tarFd, "\0", 1);
929 }
930 close( inputFileFd);
931 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000932
933 return( TRUE);
Erik Andersen6acaa402000-03-26 14:03:20 +0000934}
935
Eric Andersen46a98df2000-09-19 21:35:14 +0000936static int writeTarFile(const char* tarName, int tostdoutFlag,
937 int verboseFlag, int argc, char **argv, char** excludeList)
Erik Andersen6acaa402000-03-26 14:03:20 +0000938{
Erik Andersen3364d782000-03-28 00:58:14 +0000939 int tarFd=-1;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000940 int errorFlag=FALSE;
Erik Andersen5661fe02000-04-05 01:00:52 +0000941 ssize_t size;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000942 struct TarBallInfo tbInfo;
943 tbInfo.verboseFlag = verboseFlag;
Erik Andersen3364d782000-03-28 00:58:14 +0000944
945 /* Make sure there is at least one file to tar up. */
946 if (argc <= 0)
Matt Kraaibe84cd42000-07-12 17:02:35 +0000947 fatalError("Cowardly refusing to create an empty archive\n");
Erik Andersen6acaa402000-03-26 14:03:20 +0000948
949 /* Open the tar file for writing. */
Eric Andersen46a98df2000-09-19 21:35:14 +0000950 if (tostdoutFlag == TRUE)
Erik Andersen68a9ea42000-04-04 18:39:50 +0000951 tbInfo.tarFd = fileno(stdout);
Erik Andersen6acaa402000-03-26 14:03:20 +0000952 else
Erik Andersen68a9ea42000-04-04 18:39:50 +0000953 tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
954 if (tbInfo.tarFd < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000955 errorMsg( "Error opening '%s': %s\n", tarName, strerror(errno));
Erik Andersen6acaa402000-03-26 14:03:20 +0000956 return ( FALSE);
957 }
Erik Andersenecd51242000-04-08 03:08:21 +0000958 tbInfo.excludeList=excludeList;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000959 /* Store the stat info for the tarball's file, so
960 * can avoid including the tarball into itself.... */
961 if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0)
Erik Andersen3364d782000-03-28 00:58:14 +0000962 fatalError(io_error, tarName, strerror(errno));
Erik Andersen6acaa402000-03-26 14:03:20 +0000963
964 /* Set the umask for this process so it doesn't
965 * screw up permission setting for us later. */
966 umask(0);
967
968 /* Read the directory/files and iterate over them one at a time */
Erik Andersen3364d782000-03-28 00:58:14 +0000969 while (argc-- > 0) {
970 if (recursiveAction(*argv++, TRUE, FALSE, FALSE,
Erik Andersen68a9ea42000-04-04 18:39:50 +0000971 writeFileToTarball, writeFileToTarball,
972 (void*) &tbInfo) == FALSE) {
973 errorFlag = TRUE;
Erik Andersen3364d782000-03-28 00:58:14 +0000974 }
Erik Andersen6acaa402000-03-26 14:03:20 +0000975 }
Erik Andersen5661fe02000-04-05 01:00:52 +0000976 /* Write two empty blocks to the end of the archive */
977 for (size=0; size<(2*TAR_BLOCK_SIZE); size++) {
978 write(tbInfo.tarFd, "\0", 1);
979 }
Erik Andersen0817d132000-04-09 15:17:40 +0000980
981 /* To be pedantically correct, we would check if the tarball
982 * is smaller then 20 tar blocks, and pad it if it was smaller,
983 * but that isn't necessary for GNU tar interoperability, and
984 * so is considered a waste of space */
985
Erik Andersen68a9ea42000-04-04 18:39:50 +0000986 /* Hang up the tools, close up shop, head home */
Erik Andersen6acaa402000-03-26 14:03:20 +0000987 close(tarFd);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000988 if (errorFlag == TRUE) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000989 errorMsg("Error exit delayed from previous errors\n");
Erik Andersen68a9ea42000-04-04 18:39:50 +0000990 return(FALSE);
991 }
Erik Andersen6acaa402000-03-26 14:03:20 +0000992 return( TRUE);
993}
994
995
996#endif
997