blob: 86f996576d8848b336f554ab3f450e3566a1a158 [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
Eric Andersene77ae3a1999-10-19 20:03:34 +000053
Erik Andersen0817d132000-04-09 15:17:40 +000054static const char tar_usage[] =
Erik Andersen05100cd2000-01-16 01:30:52 +000055#ifdef BB_FEATURE_TAR_CREATE
Erik Andersen0817d132000-04-09 15:17:40 +000056 "tar -[cxtvO] "
Erik Andersen05100cd2000-01-16 01:30:52 +000057#else
Erik Andersen0817d132000-04-09 15:17:40 +000058 "tar -[xtvO] "
Erik Andersen05100cd2000-01-16 01:30:52 +000059#endif
Erik Andersen0817d132000-04-09 15:17:40 +000060#if defined BB_FEATURE_TAR_EXCLUDE
61 "[--exclude File] "
62#endif
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000063 "[-f tarFile] [FILE] ...\n"
64#ifndef BB_FEATURE_TRIVIAL_HELP
65 "\nCreate, extract, or list files from a tar file. Note that\n"
Erik Andersen0817d132000-04-09 15:17:40 +000066 "this version of tar treats hard links as separate files.\n\n"
67 "Main operation mode:\n"
68#ifdef BB_FEATURE_TAR_CREATE
69 "\tc\t\tcreate\n"
70#endif
71 "\tx\t\textract\n"
72 "\tt\t\tlist\n"
Erik Andersena3e57ca2000-04-19 03:38:01 +000073 "\nFile selection:\n"
Erik Andersen0817d132000-04-09 15:17:40 +000074 "\tf\t\tname of tarfile or \"-\" for stdin\n"
75 "\tO\t\textract to stdout\n"
76#if defined BB_FEATURE_TAR_EXCLUDE
77 "\t--exclude\tfile to exclude\n"
78#endif
Erik Andersena3e57ca2000-04-19 03:38:01 +000079 "\nInformative output:\n"
Erik Andersen0817d132000-04-09 15:17:40 +000080 "\tv\t\tverbosely list files processed\n"
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000081#endif
Erik Andersen0817d132000-04-09 15:17:40 +000082 ;
Eric Andersene77ae3a1999-10-19 20:03:34 +000083
Erik Andersen298854f2000-03-23 01:09:18 +000084/* Tar file constants */
Erik Andersen1ad302a2000-03-24 00:54:46 +000085#ifndef MAJOR
86#define MAJOR(dev) (((dev)>>8)&0xff)
87#define MINOR(dev) ((dev)&0xff)
88#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000089
90
Erik Andersen298854f2000-03-23 01:09:18 +000091/* POSIX tar Header Block, from POSIX 1003.1-1990 */
92struct TarHeader
93{
94 /* byte offset */
Erik Andersen0817d132000-04-09 15:17:40 +000095 char name[100]; /* 0-99 */
96 char mode[8]; /* 100-107 */
97 char uid[8]; /* 108-115 */
98 char gid[8]; /* 116-123 */
99 char size[12]; /* 124-135 */
100 char mtime[12]; /* 136-147 */
101 char chksum[8]; /* 148-155 */
102 char typeflag; /* 156-156 */
103 char linkname[100]; /* 157-256 */
104 char magic[6]; /* 257-262 */
105 char version[2]; /* 263-264 */
106 char uname[32]; /* 265-296 */
107 char gname[32]; /* 297-328 */
108 char devmajor[8]; /* 329-336 */
109 char devminor[8]; /* 337-344 */
110 char prefix[155]; /* 345-499 */
111 char padding[12]; /* 500-512 (pad to exactly the TAR_BLOCK_SIZE) */
Erik Andersen298854f2000-03-23 01:09:18 +0000112};
113typedef struct TarHeader TarHeader;
Eric Andersencc8ed391999-10-05 16:24:54 +0000114
Eric Andersencc8ed391999-10-05 16:24:54 +0000115
Erik Andersen298854f2000-03-23 01:09:18 +0000116/* A few useful constants */
117#define TAR_MAGIC "ustar" /* ustar and a null */
Erik Andersen0817d132000-04-09 15:17:40 +0000118#define TAR_VERSION " " /* Be compatable with GNU tar format */
Erik Andersen298854f2000-03-23 01:09:18 +0000119#define TAR_MAGIC_LEN 6
120#define TAR_VERSION_LEN 2
Erik Andersen298854f2000-03-23 01:09:18 +0000121#define TAR_BLOCK_SIZE 512
122
123/* A nice enum with all the possible tar file content types */
124enum TarFileType
125{
126 REGTYPE = '0', /* regular file */
127 REGTYPE0 = '\0', /* regular file (ancient bug compat)*/
128 LNKTYPE = '1', /* hard link */
129 SYMTYPE = '2', /* symbolic link */
130 CHRTYPE = '3', /* character special */
131 BLKTYPE = '4', /* block special */
132 DIRTYPE = '5', /* directory */
133 FIFOTYPE = '6', /* FIFO special */
134 CONTTYPE = '7', /* reserved */
135};
136typedef enum TarFileType TarFileType;
137
138/* This struct ignores magic, non-numeric user name,
139 * non-numeric group name, and the checksum, since
140 * these are all ignored by BusyBox tar. */
141struct TarInfo
142{
143 int tarFd; /* An open file descriptor for reading from the tarball */
144 char * name; /* File name */
145 mode_t mode; /* Unix mode, including device bits. */
146 uid_t uid; /* Numeric UID */
147 gid_t gid; /* Numeric GID */
148 size_t size; /* Size of file */
149 time_t mtime; /* Last-modified time */
150 enum TarFileType type; /* Regular, directory, link, etc */
151 char * linkname; /* Name for symbolic and hard links */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000152 long devmajor; /* Major number for special device */
153 long devminor; /* Minor number for special device */
Erik Andersen298854f2000-03-23 01:09:18 +0000154};
155typedef struct TarInfo TarInfo;
156
Erik Andersene454fb62000-03-23 04:27:58 +0000157/* Local procedures to restore files from a tar file. */
Erik Andersen298854f2000-03-23 01:09:18 +0000158static int readTarFile(const char* tarName, int extractFlag, int listFlag,
Erik Andersenecd51242000-04-08 03:08:21 +0000159 int tostdoutFlag, int verboseFlag, char** excludeList);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000160
161
Eric Andersencc8ed391999-10-05 16:24:54 +0000162
Erik Andersen05100cd2000-01-16 01:30:52 +0000163#ifdef BB_FEATURE_TAR_CREATE
Erik Andersen6acaa402000-03-26 14:03:20 +0000164/* Local procedures to save files into a tar file. */
Erik Andersen68a9ea42000-04-04 18:39:50 +0000165static int writeTarFile(const char* tarName, int tostdoutFlag,
Erik Andersenecd51242000-04-08 03:08:21 +0000166 int verboseFlag, int argc, char **argv, char** excludeList);
Erik Andersen05100cd2000-01-16 01:30:52 +0000167#endif
168
Eric Andersencc8ed391999-10-05 16:24:54 +0000169
Erik Andersene49d5ec2000-02-08 19:58:47 +0000170extern int tar_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000171{
Erik Andersenecd51242000-04-08 03:08:21 +0000172 char** excludeList=NULL;
Erik Andersen0817d132000-04-09 15:17:40 +0000173#if defined BB_FEATURE_TAR_EXCLUDE
Erik Andersenecd51242000-04-08 03:08:21 +0000174 int excludeListSize=0;
Erik Andersen0817d132000-04-09 15:17:40 +0000175#endif
Eric Andersen108a4432000-05-30 19:05:57 +0000176 const char *tarName="-";
Erik Andersen298854f2000-03-23 01:09:18 +0000177 int listFlag = FALSE;
178 int extractFlag = FALSE;
179 int createFlag = FALSE;
180 int verboseFlag = FALSE;
181 int tostdoutFlag = FALSE;
Erik Andersenecd51242000-04-08 03:08:21 +0000182 int stopIt;
Eric Andersencc8ed391999-10-05 16:24:54 +0000183
Erik Andersenecd51242000-04-08 03:08:21 +0000184 if (argc <= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000185 usage(tar_usage);
Eric Andersencc8ed391999-10-05 16:24:54 +0000186
Erik Andersenecd51242000-04-08 03:08:21 +0000187 /* Parse any options */
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000188 while (--argc > 0 && strspn(*(++argv), "-cxt") >0 ) {
Erik Andersenecd51242000-04-08 03:08:21 +0000189 stopIt=FALSE;
Eric Andersen108a4432000-05-30 19:05:57 +0000190 while (stopIt==FALSE && *argv && **argv) {
Erik Andersenecd51242000-04-08 03:08:21 +0000191 switch (**argv) {
192 case 'f':
193 if (--argc == 0) {
194 fatalError( "Option requires an argument: No file specified\n");
195 }
Eric Andersen108a4432000-05-30 19:05:57 +0000196 if (*tarName != '-')
Erik Andersenecd51242000-04-08 03:08:21 +0000197 fatalError( "Only one 'f' option allowed\n");
198 tarName = *(++argv);
199 if (tarName == NULL)
200 fatalError( "Option requires an argument: No file specified\n");
201 stopIt=TRUE;
202 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000203
Erik Andersenecd51242000-04-08 03:08:21 +0000204 case 't':
205 if (extractFlag == TRUE || createFlag == TRUE)
206 goto flagError;
207 listFlag = TRUE;
208 break;
Erik Andersende552872000-01-23 01:34:05 +0000209
Erik Andersenecd51242000-04-08 03:08:21 +0000210 case 'x':
211 if (listFlag == TRUE || createFlag == TRUE)
212 goto flagError;
213 extractFlag = TRUE;
214 break;
215 case 'c':
216 if (extractFlag == TRUE || listFlag == TRUE)
217 goto flagError;
218 createFlag = TRUE;
219 break;
Erik Andersende552872000-01-23 01:34:05 +0000220
Erik Andersenecd51242000-04-08 03:08:21 +0000221 case 'v':
222 verboseFlag = TRUE;
223 break;
Erik Andersende552872000-01-23 01:34:05 +0000224
Erik Andersenecd51242000-04-08 03:08:21 +0000225 case 'O':
226 tostdoutFlag = TRUE;
227 tarName = "-";
228 break;
Erik Andersenecd51242000-04-08 03:08:21 +0000229 case '-':
Erik Andersen0817d132000-04-09 15:17:40 +0000230#if defined BB_FEATURE_TAR_EXCLUDE
231 if (strcmp(*argv, "-exclude")==0) {
232 if (--argc == 0) {
233 fatalError( "Option requires an argument: No file specified\n");
234 }
235 excludeList=realloc( excludeList, sizeof(char**) * (excludeListSize+2));
236 excludeList[excludeListSize] = *(++argv);
237 /* Remove leading "/"s */
238 if (*excludeList[excludeListSize] =='/') {
239 excludeList[excludeListSize] = (excludeList[excludeListSize])+1;
240 }
241 if (excludeList[excludeListSize++] == NULL)
242 fatalError( "Option requires an argument: No file specified\n");
243 /* Tack a NULL onto the end of the list */
244 excludeList[excludeListSize] = NULL;
245 stopIt=TRUE;
246 break;
247 }
248#endif
Erik Andersenecd51242000-04-08 03:08:21 +0000249 break;
Erik Andersende552872000-01-23 01:34:05 +0000250
Erik Andersenecd51242000-04-08 03:08:21 +0000251 default:
252 fatalError( "Unknown tar flag '%c'\n"
253 "Try `tar --help' for more information\n", **argv);
254 }
Eric Andersen108a4432000-05-30 19:05:57 +0000255 ++(*argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000256 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000257 }
258
Erik Andersene49d5ec2000-02-08 19:58:47 +0000259 /*
260 * Do the correct type of action supplying the rest of the
261 * command line arguments as the list of files to process.
262 */
263 if (createFlag == TRUE) {
Erik Andersende552872000-01-23 01:34:05 +0000264#ifndef BB_FEATURE_TAR_CREATE
Erik Andersen298854f2000-03-23 01:09:18 +0000265 fatalError( "This version of tar was not compiled with tar creation support.\n");
Erik Andersende552872000-01-23 01:34:05 +0000266#else
Erik Andersenecd51242000-04-08 03:08:21 +0000267 exit(writeTarFile(tarName, tostdoutFlag, verboseFlag, argc, argv, excludeList));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000268#endif
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000269 }
270 if (listFlag == TRUE || extractFlag == TRUE) {
Erik Andersenecd51242000-04-08 03:08:21 +0000271 exit(readTarFile(tarName, extractFlag, listFlag, tostdoutFlag, verboseFlag, excludeList));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000272 }
Erik Andersende552872000-01-23 01:34:05 +0000273
Erik Andersene49d5ec2000-02-08 19:58:47 +0000274 flagError:
Erik Andersen298854f2000-03-23 01:09:18 +0000275 fatalError( "Exactly one of 'c', 'x' or 't' must be specified\n");
276}
277
278static void
Erik Andersen6a34b532000-04-07 06:55:38 +0000279fixUpPermissions(TarInfo *header)
280{
281 struct utimbuf t;
282 /* Now set permissions etc for the new file */
283 chown(header->name, header->uid, header->gid);
284 chmod(header->name, header->mode);
285 /* Reset the time */
286 t.actime = time(0);
287 t.modtime = header->mtime;
288 utime(header->name, &t);
289}
290
291static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000292tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersen298854f2000-03-23 01:09:18 +0000293{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000294 size_t writeSize;
295 size_t readSize;
296 size_t actualWriteSz;
297 char buffer[BUFSIZ];
298 size_t size = header->size;
299 int outFd=fileno(stdout);
300
301 /* Open the file to be written, if a file is supposed to be written */
302 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000303 /* Create the path to the file, just in case it isn't there...
304 * This should not screw up path permissions or anything. */
305 createPath(header->name, 0777);
Eric Andersen0c6a9702000-06-09 20:51:50 +0000306 if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY,
307 header->mode & ~S_IFMT)) < 0) {
308 errorMsg(io_error, header->name, strerror(errno));
309 return( FALSE);
310 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000311 }
312
313 /* Write out the file, if we are supposed to be doing that */
314 while ( size > 0 ) {
315 actualWriteSz=0;
316 if ( size > sizeof(buffer) )
317 writeSize = readSize = sizeof(buffer);
318 else {
319 int mod = size % 512;
320 if ( mod != 0 )
321 readSize = size + (512 - mod);
322 else
323 readSize = size;
324 writeSize = size;
325 }
326 if ( (readSize = fullRead(header->tarFd, buffer, readSize)) <= 0 ) {
327 /* Tarball seems to have a problem */
Erik Andersendeea0482000-03-25 23:09:29 +0000328 errorMsg("tar: Unexpected EOF in archive\n");
Erik Andersen6a34b532000-04-07 06:55:38 +0000329 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000330 }
331 if ( readSize < writeSize )
332 writeSize = readSize;
333
334 /* Write out the file, if we are supposed to be doing that */
335 if (extractFlag==TRUE) {
336
337 if ((actualWriteSz=fullWrite(outFd, buffer, writeSize)) != writeSize ) {
338 /* Output file seems to have a problem */
339 errorMsg(io_error, header->name, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000340 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000341 }
Erik Andersen0817d132000-04-09 15:17:40 +0000342 } else {
343 actualWriteSz=writeSize;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000344 }
345
346 size -= actualWriteSz;
347 }
348
349 /* Now we are done writing the file out, so try
350 * and fix up the permissions and whatnot */
351 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000352 close(outFd);
Erik Andersen6a34b532000-04-07 06:55:38 +0000353 fixUpPermissions(header);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000354 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000355 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000356}
357
Erik Andersen6a34b532000-04-07 06:55:38 +0000358static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000359tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000360{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000361
362 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000363 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000364
365 if (createPath(header->name, header->mode) != TRUE) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000366 errorMsg("tar: %s: Cannot mkdir: %s\n",
367 header->name, strerror(errno));
368 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000369 }
370 /* make the final component, just in case it was
371 * omitted by createPath() (which will skip the
372 * directory if it doesn't have a terminating '/') */
Erik Andersen6acaa402000-03-26 14:03:20 +0000373 if (mkdir(header->name, header->mode) == 0) {
374 fixUpPermissions(header);
375 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000376 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000377}
378
Erik Andersen6a34b532000-04-07 06:55:38 +0000379static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000380tarExtractHardLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000381{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000382 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000383 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000384
385 if (link(header->linkname, header->name) < 0) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000386 errorMsg("tar: %s: Cannot create hard link to '%s': %s\n",
Erik Andersen5661fe02000-04-05 01:00:52 +0000387 header->name, header->linkname, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000388 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000389 }
390
391 /* Now set permissions etc for the new directory */
392 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000393 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000394}
395
Erik Andersen6a34b532000-04-07 06:55:38 +0000396static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000397tarExtractSymLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000398{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000399 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000400 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000401
402#ifdef S_ISLNK
403 if (symlink(header->linkname, header->name) < 0) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000404 errorMsg("tar: %s: Cannot create symlink to '%s': %s\n",
Erik Andersen5661fe02000-04-05 01:00:52 +0000405 header->name, header->linkname, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000406 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000407 }
408 /* Try to change ownership of the symlink.
409 * If libs doesn't support that, don't bother.
410 * Changing the pointed-to-file is the Wrong Thing(tm).
411 */
412#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
413 lchown(header->name, header->uid, header->gid);
414#endif
415
416 /* Do not change permissions or date on symlink,
417 * since it changes the pointed to file instead. duh. */
418#else
Erik Andersen6a34b532000-04-07 06:55:38 +0000419 errorMsg("tar: %s: Cannot create symlink to '%s': %s\n",
420 header->name, header->linkname,
421 "symlinks not supported");
Erik Andersen1ad302a2000-03-24 00:54:46 +0000422#endif
Erik Andersen6a34b532000-04-07 06:55:38 +0000423 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000424}
425
Erik Andersen6a34b532000-04-07 06:55:38 +0000426static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000427tarExtractSpecial(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000428{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000429 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000430 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000431
432 if (S_ISCHR(header->mode) || S_ISBLK(header->mode) || S_ISSOCK(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000433 if (mknod(header->name, header->mode, makedev(header->devmajor, header->devminor)) < 0) {
434 errorMsg("tar: %s: Cannot mknod: %s\n",
435 header->name, strerror(errno));
436 return( FALSE);
437 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000438 } else if (S_ISFIFO(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000439 if (mkfifo(header->name, header->mode) < 0) {
440 errorMsg("tar: %s: Cannot mkfifo: %s\n",
441 header->name, strerror(errno));
442 return( FALSE);
443 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000444 }
445
446 /* Now set permissions etc for the new directory */
447 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000448 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000449}
450
Erik Andersen1ad302a2000-03-24 00:54:46 +0000451/* Read an octal value in a field of the specified width, with optional
Erik Andersen298854f2000-03-23 01:09:18 +0000452 * spaces on both sides of the number and with an optional null character
Erik Andersen1ad302a2000-03-24 00:54:46 +0000453 * at the end. Returns -1 on an illegal format. */
Erik Andersen298854f2000-03-23 01:09:18 +0000454static long getOctal(const char *cp, int size)
Eric Andersencc8ed391999-10-05 16:24:54 +0000455{
Erik Andersen298854f2000-03-23 01:09:18 +0000456 long val = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000457
Erik Andersen298854f2000-03-23 01:09:18 +0000458 for(;(size > 0) && (*cp == ' '); cp++, size--);
459 if ((size == 0) || !isOctal(*cp))
460 return -1;
461 for(; (size > 0) && isOctal(*cp); size--) {
462 val = val * 8 + *cp++ - '0';
Eric Andersencc8ed391999-10-05 16:24:54 +0000463 }
Erik Andersen298854f2000-03-23 01:09:18 +0000464 for (;(size > 0) && (*cp == ' '); cp++, size--);
465 if ((size > 0) && *cp)
466 return -1;
467 return val;
468}
Eric Andersencc8ed391999-10-05 16:24:54 +0000469
Erik Andersen1ad302a2000-03-24 00:54:46 +0000470
Erik Andersen298854f2000-03-23 01:09:18 +0000471/* Parse the tar header and fill in the nice struct with the details */
472static int
Erik Andersen3364d782000-03-28 00:58:14 +0000473readTarHeader(struct TarHeader *rawHeader, struct TarInfo *header)
Erik Andersen298854f2000-03-23 01:09:18 +0000474{
Erik Andersene454fb62000-03-23 04:27:58 +0000475 int i;
Erik Andersen84e09e42000-04-08 20:58:35 +0000476 long chksum, sum=0;
Erik Andersene454fb62000-03-23 04:27:58 +0000477 unsigned char *s = (unsigned char *)rawHeader;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000478
Erik Andersen298854f2000-03-23 01:09:18 +0000479 header->name = rawHeader->name;
Erik Andersen84e09e42000-04-08 20:58:35 +0000480 /* Check for and relativify any absolute paths */
481 if ( *(header->name) == '/' ) {
482 static int alreadyWarned=FALSE;
483
484 while (*(header->name) == '/')
485 ++*(header->name);
486
487 if (alreadyWarned == FALSE) {
488 errorMsg("tar: Removing leading '/' from member names\n");
489 alreadyWarned = TRUE;
490 }
491 }
492
Erik Andersen298854f2000-03-23 01:09:18 +0000493 header->mode = getOctal(rawHeader->mode, sizeof(rawHeader->mode));
494 header->uid = getOctal(rawHeader->uid, sizeof(rawHeader->uid));
495 header->gid = getOctal(rawHeader->gid, sizeof(rawHeader->gid));
496 header->size = getOctal(rawHeader->size, sizeof(rawHeader->size));
497 header->mtime = getOctal(rawHeader->mtime, sizeof(rawHeader->mtime));
498 chksum = getOctal(rawHeader->chksum, sizeof(rawHeader->chksum));
499 header->type = rawHeader->typeflag;
500 header->linkname = rawHeader->linkname;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000501 header->devmajor = getOctal(rawHeader->devmajor, sizeof(rawHeader->devmajor));
502 header->devminor = getOctal(rawHeader->devminor, sizeof(rawHeader->devminor));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000503
Erik Andersen298854f2000-03-23 01:09:18 +0000504 /* Check the checksum */
Erik Andersen84e09e42000-04-08 20:58:35 +0000505 for (i = sizeof(*rawHeader); i-- != 0;) {
Erik Andersen298854f2000-03-23 01:09:18 +0000506 sum += *s++;
Erik Andersen84e09e42000-04-08 20:58:35 +0000507 }
508 /* Remove the effects of the checksum field (replace
509 * with blanks for the purposes of the checksum) */
510 s = rawHeader->chksum;
511 for (i = sizeof(rawHeader->chksum) ; i-- != 0;) {
512 sum -= *s++;
513 }
514 sum += ' ' * sizeof(rawHeader->chksum);
Erik Andersene454fb62000-03-23 04:27:58 +0000515 if (sum == chksum )
Erik Andersen298854f2000-03-23 01:09:18 +0000516 return ( TRUE);
517 return( FALSE);
518}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000519
Erik Andersene49d5ec2000-02-08 19:58:47 +0000520
Erik Andersen1ad302a2000-03-24 00:54:46 +0000521/*
522 * Read a tar file and extract or list the specified files within it.
523 * If the list is empty than all files are extracted or listed.
524 */
525static int readTarFile(const char* tarName, int extractFlag, int listFlag,
Erik Andersenecd51242000-04-08 03:08:21 +0000526 int tostdoutFlag, int verboseFlag, char** excludeList)
Erik Andersen1ad302a2000-03-24 00:54:46 +0000527{
Erik Andersen0817d132000-04-09 15:17:40 +0000528 int status, tarFd=-1;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000529 int errorFlag=FALSE;
530 TarHeader rawHeader;
531 TarInfo header;
Erik Andersen0817d132000-04-09 15:17:40 +0000532#if defined BB_FEATURE_TAR_EXCLUDE
533 char** tmpList;
534#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000535
Erik Andersen1ad302a2000-03-24 00:54:46 +0000536 /* Open the tar file for reading. */
537 if (!strcmp(tarName, "-"))
538 tarFd = fileno(stdin);
539 else
540 tarFd = open(tarName, O_RDONLY);
541 if (tarFd < 0) {
Erik Andersendeea0482000-03-25 23:09:29 +0000542 errorMsg( "Error opening '%s': %s\n", tarName, strerror(errno));
Erik Andersen1ad302a2000-03-24 00:54:46 +0000543 return ( FALSE);
Erik Andersen06936df2000-01-23 02:14:20 +0000544 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000545
Erik Andersene49d5ec2000-02-08 19:58:47 +0000546 /* Set the umask for this process so it doesn't
Erik Andersen1ad302a2000-03-24 00:54:46 +0000547 * screw up permission setting for us later. */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000548 umask(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000549
Erik Andersen1ad302a2000-03-24 00:54:46 +0000550 /* Read the tar file, and iterate over it one file at a time */
551 while ( (status = fullRead(tarFd, (char*)&rawHeader, TAR_BLOCK_SIZE)) == TAR_BLOCK_SIZE ) {
Erik Andersene2729152000-02-18 21:34:17 +0000552
Erik Andersen1ad302a2000-03-24 00:54:46 +0000553 /* First, try to read the header */
Erik Andersen3364d782000-03-28 00:58:14 +0000554 if ( readTarHeader(&rawHeader, &header) == FALSE ) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000555 if ( *(header.name) == '\0' ) {
556 goto endgame;
557 } else {
558 errorFlag=TRUE;
559 errorMsg("Bad tar header, skipping\n");
560 continue;
561 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000562 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000563 if ( *(header.name) == '\0' )
564 goto endgame;
Erik Andersen0817d132000-04-09 15:17:40 +0000565 header.tarFd = tarFd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000566
Erik Andersen0817d132000-04-09 15:17:40 +0000567#if defined BB_FEATURE_TAR_EXCLUDE
568 {
569 int skipFlag=FALSE;
570 /* Check for excluded files.... */
571 for (tmpList=excludeList; tmpList && *tmpList; tmpList++) {
572 /* Do some extra hoop jumping for when directory names
573 * end in '/' but the entry in tmpList doesn't */
574 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
575 header.name[strlen(header.name)-1]=='/'
576 && strncmp( *tmpList, header.name,
577 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
578 /* If it is a regular file, pretend to extract it with
579 * the extractFlag set to FALSE, so the junk in the tarball
580 * is properly skipped over */
581 if ( header.type==REGTYPE || header.type==REGTYPE0 ) {
582 tarExtractRegularFile(&header, FALSE, FALSE);
583 }
584 skipFlag=TRUE;
585 break;
586 }
587 }
588 /* There are not the droids you're looking for, move along */
589 if (skipFlag==TRUE)
590 continue;
591 }
592#endif
Erik Andersen1ad302a2000-03-24 00:54:46 +0000593 /* Special treatment if the list (-t) flag is on */
594 if (verboseFlag == TRUE && extractFlag == FALSE) {
595 int len, len1;
596 char buf[35];
597 struct tm *tm = localtime (&(header.mtime));
598
Erik Andersen6a34b532000-04-07 06:55:38 +0000599 len=printf("%s ", modeString(header.mode));
600 memset(buf, 0, 8*sizeof(char));
601 my_getpwuid(buf, header.uid);
602 if (! *buf)
603 len+=printf("%d", header.uid);
604 else
605 len+=printf("%s", buf);
606 memset(buf, 0, 8*sizeof(char));
607 my_getgrgid(buf, header.gid);
608 if (! *buf)
609 len+=printf("/%-d ", header.gid);
610 else
611 len+=printf("/%-s ", buf);
612
Erik Andersen1ad302a2000-03-24 00:54:46 +0000613 if (header.type==CHRTYPE || header.type==BLKTYPE) {
614 len1=snprintf(buf, sizeof(buf), "%ld,%-ld ",
615 header.devmajor, header.devminor);
616 } else {
Erik Andersen1d1d9502000-04-21 01:26:49 +0000617 len1=snprintf(buf, sizeof(buf), "%lu ", (long)header.size);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000618 }
619 /* Jump through some hoops to make the columns match up */
620 for(;(len+len1)<31;len++)
621 printf(" ");
622 printf(buf);
623
624 /* Use ISO 8610 time format */
625 if (tm) {
626 printf ("%04d-%02d-%02d %02d:%02d:%02d ",
627 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
628 tm->tm_hour, tm->tm_min, tm->tm_sec);
629 }
630 }
631 /* List contents if we are supposed to do that */
632 if (verboseFlag == TRUE || listFlag == TRUE) {
633 /* Now the normal listing */
634 printf("%s", header.name);
Erik Andersen6a34b532000-04-07 06:55:38 +0000635 }
636 if (verboseFlag == TRUE && listFlag == TRUE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000637 /* If this is a link, say so */
638 if (header.type==LNKTYPE)
639 printf(" link to %s", header.linkname);
640 else if (header.type==SYMTYPE)
641 printf(" -> %s", header.linkname);
Erik Andersen6a34b532000-04-07 06:55:38 +0000642 }
643 if (verboseFlag == TRUE || listFlag == TRUE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000644 printf("\n");
645 }
646
Erik Andersen6a34b532000-04-07 06:55:38 +0000647 /* Remove any clutter lying in our way */
648 unlink( header.name);
Eric Andersencc8ed391999-10-05 16:24:54 +0000649
Erik Andersen1ad302a2000-03-24 00:54:46 +0000650 /* If we got here, we can be certain we have a legitimate
651 * header to work with. So work with it. */
652 switch ( header.type ) {
653 case REGTYPE:
654 case REGTYPE0:
655 /* If the name ends in a '/' then assume it is
656 * supposed to be a directory, and fall through */
657 if (header.name[strlen(header.name)-1] != '/') {
Erik Andersen6a34b532000-04-07 06:55:38 +0000658 if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE)
659 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000660 break;
661 }
662 case DIRTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000663 if (tarExtractDirectory( &header, extractFlag, tostdoutFlag)==FALSE)
664 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000665 break;
666 case LNKTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000667 if (tarExtractHardLink( &header, extractFlag, tostdoutFlag)==FALSE)
668 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000669 break;
670 case SYMTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000671 if (tarExtractSymLink( &header, extractFlag, tostdoutFlag)==FALSE)
672 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000673 break;
674 case CHRTYPE:
675 case BLKTYPE:
676 case FIFOTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000677 if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE)
678 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000679 break;
680 default:
681 close( tarFd);
682 return( FALSE);
683 }
684 }
685 close(tarFd);
686 if (status > 0) {
687 /* Bummer - we read a partial header */
Erik Andersendeea0482000-03-25 23:09:29 +0000688 errorMsg( "Error reading '%s': %s\n", tarName, strerror(errno));
Erik Andersen1ad302a2000-03-24 00:54:46 +0000689 return ( FALSE);
690 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000691 else if (errorFlag==TRUE) {
692 errorMsg( "tar: Error exit delayed from previous errors\n");
693 return( FALSE);
694 } else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000695 return( status);
696
697 /* Stuff to do when we are done */
698endgame:
699 close( tarFd);
700 if ( *(header.name) == '\0' ) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000701 if (errorFlag==TRUE)
702 errorMsg( "tar: Error exit delayed from previous errors\n");
703 else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000704 return( TRUE);
705 }
706 return( FALSE);
707}
708
Erik Andersen6acaa402000-03-26 14:03:20 +0000709
710#ifdef BB_FEATURE_TAR_CREATE
711
Erik Andersen68a9ea42000-04-04 18:39:50 +0000712/* Some info to be carried along when creating a new tarball */
713struct TarBallInfo
714{
715 char* fileName; /* File name of the tarball */
716 int tarFd; /* Open-for-write file descriptor
717 for the tarball */
718 struct stat statBuf; /* Stat info for the tarball, letting
719 us know the inode and device that the
720 tarball lives, so we can avoid trying
721 to include the tarball into itself */
722 int verboseFlag; /* Whether to print extra stuff or not */
Erik Andersenecd51242000-04-08 03:08:21 +0000723 char** excludeList; /* List of files to not include */
Erik Andersen68a9ea42000-04-04 18:39:50 +0000724};
725typedef struct TarBallInfo TarBallInfo;
726
727
Erik Andersen6acaa402000-03-26 14:03:20 +0000728/* Put an octal string into the specified buffer.
729 * The number is zero and space padded and possibly null padded.
730 * Returns TRUE if successful. */
731static int putOctal (char *cp, int len, long value)
732{
733 int tempLength;
Erik Andersen6acaa402000-03-26 14:03:20 +0000734 char tempBuffer[32];
Erik Andersen5661fe02000-04-05 01:00:52 +0000735 char *tempString = tempBuffer;
Erik Andersen6acaa402000-03-26 14:03:20 +0000736
737 /* Create a string of the specified length with an initial space,
738 * leading zeroes and the octal number, and a trailing null. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000739 sprintf (tempString, "%0*lo", len - 1, value);
Erik Andersen6acaa402000-03-26 14:03:20 +0000740
741 /* If the string is too large, suppress the leading space. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000742 tempLength = strlen (tempString) + 1;
Erik Andersen6acaa402000-03-26 14:03:20 +0000743 if (tempLength > len) {
744 tempLength--;
745 tempString++;
746 }
747
748 /* If the string is still too large, suppress the trailing null. */
749 if (tempLength > len)
750 tempLength--;
751
752 /* If the string is still too large, fail. */
753 if (tempLength > len)
754 return FALSE;
755
756 /* Copy the string to the field. */
757 memcpy (cp, tempString, len);
758
759 return TRUE;
760}
761
Erik Andersen68a9ea42000-04-04 18:39:50 +0000762/* Write out a tar header for the specified file/directory/whatever */
Erik Andersen3364d782000-03-28 00:58:14 +0000763static int
Erik Andersen5661fe02000-04-05 01:00:52 +0000764writeTarHeader(struct TarBallInfo *tbInfo, const char *fileName, struct stat *statbuf)
Erik Andersen6acaa402000-03-26 14:03:20 +0000765{
Erik Andersen5661fe02000-04-05 01:00:52 +0000766 long chksum=0;
767 struct TarHeader header;
Erik Andersen0817d132000-04-09 15:17:40 +0000768#if defined BB_FEATURE_TAR_EXCLUDE
Erik Andersenecd51242000-04-08 03:08:21 +0000769 char** tmpList;
Erik Andersen0817d132000-04-09 15:17:40 +0000770#endif
Erik Andersen5661fe02000-04-05 01:00:52 +0000771 const unsigned char *cp = (const unsigned char *) &header;
772 ssize_t size = sizeof(struct TarHeader);
773
774 memset( &header, 0, size);
Erik Andersen3364d782000-03-28 00:58:14 +0000775
Erik Andersen68a9ea42000-04-04 18:39:50 +0000776 if (*fileName=='/') {
777 static int alreadyWarned=FALSE;
778 if (alreadyWarned==FALSE) {
779 errorMsg("tar: Removing leading '/' from member names\n");
780 alreadyWarned=TRUE;
781 }
Erik Andersen84e09e42000-04-08 20:58:35 +0000782 strncpy(header.name, fileName+1, sizeof(header.name));
Erik Andersen3364d782000-03-28 00:58:14 +0000783 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000784 else {
Erik Andersen84e09e42000-04-08 20:58:35 +0000785 strncpy(header.name, fileName, sizeof(header.name));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000786 }
Erik Andersen84e09e42000-04-08 20:58:35 +0000787
Erik Andersen0817d132000-04-09 15:17:40 +0000788#if defined BB_FEATURE_TAR_EXCLUDE
789 /* Check for excluded files.... */
Erik Andersenecd51242000-04-08 03:08:21 +0000790 for (tmpList=tbInfo->excludeList; tmpList && *tmpList; tmpList++) {
Erik Andersen0817d132000-04-09 15:17:40 +0000791 /* Do some extra hoop jumping for when directory names
792 * end in '/' but the entry in tmpList doesn't */
793 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
794 header.name[strlen(header.name)-1]=='/'
795 && strncmp( *tmpList, header.name,
796 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
797 /* Set the mode to something that is not a regular file, thereby
798 * faking out writeTarFile into thinking that nothing further need
799 * be done for this file. Yes, I know this is ugly, but it works. */
800 statbuf->st_mode = 0;
801 return( TRUE);
802 }
Erik Andersenecd51242000-04-08 03:08:21 +0000803 }
Erik Andersen0817d132000-04-09 15:17:40 +0000804#endif
Erik Andersenecd51242000-04-08 03:08:21 +0000805
Erik Andersen5661fe02000-04-05 01:00:52 +0000806 putOctal(header.mode, sizeof(header.mode), statbuf->st_mode);
807 putOctal(header.uid, sizeof(header.uid), statbuf->st_uid);
808 putOctal(header.gid, sizeof(header.gid), statbuf->st_gid);
809 putOctal(header.size, sizeof(header.size), 0); /* Regular file size is handled later */
810 putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime);
811 strncpy(header.magic, TAR_MAGIC TAR_VERSION,
812 TAR_MAGIC_LEN + TAR_VERSION_LEN );
Erik Andersen68a9ea42000-04-04 18:39:50 +0000813
Erik Andersen84e09e42000-04-08 20:58:35 +0000814 /* Enter the user and group names (default to root if it fails) */
Erik Andersen5661fe02000-04-05 01:00:52 +0000815 my_getpwuid(header.uname, statbuf->st_uid);
Erik Andersen5661fe02000-04-05 01:00:52 +0000816 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000817 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +0000818 my_getgrgid(header.gname, statbuf->st_gid);
819 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000820 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +0000821
Erik Andersen95c1c1e2000-04-14 21:45:29 +0000822 /* WARNING/NOTICE: I break Hard Links */
Erik Andersen68a9ea42000-04-04 18:39:50 +0000823 if (S_ISLNK(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000824 char buffer[BUFSIZ];
825 header.typeflag = SYMTYPE;
826 if ( readlink(fileName, buffer, sizeof(buffer) - 1) < 0) {
827 errorMsg("Error reading symlink '%s': %s\n", header.name, strerror(errno));
828 return ( FALSE);
829 }
Erik Andersen95c1c1e2000-04-14 21:45:29 +0000830 strncpy(header.linkname, buffer, sizeof(header.linkname));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000831 } else if (S_ISDIR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000832 header.typeflag = DIRTYPE;
833 strncat(header.name, "/", sizeof(header.name));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000834 } else if (S_ISCHR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000835 header.typeflag = CHRTYPE;
836 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
837 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000838 } else if (S_ISBLK(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000839 header.typeflag = BLKTYPE;
840 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
841 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000842 } else if (S_ISFIFO(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000843 header.typeflag = FIFOTYPE;
844 } else if (S_ISREG(statbuf->st_mode)) {
845 header.typeflag = REGTYPE;
846 putOctal(header.size, sizeof(header.size), statbuf->st_size);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000847 } else {
Erik Andersen5661fe02000-04-05 01:00:52 +0000848 errorMsg("tar: %s: Unknown file type\n", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000849 return ( FALSE);
850 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000851
Erik Andersen5661fe02000-04-05 01:00:52 +0000852 /* Calculate and store the checksum (i.e. the sum of all of the bytes of
853 * the header). The checksum field must be filled with blanks for the
854 * calculation. The checksum field is formatted differently from the
855 * other fields: it has [6] digits, a null, then a space -- rather than
856 * digits, followed by a null like the other fields... */
857 memset(header.chksum, ' ', sizeof(header.chksum));
858 cp = (const unsigned char *) &header;
859 while (size-- > 0)
860 chksum += *cp++;
861 putOctal(header.chksum, 7, chksum);
862
863 /* Now write the header out to disk */
864 if ((size=fullWrite(tbInfo->tarFd, (char*)&header, sizeof(struct TarHeader))) < 0) {
865 errorMsg(io_error, fileName, strerror(errno));
866 return ( FALSE);
867 }
868 /* Pad the header up to the tar block size */
869 for (; size<TAR_BLOCK_SIZE; size++) {
870 write(tbInfo->tarFd, "\0", 1);
871 }
872 /* Now do the verbose thing (or not) */
873 if (tbInfo->verboseFlag==TRUE)
874 fprintf(stdout, "%s\n", header.name);
Erik Andersen3364d782000-03-28 00:58:14 +0000875
876 return ( TRUE);
877}
878
879
Erik Andersen68a9ea42000-04-04 18:39:50 +0000880static int writeFileToTarball(const char *fileName, struct stat *statbuf, void* userData)
Erik Andersen3364d782000-03-28 00:58:14 +0000881{
Erik Andersen68a9ea42000-04-04 18:39:50 +0000882 struct TarBallInfo *tbInfo = (struct TarBallInfo *)userData;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000883
884 /* It is against the rules to archive a socket */
885 if (S_ISSOCK(statbuf->st_mode)) {
886 errorMsg("tar: %s: socket ignored\n", fileName);
887 return( TRUE);
888 }
889
890 /* It is a bad idea to store the archive we are in the process of creating,
891 * so check the device and inode to be sure that this particular file isn't
892 * the new tarball */
893 if (tbInfo->statBuf.st_dev == statbuf->st_dev &&
894 tbInfo->statBuf.st_ino == statbuf->st_ino) {
895 errorMsg("tar: %s: file is the archive; skipping\n", fileName);
896 return( TRUE);
897 }
898
Erik Andersen5661fe02000-04-05 01:00:52 +0000899 if (writeTarHeader(tbInfo, fileName, statbuf)==FALSE) {
900 return( FALSE);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000901 }
Erik Andersen5661fe02000-04-05 01:00:52 +0000902
903 /* Now, if the file is a regular file, copy it out to the tarball */
904 if (S_ISREG(statbuf->st_mode)) {
905 int inputFileFd;
906 char buffer[BUFSIZ];
907 ssize_t size=0, readSize=0;
908
909 /* open the file we want to archive, and make sure all is well */
910 if ((inputFileFd = open(fileName, O_RDONLY)) < 0) {
911 errorMsg("tar: %s: Cannot open: %s\n", fileName, strerror(errno));
912 return( FALSE);
913 }
914
915 /* write the file to the archive */
916 while ( (size = fullRead(inputFileFd, buffer, sizeof(buffer))) > 0 ) {
917 if (fullWrite(tbInfo->tarFd, buffer, size) != size ) {
918 /* Output file seems to have a problem */
919 errorMsg(io_error, fileName, strerror(errno));
920 return( FALSE);
921 }
922 readSize+=size;
923 }
924 if (size == -1) {
925 errorMsg(io_error, fileName, strerror(errno));
926 return( FALSE);
927 }
928 /* Pad the file up to the tar block size */
929 for (; (readSize%TAR_BLOCK_SIZE) != 0; readSize++) {
930 write(tbInfo->tarFd, "\0", 1);
931 }
932 close( inputFileFd);
933 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000934
935 return( TRUE);
Erik Andersen6acaa402000-03-26 14:03:20 +0000936}
937
Erik Andersen68a9ea42000-04-04 18:39:50 +0000938static int writeTarFile(const char* tarName, int tostdoutFlag,
Erik Andersenecd51242000-04-08 03:08:21 +0000939 int verboseFlag, int argc, char **argv, char** excludeList)
Erik Andersen6acaa402000-03-26 14:03:20 +0000940{
Erik Andersen3364d782000-03-28 00:58:14 +0000941 int tarFd=-1;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000942 int errorFlag=FALSE;
Erik Andersen5661fe02000-04-05 01:00:52 +0000943 ssize_t size;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000944 struct TarBallInfo tbInfo;
945 tbInfo.verboseFlag = verboseFlag;
Erik Andersen3364d782000-03-28 00:58:14 +0000946
947 /* Make sure there is at least one file to tar up. */
948 if (argc <= 0)
949 fatalError("tar: Cowardly refusing to create an empty archive\n");
Erik Andersen6acaa402000-03-26 14:03:20 +0000950
951 /* Open the tar file for writing. */
Erik Andersen3364d782000-03-28 00:58:14 +0000952 if (tostdoutFlag == TRUE)
Erik Andersen68a9ea42000-04-04 18:39:50 +0000953 tbInfo.tarFd = fileno(stdout);
Erik Andersen6acaa402000-03-26 14:03:20 +0000954 else
Erik Andersen68a9ea42000-04-04 18:39:50 +0000955 tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
956 if (tbInfo.tarFd < 0) {
957 errorMsg( "tar: Error opening '%s': %s\n", tarName, strerror(errno));
Erik Andersen6acaa402000-03-26 14:03:20 +0000958 return ( FALSE);
959 }
Erik Andersenecd51242000-04-08 03:08:21 +0000960 tbInfo.excludeList=excludeList;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000961 /* Store the stat info for the tarball's file, so
962 * can avoid including the tarball into itself.... */
963 if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0)
Erik Andersen3364d782000-03-28 00:58:14 +0000964 fatalError(io_error, tarName, strerror(errno));
Erik Andersen6acaa402000-03-26 14:03:20 +0000965
966 /* Set the umask for this process so it doesn't
967 * screw up permission setting for us later. */
968 umask(0);
969
970 /* Read the directory/files and iterate over them one at a time */
Erik Andersen3364d782000-03-28 00:58:14 +0000971 while (argc-- > 0) {
972 if (recursiveAction(*argv++, TRUE, FALSE, FALSE,
Erik Andersen68a9ea42000-04-04 18:39:50 +0000973 writeFileToTarball, writeFileToTarball,
974 (void*) &tbInfo) == FALSE) {
975 errorFlag = TRUE;
Erik Andersen3364d782000-03-28 00:58:14 +0000976 }
Erik Andersen6acaa402000-03-26 14:03:20 +0000977 }
Erik Andersen5661fe02000-04-05 01:00:52 +0000978 /* Write two empty blocks to the end of the archive */
979 for (size=0; size<(2*TAR_BLOCK_SIZE); size++) {
980 write(tbInfo.tarFd, "\0", 1);
981 }
Erik Andersen0817d132000-04-09 15:17:40 +0000982
983 /* To be pedantically correct, we would check if the tarball
984 * is smaller then 20 tar blocks, and pad it if it was smaller,
985 * but that isn't necessary for GNU tar interoperability, and
986 * so is considered a waste of space */
987
Erik Andersen68a9ea42000-04-04 18:39:50 +0000988 /* Hang up the tools, close up shop, head home */
Erik Andersen6acaa402000-03-26 14:03:20 +0000989 close(tarFd);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000990 if (errorFlag == TRUE) {
991 errorMsg("tar: Error exit delayed from previous errors\n");
992 return(FALSE);
993 }
Erik Andersen6acaa402000-03-26 14:03:20 +0000994 return( TRUE);
995}
996
997
998#endif
999