blob: 91baa2ddba8deee891e3eef3cddad3fedb2d058f [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 Andersen05100cd2000-01-16 01:30:52 +000055#ifdef BB_FEATURE_TAR_CREATE
56
Eric Andersene77ae3a1999-10-19 20:03:34 +000057static const char tar_usage[] =
Erik Andersene49d5ec2000-02-08 19:58:47 +000058 "tar -[cxtvOf] [tarFileName] [FILE] ...\n\n"
Erik Andersen5661fe02000-04-05 01:00:52 +000059 "Create, extract, or list files from a tar file. Note that\n"
60 "this version of tar packs hard links as separate files.\n\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +000061 "Options:\n"
62
63 "\tc=create, x=extract, t=list contents, v=verbose,\n"
64 "\tO=extract to stdout, f=tarfile or \"-\" for stdin\n";
Eric Andersene77ae3a1999-10-19 20:03:34 +000065
Erik Andersen05100cd2000-01-16 01:30:52 +000066#else
67
68static const char tar_usage[] =
Erik Andersene49d5ec2000-02-08 19:58:47 +000069 "tar -[xtvOf] [tarFileName] [FILE] ...\n\n"
70 "Extract, or list files stored in a tar file. This\n"
71 "version of tar does not support creation of tar files.\n\n"
72 "Options:\n"
73
74 "\tx=extract, t=list contents, v=verbose,\n"
75 "\tO=extract to stdout, f=tarfile or \"-\" for stdin\n";
Erik Andersen05100cd2000-01-16 01:30:52 +000076
77#endif
Eric Andersene77ae3a1999-10-19 20:03:34 +000078
79
Erik Andersen298854f2000-03-23 01:09:18 +000080/* Tar file constants */
Erik Andersen1ad302a2000-03-24 00:54:46 +000081#ifndef MAJOR
82#define MAJOR(dev) (((dev)>>8)&0xff)
83#define MINOR(dev) ((dev)&0xff)
84#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000085
86
Erik Andersen298854f2000-03-23 01:09:18 +000087/* POSIX tar Header Block, from POSIX 1003.1-1990 */
88struct TarHeader
89{
90 /* byte offset */
91 char name[100]; /* 0 */
92 char mode[8]; /* 100 */
93 char uid[8]; /* 108 */
94 char gid[8]; /* 116 */
95 char size[12]; /* 124 */
96 char mtime[12]; /* 136 */
97 char chksum[8]; /* 148 */
98 char typeflag; /* 156 */
99 char linkname[100]; /* 157 */
100 char magic[6]; /* 257 */
101 char version[2]; /* 263 */
102 char uname[32]; /* 265 */
103 char gname[32]; /* 297 */
104 char devmajor[8]; /* 329 */
105 char devminor[8]; /* 337 */
106 char prefix[155]; /* 345 */
107 /* padding 500 */
108};
109typedef struct TarHeader TarHeader;
Eric Andersencc8ed391999-10-05 16:24:54 +0000110
Eric Andersencc8ed391999-10-05 16:24:54 +0000111
Erik Andersen298854f2000-03-23 01:09:18 +0000112/* A few useful constants */
113#define TAR_MAGIC "ustar" /* ustar and a null */
Erik Andersen5661fe02000-04-05 01:00:52 +0000114//#define TAR_VERSION "00" /* 00 and no null */
115#define TAR_VERSION " " /* Be compatable with old GNU format */
Erik Andersen298854f2000-03-23 01:09:18 +0000116#define TAR_MAGIC_LEN 6
117#define TAR_VERSION_LEN 2
Erik Andersen298854f2000-03-23 01:09:18 +0000118#define TAR_BLOCK_SIZE 512
119
120/* A nice enum with all the possible tar file content types */
121enum TarFileType
122{
123 REGTYPE = '0', /* regular file */
124 REGTYPE0 = '\0', /* regular file (ancient bug compat)*/
125 LNKTYPE = '1', /* hard link */
126 SYMTYPE = '2', /* symbolic link */
127 CHRTYPE = '3', /* character special */
128 BLKTYPE = '4', /* block special */
129 DIRTYPE = '5', /* directory */
130 FIFOTYPE = '6', /* FIFO special */
131 CONTTYPE = '7', /* reserved */
132};
133typedef enum TarFileType TarFileType;
134
135/* This struct ignores magic, non-numeric user name,
136 * non-numeric group name, and the checksum, since
137 * these are all ignored by BusyBox tar. */
138struct TarInfo
139{
140 int tarFd; /* An open file descriptor for reading from the tarball */
141 char * name; /* File name */
142 mode_t mode; /* Unix mode, including device bits. */
143 uid_t uid; /* Numeric UID */
144 gid_t gid; /* Numeric GID */
145 size_t size; /* Size of file */
146 time_t mtime; /* Last-modified time */
147 enum TarFileType type; /* Regular, directory, link, etc */
148 char * linkname; /* Name for symbolic and hard links */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000149 long devmajor; /* Major number for special device */
150 long devminor; /* Minor number for special device */
Erik Andersen298854f2000-03-23 01:09:18 +0000151};
152typedef struct TarInfo TarInfo;
153
154/* Static data */
155static const unsigned long TarChecksumOffset = (const unsigned long)&(((TarHeader *)0)->chksum);
Eric Andersencc8ed391999-10-05 16:24:54 +0000156
157
Erik Andersene454fb62000-03-23 04:27:58 +0000158/* Local procedures to restore files from a tar file. */
Erik Andersen298854f2000-03-23 01:09:18 +0000159static int readTarFile(const char* tarName, int extractFlag, int listFlag,
160 int tostdoutFlag, int verboseFlag);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000161
162
Eric Andersencc8ed391999-10-05 16:24:54 +0000163
Erik Andersen05100cd2000-01-16 01:30:52 +0000164#ifdef BB_FEATURE_TAR_CREATE
Erik Andersen6acaa402000-03-26 14:03:20 +0000165/* Local procedures to save files into a tar file. */
Erik Andersen68a9ea42000-04-04 18:39:50 +0000166static int writeTarFile(const char* tarName, int tostdoutFlag,
167 int verboseFlag, int argc, char **argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000168static int putOctal(char *cp, int len, long value);
Eric Andersencc8ed391999-10-05 16:24:54 +0000169
Erik Andersen05100cd2000-01-16 01:30:52 +0000170#endif
171
Eric Andersencc8ed391999-10-05 16:24:54 +0000172
Erik Andersene49d5ec2000-02-08 19:58:47 +0000173extern int tar_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000174{
Erik Andersen298854f2000-03-23 01:09:18 +0000175 const char *tarName=NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000176 const char *options;
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;
Eric Andersencc8ed391999-10-05 16:24:54 +0000182
Erik Andersene49d5ec2000-02-08 19:58:47 +0000183 argc--;
184 argv++;
Eric Andersencc8ed391999-10-05 16:24:54 +0000185
Erik Andersene49d5ec2000-02-08 19:58:47 +0000186 if (argc < 1)
187 usage(tar_usage);
Eric Andersencc8ed391999-10-05 16:24:54 +0000188
Erik Andersen298854f2000-03-23 01:09:18 +0000189 /* Parse options */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000190 if (**argv == '-')
191 options = (*argv++) + 1;
192 else
193 options = (*argv++);
194 argc--;
Eric Andersencc8ed391999-10-05 16:24:54 +0000195
Erik Andersene49d5ec2000-02-08 19:58:47 +0000196 for (; *options; options++) {
197 switch (*options) {
198 case 'f':
Erik Andersen298854f2000-03-23 01:09:18 +0000199 if (tarName != NULL)
200 fatalError( "Only one 'f' option allowed\n");
Erik Andersende552872000-01-23 01:34:05 +0000201
Erik Andersene49d5ec2000-02-08 19:58:47 +0000202 tarName = *argv++;
Erik Andersen298854f2000-03-23 01:09:18 +0000203 if (tarName == NULL)
204 fatalError( "Option requires an argument: No file specified\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000205 argc--;
Erik Andersende552872000-01-23 01:34:05 +0000206
Erik Andersene49d5ec2000-02-08 19:58:47 +0000207 break;
Erik Andersende552872000-01-23 01:34:05 +0000208
Erik Andersene49d5ec2000-02-08 19:58:47 +0000209 case 't':
210 if (extractFlag == TRUE || createFlag == TRUE)
211 goto flagError;
212 listFlag = TRUE;
213 break;
Erik Andersende552872000-01-23 01:34:05 +0000214
Erik Andersene49d5ec2000-02-08 19:58:47 +0000215 case 'x':
216 if (listFlag == TRUE || createFlag == TRUE)
217 goto flagError;
218 extractFlag = TRUE;
219 break;
220 case 'c':
221 if (extractFlag == TRUE || listFlag == TRUE)
222 goto flagError;
223 createFlag = TRUE;
224 break;
Erik Andersende552872000-01-23 01:34:05 +0000225
Erik Andersene49d5ec2000-02-08 19:58:47 +0000226 case 'v':
227 verboseFlag = TRUE;
228 break;
Erik Andersende552872000-01-23 01:34:05 +0000229
Erik Andersene49d5ec2000-02-08 19:58:47 +0000230 case 'O':
231 tostdoutFlag = TRUE;
Erik Andersen3364d782000-03-28 00:58:14 +0000232 tarName = "-";
Erik Andersene49d5ec2000-02-08 19:58:47 +0000233 break;
Erik Andersende552872000-01-23 01:34:05 +0000234
Erik Andersene49d5ec2000-02-08 19:58:47 +0000235 case '-':
236 usage(tar_usage);
237 break;
Erik Andersende552872000-01-23 01:34:05 +0000238
Erik Andersene49d5ec2000-02-08 19:58:47 +0000239 default:
Erik Andersen298854f2000-03-23 01:09:18 +0000240 fatalError( "Unknown tar flag '%c'\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +0000241 "Try `tar --help' for more information\n", *options);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000242 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000243 }
244
Erik Andersene49d5ec2000-02-08 19:58:47 +0000245 /*
246 * Do the correct type of action supplying the rest of the
247 * command line arguments as the list of files to process.
248 */
249 if (createFlag == TRUE) {
Erik Andersende552872000-01-23 01:34:05 +0000250#ifndef BB_FEATURE_TAR_CREATE
Erik Andersen298854f2000-03-23 01:09:18 +0000251 fatalError( "This version of tar was not compiled with tar creation support.\n");
Erik Andersende552872000-01-23 01:34:05 +0000252#else
Erik Andersen68a9ea42000-04-04 18:39:50 +0000253 exit(writeTarFile(tarName, tostdoutFlag, verboseFlag, argc, argv));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000254#endif
255 } else {
Erik Andersen298854f2000-03-23 01:09:18 +0000256 exit(readTarFile(tarName, extractFlag, listFlag, tostdoutFlag, verboseFlag));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000257 }
Erik Andersende552872000-01-23 01:34:05 +0000258
Erik Andersene49d5ec2000-02-08 19:58:47 +0000259 flagError:
Erik Andersen298854f2000-03-23 01:09:18 +0000260 fatalError( "Exactly one of 'c', 'x' or 't' must be specified\n");
261}
262
263static void
Erik Andersen6a34b532000-04-07 06:55:38 +0000264fixUpPermissions(TarInfo *header)
265{
266 struct utimbuf t;
267 /* Now set permissions etc for the new file */
268 chown(header->name, header->uid, header->gid);
269 chmod(header->name, header->mode);
270 /* Reset the time */
271 t.actime = time(0);
272 t.modtime = header->mtime;
273 utime(header->name, &t);
274}
275
276static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000277tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersen298854f2000-03-23 01:09:18 +0000278{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000279 size_t writeSize;
280 size_t readSize;
281 size_t actualWriteSz;
282 char buffer[BUFSIZ];
283 size_t size = header->size;
284 int outFd=fileno(stdout);
285
286 /* Open the file to be written, if a file is supposed to be written */
287 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
288 if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY, header->mode & ~S_IFMT)) < 0)
289 errorMsg(io_error, header->name, strerror(errno));
290 /* Create the path to the file, just in case it isn't there...
291 * This should not screw up path permissions or anything. */
292 createPath(header->name, 0777);
293 }
294
295 /* Write out the file, if we are supposed to be doing that */
296 while ( size > 0 ) {
297 actualWriteSz=0;
298 if ( size > sizeof(buffer) )
299 writeSize = readSize = sizeof(buffer);
300 else {
301 int mod = size % 512;
302 if ( mod != 0 )
303 readSize = size + (512 - mod);
304 else
305 readSize = size;
306 writeSize = size;
307 }
308 if ( (readSize = fullRead(header->tarFd, buffer, readSize)) <= 0 ) {
309 /* Tarball seems to have a problem */
Erik Andersendeea0482000-03-25 23:09:29 +0000310 errorMsg("tar: Unexpected EOF in archive\n");
Erik Andersen6a34b532000-04-07 06:55:38 +0000311 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000312 }
313 if ( readSize < writeSize )
314 writeSize = readSize;
315
316 /* Write out the file, if we are supposed to be doing that */
317 if (extractFlag==TRUE) {
318
319 if ((actualWriteSz=fullWrite(outFd, buffer, writeSize)) != writeSize ) {
320 /* Output file seems to have a problem */
321 errorMsg(io_error, header->name, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000322 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000323 }
324 }
325
326 size -= actualWriteSz;
327 }
328
329 /* Now we are done writing the file out, so try
330 * and fix up the permissions and whatnot */
331 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000332 close(outFd);
Erik Andersen6a34b532000-04-07 06:55:38 +0000333 fixUpPermissions(header);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000334 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000335 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000336}
337
Erik Andersen6a34b532000-04-07 06:55:38 +0000338static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000339tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000340{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000341
342 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000343 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000344
345 if (createPath(header->name, header->mode) != TRUE) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000346 errorMsg("tar: %s: Cannot mkdir: %s\n",
347 header->name, strerror(errno));
348 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000349 }
350 /* make the final component, just in case it was
351 * omitted by createPath() (which will skip the
352 * directory if it doesn't have a terminating '/') */
Erik Andersen6acaa402000-03-26 14:03:20 +0000353 if (mkdir(header->name, header->mode) == 0) {
354 fixUpPermissions(header);
355 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000356 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000357}
358
Erik Andersen6a34b532000-04-07 06:55:38 +0000359static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000360tarExtractHardLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000361{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000362 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 (link(header->linkname, header->name) < 0) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000366 errorMsg("tar: %s: Cannot create hard link to '%s': %s\n",
Erik Andersen5661fe02000-04-05 01:00:52 +0000367 header->name, header->linkname, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000368 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000369 }
370
371 /* Now set permissions etc for the new directory */
372 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000373 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000374}
375
Erik Andersen6a34b532000-04-07 06:55:38 +0000376static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000377tarExtractSymLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000378{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000379 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000380 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000381
382#ifdef S_ISLNK
383 if (symlink(header->linkname, header->name) < 0) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000384 errorMsg("tar: %s: Cannot create symlink to '%s': %s\n",
Erik Andersen5661fe02000-04-05 01:00:52 +0000385 header->name, header->linkname, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000386 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000387 }
388 /* Try to change ownership of the symlink.
389 * If libs doesn't support that, don't bother.
390 * Changing the pointed-to-file is the Wrong Thing(tm).
391 */
392#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
393 lchown(header->name, header->uid, header->gid);
394#endif
395
396 /* Do not change permissions or date on symlink,
397 * since it changes the pointed to file instead. duh. */
398#else
Erik Andersen6a34b532000-04-07 06:55:38 +0000399 errorMsg("tar: %s: Cannot create symlink to '%s': %s\n",
400 header->name, header->linkname,
401 "symlinks not supported");
Erik Andersen1ad302a2000-03-24 00:54:46 +0000402#endif
Erik Andersen6a34b532000-04-07 06:55:38 +0000403 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000404}
405
Erik Andersen6a34b532000-04-07 06:55:38 +0000406static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000407tarExtractSpecial(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000408{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000409 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000410 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000411
412 if (S_ISCHR(header->mode) || S_ISBLK(header->mode) || S_ISSOCK(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000413 if (mknod(header->name, header->mode, makedev(header->devmajor, header->devminor)) < 0) {
414 errorMsg("tar: %s: Cannot mknod: %s\n",
415 header->name, strerror(errno));
416 return( FALSE);
417 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000418 } else if (S_ISFIFO(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000419 if (mkfifo(header->name, header->mode) < 0) {
420 errorMsg("tar: %s: Cannot mkfifo: %s\n",
421 header->name, strerror(errno));
422 return( FALSE);
423 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000424 }
425
426 /* Now set permissions etc for the new directory */
427 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000428 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000429}
430
Erik Andersen1ad302a2000-03-24 00:54:46 +0000431/* Read an octal value in a field of the specified width, with optional
Erik Andersen298854f2000-03-23 01:09:18 +0000432 * spaces on both sides of the number and with an optional null character
Erik Andersen1ad302a2000-03-24 00:54:46 +0000433 * at the end. Returns -1 on an illegal format. */
Erik Andersen298854f2000-03-23 01:09:18 +0000434static long getOctal(const char *cp, int size)
Eric Andersencc8ed391999-10-05 16:24:54 +0000435{
Erik Andersen298854f2000-03-23 01:09:18 +0000436 long val = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000437
Erik Andersen298854f2000-03-23 01:09:18 +0000438 for(;(size > 0) && (*cp == ' '); cp++, size--);
439 if ((size == 0) || !isOctal(*cp))
440 return -1;
441 for(; (size > 0) && isOctal(*cp); size--) {
442 val = val * 8 + *cp++ - '0';
Eric Andersencc8ed391999-10-05 16:24:54 +0000443 }
Erik Andersen298854f2000-03-23 01:09:18 +0000444 for (;(size > 0) && (*cp == ' '); cp++, size--);
445 if ((size > 0) && *cp)
446 return -1;
447 return val;
448}
Eric Andersencc8ed391999-10-05 16:24:54 +0000449
Erik Andersen1ad302a2000-03-24 00:54:46 +0000450
Erik Andersen298854f2000-03-23 01:09:18 +0000451/* Parse the tar header and fill in the nice struct with the details */
452static int
Erik Andersen3364d782000-03-28 00:58:14 +0000453readTarHeader(struct TarHeader *rawHeader, struct TarInfo *header)
Erik Andersen298854f2000-03-23 01:09:18 +0000454{
Erik Andersene454fb62000-03-23 04:27:58 +0000455 int i;
456 long chksum, sum;
457 unsigned char *s = (unsigned char *)rawHeader;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000458
Erik Andersen298854f2000-03-23 01:09:18 +0000459 header->name = rawHeader->name;
460 header->mode = getOctal(rawHeader->mode, sizeof(rawHeader->mode));
461 header->uid = getOctal(rawHeader->uid, sizeof(rawHeader->uid));
462 header->gid = getOctal(rawHeader->gid, sizeof(rawHeader->gid));
463 header->size = getOctal(rawHeader->size, sizeof(rawHeader->size));
464 header->mtime = getOctal(rawHeader->mtime, sizeof(rawHeader->mtime));
465 chksum = getOctal(rawHeader->chksum, sizeof(rawHeader->chksum));
466 header->type = rawHeader->typeflag;
467 header->linkname = rawHeader->linkname;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000468 header->devmajor = getOctal(rawHeader->devmajor, sizeof(rawHeader->devmajor));
469 header->devminor = getOctal(rawHeader->devminor, sizeof(rawHeader->devminor));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000470
Erik Andersen298854f2000-03-23 01:09:18 +0000471 /* Check the checksum */
472 sum = ' ' * sizeof(rawHeader->chksum);
473 for ( i = TarChecksumOffset; i > 0; i-- )
474 sum += *s++;
Erik Andersene454fb62000-03-23 04:27:58 +0000475 s += sizeof(rawHeader->chksum);
476 for ( i = (512 - TarChecksumOffset - sizeof(rawHeader->chksum)); i > 0; i-- )
Erik Andersen298854f2000-03-23 01:09:18 +0000477 sum += *s++;
Erik Andersene454fb62000-03-23 04:27:58 +0000478 if (sum == chksum )
Erik Andersen298854f2000-03-23 01:09:18 +0000479 return ( TRUE);
480 return( FALSE);
481}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000482
Erik Andersene49d5ec2000-02-08 19:58:47 +0000483
Erik Andersen1ad302a2000-03-24 00:54:46 +0000484/*
485 * Read a tar file and extract or list the specified files within it.
486 * If the list is empty than all files are extracted or listed.
487 */
488static int readTarFile(const char* tarName, int extractFlag, int listFlag,
489 int tostdoutFlag, int verboseFlag)
490{
491 int status, tarFd=0;
492 int errorFlag=FALSE;
493 TarHeader rawHeader;
494 TarInfo header;
495 int alreadyWarned=FALSE;
496 //int skipFileFlag=FALSE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000497
Erik Andersen1ad302a2000-03-24 00:54:46 +0000498 /* Open the tar file for reading. */
499 if (!strcmp(tarName, "-"))
500 tarFd = fileno(stdin);
501 else
502 tarFd = open(tarName, O_RDONLY);
503 if (tarFd < 0) {
Erik Andersendeea0482000-03-25 23:09:29 +0000504 errorMsg( "Error opening '%s': %s\n", tarName, strerror(errno));
Erik Andersen1ad302a2000-03-24 00:54:46 +0000505 return ( FALSE);
Erik Andersen06936df2000-01-23 02:14:20 +0000506 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000507
Erik Andersene49d5ec2000-02-08 19:58:47 +0000508 /* Set the umask for this process so it doesn't
Erik Andersen1ad302a2000-03-24 00:54:46 +0000509 * screw up permission setting for us later. */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000510 umask(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000511
Erik Andersen1ad302a2000-03-24 00:54:46 +0000512 /* Read the tar file, and iterate over it one file at a time */
513 while ( (status = fullRead(tarFd, (char*)&rawHeader, TAR_BLOCK_SIZE)) == TAR_BLOCK_SIZE ) {
Erik Andersene2729152000-02-18 21:34:17 +0000514
Erik Andersen1ad302a2000-03-24 00:54:46 +0000515 /* First, try to read the header */
Erik Andersen3364d782000-03-28 00:58:14 +0000516 if ( readTarHeader(&rawHeader, &header) == FALSE ) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000517 close( tarFd);
518 if ( *(header.name) == '\0' ) {
519 goto endgame;
520 } else {
521 errorFlag=TRUE;
522 errorMsg("Bad tar header, skipping\n");
523 continue;
524 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000525 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000526 if ( *(header.name) == '\0' )
527 goto endgame;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000528
Erik Andersen1ad302a2000-03-24 00:54:46 +0000529 /* Check for and relativify any absolute paths */
530 if ( *(header.name) == '/' ) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000531
Erik Andersen1ad302a2000-03-24 00:54:46 +0000532 while (*(header.name) == '/')
533 ++*(header.name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000534
Erik Andersen1ad302a2000-03-24 00:54:46 +0000535 if (alreadyWarned == FALSE) {
536 errorMsg("Absolute path detected, removing leading slashes\n");
537 alreadyWarned = TRUE;
538 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000539 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000540
541 /* Special treatment if the list (-t) flag is on */
542 if (verboseFlag == TRUE && extractFlag == FALSE) {
543 int len, len1;
544 char buf[35];
545 struct tm *tm = localtime (&(header.mtime));
546
Erik Andersen6a34b532000-04-07 06:55:38 +0000547 len=printf("%s ", modeString(header.mode));
548 memset(buf, 0, 8*sizeof(char));
549 my_getpwuid(buf, header.uid);
550 if (! *buf)
551 len+=printf("%d", header.uid);
552 else
553 len+=printf("%s", buf);
554 memset(buf, 0, 8*sizeof(char));
555 my_getgrgid(buf, header.gid);
556 if (! *buf)
557 len+=printf("/%-d ", header.gid);
558 else
559 len+=printf("/%-s ", buf);
560
Erik Andersen1ad302a2000-03-24 00:54:46 +0000561 if (header.type==CHRTYPE || header.type==BLKTYPE) {
562 len1=snprintf(buf, sizeof(buf), "%ld,%-ld ",
563 header.devmajor, header.devminor);
564 } else {
565 len1=snprintf(buf, sizeof(buf), "%d ", header.size);
566 }
567 /* Jump through some hoops to make the columns match up */
568 for(;(len+len1)<31;len++)
569 printf(" ");
570 printf(buf);
571
572 /* Use ISO 8610 time format */
573 if (tm) {
574 printf ("%04d-%02d-%02d %02d:%02d:%02d ",
575 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
576 tm->tm_hour, tm->tm_min, tm->tm_sec);
577 }
578 }
579 /* List contents if we are supposed to do that */
580 if (verboseFlag == TRUE || listFlag == TRUE) {
581 /* Now the normal listing */
582 printf("%s", header.name);
Erik Andersen6a34b532000-04-07 06:55:38 +0000583 }
584 if (verboseFlag == TRUE && listFlag == TRUE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000585 /* If this is a link, say so */
586 if (header.type==LNKTYPE)
587 printf(" link to %s", header.linkname);
588 else if (header.type==SYMTYPE)
589 printf(" -> %s", header.linkname);
Erik Andersen6a34b532000-04-07 06:55:38 +0000590 }
591 if (verboseFlag == TRUE || listFlag == TRUE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000592 printf("\n");
593 }
594
595#if 0
596 /* See if we want to restore this file or not */
597 skipFileFlag=FALSE;
598 if (wantFileName(outName) == FALSE) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000599 skipFileFlag = TRUE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000600 }
Erik Andersen05100cd2000-01-16 01:30:52 +0000601#endif
Erik Andersen6a34b532000-04-07 06:55:38 +0000602 /* Remove any clutter lying in our way */
603 unlink( header.name);
Eric Andersencc8ed391999-10-05 16:24:54 +0000604
Erik Andersen1ad302a2000-03-24 00:54:46 +0000605 /* If we got here, we can be certain we have a legitimate
606 * header to work with. So work with it. */
607 switch ( header.type ) {
608 case REGTYPE:
609 case REGTYPE0:
610 /* If the name ends in a '/' then assume it is
611 * supposed to be a directory, and fall through */
612 if (header.name[strlen(header.name)-1] != '/') {
Erik Andersen6a34b532000-04-07 06:55:38 +0000613 if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE)
614 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000615 break;
616 }
617 case DIRTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000618 if (tarExtractDirectory( &header, extractFlag, tostdoutFlag)==FALSE)
619 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000620 break;
621 case LNKTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000622 if (tarExtractHardLink( &header, extractFlag, tostdoutFlag)==FALSE)
623 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000624 break;
625 case SYMTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000626 if (tarExtractSymLink( &header, extractFlag, tostdoutFlag)==FALSE)
627 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000628 break;
629 case CHRTYPE:
630 case BLKTYPE:
631 case FIFOTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000632 if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE)
633 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000634 break;
635 default:
636 close( tarFd);
637 return( FALSE);
638 }
639 }
640 close(tarFd);
641 if (status > 0) {
642 /* Bummer - we read a partial header */
Erik Andersendeea0482000-03-25 23:09:29 +0000643 errorMsg( "Error reading '%s': %s\n", tarName, strerror(errno));
Erik Andersen1ad302a2000-03-24 00:54:46 +0000644 return ( FALSE);
645 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000646 else if (errorFlag==TRUE) {
647 errorMsg( "tar: Error exit delayed from previous errors\n");
648 return( FALSE);
649 } else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000650 return( status);
651
652 /* Stuff to do when we are done */
653endgame:
654 close( tarFd);
655 if ( *(header.name) == '\0' ) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000656 if (errorFlag==TRUE)
657 errorMsg( "tar: Error exit delayed from previous errors\n");
658 else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000659 return( TRUE);
660 }
661 return( FALSE);
662}
663
Erik Andersen6acaa402000-03-26 14:03:20 +0000664
665#ifdef BB_FEATURE_TAR_CREATE
666
Erik Andersen68a9ea42000-04-04 18:39:50 +0000667/* Some info to be carried along when creating a new tarball */
668struct TarBallInfo
669{
670 char* fileName; /* File name of the tarball */
671 int tarFd; /* Open-for-write file descriptor
672 for the tarball */
673 struct stat statBuf; /* Stat info for the tarball, letting
674 us know the inode and device that the
675 tarball lives, so we can avoid trying
676 to include the tarball into itself */
677 int verboseFlag; /* Whether to print extra stuff or not */
678};
679typedef struct TarBallInfo TarBallInfo;
680
681
Erik Andersen6acaa402000-03-26 14:03:20 +0000682/* Put an octal string into the specified buffer.
683 * The number is zero and space padded and possibly null padded.
684 * Returns TRUE if successful. */
685static int putOctal (char *cp, int len, long value)
686{
687 int tempLength;
Erik Andersen6acaa402000-03-26 14:03:20 +0000688 char tempBuffer[32];
Erik Andersen5661fe02000-04-05 01:00:52 +0000689 char *tempString = tempBuffer;
Erik Andersen6acaa402000-03-26 14:03:20 +0000690
691 /* Create a string of the specified length with an initial space,
692 * leading zeroes and the octal number, and a trailing null. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000693 sprintf (tempString, "%0*lo", len - 1, value);
Erik Andersen6acaa402000-03-26 14:03:20 +0000694
695 /* If the string is too large, suppress the leading space. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000696 tempLength = strlen (tempString) + 1;
Erik Andersen6acaa402000-03-26 14:03:20 +0000697 if (tempLength > len) {
698 tempLength--;
699 tempString++;
700 }
701
702 /* If the string is still too large, suppress the trailing null. */
703 if (tempLength > len)
704 tempLength--;
705
706 /* If the string is still too large, fail. */
707 if (tempLength > len)
708 return FALSE;
709
710 /* Copy the string to the field. */
711 memcpy (cp, tempString, len);
712
713 return TRUE;
714}
715
Erik Andersen68a9ea42000-04-04 18:39:50 +0000716/* Write out a tar header for the specified file/directory/whatever */
Erik Andersen3364d782000-03-28 00:58:14 +0000717static int
Erik Andersen5661fe02000-04-05 01:00:52 +0000718writeTarHeader(struct TarBallInfo *tbInfo, const char *fileName, struct stat *statbuf)
Erik Andersen6acaa402000-03-26 14:03:20 +0000719{
Erik Andersen5661fe02000-04-05 01:00:52 +0000720 long chksum=0;
721 struct TarHeader header;
722 const unsigned char *cp = (const unsigned char *) &header;
723 ssize_t size = sizeof(struct TarHeader);
724
725 memset( &header, 0, size);
Erik Andersen3364d782000-03-28 00:58:14 +0000726
Erik Andersen68a9ea42000-04-04 18:39:50 +0000727 if (*fileName=='/') {
728 static int alreadyWarned=FALSE;
729 if (alreadyWarned==FALSE) {
730 errorMsg("tar: Removing leading '/' from member names\n");
731 alreadyWarned=TRUE;
732 }
Erik Andersen5661fe02000-04-05 01:00:52 +0000733 strcpy(header.name, fileName+1);
Erik Andersen3364d782000-03-28 00:58:14 +0000734 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000735 else {
Erik Andersen5661fe02000-04-05 01:00:52 +0000736 strcpy(header.name, fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000737 }
Erik Andersen5661fe02000-04-05 01:00:52 +0000738 putOctal(header.mode, sizeof(header.mode), statbuf->st_mode);
739 putOctal(header.uid, sizeof(header.uid), statbuf->st_uid);
740 putOctal(header.gid, sizeof(header.gid), statbuf->st_gid);
741 putOctal(header.size, sizeof(header.size), 0); /* Regular file size is handled later */
742 putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime);
743 strncpy(header.magic, TAR_MAGIC TAR_VERSION,
744 TAR_MAGIC_LEN + TAR_VERSION_LEN );
Erik Andersen68a9ea42000-04-04 18:39:50 +0000745
Erik Andersen5661fe02000-04-05 01:00:52 +0000746 my_getpwuid(header.uname, statbuf->st_uid);
747 /* Put some sort of sane fallback in place... */
748 if (! *header.uname)
749 strncpy(header.uname, "root", 5);
750 my_getgrgid(header.gname, statbuf->st_gid);
751 if (! *header.uname)
752 strncpy(header.uname, "root", 5);
753
754 // FIXME: (or most likely not) I break Hard Links
Erik Andersen68a9ea42000-04-04 18:39:50 +0000755 if (S_ISLNK(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000756 char buffer[BUFSIZ];
757 header.typeflag = SYMTYPE;
758 if ( readlink(fileName, buffer, sizeof(buffer) - 1) < 0) {
759 errorMsg("Error reading symlink '%s': %s\n", header.name, strerror(errno));
760 return ( FALSE);
761 }
762 strncpy(header.linkname, buffer, sizeof(header.linkname));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000763 } else if (S_ISDIR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000764 header.typeflag = DIRTYPE;
765 strncat(header.name, "/", sizeof(header.name));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000766 } else if (S_ISCHR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000767 header.typeflag = CHRTYPE;
768 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
769 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000770 } else if (S_ISBLK(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000771 header.typeflag = BLKTYPE;
772 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
773 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000774 } else if (S_ISFIFO(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000775 header.typeflag = FIFOTYPE;
776 } else if (S_ISREG(statbuf->st_mode)) {
777 header.typeflag = REGTYPE;
778 putOctal(header.size, sizeof(header.size), statbuf->st_size);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000779 } else {
Erik Andersen5661fe02000-04-05 01:00:52 +0000780 errorMsg("tar: %s: Unknown file type\n", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000781 return ( FALSE);
782 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000783
Erik Andersen5661fe02000-04-05 01:00:52 +0000784 /* Calculate and store the checksum (i.e. the sum of all of the bytes of
785 * the header). The checksum field must be filled with blanks for the
786 * calculation. The checksum field is formatted differently from the
787 * other fields: it has [6] digits, a null, then a space -- rather than
788 * digits, followed by a null like the other fields... */
789 memset(header.chksum, ' ', sizeof(header.chksum));
790 cp = (const unsigned char *) &header;
791 while (size-- > 0)
792 chksum += *cp++;
793 putOctal(header.chksum, 7, chksum);
794
795 /* Now write the header out to disk */
796 if ((size=fullWrite(tbInfo->tarFd, (char*)&header, sizeof(struct TarHeader))) < 0) {
797 errorMsg(io_error, fileName, strerror(errno));
798 return ( FALSE);
799 }
800 /* Pad the header up to the tar block size */
801 for (; size<TAR_BLOCK_SIZE; size++) {
802 write(tbInfo->tarFd, "\0", 1);
803 }
804 /* Now do the verbose thing (or not) */
805 if (tbInfo->verboseFlag==TRUE)
806 fprintf(stdout, "%s\n", header.name);
Erik Andersen3364d782000-03-28 00:58:14 +0000807
808 return ( TRUE);
809}
810
811
Erik Andersen68a9ea42000-04-04 18:39:50 +0000812static int writeFileToTarball(const char *fileName, struct stat *statbuf, void* userData)
Erik Andersen3364d782000-03-28 00:58:14 +0000813{
Erik Andersen68a9ea42000-04-04 18:39:50 +0000814 struct TarBallInfo *tbInfo = (struct TarBallInfo *)userData;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000815
816 /* It is against the rules to archive a socket */
817 if (S_ISSOCK(statbuf->st_mode)) {
818 errorMsg("tar: %s: socket ignored\n", fileName);
819 return( TRUE);
820 }
821
822 /* It is a bad idea to store the archive we are in the process of creating,
823 * so check the device and inode to be sure that this particular file isn't
824 * the new tarball */
825 if (tbInfo->statBuf.st_dev == statbuf->st_dev &&
826 tbInfo->statBuf.st_ino == statbuf->st_ino) {
827 errorMsg("tar: %s: file is the archive; skipping\n", fileName);
828 return( TRUE);
829 }
830
Erik Andersen5661fe02000-04-05 01:00:52 +0000831 if (writeTarHeader(tbInfo, fileName, statbuf)==FALSE) {
832 return( FALSE);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000833 }
Erik Andersen5661fe02000-04-05 01:00:52 +0000834
835 /* Now, if the file is a regular file, copy it out to the tarball */
836 if (S_ISREG(statbuf->st_mode)) {
837 int inputFileFd;
838 char buffer[BUFSIZ];
839 ssize_t size=0, readSize=0;
840
841 /* open the file we want to archive, and make sure all is well */
842 if ((inputFileFd = open(fileName, O_RDONLY)) < 0) {
843 errorMsg("tar: %s: Cannot open: %s\n", fileName, strerror(errno));
844 return( FALSE);
845 }
846
847 /* write the file to the archive */
848 while ( (size = fullRead(inputFileFd, buffer, sizeof(buffer))) > 0 ) {
849 if (fullWrite(tbInfo->tarFd, buffer, size) != size ) {
850 /* Output file seems to have a problem */
851 errorMsg(io_error, fileName, strerror(errno));
852 return( FALSE);
853 }
854 readSize+=size;
855 }
856 if (size == -1) {
857 errorMsg(io_error, fileName, strerror(errno));
858 return( FALSE);
859 }
860 /* Pad the file up to the tar block size */
861 for (; (readSize%TAR_BLOCK_SIZE) != 0; readSize++) {
862 write(tbInfo->tarFd, "\0", 1);
863 }
864 close( inputFileFd);
865 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000866
867 return( TRUE);
Erik Andersen6acaa402000-03-26 14:03:20 +0000868}
869
Erik Andersen68a9ea42000-04-04 18:39:50 +0000870static int writeTarFile(const char* tarName, int tostdoutFlag,
871 int verboseFlag, int argc, char **argv)
Erik Andersen6acaa402000-03-26 14:03:20 +0000872{
Erik Andersen3364d782000-03-28 00:58:14 +0000873 int tarFd=-1;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000874 int errorFlag=FALSE;
Erik Andersen5661fe02000-04-05 01:00:52 +0000875 ssize_t size;
Erik Andersen6acaa402000-03-26 14:03:20 +0000876 //int skipFileFlag=FALSE;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000877 struct TarBallInfo tbInfo;
878 tbInfo.verboseFlag = verboseFlag;
Erik Andersen3364d782000-03-28 00:58:14 +0000879
880 /* Make sure there is at least one file to tar up. */
881 if (argc <= 0)
882 fatalError("tar: Cowardly refusing to create an empty archive\n");
Erik Andersen6acaa402000-03-26 14:03:20 +0000883
884 /* Open the tar file for writing. */
Erik Andersen3364d782000-03-28 00:58:14 +0000885 if (tostdoutFlag == TRUE)
Erik Andersen68a9ea42000-04-04 18:39:50 +0000886 tbInfo.tarFd = fileno(stdout);
Erik Andersen6acaa402000-03-26 14:03:20 +0000887 else
Erik Andersen68a9ea42000-04-04 18:39:50 +0000888 tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
889 if (tbInfo.tarFd < 0) {
890 errorMsg( "tar: Error opening '%s': %s\n", tarName, strerror(errno));
Erik Andersen6acaa402000-03-26 14:03:20 +0000891 return ( FALSE);
892 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000893 /* Store the stat info for the tarball's file, so
894 * can avoid including the tarball into itself.... */
895 if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0)
Erik Andersen3364d782000-03-28 00:58:14 +0000896 fatalError(io_error, tarName, strerror(errno));
Erik Andersen6acaa402000-03-26 14:03:20 +0000897
898 /* Set the umask for this process so it doesn't
899 * screw up permission setting for us later. */
900 umask(0);
901
902 /* Read the directory/files and iterate over them one at a time */
Erik Andersen3364d782000-03-28 00:58:14 +0000903 while (argc-- > 0) {
904 if (recursiveAction(*argv++, TRUE, FALSE, FALSE,
Erik Andersen68a9ea42000-04-04 18:39:50 +0000905 writeFileToTarball, writeFileToTarball,
906 (void*) &tbInfo) == FALSE) {
907 errorFlag = TRUE;
Erik Andersen3364d782000-03-28 00:58:14 +0000908 }
Erik Andersen6acaa402000-03-26 14:03:20 +0000909 }
Erik Andersen5661fe02000-04-05 01:00:52 +0000910 /* Write two empty blocks to the end of the archive */
911 for (size=0; size<(2*TAR_BLOCK_SIZE); size++) {
912 write(tbInfo.tarFd, "\0", 1);
913 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000914 /* Hang up the tools, close up shop, head home */
Erik Andersen6acaa402000-03-26 14:03:20 +0000915 close(tarFd);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000916 if (errorFlag == TRUE) {
917 errorMsg("tar: Error exit delayed from previous errors\n");
918 return(FALSE);
919 }
Erik Andersen6acaa402000-03-26 14:03:20 +0000920 return( TRUE);
921}
922
923
924#endif
925