blob: 37a28a3d020a531eb04bda330328af519a599598 [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 Andersen6acaa402000-03-26 14:03:20 +00003 *
4 * Mini tar implementation for busybox Note, that as of BusyBox 0.43 tar has
5 * been completely rewritten from the ground up. It still has remnents of the
6 * old code lying about, but it pretty different (i.e. cleaner, less global
7 * variables, etc)
Eric Andersenc4996011999-10-20 22:08:37 +00008 *
Erik Andersen1ad302a2000-03-24 00:54:46 +00009 * Copyright (C) 1999 by Lineo, inc.
10 * 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 *
18 * Based in part on the tar implementation in busybox-0.28
19 * 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"
59 "Create, extract, or list files from a tar file.\n\n"
60 "Options:\n"
61
62 "\tc=create, x=extract, t=list contents, v=verbose,\n"
63 "\tO=extract to stdout, f=tarfile or \"-\" for stdin\n";
Eric Andersene77ae3a1999-10-19 20:03:34 +000064
Erik Andersen05100cd2000-01-16 01:30:52 +000065#else
66
67static const char tar_usage[] =
Erik Andersene49d5ec2000-02-08 19:58:47 +000068 "tar -[xtvOf] [tarFileName] [FILE] ...\n\n"
69 "Extract, or list files stored in a tar file. This\n"
70 "version of tar does not support creation of tar files.\n\n"
71 "Options:\n"
72
73 "\tx=extract, t=list contents, v=verbose,\n"
74 "\tO=extract to stdout, f=tarfile or \"-\" for stdin\n";
Erik Andersen05100cd2000-01-16 01:30:52 +000075
76#endif
Eric Andersene77ae3a1999-10-19 20:03:34 +000077
78
Erik Andersen298854f2000-03-23 01:09:18 +000079/* Tar file constants */
Erik Andersen1ad302a2000-03-24 00:54:46 +000080#ifndef MAJOR
81#define MAJOR(dev) (((dev)>>8)&0xff)
82#define MINOR(dev) ((dev)&0xff)
83#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000084
85
Erik Andersen298854f2000-03-23 01:09:18 +000086/* POSIX tar Header Block, from POSIX 1003.1-1990 */
87struct TarHeader
88{
89 /* byte offset */
90 char name[100]; /* 0 */
91 char mode[8]; /* 100 */
92 char uid[8]; /* 108 */
93 char gid[8]; /* 116 */
94 char size[12]; /* 124 */
95 char mtime[12]; /* 136 */
96 char chksum[8]; /* 148 */
97 char typeflag; /* 156 */
98 char linkname[100]; /* 157 */
99 char magic[6]; /* 257 */
100 char version[2]; /* 263 */
101 char uname[32]; /* 265 */
102 char gname[32]; /* 297 */
103 char devmajor[8]; /* 329 */
104 char devminor[8]; /* 337 */
105 char prefix[155]; /* 345 */
106 /* padding 500 */
107};
108typedef struct TarHeader TarHeader;
Eric Andersencc8ed391999-10-05 16:24:54 +0000109
Eric Andersencc8ed391999-10-05 16:24:54 +0000110
Erik Andersen298854f2000-03-23 01:09:18 +0000111/* A few useful constants */
112#define TAR_MAGIC "ustar" /* ustar and a null */
113#define TAR_VERSION "00" /* 00 and no null */
114#define TAR_MAGIC_LEN 6
115#define TAR_VERSION_LEN 2
116#define TAR_NAME_LEN 100
117#define TAR_BLOCK_SIZE 512
118
119/* A nice enum with all the possible tar file content types */
120enum TarFileType
121{
122 REGTYPE = '0', /* regular file */
123 REGTYPE0 = '\0', /* regular file (ancient bug compat)*/
124 LNKTYPE = '1', /* hard link */
125 SYMTYPE = '2', /* symbolic link */
126 CHRTYPE = '3', /* character special */
127 BLKTYPE = '4', /* block special */
128 DIRTYPE = '5', /* directory */
129 FIFOTYPE = '6', /* FIFO special */
130 CONTTYPE = '7', /* reserved */
131};
132typedef enum TarFileType TarFileType;
133
134/* This struct ignores magic, non-numeric user name,
135 * non-numeric group name, and the checksum, since
136 * these are all ignored by BusyBox tar. */
137struct TarInfo
138{
139 int tarFd; /* An open file descriptor for reading from the tarball */
140 char * name; /* File name */
141 mode_t mode; /* Unix mode, including device bits. */
142 uid_t uid; /* Numeric UID */
143 gid_t gid; /* Numeric GID */
144 size_t size; /* Size of file */
145 time_t mtime; /* Last-modified time */
146 enum TarFileType type; /* Regular, directory, link, etc */
147 char * linkname; /* Name for symbolic and hard links */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000148 long devmajor; /* Major number for special device */
149 long devminor; /* Minor number for special device */
Erik Andersen298854f2000-03-23 01:09:18 +0000150};
151typedef struct TarInfo TarInfo;
152
153/* Static data */
154static const unsigned long TarChecksumOffset = (const unsigned long)&(((TarHeader *)0)->chksum);
Eric Andersencc8ed391999-10-05 16:24:54 +0000155
156
Erik Andersene454fb62000-03-23 04:27:58 +0000157/* Local procedures to restore files from a tar file. */
Erik Andersen298854f2000-03-23 01:09:18 +0000158static int readTarFile(const char* tarName, int extractFlag, int listFlag,
159 int tostdoutFlag, int verboseFlag);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000160
161
Eric Andersencc8ed391999-10-05 16:24:54 +0000162
Erik Andersen05100cd2000-01-16 01:30:52 +0000163#ifdef BB_FEATURE_TAR_CREATE
Erik Andersen6acaa402000-03-26 14:03:20 +0000164/* Local procedures to save files into a tar file. */
165static int writeTarFile(const char* tarName, int extractFlag, int listFlag,
166 int tostdoutFlag, int verboseFlag, int argc, char **argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000167static int putOctal(char *cp, int len, long value);
Eric Andersencc8ed391999-10-05 16:24:54 +0000168
Erik Andersen05100cd2000-01-16 01:30:52 +0000169#endif
170
Eric Andersencc8ed391999-10-05 16:24:54 +0000171
Erik Andersene49d5ec2000-02-08 19:58:47 +0000172extern int tar_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000173{
Erik Andersen298854f2000-03-23 01:09:18 +0000174 const char *tarName=NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000175 const char *options;
Erik Andersen298854f2000-03-23 01:09:18 +0000176 int listFlag = FALSE;
177 int extractFlag = FALSE;
178 int createFlag = FALSE;
179 int verboseFlag = FALSE;
180 int tostdoutFlag = FALSE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000181
Erik Andersene49d5ec2000-02-08 19:58:47 +0000182 argc--;
183 argv++;
Eric Andersencc8ed391999-10-05 16:24:54 +0000184
Erik Andersene49d5ec2000-02-08 19:58:47 +0000185 if (argc < 1)
186 usage(tar_usage);
Eric Andersencc8ed391999-10-05 16:24:54 +0000187
Erik Andersen298854f2000-03-23 01:09:18 +0000188 /* Parse options */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000189 if (**argv == '-')
190 options = (*argv++) + 1;
191 else
192 options = (*argv++);
193 argc--;
Eric Andersencc8ed391999-10-05 16:24:54 +0000194
Erik Andersene49d5ec2000-02-08 19:58:47 +0000195 for (; *options; options++) {
196 switch (*options) {
197 case 'f':
Erik Andersen298854f2000-03-23 01:09:18 +0000198 if (tarName != NULL)
199 fatalError( "Only one 'f' option allowed\n");
Erik Andersende552872000-01-23 01:34:05 +0000200
Erik Andersene49d5ec2000-02-08 19:58:47 +0000201 tarName = *argv++;
Erik Andersen298854f2000-03-23 01:09:18 +0000202 if (tarName == NULL)
203 fatalError( "Option requires an argument: No file specified\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000204 argc--;
Erik Andersende552872000-01-23 01:34:05 +0000205
Erik Andersene49d5ec2000-02-08 19:58:47 +0000206 break;
Erik Andersende552872000-01-23 01:34:05 +0000207
Erik Andersene49d5ec2000-02-08 19:58:47 +0000208 case 't':
209 if (extractFlag == TRUE || createFlag == TRUE)
210 goto flagError;
211 listFlag = TRUE;
212 break;
Erik Andersende552872000-01-23 01:34:05 +0000213
Erik Andersene49d5ec2000-02-08 19:58:47 +0000214 case 'x':
215 if (listFlag == TRUE || createFlag == TRUE)
216 goto flagError;
217 extractFlag = TRUE;
218 break;
219 case 'c':
220 if (extractFlag == TRUE || listFlag == TRUE)
221 goto flagError;
222 createFlag = TRUE;
223 break;
Erik Andersende552872000-01-23 01:34:05 +0000224
Erik Andersene49d5ec2000-02-08 19:58:47 +0000225 case 'v':
226 verboseFlag = TRUE;
227 break;
Erik Andersende552872000-01-23 01:34:05 +0000228
Erik Andersene49d5ec2000-02-08 19:58:47 +0000229 case 'O':
230 tostdoutFlag = TRUE;
231 break;
Erik Andersende552872000-01-23 01:34:05 +0000232
Erik Andersene49d5ec2000-02-08 19:58:47 +0000233 case '-':
234 usage(tar_usage);
235 break;
Erik Andersende552872000-01-23 01:34:05 +0000236
Erik Andersene49d5ec2000-02-08 19:58:47 +0000237 default:
Erik Andersen298854f2000-03-23 01:09:18 +0000238 fatalError( "Unknown tar flag '%c'\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +0000239 "Try `tar --help' for more information\n", *options);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000240 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000241 }
242
Erik Andersene49d5ec2000-02-08 19:58:47 +0000243 /*
244 * Do the correct type of action supplying the rest of the
245 * command line arguments as the list of files to process.
246 */
247 if (createFlag == TRUE) {
Erik Andersende552872000-01-23 01:34:05 +0000248#ifndef BB_FEATURE_TAR_CREATE
Erik Andersen298854f2000-03-23 01:09:18 +0000249 fatalError( "This version of tar was not compiled with tar creation support.\n");
Erik Andersende552872000-01-23 01:34:05 +0000250#else
Erik Andersen6acaa402000-03-26 14:03:20 +0000251 exit(writeTarFile(tarName, extractFlag, listFlag, tostdoutFlag, verboseFlag, argc, argv));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000252#endif
253 } else {
Erik Andersen298854f2000-03-23 01:09:18 +0000254 exit(readTarFile(tarName, extractFlag, listFlag, tostdoutFlag, verboseFlag));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000255 }
Erik Andersende552872000-01-23 01:34:05 +0000256
Erik Andersene49d5ec2000-02-08 19:58:47 +0000257 flagError:
Erik Andersen298854f2000-03-23 01:09:18 +0000258 fatalError( "Exactly one of 'c', 'x' or 't' must be specified\n");
259}
260
261static void
Erik Andersen1ad302a2000-03-24 00:54:46 +0000262tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersen298854f2000-03-23 01:09:18 +0000263{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000264 size_t writeSize;
265 size_t readSize;
266 size_t actualWriteSz;
267 char buffer[BUFSIZ];
268 size_t size = header->size;
269 int outFd=fileno(stdout);
270
271 /* Open the file to be written, if a file is supposed to be written */
272 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
273 if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY, header->mode & ~S_IFMT)) < 0)
274 errorMsg(io_error, header->name, strerror(errno));
275 /* Create the path to the file, just in case it isn't there...
276 * This should not screw up path permissions or anything. */
277 createPath(header->name, 0777);
278 }
279
280 /* Write out the file, if we are supposed to be doing that */
281 while ( size > 0 ) {
282 actualWriteSz=0;
283 if ( size > sizeof(buffer) )
284 writeSize = readSize = sizeof(buffer);
285 else {
286 int mod = size % 512;
287 if ( mod != 0 )
288 readSize = size + (512 - mod);
289 else
290 readSize = size;
291 writeSize = size;
292 }
293 if ( (readSize = fullRead(header->tarFd, buffer, readSize)) <= 0 ) {
294 /* Tarball seems to have a problem */
Erik Andersendeea0482000-03-25 23:09:29 +0000295 errorMsg("tar: Unexpected EOF in archive\n");
Erik Andersen1ad302a2000-03-24 00:54:46 +0000296 return;
297 }
298 if ( readSize < writeSize )
299 writeSize = readSize;
300
301 /* Write out the file, if we are supposed to be doing that */
302 if (extractFlag==TRUE) {
303
304 if ((actualWriteSz=fullWrite(outFd, buffer, writeSize)) != writeSize ) {
305 /* Output file seems to have a problem */
306 errorMsg(io_error, header->name, strerror(errno));
307 return;
308 }
309 }
310
311 size -= actualWriteSz;
312 }
313
314 /* Now we are done writing the file out, so try
315 * and fix up the permissions and whatnot */
316 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
317 struct utimbuf t;
318 /* Now set permissions etc for the new file */
319 fchown(outFd, header->uid, header->gid);
320 fchmod(outFd, header->mode & ~S_IFMT);
321 close(outFd);
322 /* File must be closed before trying to change the date */
323 t.actime = time(0);
324 t.modtime = header->mtime;
325 utime(header->name, &t);
326 }
327}
328
329static void
330fixUpPermissions(TarInfo *header)
331{
332 struct utimbuf t;
333 /* Now set permissions etc for the new file */
334 chown(header->name, header->uid, header->gid);
335 chmod(header->name, header->mode);
336 /* Reset the time */
337 t.actime = time(0);
338 t.modtime = header->mtime;
339 utime(header->name, &t);
Eric Andersencc8ed391999-10-05 16:24:54 +0000340}
Erik Andersene454fb62000-03-23 04:27:58 +0000341
342static void
Erik Andersen1ad302a2000-03-24 00:54:46 +0000343tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000344{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000345
346 if (extractFlag==FALSE || tostdoutFlag==TRUE)
347 return;
348
349 if (createPath(header->name, header->mode) != TRUE) {
350 errorMsg("Error creating directory '%s': %s", header->name, strerror(errno));
351 return;
352 }
353 /* make the final component, just in case it was
354 * omitted by createPath() (which will skip the
355 * directory if it doesn't have a terminating '/') */
Erik Andersen6acaa402000-03-26 14:03:20 +0000356 if (mkdir(header->name, header->mode) == 0) {
357 fixUpPermissions(header);
358 }
Erik Andersene454fb62000-03-23 04:27:58 +0000359}
360
361static void
Erik Andersen1ad302a2000-03-24 00:54:46 +0000362tarExtractHardLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000363{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000364 if (extractFlag==FALSE || tostdoutFlag==TRUE)
365 return;
366
367 if (link(header->linkname, header->name) < 0) {
Erik Andersendeea0482000-03-25 23:09:29 +0000368 errorMsg("Error creating hard link '%s': %s\n", header->linkname, strerror(errno));
Erik Andersen1ad302a2000-03-24 00:54:46 +0000369 return;
370 }
371
372 /* Now set permissions etc for the new directory */
373 fixUpPermissions(header);
Erik Andersene454fb62000-03-23 04:27:58 +0000374}
375
376static void
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)
380 return;
381
382#ifdef S_ISLNK
383 if (symlink(header->linkname, header->name) < 0) {
Erik Andersendeea0482000-03-25 23:09:29 +0000384 errorMsg("Error creating symlink '%s': %s\n", header->linkname, strerror(errno));
Erik Andersen1ad302a2000-03-24 00:54:46 +0000385 return;
386 }
387 /* Try to change ownership of the symlink.
388 * If libs doesn't support that, don't bother.
389 * Changing the pointed-to-file is the Wrong Thing(tm).
390 */
391#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
392 lchown(header->name, header->uid, header->gid);
393#endif
394
395 /* Do not change permissions or date on symlink,
396 * since it changes the pointed to file instead. duh. */
397#else
398 fprintf(stderr, "Cannot create symbolic links\n");
399#endif
Erik Andersene454fb62000-03-23 04:27:58 +0000400}
401
402static void
Erik Andersen1ad302a2000-03-24 00:54:46 +0000403tarExtractSpecial(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000404{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000405 if (extractFlag==FALSE || tostdoutFlag==TRUE)
406 return;
407
408 if (S_ISCHR(header->mode) || S_ISBLK(header->mode) || S_ISSOCK(header->mode)) {
409 mknod(header->name, header->mode, makedev(header->devmajor, header->devminor));
410 } else if (S_ISFIFO(header->mode)) {
411 mkfifo(header->name, header->mode);
412 } else {
413 open(header->name, O_WRONLY | O_CREAT | O_TRUNC, header->mode);
414 }
415
416 /* Now set permissions etc for the new directory */
417 fixUpPermissions(header);
Erik Andersene454fb62000-03-23 04:27:58 +0000418}
419
Erik Andersen1ad302a2000-03-24 00:54:46 +0000420/* Read an octal value in a field of the specified width, with optional
Erik Andersen298854f2000-03-23 01:09:18 +0000421 * spaces on both sides of the number and with an optional null character
Erik Andersen1ad302a2000-03-24 00:54:46 +0000422 * at the end. Returns -1 on an illegal format. */
Erik Andersen298854f2000-03-23 01:09:18 +0000423static long getOctal(const char *cp, int size)
Eric Andersencc8ed391999-10-05 16:24:54 +0000424{
Erik Andersen298854f2000-03-23 01:09:18 +0000425 long val = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000426
Erik Andersen298854f2000-03-23 01:09:18 +0000427 for(;(size > 0) && (*cp == ' '); cp++, size--);
428 if ((size == 0) || !isOctal(*cp))
429 return -1;
430 for(; (size > 0) && isOctal(*cp); size--) {
431 val = val * 8 + *cp++ - '0';
Eric Andersencc8ed391999-10-05 16:24:54 +0000432 }
Erik Andersen298854f2000-03-23 01:09:18 +0000433 for (;(size > 0) && (*cp == ' '); cp++, size--);
434 if ((size > 0) && *cp)
435 return -1;
436 return val;
437}
Eric Andersencc8ed391999-10-05 16:24:54 +0000438
Erik Andersen1ad302a2000-03-24 00:54:46 +0000439
Erik Andersen298854f2000-03-23 01:09:18 +0000440/* Parse the tar header and fill in the nice struct with the details */
441static int
442parseTarHeader(struct TarHeader *rawHeader, struct TarInfo *header)
443{
Erik Andersene454fb62000-03-23 04:27:58 +0000444 int i;
445 long chksum, sum;
446 unsigned char *s = (unsigned char *)rawHeader;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000447
Erik Andersen298854f2000-03-23 01:09:18 +0000448 header->name = rawHeader->name;
449 header->mode = getOctal(rawHeader->mode, sizeof(rawHeader->mode));
450 header->uid = getOctal(rawHeader->uid, sizeof(rawHeader->uid));
451 header->gid = getOctal(rawHeader->gid, sizeof(rawHeader->gid));
452 header->size = getOctal(rawHeader->size, sizeof(rawHeader->size));
453 header->mtime = getOctal(rawHeader->mtime, sizeof(rawHeader->mtime));
454 chksum = getOctal(rawHeader->chksum, sizeof(rawHeader->chksum));
455 header->type = rawHeader->typeflag;
456 header->linkname = rawHeader->linkname;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000457 header->devmajor = getOctal(rawHeader->devmajor, sizeof(rawHeader->devmajor));
458 header->devminor = getOctal(rawHeader->devminor, sizeof(rawHeader->devminor));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000459
Erik Andersen298854f2000-03-23 01:09:18 +0000460 /* Check the checksum */
461 sum = ' ' * sizeof(rawHeader->chksum);
462 for ( i = TarChecksumOffset; i > 0; i-- )
463 sum += *s++;
Erik Andersene454fb62000-03-23 04:27:58 +0000464 s += sizeof(rawHeader->chksum);
465 for ( i = (512 - TarChecksumOffset - sizeof(rawHeader->chksum)); i > 0; i-- )
Erik Andersen298854f2000-03-23 01:09:18 +0000466 sum += *s++;
Erik Andersene454fb62000-03-23 04:27:58 +0000467 if (sum == chksum )
Erik Andersen298854f2000-03-23 01:09:18 +0000468 return ( TRUE);
469 return( FALSE);
470}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000471
Erik Andersene49d5ec2000-02-08 19:58:47 +0000472
Erik Andersen1ad302a2000-03-24 00:54:46 +0000473/*
474 * Read a tar file and extract or list the specified files within it.
475 * If the list is empty than all files are extracted or listed.
476 */
477static int readTarFile(const char* tarName, int extractFlag, int listFlag,
478 int tostdoutFlag, int verboseFlag)
479{
480 int status, tarFd=0;
481 int errorFlag=FALSE;
482 TarHeader rawHeader;
483 TarInfo header;
484 int alreadyWarned=FALSE;
485 //int skipFileFlag=FALSE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000486
Erik Andersen1ad302a2000-03-24 00:54:46 +0000487 /* Open the tar file for reading. */
488 if (!strcmp(tarName, "-"))
489 tarFd = fileno(stdin);
490 else
491 tarFd = open(tarName, O_RDONLY);
492 if (tarFd < 0) {
Erik Andersendeea0482000-03-25 23:09:29 +0000493 errorMsg( "Error opening '%s': %s\n", tarName, strerror(errno));
Erik Andersen1ad302a2000-03-24 00:54:46 +0000494 return ( FALSE);
Erik Andersen06936df2000-01-23 02:14:20 +0000495 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000496
Erik Andersene49d5ec2000-02-08 19:58:47 +0000497 /* Set the umask for this process so it doesn't
Erik Andersen1ad302a2000-03-24 00:54:46 +0000498 * screw up permission setting for us later. */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000499 umask(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000500
Erik Andersen1ad302a2000-03-24 00:54:46 +0000501 /* Read the tar file, and iterate over it one file at a time */
502 while ( (status = fullRead(tarFd, (char*)&rawHeader, TAR_BLOCK_SIZE)) == TAR_BLOCK_SIZE ) {
Erik Andersene2729152000-02-18 21:34:17 +0000503
Erik Andersen1ad302a2000-03-24 00:54:46 +0000504 /* First, try to read the header */
505 if ( parseTarHeader(&rawHeader, &header) == FALSE ) {
506 close( tarFd);
507 if ( *(header.name) == '\0' ) {
508 goto endgame;
509 } else {
510 errorFlag=TRUE;
511 errorMsg("Bad tar header, skipping\n");
512 continue;
513 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000514 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000515 if ( *(header.name) == '\0' )
516 goto endgame;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000517
Erik Andersen1ad302a2000-03-24 00:54:46 +0000518 /* Check for and relativify any absolute paths */
519 if ( *(header.name) == '/' ) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000520
Erik Andersen1ad302a2000-03-24 00:54:46 +0000521 while (*(header.name) == '/')
522 ++*(header.name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000523
Erik Andersen1ad302a2000-03-24 00:54:46 +0000524 if (alreadyWarned == FALSE) {
525 errorMsg("Absolute path detected, removing leading slashes\n");
526 alreadyWarned = TRUE;
527 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000528 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000529
530 /* Special treatment if the list (-t) flag is on */
531 if (verboseFlag == TRUE && extractFlag == FALSE) {
532 int len, len1;
533 char buf[35];
534 struct tm *tm = localtime (&(header.mtime));
535
536 len=printf("%s %d/%-d ", modeString(header.mode), header.uid, header.gid);
537 if (header.type==CHRTYPE || header.type==BLKTYPE) {
538 len1=snprintf(buf, sizeof(buf), "%ld,%-ld ",
539 header.devmajor, header.devminor);
540 } else {
541 len1=snprintf(buf, sizeof(buf), "%d ", header.size);
542 }
543 /* Jump through some hoops to make the columns match up */
544 for(;(len+len1)<31;len++)
545 printf(" ");
546 printf(buf);
547
548 /* Use ISO 8610 time format */
549 if (tm) {
550 printf ("%04d-%02d-%02d %02d:%02d:%02d ",
551 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
552 tm->tm_hour, tm->tm_min, tm->tm_sec);
553 }
554 }
555 /* List contents if we are supposed to do that */
556 if (verboseFlag == TRUE || listFlag == TRUE) {
557 /* Now the normal listing */
558 printf("%s", header.name);
559 /* If this is a link, say so */
560 if (header.type==LNKTYPE)
561 printf(" link to %s", header.linkname);
562 else if (header.type==SYMTYPE)
563 printf(" -> %s", header.linkname);
564 printf("\n");
565 }
566
567#if 0
568 /* See if we want to restore this file or not */
569 skipFileFlag=FALSE;
570 if (wantFileName(outName) == FALSE) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000571 skipFileFlag = TRUE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000572 }
Erik Andersen05100cd2000-01-16 01:30:52 +0000573#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000574
Erik Andersen1ad302a2000-03-24 00:54:46 +0000575 /* If we got here, we can be certain we have a legitimate
576 * header to work with. So work with it. */
577 switch ( header.type ) {
578 case REGTYPE:
579 case REGTYPE0:
580 /* If the name ends in a '/' then assume it is
581 * supposed to be a directory, and fall through */
582 if (header.name[strlen(header.name)-1] != '/') {
583 tarExtractRegularFile(&header, extractFlag, tostdoutFlag);
584 break;
585 }
586 case DIRTYPE:
587 tarExtractDirectory( &header, extractFlag, tostdoutFlag);
588 break;
589 case LNKTYPE:
590 tarExtractHardLink( &header, extractFlag, tostdoutFlag);
591 break;
592 case SYMTYPE:
593 tarExtractSymLink( &header, extractFlag, tostdoutFlag);
594 break;
595 case CHRTYPE:
596 case BLKTYPE:
597 case FIFOTYPE:
598 tarExtractSpecial( &header, extractFlag, tostdoutFlag);
599 break;
600 default:
601 close( tarFd);
602 return( FALSE);
603 }
604 }
605 close(tarFd);
606 if (status > 0) {
607 /* Bummer - we read a partial header */
Erik Andersendeea0482000-03-25 23:09:29 +0000608 errorMsg( "Error reading '%s': %s\n", tarName, strerror(errno));
Erik Andersen1ad302a2000-03-24 00:54:46 +0000609 return ( FALSE);
610 }
611 else
612 return( status);
613
614 /* Stuff to do when we are done */
615endgame:
616 close( tarFd);
617 if ( *(header.name) == '\0' ) {
618 if (errorFlag==FALSE)
619 return( TRUE);
620 }
621 return( FALSE);
622}
623
Erik Andersen6acaa402000-03-26 14:03:20 +0000624
625#ifdef BB_FEATURE_TAR_CREATE
626
627/* Put an octal string into the specified buffer.
628 * The number is zero and space padded and possibly null padded.
629 * Returns TRUE if successful. */
630static int putOctal (char *cp, int len, long value)
631{
632 int tempLength;
633 char *tempString;
634 char tempBuffer[32];
635
636 /* Create a string of the specified length with an initial space,
637 * leading zeroes and the octal number, and a trailing null. */
638 tempString = tempBuffer;
639
640 sprintf (tempString, " %0*lo", len - 2, value);
641
642 tempLength = strlen (tempString) + 1;
643
644 /* If the string is too large, suppress the leading space. */
645 if (tempLength > len) {
646 tempLength--;
647 tempString++;
648 }
649
650 /* If the string is still too large, suppress the trailing null. */
651 if (tempLength > len)
652 tempLength--;
653
654 /* If the string is still too large, fail. */
655 if (tempLength > len)
656 return FALSE;
657
658 /* Copy the string to the field. */
659 memcpy (cp, tempString, len);
660
661 return TRUE;
662}
663
664static int fileAction(const char *fileName, struct stat *statbuf)
665{
666 fprintf(stdout, "%s\n", fileName);
667 return (TRUE);
668}
669
670static int writeTarFile(const char* tarName, int extractFlag, int listFlag,
671 int tostdoutFlag, int verboseFlag, int argc, char **argv)
672{
673 int tarFd=0;
674 //int errorFlag=FALSE;
675 //TarHeader rawHeader;
676 //TarInfo header;
677 //int alreadyWarned=FALSE;
678 char *directory = ".";
679 //int skipFileFlag=FALSE;
680
681 /* Open the tar file for writing. */
682 if (!strcmp(tarName, "-"))
683 tarFd = fileno(stdout);
684 else
685 tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
686 if (tarFd < 0) {
687 errorMsg( "Error opening '%s': %s\n", tarName, strerror(errno));
688 return ( FALSE);
689 }
690
691 /* Set the umask for this process so it doesn't
692 * screw up permission setting for us later. */
693 umask(0);
694
695 /* Read the directory/files and iterate over them one at a time */
696 if (recursiveAction(directory, TRUE, FALSE, FALSE,
697 fileAction, fileAction) == FALSE) {
698 exit(FALSE);
699 }
700
701
702 // TODO: DO STUFF HERE
703 close(tarFd);
704 return( TRUE);
705}
706
707
708#endif
709