blob: d3b0e5c0a50c2160e07af0076f68d0ede6658faa [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>
Eric Andersencc8ed391999-10-05 16:24:54 +000052
Erik Andersen298854f2000-03-23 01:09:18 +000053/* Tar file constants */
Erik Andersen1ad302a2000-03-24 00:54:46 +000054#ifndef MAJOR
55#define MAJOR(dev) (((dev)>>8)&0xff)
56#define MINOR(dev) ((dev)&0xff)
57#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000058
59
Erik Andersen298854f2000-03-23 01:09:18 +000060/* POSIX tar Header Block, from POSIX 1003.1-1990 */
61struct TarHeader
62{
63 /* byte offset */
Erik Andersen0817d132000-04-09 15:17:40 +000064 char name[100]; /* 0-99 */
65 char mode[8]; /* 100-107 */
66 char uid[8]; /* 108-115 */
67 char gid[8]; /* 116-123 */
68 char size[12]; /* 124-135 */
69 char mtime[12]; /* 136-147 */
70 char chksum[8]; /* 148-155 */
71 char typeflag; /* 156-156 */
72 char linkname[100]; /* 157-256 */
73 char magic[6]; /* 257-262 */
74 char version[2]; /* 263-264 */
75 char uname[32]; /* 265-296 */
76 char gname[32]; /* 297-328 */
77 char devmajor[8]; /* 329-336 */
78 char devminor[8]; /* 337-344 */
79 char prefix[155]; /* 345-499 */
80 char padding[12]; /* 500-512 (pad to exactly the TAR_BLOCK_SIZE) */
Erik Andersen298854f2000-03-23 01:09:18 +000081};
82typedef struct TarHeader TarHeader;
Eric Andersencc8ed391999-10-05 16:24:54 +000083
Eric Andersencc8ed391999-10-05 16:24:54 +000084
Erik Andersen298854f2000-03-23 01:09:18 +000085/* A few useful constants */
86#define TAR_MAGIC "ustar" /* ustar and a null */
Erik Andersen0817d132000-04-09 15:17:40 +000087#define TAR_VERSION " " /* Be compatable with GNU tar format */
Erik Andersen298854f2000-03-23 01:09:18 +000088#define TAR_MAGIC_LEN 6
89#define TAR_VERSION_LEN 2
Erik Andersen298854f2000-03-23 01:09:18 +000090#define TAR_BLOCK_SIZE 512
91
92/* A nice enum with all the possible tar file content types */
93enum TarFileType
94{
95 REGTYPE = '0', /* regular file */
96 REGTYPE0 = '\0', /* regular file (ancient bug compat)*/
97 LNKTYPE = '1', /* hard link */
98 SYMTYPE = '2', /* symbolic link */
99 CHRTYPE = '3', /* character special */
100 BLKTYPE = '4', /* block special */
101 DIRTYPE = '5', /* directory */
102 FIFOTYPE = '6', /* FIFO special */
103 CONTTYPE = '7', /* reserved */
104};
105typedef enum TarFileType TarFileType;
106
107/* This struct ignores magic, non-numeric user name,
108 * non-numeric group name, and the checksum, since
109 * these are all ignored by BusyBox tar. */
110struct TarInfo
111{
112 int tarFd; /* An open file descriptor for reading from the tarball */
113 char * name; /* File name */
114 mode_t mode; /* Unix mode, including device bits. */
115 uid_t uid; /* Numeric UID */
116 gid_t gid; /* Numeric GID */
117 size_t size; /* Size of file */
118 time_t mtime; /* Last-modified time */
119 enum TarFileType type; /* Regular, directory, link, etc */
120 char * linkname; /* Name for symbolic and hard links */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000121 long devmajor; /* Major number for special device */
122 long devminor; /* Minor number for special device */
Erik Andersen298854f2000-03-23 01:09:18 +0000123};
124typedef struct TarInfo TarInfo;
125
Erik Andersene454fb62000-03-23 04:27:58 +0000126/* Local procedures to restore files from a tar file. */
Erik Andersen298854f2000-03-23 01:09:18 +0000127static int readTarFile(const char* tarName, int extractFlag, int listFlag,
Matt Kraaib92223b2000-09-04 08:25:42 +0000128 int tostdoutFlag, int verboseFlag, char** extractList,
129 char** excludeList);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000130
131
Eric Andersencc8ed391999-10-05 16:24:54 +0000132
Erik Andersen05100cd2000-01-16 01:30:52 +0000133#ifdef BB_FEATURE_TAR_CREATE
Erik Andersen6acaa402000-03-26 14:03:20 +0000134/* Local procedures to save files into a tar file. */
Erik Andersen68a9ea42000-04-04 18:39:50 +0000135static int writeTarFile(const char* tarName, int tostdoutFlag,
Erik Andersenecd51242000-04-08 03:08:21 +0000136 int verboseFlag, int argc, char **argv, char** excludeList);
Erik Andersen05100cd2000-01-16 01:30:52 +0000137#endif
138
Eric Andersencc8ed391999-10-05 16:24:54 +0000139
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 Andersenfdd51032000-08-02 18:48:26 +0000152 int opt;
Eric Andersencc8ed391999-10-05 16:24:54 +0000153
Erik Andersenecd51242000-04-08 03:08:21 +0000154 if (argc <= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000155 usage(tar_usage);
Eric Andersencc8ed391999-10-05 16:24:54 +0000156
Eric Andersenfdd51032000-08-02 18:48:26 +0000157 /* do normal option parsing */
158 while ((opt = getopt(argc, argv, "cxtvOf:-:")) > 0) {
159 switch (opt) {
Erik Andersenecd51242000-04-08 03:08:21 +0000160 case 'c':
161 if (extractFlag == TRUE || listFlag == TRUE)
162 goto flagError;
163 createFlag = TRUE;
164 break;
Eric Andersenfdd51032000-08-02 18:48:26 +0000165 case 'x':
166 if (listFlag == TRUE || createFlag == TRUE)
167 goto flagError;
168 extractFlag = TRUE;
169 break;
170 case 't':
171 if (extractFlag == TRUE || createFlag == TRUE)
172 goto flagError;
173 listFlag = TRUE;
174 break;
Erik Andersenecd51242000-04-08 03:08:21 +0000175 case 'v':
176 verboseFlag = TRUE;
177 break;
Erik Andersenecd51242000-04-08 03:08:21 +0000178 case 'O':
179 tostdoutFlag = TRUE;
180 tarName = "-";
Eric Andersenfdd51032000-08-02 18:48:26 +0000181 break;
182 case 'f':
183 if (*tarName != '-')
184 fatalError( "Only one 'f' option allowed\n");
185 tarName = optarg;
186 if (!strcmp(tarName, "-") && createFlag == TRUE)
187 tostdoutFlag = TRUE;
Erik Andersenecd51242000-04-08 03:08:21 +0000188 break;
Erik Andersenecd51242000-04-08 03:08:21 +0000189 case '-':
Erik Andersen0817d132000-04-09 15:17:40 +0000190#if defined BB_FEATURE_TAR_EXCLUDE
Eric Andersenfdd51032000-08-02 18:48:26 +0000191 if (strcmp(optarg, "exclude")==0) {
192 if (argv[optind]==NULL)
193 fatalError( "option `--exclude' requires an argument\n");
Matt Kraaib92223b2000-09-04 08:25:42 +0000194 excludeList=xrealloc( excludeList, sizeof(char**) * (excludeListSize+2));
Eric Andersenfdd51032000-08-02 18:48:26 +0000195 excludeList[excludeListSize] = argv[optind];
Erik Andersen0817d132000-04-09 15:17:40 +0000196 /* Remove leading "/"s */
197 if (*excludeList[excludeListSize] =='/') {
198 excludeList[excludeListSize] = (excludeList[excludeListSize])+1;
199 }
Erik Andersen0817d132000-04-09 15:17:40 +0000200 /* Tack a NULL onto the end of the list */
201 excludeList[excludeListSize] = NULL;
Eric Andersenfdd51032000-08-02 18:48:26 +0000202 optind++;
Erik Andersen0817d132000-04-09 15:17:40 +0000203 break;
204 }
205#endif
Eric Andersenfdd51032000-08-02 18:48:26 +0000206 fatalError( "Unknown tar flag '%s'\n"
207 "Try `tar --help' for more information\n", optarg);
Erik Andersenecd51242000-04-08 03:08:21 +0000208 default:
209 fatalError( "Unknown tar flag '%c'\n"
210 "Try `tar --help' for more information\n", **argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000211 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000212 }
213
Erik Andersene49d5ec2000-02-08 19:58:47 +0000214 /*
215 * Do the correct type of action supplying the rest of the
216 * command line arguments as the list of files to process.
217 */
218 if (createFlag == TRUE) {
Erik Andersende552872000-01-23 01:34:05 +0000219#ifndef BB_FEATURE_TAR_CREATE
Erik Andersen298854f2000-03-23 01:09:18 +0000220 fatalError( "This version of tar was not compiled with tar creation support.\n");
Erik Andersende552872000-01-23 01:34:05 +0000221#else
Eric Andersenfdd51032000-08-02 18:48:26 +0000222 exit(writeTarFile(tarName, tostdoutFlag, verboseFlag, argc-optind, &argv[optind], excludeList));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000223#endif
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000224 }
225 if (listFlag == TRUE || extractFlag == TRUE) {
Matt Kraaib92223b2000-09-04 08:25:42 +0000226 exit(readTarFile(tarName, extractFlag, listFlag, tostdoutFlag, verboseFlag, &argv[optind], excludeList));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000227 }
Erik Andersende552872000-01-23 01:34:05 +0000228
Erik Andersene49d5ec2000-02-08 19:58:47 +0000229 flagError:
Erik Andersen298854f2000-03-23 01:09:18 +0000230 fatalError( "Exactly one of 'c', 'x' or 't' must be specified\n");
231}
232
233static void
Erik Andersen6a34b532000-04-07 06:55:38 +0000234fixUpPermissions(TarInfo *header)
235{
236 struct utimbuf t;
237 /* Now set permissions etc for the new file */
238 chown(header->name, header->uid, header->gid);
239 chmod(header->name, header->mode);
240 /* Reset the time */
241 t.actime = time(0);
242 t.modtime = header->mtime;
243 utime(header->name, &t);
244}
245
246static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000247tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersen298854f2000-03-23 01:09:18 +0000248{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000249 size_t writeSize;
250 size_t readSize;
251 size_t actualWriteSz;
252 char buffer[BUFSIZ];
253 size_t size = header->size;
254 int outFd=fileno(stdout);
255
256 /* Open the file to be written, if a file is supposed to be written */
257 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000258 /* Create the path to the file, just in case it isn't there...
259 * This should not screw up path permissions or anything. */
260 createPath(header->name, 0777);
Eric Andersen0c6a9702000-06-09 20:51:50 +0000261 if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY,
262 header->mode & ~S_IFMT)) < 0) {
263 errorMsg(io_error, header->name, strerror(errno));
264 return( FALSE);
265 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000266 }
267
268 /* Write out the file, if we are supposed to be doing that */
269 while ( size > 0 ) {
270 actualWriteSz=0;
271 if ( size > sizeof(buffer) )
272 writeSize = readSize = sizeof(buffer);
273 else {
274 int mod = size % 512;
275 if ( mod != 0 )
276 readSize = size + (512 - mod);
277 else
278 readSize = size;
279 writeSize = size;
280 }
281 if ( (readSize = fullRead(header->tarFd, buffer, readSize)) <= 0 ) {
282 /* Tarball seems to have a problem */
Matt Kraaibe84cd42000-07-12 17:02:35 +0000283 errorMsg("Unexpected EOF in archive\n");
Erik Andersen6a34b532000-04-07 06:55:38 +0000284 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000285 }
286 if ( readSize < writeSize )
287 writeSize = readSize;
288
289 /* Write out the file, if we are supposed to be doing that */
290 if (extractFlag==TRUE) {
291
292 if ((actualWriteSz=fullWrite(outFd, buffer, writeSize)) != writeSize ) {
293 /* Output file seems to have a problem */
294 errorMsg(io_error, header->name, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000295 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000296 }
Erik Andersen0817d132000-04-09 15:17:40 +0000297 } else {
298 actualWriteSz=writeSize;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000299 }
300
301 size -= actualWriteSz;
302 }
303
304 /* Now we are done writing the file out, so try
305 * and fix up the permissions and whatnot */
306 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000307 close(outFd);
Erik Andersen6a34b532000-04-07 06:55:38 +0000308 fixUpPermissions(header);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000309 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000310 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000311}
312
Erik Andersen6a34b532000-04-07 06:55:38 +0000313static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000314tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000315{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000316
317 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000318 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000319
320 if (createPath(header->name, header->mode) != TRUE) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000321 errorMsg("%s: Cannot mkdir: %s\n",
Erik Andersen6a34b532000-04-07 06:55:38 +0000322 header->name, strerror(errno));
323 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000324 }
325 /* make the final component, just in case it was
326 * omitted by createPath() (which will skip the
327 * directory if it doesn't have a terminating '/') */
Erik Andersen6acaa402000-03-26 14:03:20 +0000328 if (mkdir(header->name, header->mode) == 0) {
329 fixUpPermissions(header);
330 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000331 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000332}
333
Erik Andersen6a34b532000-04-07 06:55:38 +0000334static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000335tarExtractHardLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000336{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000337 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000338 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000339
340 if (link(header->linkname, header->name) < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000341 errorMsg("%s: Cannot create hard link to '%s': %s\n",
Erik Andersen5661fe02000-04-05 01:00:52 +0000342 header->name, header->linkname, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000343 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000344 }
345
346 /* Now set permissions etc for the new directory */
347 fixUpPermissions(header);
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 +0000352tarExtractSymLink(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#ifdef S_ISLNK
358 if (symlink(header->linkname, header->name) < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000359 errorMsg("%s: Cannot create symlink to '%s': %s\n",
Erik Andersen5661fe02000-04-05 01:00:52 +0000360 header->name, header->linkname, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000361 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000362 }
363 /* Try to change ownership of the symlink.
364 * If libs doesn't support that, don't bother.
365 * Changing the pointed-to-file is the Wrong Thing(tm).
366 */
367#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
368 lchown(header->name, header->uid, header->gid);
369#endif
370
371 /* Do not change permissions or date on symlink,
372 * since it changes the pointed to file instead. duh. */
373#else
Matt Kraaibe84cd42000-07-12 17:02:35 +0000374 errorMsg("%s: Cannot create symlink to '%s': %s\n",
Erik Andersen6a34b532000-04-07 06:55:38 +0000375 header->name, header->linkname,
376 "symlinks not supported");
Erik Andersen1ad302a2000-03-24 00:54:46 +0000377#endif
Erik Andersen6a34b532000-04-07 06:55:38 +0000378 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000379}
380
Erik Andersen6a34b532000-04-07 06:55:38 +0000381static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000382tarExtractSpecial(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000383{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000384 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000385 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000386
387 if (S_ISCHR(header->mode) || S_ISBLK(header->mode) || S_ISSOCK(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000388 if (mknod(header->name, header->mode, makedev(header->devmajor, header->devminor)) < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000389 errorMsg("%s: Cannot mknod: %s\n",
Erik Andersen6a34b532000-04-07 06:55:38 +0000390 header->name, strerror(errno));
391 return( FALSE);
392 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000393 } else if (S_ISFIFO(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000394 if (mkfifo(header->name, header->mode) < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000395 errorMsg("%s: Cannot mkfifo: %s\n",
Erik Andersen6a34b532000-04-07 06:55:38 +0000396 header->name, strerror(errno));
397 return( FALSE);
398 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000399 }
400
401 /* Now set permissions etc for the new directory */
402 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000403 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000404}
405
Erik Andersen1ad302a2000-03-24 00:54:46 +0000406/* Read an octal value in a field of the specified width, with optional
Erik Andersen298854f2000-03-23 01:09:18 +0000407 * spaces on both sides of the number and with an optional null character
Erik Andersen1ad302a2000-03-24 00:54:46 +0000408 * at the end. Returns -1 on an illegal format. */
Erik Andersen298854f2000-03-23 01:09:18 +0000409static long getOctal(const char *cp, int size)
Eric Andersencc8ed391999-10-05 16:24:54 +0000410{
Erik Andersen298854f2000-03-23 01:09:18 +0000411 long val = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000412
Erik Andersen298854f2000-03-23 01:09:18 +0000413 for(;(size > 0) && (*cp == ' '); cp++, size--);
414 if ((size == 0) || !isOctal(*cp))
415 return -1;
416 for(; (size > 0) && isOctal(*cp); size--) {
417 val = val * 8 + *cp++ - '0';
Eric Andersencc8ed391999-10-05 16:24:54 +0000418 }
Erik Andersen298854f2000-03-23 01:09:18 +0000419 for (;(size > 0) && (*cp == ' '); cp++, size--);
420 if ((size > 0) && *cp)
421 return -1;
422 return val;
423}
Eric Andersencc8ed391999-10-05 16:24:54 +0000424
Erik Andersen1ad302a2000-03-24 00:54:46 +0000425
Erik Andersen298854f2000-03-23 01:09:18 +0000426/* Parse the tar header and fill in the nice struct with the details */
427static int
Erik Andersen3364d782000-03-28 00:58:14 +0000428readTarHeader(struct TarHeader *rawHeader, struct TarInfo *header)
Erik Andersen298854f2000-03-23 01:09:18 +0000429{
Erik Andersene454fb62000-03-23 04:27:58 +0000430 int i;
Erik Andersen84e09e42000-04-08 20:58:35 +0000431 long chksum, sum=0;
Erik Andersene454fb62000-03-23 04:27:58 +0000432 unsigned char *s = (unsigned char *)rawHeader;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000433
Erik Andersen298854f2000-03-23 01:09:18 +0000434 header->name = rawHeader->name;
Erik Andersen84e09e42000-04-08 20:58:35 +0000435 /* Check for and relativify any absolute paths */
436 if ( *(header->name) == '/' ) {
437 static int alreadyWarned=FALSE;
438
439 while (*(header->name) == '/')
440 ++*(header->name);
441
442 if (alreadyWarned == FALSE) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000443 errorMsg("Removing leading '/' from member names\n");
Erik Andersen84e09e42000-04-08 20:58:35 +0000444 alreadyWarned = TRUE;
445 }
446 }
447
Erik Andersen298854f2000-03-23 01:09:18 +0000448 header->mode = getOctal(rawHeader->mode, sizeof(rawHeader->mode));
449 header->uid = getOctal(rawHeader->uid, sizeof(rawHeader->uid));
450 header->gid = getOctal(rawHeader->gid, sizeof(rawHeader->gid));
451 header->size = getOctal(rawHeader->size, sizeof(rawHeader->size));
452 header->mtime = getOctal(rawHeader->mtime, sizeof(rawHeader->mtime));
453 chksum = getOctal(rawHeader->chksum, sizeof(rawHeader->chksum));
454 header->type = rawHeader->typeflag;
455 header->linkname = rawHeader->linkname;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000456 header->devmajor = getOctal(rawHeader->devmajor, sizeof(rawHeader->devmajor));
457 header->devminor = getOctal(rawHeader->devminor, sizeof(rawHeader->devminor));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000458
Erik Andersen298854f2000-03-23 01:09:18 +0000459 /* Check the checksum */
Erik Andersen84e09e42000-04-08 20:58:35 +0000460 for (i = sizeof(*rawHeader); i-- != 0;) {
Erik Andersen298854f2000-03-23 01:09:18 +0000461 sum += *s++;
Erik Andersen84e09e42000-04-08 20:58:35 +0000462 }
463 /* Remove the effects of the checksum field (replace
464 * with blanks for the purposes of the checksum) */
465 s = rawHeader->chksum;
466 for (i = sizeof(rawHeader->chksum) ; i-- != 0;) {
467 sum -= *s++;
468 }
469 sum += ' ' * sizeof(rawHeader->chksum);
Erik Andersene454fb62000-03-23 04:27:58 +0000470 if (sum == chksum )
Erik Andersen298854f2000-03-23 01:09:18 +0000471 return ( TRUE);
472 return( FALSE);
473}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000474
Erik Andersene49d5ec2000-02-08 19:58:47 +0000475
Erik Andersen1ad302a2000-03-24 00:54:46 +0000476/*
477 * Read a tar file and extract or list the specified files within it.
478 * If the list is empty than all files are extracted or listed.
479 */
480static int readTarFile(const char* tarName, int extractFlag, int listFlag,
Matt Kraaib92223b2000-09-04 08:25:42 +0000481 int tostdoutFlag, int verboseFlag, char** extractList,
482 char** excludeList)
Erik Andersen1ad302a2000-03-24 00:54:46 +0000483{
Erik Andersen0817d132000-04-09 15:17:40 +0000484 int status, tarFd=-1;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000485 int errorFlag=FALSE;
486 TarHeader rawHeader;
487 TarInfo header;
Erik Andersen0817d132000-04-09 15:17:40 +0000488#if defined BB_FEATURE_TAR_EXCLUDE
489 char** tmpList;
490#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000491
Erik Andersen1ad302a2000-03-24 00:54:46 +0000492 /* Open the tar file for reading. */
493 if (!strcmp(tarName, "-"))
494 tarFd = fileno(stdin);
495 else
496 tarFd = open(tarName, O_RDONLY);
497 if (tarFd < 0) {
Erik Andersendeea0482000-03-25 23:09:29 +0000498 errorMsg( "Error opening '%s': %s\n", tarName, strerror(errno));
Erik Andersen1ad302a2000-03-24 00:54:46 +0000499 return ( FALSE);
Erik Andersen06936df2000-01-23 02:14:20 +0000500 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000501
Erik Andersene49d5ec2000-02-08 19:58:47 +0000502 /* Set the umask for this process so it doesn't
Erik Andersen1ad302a2000-03-24 00:54:46 +0000503 * screw up permission setting for us later. */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000504 umask(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000505
Erik Andersen1ad302a2000-03-24 00:54:46 +0000506 /* Read the tar file, and iterate over it one file at a time */
507 while ( (status = fullRead(tarFd, (char*)&rawHeader, TAR_BLOCK_SIZE)) == TAR_BLOCK_SIZE ) {
Erik Andersene2729152000-02-18 21:34:17 +0000508
Erik Andersen1ad302a2000-03-24 00:54:46 +0000509 /* First, try to read the header */
Erik Andersen3364d782000-03-28 00:58:14 +0000510 if ( readTarHeader(&rawHeader, &header) == FALSE ) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000511 if ( *(header.name) == '\0' ) {
512 goto endgame;
513 } else {
514 errorFlag=TRUE;
515 errorMsg("Bad tar header, skipping\n");
516 continue;
517 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000518 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000519 if ( *(header.name) == '\0' )
520 goto endgame;
Erik Andersen0817d132000-04-09 15:17:40 +0000521 header.tarFd = tarFd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000522
Erik Andersen0817d132000-04-09 15:17:40 +0000523#if defined BB_FEATURE_TAR_EXCLUDE
524 {
525 int skipFlag=FALSE;
526 /* Check for excluded files.... */
527 for (tmpList=excludeList; tmpList && *tmpList; tmpList++) {
528 /* Do some extra hoop jumping for when directory names
529 * end in '/' but the entry in tmpList doesn't */
530 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
531 header.name[strlen(header.name)-1]=='/'
532 && strncmp( *tmpList, header.name,
533 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
534 /* If it is a regular file, pretend to extract it with
535 * the extractFlag set to FALSE, so the junk in the tarball
536 * is properly skipped over */
537 if ( header.type==REGTYPE || header.type==REGTYPE0 ) {
538 tarExtractRegularFile(&header, FALSE, FALSE);
539 }
540 skipFlag=TRUE;
541 break;
542 }
543 }
544 /* There are not the droids you're looking for, move along */
545 if (skipFlag==TRUE)
546 continue;
547 }
548#endif
Matt Kraaib92223b2000-09-04 08:25:42 +0000549 if (*extractList != NULL) {
550 int skipFlag = TRUE;
551 for (tmpList = extractList; *tmpList != NULL; tmpList++) {
552 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
553 header.name[strlen(header.name)-1]=='/'
554 && strncmp( *tmpList, header.name,
555 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
556 /* If it is a regular file, pretend to extract it with
557 * the extractFlag set to FALSE, so the junk in the tarball
558 * is properly skipped over */
559 skipFlag = FALSE;
560 break;
561 }
562 }
563 /* There are not the droids you're looking for, move along */
564 if (skipFlag == TRUE) {
565 if ( header.type==REGTYPE || header.type==REGTYPE0 )
566 tarExtractRegularFile(&header, FALSE, FALSE);
567 continue;
568 }
569 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000570 /* Special treatment if the list (-t) flag is on */
571 if (verboseFlag == TRUE && extractFlag == FALSE) {
572 int len, len1;
573 char buf[35];
574 struct tm *tm = localtime (&(header.mtime));
575
Erik Andersen6a34b532000-04-07 06:55:38 +0000576 len=printf("%s ", modeString(header.mode));
577 memset(buf, 0, 8*sizeof(char));
578 my_getpwuid(buf, header.uid);
579 if (! *buf)
580 len+=printf("%d", header.uid);
581 else
582 len+=printf("%s", buf);
583 memset(buf, 0, 8*sizeof(char));
584 my_getgrgid(buf, header.gid);
585 if (! *buf)
586 len+=printf("/%-d ", header.gid);
587 else
588 len+=printf("/%-s ", buf);
589
Erik Andersen1ad302a2000-03-24 00:54:46 +0000590 if (header.type==CHRTYPE || header.type==BLKTYPE) {
591 len1=snprintf(buf, sizeof(buf), "%ld,%-ld ",
592 header.devmajor, header.devminor);
593 } else {
Erik Andersen1d1d9502000-04-21 01:26:49 +0000594 len1=snprintf(buf, sizeof(buf), "%lu ", (long)header.size);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000595 }
596 /* Jump through some hoops to make the columns match up */
597 for(;(len+len1)<31;len++)
598 printf(" ");
599 printf(buf);
600
601 /* Use ISO 8610 time format */
602 if (tm) {
603 printf ("%04d-%02d-%02d %02d:%02d:%02d ",
604 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
605 tm->tm_hour, tm->tm_min, tm->tm_sec);
606 }
607 }
608 /* List contents if we are supposed to do that */
Eric Andersenfdd51032000-08-02 18:48:26 +0000609 if (verboseFlag == TRUE && listFlag != TRUE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000610 /* Now the normal listing */
Eric Andersenfdd51032000-08-02 18:48:26 +0000611 FILE *vbFd = stdout;
612 if (tostdoutFlag == TRUE) // If the archive goes to stdout, verbose to stderr
613 vbFd = stderr;
614 fprintf(vbFd, "%s\n", header.name);
Erik Andersen6a34b532000-04-07 06:55:38 +0000615 }
Eric Andersenfdd51032000-08-02 18:48:26 +0000616
Erik Andersen6a34b532000-04-07 06:55:38 +0000617 if (verboseFlag == TRUE && listFlag == TRUE) {
Eric Andersenfdd51032000-08-02 18:48:26 +0000618 printf("%s", header.name);
619 if (header.type==LNKTYPE) /* If this is a link, say so */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000620 printf(" link to %s", header.linkname);
621 else if (header.type==SYMTYPE)
622 printf(" -> %s", header.linkname);
623 printf("\n");
624 }
625
Matt Kraaif4462972000-09-01 02:50:48 +0000626 /* Remove files if we would overwrite them */
Matt Kraaida542f32000-09-01 02:53:01 +0000627 if (extractFlag == TRUE && tostdoutFlag == FALSE)
Matt Kraaif4462972000-09-01 02:50:48 +0000628 unlink(header.name);
Eric Andersencc8ed391999-10-05 16:24:54 +0000629
Erik Andersen1ad302a2000-03-24 00:54:46 +0000630 /* If we got here, we can be certain we have a legitimate
631 * header to work with. So work with it. */
632 switch ( header.type ) {
633 case REGTYPE:
634 case REGTYPE0:
635 /* If the name ends in a '/' then assume it is
636 * supposed to be a directory, and fall through */
637 if (header.name[strlen(header.name)-1] != '/') {
Erik Andersen6a34b532000-04-07 06:55:38 +0000638 if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE)
639 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000640 break;
641 }
642 case DIRTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000643 if (tarExtractDirectory( &header, extractFlag, tostdoutFlag)==FALSE)
644 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000645 break;
646 case LNKTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000647 if (tarExtractHardLink( &header, extractFlag, tostdoutFlag)==FALSE)
648 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000649 break;
650 case SYMTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000651 if (tarExtractSymLink( &header, extractFlag, tostdoutFlag)==FALSE)
652 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000653 break;
654 case CHRTYPE:
655 case BLKTYPE:
656 case FIFOTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000657 if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE)
658 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000659 break;
660 default:
661 close( tarFd);
662 return( FALSE);
663 }
664 }
665 close(tarFd);
666 if (status > 0) {
667 /* Bummer - we read a partial header */
Erik Andersendeea0482000-03-25 23:09:29 +0000668 errorMsg( "Error reading '%s': %s\n", tarName, strerror(errno));
Erik Andersen1ad302a2000-03-24 00:54:46 +0000669 return ( FALSE);
670 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000671 else if (errorFlag==TRUE) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000672 errorMsg( "Error exit delayed from previous errors\n");
Erik Andersen6a34b532000-04-07 06:55:38 +0000673 return( FALSE);
674 } else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000675 return( status);
676
677 /* Stuff to do when we are done */
678endgame:
679 close( tarFd);
680 if ( *(header.name) == '\0' ) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000681 if (errorFlag==TRUE)
Matt Kraaibe84cd42000-07-12 17:02:35 +0000682 errorMsg( "Error exit delayed from previous errors\n");
Erik Andersen6a34b532000-04-07 06:55:38 +0000683 else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000684 return( TRUE);
685 }
686 return( FALSE);
687}
688
Erik Andersen6acaa402000-03-26 14:03:20 +0000689
690#ifdef BB_FEATURE_TAR_CREATE
691
Erik Andersen68a9ea42000-04-04 18:39:50 +0000692/* Some info to be carried along when creating a new tarball */
693struct TarBallInfo
694{
695 char* fileName; /* File name of the tarball */
696 int tarFd; /* Open-for-write file descriptor
697 for the tarball */
698 struct stat statBuf; /* Stat info for the tarball, letting
699 us know the inode and device that the
700 tarball lives, so we can avoid trying
701 to include the tarball into itself */
702 int verboseFlag; /* Whether to print extra stuff or not */
Erik Andersenecd51242000-04-08 03:08:21 +0000703 char** excludeList; /* List of files to not include */
Erik Andersen68a9ea42000-04-04 18:39:50 +0000704};
705typedef struct TarBallInfo TarBallInfo;
706
707
Erik Andersen6acaa402000-03-26 14:03:20 +0000708/* Put an octal string into the specified buffer.
709 * The number is zero and space padded and possibly null padded.
710 * Returns TRUE if successful. */
711static int putOctal (char *cp, int len, long value)
712{
713 int tempLength;
Erik Andersen6acaa402000-03-26 14:03:20 +0000714 char tempBuffer[32];
Erik Andersen5661fe02000-04-05 01:00:52 +0000715 char *tempString = tempBuffer;
Erik Andersen6acaa402000-03-26 14:03:20 +0000716
717 /* Create a string of the specified length with an initial space,
718 * leading zeroes and the octal number, and a trailing null. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000719 sprintf (tempString, "%0*lo", len - 1, value);
Erik Andersen6acaa402000-03-26 14:03:20 +0000720
721 /* If the string is too large, suppress the leading space. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000722 tempLength = strlen (tempString) + 1;
Erik Andersen6acaa402000-03-26 14:03:20 +0000723 if (tempLength > len) {
724 tempLength--;
725 tempString++;
726 }
727
728 /* If the string is still too large, suppress the trailing null. */
729 if (tempLength > len)
730 tempLength--;
731
732 /* If the string is still too large, fail. */
733 if (tempLength > len)
734 return FALSE;
735
736 /* Copy the string to the field. */
737 memcpy (cp, tempString, len);
738
739 return TRUE;
740}
741
Erik Andersen68a9ea42000-04-04 18:39:50 +0000742/* Write out a tar header for the specified file/directory/whatever */
Erik Andersen3364d782000-03-28 00:58:14 +0000743static int
Erik Andersen5661fe02000-04-05 01:00:52 +0000744writeTarHeader(struct TarBallInfo *tbInfo, const char *fileName, struct stat *statbuf)
Erik Andersen6acaa402000-03-26 14:03:20 +0000745{
Erik Andersen5661fe02000-04-05 01:00:52 +0000746 long chksum=0;
747 struct TarHeader header;
Erik Andersen0817d132000-04-09 15:17:40 +0000748#if defined BB_FEATURE_TAR_EXCLUDE
Erik Andersenecd51242000-04-08 03:08:21 +0000749 char** tmpList;
Erik Andersen0817d132000-04-09 15:17:40 +0000750#endif
Erik Andersen5661fe02000-04-05 01:00:52 +0000751 const unsigned char *cp = (const unsigned char *) &header;
752 ssize_t size = sizeof(struct TarHeader);
Eric Andersenfdd51032000-08-02 18:48:26 +0000753
Erik Andersen5661fe02000-04-05 01:00:52 +0000754 memset( &header, 0, size);
Erik Andersen3364d782000-03-28 00:58:14 +0000755
Erik Andersen68a9ea42000-04-04 18:39:50 +0000756 if (*fileName=='/') {
757 static int alreadyWarned=FALSE;
758 if (alreadyWarned==FALSE) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000759 errorMsg("Removing leading '/' from member names\n");
Erik Andersen68a9ea42000-04-04 18:39:50 +0000760 alreadyWarned=TRUE;
761 }
Erik Andersen84e09e42000-04-08 20:58:35 +0000762 strncpy(header.name, fileName+1, sizeof(header.name));
Erik Andersen3364d782000-03-28 00:58:14 +0000763 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000764 else {
Erik Andersen84e09e42000-04-08 20:58:35 +0000765 strncpy(header.name, fileName, sizeof(header.name));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000766 }
Erik Andersen84e09e42000-04-08 20:58:35 +0000767
Erik Andersen0817d132000-04-09 15:17:40 +0000768#if defined BB_FEATURE_TAR_EXCLUDE
769 /* Check for excluded files.... */
Erik Andersenecd51242000-04-08 03:08:21 +0000770 for (tmpList=tbInfo->excludeList; tmpList && *tmpList; tmpList++) {
Erik Andersen0817d132000-04-09 15:17:40 +0000771 /* Do some extra hoop jumping for when directory names
772 * end in '/' but the entry in tmpList doesn't */
773 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
774 header.name[strlen(header.name)-1]=='/'
775 && strncmp( *tmpList, header.name,
776 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
777 /* Set the mode to something that is not a regular file, thereby
778 * faking out writeTarFile into thinking that nothing further need
779 * be done for this file. Yes, I know this is ugly, but it works. */
780 statbuf->st_mode = 0;
781 return( TRUE);
782 }
Erik Andersenecd51242000-04-08 03:08:21 +0000783 }
Erik Andersen0817d132000-04-09 15:17:40 +0000784#endif
Erik Andersenecd51242000-04-08 03:08:21 +0000785
Erik Andersen5661fe02000-04-05 01:00:52 +0000786 putOctal(header.mode, sizeof(header.mode), statbuf->st_mode);
787 putOctal(header.uid, sizeof(header.uid), statbuf->st_uid);
788 putOctal(header.gid, sizeof(header.gid), statbuf->st_gid);
789 putOctal(header.size, sizeof(header.size), 0); /* Regular file size is handled later */
790 putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime);
791 strncpy(header.magic, TAR_MAGIC TAR_VERSION,
792 TAR_MAGIC_LEN + TAR_VERSION_LEN );
Erik Andersen68a9ea42000-04-04 18:39:50 +0000793
Erik Andersen84e09e42000-04-08 20:58:35 +0000794 /* Enter the user and group names (default to root if it fails) */
Erik Andersen5661fe02000-04-05 01:00:52 +0000795 my_getpwuid(header.uname, statbuf->st_uid);
Erik Andersen5661fe02000-04-05 01:00:52 +0000796 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000797 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +0000798 my_getgrgid(header.gname, statbuf->st_gid);
799 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000800 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +0000801
Erik Andersen95c1c1e2000-04-14 21:45:29 +0000802 /* WARNING/NOTICE: I break Hard Links */
Erik Andersen68a9ea42000-04-04 18:39:50 +0000803 if (S_ISLNK(statbuf->st_mode)) {
Eric Andersen3adffb72000-06-26 10:54:06 +0000804 int link_size=0;
Erik Andersen5661fe02000-04-05 01:00:52 +0000805 char buffer[BUFSIZ];
806 header.typeflag = SYMTYPE;
Eric Andersen3adffb72000-06-26 10:54:06 +0000807 link_size = readlink(fileName, buffer, sizeof(buffer) - 1);
808 if ( link_size < 0) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000809 errorMsg("Error reading symlink '%s': %s\n", header.name, strerror(errno));
810 return ( FALSE);
811 }
Eric Andersen3adffb72000-06-26 10:54:06 +0000812 buffer[link_size] = '\0';
Erik Andersen95c1c1e2000-04-14 21:45:29 +0000813 strncpy(header.linkname, buffer, sizeof(header.linkname));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000814 } else if (S_ISDIR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000815 header.typeflag = DIRTYPE;
816 strncat(header.name, "/", sizeof(header.name));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000817 } else if (S_ISCHR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000818 header.typeflag = CHRTYPE;
819 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
820 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000821 } else if (S_ISBLK(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000822 header.typeflag = BLKTYPE;
823 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
824 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000825 } else if (S_ISFIFO(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000826 header.typeflag = FIFOTYPE;
827 } else if (S_ISREG(statbuf->st_mode)) {
828 header.typeflag = REGTYPE;
829 putOctal(header.size, sizeof(header.size), statbuf->st_size);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000830 } else {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000831 errorMsg("%s: Unknown file type\n", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000832 return ( FALSE);
833 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000834
Erik Andersen5661fe02000-04-05 01:00:52 +0000835 /* Calculate and store the checksum (i.e. the sum of all of the bytes of
836 * the header). The checksum field must be filled with blanks for the
837 * calculation. The checksum field is formatted differently from the
838 * other fields: it has [6] digits, a null, then a space -- rather than
839 * digits, followed by a null like the other fields... */
840 memset(header.chksum, ' ', sizeof(header.chksum));
841 cp = (const unsigned char *) &header;
842 while (size-- > 0)
843 chksum += *cp++;
844 putOctal(header.chksum, 7, chksum);
845
846 /* Now write the header out to disk */
847 if ((size=fullWrite(tbInfo->tarFd, (char*)&header, sizeof(struct TarHeader))) < 0) {
848 errorMsg(io_error, fileName, strerror(errno));
849 return ( FALSE);
850 }
851 /* Pad the header up to the tar block size */
852 for (; size<TAR_BLOCK_SIZE; size++) {
853 write(tbInfo->tarFd, "\0", 1);
854 }
855 /* Now do the verbose thing (or not) */
Eric Andersenfdd51032000-08-02 18:48:26 +0000856 if (tbInfo->verboseFlag==TRUE) {
857 FILE *vbFd = stdout;
858 if (tbInfo->tarFd == fileno(stdout)) // If the archive goes to stdout, verbose to stderr
859 vbFd = stderr;
860 fprintf(vbFd, "%s\n", header.name);
861 }
Erik Andersen3364d782000-03-28 00:58:14 +0000862
863 return ( TRUE);
864}
865
866
Erik Andersen68a9ea42000-04-04 18:39:50 +0000867static int writeFileToTarball(const char *fileName, struct stat *statbuf, void* userData)
Erik Andersen3364d782000-03-28 00:58:14 +0000868{
Erik Andersen68a9ea42000-04-04 18:39:50 +0000869 struct TarBallInfo *tbInfo = (struct TarBallInfo *)userData;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000870
871 /* It is against the rules to archive a socket */
872 if (S_ISSOCK(statbuf->st_mode)) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000873 errorMsg("%s: socket ignored\n", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000874 return( TRUE);
875 }
876
877 /* It is a bad idea to store the archive we are in the process of creating,
878 * so check the device and inode to be sure that this particular file isn't
879 * the new tarball */
880 if (tbInfo->statBuf.st_dev == statbuf->st_dev &&
881 tbInfo->statBuf.st_ino == statbuf->st_ino) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000882 errorMsg("%s: file is the archive; skipping\n", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000883 return( TRUE);
884 }
885
Erik Andersen5661fe02000-04-05 01:00:52 +0000886 if (writeTarHeader(tbInfo, fileName, statbuf)==FALSE) {
887 return( FALSE);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000888 }
Erik Andersen5661fe02000-04-05 01:00:52 +0000889
890 /* Now, if the file is a regular file, copy it out to the tarball */
891 if (S_ISREG(statbuf->st_mode)) {
892 int inputFileFd;
893 char buffer[BUFSIZ];
894 ssize_t size=0, readSize=0;
895
896 /* open the file we want to archive, and make sure all is well */
897 if ((inputFileFd = open(fileName, O_RDONLY)) < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000898 errorMsg("%s: Cannot open: %s\n", fileName, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +0000899 return( FALSE);
900 }
901
902 /* write the file to the archive */
903 while ( (size = fullRead(inputFileFd, buffer, sizeof(buffer))) > 0 ) {
904 if (fullWrite(tbInfo->tarFd, buffer, size) != size ) {
905 /* Output file seems to have a problem */
906 errorMsg(io_error, fileName, strerror(errno));
907 return( FALSE);
908 }
909 readSize+=size;
910 }
911 if (size == -1) {
912 errorMsg(io_error, fileName, strerror(errno));
913 return( FALSE);
914 }
915 /* Pad the file up to the tar block size */
916 for (; (readSize%TAR_BLOCK_SIZE) != 0; readSize++) {
917 write(tbInfo->tarFd, "\0", 1);
918 }
919 close( inputFileFd);
920 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000921
922 return( TRUE);
Erik Andersen6acaa402000-03-26 14:03:20 +0000923}
924
Erik Andersen68a9ea42000-04-04 18:39:50 +0000925static int writeTarFile(const char* tarName, int tostdoutFlag,
Erik Andersenecd51242000-04-08 03:08:21 +0000926 int verboseFlag, int argc, char **argv, char** excludeList)
Erik Andersen6acaa402000-03-26 14:03:20 +0000927{
Erik Andersen3364d782000-03-28 00:58:14 +0000928 int tarFd=-1;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000929 int errorFlag=FALSE;
Erik Andersen5661fe02000-04-05 01:00:52 +0000930 ssize_t size;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000931 struct TarBallInfo tbInfo;
932 tbInfo.verboseFlag = verboseFlag;
Erik Andersen3364d782000-03-28 00:58:14 +0000933
934 /* Make sure there is at least one file to tar up. */
935 if (argc <= 0)
Matt Kraaibe84cd42000-07-12 17:02:35 +0000936 fatalError("Cowardly refusing to create an empty archive\n");
Erik Andersen6acaa402000-03-26 14:03:20 +0000937
938 /* Open the tar file for writing. */
Erik Andersen3364d782000-03-28 00:58:14 +0000939 if (tostdoutFlag == TRUE)
Erik Andersen68a9ea42000-04-04 18:39:50 +0000940 tbInfo.tarFd = fileno(stdout);
Erik Andersen6acaa402000-03-26 14:03:20 +0000941 else
Erik Andersen68a9ea42000-04-04 18:39:50 +0000942 tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
943 if (tbInfo.tarFd < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000944 errorMsg( "Error opening '%s': %s\n", tarName, strerror(errno));
Erik Andersen6acaa402000-03-26 14:03:20 +0000945 return ( FALSE);
946 }
Erik Andersenecd51242000-04-08 03:08:21 +0000947 tbInfo.excludeList=excludeList;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000948 /* Store the stat info for the tarball's file, so
949 * can avoid including the tarball into itself.... */
950 if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0)
Erik Andersen3364d782000-03-28 00:58:14 +0000951 fatalError(io_error, tarName, strerror(errno));
Erik Andersen6acaa402000-03-26 14:03:20 +0000952
953 /* Set the umask for this process so it doesn't
954 * screw up permission setting for us later. */
955 umask(0);
956
957 /* Read the directory/files and iterate over them one at a time */
Erik Andersen3364d782000-03-28 00:58:14 +0000958 while (argc-- > 0) {
959 if (recursiveAction(*argv++, TRUE, FALSE, FALSE,
Erik Andersen68a9ea42000-04-04 18:39:50 +0000960 writeFileToTarball, writeFileToTarball,
961 (void*) &tbInfo) == FALSE) {
962 errorFlag = TRUE;
Erik Andersen3364d782000-03-28 00:58:14 +0000963 }
Erik Andersen6acaa402000-03-26 14:03:20 +0000964 }
Erik Andersen5661fe02000-04-05 01:00:52 +0000965 /* Write two empty blocks to the end of the archive */
966 for (size=0; size<(2*TAR_BLOCK_SIZE); size++) {
967 write(tbInfo.tarFd, "\0", 1);
968 }
Erik Andersen0817d132000-04-09 15:17:40 +0000969
970 /* To be pedantically correct, we would check if the tarball
971 * is smaller then 20 tar blocks, and pad it if it was smaller,
972 * but that isn't necessary for GNU tar interoperability, and
973 * so is considered a waste of space */
974
Erik Andersen68a9ea42000-04-04 18:39:50 +0000975 /* Hang up the tools, close up shop, head home */
Erik Andersen6acaa402000-03-26 14:03:20 +0000976 close(tarFd);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000977 if (errorFlag == TRUE) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000978 errorMsg("Error exit delayed from previous errors\n");
Erik Andersen68a9ea42000-04-04 18:39:50 +0000979 return(FALSE);
980 }
Erik Andersen6acaa402000-03-26 14:03:20 +0000981 return( TRUE);
982}
983
984
985#endif
986