blob: 8f83d04a6058d3acb7977ee0a981f780514051ae [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/*
Eric Andersenc4996011999-10-20 22:08:37 +00003 * Mini tar implementation for busybox based on code taken from sash.
4 *
Eric Andersencc8ed391999-10-05 16:24:54 +00005 * Copyright (c) 1999 by David I. Bell
6 * Permission is granted to use, distribute, or modify this source,
7 * provided that this copyright notice remains intact.
8 *
Eric Andersencc8ed391999-10-05 16:24:54 +00009 * Permission to distribute this code under the GPL has been granted.
Eric Andersenc4996011999-10-20 22:08:37 +000010 *
Eric Andersen3cf52d11999-10-12 22:26:06 +000011 * Modified for busybox by Erik Andersen <andersee@debian.org>
Eric Andersenc4996011999-10-20 22:08:37 +000012 * Adjusted to grok stdin/stdout options.
13 *
Eric Andersen96bcfd31999-11-12 01:30:18 +000014 * Modified to handle device special files by Matt Porter
15 * <porter@debian.org>
16 *
Eric Andersenc4996011999-10-20 22:08:37 +000017 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 *
Eric Andersencc8ed391999-10-05 16:24:54 +000031 */
32
33
34#include "internal.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000035#include <stdio.h>
36#include <dirent.h>
37#include <errno.h>
38#include <fcntl.h>
39#include <signal.h>
40#include <time.h>
Erik Andersen7dc16072000-01-04 01:10:25 +000041#include <utime.h>
Eric Andersen96bcfd31999-11-12 01:30:18 +000042#include <sys/types.h>
Eric Andersen08b10341999-11-19 02:38:58 +000043#include <sys/sysmacros.h>
Erik Andersene49d5ec2000-02-08 19:58:47 +000044#include <sys/param.h> /* for PATH_MAX */
Eric Andersencc8ed391999-10-05 16:24:54 +000045
Eric Andersene77ae3a1999-10-19 20:03:34 +000046
Erik Andersen05100cd2000-01-16 01:30:52 +000047#ifdef BB_FEATURE_TAR_CREATE
48
Eric Andersene77ae3a1999-10-19 20:03:34 +000049static const char tar_usage[] =
Erik Andersene49d5ec2000-02-08 19:58:47 +000050 "tar -[cxtvOf] [tarFileName] [FILE] ...\n\n"
51 "Create, extract, or list files from a tar file.\n\n"
52 "Options:\n"
53
54 "\tc=create, x=extract, t=list contents, v=verbose,\n"
55 "\tO=extract to stdout, f=tarfile or \"-\" for stdin\n";
Eric Andersene77ae3a1999-10-19 20:03:34 +000056
Erik Andersen05100cd2000-01-16 01:30:52 +000057#else
58
59static const char tar_usage[] =
Erik Andersene49d5ec2000-02-08 19:58:47 +000060 "tar -[xtvOf] [tarFileName] [FILE] ...\n\n"
61 "Extract, or list files stored in a tar file. This\n"
62 "version of tar does not support creation of tar files.\n\n"
63 "Options:\n"
64
65 "\tx=extract, t=list contents, v=verbose,\n"
66 "\tO=extract to stdout, f=tarfile or \"-\" for stdin\n";
Erik Andersen05100cd2000-01-16 01:30:52 +000067
68#endif
Eric Andersene77ae3a1999-10-19 20:03:34 +000069
70
Erik Andersen298854f2000-03-23 01:09:18 +000071/* Tar file constants */
Eric Andersencc8ed391999-10-05 16:24:54 +000072
73
Erik Andersen298854f2000-03-23 01:09:18 +000074/* POSIX tar Header Block, from POSIX 1003.1-1990 */
75struct TarHeader
76{
77 /* byte offset */
78 char name[100]; /* 0 */
79 char mode[8]; /* 100 */
80 char uid[8]; /* 108 */
81 char gid[8]; /* 116 */
82 char size[12]; /* 124 */
83 char mtime[12]; /* 136 */
84 char chksum[8]; /* 148 */
85 char typeflag; /* 156 */
86 char linkname[100]; /* 157 */
87 char magic[6]; /* 257 */
88 char version[2]; /* 263 */
89 char uname[32]; /* 265 */
90 char gname[32]; /* 297 */
91 char devmajor[8]; /* 329 */
92 char devminor[8]; /* 337 */
93 char prefix[155]; /* 345 */
94 /* padding 500 */
95};
96typedef struct TarHeader TarHeader;
Eric Andersencc8ed391999-10-05 16:24:54 +000097
Eric Andersencc8ed391999-10-05 16:24:54 +000098
Erik Andersen298854f2000-03-23 01:09:18 +000099/* A few useful constants */
100#define TAR_MAGIC "ustar" /* ustar and a null */
101#define TAR_VERSION "00" /* 00 and no null */
102#define TAR_MAGIC_LEN 6
103#define TAR_VERSION_LEN 2
104#define TAR_NAME_LEN 100
105#define TAR_BLOCK_SIZE 512
106
107/* A nice enum with all the possible tar file content types */
108enum TarFileType
109{
110 REGTYPE = '0', /* regular file */
111 REGTYPE0 = '\0', /* regular file (ancient bug compat)*/
112 LNKTYPE = '1', /* hard link */
113 SYMTYPE = '2', /* symbolic link */
114 CHRTYPE = '3', /* character special */
115 BLKTYPE = '4', /* block special */
116 DIRTYPE = '5', /* directory */
117 FIFOTYPE = '6', /* FIFO special */
118 CONTTYPE = '7', /* reserved */
119};
120typedef enum TarFileType TarFileType;
121
122/* This struct ignores magic, non-numeric user name,
123 * non-numeric group name, and the checksum, since
124 * these are all ignored by BusyBox tar. */
125struct TarInfo
126{
127 int tarFd; /* An open file descriptor for reading from the tarball */
128 char * name; /* File name */
129 mode_t mode; /* Unix mode, including device bits. */
130 uid_t uid; /* Numeric UID */
131 gid_t gid; /* Numeric GID */
132 size_t size; /* Size of file */
133 time_t mtime; /* Last-modified time */
134 enum TarFileType type; /* Regular, directory, link, etc */
135 char * linkname; /* Name for symbolic and hard links */
136 dev_t device; /* Special device for mknod() */
137};
138typedef struct TarInfo TarInfo;
139
140/* Static data */
141static const unsigned long TarChecksumOffset = (const unsigned long)&(((TarHeader *)0)->chksum);
Eric Andersencc8ed391999-10-05 16:24:54 +0000142
143
144/*
145 * Static data.
146 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000147static int inHeader; // <- check me
Eric Andersend73dc5b1999-11-10 23:13:02 +0000148static int badHeader;
Eric Andersend73dc5b1999-11-10 23:13:02 +0000149static int skipFileFlag;
150static int warnedRoot;
151static int eofFlag;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000152static long dataCc;
153static int outFd;
Erik Andersen7dc16072000-01-04 01:10:25 +0000154static const char *outName;
Eric Andersencc8ed391999-10-05 16:24:54 +0000155
Erik Andersen7dc16072000-01-04 01:10:25 +0000156static int mode;
157static int uid;
158static int gid;
159static time_t mtime;
Eric Andersencc8ed391999-10-05 16:24:54 +0000160
161/*
162 * Static data associated with the tar file.
163 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000164static int tarFd;
165static dev_t tarDev;
166static ino_t tarInode;
Eric Andersencc8ed391999-10-05 16:24:54 +0000167
168
169/*
170 * Local procedures to restore files from a tar file.
171 */
Erik Andersen298854f2000-03-23 01:09:18 +0000172static int readTarFile(const char* tarName, int extractFlag, int listFlag,
173 int tostdoutFlag, int verboseFlag);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000174static void readData(const char *cp, int count);
175static long getOctal(const char *cp, int len);
Erik Andersen298854f2000-03-23 01:09:18 +0000176static int parseTarHeader(struct TarHeader *rawHeader, struct TarInfo *header);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000177static int wantFileName(const char *fileName,
Erik Andersen298854f2000-03-23 01:09:18 +0000178 int argc, char **argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000179
Erik Andersen05100cd2000-01-16 01:30:52 +0000180#ifdef BB_FEATURE_TAR_CREATE
Eric Andersencc8ed391999-10-05 16:24:54 +0000181/*
182 * Local procedures to save files into a tar file.
183 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000184static void saveFile(const char *fileName, int seeLinks);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000185static void saveRegularFile(const char *fileName,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000186 const struct stat *statbuf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000187static void saveDirectory(const char *fileName,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000188 const struct stat *statbuf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000189static void writeHeader(const char *fileName, const struct stat *statbuf);
Erik Andersen298854f2000-03-23 01:09:18 +0000190static void writeTarFile(int argc, char **argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000191static void writeTarBlock(const char *buf, int len);
192static int putOctal(char *cp, int len, long value);
Eric Andersencc8ed391999-10-05 16:24:54 +0000193
Erik Andersen05100cd2000-01-16 01:30:52 +0000194#endif
195
Eric Andersencc8ed391999-10-05 16:24:54 +0000196
Erik Andersene49d5ec2000-02-08 19:58:47 +0000197extern int tar_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000198{
Erik Andersen298854f2000-03-23 01:09:18 +0000199 const char *tarName=NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000200 const char *options;
Erik Andersen298854f2000-03-23 01:09:18 +0000201 int listFlag = FALSE;
202 int extractFlag = FALSE;
203 int createFlag = FALSE;
204 int verboseFlag = FALSE;
205 int tostdoutFlag = FALSE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000206
Erik Andersene49d5ec2000-02-08 19:58:47 +0000207 argc--;
208 argv++;
Eric Andersencc8ed391999-10-05 16:24:54 +0000209
Erik Andersene49d5ec2000-02-08 19:58:47 +0000210 if (argc < 1)
211 usage(tar_usage);
Eric Andersencc8ed391999-10-05 16:24:54 +0000212
Erik Andersen298854f2000-03-23 01:09:18 +0000213 /* Parse options */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000214 if (**argv == '-')
215 options = (*argv++) + 1;
216 else
217 options = (*argv++);
218 argc--;
Eric Andersencc8ed391999-10-05 16:24:54 +0000219
Erik Andersene49d5ec2000-02-08 19:58:47 +0000220 for (; *options; options++) {
221 switch (*options) {
222 case 'f':
Erik Andersen298854f2000-03-23 01:09:18 +0000223 if (tarName != NULL)
224 fatalError( "Only one 'f' option allowed\n");
Erik Andersende552872000-01-23 01:34:05 +0000225
Erik Andersene49d5ec2000-02-08 19:58:47 +0000226 tarName = *argv++;
Erik Andersen298854f2000-03-23 01:09:18 +0000227 if (tarName == NULL)
228 fatalError( "Option requires an argument: No file specified\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000229 argc--;
Erik Andersende552872000-01-23 01:34:05 +0000230
Erik Andersene49d5ec2000-02-08 19:58:47 +0000231 break;
Erik Andersende552872000-01-23 01:34:05 +0000232
Erik Andersene49d5ec2000-02-08 19:58:47 +0000233 case 't':
234 if (extractFlag == TRUE || createFlag == TRUE)
235 goto flagError;
236 listFlag = TRUE;
237 break;
Erik Andersende552872000-01-23 01:34:05 +0000238
Erik Andersene49d5ec2000-02-08 19:58:47 +0000239 case 'x':
240 if (listFlag == TRUE || createFlag == TRUE)
241 goto flagError;
242 extractFlag = TRUE;
243 break;
244 case 'c':
245 if (extractFlag == TRUE || listFlag == TRUE)
246 goto flagError;
247 createFlag = TRUE;
248 break;
Erik Andersende552872000-01-23 01:34:05 +0000249
Erik Andersene49d5ec2000-02-08 19:58:47 +0000250 case 'v':
251 verboseFlag = TRUE;
252 break;
Erik Andersende552872000-01-23 01:34:05 +0000253
Erik Andersene49d5ec2000-02-08 19:58:47 +0000254 case 'O':
255 tostdoutFlag = TRUE;
256 break;
Erik Andersende552872000-01-23 01:34:05 +0000257
Erik Andersene49d5ec2000-02-08 19:58:47 +0000258 case '-':
259 usage(tar_usage);
260 break;
Erik Andersende552872000-01-23 01:34:05 +0000261
Erik Andersene49d5ec2000-02-08 19:58:47 +0000262 default:
Erik Andersen298854f2000-03-23 01:09:18 +0000263 fatalError( "Unknown tar flag '%c'\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +0000264 "Try `tar --help' for more information\n", *options);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000265 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000266 }
267
Erik Andersene49d5ec2000-02-08 19:58:47 +0000268 /*
269 * Do the correct type of action supplying the rest of the
270 * command line arguments as the list of files to process.
271 */
272 if (createFlag == TRUE) {
Erik Andersende552872000-01-23 01:34:05 +0000273#ifndef BB_FEATURE_TAR_CREATE
Erik Andersen298854f2000-03-23 01:09:18 +0000274 fatalError( "This version of tar was not compiled with tar creation support.\n");
Erik Andersende552872000-01-23 01:34:05 +0000275#else
Erik Andersen298854f2000-03-23 01:09:18 +0000276 exit(writeTarFile(argc, argv));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000277#endif
278 } else {
Erik Andersen298854f2000-03-23 01:09:18 +0000279 exit(readTarFile(tarName, extractFlag, listFlag, tostdoutFlag, verboseFlag));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000280 }
Erik Andersende552872000-01-23 01:34:05 +0000281
Erik Andersene49d5ec2000-02-08 19:58:47 +0000282 flagError:
Erik Andersen298854f2000-03-23 01:09:18 +0000283 fatalError( "Exactly one of 'c', 'x' or 't' must be specified\n");
284}
285
286static void
287tarExtractRegularFile(TarInfo *header, int extractFlag, int listFlag, int tostdoutFlag, int verboseFlag)
288{
289
Eric Andersencc8ed391999-10-05 16:24:54 +0000290}
291
292
293/*
294 * Read a tar file and extract or list the specified files within it.
295 * If the list is empty than all files are extracted or listed.
296 */
Erik Andersen298854f2000-03-23 01:09:18 +0000297static int readTarFile(const char* tarName, int extractFlag, int listFlag,
298 int tostdoutFlag, int verboseFlag)
Eric Andersencc8ed391999-10-05 16:24:54 +0000299{
Erik Andersen298854f2000-03-23 01:09:18 +0000300 int status, tarFd=0;
301 int errorFlag=FALSE;
302 TarHeader rawHeader;
303 TarInfo header;
Eric Andersencc8ed391999-10-05 16:24:54 +0000304
Erik Andersen298854f2000-03-23 01:09:18 +0000305 /* Open the tar file for reading. */
306 if (!strcmp(tarName, "-"))
Erik Andersene49d5ec2000-02-08 19:58:47 +0000307 tarFd = fileno(stdin);
Erik Andersen298854f2000-03-23 01:09:18 +0000308 else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000309 tarFd = open(tarName, O_RDONLY);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000310 if (tarFd < 0) {
Erik Andersen298854f2000-03-23 01:09:18 +0000311 errorMsg( "Error opening '%s': %s", tarName, strerror(errno));
312 return ( FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000313 }
314
Erik Andersen298854f2000-03-23 01:09:18 +0000315 /* Read the tar file */
316 while ( (status = fullRead(tarFd, (char*)&rawHeader, TAR_BLOCK_SIZE)) == TAR_BLOCK_SIZE ) {
317 /* Now see if the header looks ok */
318 if ( parseTarHeader(&rawHeader, &header) == FALSE ) {
319 close( tarFd);
320 if ( *(header.name) == '\0' ) {
321 goto endgame;
322 } else {
323 errorFlag=TRUE;
324 errorMsg("Bad tar header, skipping\n");
325 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000326 }
327 }
Erik Andersen298854f2000-03-23 01:09:18 +0000328 if ( *(header.name) == '\0' )
329 goto endgame;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000330
Erik Andersen298854f2000-03-23 01:09:18 +0000331 /* If we got here, we can be certain we have a legitimate
332 * header to work with. So work with it. */
333 switch ( header.type ) {
334 case REGTYPE:
335 case REGTYPE0:
336 /* If the name ends in a '/' then assume it is
337 * supposed to be a directory, and fall through */
338 if (header.name[strlen(header.name)-1] != '/') {
339 tarExtractRegularFile(&header, extractFlag, listFlag, tostdoutFlag, verboseFlag);
340 break;
341 }
342#if 0
343 case Directory:
344 tarExtractDirectory( &header, extractFlag, listFlag, tostdoutFlag, verboseFlag);
345 break;
346 case HardLink:
347 tarExtractHardLink( &header, extractFlag, listFlag, tostdoutFlag, verboseFlag);
348 break;
349 case SymbolicLink:
350 tarExtractSymLink( &header, extractFlag, listFlag, tostdoutFlag, verboseFlag);
351 break;
352 case CharacterDevice:
353 case BlockDevice:
354 case FIFO:
355 tarExtractSpecial( &header, extractFlag, listFlag, tostdoutFlag, verboseFlag);
356 break;
357#endif
358 default:
359 close( tarFd);
360 return( FALSE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000361 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000362 }
Erik Andersen298854f2000-03-23 01:09:18 +0000363
364 close(tarFd);
365 if (status > 0) {
366 /* Bummer - we read a partial header */
367 errorMsg( "Error reading '%s': %s", tarName, strerror(errno));
368 return ( FALSE);
369 }
370 else
371 return( status);
372
373 /* Stuff we do when we know we are done with the file */
374endgame:
375 close( tarFd);
376 if ( *(header.name) == '\0' ) {
377 if (errorFlag==FALSE)
378 return( TRUE);
379 }
380 return( FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000381}
382
Eric Andersencc8ed391999-10-05 16:24:54 +0000383/*
Erik Andersen298854f2000-03-23 01:09:18 +0000384 * Read an octal value in a field of the specified width, with optional
385 * spaces on both sides of the number and with an optional null character
386 * at the end. Returns -1 on an illegal format.
Eric Andersencc8ed391999-10-05 16:24:54 +0000387 */
Erik Andersen298854f2000-03-23 01:09:18 +0000388static long getOctal(const char *cp, int size)
Eric Andersencc8ed391999-10-05 16:24:54 +0000389{
Erik Andersen298854f2000-03-23 01:09:18 +0000390 long val = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000391
Erik Andersen298854f2000-03-23 01:09:18 +0000392 for(;(size > 0) && (*cp == ' '); cp++, size--);
393 if ((size == 0) || !isOctal(*cp))
394 return -1;
395 for(; (size > 0) && isOctal(*cp); size--) {
396 val = val * 8 + *cp++ - '0';
Eric Andersencc8ed391999-10-05 16:24:54 +0000397 }
Erik Andersen298854f2000-03-23 01:09:18 +0000398 for (;(size > 0) && (*cp == ' '); cp++, size--);
399 if ((size > 0) && *cp)
400 return -1;
401 return val;
402}
Eric Andersencc8ed391999-10-05 16:24:54 +0000403
Erik Andersen298854f2000-03-23 01:09:18 +0000404/* Parse the tar header and fill in the nice struct with the details */
405static int
406parseTarHeader(struct TarHeader *rawHeader, struct TarInfo *header)
407{
408 long major, minor, chksum, sum;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000409
Erik Andersen298854f2000-03-23 01:09:18 +0000410 header->name = rawHeader->name;
411 header->mode = getOctal(rawHeader->mode, sizeof(rawHeader->mode));
412 header->uid = getOctal(rawHeader->uid, sizeof(rawHeader->uid));
413 header->gid = getOctal(rawHeader->gid, sizeof(rawHeader->gid));
414 header->size = getOctal(rawHeader->size, sizeof(rawHeader->size));
415 header->mtime = getOctal(rawHeader->mtime, sizeof(rawHeader->mtime));
416 chksum = getOctal(rawHeader->chksum, sizeof(rawHeader->chksum));
417 header->type = rawHeader->typeflag;
418 header->linkname = rawHeader->linkname;
419 header->device = MAJOR(getOctal(rawHeader->devmajor, sizeof(rawHeader->devmajor))) |
420 MINOR(getOctal(rawHeader->devminor, sizeof(rawHeader->devminor)));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000421
Erik Andersen298854f2000-03-23 01:09:18 +0000422 /* Check the checksum */
423 sum = ' ' * sizeof(rawHeader->chksum);
424 for ( i = TarChecksumOffset; i > 0; i-- )
425 sum += *s++;
426 s += sizeof(h->chksum);
427 for ( i = (512 - TarChecksumOffset - sizeof(h->chksum)); i > 0; i-- )
428 sum += *s++;
429 if (sum == checksum )
430 return ( TRUE);
431 return( FALSE);
432}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000433
Erik Andersen298854f2000-03-23 01:09:18 +0000434#if 0
435 if ((header->mode < 0) || (header->uid < 0) ||
436 (header->gid < 0) || (header->size < 0)) {
437 errorMsg(stderr, "Bad tar header, skipping\n");
438 return( FALSE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000439 }
440
441 badHeader = FALSE;
442 skipFileFlag = FALSE;
443 devFileFlag = FALSE;
444
445 /*
446 * Check for the file modes.
447 */
448 hardLink = ((hp->typeFlag == TAR_TYPE_HARD_LINK) ||
449 (hp->typeFlag == TAR_TYPE_HARD_LINK - '0'));
450
451 softLink = ((hp->typeFlag == TAR_TYPE_SOFT_LINK) ||
452 (hp->typeFlag == TAR_TYPE_SOFT_LINK - '0'));
453
454 /*
455 * Check for a directory.
456 */
457 if (outName[strlen(outName) - 1] == '/')
458 mode |= S_IFDIR;
459
460 /*
461 * Check for absolute paths in the file.
462 * If we find any, then warn the user and make them relative.
463 */
464 if (*outName == '/') {
465 while (*outName == '/')
466 outName++;
467
468 if (warnedRoot == FALSE) {
469 fprintf(stderr,
470 "Absolute path detected, removing leading slashes\n");
471 }
472
473 warnedRoot = TRUE;
474 }
475
476 /*
477 * See if we want this file to be restored.
478 * If not, then set up to skip it.
479 */
Erik Andersen298854f2000-03-23 01:09:18 +0000480 if (wantFileName(outName, argc, argv) == FALSE) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000481 if (!hardLink && !softLink && (S_ISREG(mode) || S_ISCHR(mode)
482 || S_ISBLK(mode) || S_ISSOCK(mode)
483 || S_ISFIFO(mode))) {
484 inHeader = (size == 0) ? TRUE : FALSE;
485 dataCc = size;
486 }
487
488 skipFileFlag = TRUE;
489
490 return;
491 }
492
493 /*
494 * This file is to be handled.
495 * If we aren't extracting then just list information about the file.
496 */
497 if (extractFlag == FALSE) {
498 if (verboseFlag == TRUE) {
499 printf("%s %3d/%-d ", modeString(mode), uid, gid);
500 if (S_ISCHR(mode) || S_ISBLK(mode))
501 printf("%4d,%4d %s ", major, minor, timeString(mtime));
502 else
503 printf("%9ld %s ", size, timeString(mtime));
504 }
505 printf("%s", outName);
506
507 if (hardLink)
508 printf(" (link to \"%s\")", hp->linkName);
509 else if (softLink)
510 printf(" (symlink to \"%s\")", hp->linkName);
511 else if (S_ISREG(mode) || S_ISCHR(mode) || S_ISBLK(mode) ||
512 S_ISSOCK(mode) || S_ISFIFO(mode)) {
513 inHeader = (size == 0) ? TRUE : FALSE;
514 dataCc = size;
515 }
516
517 printf("\n");
518
519 return;
520 }
521
522 /*
523 * We really want to extract the file.
524 */
525 if (verboseFlag == TRUE)
526 printf("x %s\n", outName);
527
528 if (hardLink) {
529 if (link(hp->linkName, outName) < 0) {
530 perror(outName);
531 return;
532 }
533 /* Set the file time */
534 utb.actime = mtime;
535 utb.modtime = mtime;
536 utime(outName, &utb);
537 /* Set the file permissions */
538 chown(outName, uid, gid);
539 chmod(outName, mode);
540 return;
541 }
542
543 if (softLink) {
544#ifdef S_ISLNK
545 if (symlink(hp->linkName, outName) < 0) {
546 perror(outName);
547 return;
548 }
549 /* Try to change ownership of the symlink.
550 * If libs doesn't support that, don't bother.
551 * Changing the pointed-to file is the Wrong Thing(tm).
552 */
Erik Andersen7dc16072000-01-04 01:10:25 +0000553#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000554 lchown(outName, uid, gid);
Erik Andersen7dc16072000-01-04 01:10:25 +0000555#endif
556
Erik Andersene49d5ec2000-02-08 19:58:47 +0000557 /* Do not change permissions or date on symlink,
558 * since it changes the pointed to file instead. duh. */
Eric Andersencc8ed391999-10-05 16:24:54 +0000559#else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000560 fprintf(stderr, "Cannot create symbolic links\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000561#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000562 return;
Erik Andersen06936df2000-01-23 02:14:20 +0000563 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000564
Erik Andersene49d5ec2000-02-08 19:58:47 +0000565 /* Set the umask for this process so it doesn't
566 * screw things up. */
567 umask(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000568
Erik Andersene49d5ec2000-02-08 19:58:47 +0000569 /*
570 * If the file is a directory, then just create the path.
571 */
572 if (S_ISDIR(mode)) {
573 if (createPath(outName, mode) == TRUE) {
Erik Andersene2729152000-02-18 21:34:17 +0000574 /* make the final component, just in case it was
575 * omitted by createPath() (which will skip the
576 * directory if it doesn't have a terminating '/')
577 */
578 mkdir(outName, mode);
579
Erik Andersene49d5ec2000-02-08 19:58:47 +0000580 /* Set the file time */
581 utb.actime = mtime;
582 utb.modtime = mtime;
583 utime(outName, &utb);
584 /* Set the file permissions */
585 chown(outName, uid, gid);
586 chmod(outName, mode);
587 return;
588 }
589 return;
Eric Andersen96bcfd31999-11-12 01:30:18 +0000590 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000591
592 /*
593 * There is a file to write.
594 * First create the path to it if necessary with default permissions.
595 */
596 createPath(outName, 0777);
597
598 inHeader = (size == 0) ? TRUE : FALSE;
599 dataCc = size;
600
601 /*
602 * Start the output file.
603 */
604 if (tostdoutFlag == TRUE)
605 outFd = fileno(stdout);
606 else {
607 if (S_ISCHR(mode) || S_ISBLK(mode) || S_ISSOCK(mode)) {
608 devFileFlag = TRUE;
609 outFd = mknod(outName, mode, makedev(major, minor));
610 } else if (S_ISFIFO(mode)) {
611 devFileFlag = TRUE;
612 outFd = mkfifo(outName, mode);
613 } else {
614 outFd = open(outName, O_WRONLY | O_CREAT | O_TRUNC, mode);
615 }
616 if (outFd < 0) {
617 perror(outName);
618 skipFileFlag = TRUE;
619 return;
620 }
621 /* Set the file time */
622 utb.actime = mtime;
623 utb.modtime = mtime;
624 utime(outName, &utb);
625 /* Set the file permissions */
626 chown(outName, uid, gid);
627 chmod(outName, mode);
Eric Andersen96bcfd31999-11-12 01:30:18 +0000628 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000629
Eric Andersencc8ed391999-10-05 16:24:54 +0000630
Erik Andersene49d5ec2000-02-08 19:58:47 +0000631 /*
632 * If the file is empty, then that's all we need to do.
633 */
634 if (size == 0 && (tostdoutFlag == FALSE) && (devFileFlag == FALSE)) {
635 close(outFd);
636 outFd = -1;
637 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000638}
639
640
641/*
642 * Handle a data block of some specified size that was read.
643 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000644static void readData(const char *cp, int count)
Eric Andersencc8ed391999-10-05 16:24:54 +0000645{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000646 /*
647 * Reduce the amount of data left in this file.
648 * If there is no more data left, then we need to read
649 * the header again.
650 */
651 dataCc -= count;
Eric Andersencc8ed391999-10-05 16:24:54 +0000652
Erik Andersene49d5ec2000-02-08 19:58:47 +0000653 if (dataCc <= 0)
654 inHeader = TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000655
Erik Andersene49d5ec2000-02-08 19:58:47 +0000656 /*
657 * If we aren't extracting files or this file is being
658 * skipped then do nothing more.
659 */
660 if (extractFlag == FALSE || skipFileFlag == TRUE)
661 return;
Eric Andersencc8ed391999-10-05 16:24:54 +0000662
Erik Andersene49d5ec2000-02-08 19:58:47 +0000663 /*
664 * Write the data to the output file.
665 */
666 if (fullWrite(outFd, cp, count) < 0) {
667 perror(outName);
668 if (tostdoutFlag == FALSE) {
669 close(outFd);
670 outFd = -1;
671 }
672 skipFileFlag = TRUE;
673 return;
Eric Andersencc8ed391999-10-05 16:24:54 +0000674 }
675
Erik Andersene49d5ec2000-02-08 19:58:47 +0000676 /*
677 * Check if we are done writing to the file now.
678 */
679 if (dataCc <= 0 && tostdoutFlag == FALSE) {
680 struct utimbuf utb;
Eric Andersencc8ed391999-10-05 16:24:54 +0000681
Erik Andersene49d5ec2000-02-08 19:58:47 +0000682 if (close(outFd))
683 perror(outName);
Erik Andersen7dc16072000-01-04 01:10:25 +0000684
Erik Andersene49d5ec2000-02-08 19:58:47 +0000685 /* Set the file time */
686 utb.actime = mtime;
687 utb.modtime = mtime;
688 utime(outName, &utb);
689 /* Set the file permissions */
690 chown(outName, uid, gid);
691 chmod(outName, mode);
692
693 outFd = -1;
694 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000695}
696
697
698/*
Erik Andersen05100cd2000-01-16 01:30:52 +0000699 * See if the specified file name belongs to one of the specified list
700 * of path prefixes. An empty list implies that all files are wanted.
701 * Returns TRUE if the file is selected.
702 */
703static int
Erik Andersen298854f2000-03-23 01:09:18 +0000704wantFileName(const char *fileName, int argc, char **argv)
Erik Andersen05100cd2000-01-16 01:30:52 +0000705{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000706 const char *pathName;
707 int fileLength;
708 int pathLength;
Erik Andersen05100cd2000-01-16 01:30:52 +0000709
Erik Andersene49d5ec2000-02-08 19:58:47 +0000710 /*
711 * If there are no files in the list, then the file is wanted.
712 */
Erik Andersen298854f2000-03-23 01:09:18 +0000713 if (argc == 0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000714 return TRUE;
Erik Andersen05100cd2000-01-16 01:30:52 +0000715
Erik Andersene49d5ec2000-02-08 19:58:47 +0000716 fileLength = strlen(fileName);
Erik Andersen05100cd2000-01-16 01:30:52 +0000717
Erik Andersene49d5ec2000-02-08 19:58:47 +0000718 /*
719 * Check each of the test paths.
720 */
Erik Andersen298854f2000-03-23 01:09:18 +0000721 while (argc-- > 0) {
722 pathName = *argv++;
Erik Andersen05100cd2000-01-16 01:30:52 +0000723
Erik Andersene49d5ec2000-02-08 19:58:47 +0000724 pathLength = strlen(pathName);
Erik Andersen05100cd2000-01-16 01:30:52 +0000725
Erik Andersene49d5ec2000-02-08 19:58:47 +0000726 if (fileLength < pathLength)
727 continue;
Erik Andersen05100cd2000-01-16 01:30:52 +0000728
Erik Andersene49d5ec2000-02-08 19:58:47 +0000729 if (memcmp(fileName, pathName, pathLength) != 0)
730 continue;
Erik Andersen05100cd2000-01-16 01:30:52 +0000731
Erik Andersene49d5ec2000-02-08 19:58:47 +0000732 if ((fileLength == pathLength) || (fileName[pathLength] == '/')) {
733 return TRUE;
734 }
Erik Andersen05100cd2000-01-16 01:30:52 +0000735 }
Erik Andersen05100cd2000-01-16 01:30:52 +0000736
Erik Andersene49d5ec2000-02-08 19:58:47 +0000737 return FALSE;
Erik Andersen05100cd2000-01-16 01:30:52 +0000738}
739
Erik Andersen05100cd2000-01-16 01:30:52 +0000740
741/* From here to the end of the file is the tar writing stuff.
742 * If you do not have BB_FEATURE_TAR_CREATE defined, this will
743 * not be built.
744 * */
745#ifdef BB_FEATURE_TAR_CREATE
746
747/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000748 * Write a tar file containing the specified files.
749 */
Erik Andersen298854f2000-03-23 01:09:18 +0000750static void writeTarFile(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000751{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000752 struct stat statbuf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000753
Erik Andersene49d5ec2000-02-08 19:58:47 +0000754 /*
755 * Make sure there is at least one file specified.
756 */
Erik Andersen298854f2000-03-23 01:09:18 +0000757 if (argc <= 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000758 fprintf(stderr, "No files specified to be saved\n");
759 errorFlag = TRUE;
760 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000761
Erik Andersene49d5ec2000-02-08 19:58:47 +0000762 /*
763 * Create the tar file for writing.
764 */
765 if ((tarName == NULL) || !strcmp(tarName, "-")) {
766 tostdoutFlag = TRUE;
767 tarFd = fileno(stdout);
768 } else
769 tarFd = open(tarName, O_WRONLY | O_CREAT | O_TRUNC, 0666);
Eric Andersencc8ed391999-10-05 16:24:54 +0000770
Erik Andersene49d5ec2000-02-08 19:58:47 +0000771 if (tarFd < 0) {
772 perror(tarName);
773 errorFlag = TRUE;
774 return;
775 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000776
Erik Andersene49d5ec2000-02-08 19:58:47 +0000777 /*
778 * Get the device and inode of the tar file for checking later.
779 */
780 if (fstat(tarFd, &statbuf) < 0) {
781 perror(tarName);
782 errorFlag = TRUE;
783 goto done;
784 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000785
Erik Andersene49d5ec2000-02-08 19:58:47 +0000786 tarDev = statbuf.st_dev;
787 tarInode = statbuf.st_ino;
Eric Andersencc8ed391999-10-05 16:24:54 +0000788
Erik Andersene49d5ec2000-02-08 19:58:47 +0000789 /*
790 * Append each file name into the archive file.
791 * Follow symbolic links for these top level file names.
792 */
Erik Andersen298854f2000-03-23 01:09:18 +0000793 while (errorFlag == FALSE && (argc-- > 0)) {
794 saveFile(*argv++, FALSE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000795 }
796
797 /*
798 * Now write an empty block of zeroes to end the archive.
799 */
800 writeTarBlock("", 1);
Eric Andersencc8ed391999-10-05 16:24:54 +0000801
802
Eric Andersen3cf52d11999-10-12 22:26:06 +0000803 done:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000804 /*
805 * Close the tar file and check for errors if it was opened.
806 */
807 if ((tostdoutFlag == FALSE) && (tarFd >= 0) && (close(tarFd) < 0))
808 perror(tarName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000809}
810
Eric Andersencc8ed391999-10-05 16:24:54 +0000811/*
812 * Save one file into the tar file.
813 * If the file is a directory, then this will recursively save all of
814 * the files and directories within the directory. The seeLinks
815 * flag indicates whether or not we want to see symbolic links as
816 * they really are, instead of blindly following them.
817 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000818static void saveFile(const char *fileName, int seeLinks)
Eric Andersencc8ed391999-10-05 16:24:54 +0000819{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000820 int status;
821 struct stat statbuf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000822
Erik Andersene49d5ec2000-02-08 19:58:47 +0000823 if (verboseFlag == TRUE)
824 printf("a %s\n", fileName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000825
Erik Andersene49d5ec2000-02-08 19:58:47 +0000826 /*
827 * Check that the file name will fit in the header.
828 */
829 if (strlen(fileName) >= TAR_NAME_SIZE) {
830 fprintf(stderr, "%s: File name is too long\n", fileName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000831
Erik Andersene49d5ec2000-02-08 19:58:47 +0000832 return;
833 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000834
Erik Andersene49d5ec2000-02-08 19:58:47 +0000835 /*
836 * Find out about the file.
837 */
Eric Andersencc8ed391999-10-05 16:24:54 +0000838#ifdef S_ISLNK
Erik Andersene49d5ec2000-02-08 19:58:47 +0000839 if (seeLinks == TRUE)
840 status = lstat(fileName, &statbuf);
841 else
Eric Andersencc8ed391999-10-05 16:24:54 +0000842#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000843 status = stat(fileName, &statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +0000844
Erik Andersene49d5ec2000-02-08 19:58:47 +0000845 if (status < 0) {
846 perror(fileName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000847
Erik Andersene49d5ec2000-02-08 19:58:47 +0000848 return;
849 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000850
Erik Andersene49d5ec2000-02-08 19:58:47 +0000851 /*
852 * Make sure we aren't trying to save our file into itself.
853 */
854 if ((statbuf.st_dev == tarDev) && (statbuf.st_ino == tarInode)) {
855 fprintf(stderr, "Skipping saving of archive file itself\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000856
Erik Andersene49d5ec2000-02-08 19:58:47 +0000857 return;
858 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000859
Erik Andersene49d5ec2000-02-08 19:58:47 +0000860 /*
861 * Check the type of file.
862 */
863 mode = statbuf.st_mode;
Eric Andersencc8ed391999-10-05 16:24:54 +0000864
Erik Andersene49d5ec2000-02-08 19:58:47 +0000865 if (S_ISDIR(mode)) {
866 saveDirectory(fileName, &statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +0000867
Erik Andersene49d5ec2000-02-08 19:58:47 +0000868 return;
869 }
870 if (S_ISREG(mode)) {
871 saveRegularFile(fileName, &statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +0000872
Erik Andersene49d5ec2000-02-08 19:58:47 +0000873 return;
874 }
875
876 /* Some day add support for tarring these up... but not today. :) */
Eric Andersen96bcfd31999-11-12 01:30:18 +0000877// if (S_ISLNK(mode) || S_ISFIFO(mode) || S_ISBLK(mode) || S_ISCHR (mode) ) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000878// fprintf (stderr, "%s: This version of tar can't store this type of file\n", fileName);
Eric Andersen96bcfd31999-11-12 01:30:18 +0000879// }
Eric Andersencc8ed391999-10-05 16:24:54 +0000880
Erik Andersene49d5ec2000-02-08 19:58:47 +0000881 /*
882 * The file is a strange type of file, ignore it.
883 */
884 fprintf(stderr, "%s: not a directory or regular file\n", fileName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000885}
886
887
888/*
889 * Save a regular file to the tar file.
890 */
891static void
Erik Andersene49d5ec2000-02-08 19:58:47 +0000892saveRegularFile(const char *fileName, const struct stat *statbuf)
Eric Andersencc8ed391999-10-05 16:24:54 +0000893{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000894 int sawEof;
895 int fileFd;
896 int cc;
897 int dataCount;
898 long fullDataCount;
899 char data[TAR_BLOCK_SIZE * 16];
Eric Andersen3cf52d11999-10-12 22:26:06 +0000900
901 /*
Erik Andersene49d5ec2000-02-08 19:58:47 +0000902 * Open the file for reading.
Eric Andersen3cf52d11999-10-12 22:26:06 +0000903 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000904 fileFd = open(fileName, O_RDONLY);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000905
Erik Andersene49d5ec2000-02-08 19:58:47 +0000906 if (fileFd < 0) {
907 perror(fileName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000908
909 return;
910 }
911
Eric Andersen3cf52d11999-10-12 22:26:06 +0000912 /*
Erik Andersene49d5ec2000-02-08 19:58:47 +0000913 * Write out the header for the file.
Eric Andersencc8ed391999-10-05 16:24:54 +0000914 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000915 writeHeader(fileName, statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +0000916
Eric Andersen3cf52d11999-10-12 22:26:06 +0000917 /*
Erik Andersene49d5ec2000-02-08 19:58:47 +0000918 * Write the data blocks of the file.
919 * We must be careful to write the amount of data that the stat
920 * buffer indicated, even if the file has changed size. Otherwise
921 * the tar file will be incorrect.
Eric Andersencc8ed391999-10-05 16:24:54 +0000922 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000923 fullDataCount = statbuf->st_size;
924 sawEof = FALSE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000925
Erik Andersene49d5ec2000-02-08 19:58:47 +0000926 while (fullDataCount > 0) {
927 /*
928 * Get the amount to write this iteration which is
929 * the minumum of the amount left to write and the
930 * buffer size.
931 */
932 dataCount = sizeof(data);
Eric Andersencc8ed391999-10-05 16:24:54 +0000933
Erik Andersene49d5ec2000-02-08 19:58:47 +0000934 if (dataCount > fullDataCount)
935 dataCount = (int) fullDataCount;
936
937 /*
938 * Read the data from the file if we haven't seen the
939 * end of file yet.
940 */
941 cc = 0;
942
943 if (sawEof == FALSE) {
944 cc = fullRead(fileFd, data, dataCount);
945
946 if (cc < 0) {
947 perror(fileName);
948
949 (void) close(fileFd);
950 errorFlag = TRUE;
951
952 return;
953 }
954
955 /*
956 * If the file ended too soon, complain and set
957 * a flag so we will zero fill the rest of it.
958 */
959 if (cc < dataCount) {
960 fprintf(stderr, "%s: Short read - zero filling", fileName);
961
962 sawEof = TRUE;
963 }
964 }
965
966 /*
967 * Zero fill the rest of the data if necessary.
968 */
969 if (cc < dataCount)
970 memset(data + cc, 0, dataCount - cc);
971
972 /*
973 * Write the buffer to the TAR file.
974 */
975 writeTarBlock(data, dataCount);
976
977 fullDataCount -= dataCount;
978 }
979
980 /*
981 * Close the file.
982 */
983 if ((tostdoutFlag == FALSE) && close(fileFd) < 0)
984 fprintf(stderr, "%s: close: %s\n", fileName, strerror(errno));
Eric Andersencc8ed391999-10-05 16:24:54 +0000985}
986
987
988/*
989 * Save a directory and all of its files to the tar file.
990 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000991static void saveDirectory(const char *dirName, const struct stat *statbuf)
Eric Andersencc8ed391999-10-05 16:24:54 +0000992{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000993 DIR *dir;
994 struct dirent *entry;
995 int needSlash;
996 char fullName[PATH_MAX + 1];
Eric Andersencc8ed391999-10-05 16:24:54 +0000997
Erik Andersene49d5ec2000-02-08 19:58:47 +0000998 /*
999 * Construct the directory name as used in the tar file by appending
1000 * a slash character to it.
1001 */
1002 strcpy(fullName, dirName);
1003 strcat(fullName, "/");
Eric Andersencc8ed391999-10-05 16:24:54 +00001004
Erik Andersene49d5ec2000-02-08 19:58:47 +00001005 /*
1006 * Write out the header for the directory entry.
1007 */
1008 writeHeader(fullName, statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +00001009
Erik Andersene49d5ec2000-02-08 19:58:47 +00001010 /*
1011 * Open the directory.
1012 */
1013 dir = opendir(dirName);
Eric Andersencc8ed391999-10-05 16:24:54 +00001014
Erik Andersene49d5ec2000-02-08 19:58:47 +00001015 if (dir == NULL) {
1016 fprintf(stderr, "Cannot read directory \"%s\": %s\n",
1017 dirName, strerror(errno));
Eric Andersencc8ed391999-10-05 16:24:54 +00001018
Erik Andersene49d5ec2000-02-08 19:58:47 +00001019 return;
Eric Andersencc8ed391999-10-05 16:24:54 +00001020 }
1021
Eric Andersen3cf52d11999-10-12 22:26:06 +00001022 /*
Erik Andersene49d5ec2000-02-08 19:58:47 +00001023 * See if a slash is needed.
Eric Andersencc8ed391999-10-05 16:24:54 +00001024 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001025 needSlash = (*dirName && (dirName[strlen(dirName) - 1] != '/'));
Eric Andersen3cf52d11999-10-12 22:26:06 +00001026
1027 /*
Erik Andersene49d5ec2000-02-08 19:58:47 +00001028 * Read all of the directory entries and check them,
1029 * except for the current and parent directory entries.
Eric Andersencc8ed391999-10-05 16:24:54 +00001030 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001031 while (errorFlag == FALSE && ((entry = readdir(dir)) != NULL)) {
1032 if ((strcmp(entry->d_name, ".") == 0) ||
1033 (strcmp(entry->d_name, "..") == 0)) {
1034 continue;
1035 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001036
Erik Andersene49d5ec2000-02-08 19:58:47 +00001037 /*
1038 * Build the full path name to the file.
1039 */
1040 strcpy(fullName, dirName);
1041
1042 if (needSlash)
1043 strcat(fullName, "/");
1044
1045 strcat(fullName, entry->d_name);
1046
1047 /*
1048 * Write this file to the tar file, noticing whether or not
1049 * the file is a symbolic link.
1050 */
1051 saveFile(fullName, TRUE);
1052 }
1053
1054 /*
1055 * All done, close the directory.
1056 */
1057 closedir(dir);
Eric Andersencc8ed391999-10-05 16:24:54 +00001058}
1059
1060
1061/*
1062 * Write a tar header for the specified file name and status.
1063 * It is assumed that the file name fits.
1064 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001065static void writeHeader(const char *fileName, const struct stat *statbuf)
Eric Andersencc8ed391999-10-05 16:24:54 +00001066{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001067 long checkSum;
1068 const unsigned char *cp;
1069 int len;
1070 TarHeader header;
Eric Andersencc8ed391999-10-05 16:24:54 +00001071
Erik Andersene49d5ec2000-02-08 19:58:47 +00001072 /*
1073 * Zero the header block in preparation for filling it in.
1074 */
1075 memset((char *) &header, 0, sizeof(header));
Eric Andersencc8ed391999-10-05 16:24:54 +00001076
Erik Andersene49d5ec2000-02-08 19:58:47 +00001077 /*
1078 * Fill in the header.
1079 */
1080 strcpy(header.name, fileName);
Eric Andersencc8ed391999-10-05 16:24:54 +00001081
Erik Andersene49d5ec2000-02-08 19:58:47 +00001082 strncpy(header.magic, TAR_MAGIC, sizeof(header.magic));
1083 strncpy(header.version, TAR_VERSION, sizeof(header.version));
Eric Andersencc8ed391999-10-05 16:24:54 +00001084
Erik Andersene49d5ec2000-02-08 19:58:47 +00001085 putOctal(header.mode, sizeof(header.mode), statbuf->st_mode & 0777);
1086 putOctal(header.uid, sizeof(header.uid), statbuf->st_uid);
1087 putOctal(header.gid, sizeof(header.gid), statbuf->st_gid);
1088 putOctal(header.size, sizeof(header.size), statbuf->st_size);
1089 putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime);
Eric Andersencc8ed391999-10-05 16:24:54 +00001090
Erik Andersene49d5ec2000-02-08 19:58:47 +00001091 header.typeFlag = TAR_TYPE_REGULAR;
Eric Andersencc8ed391999-10-05 16:24:54 +00001092
Erik Andersene49d5ec2000-02-08 19:58:47 +00001093 /*
1094 * Calculate and store the checksum.
1095 * This is the sum of all of the bytes of the header,
1096 * with the checksum field itself treated as blanks.
1097 */
1098 memset(header.checkSum, ' ', sizeof(header.checkSum));
Eric Andersencc8ed391999-10-05 16:24:54 +00001099
Erik Andersene49d5ec2000-02-08 19:58:47 +00001100 cp = (const unsigned char *) &header;
1101 len = sizeof(header);
1102 checkSum = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +00001103
Erik Andersene49d5ec2000-02-08 19:58:47 +00001104 while (len-- > 0)
1105 checkSum += *cp++;
Eric Andersencc8ed391999-10-05 16:24:54 +00001106
Erik Andersene49d5ec2000-02-08 19:58:47 +00001107 putOctal(header.checkSum, sizeof(header.checkSum), checkSum);
Eric Andersencc8ed391999-10-05 16:24:54 +00001108
Erik Andersene49d5ec2000-02-08 19:58:47 +00001109 /*
1110 * Write the tar header.
1111 */
1112 writeTarBlock((const char *) &header, sizeof(header));
Eric Andersencc8ed391999-10-05 16:24:54 +00001113}
1114
1115
1116/*
1117 * Write data to one or more blocks of the tar file.
1118 * The data is always padded out to a multiple of TAR_BLOCK_SIZE.
1119 * The errorFlag static variable is set on an error.
1120 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001121static void writeTarBlock(const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +00001122{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001123 int partialLength;
1124 int completeLength;
1125 char fullBlock[TAR_BLOCK_SIZE];
Eric Andersencc8ed391999-10-05 16:24:54 +00001126
Erik Andersene49d5ec2000-02-08 19:58:47 +00001127 /*
1128 * If we had a write error before, then do nothing more.
1129 */
1130 if (errorFlag == TRUE)
1131 return;
Eric Andersencc8ed391999-10-05 16:24:54 +00001132
Erik Andersene49d5ec2000-02-08 19:58:47 +00001133 /*
1134 * Get the amount of complete and partial blocks.
1135 */
1136 partialLength = len % TAR_BLOCK_SIZE;
1137 completeLength = len - partialLength;
Eric Andersencc8ed391999-10-05 16:24:54 +00001138
Erik Andersene49d5ec2000-02-08 19:58:47 +00001139 /*
1140 * Write all of the complete blocks.
1141 */
1142 if ((completeLength > 0) && !fullWrite(tarFd, buf, completeLength)) {
1143 perror(tarName);
Eric Andersencc8ed391999-10-05 16:24:54 +00001144
Erik Andersene49d5ec2000-02-08 19:58:47 +00001145 errorFlag = TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +00001146
Erik Andersene49d5ec2000-02-08 19:58:47 +00001147 return;
1148 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001149
Erik Andersene49d5ec2000-02-08 19:58:47 +00001150 /*
1151 * If there are no partial blocks left, we are done.
1152 */
1153 if (partialLength == 0)
1154 return;
Eric Andersencc8ed391999-10-05 16:24:54 +00001155
Erik Andersene49d5ec2000-02-08 19:58:47 +00001156 /*
1157 * Copy the partial data into a complete block, and pad the rest
1158 * of it with zeroes.
1159 */
1160 memcpy(fullBlock, buf + completeLength, partialLength);
1161 memset(fullBlock + partialLength, 0, TAR_BLOCK_SIZE - partialLength);
Eric Andersencc8ed391999-10-05 16:24:54 +00001162
Erik Andersene49d5ec2000-02-08 19:58:47 +00001163 /*
1164 * Write the last complete block.
1165 */
1166 if (!fullWrite(tarFd, fullBlock, TAR_BLOCK_SIZE)) {
1167 perror(tarName);
Eric Andersencc8ed391999-10-05 16:24:54 +00001168
Erik Andersene49d5ec2000-02-08 19:58:47 +00001169 errorFlag = TRUE;
1170 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001171}
1172
1173
1174/*
Eric Andersencc8ed391999-10-05 16:24:54 +00001175 * Put an octal string into the specified buffer.
1176 * The number is zero and space padded and possibly null padded.
1177 * Returns TRUE if successful.
1178 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001179static int putOctal(char *cp, int len, long value)
Eric Andersencc8ed391999-10-05 16:24:54 +00001180{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001181 int tempLength;
1182 char *tempString;
1183 char tempBuffer[32];
Eric Andersencc8ed391999-10-05 16:24:54 +00001184
Erik Andersene49d5ec2000-02-08 19:58:47 +00001185 /*
1186 * Create a string of the specified length with an initial space,
1187 * leading zeroes and the octal number, and a trailing null.
1188 */
1189 tempString = tempBuffer;
Eric Andersencc8ed391999-10-05 16:24:54 +00001190
Erik Andersene49d5ec2000-02-08 19:58:47 +00001191 sprintf(tempString, " %0*lo", len - 2, value);
Eric Andersencc8ed391999-10-05 16:24:54 +00001192
Erik Andersene49d5ec2000-02-08 19:58:47 +00001193 tempLength = strlen(tempString) + 1;
Eric Andersencc8ed391999-10-05 16:24:54 +00001194
Erik Andersene49d5ec2000-02-08 19:58:47 +00001195 /*
1196 * If the string is too large, suppress the leading space.
1197 */
1198 if (tempLength > len) {
1199 tempLength--;
1200 tempString++;
1201 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001202
Erik Andersene49d5ec2000-02-08 19:58:47 +00001203 /*
1204 * If the string is still too large, suppress the trailing null.
1205 */
1206 if (tempLength > len)
1207 tempLength--;
Eric Andersencc8ed391999-10-05 16:24:54 +00001208
Erik Andersene49d5ec2000-02-08 19:58:47 +00001209 /*
1210 * If the string is still too large, fail.
1211 */
1212 if (tempLength > len)
1213 return FALSE;
Eric Andersencc8ed391999-10-05 16:24:54 +00001214
Erik Andersene49d5ec2000-02-08 19:58:47 +00001215 /*
1216 * Copy the string to the field.
1217 */
1218 memcpy(cp, tempString, len);
Eric Andersencc8ed391999-10-05 16:24:54 +00001219
Erik Andersene49d5ec2000-02-08 19:58:47 +00001220 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +00001221}
Erik Andersen05100cd2000-01-16 01:30:52 +00001222#endif
Eric Andersencc8ed391999-10-05 16:24:54 +00001223
Eric Andersencc8ed391999-10-05 16:24:54 +00001224/* END CODE */
Erik Andersen298854f2000-03-23 01:09:18 +00001225#endif