blob: 9459dd6d5b43a9b843ccf235e950504c18af1a27 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002/*
Erik Andersen68a9ea42000-04-04 18:39:50 +00003 * Mini tar implementation for busybox
Erik Andersen6acaa402000-03-26 14:03:20 +00004 *
Erik Andersen61677fe2000-04-13 01:18:56 +00005 * Note, that as of BusyBox-0.43, tar has been completely rewritten from the
6 * ground up. It still has remnents of the old code lying about, but it is
7 * very different now (i.e. cleaner, less global variables, etc)
Eric Andersenc4996011999-10-20 22:08:37 +00008 *
Eric Andersen8ec10a92001-01-27 09:33:39 +00009 * Copyright (C) 1999,2000,2001 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
Eric Andersencc8ed391999-10-05 16:24:54 +000039#include <stdio.h>
40#include <dirent.h>
41#include <errno.h>
42#include <fcntl.h>
43#include <signal.h>
44#include <time.h>
Erik Andersen7dc16072000-01-04 01:10:25 +000045#include <utime.h>
Eric Andersen96bcfd31999-11-12 01:30:18 +000046#include <sys/types.h>
Eric Andersen08b10341999-11-19 02:38:58 +000047#include <sys/sysmacros.h>
Matt Kraai43c8c382000-09-04 16:51:55 +000048#include <getopt.h>
Matt Kraaibe7499c2001-01-03 17:22:10 +000049#include <fnmatch.h>
Eric Andersened3ef502001-01-27 08:24:39 +000050#include <string.h>
51#include <stdlib.h>
52#include <unistd.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000053#include "busybox.h"
54#define BB_DECLARE_EXTERN
55#define bb_need_io_error
56#define bb_need_name_longer_than_foo
57#include "messages.c"
Eric Andersencc8ed391999-10-05 16:24:54 +000058
Erik Andersen298854f2000-03-23 01:09:18 +000059/* Tar file constants */
Erik Andersen1ad302a2000-03-24 00:54:46 +000060#ifndef MAJOR
61#define MAJOR(dev) (((dev)>>8)&0xff)
62#define MINOR(dev) ((dev)&0xff)
63#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000064
Mark Whitley59ab0252001-01-23 22:30:04 +000065enum { NAME_SIZE = 100 }; /* because gcc won't let me use 'static const int' */
Eric Andersencc8ed391999-10-05 16:24:54 +000066
Erik Andersen298854f2000-03-23 01:09:18 +000067/* POSIX tar Header Block, from POSIX 1003.1-1990 */
68struct TarHeader
69{
70 /* byte offset */
Eric Andersen1b1cfde2000-09-24 00:54:37 +000071 char name[NAME_SIZE]; /* 0-99 */
Erik Andersen0817d132000-04-09 15:17:40 +000072 char mode[8]; /* 100-107 */
73 char uid[8]; /* 108-115 */
74 char gid[8]; /* 116-123 */
75 char size[12]; /* 124-135 */
76 char mtime[12]; /* 136-147 */
77 char chksum[8]; /* 148-155 */
78 char typeflag; /* 156-156 */
Eric Andersen1b1cfde2000-09-24 00:54:37 +000079 char linkname[NAME_SIZE]; /* 157-256 */
Erik Andersen0817d132000-04-09 15:17:40 +000080 char magic[6]; /* 257-262 */
81 char version[2]; /* 263-264 */
82 char uname[32]; /* 265-296 */
83 char gname[32]; /* 297-328 */
84 char devmajor[8]; /* 329-336 */
85 char devminor[8]; /* 337-344 */
86 char prefix[155]; /* 345-499 */
87 char padding[12]; /* 500-512 (pad to exactly the TAR_BLOCK_SIZE) */
Erik Andersen298854f2000-03-23 01:09:18 +000088};
89typedef struct TarHeader TarHeader;
Eric Andersencc8ed391999-10-05 16:24:54 +000090
Eric Andersencc8ed391999-10-05 16:24:54 +000091
Erik Andersen298854f2000-03-23 01:09:18 +000092/* A few useful constants */
93#define TAR_MAGIC "ustar" /* ustar and a null */
Erik Andersen0817d132000-04-09 15:17:40 +000094#define TAR_VERSION " " /* Be compatable with GNU tar format */
Mark Whitley59ab0252001-01-23 22:30:04 +000095static const int TAR_MAGIC_LEN = 6;
96static const int TAR_VERSION_LEN = 2;
97static const int TAR_BLOCK_SIZE = 512;
Erik Andersen298854f2000-03-23 01:09:18 +000098
99/* A nice enum with all the possible tar file content types */
100enum TarFileType
101{
102 REGTYPE = '0', /* regular file */
103 REGTYPE0 = '\0', /* regular file (ancient bug compat)*/
104 LNKTYPE = '1', /* hard link */
105 SYMTYPE = '2', /* symbolic link */
106 CHRTYPE = '3', /* character special */
107 BLKTYPE = '4', /* block special */
108 DIRTYPE = '5', /* directory */
109 FIFOTYPE = '6', /* FIFO special */
110 CONTTYPE = '7', /* reserved */
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000111 GNULONGLINK = 'K', /* GNU long (>100 chars) link name */
112 GNULONGNAME = 'L', /* GNU long (>100 chars) file name */
Erik Andersen298854f2000-03-23 01:09:18 +0000113};
114typedef enum TarFileType TarFileType;
115
116/* This struct ignores magic, non-numeric user name,
117 * non-numeric group name, and the checksum, since
118 * these are all ignored by BusyBox tar. */
119struct TarInfo
120{
121 int tarFd; /* An open file descriptor for reading from the tarball */
122 char * name; /* File name */
123 mode_t mode; /* Unix mode, including device bits. */
124 uid_t uid; /* Numeric UID */
125 gid_t gid; /* Numeric GID */
126 size_t size; /* Size of file */
127 time_t mtime; /* Last-modified time */
128 enum TarFileType type; /* Regular, directory, link, etc */
129 char * linkname; /* Name for symbolic and hard links */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000130 long devmajor; /* Major number for special device */
131 long devminor; /* Minor number for special device */
Erik Andersen298854f2000-03-23 01:09:18 +0000132};
133typedef struct TarInfo TarInfo;
134
Erik Andersene454fb62000-03-23 04:27:58 +0000135/* Local procedures to restore files from a tar file. */
Glenn L McGrath7541e3a2001-01-02 23:41:50 +0000136extern int readTarFile(int tarFd, int extractFlag, int listFlag,
Matt Kraaib92223b2000-09-04 08:25:42 +0000137 int tostdoutFlag, int verboseFlag, char** extractList,
138 char** excludeList);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000139
Erik Andersen05100cd2000-01-16 01:30:52 +0000140#ifdef BB_FEATURE_TAR_CREATE
Erik Andersen6acaa402000-03-26 14:03:20 +0000141/* Local procedures to save files into a tar file. */
Matt Kraaid8ad76c2000-11-08 02:35:47 +0000142static int writeTarFile(const char* tarName, int verboseFlag, char **argv,
143 char** excludeList);
Erik Andersen05100cd2000-01-16 01:30:52 +0000144#endif
145
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000146#if defined BB_FEATURE_TAR_EXCLUDE
Eric Andersen3e6ff902001-03-09 21:24:12 +0000147static struct option longopts[] = {
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000148 { "exclude", 1, NULL, 'e' },
149 { NULL, 0, NULL, 0 }
150};
151#endif
152
Erik Andersene49d5ec2000-02-08 19:58:47 +0000153extern int tar_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000154{
Erik Andersenecd51242000-04-08 03:08:21 +0000155 char** excludeList=NULL;
Matt Kraaic119ab92000-11-30 04:44:54 +0000156 char** extractList=NULL;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000157 const char *tarName="-";
Erik Andersen0817d132000-04-09 15:17:40 +0000158#if defined BB_FEATURE_TAR_EXCLUDE
Erik Andersenecd51242000-04-08 03:08:21 +0000159 int excludeListSize=0;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000160 FILE *fileList;
161 char file[256];
Erik Andersen0817d132000-04-09 15:17:40 +0000162#endif
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000163#if defined BB_FEATURE_TAR_GZIP
Glenn L McGrath018e9e62001-03-28 07:27:26 +0000164 FILE *comp_file = NULL;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000165 int unzipFlag = FALSE;
166#endif
Erik Andersen298854f2000-03-23 01:09:18 +0000167 int listFlag = FALSE;
168 int extractFlag = FALSE;
169 int createFlag = FALSE;
170 int verboseFlag = FALSE;
171 int tostdoutFlag = FALSE;
Eric Andersen02f3b2e2000-12-01 19:04:52 +0000172 int status = FALSE;
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000173 int opt;
Glenn L McGrath018e9e62001-03-28 07:27:26 +0000174 pid_t pid;
Eric Andersencc8ed391999-10-05 16:24:54 +0000175
Erik Andersenecd51242000-04-08 03:08:21 +0000176 if (argc <= 1)
Eric Andersen67991cf2001-02-14 21:23:06 +0000177 show_usage();
Eric Andersencc8ed391999-10-05 16:24:54 +0000178
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000179 if (argv[1][0] != '-') {
180 char *tmp = xmalloc(strlen(argv[1]) + 2);
181 tmp[0] = '-';
182 strcpy(tmp + 1, argv[1]);
183 argv[1] = tmp;
184 }
185
186 while (
187#ifndef BB_FEATURE_TAR_EXCLUDE
188 (opt = getopt(argc, argv, "cxtzvOf:"))
189#else
190 (opt = getopt_long(argc, argv, "cxtzvOf:X:", longopts, NULL))
191#endif
192 > 0) {
193 switch (opt) {
194 case 'c':
195 if (extractFlag == TRUE || listFlag == TRUE)
196 goto flagError;
197 createFlag = TRUE;
198 break;
199 case 'x':
200 if (listFlag == TRUE || createFlag == TRUE)
201 goto flagError;
202 extractFlag = TRUE;
203 break;
204 case 't':
205 if (extractFlag == TRUE || createFlag == TRUE)
206 goto flagError;
207 listFlag = TRUE;
208 break;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000209#ifdef BB_FEATURE_TAR_GZIP
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000210 case 'z':
211 unzipFlag = TRUE;
212 break;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000213#endif
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000214 case 'v':
215 verboseFlag = TRUE;
216 break;
217 case 'O':
218 tostdoutFlag = TRUE;
219 break;
220 case 'f':
221 if (*tarName != '-')
Matt Kraaidd19c692001-01-31 19:00:21 +0000222 error_msg_and_die( "Only one 'f' option allowed");
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000223 tarName = optarg;
224 break;
Erik Andersen0817d132000-04-09 15:17:40 +0000225#if defined BB_FEATURE_TAR_EXCLUDE
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000226 case 'e':
227 excludeList=xrealloc( excludeList,
228 sizeof(char *) * (excludeListSize+2));
229 excludeList[excludeListSize] = optarg;
230 /* Tack a NULL onto the end of the list */
231 excludeList[++excludeListSize] = NULL;
232 case 'X':
233 fileList = xfopen(optarg, "r");
234 while (fgets(file, sizeof(file), fileList) != NULL) {
235 excludeList = xrealloc(excludeList,
236 sizeof(char *) * (excludeListSize+2));
Matt Kraai05e782d2001-02-01 16:49:30 +0000237 chomp(file);
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000238 excludeList[excludeListSize] = xstrdup(file);
239 /* Tack a NULL onto the end of the list */
240 excludeList[++excludeListSize] = NULL;
241 }
242 fclose(fileList);
243 break;
Erik Andersen0817d132000-04-09 15:17:40 +0000244#endif
Erik Andersenecd51242000-04-08 03:08:21 +0000245 default:
Eric Andersen67991cf2001-02-14 21:23:06 +0000246 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000247 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000248 }
249
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000250 /*
Erik Andersene49d5ec2000-02-08 19:58:47 +0000251 * Do the correct type of action supplying the rest of the
252 * command line arguments as the list of files to process.
253 */
254 if (createFlag == TRUE) {
Erik Andersende552872000-01-23 01:34:05 +0000255#ifndef BB_FEATURE_TAR_CREATE
Matt Kraaidd19c692001-01-31 19:00:21 +0000256 error_msg_and_die( "This version of tar was not compiled with tar creation support.");
Erik Andersende552872000-01-23 01:34:05 +0000257#else
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000258#ifdef BB_FEATURE_TAR_GZIP
259 if (unzipFlag==TRUE)
Matt Kraaidd19c692001-01-31 19:00:21 +0000260 error_msg_and_die("Creation of compressed not internally support by tar, pipe to busybox gunzip");
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000261#endif
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000262 status = writeTarFile(tarName, verboseFlag, argv + optind, excludeList);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000263#endif
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000264 }
265 if (listFlag == TRUE || extractFlag == TRUE) {
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000266 int tarFd;
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000267 if (argv[optind])
268 extractList = argv + optind;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000269 /* Open the tar file for reading. */
270 if (!strcmp(tarName, "-"))
271 tarFd = fileno(stdin);
272 else
273 tarFd = open(tarName, O_RDONLY);
274 if (tarFd < 0)
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000275 perror_msg_and_die("Error opening '%s'", tarName);
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000276
277#ifdef BB_FEATURE_TAR_GZIP
278 /* unzip tarFd in a seperate process */
Glenn L McGrath018e9e62001-03-28 07:27:26 +0000279 if (unzipFlag == TRUE) {
280 comp_file = fdopen(tarFd, "r");
Glenn L McGrath018e9e62001-03-28 07:27:26 +0000281 if ((tarFd = gz_open(comp_file, &pid)) == EXIT_FAILURE) {
282 error_msg_and_die("Couldnt unzip file");
283 }
Glenn L McGrath018e9e62001-03-28 07:27:26 +0000284 }
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000285#endif
286 status = readTarFile(tarFd, extractFlag, listFlag, tostdoutFlag,
Matt Kraai3e856ce2000-12-01 02:55:13 +0000287 verboseFlag, extractList, excludeList);
Glenn L McGrath018e9e62001-03-28 07:27:26 +0000288 close(tarFd);
Glenn L McGrath018e9e62001-03-28 07:27:26 +0000289#ifdef BB_FEATURE_TAR_GZIP
Glenn L McGrathae8ad352001-03-28 23:57:51 +0000290 if (unzipFlag == TRUE) {
291 gz_close(pid);
292 fclose(comp_file);
293 }
Glenn L McGrath018e9e62001-03-28 07:27:26 +0000294#endif
Glenn L McGrathae8ad352001-03-28 23:57:51 +0000295 }
Glenn L McGrath018e9e62001-03-28 07:27:26 +0000296
Matt Kraai3e856ce2000-12-01 02:55:13 +0000297 if (status == TRUE)
298 return EXIT_SUCCESS;
299 else
300 return EXIT_FAILURE;
301
Erik Andersene49d5ec2000-02-08 19:58:47 +0000302 flagError:
Matt Kraaidd19c692001-01-31 19:00:21 +0000303 error_msg_and_die( "Exactly one of 'c', 'x' or 't' must be specified");
Erik Andersen298854f2000-03-23 01:09:18 +0000304}
305
306static void
Erik Andersen6a34b532000-04-07 06:55:38 +0000307fixUpPermissions(TarInfo *header)
308{
309 struct utimbuf t;
310 /* Now set permissions etc for the new file */
311 chown(header->name, header->uid, header->gid);
312 chmod(header->name, header->mode);
313 /* Reset the time */
314 t.actime = time(0);
315 t.modtime = header->mtime;
316 utime(header->name, &t);
317}
318
319static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000320tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersen298854f2000-03-23 01:09:18 +0000321{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000322 size_t writeSize;
323 size_t readSize;
324 size_t actualWriteSz;
325 char buffer[BUFSIZ];
326 size_t size = header->size;
327 int outFd=fileno(stdout);
328
329 /* Open the file to be written, if a file is supposed to be written */
330 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000331 /* Create the path to the file, just in case it isn't there...
332 * This should not screw up path permissions or anything. */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000333 create_path(header->name, 0777);
Eric Andersen0c6a9702000-06-09 20:51:50 +0000334 if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY,
335 header->mode & ~S_IFMT)) < 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000336 error_msg(io_error, header->name, strerror(errno));
Eric Andersen0c6a9702000-06-09 20:51:50 +0000337 return( FALSE);
338 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000339 }
340
341 /* Write out the file, if we are supposed to be doing that */
342 while ( size > 0 ) {
343 actualWriteSz=0;
344 if ( size > sizeof(buffer) )
345 writeSize = readSize = sizeof(buffer);
346 else {
347 int mod = size % 512;
348 if ( mod != 0 )
349 readSize = size + (512 - mod);
350 else
351 readSize = size;
352 writeSize = size;
353 }
Mark Whitleyf57c9442000-12-07 19:56:48 +0000354 if ( (readSize = full_read(header->tarFd, buffer, readSize)) <= 0 ) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000355 /* Tarball seems to have a problem */
Matt Kraaidd19c692001-01-31 19:00:21 +0000356 error_msg("Unexpected EOF in archive");
Erik Andersen6a34b532000-04-07 06:55:38 +0000357 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000358 }
359 if ( readSize < writeSize )
360 writeSize = readSize;
361
362 /* Write out the file, if we are supposed to be doing that */
363 if (extractFlag==TRUE) {
364
Mark Whitleyf57c9442000-12-07 19:56:48 +0000365 if ((actualWriteSz=full_write(outFd, buffer, writeSize)) != writeSize ) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000366 /* Output file seems to have a problem */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000367 error_msg(io_error, header->name, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000368 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000369 }
Erik Andersen0817d132000-04-09 15:17:40 +0000370 } else {
371 actualWriteSz=writeSize;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000372 }
373
374 size -= actualWriteSz;
375 }
376
377 /* Now we are done writing the file out, so try
378 * and fix up the permissions and whatnot */
379 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000380 close(outFd);
Erik Andersen6a34b532000-04-07 06:55:38 +0000381 fixUpPermissions(header);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000382 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000383 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000384}
385
Erik Andersen6a34b532000-04-07 06:55:38 +0000386static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000387tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000388{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000389
390 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000391 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000392
Mark Whitleyf57c9442000-12-07 19:56:48 +0000393 if (create_path(header->name, header->mode) != TRUE) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000394 perror_msg("%s: Cannot mkdir", header->name);
Erik Andersen6a34b532000-04-07 06:55:38 +0000395 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000396 }
397 /* make the final component, just in case it was
Mark Whitleyf57c9442000-12-07 19:56:48 +0000398 * omitted by create_path() (which will skip the
Erik Andersen1ad302a2000-03-24 00:54:46 +0000399 * directory if it doesn't have a terminating '/') */
Matt Kraai541ffe32001-01-13 21:46:25 +0000400 if (mkdir(header->name, header->mode) < 0 && errno != EEXIST) {
401 perror_msg("%s", header->name);
402 return FALSE;
Erik Andersen6acaa402000-03-26 14:03:20 +0000403 }
Matt Kraai541ffe32001-01-13 21:46:25 +0000404
405 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000406 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000407}
408
Erik Andersen6a34b532000-04-07 06:55:38 +0000409static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000410tarExtractHardLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000411{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000412 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000413 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000414
415 if (link(header->linkname, header->name) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000416 perror_msg("%s: Cannot create hard link to '%s'", header->name,
417 header->linkname);
Erik Andersen6a34b532000-04-07 06:55:38 +0000418 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000419 }
420
421 /* Now set permissions etc for the new directory */
422 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000423 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000424}
425
Erik Andersen6a34b532000-04-07 06:55:38 +0000426static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000427tarExtractSymLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000428{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000429 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000430 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000431
432#ifdef S_ISLNK
433 if (symlink(header->linkname, header->name) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000434 perror_msg("%s: Cannot create symlink to '%s'", header->name,
435 header->linkname);
Erik Andersen6a34b532000-04-07 06:55:38 +0000436 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000437 }
438 /* Try to change ownership of the symlink.
439 * If libs doesn't support that, don't bother.
440 * Changing the pointed-to-file is the Wrong Thing(tm).
441 */
442#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
443 lchown(header->name, header->uid, header->gid);
444#endif
445
446 /* Do not change permissions or date on symlink,
447 * since it changes the pointed to file instead. duh. */
448#else
Matt Kraaidd19c692001-01-31 19:00:21 +0000449 error_msg("%s: Cannot create symlink to '%s': %s",
Erik Andersen6a34b532000-04-07 06:55:38 +0000450 header->name, header->linkname,
451 "symlinks not supported");
Erik Andersen1ad302a2000-03-24 00:54:46 +0000452#endif
Erik Andersen6a34b532000-04-07 06:55:38 +0000453 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000454}
455
Erik Andersen6a34b532000-04-07 06:55:38 +0000456static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000457tarExtractSpecial(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000458{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000459 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000460 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000461
462 if (S_ISCHR(header->mode) || S_ISBLK(header->mode) || S_ISSOCK(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000463 if (mknod(header->name, header->mode, makedev(header->devmajor, header->devminor)) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000464 perror_msg("%s: Cannot mknod", header->name);
Erik Andersen6a34b532000-04-07 06:55:38 +0000465 return( FALSE);
466 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000467 } else if (S_ISFIFO(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000468 if (mkfifo(header->name, header->mode) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000469 perror_msg("%s: Cannot mkfifo", header->name);
Erik Andersen6a34b532000-04-07 06:55:38 +0000470 return( FALSE);
471 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000472 }
473
474 /* Now set permissions etc for the new directory */
475 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000476 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000477}
478
Erik Andersen1ad302a2000-03-24 00:54:46 +0000479/* Read an octal value in a field of the specified width, with optional
Erik Andersen298854f2000-03-23 01:09:18 +0000480 * spaces on both sides of the number and with an optional null character
Erik Andersen1ad302a2000-03-24 00:54:46 +0000481 * at the end. Returns -1 on an illegal format. */
Erik Andersen298854f2000-03-23 01:09:18 +0000482static long getOctal(const char *cp, int size)
Eric Andersencc8ed391999-10-05 16:24:54 +0000483{
Erik Andersen298854f2000-03-23 01:09:18 +0000484 long val = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000485
Erik Andersen298854f2000-03-23 01:09:18 +0000486 for(;(size > 0) && (*cp == ' '); cp++, size--);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000487 if ((size == 0) || !is_octal(*cp))
Erik Andersen298854f2000-03-23 01:09:18 +0000488 return -1;
Mark Whitleyf57c9442000-12-07 19:56:48 +0000489 for(; (size > 0) && is_octal(*cp); size--) {
Erik Andersen298854f2000-03-23 01:09:18 +0000490 val = val * 8 + *cp++ - '0';
Eric Andersencc8ed391999-10-05 16:24:54 +0000491 }
Erik Andersen298854f2000-03-23 01:09:18 +0000492 for (;(size > 0) && (*cp == ' '); cp++, size--);
493 if ((size > 0) && *cp)
494 return -1;
495 return val;
496}
Eric Andersencc8ed391999-10-05 16:24:54 +0000497
Erik Andersen1ad302a2000-03-24 00:54:46 +0000498
Erik Andersen298854f2000-03-23 01:09:18 +0000499/* Parse the tar header and fill in the nice struct with the details */
500static int
Erik Andersen3364d782000-03-28 00:58:14 +0000501readTarHeader(struct TarHeader *rawHeader, struct TarInfo *header)
Erik Andersen298854f2000-03-23 01:09:18 +0000502{
Erik Andersene454fb62000-03-23 04:27:58 +0000503 int i;
Erik Andersen84e09e42000-04-08 20:58:35 +0000504 long chksum, sum=0;
Erik Andersene454fb62000-03-23 04:27:58 +0000505 unsigned char *s = (unsigned char *)rawHeader;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000506
Erik Andersen298854f2000-03-23 01:09:18 +0000507 header->name = rawHeader->name;
Erik Andersen84e09e42000-04-08 20:58:35 +0000508 /* Check for and relativify any absolute paths */
509 if ( *(header->name) == '/' ) {
510 static int alreadyWarned=FALSE;
511
512 while (*(header->name) == '/')
513 ++*(header->name);
514
515 if (alreadyWarned == FALSE) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000516 error_msg("Removing leading '/' from member names");
Erik Andersen84e09e42000-04-08 20:58:35 +0000517 alreadyWarned = TRUE;
518 }
519 }
520
Erik Andersen298854f2000-03-23 01:09:18 +0000521 header->mode = getOctal(rawHeader->mode, sizeof(rawHeader->mode));
522 header->uid = getOctal(rawHeader->uid, sizeof(rawHeader->uid));
523 header->gid = getOctal(rawHeader->gid, sizeof(rawHeader->gid));
524 header->size = getOctal(rawHeader->size, sizeof(rawHeader->size));
525 header->mtime = getOctal(rawHeader->mtime, sizeof(rawHeader->mtime));
526 chksum = getOctal(rawHeader->chksum, sizeof(rawHeader->chksum));
527 header->type = rawHeader->typeflag;
528 header->linkname = rawHeader->linkname;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000529 header->devmajor = getOctal(rawHeader->devmajor, sizeof(rawHeader->devmajor));
530 header->devminor = getOctal(rawHeader->devminor, sizeof(rawHeader->devminor));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000531
Erik Andersen298854f2000-03-23 01:09:18 +0000532 /* Check the checksum */
Erik Andersen84e09e42000-04-08 20:58:35 +0000533 for (i = sizeof(*rawHeader); i-- != 0;) {
Erik Andersen298854f2000-03-23 01:09:18 +0000534 sum += *s++;
Erik Andersen84e09e42000-04-08 20:58:35 +0000535 }
536 /* Remove the effects of the checksum field (replace
537 * with blanks for the purposes of the checksum) */
538 s = rawHeader->chksum;
539 for (i = sizeof(rawHeader->chksum) ; i-- != 0;) {
540 sum -= *s++;
541 }
542 sum += ' ' * sizeof(rawHeader->chksum);
Erik Andersene454fb62000-03-23 04:27:58 +0000543 if (sum == chksum )
Erik Andersen298854f2000-03-23 01:09:18 +0000544 return ( TRUE);
545 return( FALSE);
546}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000547
Eric Andersen3e6ff902001-03-09 21:24:12 +0000548static int exclude_file(char **excluded_files, const char *file)
Matt Kraaibe7499c2001-01-03 17:22:10 +0000549{
550 int i;
551
552 if (excluded_files == NULL)
553 return 0;
554
555 for (i = 0; excluded_files[i] != NULL; i++) {
556 if (excluded_files[i][0] == '/') {
557 if (fnmatch(excluded_files[i], file,
558 FNM_PATHNAME | FNM_LEADING_DIR) == 0)
559 return 1;
560 } else {
561 const char *p;
562
563 for (p = file; p[0] != '\0'; p++) {
564 if ((p == file || p[-1] == '/') && p[0] != '/' &&
565 fnmatch(excluded_files[i], p,
566 FNM_PATHNAME | FNM_LEADING_DIR) == 0)
567 return 1;
568 }
569 }
570 }
571
572 return 0;
573}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000574
Eric Andersen3e6ff902001-03-09 21:24:12 +0000575static int extract_file(char **extract_files, const char *file)
Matt Kraai8f8dab92001-01-22 05:25:19 +0000576{
577 int i;
578
579 if (extract_files == NULL)
580 return 1;
581
582 for (i = 0; extract_files[i] != NULL; i++) {
583 if (fnmatch(extract_files[i], file, FNM_LEADING_DIR) == 0)
584 return 1;
585 }
586
587 return 0;
588}
589
Erik Andersen1ad302a2000-03-24 00:54:46 +0000590/*
591 * Read a tar file and extract or list the specified files within it.
592 * If the list is empty than all files are extracted or listed.
593 */
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000594extern int readTarFile(int tarFd, int extractFlag, int listFlag,
Matt Kraaib92223b2000-09-04 08:25:42 +0000595 int tostdoutFlag, int verboseFlag, char** extractList,
596 char** excludeList)
Erik Andersen1ad302a2000-03-24 00:54:46 +0000597{
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000598 int status;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000599 int errorFlag=FALSE;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000600 int skipNextHeaderFlag=FALSE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000601 TarHeader rawHeader;
602 TarInfo header;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000603
Erik Andersene49d5ec2000-02-08 19:58:47 +0000604 /* Set the umask for this process so it doesn't
Erik Andersen1ad302a2000-03-24 00:54:46 +0000605 * screw up permission setting for us later. */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000606 umask(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000607
Erik Andersen1ad302a2000-03-24 00:54:46 +0000608 /* Read the tar file, and iterate over it one file at a time */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000609 while ( (status = full_read(tarFd, (char*)&rawHeader, TAR_BLOCK_SIZE)) == TAR_BLOCK_SIZE ) {
Erik Andersene2729152000-02-18 21:34:17 +0000610
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000611 /* Try to read the header */
Erik Andersen3364d782000-03-28 00:58:14 +0000612 if ( readTarHeader(&rawHeader, &header) == FALSE ) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000613 if ( *(header.name) == '\0' ) {
614 goto endgame;
615 } else {
616 errorFlag=TRUE;
Matt Kraaidd19c692001-01-31 19:00:21 +0000617 error_msg("Bad tar header, skipping");
Erik Andersen1ad302a2000-03-24 00:54:46 +0000618 continue;
619 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000620 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000621 if ( *(header.name) == '\0' )
622 goto endgame;
Erik Andersen0817d132000-04-09 15:17:40 +0000623 header.tarFd = tarFd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000624
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000625 /* Skip funky extra GNU headers that precede long files */
626 if ( (header.type == GNULONGNAME) || (header.type == GNULONGLINK) ) {
627 skipNextHeaderFlag=TRUE;
Matt Kraaiab8f9e22000-11-18 01:28:57 +0000628 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
629 errorFlag = TRUE;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000630 continue;
631 }
632 if ( skipNextHeaderFlag == TRUE ) {
633 skipNextHeaderFlag=FALSE;
Mark Whitleyf57c9442000-12-07 19:56:48 +0000634 error_msg(name_longer_than_foo, NAME_SIZE);
Matt Kraaiab8f9e22000-11-18 01:28:57 +0000635 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
636 errorFlag = TRUE;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000637 continue;
638 }
639
Erik Andersen0817d132000-04-09 15:17:40 +0000640#if defined BB_FEATURE_TAR_EXCLUDE
Matt Kraaibe7499c2001-01-03 17:22:10 +0000641 if (exclude_file(excludeList, header.name)) {
Erik Andersen0817d132000-04-09 15:17:40 +0000642 /* There are not the droids you're looking for, move along */
Matt Kraaibe7499c2001-01-03 17:22:10 +0000643 /* If it is a regular file, pretend to extract it with
644 * the extractFlag set to FALSE, so the junk in the tarball
645 * is properly skipped over */
646 if ( header.type==REGTYPE || header.type==REGTYPE0 ) {
647 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
648 errorFlag = TRUE;
649 }
650 continue;
Erik Andersen0817d132000-04-09 15:17:40 +0000651 }
652#endif
Matt Kraaibe7499c2001-01-03 17:22:10 +0000653
Matt Kraai8f8dab92001-01-22 05:25:19 +0000654 if (!extract_file(extractList, header.name)) {
Matt Kraaib92223b2000-09-04 08:25:42 +0000655 /* There are not the droids you're looking for, move along */
Matt Kraai8f8dab92001-01-22 05:25:19 +0000656 /* If it is a regular file, pretend to extract it with
657 * the extractFlag set to FALSE, so the junk in the tarball
658 * is properly skipped over */
659 if ( header.type==REGTYPE || header.type==REGTYPE0 ) {
660 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
661 errorFlag = TRUE;
Matt Kraaib92223b2000-09-04 08:25:42 +0000662 }
Matt Kraai8f8dab92001-01-22 05:25:19 +0000663 continue;
Matt Kraaib92223b2000-09-04 08:25:42 +0000664 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000665
Matt Kraai82cfbad2000-09-15 21:18:43 +0000666 if (listFlag == TRUE) {
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000667 /* Special treatment if the list (-t) flag is on */
668 if (verboseFlag == TRUE) {
669 int len, len1;
670 char buf[35];
671 struct tm *tm = localtime (&(header.mtime));
672
Mark Whitleyf57c9442000-12-07 19:56:48 +0000673 len=printf("%s ", mode_string(header.mode));
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000674 my_getpwuid(buf, header.uid);
675 if (! *buf)
676 len+=printf("%d", header.uid);
677 else
678 len+=printf("%s", buf);
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000679 my_getgrgid(buf, header.gid);
680 if (! *buf)
681 len+=printf("/%-d ", header.gid);
682 else
683 len+=printf("/%-s ", buf);
684
685 if (header.type==CHRTYPE || header.type==BLKTYPE) {
686 len1=snprintf(buf, sizeof(buf), "%ld,%-ld ",
687 header.devmajor, header.devminor);
688 } else {
689 len1=snprintf(buf, sizeof(buf), "%lu ", (long)header.size);
690 }
691 /* Jump through some hoops to make the columns match up */
692 for(;(len+len1)<31;len++)
693 printf(" ");
694 printf(buf);
695
696 /* Use ISO 8610 time format */
697 if (tm) {
698 printf ("%04d-%02d-%02d %02d:%02d:%02d ",
699 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
700 tm->tm_hour, tm->tm_min, tm->tm_sec);
701 }
702 }
Eric Andersenfdd51032000-08-02 18:48:26 +0000703 printf("%s", header.name);
Matt Kraai82cfbad2000-09-15 21:18:43 +0000704 if (verboseFlag == TRUE) {
705 if (header.type==LNKTYPE) /* If this is a link, say so */
706 printf(" link to %s", header.linkname);
707 else if (header.type==SYMTYPE)
708 printf(" -> %s", header.linkname);
709 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000710 printf("\n");
711 }
712
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000713 /* List contents if we are supposed to do that */
714 if (verboseFlag == TRUE && extractFlag == TRUE) {
715 /* Now the normal listing */
716 FILE *vbFd = stdout;
717 if (tostdoutFlag == TRUE) // If the archive goes to stdout, verbose to stderr
718 vbFd = stderr;
719 fprintf(vbFd, "%s\n", header.name);
720 }
721
Matt Kraaif4462972000-09-01 02:50:48 +0000722 /* Remove files if we would overwrite them */
Matt Kraaida542f32000-09-01 02:53:01 +0000723 if (extractFlag == TRUE && tostdoutFlag == FALSE)
Matt Kraaif4462972000-09-01 02:50:48 +0000724 unlink(header.name);
Eric Andersencc8ed391999-10-05 16:24:54 +0000725
Erik Andersen1ad302a2000-03-24 00:54:46 +0000726 /* If we got here, we can be certain we have a legitimate
727 * header to work with. So work with it. */
728 switch ( header.type ) {
729 case REGTYPE:
730 case REGTYPE0:
731 /* If the name ends in a '/' then assume it is
732 * supposed to be a directory, and fall through */
733 if (header.name[strlen(header.name)-1] != '/') {
Erik Andersen6a34b532000-04-07 06:55:38 +0000734 if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE)
735 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000736 break;
737 }
738 case DIRTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000739 if (tarExtractDirectory( &header, extractFlag, tostdoutFlag)==FALSE)
740 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000741 break;
742 case LNKTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000743 if (tarExtractHardLink( &header, extractFlag, tostdoutFlag)==FALSE)
744 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000745 break;
746 case SYMTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000747 if (tarExtractSymLink( &header, extractFlag, tostdoutFlag)==FALSE)
748 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000749 break;
750 case CHRTYPE:
751 case BLKTYPE:
752 case FIFOTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000753 if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE)
754 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000755 break;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000756#if 0
757 /* Handled earlier */
758 case GNULONGNAME:
759 case GNULONGLINK:
760 skipNextHeaderFlag=TRUE;
761 break;
762#endif
Erik Andersen1ad302a2000-03-24 00:54:46 +0000763 default:
Matt Kraaidd19c692001-01-31 19:00:21 +0000764 error_msg("Unknown file type '%c' in tar file", header.type);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000765 close( tarFd);
766 return( FALSE);
767 }
768 }
769 close(tarFd);
770 if (status > 0) {
771 /* Bummer - we read a partial header */
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000772 perror_msg("Error reading tar file");
Erik Andersen1ad302a2000-03-24 00:54:46 +0000773 return ( FALSE);
774 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000775 else if (errorFlag==TRUE) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000776 error_msg( "Error exit delayed from previous errors");
Erik Andersen6a34b532000-04-07 06:55:38 +0000777 return( FALSE);
778 } else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000779 return( status);
780
781 /* Stuff to do when we are done */
782endgame:
783 close( tarFd);
784 if ( *(header.name) == '\0' ) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000785 if (errorFlag==TRUE)
Matt Kraaidd19c692001-01-31 19:00:21 +0000786 error_msg( "Error exit delayed from previous errors");
Erik Andersen6a34b532000-04-07 06:55:38 +0000787 else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000788 return( TRUE);
789 }
790 return( FALSE);
791}
792
Erik Andersen6acaa402000-03-26 14:03:20 +0000793
794#ifdef BB_FEATURE_TAR_CREATE
795
Eric Andersen3d957c82000-12-07 00:34:58 +0000796/*
797** writeTarFile(), writeFileToTarball(), and writeTarHeader() are
798** the only functions that deal with the HardLinkInfo structure.
799** Even these functions use the xxxHardLinkInfo() functions.
800*/
801typedef struct HardLinkInfo HardLinkInfo;
802struct HardLinkInfo
803{
804 HardLinkInfo *next; /* Next entry in list */
805 dev_t dev; /* Device number */
806 ino_t ino; /* Inode number */
807 short linkCount; /* (Hard) Link Count */
808 char name[1]; /* Start of filename (must be last) */
809};
810
Erik Andersen68a9ea42000-04-04 18:39:50 +0000811/* Some info to be carried along when creating a new tarball */
812struct TarBallInfo
813{
814 char* fileName; /* File name of the tarball */
815 int tarFd; /* Open-for-write file descriptor
816 for the tarball */
817 struct stat statBuf; /* Stat info for the tarball, letting
818 us know the inode and device that the
819 tarball lives, so we can avoid trying
820 to include the tarball into itself */
821 int verboseFlag; /* Whether to print extra stuff or not */
Erik Andersenecd51242000-04-08 03:08:21 +0000822 char** excludeList; /* List of files to not include */
Eric Andersen3d957c82000-12-07 00:34:58 +0000823 HardLinkInfo *hlInfoHead; /* Hard Link Tracking Information */
824 HardLinkInfo *hlInfo; /* Hard Link Info for the current file */
Erik Andersen68a9ea42000-04-04 18:39:50 +0000825};
826typedef struct TarBallInfo TarBallInfo;
827
828
Eric Andersen3d957c82000-12-07 00:34:58 +0000829/* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */
830static void
831addHardLinkInfo (HardLinkInfo **hlInfoHeadPtr, dev_t dev, ino_t ino,
832 short linkCount, const char *name)
833{
834 /* Note: hlInfoHeadPtr can never be NULL! */
835 HardLinkInfo *hlInfo;
836
837 hlInfo = (HardLinkInfo *)xmalloc(sizeof(HardLinkInfo)+strlen(name)+1);
838 if (hlInfo) {
839 hlInfo->next = *hlInfoHeadPtr;
840 *hlInfoHeadPtr = hlInfo;
841 hlInfo->dev = dev;
842 hlInfo->ino = ino;
843 hlInfo->linkCount = linkCount;
844 strcpy(hlInfo->name, name);
845 }
846 return;
847}
848
849static void
850freeHardLinkInfo (HardLinkInfo **hlInfoHeadPtr)
851{
852 HardLinkInfo *hlInfo = NULL;
853 HardLinkInfo *hlInfoNext = NULL;
854
855 if (hlInfoHeadPtr) {
856 hlInfo = *hlInfoHeadPtr;
857 while (hlInfo) {
858 hlInfoNext = hlInfo->next;
859 free(hlInfo);
860 hlInfo = hlInfoNext;
861 }
862 *hlInfoHeadPtr = NULL;
863 }
864 return;
865}
866
867/* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */
868static HardLinkInfo *
869findHardLinkInfo (HardLinkInfo *hlInfo, dev_t dev, ino_t ino)
870{
871 while(hlInfo) {
872 if ((ino == hlInfo->ino) && (dev == hlInfo->dev))
873 break;
874 hlInfo = hlInfo->next;
875 }
876 return(hlInfo);
877}
878
Erik Andersen6acaa402000-03-26 14:03:20 +0000879/* Put an octal string into the specified buffer.
880 * The number is zero and space padded and possibly null padded.
881 * Returns TRUE if successful. */
882static int putOctal (char *cp, int len, long value)
883{
884 int tempLength;
Erik Andersen6acaa402000-03-26 14:03:20 +0000885 char tempBuffer[32];
Erik Andersen5661fe02000-04-05 01:00:52 +0000886 char *tempString = tempBuffer;
Erik Andersen6acaa402000-03-26 14:03:20 +0000887
888 /* Create a string of the specified length with an initial space,
889 * leading zeroes and the octal number, and a trailing null. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000890 sprintf (tempString, "%0*lo", len - 1, value);
Erik Andersen6acaa402000-03-26 14:03:20 +0000891
892 /* If the string is too large, suppress the leading space. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000893 tempLength = strlen (tempString) + 1;
Erik Andersen6acaa402000-03-26 14:03:20 +0000894 if (tempLength > len) {
895 tempLength--;
896 tempString++;
897 }
898
899 /* If the string is still too large, suppress the trailing null. */
900 if (tempLength > len)
901 tempLength--;
902
903 /* If the string is still too large, fail. */
904 if (tempLength > len)
905 return FALSE;
906
907 /* Copy the string to the field. */
908 memcpy (cp, tempString, len);
909
910 return TRUE;
911}
912
Erik Andersen68a9ea42000-04-04 18:39:50 +0000913/* Write out a tar header for the specified file/directory/whatever */
Erik Andersen3364d782000-03-28 00:58:14 +0000914static int
Matt Kraaie80a2632000-12-19 20:45:49 +0000915writeTarHeader(struct TarBallInfo *tbInfo, const char *header_name,
916 const char *real_name, struct stat *statbuf)
Erik Andersen6acaa402000-03-26 14:03:20 +0000917{
Erik Andersen5661fe02000-04-05 01:00:52 +0000918 long chksum=0;
919 struct TarHeader header;
920 const unsigned char *cp = (const unsigned char *) &header;
921 ssize_t size = sizeof(struct TarHeader);
Eric Andersenfdd51032000-08-02 18:48:26 +0000922
Erik Andersen5661fe02000-04-05 01:00:52 +0000923 memset( &header, 0, size);
Erik Andersen3364d782000-03-28 00:58:14 +0000924
Matt Kraaie80a2632000-12-19 20:45:49 +0000925 strncpy(header.name, header_name, sizeof(header.name));
Erik Andersenecd51242000-04-08 03:08:21 +0000926
Erik Andersen5661fe02000-04-05 01:00:52 +0000927 putOctal(header.mode, sizeof(header.mode), statbuf->st_mode);
928 putOctal(header.uid, sizeof(header.uid), statbuf->st_uid);
929 putOctal(header.gid, sizeof(header.gid), statbuf->st_gid);
930 putOctal(header.size, sizeof(header.size), 0); /* Regular file size is handled later */
931 putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime);
932 strncpy(header.magic, TAR_MAGIC TAR_VERSION,
933 TAR_MAGIC_LEN + TAR_VERSION_LEN );
Erik Andersen68a9ea42000-04-04 18:39:50 +0000934
Erik Andersen84e09e42000-04-08 20:58:35 +0000935 /* Enter the user and group names (default to root if it fails) */
Erik Andersen5661fe02000-04-05 01:00:52 +0000936 my_getpwuid(header.uname, statbuf->st_uid);
Erik Andersen5661fe02000-04-05 01:00:52 +0000937 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000938 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +0000939 my_getgrgid(header.gname, statbuf->st_gid);
940 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000941 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +0000942
Eric Andersen3d957c82000-12-07 00:34:58 +0000943 if (tbInfo->hlInfo) {
944 /* This is a hard link */
945 header.typeflag = LNKTYPE;
946 strncpy(header.linkname, tbInfo->hlInfo->name, sizeof(header.linkname));
947 } else if (S_ISLNK(statbuf->st_mode)) {
Eric Andersen3adffb72000-06-26 10:54:06 +0000948 int link_size=0;
Erik Andersen5661fe02000-04-05 01:00:52 +0000949 char buffer[BUFSIZ];
950 header.typeflag = SYMTYPE;
Matt Kraaie80a2632000-12-19 20:45:49 +0000951 link_size = readlink(real_name, buffer, sizeof(buffer) - 1);
Eric Andersen3adffb72000-06-26 10:54:06 +0000952 if ( link_size < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000953 perror_msg("Error reading symlink '%s'", header.name);
Erik Andersen5661fe02000-04-05 01:00:52 +0000954 return ( FALSE);
955 }
Eric Andersen3adffb72000-06-26 10:54:06 +0000956 buffer[link_size] = '\0';
Erik Andersen95c1c1e2000-04-14 21:45:29 +0000957 strncpy(header.linkname, buffer, sizeof(header.linkname));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000958 } else if (S_ISDIR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000959 header.typeflag = DIRTYPE;
960 strncat(header.name, "/", sizeof(header.name));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000961 } else if (S_ISCHR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000962 header.typeflag = CHRTYPE;
963 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
964 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000965 } else if (S_ISBLK(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000966 header.typeflag = BLKTYPE;
967 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
968 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000969 } else if (S_ISFIFO(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000970 header.typeflag = FIFOTYPE;
971 } else if (S_ISREG(statbuf->st_mode)) {
972 header.typeflag = REGTYPE;
973 putOctal(header.size, sizeof(header.size), statbuf->st_size);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000974 } else {
Matt Kraaidd19c692001-01-31 19:00:21 +0000975 error_msg("%s: Unknown file type", real_name);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000976 return ( FALSE);
977 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000978
Erik Andersen5661fe02000-04-05 01:00:52 +0000979 /* Calculate and store the checksum (i.e. the sum of all of the bytes of
980 * the header). The checksum field must be filled with blanks for the
981 * calculation. The checksum field is formatted differently from the
982 * other fields: it has [6] digits, a null, then a space -- rather than
983 * digits, followed by a null like the other fields... */
984 memset(header.chksum, ' ', sizeof(header.chksum));
985 cp = (const unsigned char *) &header;
986 while (size-- > 0)
987 chksum += *cp++;
988 putOctal(header.chksum, 7, chksum);
989
990 /* Now write the header out to disk */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000991 if ((size=full_write(tbInfo->tarFd, (char*)&header, sizeof(struct TarHeader))) < 0) {
Matt Kraaie80a2632000-12-19 20:45:49 +0000992 error_msg(io_error, real_name, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +0000993 return ( FALSE);
994 }
995 /* Pad the header up to the tar block size */
996 for (; size<TAR_BLOCK_SIZE; size++) {
997 write(tbInfo->tarFd, "\0", 1);
998 }
999 /* Now do the verbose thing (or not) */
Eric Andersenfdd51032000-08-02 18:48:26 +00001000 if (tbInfo->verboseFlag==TRUE) {
1001 FILE *vbFd = stdout;
1002 if (tbInfo->tarFd == fileno(stdout)) // If the archive goes to stdout, verbose to stderr
1003 vbFd = stderr;
1004 fprintf(vbFd, "%s\n", header.name);
1005 }
Erik Andersen3364d782000-03-28 00:58:14 +00001006
1007 return ( TRUE);
1008}
1009
1010
Erik Andersen68a9ea42000-04-04 18:39:50 +00001011static int writeFileToTarball(const char *fileName, struct stat *statbuf, void* userData)
Erik Andersen3364d782000-03-28 00:58:14 +00001012{
Erik Andersen68a9ea42000-04-04 18:39:50 +00001013 struct TarBallInfo *tbInfo = (struct TarBallInfo *)userData;
Matt Kraaie80a2632000-12-19 20:45:49 +00001014 const char *header_name;
Erik Andersen68a9ea42000-04-04 18:39:50 +00001015
Eric Andersen3d957c82000-12-07 00:34:58 +00001016 /*
1017 ** Check to see if we are dealing with a hard link.
1018 ** If so -
1019 ** Treat the first occurance of a given dev/inode as a file while
1020 ** treating any additional occurances as hard links. This is done
1021 ** by adding the file information to the HardLinkInfo linked list.
1022 */
1023 tbInfo->hlInfo = NULL;
1024 if (statbuf->st_nlink > 1) {
1025 tbInfo->hlInfo = findHardLinkInfo(tbInfo->hlInfoHead, statbuf->st_dev,
1026 statbuf->st_ino);
1027 if (tbInfo->hlInfo == NULL)
1028 addHardLinkInfo (&tbInfo->hlInfoHead, statbuf->st_dev,
1029 statbuf->st_ino, statbuf->st_nlink, fileName);
1030 }
1031
Erik Andersen68a9ea42000-04-04 18:39:50 +00001032 /* It is against the rules to archive a socket */
1033 if (S_ISSOCK(statbuf->st_mode)) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001034 error_msg("%s: socket ignored", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001035 return( TRUE);
1036 }
1037
1038 /* It is a bad idea to store the archive we are in the process of creating,
1039 * so check the device and inode to be sure that this particular file isn't
1040 * the new tarball */
1041 if (tbInfo->statBuf.st_dev == statbuf->st_dev &&
1042 tbInfo->statBuf.st_ino == statbuf->st_ino) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001043 error_msg("%s: file is the archive; skipping", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001044 return( TRUE);
1045 }
1046
Matt Kraaie80a2632000-12-19 20:45:49 +00001047 header_name = fileName;
1048 while (header_name[0] == '/') {
Matt Kraaia1f97752000-12-19 06:24:08 +00001049 static int alreadyWarned=FALSE;
1050 if (alreadyWarned==FALSE) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001051 error_msg("Removing leading '/' from member names");
Matt Kraaia1f97752000-12-19 06:24:08 +00001052 alreadyWarned=TRUE;
1053 }
Matt Kraaie80a2632000-12-19 20:45:49 +00001054 header_name++;
Matt Kraaia1f97752000-12-19 06:24:08 +00001055 }
1056
Eric Andersen1b1cfde2000-09-24 00:54:37 +00001057 if (strlen(fileName) >= NAME_SIZE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001058 error_msg(name_longer_than_foo, NAME_SIZE);
Eric Andersen1b1cfde2000-09-24 00:54:37 +00001059 return ( TRUE);
1060 }
1061
Matt Kraaie80a2632000-12-19 20:45:49 +00001062 if (header_name[0] == '\0')
Matt Kraaia1f97752000-12-19 06:24:08 +00001063 return TRUE;
1064
1065#if defined BB_FEATURE_TAR_EXCLUDE
Matt Kraaibe7499c2001-01-03 17:22:10 +00001066 if (exclude_file(tbInfo->excludeList, header_name)) {
1067 return SKIP;
Matt Kraaia1f97752000-12-19 06:24:08 +00001068 }
1069#endif
1070
Matt Kraaie80a2632000-12-19 20:45:49 +00001071 if (writeTarHeader(tbInfo, header_name, fileName, statbuf)==FALSE) {
Erik Andersen5661fe02000-04-05 01:00:52 +00001072 return( FALSE);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001073 }
Erik Andersen5661fe02000-04-05 01:00:52 +00001074
1075 /* Now, if the file is a regular file, copy it out to the tarball */
Eric Andersen3d957c82000-12-07 00:34:58 +00001076 if ((tbInfo->hlInfo == NULL)
1077 && (S_ISREG(statbuf->st_mode))) {
Erik Andersen5661fe02000-04-05 01:00:52 +00001078 int inputFileFd;
1079 char buffer[BUFSIZ];
1080 ssize_t size=0, readSize=0;
1081
1082 /* open the file we want to archive, and make sure all is well */
1083 if ((inputFileFd = open(fileName, O_RDONLY)) < 0) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001084 error_msg("%s: Cannot open: %s", fileName, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +00001085 return( FALSE);
1086 }
1087
1088 /* write the file to the archive */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001089 while ( (size = full_read(inputFileFd, buffer, sizeof(buffer))) > 0 ) {
1090 if (full_write(tbInfo->tarFd, buffer, size) != size ) {
Erik Andersen5661fe02000-04-05 01:00:52 +00001091 /* Output file seems to have a problem */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001092 error_msg(io_error, fileName, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +00001093 return( FALSE);
1094 }
1095 readSize+=size;
1096 }
1097 if (size == -1) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001098 error_msg(io_error, fileName, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +00001099 return( FALSE);
1100 }
1101 /* Pad the file up to the tar block size */
1102 for (; (readSize%TAR_BLOCK_SIZE) != 0; readSize++) {
1103 write(tbInfo->tarFd, "\0", 1);
1104 }
1105 close( inputFileFd);
1106 }
Erik Andersen68a9ea42000-04-04 18:39:50 +00001107
1108 return( TRUE);
Erik Andersen6acaa402000-03-26 14:03:20 +00001109}
1110
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001111static int writeTarFile(const char* tarName, int verboseFlag, char **argv,
1112 char** excludeList)
Erik Andersen6acaa402000-03-26 14:03:20 +00001113{
Erik Andersen3364d782000-03-28 00:58:14 +00001114 int tarFd=-1;
Erik Andersen68a9ea42000-04-04 18:39:50 +00001115 int errorFlag=FALSE;
Erik Andersen5661fe02000-04-05 01:00:52 +00001116 ssize_t size;
Erik Andersen68a9ea42000-04-04 18:39:50 +00001117 struct TarBallInfo tbInfo;
1118 tbInfo.verboseFlag = verboseFlag;
Eric Andersen3d957c82000-12-07 00:34:58 +00001119 tbInfo.hlInfoHead = NULL;
Erik Andersen3364d782000-03-28 00:58:14 +00001120
1121 /* Make sure there is at least one file to tar up. */
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001122 if (*argv == NULL)
Matt Kraaidd19c692001-01-31 19:00:21 +00001123 error_msg_and_die("Cowardly refusing to create an empty archive");
Erik Andersen6acaa402000-03-26 14:03:20 +00001124
1125 /* Open the tar file for writing. */
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001126 if (!strcmp(tarName, "-"))
Erik Andersen68a9ea42000-04-04 18:39:50 +00001127 tbInfo.tarFd = fileno(stdout);
Erik Andersen6acaa402000-03-26 14:03:20 +00001128 else
Erik Andersen68a9ea42000-04-04 18:39:50 +00001129 tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1130 if (tbInfo.tarFd < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +00001131 perror_msg( "Error opening '%s'", tarName);
Eric Andersen3d957c82000-12-07 00:34:58 +00001132 freeHardLinkInfo(&tbInfo.hlInfoHead);
Erik Andersen6acaa402000-03-26 14:03:20 +00001133 return ( FALSE);
1134 }
Erik Andersenecd51242000-04-08 03:08:21 +00001135 tbInfo.excludeList=excludeList;
Erik Andersen68a9ea42000-04-04 18:39:50 +00001136 /* Store the stat info for the tarball's file, so
1137 * can avoid including the tarball into itself.... */
1138 if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001139 error_msg_and_die(io_error, tarName, strerror(errno));
Erik Andersen6acaa402000-03-26 14:03:20 +00001140
1141 /* Set the umask for this process so it doesn't
1142 * screw up permission setting for us later. */
1143 umask(0);
1144
1145 /* Read the directory/files and iterate over them one at a time */
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001146 while (*argv != NULL) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001147 if (recursive_action(*argv++, TRUE, FALSE, FALSE,
Erik Andersen68a9ea42000-04-04 18:39:50 +00001148 writeFileToTarball, writeFileToTarball,
1149 (void*) &tbInfo) == FALSE) {
1150 errorFlag = TRUE;
Erik Andersen3364d782000-03-28 00:58:14 +00001151 }
Erik Andersen6acaa402000-03-26 14:03:20 +00001152 }
Erik Andersen5661fe02000-04-05 01:00:52 +00001153 /* Write two empty blocks to the end of the archive */
1154 for (size=0; size<(2*TAR_BLOCK_SIZE); size++) {
1155 write(tbInfo.tarFd, "\0", 1);
1156 }
Erik Andersen0817d132000-04-09 15:17:40 +00001157
1158 /* To be pedantically correct, we would check if the tarball
Eric Andersen3c5ee9a2000-11-14 22:15:48 +00001159 * is smaller than 20 tar blocks, and pad it if it was smaller,
Erik Andersen0817d132000-04-09 15:17:40 +00001160 * but that isn't necessary for GNU tar interoperability, and
1161 * so is considered a waste of space */
1162
Erik Andersen68a9ea42000-04-04 18:39:50 +00001163 /* Hang up the tools, close up shop, head home */
Erik Andersen6acaa402000-03-26 14:03:20 +00001164 close(tarFd);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001165 if (errorFlag == TRUE) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001166 error_msg("Error exit delayed from previous errors");
Eric Andersen3d957c82000-12-07 00:34:58 +00001167 freeHardLinkInfo(&tbInfo.hlInfoHead);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001168 return(FALSE);
1169 }
Eric Andersen3d957c82000-12-07 00:34:58 +00001170 freeHardLinkInfo(&tbInfo.hlInfoHead);
Erik Andersen6acaa402000-03-26 14:03:20 +00001171 return( TRUE);
1172}
1173
1174
1175#endif
1176