blob: 723943245f2d99a0df9a1ce6f1aed2eff2937b68 [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. */
Matt Kraaib2f36af2000-09-15 22:47:34 +0000136static int writeTarFile(const char* tarName, int verboseFlag, int argc,
137 char **argv, char** excludeList);
Erik Andersen05100cd2000-01-16 01:30:52 +0000138#endif
139
Matt Kraai43c8c382000-09-04 16:51:55 +0000140static struct option longopts[] =
141{
142#ifdef BB_FEATURE_TAR_EXCLUDE
143 {"exclude",required_argument,NULL,'e'},
144#endif
145 {NULL,0,NULL,0}
146};
Eric Andersencc8ed391999-10-05 16:24:54 +0000147
Erik Andersene49d5ec2000-02-08 19:58:47 +0000148extern int tar_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000149{
Erik Andersenecd51242000-04-08 03:08:21 +0000150 char** excludeList=NULL;
Erik Andersen0817d132000-04-09 15:17:40 +0000151#if defined BB_FEATURE_TAR_EXCLUDE
Erik Andersenecd51242000-04-08 03:08:21 +0000152 int excludeListSize=0;
Erik Andersen0817d132000-04-09 15:17:40 +0000153#endif
Eric Andersen108a4432000-05-30 19:05:57 +0000154 const char *tarName="-";
Erik Andersen298854f2000-03-23 01:09:18 +0000155 int listFlag = FALSE;
156 int extractFlag = FALSE;
157 int createFlag = FALSE;
158 int verboseFlag = FALSE;
159 int tostdoutFlag = FALSE;
Eric Andersenfdd51032000-08-02 18:48:26 +0000160 int opt;
Eric Andersencc8ed391999-10-05 16:24:54 +0000161
Erik Andersenecd51242000-04-08 03:08:21 +0000162 if (argc <= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000163 usage(tar_usage);
Eric Andersencc8ed391999-10-05 16:24:54 +0000164
Eric Andersenfdd51032000-08-02 18:48:26 +0000165 /* do normal option parsing */
Matt Kraai43c8c382000-09-04 16:51:55 +0000166 while ((opt = getopt_long(argc, argv, "cxtvOf:", longopts, NULL)) != EOF) {
Eric Andersenfdd51032000-08-02 18:48:26 +0000167 switch (opt) {
Erik Andersenecd51242000-04-08 03:08:21 +0000168 case 'c':
169 if (extractFlag == TRUE || listFlag == TRUE)
170 goto flagError;
171 createFlag = TRUE;
172 break;
Eric Andersenfdd51032000-08-02 18:48:26 +0000173 case 'x':
174 if (listFlag == TRUE || createFlag == TRUE)
175 goto flagError;
176 extractFlag = TRUE;
177 break;
178 case 't':
179 if (extractFlag == TRUE || createFlag == TRUE)
180 goto flagError;
181 listFlag = TRUE;
182 break;
Erik Andersenecd51242000-04-08 03:08:21 +0000183 case 'v':
184 verboseFlag = TRUE;
185 break;
Erik Andersenecd51242000-04-08 03:08:21 +0000186 case 'O':
187 tostdoutFlag = TRUE;
Eric Andersenfdd51032000-08-02 18:48:26 +0000188 break;
189 case 'f':
190 if (*tarName != '-')
191 fatalError( "Only one 'f' option allowed\n");
192 tarName = optarg;
Erik Andersenecd51242000-04-08 03:08:21 +0000193 break;
Erik Andersen0817d132000-04-09 15:17:40 +0000194#if defined BB_FEATURE_TAR_EXCLUDE
Matt Kraai43c8c382000-09-04 16:51:55 +0000195 case 'e':
196 excludeList=xrealloc( excludeList, sizeof(char**) * (excludeListSize+2));
197 excludeList[excludeListSize] = optarg;
198 /* Remove leading "/"s */
199 if (*excludeList[excludeListSize] =='/')
200 excludeList[excludeListSize] = (excludeList[excludeListSize])+1;
201 /* Tack a NULL onto the end of the list */
202 excludeList[++excludeListSize] = NULL;
203 break;
Erik Andersen0817d132000-04-09 15:17:40 +0000204#endif
Erik Andersenecd51242000-04-08 03:08:21 +0000205 default:
Matt Kraai43c8c382000-09-04 16:51:55 +0000206 usage(tar_usage);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000207 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000208 }
209
Erik Andersene49d5ec2000-02-08 19:58:47 +0000210 /*
211 * Do the correct type of action supplying the rest of the
212 * command line arguments as the list of files to process.
213 */
214 if (createFlag == TRUE) {
Erik Andersende552872000-01-23 01:34:05 +0000215#ifndef BB_FEATURE_TAR_CREATE
Erik Andersen298854f2000-03-23 01:09:18 +0000216 fatalError( "This version of tar was not compiled with tar creation support.\n");
Erik Andersende552872000-01-23 01:34:05 +0000217#else
Matt Kraaib2f36af2000-09-15 22:47:34 +0000218 exit(writeTarFile(tarName, verboseFlag, argc-optind, &argv[optind], excludeList));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000219#endif
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000220 }
221 if (listFlag == TRUE || extractFlag == TRUE) {
Matt Kraaib92223b2000-09-04 08:25:42 +0000222 exit(readTarFile(tarName, extractFlag, listFlag, tostdoutFlag, verboseFlag, &argv[optind], excludeList));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000223 }
Erik Andersende552872000-01-23 01:34:05 +0000224
Erik Andersene49d5ec2000-02-08 19:58:47 +0000225 flagError:
Erik Andersen298854f2000-03-23 01:09:18 +0000226 fatalError( "Exactly one of 'c', 'x' or 't' must be specified\n");
227}
228
229static void
Erik Andersen6a34b532000-04-07 06:55:38 +0000230fixUpPermissions(TarInfo *header)
231{
232 struct utimbuf t;
233 /* Now set permissions etc for the new file */
234 chown(header->name, header->uid, header->gid);
235 chmod(header->name, header->mode);
236 /* Reset the time */
237 t.actime = time(0);
238 t.modtime = header->mtime;
239 utime(header->name, &t);
240}
241
242static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000243tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersen298854f2000-03-23 01:09:18 +0000244{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000245 size_t writeSize;
246 size_t readSize;
247 size_t actualWriteSz;
248 char buffer[BUFSIZ];
249 size_t size = header->size;
250 int outFd=fileno(stdout);
251
252 /* Open the file to be written, if a file is supposed to be written */
253 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000254 /* Create the path to the file, just in case it isn't there...
255 * This should not screw up path permissions or anything. */
256 createPath(header->name, 0777);
Eric Andersen0c6a9702000-06-09 20:51:50 +0000257 if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY,
258 header->mode & ~S_IFMT)) < 0) {
259 errorMsg(io_error, header->name, strerror(errno));
260 return( FALSE);
261 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000262 }
263
264 /* Write out the file, if we are supposed to be doing that */
265 while ( size > 0 ) {
266 actualWriteSz=0;
267 if ( size > sizeof(buffer) )
268 writeSize = readSize = sizeof(buffer);
269 else {
270 int mod = size % 512;
271 if ( mod != 0 )
272 readSize = size + (512 - mod);
273 else
274 readSize = size;
275 writeSize = size;
276 }
277 if ( (readSize = fullRead(header->tarFd, buffer, readSize)) <= 0 ) {
278 /* Tarball seems to have a problem */
Matt Kraaibe84cd42000-07-12 17:02:35 +0000279 errorMsg("Unexpected EOF in archive\n");
Erik Andersen6a34b532000-04-07 06:55:38 +0000280 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000281 }
282 if ( readSize < writeSize )
283 writeSize = readSize;
284
285 /* Write out the file, if we are supposed to be doing that */
286 if (extractFlag==TRUE) {
287
288 if ((actualWriteSz=fullWrite(outFd, buffer, writeSize)) != writeSize ) {
289 /* Output file seems to have a problem */
290 errorMsg(io_error, header->name, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000291 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000292 }
Erik Andersen0817d132000-04-09 15:17:40 +0000293 } else {
294 actualWriteSz=writeSize;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000295 }
296
297 size -= actualWriteSz;
298 }
299
300 /* Now we are done writing the file out, so try
301 * and fix up the permissions and whatnot */
302 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000303 close(outFd);
Erik Andersen6a34b532000-04-07 06:55:38 +0000304 fixUpPermissions(header);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000305 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000306 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000307}
308
Erik Andersen6a34b532000-04-07 06:55:38 +0000309static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000310tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000311{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000312
313 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000314 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000315
316 if (createPath(header->name, header->mode) != TRUE) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000317 errorMsg("%s: Cannot mkdir: %s\n",
Erik Andersen6a34b532000-04-07 06:55:38 +0000318 header->name, strerror(errno));
319 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000320 }
321 /* make the final component, just in case it was
322 * omitted by createPath() (which will skip the
323 * directory if it doesn't have a terminating '/') */
Erik Andersen6acaa402000-03-26 14:03:20 +0000324 if (mkdir(header->name, header->mode) == 0) {
325 fixUpPermissions(header);
326 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000327 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000328}
329
Erik Andersen6a34b532000-04-07 06:55:38 +0000330static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000331tarExtractHardLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000332{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000333 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000334 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000335
336 if (link(header->linkname, header->name) < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000337 errorMsg("%s: Cannot create hard link to '%s': %s\n",
Erik Andersen5661fe02000-04-05 01:00:52 +0000338 header->name, header->linkname, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000339 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000340 }
341
342 /* Now set permissions etc for the new directory */
343 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000344 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000345}
346
Erik Andersen6a34b532000-04-07 06:55:38 +0000347static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000348tarExtractSymLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000349{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000350 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000351 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000352
353#ifdef S_ISLNK
354 if (symlink(header->linkname, header->name) < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000355 errorMsg("%s: Cannot create symlink to '%s': %s\n",
Erik Andersen5661fe02000-04-05 01:00:52 +0000356 header->name, header->linkname, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000357 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000358 }
359 /* Try to change ownership of the symlink.
360 * If libs doesn't support that, don't bother.
361 * Changing the pointed-to-file is the Wrong Thing(tm).
362 */
363#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
364 lchown(header->name, header->uid, header->gid);
365#endif
366
367 /* Do not change permissions or date on symlink,
368 * since it changes the pointed to file instead. duh. */
369#else
Matt Kraaibe84cd42000-07-12 17:02:35 +0000370 errorMsg("%s: Cannot create symlink to '%s': %s\n",
Erik Andersen6a34b532000-04-07 06:55:38 +0000371 header->name, header->linkname,
372 "symlinks not supported");
Erik Andersen1ad302a2000-03-24 00:54:46 +0000373#endif
Erik Andersen6a34b532000-04-07 06:55:38 +0000374 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000375}
376
Erik Andersen6a34b532000-04-07 06:55:38 +0000377static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000378tarExtractSpecial(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000379{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000380 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000381 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000382
383 if (S_ISCHR(header->mode) || S_ISBLK(header->mode) || S_ISSOCK(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000384 if (mknod(header->name, header->mode, makedev(header->devmajor, header->devminor)) < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000385 errorMsg("%s: Cannot mknod: %s\n",
Erik Andersen6a34b532000-04-07 06:55:38 +0000386 header->name, strerror(errno));
387 return( FALSE);
388 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000389 } else if (S_ISFIFO(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000390 if (mkfifo(header->name, header->mode) < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000391 errorMsg("%s: Cannot mkfifo: %s\n",
Erik Andersen6a34b532000-04-07 06:55:38 +0000392 header->name, strerror(errno));
393 return( FALSE);
394 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000395 }
396
397 /* Now set permissions etc for the new directory */
398 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000399 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000400}
401
Erik Andersen1ad302a2000-03-24 00:54:46 +0000402/* Read an octal value in a field of the specified width, with optional
Erik Andersen298854f2000-03-23 01:09:18 +0000403 * spaces on both sides of the number and with an optional null character
Erik Andersen1ad302a2000-03-24 00:54:46 +0000404 * at the end. Returns -1 on an illegal format. */
Erik Andersen298854f2000-03-23 01:09:18 +0000405static long getOctal(const char *cp, int size)
Eric Andersencc8ed391999-10-05 16:24:54 +0000406{
Erik Andersen298854f2000-03-23 01:09:18 +0000407 long val = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000408
Erik Andersen298854f2000-03-23 01:09:18 +0000409 for(;(size > 0) && (*cp == ' '); cp++, size--);
410 if ((size == 0) || !isOctal(*cp))
411 return -1;
412 for(; (size > 0) && isOctal(*cp); size--) {
413 val = val * 8 + *cp++ - '0';
Eric Andersencc8ed391999-10-05 16:24:54 +0000414 }
Erik Andersen298854f2000-03-23 01:09:18 +0000415 for (;(size > 0) && (*cp == ' '); cp++, size--);
416 if ((size > 0) && *cp)
417 return -1;
418 return val;
419}
Eric Andersencc8ed391999-10-05 16:24:54 +0000420
Erik Andersen1ad302a2000-03-24 00:54:46 +0000421
Erik Andersen298854f2000-03-23 01:09:18 +0000422/* Parse the tar header and fill in the nice struct with the details */
423static int
Erik Andersen3364d782000-03-28 00:58:14 +0000424readTarHeader(struct TarHeader *rawHeader, struct TarInfo *header)
Erik Andersen298854f2000-03-23 01:09:18 +0000425{
Erik Andersene454fb62000-03-23 04:27:58 +0000426 int i;
Erik Andersen84e09e42000-04-08 20:58:35 +0000427 long chksum, sum=0;
Erik Andersene454fb62000-03-23 04:27:58 +0000428 unsigned char *s = (unsigned char *)rawHeader;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000429
Erik Andersen298854f2000-03-23 01:09:18 +0000430 header->name = rawHeader->name;
Erik Andersen84e09e42000-04-08 20:58:35 +0000431 /* Check for and relativify any absolute paths */
432 if ( *(header->name) == '/' ) {
433 static int alreadyWarned=FALSE;
434
435 while (*(header->name) == '/')
436 ++*(header->name);
437
438 if (alreadyWarned == FALSE) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000439 errorMsg("Removing leading '/' from member names\n");
Erik Andersen84e09e42000-04-08 20:58:35 +0000440 alreadyWarned = TRUE;
441 }
442 }
443
Erik Andersen298854f2000-03-23 01:09:18 +0000444 header->mode = getOctal(rawHeader->mode, sizeof(rawHeader->mode));
445 header->uid = getOctal(rawHeader->uid, sizeof(rawHeader->uid));
446 header->gid = getOctal(rawHeader->gid, sizeof(rawHeader->gid));
447 header->size = getOctal(rawHeader->size, sizeof(rawHeader->size));
448 header->mtime = getOctal(rawHeader->mtime, sizeof(rawHeader->mtime));
449 chksum = getOctal(rawHeader->chksum, sizeof(rawHeader->chksum));
450 header->type = rawHeader->typeflag;
451 header->linkname = rawHeader->linkname;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000452 header->devmajor = getOctal(rawHeader->devmajor, sizeof(rawHeader->devmajor));
453 header->devminor = getOctal(rawHeader->devminor, sizeof(rawHeader->devminor));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000454
Erik Andersen298854f2000-03-23 01:09:18 +0000455 /* Check the checksum */
Erik Andersen84e09e42000-04-08 20:58:35 +0000456 for (i = sizeof(*rawHeader); i-- != 0;) {
Erik Andersen298854f2000-03-23 01:09:18 +0000457 sum += *s++;
Erik Andersen84e09e42000-04-08 20:58:35 +0000458 }
459 /* Remove the effects of the checksum field (replace
460 * with blanks for the purposes of the checksum) */
461 s = rawHeader->chksum;
462 for (i = sizeof(rawHeader->chksum) ; i-- != 0;) {
463 sum -= *s++;
464 }
465 sum += ' ' * sizeof(rawHeader->chksum);
Erik Andersene454fb62000-03-23 04:27:58 +0000466 if (sum == chksum )
Erik Andersen298854f2000-03-23 01:09:18 +0000467 return ( TRUE);
468 return( FALSE);
469}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000470
Erik Andersene49d5ec2000-02-08 19:58:47 +0000471
Erik Andersen1ad302a2000-03-24 00:54:46 +0000472/*
473 * Read a tar file and extract or list the specified files within it.
474 * If the list is empty than all files are extracted or listed.
475 */
476static int readTarFile(const char* tarName, int extractFlag, int listFlag,
Matt Kraaib92223b2000-09-04 08:25:42 +0000477 int tostdoutFlag, int verboseFlag, char** extractList,
478 char** excludeList)
Erik Andersen1ad302a2000-03-24 00:54:46 +0000479{
Erik Andersen0817d132000-04-09 15:17:40 +0000480 int status, tarFd=-1;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000481 int errorFlag=FALSE;
482 TarHeader rawHeader;
483 TarInfo header;
Erik Andersen0817d132000-04-09 15:17:40 +0000484 char** tmpList;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000485
Erik Andersen1ad302a2000-03-24 00:54:46 +0000486 /* Open the tar file for reading. */
487 if (!strcmp(tarName, "-"))
488 tarFd = fileno(stdin);
489 else
490 tarFd = open(tarName, O_RDONLY);
491 if (tarFd < 0) {
Erik Andersendeea0482000-03-25 23:09:29 +0000492 errorMsg( "Error opening '%s': %s\n", tarName, strerror(errno));
Erik Andersen1ad302a2000-03-24 00:54:46 +0000493 return ( FALSE);
Erik Andersen06936df2000-01-23 02:14:20 +0000494 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000495
Erik Andersene49d5ec2000-02-08 19:58:47 +0000496 /* Set the umask for this process so it doesn't
Erik Andersen1ad302a2000-03-24 00:54:46 +0000497 * screw up permission setting for us later. */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000498 umask(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000499
Erik Andersen1ad302a2000-03-24 00:54:46 +0000500 /* Read the tar file, and iterate over it one file at a time */
501 while ( (status = fullRead(tarFd, (char*)&rawHeader, TAR_BLOCK_SIZE)) == TAR_BLOCK_SIZE ) {
Erik Andersene2729152000-02-18 21:34:17 +0000502
Erik Andersen1ad302a2000-03-24 00:54:46 +0000503 /* First, try to read the header */
Erik Andersen3364d782000-03-28 00:58:14 +0000504 if ( readTarHeader(&rawHeader, &header) == FALSE ) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000505 if ( *(header.name) == '\0' ) {
506 goto endgame;
507 } else {
508 errorFlag=TRUE;
509 errorMsg("Bad tar header, skipping\n");
510 continue;
511 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000512 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000513 if ( *(header.name) == '\0' )
514 goto endgame;
Erik Andersen0817d132000-04-09 15:17:40 +0000515 header.tarFd = tarFd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000516
Erik Andersen0817d132000-04-09 15:17:40 +0000517#if defined BB_FEATURE_TAR_EXCLUDE
518 {
519 int skipFlag=FALSE;
520 /* Check for excluded files.... */
521 for (tmpList=excludeList; tmpList && *tmpList; tmpList++) {
522 /* Do some extra hoop jumping for when directory names
523 * end in '/' but the entry in tmpList doesn't */
524 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
525 header.name[strlen(header.name)-1]=='/'
526 && strncmp( *tmpList, header.name,
527 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
528 /* If it is a regular file, pretend to extract it with
529 * the extractFlag set to FALSE, so the junk in the tarball
530 * is properly skipped over */
531 if ( header.type==REGTYPE || header.type==REGTYPE0 ) {
532 tarExtractRegularFile(&header, FALSE, FALSE);
533 }
534 skipFlag=TRUE;
535 break;
536 }
537 }
538 /* There are not the droids you're looking for, move along */
539 if (skipFlag==TRUE)
540 continue;
541 }
542#endif
Matt Kraaib92223b2000-09-04 08:25:42 +0000543 if (*extractList != NULL) {
544 int skipFlag = TRUE;
545 for (tmpList = extractList; *tmpList != NULL; tmpList++) {
546 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
547 header.name[strlen(header.name)-1]=='/'
548 && strncmp( *tmpList, header.name,
549 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
550 /* If it is a regular file, pretend to extract it with
551 * the extractFlag set to FALSE, so the junk in the tarball
552 * is properly skipped over */
553 skipFlag = FALSE;
554 break;
555 }
556 }
557 /* There are not the droids you're looking for, move along */
558 if (skipFlag == TRUE) {
559 if ( header.type==REGTYPE || header.type==REGTYPE0 )
560 tarExtractRegularFile(&header, FALSE, FALSE);
561 continue;
562 }
563 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000564
Matt Kraai82cfbad2000-09-15 21:18:43 +0000565 if (listFlag == TRUE) {
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000566 /* Special treatment if the list (-t) flag is on */
567 if (verboseFlag == TRUE) {
568 int len, len1;
569 char buf[35];
570 struct tm *tm = localtime (&(header.mtime));
571
572 len=printf("%s ", modeString(header.mode));
573 memset(buf, 0, 8*sizeof(char));
574 my_getpwuid(buf, header.uid);
575 if (! *buf)
576 len+=printf("%d", header.uid);
577 else
578 len+=printf("%s", buf);
579 memset(buf, 0, 8*sizeof(char));
580 my_getgrgid(buf, header.gid);
581 if (! *buf)
582 len+=printf("/%-d ", header.gid);
583 else
584 len+=printf("/%-s ", buf);
585
586 if (header.type==CHRTYPE || header.type==BLKTYPE) {
587 len1=snprintf(buf, sizeof(buf), "%ld,%-ld ",
588 header.devmajor, header.devminor);
589 } else {
590 len1=snprintf(buf, sizeof(buf), "%lu ", (long)header.size);
591 }
592 /* Jump through some hoops to make the columns match up */
593 for(;(len+len1)<31;len++)
594 printf(" ");
595 printf(buf);
596
597 /* Use ISO 8610 time format */
598 if (tm) {
599 printf ("%04d-%02d-%02d %02d:%02d:%02d ",
600 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
601 tm->tm_hour, tm->tm_min, tm->tm_sec);
602 }
603 }
Eric Andersenfdd51032000-08-02 18:48:26 +0000604 printf("%s", header.name);
Matt Kraai82cfbad2000-09-15 21:18:43 +0000605 if (verboseFlag == TRUE) {
606 if (header.type==LNKTYPE) /* If this is a link, say so */
607 printf(" link to %s", header.linkname);
608 else if (header.type==SYMTYPE)
609 printf(" -> %s", header.linkname);
610 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000611 printf("\n");
612 }
613
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000614 /* List contents if we are supposed to do that */
615 if (verboseFlag == TRUE && extractFlag == TRUE) {
616 /* Now the normal listing */
617 FILE *vbFd = stdout;
618 if (tostdoutFlag == TRUE) // If the archive goes to stdout, verbose to stderr
619 vbFd = stderr;
620 fprintf(vbFd, "%s\n", header.name);
621 }
622
Matt Kraaif4462972000-09-01 02:50:48 +0000623 /* Remove files if we would overwrite them */
Matt Kraaida542f32000-09-01 02:53:01 +0000624 if (extractFlag == TRUE && tostdoutFlag == FALSE)
Matt Kraaif4462972000-09-01 02:50:48 +0000625 unlink(header.name);
Eric Andersencc8ed391999-10-05 16:24:54 +0000626
Erik Andersen1ad302a2000-03-24 00:54:46 +0000627 /* If we got here, we can be certain we have a legitimate
628 * header to work with. So work with it. */
629 switch ( header.type ) {
630 case REGTYPE:
631 case REGTYPE0:
632 /* If the name ends in a '/' then assume it is
633 * supposed to be a directory, and fall through */
634 if (header.name[strlen(header.name)-1] != '/') {
Erik Andersen6a34b532000-04-07 06:55:38 +0000635 if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE)
636 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000637 break;
638 }
639 case DIRTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000640 if (tarExtractDirectory( &header, extractFlag, tostdoutFlag)==FALSE)
641 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000642 break;
643 case LNKTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000644 if (tarExtractHardLink( &header, extractFlag, tostdoutFlag)==FALSE)
645 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000646 break;
647 case SYMTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000648 if (tarExtractSymLink( &header, extractFlag, tostdoutFlag)==FALSE)
649 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000650 break;
651 case CHRTYPE:
652 case BLKTYPE:
653 case FIFOTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000654 if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE)
655 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000656 break;
657 default:
658 close( tarFd);
659 return( FALSE);
660 }
661 }
662 close(tarFd);
663 if (status > 0) {
664 /* Bummer - we read a partial header */
Erik Andersendeea0482000-03-25 23:09:29 +0000665 errorMsg( "Error reading '%s': %s\n", tarName, strerror(errno));
Erik Andersen1ad302a2000-03-24 00:54:46 +0000666 return ( FALSE);
667 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000668 else if (errorFlag==TRUE) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000669 errorMsg( "Error exit delayed from previous errors\n");
Erik Andersen6a34b532000-04-07 06:55:38 +0000670 return( FALSE);
671 } else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000672 return( status);
673
674 /* Stuff to do when we are done */
675endgame:
676 close( tarFd);
677 if ( *(header.name) == '\0' ) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000678 if (errorFlag==TRUE)
Matt Kraaibe84cd42000-07-12 17:02:35 +0000679 errorMsg( "Error exit delayed from previous errors\n");
Erik Andersen6a34b532000-04-07 06:55:38 +0000680 else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000681 return( TRUE);
682 }
683 return( FALSE);
684}
685
Erik Andersen6acaa402000-03-26 14:03:20 +0000686
687#ifdef BB_FEATURE_TAR_CREATE
688
Erik Andersen68a9ea42000-04-04 18:39:50 +0000689/* Some info to be carried along when creating a new tarball */
690struct TarBallInfo
691{
692 char* fileName; /* File name of the tarball */
693 int tarFd; /* Open-for-write file descriptor
694 for the tarball */
695 struct stat statBuf; /* Stat info for the tarball, letting
696 us know the inode and device that the
697 tarball lives, so we can avoid trying
698 to include the tarball into itself */
699 int verboseFlag; /* Whether to print extra stuff or not */
Erik Andersenecd51242000-04-08 03:08:21 +0000700 char** excludeList; /* List of files to not include */
Erik Andersen68a9ea42000-04-04 18:39:50 +0000701};
702typedef struct TarBallInfo TarBallInfo;
703
704
Erik Andersen6acaa402000-03-26 14:03:20 +0000705/* Put an octal string into the specified buffer.
706 * The number is zero and space padded and possibly null padded.
707 * Returns TRUE if successful. */
708static int putOctal (char *cp, int len, long value)
709{
710 int tempLength;
Erik Andersen6acaa402000-03-26 14:03:20 +0000711 char tempBuffer[32];
Erik Andersen5661fe02000-04-05 01:00:52 +0000712 char *tempString = tempBuffer;
Erik Andersen6acaa402000-03-26 14:03:20 +0000713
714 /* Create a string of the specified length with an initial space,
715 * leading zeroes and the octal number, and a trailing null. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000716 sprintf (tempString, "%0*lo", len - 1, value);
Erik Andersen6acaa402000-03-26 14:03:20 +0000717
718 /* If the string is too large, suppress the leading space. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000719 tempLength = strlen (tempString) + 1;
Erik Andersen6acaa402000-03-26 14:03:20 +0000720 if (tempLength > len) {
721 tempLength--;
722 tempString++;
723 }
724
725 /* If the string is still too large, suppress the trailing null. */
726 if (tempLength > len)
727 tempLength--;
728
729 /* If the string is still too large, fail. */
730 if (tempLength > len)
731 return FALSE;
732
733 /* Copy the string to the field. */
734 memcpy (cp, tempString, len);
735
736 return TRUE;
737}
738
Erik Andersen68a9ea42000-04-04 18:39:50 +0000739/* Write out a tar header for the specified file/directory/whatever */
Erik Andersen3364d782000-03-28 00:58:14 +0000740static int
Erik Andersen5661fe02000-04-05 01:00:52 +0000741writeTarHeader(struct TarBallInfo *tbInfo, const char *fileName, struct stat *statbuf)
Erik Andersen6acaa402000-03-26 14:03:20 +0000742{
Erik Andersen5661fe02000-04-05 01:00:52 +0000743 long chksum=0;
744 struct TarHeader header;
Erik Andersen0817d132000-04-09 15:17:40 +0000745#if defined BB_FEATURE_TAR_EXCLUDE
Erik Andersenecd51242000-04-08 03:08:21 +0000746 char** tmpList;
Erik Andersen0817d132000-04-09 15:17:40 +0000747#endif
Erik Andersen5661fe02000-04-05 01:00:52 +0000748 const unsigned char *cp = (const unsigned char *) &header;
749 ssize_t size = sizeof(struct TarHeader);
Eric Andersenfdd51032000-08-02 18:48:26 +0000750
Erik Andersen5661fe02000-04-05 01:00:52 +0000751 memset( &header, 0, size);
Erik Andersen3364d782000-03-28 00:58:14 +0000752
Erik Andersen68a9ea42000-04-04 18:39:50 +0000753 if (*fileName=='/') {
754 static int alreadyWarned=FALSE;
755 if (alreadyWarned==FALSE) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000756 errorMsg("Removing leading '/' from member names\n");
Erik Andersen68a9ea42000-04-04 18:39:50 +0000757 alreadyWarned=TRUE;
758 }
Erik Andersen84e09e42000-04-08 20:58:35 +0000759 strncpy(header.name, fileName+1, sizeof(header.name));
Erik Andersen3364d782000-03-28 00:58:14 +0000760 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000761 else {
Erik Andersen84e09e42000-04-08 20:58:35 +0000762 strncpy(header.name, fileName, sizeof(header.name));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000763 }
Erik Andersen84e09e42000-04-08 20:58:35 +0000764
Erik Andersen0817d132000-04-09 15:17:40 +0000765#if defined BB_FEATURE_TAR_EXCLUDE
766 /* Check for excluded files.... */
Erik Andersenecd51242000-04-08 03:08:21 +0000767 for (tmpList=tbInfo->excludeList; tmpList && *tmpList; tmpList++) {
Erik Andersen0817d132000-04-09 15:17:40 +0000768 /* Do some extra hoop jumping for when directory names
769 * end in '/' but the entry in tmpList doesn't */
770 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
771 header.name[strlen(header.name)-1]=='/'
772 && strncmp( *tmpList, header.name,
773 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
774 /* Set the mode to something that is not a regular file, thereby
775 * faking out writeTarFile into thinking that nothing further need
776 * be done for this file. Yes, I know this is ugly, but it works. */
777 statbuf->st_mode = 0;
778 return( TRUE);
779 }
Erik Andersenecd51242000-04-08 03:08:21 +0000780 }
Erik Andersen0817d132000-04-09 15:17:40 +0000781#endif
Erik Andersenecd51242000-04-08 03:08:21 +0000782
Erik Andersen5661fe02000-04-05 01:00:52 +0000783 putOctal(header.mode, sizeof(header.mode), statbuf->st_mode);
784 putOctal(header.uid, sizeof(header.uid), statbuf->st_uid);
785 putOctal(header.gid, sizeof(header.gid), statbuf->st_gid);
786 putOctal(header.size, sizeof(header.size), 0); /* Regular file size is handled later */
787 putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime);
788 strncpy(header.magic, TAR_MAGIC TAR_VERSION,
789 TAR_MAGIC_LEN + TAR_VERSION_LEN );
Erik Andersen68a9ea42000-04-04 18:39:50 +0000790
Erik Andersen84e09e42000-04-08 20:58:35 +0000791 /* Enter the user and group names (default to root if it fails) */
Erik Andersen5661fe02000-04-05 01:00:52 +0000792 my_getpwuid(header.uname, statbuf->st_uid);
Erik Andersen5661fe02000-04-05 01:00:52 +0000793 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000794 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +0000795 my_getgrgid(header.gname, statbuf->st_gid);
796 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000797 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +0000798
Erik Andersen95c1c1e2000-04-14 21:45:29 +0000799 /* WARNING/NOTICE: I break Hard Links */
Erik Andersen68a9ea42000-04-04 18:39:50 +0000800 if (S_ISLNK(statbuf->st_mode)) {
Eric Andersen3adffb72000-06-26 10:54:06 +0000801 int link_size=0;
Erik Andersen5661fe02000-04-05 01:00:52 +0000802 char buffer[BUFSIZ];
803 header.typeflag = SYMTYPE;
Eric Andersen3adffb72000-06-26 10:54:06 +0000804 link_size = readlink(fileName, buffer, sizeof(buffer) - 1);
805 if ( link_size < 0) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000806 errorMsg("Error reading symlink '%s': %s\n", header.name, strerror(errno));
807 return ( FALSE);
808 }
Eric Andersen3adffb72000-06-26 10:54:06 +0000809 buffer[link_size] = '\0';
Erik Andersen95c1c1e2000-04-14 21:45:29 +0000810 strncpy(header.linkname, buffer, sizeof(header.linkname));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000811 } else if (S_ISDIR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000812 header.typeflag = DIRTYPE;
813 strncat(header.name, "/", sizeof(header.name));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000814 } else if (S_ISCHR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000815 header.typeflag = CHRTYPE;
816 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
817 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000818 } else if (S_ISBLK(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000819 header.typeflag = BLKTYPE;
820 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
821 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000822 } else if (S_ISFIFO(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000823 header.typeflag = FIFOTYPE;
824 } else if (S_ISREG(statbuf->st_mode)) {
825 header.typeflag = REGTYPE;
826 putOctal(header.size, sizeof(header.size), statbuf->st_size);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000827 } else {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000828 errorMsg("%s: Unknown file type\n", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000829 return ( FALSE);
830 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000831
Erik Andersen5661fe02000-04-05 01:00:52 +0000832 /* Calculate and store the checksum (i.e. the sum of all of the bytes of
833 * the header). The checksum field must be filled with blanks for the
834 * calculation. The checksum field is formatted differently from the
835 * other fields: it has [6] digits, a null, then a space -- rather than
836 * digits, followed by a null like the other fields... */
837 memset(header.chksum, ' ', sizeof(header.chksum));
838 cp = (const unsigned char *) &header;
839 while (size-- > 0)
840 chksum += *cp++;
841 putOctal(header.chksum, 7, chksum);
842
843 /* Now write the header out to disk */
844 if ((size=fullWrite(tbInfo->tarFd, (char*)&header, sizeof(struct TarHeader))) < 0) {
845 errorMsg(io_error, fileName, strerror(errno));
846 return ( FALSE);
847 }
848 /* Pad the header up to the tar block size */
849 for (; size<TAR_BLOCK_SIZE; size++) {
850 write(tbInfo->tarFd, "\0", 1);
851 }
852 /* Now do the verbose thing (or not) */
Eric Andersenfdd51032000-08-02 18:48:26 +0000853 if (tbInfo->verboseFlag==TRUE) {
854 FILE *vbFd = stdout;
855 if (tbInfo->tarFd == fileno(stdout)) // If the archive goes to stdout, verbose to stderr
856 vbFd = stderr;
857 fprintf(vbFd, "%s\n", header.name);
858 }
Erik Andersen3364d782000-03-28 00:58:14 +0000859
860 return ( TRUE);
861}
862
863
Erik Andersen68a9ea42000-04-04 18:39:50 +0000864static int writeFileToTarball(const char *fileName, struct stat *statbuf, void* userData)
Erik Andersen3364d782000-03-28 00:58:14 +0000865{
Erik Andersen68a9ea42000-04-04 18:39:50 +0000866 struct TarBallInfo *tbInfo = (struct TarBallInfo *)userData;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000867
868 /* It is against the rules to archive a socket */
869 if (S_ISSOCK(statbuf->st_mode)) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000870 errorMsg("%s: socket ignored\n", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000871 return( TRUE);
872 }
873
874 /* It is a bad idea to store the archive we are in the process of creating,
875 * so check the device and inode to be sure that this particular file isn't
876 * the new tarball */
877 if (tbInfo->statBuf.st_dev == statbuf->st_dev &&
878 tbInfo->statBuf.st_ino == statbuf->st_ino) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000879 errorMsg("%s: file is the archive; skipping\n", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000880 return( TRUE);
881 }
882
Erik Andersen5661fe02000-04-05 01:00:52 +0000883 if (writeTarHeader(tbInfo, fileName, statbuf)==FALSE) {
884 return( FALSE);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000885 }
Erik Andersen5661fe02000-04-05 01:00:52 +0000886
887 /* Now, if the file is a regular file, copy it out to the tarball */
888 if (S_ISREG(statbuf->st_mode)) {
889 int inputFileFd;
890 char buffer[BUFSIZ];
891 ssize_t size=0, readSize=0;
892
893 /* open the file we want to archive, and make sure all is well */
894 if ((inputFileFd = open(fileName, O_RDONLY)) < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000895 errorMsg("%s: Cannot open: %s\n", fileName, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +0000896 return( FALSE);
897 }
898
899 /* write the file to the archive */
900 while ( (size = fullRead(inputFileFd, buffer, sizeof(buffer))) > 0 ) {
901 if (fullWrite(tbInfo->tarFd, buffer, size) != size ) {
902 /* Output file seems to have a problem */
903 errorMsg(io_error, fileName, strerror(errno));
904 return( FALSE);
905 }
906 readSize+=size;
907 }
908 if (size == -1) {
909 errorMsg(io_error, fileName, strerror(errno));
910 return( FALSE);
911 }
912 /* Pad the file up to the tar block size */
913 for (; (readSize%TAR_BLOCK_SIZE) != 0; readSize++) {
914 write(tbInfo->tarFd, "\0", 1);
915 }
916 close( inputFileFd);
917 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000918
919 return( TRUE);
Erik Andersen6acaa402000-03-26 14:03:20 +0000920}
921
Matt Kraaib2f36af2000-09-15 22:47:34 +0000922static int writeTarFile(const char* tarName, int verboseFlag, int argc,
923 char **argv, char** excludeList)
Erik Andersen6acaa402000-03-26 14:03:20 +0000924{
Erik Andersen3364d782000-03-28 00:58:14 +0000925 int tarFd=-1;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000926 int errorFlag=FALSE;
Erik Andersen5661fe02000-04-05 01:00:52 +0000927 ssize_t size;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000928 struct TarBallInfo tbInfo;
929 tbInfo.verboseFlag = verboseFlag;
Erik Andersen3364d782000-03-28 00:58:14 +0000930
931 /* Make sure there is at least one file to tar up. */
932 if (argc <= 0)
Matt Kraaibe84cd42000-07-12 17:02:35 +0000933 fatalError("Cowardly refusing to create an empty archive\n");
Erik Andersen6acaa402000-03-26 14:03:20 +0000934
935 /* Open the tar file for writing. */
Matt Kraaib2f36af2000-09-15 22:47:34 +0000936 if (strcmp(tarName, "-") == 0)
Erik Andersen68a9ea42000-04-04 18:39:50 +0000937 tbInfo.tarFd = fileno(stdout);
Erik Andersen6acaa402000-03-26 14:03:20 +0000938 else
Erik Andersen68a9ea42000-04-04 18:39:50 +0000939 tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
940 if (tbInfo.tarFd < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000941 errorMsg( "Error opening '%s': %s\n", tarName, strerror(errno));
Erik Andersen6acaa402000-03-26 14:03:20 +0000942 return ( FALSE);
943 }
Erik Andersenecd51242000-04-08 03:08:21 +0000944 tbInfo.excludeList=excludeList;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000945 /* Store the stat info for the tarball's file, so
946 * can avoid including the tarball into itself.... */
947 if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0)
Erik Andersen3364d782000-03-28 00:58:14 +0000948 fatalError(io_error, tarName, strerror(errno));
Erik Andersen6acaa402000-03-26 14:03:20 +0000949
950 /* Set the umask for this process so it doesn't
951 * screw up permission setting for us later. */
952 umask(0);
953
954 /* Read the directory/files and iterate over them one at a time */
Erik Andersen3364d782000-03-28 00:58:14 +0000955 while (argc-- > 0) {
956 if (recursiveAction(*argv++, TRUE, FALSE, FALSE,
Erik Andersen68a9ea42000-04-04 18:39:50 +0000957 writeFileToTarball, writeFileToTarball,
958 (void*) &tbInfo) == FALSE) {
959 errorFlag = TRUE;
Erik Andersen3364d782000-03-28 00:58:14 +0000960 }
Erik Andersen6acaa402000-03-26 14:03:20 +0000961 }
Erik Andersen5661fe02000-04-05 01:00:52 +0000962 /* Write two empty blocks to the end of the archive */
963 for (size=0; size<(2*TAR_BLOCK_SIZE); size++) {
964 write(tbInfo.tarFd, "\0", 1);
965 }
Erik Andersen0817d132000-04-09 15:17:40 +0000966
967 /* To be pedantically correct, we would check if the tarball
968 * is smaller then 20 tar blocks, and pad it if it was smaller,
969 * but that isn't necessary for GNU tar interoperability, and
970 * so is considered a waste of space */
971
Erik Andersen68a9ea42000-04-04 18:39:50 +0000972 /* Hang up the tools, close up shop, head home */
Erik Andersen6acaa402000-03-26 14:03:20 +0000973 close(tarFd);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000974 if (errorFlag == TRUE) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000975 errorMsg("Error exit delayed from previous errors\n");
Erik Andersen68a9ea42000-04-04 18:39:50 +0000976 return(FALSE);
977 }
Erik Andersen6acaa402000-03-26 14:03:20 +0000978 return( TRUE);
979}
980
981
982#endif
983