blob: 732f26ddca21490572ad8651a32d25ba64967715 [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 Andersen68a9ea42000-04-04 18:39:50 +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 pretty
7 * different (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>
Erik Andersene49d5ec2000-02-08 19:58:47 +000052#include <sys/param.h> /* for PATH_MAX */
Eric Andersencc8ed391999-10-05 16:24:54 +000053
Eric Andersene77ae3a1999-10-19 20:03:34 +000054
Erik Andersen0817d132000-04-09 15:17:40 +000055static const char tar_usage[] =
Erik Andersen05100cd2000-01-16 01:30:52 +000056#ifdef BB_FEATURE_TAR_CREATE
Erik Andersen0817d132000-04-09 15:17:40 +000057 "tar -[cxtvO] "
Erik Andersen05100cd2000-01-16 01:30:52 +000058#else
Erik Andersen0817d132000-04-09 15:17:40 +000059 "tar -[xtvO] "
Erik Andersen05100cd2000-01-16 01:30:52 +000060#endif
Erik Andersen0817d132000-04-09 15:17:40 +000061#if defined BB_FEATURE_TAR_EXCLUDE
62 "[--exclude File] "
63#endif
64 "[-f tarFile] [FILE] ...\n\n"
65 "Create, extract, or list files from a tar file. Note that\n"
66 "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"
73 "File selection:\n"
74 "\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
79 "Informative output:\n"
80 "\tv\t\tverbosely list files processed\n"
81 ;
Eric Andersene77ae3a1999-10-19 20:03:34 +000082
Erik Andersen298854f2000-03-23 01:09:18 +000083/* Tar file constants */
Erik Andersen1ad302a2000-03-24 00:54:46 +000084#ifndef MAJOR
85#define MAJOR(dev) (((dev)>>8)&0xff)
86#define MINOR(dev) ((dev)&0xff)
87#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000088
89
Erik Andersen298854f2000-03-23 01:09:18 +000090/* POSIX tar Header Block, from POSIX 1003.1-1990 */
91struct TarHeader
92{
93 /* byte offset */
Erik Andersen0817d132000-04-09 15:17:40 +000094 char name[100]; /* 0-99 */
95 char mode[8]; /* 100-107 */
96 char uid[8]; /* 108-115 */
97 char gid[8]; /* 116-123 */
98 char size[12]; /* 124-135 */
99 char mtime[12]; /* 136-147 */
100 char chksum[8]; /* 148-155 */
101 char typeflag; /* 156-156 */
102 char linkname[100]; /* 157-256 */
103 char magic[6]; /* 257-262 */
104 char version[2]; /* 263-264 */
105 char uname[32]; /* 265-296 */
106 char gname[32]; /* 297-328 */
107 char devmajor[8]; /* 329-336 */
108 char devminor[8]; /* 337-344 */
109 char prefix[155]; /* 345-499 */
110 char padding[12]; /* 500-512 (pad to exactly the TAR_BLOCK_SIZE) */
Erik Andersen298854f2000-03-23 01:09:18 +0000111};
112typedef struct TarHeader TarHeader;
Eric Andersencc8ed391999-10-05 16:24:54 +0000113
Eric Andersencc8ed391999-10-05 16:24:54 +0000114
Erik Andersen298854f2000-03-23 01:09:18 +0000115/* A few useful constants */
116#define TAR_MAGIC "ustar" /* ustar and a null */
Erik Andersen0817d132000-04-09 15:17:40 +0000117#define TAR_VERSION " " /* Be compatable with GNU tar format */
Erik Andersen298854f2000-03-23 01:09:18 +0000118#define TAR_MAGIC_LEN 6
119#define TAR_VERSION_LEN 2
Erik Andersen298854f2000-03-23 01:09:18 +0000120#define TAR_BLOCK_SIZE 512
121
122/* A nice enum with all the possible tar file content types */
123enum TarFileType
124{
125 REGTYPE = '0', /* regular file */
126 REGTYPE0 = '\0', /* regular file (ancient bug compat)*/
127 LNKTYPE = '1', /* hard link */
128 SYMTYPE = '2', /* symbolic link */
129 CHRTYPE = '3', /* character special */
130 BLKTYPE = '4', /* block special */
131 DIRTYPE = '5', /* directory */
132 FIFOTYPE = '6', /* FIFO special */
133 CONTTYPE = '7', /* reserved */
134};
135typedef enum TarFileType TarFileType;
136
137/* This struct ignores magic, non-numeric user name,
138 * non-numeric group name, and the checksum, since
139 * these are all ignored by BusyBox tar. */
140struct TarInfo
141{
142 int tarFd; /* An open file descriptor for reading from the tarball */
143 char * name; /* File name */
144 mode_t mode; /* Unix mode, including device bits. */
145 uid_t uid; /* Numeric UID */
146 gid_t gid; /* Numeric GID */
147 size_t size; /* Size of file */
148 time_t mtime; /* Last-modified time */
149 enum TarFileType type; /* Regular, directory, link, etc */
150 char * linkname; /* Name for symbolic and hard links */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000151 long devmajor; /* Major number for special device */
152 long devminor; /* Minor number for special device */
Erik Andersen298854f2000-03-23 01:09:18 +0000153};
154typedef struct TarInfo TarInfo;
155
Erik Andersene454fb62000-03-23 04:27:58 +0000156/* Local procedures to restore files from a tar file. */
Erik Andersen298854f2000-03-23 01:09:18 +0000157static int readTarFile(const char* tarName, int extractFlag, int listFlag,
Erik Andersenecd51242000-04-08 03:08:21 +0000158 int tostdoutFlag, int verboseFlag, char** excludeList);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000159
160
Eric Andersencc8ed391999-10-05 16:24:54 +0000161
Erik Andersen05100cd2000-01-16 01:30:52 +0000162#ifdef BB_FEATURE_TAR_CREATE
Erik Andersen6acaa402000-03-26 14:03:20 +0000163/* Local procedures to save files into a tar file. */
Erik Andersen68a9ea42000-04-04 18:39:50 +0000164static int writeTarFile(const char* tarName, int tostdoutFlag,
Erik Andersenecd51242000-04-08 03:08:21 +0000165 int verboseFlag, int argc, char **argv, char** excludeList);
Erik Andersen05100cd2000-01-16 01:30:52 +0000166#endif
167
Eric Andersencc8ed391999-10-05 16:24:54 +0000168
Erik Andersene49d5ec2000-02-08 19:58:47 +0000169extern int tar_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000170{
Erik Andersenecd51242000-04-08 03:08:21 +0000171 char** excludeList=NULL;
Erik Andersen0817d132000-04-09 15:17:40 +0000172#if defined BB_FEATURE_TAR_EXCLUDE
Erik Andersenecd51242000-04-08 03:08:21 +0000173 int excludeListSize=0;
Erik Andersen0817d132000-04-09 15:17:40 +0000174#endif
Erik Andersen298854f2000-03-23 01:09:18 +0000175 const char *tarName=NULL;
Erik Andersen298854f2000-03-23 01:09:18 +0000176 int listFlag = FALSE;
177 int extractFlag = FALSE;
178 int createFlag = FALSE;
179 int verboseFlag = FALSE;
180 int tostdoutFlag = FALSE;
Erik Andersenecd51242000-04-08 03:08:21 +0000181 int stopIt;
Eric Andersencc8ed391999-10-05 16:24:54 +0000182
Erik Andersenecd51242000-04-08 03:08:21 +0000183 if (argc <= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000184 usage(tar_usage);
Eric Andersencc8ed391999-10-05 16:24:54 +0000185
Erik Andersenecd51242000-04-08 03:08:21 +0000186 /* Parse any options */
187 while (--argc > 0 && **(++argv) == '-') {
188 stopIt=FALSE;
189 while (stopIt==FALSE && *(++(*argv))) {
190 switch (**argv) {
191 case 'f':
192 if (--argc == 0) {
193 fatalError( "Option requires an argument: No file specified\n");
194 }
195 if (tarName != NULL)
196 fatalError( "Only one 'f' option allowed\n");
197 tarName = *(++argv);
198 if (tarName == NULL)
199 fatalError( "Option requires an argument: No file specified\n");
200 stopIt=TRUE;
201 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000202
Erik Andersenecd51242000-04-08 03:08:21 +0000203 case 't':
204 if (extractFlag == TRUE || createFlag == TRUE)
205 goto flagError;
206 listFlag = TRUE;
207 break;
Erik Andersende552872000-01-23 01:34:05 +0000208
Erik Andersenecd51242000-04-08 03:08:21 +0000209 case 'x':
210 if (listFlag == TRUE || createFlag == TRUE)
211 goto flagError;
212 extractFlag = TRUE;
213 break;
214 case 'c':
215 if (extractFlag == TRUE || listFlag == TRUE)
216 goto flagError;
217 createFlag = TRUE;
218 break;
Erik Andersende552872000-01-23 01:34:05 +0000219
Erik Andersenecd51242000-04-08 03:08:21 +0000220 case 'v':
221 verboseFlag = TRUE;
222 break;
Erik Andersende552872000-01-23 01:34:05 +0000223
Erik Andersenecd51242000-04-08 03:08:21 +0000224 case 'O':
225 tostdoutFlag = TRUE;
226 tarName = "-";
227 break;
Erik Andersenecd51242000-04-08 03:08:21 +0000228 case '-':
Erik Andersen0817d132000-04-09 15:17:40 +0000229#if defined BB_FEATURE_TAR_EXCLUDE
230 if (strcmp(*argv, "-exclude")==0) {
231 if (--argc == 0) {
232 fatalError( "Option requires an argument: No file specified\n");
233 }
234 excludeList=realloc( excludeList, sizeof(char**) * (excludeListSize+2));
235 excludeList[excludeListSize] = *(++argv);
236 /* Remove leading "/"s */
237 if (*excludeList[excludeListSize] =='/') {
238 excludeList[excludeListSize] = (excludeList[excludeListSize])+1;
239 }
240 if (excludeList[excludeListSize++] == NULL)
241 fatalError( "Option requires an argument: No file specified\n");
242 /* Tack a NULL onto the end of the list */
243 excludeList[excludeListSize] = NULL;
244 stopIt=TRUE;
245 break;
246 }
247#endif
Erik Andersenecd51242000-04-08 03:08:21 +0000248 usage(tar_usage);
249 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 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000255 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000256 }
257
Erik Andersene49d5ec2000-02-08 19:58:47 +0000258 /*
259 * Do the correct type of action supplying the rest of the
260 * command line arguments as the list of files to process.
261 */
262 if (createFlag == TRUE) {
Erik Andersende552872000-01-23 01:34:05 +0000263#ifndef BB_FEATURE_TAR_CREATE
Erik Andersen298854f2000-03-23 01:09:18 +0000264 fatalError( "This version of tar was not compiled with tar creation support.\n");
Erik Andersende552872000-01-23 01:34:05 +0000265#else
Erik Andersenecd51242000-04-08 03:08:21 +0000266 exit(writeTarFile(tarName, tostdoutFlag, verboseFlag, argc, argv, excludeList));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000267#endif
268 } else {
Erik Andersenecd51242000-04-08 03:08:21 +0000269 exit(readTarFile(tarName, extractFlag, listFlag, tostdoutFlag, verboseFlag, excludeList));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000270 }
Erik Andersende552872000-01-23 01:34:05 +0000271
Erik Andersene49d5ec2000-02-08 19:58:47 +0000272 flagError:
Erik Andersen298854f2000-03-23 01:09:18 +0000273 fatalError( "Exactly one of 'c', 'x' or 't' must be specified\n");
274}
275
276static void
Erik Andersen6a34b532000-04-07 06:55:38 +0000277fixUpPermissions(TarInfo *header)
278{
279 struct utimbuf t;
280 /* Now set permissions etc for the new file */
281 chown(header->name, header->uid, header->gid);
282 chmod(header->name, header->mode);
283 /* Reset the time */
284 t.actime = time(0);
285 t.modtime = header->mtime;
286 utime(header->name, &t);
287}
288
289static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000290tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersen298854f2000-03-23 01:09:18 +0000291{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000292 size_t writeSize;
293 size_t readSize;
294 size_t actualWriteSz;
295 char buffer[BUFSIZ];
296 size_t size = header->size;
297 int outFd=fileno(stdout);
298
299 /* Open the file to be written, if a file is supposed to be written */
300 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
301 if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY, header->mode & ~S_IFMT)) < 0)
302 errorMsg(io_error, header->name, strerror(errno));
303 /* 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);
306 }
307
308 /* Write out the file, if we are supposed to be doing that */
309 while ( size > 0 ) {
310 actualWriteSz=0;
311 if ( size > sizeof(buffer) )
312 writeSize = readSize = sizeof(buffer);
313 else {
314 int mod = size % 512;
315 if ( mod != 0 )
316 readSize = size + (512 - mod);
317 else
318 readSize = size;
319 writeSize = size;
320 }
321 if ( (readSize = fullRead(header->tarFd, buffer, readSize)) <= 0 ) {
322 /* Tarball seems to have a problem */
Erik Andersendeea0482000-03-25 23:09:29 +0000323 errorMsg("tar: Unexpected EOF in archive\n");
Erik Andersen6a34b532000-04-07 06:55:38 +0000324 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000325 }
326 if ( readSize < writeSize )
327 writeSize = readSize;
328
329 /* Write out the file, if we are supposed to be doing that */
330 if (extractFlag==TRUE) {
331
332 if ((actualWriteSz=fullWrite(outFd, buffer, writeSize)) != writeSize ) {
333 /* Output file seems to have a problem */
334 errorMsg(io_error, header->name, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000335 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000336 }
Erik Andersen0817d132000-04-09 15:17:40 +0000337 } else {
338 actualWriteSz=writeSize;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000339 }
340
341 size -= actualWriteSz;
342 }
343
344 /* Now we are done writing the file out, so try
345 * and fix up the permissions and whatnot */
346 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000347 close(outFd);
Erik Andersen6a34b532000-04-07 06:55:38 +0000348 fixUpPermissions(header);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000349 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000350 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000351}
352
Erik Andersen6a34b532000-04-07 06:55:38 +0000353static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000354tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000355{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000356
357 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000358 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000359
360 if (createPath(header->name, header->mode) != TRUE) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000361 errorMsg("tar: %s: Cannot mkdir: %s\n",
362 header->name, strerror(errno));
363 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000364 }
365 /* make the final component, just in case it was
366 * omitted by createPath() (which will skip the
367 * directory if it doesn't have a terminating '/') */
Erik Andersen6acaa402000-03-26 14:03:20 +0000368 if (mkdir(header->name, header->mode) == 0) {
369 fixUpPermissions(header);
370 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000371 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000372}
373
Erik Andersen6a34b532000-04-07 06:55:38 +0000374static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000375tarExtractHardLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000376{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000377 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000378 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000379
380 if (link(header->linkname, header->name) < 0) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000381 errorMsg("tar: %s: Cannot create hard link to '%s': %s\n",
Erik Andersen5661fe02000-04-05 01:00:52 +0000382 header->name, header->linkname, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000383 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000384 }
385
386 /* Now set permissions etc for the new directory */
387 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000388 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000389}
390
Erik Andersen6a34b532000-04-07 06:55:38 +0000391static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000392tarExtractSymLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000393{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000394 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000395 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000396
397#ifdef S_ISLNK
398 if (symlink(header->linkname, header->name) < 0) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000399 errorMsg("tar: %s: Cannot create symlink to '%s': %s\n",
Erik Andersen5661fe02000-04-05 01:00:52 +0000400 header->name, header->linkname, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000401 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000402 }
403 /* Try to change ownership of the symlink.
404 * If libs doesn't support that, don't bother.
405 * Changing the pointed-to-file is the Wrong Thing(tm).
406 */
407#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
408 lchown(header->name, header->uid, header->gid);
409#endif
410
411 /* Do not change permissions or date on symlink,
412 * since it changes the pointed to file instead. duh. */
413#else
Erik Andersen6a34b532000-04-07 06:55:38 +0000414 errorMsg("tar: %s: Cannot create symlink to '%s': %s\n",
415 header->name, header->linkname,
416 "symlinks not supported");
Erik Andersen1ad302a2000-03-24 00:54:46 +0000417#endif
Erik Andersen6a34b532000-04-07 06:55:38 +0000418 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000419}
420
Erik Andersen6a34b532000-04-07 06:55:38 +0000421static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000422tarExtractSpecial(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000423{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000424 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000425 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000426
427 if (S_ISCHR(header->mode) || S_ISBLK(header->mode) || S_ISSOCK(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000428 if (mknod(header->name, header->mode, makedev(header->devmajor, header->devminor)) < 0) {
429 errorMsg("tar: %s: Cannot mknod: %s\n",
430 header->name, strerror(errno));
431 return( FALSE);
432 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000433 } else if (S_ISFIFO(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000434 if (mkfifo(header->name, header->mode) < 0) {
435 errorMsg("tar: %s: Cannot mkfifo: %s\n",
436 header->name, strerror(errno));
437 return( FALSE);
438 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000439 }
440
441 /* Now set permissions etc for the new directory */
442 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000443 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000444}
445
Erik Andersen1ad302a2000-03-24 00:54:46 +0000446/* Read an octal value in a field of the specified width, with optional
Erik Andersen298854f2000-03-23 01:09:18 +0000447 * spaces on both sides of the number and with an optional null character
Erik Andersen1ad302a2000-03-24 00:54:46 +0000448 * at the end. Returns -1 on an illegal format. */
Erik Andersen298854f2000-03-23 01:09:18 +0000449static long getOctal(const char *cp, int size)
Eric Andersencc8ed391999-10-05 16:24:54 +0000450{
Erik Andersen298854f2000-03-23 01:09:18 +0000451 long val = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000452
Erik Andersen298854f2000-03-23 01:09:18 +0000453 for(;(size > 0) && (*cp == ' '); cp++, size--);
454 if ((size == 0) || !isOctal(*cp))
455 return -1;
456 for(; (size > 0) && isOctal(*cp); size--) {
457 val = val * 8 + *cp++ - '0';
Eric Andersencc8ed391999-10-05 16:24:54 +0000458 }
Erik Andersen298854f2000-03-23 01:09:18 +0000459 for (;(size > 0) && (*cp == ' '); cp++, size--);
460 if ((size > 0) && *cp)
461 return -1;
462 return val;
463}
Eric Andersencc8ed391999-10-05 16:24:54 +0000464
Erik Andersen1ad302a2000-03-24 00:54:46 +0000465
Erik Andersen298854f2000-03-23 01:09:18 +0000466/* Parse the tar header and fill in the nice struct with the details */
467static int
Erik Andersen3364d782000-03-28 00:58:14 +0000468readTarHeader(struct TarHeader *rawHeader, struct TarInfo *header)
Erik Andersen298854f2000-03-23 01:09:18 +0000469{
Erik Andersene454fb62000-03-23 04:27:58 +0000470 int i;
Erik Andersen84e09e42000-04-08 20:58:35 +0000471 long chksum, sum=0;
Erik Andersene454fb62000-03-23 04:27:58 +0000472 unsigned char *s = (unsigned char *)rawHeader;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000473
Erik Andersen298854f2000-03-23 01:09:18 +0000474 header->name = rawHeader->name;
Erik Andersen84e09e42000-04-08 20:58:35 +0000475 /* Check for and relativify any absolute paths */
476 if ( *(header->name) == '/' ) {
477 static int alreadyWarned=FALSE;
478
479 while (*(header->name) == '/')
480 ++*(header->name);
481
482 if (alreadyWarned == FALSE) {
483 errorMsg("tar: Removing leading '/' from member names\n");
484 alreadyWarned = TRUE;
485 }
486 }
487
Erik Andersen298854f2000-03-23 01:09:18 +0000488 header->mode = getOctal(rawHeader->mode, sizeof(rawHeader->mode));
489 header->uid = getOctal(rawHeader->uid, sizeof(rawHeader->uid));
490 header->gid = getOctal(rawHeader->gid, sizeof(rawHeader->gid));
491 header->size = getOctal(rawHeader->size, sizeof(rawHeader->size));
492 header->mtime = getOctal(rawHeader->mtime, sizeof(rawHeader->mtime));
493 chksum = getOctal(rawHeader->chksum, sizeof(rawHeader->chksum));
494 header->type = rawHeader->typeflag;
495 header->linkname = rawHeader->linkname;
Erik Andersen84e09e42000-04-08 20:58:35 +0000496 /* Check for and relativify any absolute paths */
497 if ( *(header->linkname) == '/' ) {
498 static int alreadyWarned=FALSE;
499
500 while (*(header->linkname) == '/')
501 ++*(header->linkname);
502
503 if (alreadyWarned == FALSE) {
504 errorMsg("tar: Removing leading '/' from link names\n");
505 alreadyWarned = TRUE;
506 }
507 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000508 header->devmajor = getOctal(rawHeader->devmajor, sizeof(rawHeader->devmajor));
509 header->devminor = getOctal(rawHeader->devminor, sizeof(rawHeader->devminor));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000510
Erik Andersen298854f2000-03-23 01:09:18 +0000511 /* Check the checksum */
Erik Andersen84e09e42000-04-08 20:58:35 +0000512 for (i = sizeof(*rawHeader); i-- != 0;) {
Erik Andersen298854f2000-03-23 01:09:18 +0000513 sum += *s++;
Erik Andersen84e09e42000-04-08 20:58:35 +0000514 }
515 /* Remove the effects of the checksum field (replace
516 * with blanks for the purposes of the checksum) */
517 s = rawHeader->chksum;
518 for (i = sizeof(rawHeader->chksum) ; i-- != 0;) {
519 sum -= *s++;
520 }
521 sum += ' ' * sizeof(rawHeader->chksum);
Erik Andersene454fb62000-03-23 04:27:58 +0000522 if (sum == chksum )
Erik Andersen298854f2000-03-23 01:09:18 +0000523 return ( TRUE);
524 return( FALSE);
525}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000526
Erik Andersene49d5ec2000-02-08 19:58:47 +0000527
Erik Andersen1ad302a2000-03-24 00:54:46 +0000528/*
529 * Read a tar file and extract or list the specified files within it.
530 * If the list is empty than all files are extracted or listed.
531 */
532static int readTarFile(const char* tarName, int extractFlag, int listFlag,
Erik Andersenecd51242000-04-08 03:08:21 +0000533 int tostdoutFlag, int verboseFlag, char** excludeList)
Erik Andersen1ad302a2000-03-24 00:54:46 +0000534{
Erik Andersen0817d132000-04-09 15:17:40 +0000535 int status, tarFd=-1;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000536 int errorFlag=FALSE;
537 TarHeader rawHeader;
538 TarInfo header;
Erik Andersen0817d132000-04-09 15:17:40 +0000539#if defined BB_FEATURE_TAR_EXCLUDE
540 char** tmpList;
541#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000542
Erik Andersen1ad302a2000-03-24 00:54:46 +0000543 /* Open the tar file for reading. */
544 if (!strcmp(tarName, "-"))
545 tarFd = fileno(stdin);
546 else
547 tarFd = open(tarName, O_RDONLY);
548 if (tarFd < 0) {
Erik Andersendeea0482000-03-25 23:09:29 +0000549 errorMsg( "Error opening '%s': %s\n", tarName, strerror(errno));
Erik Andersen1ad302a2000-03-24 00:54:46 +0000550 return ( FALSE);
Erik Andersen06936df2000-01-23 02:14:20 +0000551 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000552
Erik Andersene49d5ec2000-02-08 19:58:47 +0000553 /* Set the umask for this process so it doesn't
Erik Andersen1ad302a2000-03-24 00:54:46 +0000554 * screw up permission setting for us later. */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000555 umask(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000556
Erik Andersen1ad302a2000-03-24 00:54:46 +0000557 /* Read the tar file, and iterate over it one file at a time */
558 while ( (status = fullRead(tarFd, (char*)&rawHeader, TAR_BLOCK_SIZE)) == TAR_BLOCK_SIZE ) {
Erik Andersene2729152000-02-18 21:34:17 +0000559
Erik Andersen1ad302a2000-03-24 00:54:46 +0000560 /* First, try to read the header */
Erik Andersen3364d782000-03-28 00:58:14 +0000561 if ( readTarHeader(&rawHeader, &header) == FALSE ) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000562 if ( *(header.name) == '\0' ) {
563 goto endgame;
564 } else {
565 errorFlag=TRUE;
566 errorMsg("Bad tar header, skipping\n");
567 continue;
568 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000569 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000570 if ( *(header.name) == '\0' )
571 goto endgame;
Erik Andersen0817d132000-04-09 15:17:40 +0000572 header.tarFd = tarFd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000573
Erik Andersen0817d132000-04-09 15:17:40 +0000574#if defined BB_FEATURE_TAR_EXCLUDE
575 {
576 int skipFlag=FALSE;
577 /* Check for excluded files.... */
578 for (tmpList=excludeList; tmpList && *tmpList; tmpList++) {
579 /* Do some extra hoop jumping for when directory names
580 * end in '/' but the entry in tmpList doesn't */
581 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
582 header.name[strlen(header.name)-1]=='/'
583 && strncmp( *tmpList, header.name,
584 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
585 /* If it is a regular file, pretend to extract it with
586 * the extractFlag set to FALSE, so the junk in the tarball
587 * is properly skipped over */
588 if ( header.type==REGTYPE || header.type==REGTYPE0 ) {
589 tarExtractRegularFile(&header, FALSE, FALSE);
590 }
591 skipFlag=TRUE;
592 break;
593 }
594 }
595 /* There are not the droids you're looking for, move along */
596 if (skipFlag==TRUE)
597 continue;
598 }
599#endif
Erik Andersen1ad302a2000-03-24 00:54:46 +0000600 /* Special treatment if the list (-t) flag is on */
601 if (verboseFlag == TRUE && extractFlag == FALSE) {
602 int len, len1;
603 char buf[35];
604 struct tm *tm = localtime (&(header.mtime));
605
Erik Andersen6a34b532000-04-07 06:55:38 +0000606 len=printf("%s ", modeString(header.mode));
607 memset(buf, 0, 8*sizeof(char));
608 my_getpwuid(buf, header.uid);
609 if (! *buf)
610 len+=printf("%d", header.uid);
611 else
612 len+=printf("%s", buf);
613 memset(buf, 0, 8*sizeof(char));
614 my_getgrgid(buf, header.gid);
615 if (! *buf)
616 len+=printf("/%-d ", header.gid);
617 else
618 len+=printf("/%-s ", buf);
619
Erik Andersen1ad302a2000-03-24 00:54:46 +0000620 if (header.type==CHRTYPE || header.type==BLKTYPE) {
621 len1=snprintf(buf, sizeof(buf), "%ld,%-ld ",
622 header.devmajor, header.devminor);
623 } else {
624 len1=snprintf(buf, sizeof(buf), "%d ", header.size);
625 }
626 /* Jump through some hoops to make the columns match up */
627 for(;(len+len1)<31;len++)
628 printf(" ");
629 printf(buf);
630
631 /* Use ISO 8610 time format */
632 if (tm) {
633 printf ("%04d-%02d-%02d %02d:%02d:%02d ",
634 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
635 tm->tm_hour, tm->tm_min, tm->tm_sec);
636 }
637 }
638 /* List contents if we are supposed to do that */
639 if (verboseFlag == TRUE || listFlag == TRUE) {
640 /* Now the normal listing */
641 printf("%s", header.name);
Erik Andersen6a34b532000-04-07 06:55:38 +0000642 }
643 if (verboseFlag == TRUE && listFlag == TRUE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000644 /* If this is a link, say so */
645 if (header.type==LNKTYPE)
646 printf(" link to %s", header.linkname);
647 else if (header.type==SYMTYPE)
648 printf(" -> %s", header.linkname);
Erik Andersen6a34b532000-04-07 06:55:38 +0000649 }
650 if (verboseFlag == TRUE || listFlag == TRUE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000651 printf("\n");
652 }
653
Erik Andersen6a34b532000-04-07 06:55:38 +0000654 /* Remove any clutter lying in our way */
655 unlink( header.name);
Eric Andersencc8ed391999-10-05 16:24:54 +0000656
Erik Andersen1ad302a2000-03-24 00:54:46 +0000657 /* If we got here, we can be certain we have a legitimate
658 * header to work with. So work with it. */
659 switch ( header.type ) {
660 case REGTYPE:
661 case REGTYPE0:
662 /* If the name ends in a '/' then assume it is
663 * supposed to be a directory, and fall through */
664 if (header.name[strlen(header.name)-1] != '/') {
Erik Andersen6a34b532000-04-07 06:55:38 +0000665 if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE)
666 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000667 break;
668 }
669 case DIRTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000670 if (tarExtractDirectory( &header, extractFlag, tostdoutFlag)==FALSE)
671 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000672 break;
673 case LNKTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000674 if (tarExtractHardLink( &header, extractFlag, tostdoutFlag)==FALSE)
675 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000676 break;
677 case SYMTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000678 if (tarExtractSymLink( &header, extractFlag, tostdoutFlag)==FALSE)
679 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000680 break;
681 case CHRTYPE:
682 case BLKTYPE:
683 case FIFOTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000684 if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE)
685 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000686 break;
687 default:
688 close( tarFd);
689 return( FALSE);
690 }
691 }
692 close(tarFd);
693 if (status > 0) {
694 /* Bummer - we read a partial header */
Erik Andersendeea0482000-03-25 23:09:29 +0000695 errorMsg( "Error reading '%s': %s\n", tarName, strerror(errno));
Erik Andersen1ad302a2000-03-24 00:54:46 +0000696 return ( FALSE);
697 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000698 else if (errorFlag==TRUE) {
699 errorMsg( "tar: Error exit delayed from previous errors\n");
700 return( FALSE);
701 } else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000702 return( status);
703
704 /* Stuff to do when we are done */
705endgame:
706 close( tarFd);
707 if ( *(header.name) == '\0' ) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000708 if (errorFlag==TRUE)
709 errorMsg( "tar: Error exit delayed from previous errors\n");
710 else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000711 return( TRUE);
712 }
713 return( FALSE);
714}
715
Erik Andersen6acaa402000-03-26 14:03:20 +0000716
717#ifdef BB_FEATURE_TAR_CREATE
718
Erik Andersen68a9ea42000-04-04 18:39:50 +0000719/* Some info to be carried along when creating a new tarball */
720struct TarBallInfo
721{
722 char* fileName; /* File name of the tarball */
723 int tarFd; /* Open-for-write file descriptor
724 for the tarball */
725 struct stat statBuf; /* Stat info for the tarball, letting
726 us know the inode and device that the
727 tarball lives, so we can avoid trying
728 to include the tarball into itself */
729 int verboseFlag; /* Whether to print extra stuff or not */
Erik Andersenecd51242000-04-08 03:08:21 +0000730 char** excludeList; /* List of files to not include */
Erik Andersen68a9ea42000-04-04 18:39:50 +0000731};
732typedef struct TarBallInfo TarBallInfo;
733
734
Erik Andersen6acaa402000-03-26 14:03:20 +0000735/* Put an octal string into the specified buffer.
736 * The number is zero and space padded and possibly null padded.
737 * Returns TRUE if successful. */
738static int putOctal (char *cp, int len, long value)
739{
740 int tempLength;
Erik Andersen6acaa402000-03-26 14:03:20 +0000741 char tempBuffer[32];
Erik Andersen5661fe02000-04-05 01:00:52 +0000742 char *tempString = tempBuffer;
Erik Andersen6acaa402000-03-26 14:03:20 +0000743
744 /* Create a string of the specified length with an initial space,
745 * leading zeroes and the octal number, and a trailing null. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000746 sprintf (tempString, "%0*lo", len - 1, value);
Erik Andersen6acaa402000-03-26 14:03:20 +0000747
748 /* If the string is too large, suppress the leading space. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000749 tempLength = strlen (tempString) + 1;
Erik Andersen6acaa402000-03-26 14:03:20 +0000750 if (tempLength > len) {
751 tempLength--;
752 tempString++;
753 }
754
755 /* If the string is still too large, suppress the trailing null. */
756 if (tempLength > len)
757 tempLength--;
758
759 /* If the string is still too large, fail. */
760 if (tempLength > len)
761 return FALSE;
762
763 /* Copy the string to the field. */
764 memcpy (cp, tempString, len);
765
766 return TRUE;
767}
768
Erik Andersen68a9ea42000-04-04 18:39:50 +0000769/* Write out a tar header for the specified file/directory/whatever */
Erik Andersen3364d782000-03-28 00:58:14 +0000770static int
Erik Andersen5661fe02000-04-05 01:00:52 +0000771writeTarHeader(struct TarBallInfo *tbInfo, const char *fileName, struct stat *statbuf)
Erik Andersen6acaa402000-03-26 14:03:20 +0000772{
Erik Andersen5661fe02000-04-05 01:00:52 +0000773 long chksum=0;
774 struct TarHeader header;
Erik Andersen0817d132000-04-09 15:17:40 +0000775#if defined BB_FEATURE_TAR_EXCLUDE
Erik Andersenecd51242000-04-08 03:08:21 +0000776 char** tmpList;
Erik Andersen0817d132000-04-09 15:17:40 +0000777#endif
Erik Andersen5661fe02000-04-05 01:00:52 +0000778 const unsigned char *cp = (const unsigned char *) &header;
779 ssize_t size = sizeof(struct TarHeader);
780
781 memset( &header, 0, size);
Erik Andersen3364d782000-03-28 00:58:14 +0000782
Erik Andersen68a9ea42000-04-04 18:39:50 +0000783 if (*fileName=='/') {
784 static int alreadyWarned=FALSE;
785 if (alreadyWarned==FALSE) {
786 errorMsg("tar: Removing leading '/' from member names\n");
787 alreadyWarned=TRUE;
788 }
Erik Andersen84e09e42000-04-08 20:58:35 +0000789 strncpy(header.name, fileName+1, sizeof(header.name));
Erik Andersen3364d782000-03-28 00:58:14 +0000790 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000791 else {
Erik Andersen84e09e42000-04-08 20:58:35 +0000792 strncpy(header.name, fileName, sizeof(header.name));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000793 }
Erik Andersen84e09e42000-04-08 20:58:35 +0000794
Erik Andersen0817d132000-04-09 15:17:40 +0000795#if defined BB_FEATURE_TAR_EXCLUDE
796 /* Check for excluded files.... */
Erik Andersenecd51242000-04-08 03:08:21 +0000797 for (tmpList=tbInfo->excludeList; tmpList && *tmpList; tmpList++) {
Erik Andersen0817d132000-04-09 15:17:40 +0000798 /* Do some extra hoop jumping for when directory names
799 * end in '/' but the entry in tmpList doesn't */
800 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
801 header.name[strlen(header.name)-1]=='/'
802 && strncmp( *tmpList, header.name,
803 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
804 /* Set the mode to something that is not a regular file, thereby
805 * faking out writeTarFile into thinking that nothing further need
806 * be done for this file. Yes, I know this is ugly, but it works. */
807 statbuf->st_mode = 0;
808 return( TRUE);
809 }
Erik Andersenecd51242000-04-08 03:08:21 +0000810 }
Erik Andersen0817d132000-04-09 15:17:40 +0000811#endif
Erik Andersenecd51242000-04-08 03:08:21 +0000812
Erik Andersen5661fe02000-04-05 01:00:52 +0000813 putOctal(header.mode, sizeof(header.mode), statbuf->st_mode);
814 putOctal(header.uid, sizeof(header.uid), statbuf->st_uid);
815 putOctal(header.gid, sizeof(header.gid), statbuf->st_gid);
816 putOctal(header.size, sizeof(header.size), 0); /* Regular file size is handled later */
817 putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime);
818 strncpy(header.magic, TAR_MAGIC TAR_VERSION,
819 TAR_MAGIC_LEN + TAR_VERSION_LEN );
Erik Andersen68a9ea42000-04-04 18:39:50 +0000820
Erik Andersen84e09e42000-04-08 20:58:35 +0000821 /* Enter the user and group names (default to root if it fails) */
Erik Andersen5661fe02000-04-05 01:00:52 +0000822 my_getpwuid(header.uname, statbuf->st_uid);
Erik Andersen5661fe02000-04-05 01:00:52 +0000823 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000824 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +0000825 my_getgrgid(header.gname, statbuf->st_gid);
826 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000827 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +0000828
Erik Andersen84e09e42000-04-08 20:58:35 +0000829 // FIXME (or most likely not): I break Hard Links
Erik Andersen68a9ea42000-04-04 18:39:50 +0000830 if (S_ISLNK(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000831 char buffer[BUFSIZ];
832 header.typeflag = SYMTYPE;
833 if ( readlink(fileName, buffer, sizeof(buffer) - 1) < 0) {
834 errorMsg("Error reading symlink '%s': %s\n", header.name, strerror(errno));
835 return ( FALSE);
836 }
Erik Andersen84e09e42000-04-08 20:58:35 +0000837 if (*buffer=='/') {
838 static int alreadyWarned=FALSE;
839 if (alreadyWarned==FALSE) {
840 errorMsg("tar: Removing leading '/' from link names\n");
841 alreadyWarned=TRUE;
842 }
843 strncpy(header.linkname, buffer+1, sizeof(header.linkname));
844 }
845 else {
846 strncpy(header.linkname, buffer, sizeof(header.linkname));
847 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000848 } else if (S_ISDIR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000849 header.typeflag = DIRTYPE;
850 strncat(header.name, "/", sizeof(header.name));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000851 } else if (S_ISCHR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000852 header.typeflag = CHRTYPE;
853 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
854 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000855 } else if (S_ISBLK(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000856 header.typeflag = BLKTYPE;
857 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
858 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000859 } else if (S_ISFIFO(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000860 header.typeflag = FIFOTYPE;
861 } else if (S_ISREG(statbuf->st_mode)) {
862 header.typeflag = REGTYPE;
863 putOctal(header.size, sizeof(header.size), statbuf->st_size);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000864 } else {
Erik Andersen5661fe02000-04-05 01:00:52 +0000865 errorMsg("tar: %s: Unknown file type\n", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000866 return ( FALSE);
867 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000868
Erik Andersen5661fe02000-04-05 01:00:52 +0000869 /* Calculate and store the checksum (i.e. the sum of all of the bytes of
870 * the header). The checksum field must be filled with blanks for the
871 * calculation. The checksum field is formatted differently from the
872 * other fields: it has [6] digits, a null, then a space -- rather than
873 * digits, followed by a null like the other fields... */
874 memset(header.chksum, ' ', sizeof(header.chksum));
875 cp = (const unsigned char *) &header;
876 while (size-- > 0)
877 chksum += *cp++;
878 putOctal(header.chksum, 7, chksum);
879
880 /* Now write the header out to disk */
881 if ((size=fullWrite(tbInfo->tarFd, (char*)&header, sizeof(struct TarHeader))) < 0) {
882 errorMsg(io_error, fileName, strerror(errno));
883 return ( FALSE);
884 }
885 /* Pad the header up to the tar block size */
886 for (; size<TAR_BLOCK_SIZE; size++) {
887 write(tbInfo->tarFd, "\0", 1);
888 }
889 /* Now do the verbose thing (or not) */
890 if (tbInfo->verboseFlag==TRUE)
891 fprintf(stdout, "%s\n", header.name);
Erik Andersen3364d782000-03-28 00:58:14 +0000892
893 return ( TRUE);
894}
895
896
Erik Andersen68a9ea42000-04-04 18:39:50 +0000897static int writeFileToTarball(const char *fileName, struct stat *statbuf, void* userData)
Erik Andersen3364d782000-03-28 00:58:14 +0000898{
Erik Andersen68a9ea42000-04-04 18:39:50 +0000899 struct TarBallInfo *tbInfo = (struct TarBallInfo *)userData;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000900
901 /* It is against the rules to archive a socket */
902 if (S_ISSOCK(statbuf->st_mode)) {
903 errorMsg("tar: %s: socket ignored\n", fileName);
904 return( TRUE);
905 }
906
907 /* It is a bad idea to store the archive we are in the process of creating,
908 * so check the device and inode to be sure that this particular file isn't
909 * the new tarball */
910 if (tbInfo->statBuf.st_dev == statbuf->st_dev &&
911 tbInfo->statBuf.st_ino == statbuf->st_ino) {
912 errorMsg("tar: %s: file is the archive; skipping\n", fileName);
913 return( TRUE);
914 }
915
Erik Andersen5661fe02000-04-05 01:00:52 +0000916 if (writeTarHeader(tbInfo, fileName, statbuf)==FALSE) {
917 return( FALSE);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000918 }
Erik Andersen5661fe02000-04-05 01:00:52 +0000919
920 /* Now, if the file is a regular file, copy it out to the tarball */
921 if (S_ISREG(statbuf->st_mode)) {
922 int inputFileFd;
923 char buffer[BUFSIZ];
924 ssize_t size=0, readSize=0;
925
926 /* open the file we want to archive, and make sure all is well */
927 if ((inputFileFd = open(fileName, O_RDONLY)) < 0) {
928 errorMsg("tar: %s: Cannot open: %s\n", fileName, strerror(errno));
929 return( FALSE);
930 }
931
932 /* write the file to the archive */
933 while ( (size = fullRead(inputFileFd, buffer, sizeof(buffer))) > 0 ) {
934 if (fullWrite(tbInfo->tarFd, buffer, size) != size ) {
935 /* Output file seems to have a problem */
936 errorMsg(io_error, fileName, strerror(errno));
937 return( FALSE);
938 }
939 readSize+=size;
940 }
941 if (size == -1) {
942 errorMsg(io_error, fileName, strerror(errno));
943 return( FALSE);
944 }
945 /* Pad the file up to the tar block size */
946 for (; (readSize%TAR_BLOCK_SIZE) != 0; readSize++) {
947 write(tbInfo->tarFd, "\0", 1);
948 }
949 close( inputFileFd);
950 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000951
952 return( TRUE);
Erik Andersen6acaa402000-03-26 14:03:20 +0000953}
954
Erik Andersen68a9ea42000-04-04 18:39:50 +0000955static int writeTarFile(const char* tarName, int tostdoutFlag,
Erik Andersenecd51242000-04-08 03:08:21 +0000956 int verboseFlag, int argc, char **argv, char** excludeList)
Erik Andersen6acaa402000-03-26 14:03:20 +0000957{
Erik Andersen3364d782000-03-28 00:58:14 +0000958 int tarFd=-1;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000959 int errorFlag=FALSE;
Erik Andersen5661fe02000-04-05 01:00:52 +0000960 ssize_t size;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000961 struct TarBallInfo tbInfo;
962 tbInfo.verboseFlag = verboseFlag;
Erik Andersen3364d782000-03-28 00:58:14 +0000963
964 /* Make sure there is at least one file to tar up. */
965 if (argc <= 0)
966 fatalError("tar: Cowardly refusing to create an empty archive\n");
Erik Andersen6acaa402000-03-26 14:03:20 +0000967
968 /* Open the tar file for writing. */
Erik Andersen3364d782000-03-28 00:58:14 +0000969 if (tostdoutFlag == TRUE)
Erik Andersen68a9ea42000-04-04 18:39:50 +0000970 tbInfo.tarFd = fileno(stdout);
Erik Andersen6acaa402000-03-26 14:03:20 +0000971 else
Erik Andersen68a9ea42000-04-04 18:39:50 +0000972 tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
973 if (tbInfo.tarFd < 0) {
974 errorMsg( "tar: Error opening '%s': %s\n", tarName, strerror(errno));
Erik Andersen6acaa402000-03-26 14:03:20 +0000975 return ( FALSE);
976 }
Erik Andersenecd51242000-04-08 03:08:21 +0000977 tbInfo.excludeList=excludeList;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000978 /* Store the stat info for the tarball's file, so
979 * can avoid including the tarball into itself.... */
980 if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0)
Erik Andersen3364d782000-03-28 00:58:14 +0000981 fatalError(io_error, tarName, strerror(errno));
Erik Andersen6acaa402000-03-26 14:03:20 +0000982
983 /* Set the umask for this process so it doesn't
984 * screw up permission setting for us later. */
985 umask(0);
986
987 /* Read the directory/files and iterate over them one at a time */
Erik Andersen3364d782000-03-28 00:58:14 +0000988 while (argc-- > 0) {
989 if (recursiveAction(*argv++, TRUE, FALSE, FALSE,
Erik Andersen68a9ea42000-04-04 18:39:50 +0000990 writeFileToTarball, writeFileToTarball,
991 (void*) &tbInfo) == FALSE) {
992 errorFlag = TRUE;
Erik Andersen3364d782000-03-28 00:58:14 +0000993 }
Erik Andersen6acaa402000-03-26 14:03:20 +0000994 }
Erik Andersen5661fe02000-04-05 01:00:52 +0000995 /* Write two empty blocks to the end of the archive */
996 for (size=0; size<(2*TAR_BLOCK_SIZE); size++) {
997 write(tbInfo.tarFd, "\0", 1);
998 }
Erik Andersen0817d132000-04-09 15:17:40 +0000999
1000 /* To be pedantically correct, we would check if the tarball
1001 * is smaller then 20 tar blocks, and pad it if it was smaller,
1002 * but that isn't necessary for GNU tar interoperability, and
1003 * so is considered a waste of space */
1004
Erik Andersen68a9ea42000-04-04 18:39:50 +00001005 /* Hang up the tools, close up shop, head home */
Erik Andersen6acaa402000-03-26 14:03:20 +00001006 close(tarFd);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001007 if (errorFlag == TRUE) {
1008 errorMsg("tar: Error exit delayed from previous errors\n");
1009 return(FALSE);
1010 }
Erik Andersen6acaa402000-03-26 14:03:20 +00001011 return( TRUE);
1012}
1013
1014
1015#endif
1016