blob: 8dec4349d3f4a94892029b033856a6f2f540e03a [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
Eric Andersen77d92682001-05-23 20:32:09 +00007 * 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"
Eric Andersencc8ed391999-10-05 16:24:54 +000054
Erik Andersen298854f2000-03-23 01:09:18 +000055/* Tar file constants */
Erik Andersen1ad302a2000-03-24 00:54:46 +000056#ifndef MAJOR
57#define MAJOR(dev) (((dev)>>8)&0xff)
58#define MINOR(dev) ((dev)&0xff)
59#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000060
Mark Whitley59ab0252001-01-23 22:30:04 +000061enum { NAME_SIZE = 100 }; /* because gcc won't let me use 'static const int' */
Eric Andersencc8ed391999-10-05 16:24:54 +000062
Erik Andersen298854f2000-03-23 01:09:18 +000063/* POSIX tar Header Block, from POSIX 1003.1-1990 */
64struct TarHeader
65{
66 /* byte offset */
Eric Andersen1b1cfde2000-09-24 00:54:37 +000067 char name[NAME_SIZE]; /* 0-99 */
Erik Andersen0817d132000-04-09 15:17:40 +000068 char mode[8]; /* 100-107 */
69 char uid[8]; /* 108-115 */
70 char gid[8]; /* 116-123 */
71 char size[12]; /* 124-135 */
72 char mtime[12]; /* 136-147 */
73 char chksum[8]; /* 148-155 */
74 char typeflag; /* 156-156 */
Eric Andersen1b1cfde2000-09-24 00:54:37 +000075 char linkname[NAME_SIZE]; /* 157-256 */
Erik Andersen0817d132000-04-09 15:17:40 +000076 char magic[6]; /* 257-262 */
77 char version[2]; /* 263-264 */
78 char uname[32]; /* 265-296 */
79 char gname[32]; /* 297-328 */
80 char devmajor[8]; /* 329-336 */
81 char devminor[8]; /* 337-344 */
82 char prefix[155]; /* 345-499 */
83 char padding[12]; /* 500-512 (pad to exactly the TAR_BLOCK_SIZE) */
Erik Andersen298854f2000-03-23 01:09:18 +000084};
85typedef struct TarHeader TarHeader;
Eric Andersencc8ed391999-10-05 16:24:54 +000086
Eric Andersencc8ed391999-10-05 16:24:54 +000087
Erik Andersen298854f2000-03-23 01:09:18 +000088/* A few useful constants */
89#define TAR_MAGIC "ustar" /* ustar and a null */
Erik Andersen0817d132000-04-09 15:17:40 +000090#define TAR_VERSION " " /* Be compatable with GNU tar format */
Mark Whitley59ab0252001-01-23 22:30:04 +000091static const int TAR_MAGIC_LEN = 6;
92static const int TAR_VERSION_LEN = 2;
93static const int TAR_BLOCK_SIZE = 512;
Erik Andersen298854f2000-03-23 01:09:18 +000094
95/* A nice enum with all the possible tar file content types */
96enum TarFileType
97{
98 REGTYPE = '0', /* regular file */
99 REGTYPE0 = '\0', /* regular file (ancient bug compat)*/
100 LNKTYPE = '1', /* hard link */
101 SYMTYPE = '2', /* symbolic link */
102 CHRTYPE = '3', /* character special */
103 BLKTYPE = '4', /* block special */
104 DIRTYPE = '5', /* directory */
105 FIFOTYPE = '6', /* FIFO special */
106 CONTTYPE = '7', /* reserved */
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000107 GNULONGLINK = 'K', /* GNU long (>100 chars) link name */
108 GNULONGNAME = 'L', /* GNU long (>100 chars) file name */
Erik Andersen298854f2000-03-23 01:09:18 +0000109};
110typedef enum TarFileType TarFileType;
111
112/* This struct ignores magic, non-numeric user name,
113 * non-numeric group name, and the checksum, since
114 * these are all ignored by BusyBox tar. */
115struct TarInfo
116{
117 int tarFd; /* An open file descriptor for reading from the tarball */
118 char * name; /* File name */
119 mode_t mode; /* Unix mode, including device bits. */
120 uid_t uid; /* Numeric UID */
121 gid_t gid; /* Numeric GID */
122 size_t size; /* Size of file */
123 time_t mtime; /* Last-modified time */
Eric Andersen77d92682001-05-23 20:32:09 +0000124 enum TarFileType type; /* Regular, directory, link, etc. */
Erik Andersen298854f2000-03-23 01:09:18 +0000125 char * linkname; /* Name for symbolic and hard links */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000126 long devmajor; /* Major number for special device */
127 long devminor; /* Minor number for special device */
Erik Andersen298854f2000-03-23 01:09:18 +0000128};
129typedef struct TarInfo TarInfo;
130
Erik Andersene454fb62000-03-23 04:27:58 +0000131/* Local procedures to restore files from a tar file. */
Glenn L McGrath2975a342001-04-11 16:49:07 +0000132static int readTarFile(int tarFd, int extractFlag, int listFlag,
Matt Kraaib92223b2000-09-04 08:25:42 +0000133 int tostdoutFlag, int verboseFlag, char** extractList,
134 char** excludeList);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000135
Erik Andersen05100cd2000-01-16 01:30:52 +0000136#ifdef BB_FEATURE_TAR_CREATE
Erik Andersen6acaa402000-03-26 14:03:20 +0000137/* Local procedures to save files into a tar file. */
Matt Kraaid8ad76c2000-11-08 02:35:47 +0000138static int writeTarFile(const char* tarName, int verboseFlag, char **argv,
139 char** excludeList);
Erik Andersen05100cd2000-01-16 01:30:52 +0000140#endif
141
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000142#if defined BB_FEATURE_TAR_EXCLUDE
Eric Andersen3e6ff902001-03-09 21:24:12 +0000143static struct option longopts[] = {
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000144 { "exclude", 1, NULL, 'e' },
145 { NULL, 0, NULL, 0 }
146};
147#endif
148
Erik Andersene49d5ec2000-02-08 19:58:47 +0000149extern int tar_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000150{
Erik Andersenecd51242000-04-08 03:08:21 +0000151 char** excludeList=NULL;
Matt Kraaic119ab92000-11-30 04:44:54 +0000152 char** extractList=NULL;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000153 const char *tarName="-";
Erik Andersen0817d132000-04-09 15:17:40 +0000154#if defined BB_FEATURE_TAR_EXCLUDE
Erik Andersenecd51242000-04-08 03:08:21 +0000155 int excludeListSize=0;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000156 FILE *fileList;
157 char file[256];
Erik Andersen0817d132000-04-09 15:17:40 +0000158#endif
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000159#if defined BB_FEATURE_TAR_GZIP
Glenn L McGrath018e9e62001-03-28 07:27:26 +0000160 FILE *comp_file = NULL;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000161 int unzipFlag = FALSE;
162#endif
Erik Andersen298854f2000-03-23 01:09:18 +0000163 int listFlag = FALSE;
164 int extractFlag = FALSE;
165 int createFlag = FALSE;
166 int verboseFlag = FALSE;
167 int tostdoutFlag = FALSE;
Eric Andersen02f3b2e2000-12-01 19:04:52 +0000168 int status = FALSE;
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000169 int opt;
Glenn L McGrath018e9e62001-03-28 07:27:26 +0000170 pid_t pid;
Eric Andersencc8ed391999-10-05 16:24:54 +0000171
Erik Andersenecd51242000-04-08 03:08:21 +0000172 if (argc <= 1)
Eric Andersen67991cf2001-02-14 21:23:06 +0000173 show_usage();
Eric Andersencc8ed391999-10-05 16:24:54 +0000174
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000175 if (argv[1][0] != '-') {
176 char *tmp = xmalloc(strlen(argv[1]) + 2);
177 tmp[0] = '-';
178 strcpy(tmp + 1, argv[1]);
179 argv[1] = tmp;
180 }
181
182 while (
183#ifndef BB_FEATURE_TAR_EXCLUDE
Eric Andersenea4abff2001-06-21 15:17:59 +0000184 (opt = getopt(argc, argv, "cxtzvOf:p"))
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000185#else
Eric Andersenea4abff2001-06-21 15:17:59 +0000186 (opt = getopt_long(argc, argv, "cxtzvOf:X:p", longopts, NULL))
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000187#endif
188 > 0) {
189 switch (opt) {
190 case 'c':
191 if (extractFlag == TRUE || listFlag == TRUE)
192 goto flagError;
193 createFlag = TRUE;
194 break;
195 case 'x':
196 if (listFlag == TRUE || createFlag == TRUE)
197 goto flagError;
198 extractFlag = TRUE;
199 break;
200 case 't':
201 if (extractFlag == TRUE || createFlag == TRUE)
202 goto flagError;
203 listFlag = TRUE;
204 break;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000205#ifdef BB_FEATURE_TAR_GZIP
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000206 case 'z':
207 unzipFlag = TRUE;
208 break;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000209#endif
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000210 case 'v':
211 verboseFlag = TRUE;
212 break;
213 case 'O':
214 tostdoutFlag = TRUE;
215 break;
216 case 'f':
217 if (*tarName != '-')
Matt Kraaidd19c692001-01-31 19:00:21 +0000218 error_msg_and_die( "Only one 'f' option allowed");
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000219 tarName = optarg;
220 break;
Erik Andersen0817d132000-04-09 15:17:40 +0000221#if defined BB_FEATURE_TAR_EXCLUDE
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000222 case 'e':
223 excludeList=xrealloc( excludeList,
224 sizeof(char *) * (excludeListSize+2));
225 excludeList[excludeListSize] = optarg;
226 /* Tack a NULL onto the end of the list */
227 excludeList[++excludeListSize] = NULL;
228 case 'X':
229 fileList = xfopen(optarg, "r");
230 while (fgets(file, sizeof(file), fileList) != NULL) {
231 excludeList = xrealloc(excludeList,
232 sizeof(char *) * (excludeListSize+2));
Matt Kraai05e782d2001-02-01 16:49:30 +0000233 chomp(file);
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000234 excludeList[excludeListSize] = xstrdup(file);
235 /* Tack a NULL onto the end of the list */
236 excludeList[++excludeListSize] = NULL;
237 }
238 fclose(fileList);
239 break;
Erik Andersen0817d132000-04-09 15:17:40 +0000240#endif
Eric Andersenea4abff2001-06-21 15:17:59 +0000241 case 'p':
242 break;
243 default:
Eric Andersen67991cf2001-02-14 21:23:06 +0000244 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000245 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000246 }
247
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000248 /*
Erik Andersene49d5ec2000-02-08 19:58:47 +0000249 * Do the correct type of action supplying the rest of the
250 * command line arguments as the list of files to process.
251 */
252 if (createFlag == TRUE) {
Erik Andersende552872000-01-23 01:34:05 +0000253#ifndef BB_FEATURE_TAR_CREATE
Matt Kraaidd19c692001-01-31 19:00:21 +0000254 error_msg_and_die( "This version of tar was not compiled with tar creation support.");
Erik Andersende552872000-01-23 01:34:05 +0000255#else
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000256#ifdef BB_FEATURE_TAR_GZIP
257 if (unzipFlag==TRUE)
Matt Kraaidd19c692001-01-31 19:00:21 +0000258 error_msg_and_die("Creation of compressed not internally support by tar, pipe to busybox gunzip");
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000259#endif
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000260 status = writeTarFile(tarName, verboseFlag, argv + optind, excludeList);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000261#endif
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000262 }
263 if (listFlag == TRUE || extractFlag == TRUE) {
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000264 int tarFd;
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000265 if (argv[optind])
266 extractList = argv + optind;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000267 /* Open the tar file for reading. */
268 if (!strcmp(tarName, "-"))
269 tarFd = fileno(stdin);
270 else
271 tarFd = open(tarName, O_RDONLY);
272 if (tarFd < 0)
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000273 perror_msg_and_die("Error opening '%s'", tarName);
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000274
275#ifdef BB_FEATURE_TAR_GZIP
276 /* unzip tarFd in a seperate process */
Glenn L McGrath018e9e62001-03-28 07:27:26 +0000277 if (unzipFlag == TRUE) {
278 comp_file = fdopen(tarFd, "r");
Glenn L McGrath8e74bf92001-06-20 07:54:15 +0000279 if ((tarFd = fileno(gz_open(comp_file, &pid))) == EXIT_FAILURE) {
Glenn L McGrath018e9e62001-03-28 07:27:26 +0000280 error_msg_and_die("Couldnt unzip file");
281 }
Glenn L McGrath018e9e62001-03-28 07:27:26 +0000282 }
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000283#endif
284 status = readTarFile(tarFd, extractFlag, listFlag, tostdoutFlag,
Matt Kraai3e856ce2000-12-01 02:55:13 +0000285 verboseFlag, extractList, excludeList);
Glenn L McGrath018e9e62001-03-28 07:27:26 +0000286 close(tarFd);
Glenn L McGrath018e9e62001-03-28 07:27:26 +0000287#ifdef BB_FEATURE_TAR_GZIP
Glenn L McGrathae8ad352001-03-28 23:57:51 +0000288 if (unzipFlag == TRUE) {
289 gz_close(pid);
290 fclose(comp_file);
291 }
Glenn L McGrath018e9e62001-03-28 07:27:26 +0000292#endif
Glenn L McGrathae8ad352001-03-28 23:57:51 +0000293 }
Glenn L McGrath018e9e62001-03-28 07:27:26 +0000294
Matt Kraai3e856ce2000-12-01 02:55:13 +0000295 if (status == TRUE)
296 return EXIT_SUCCESS;
297 else
298 return EXIT_FAILURE;
299
Erik Andersene49d5ec2000-02-08 19:58:47 +0000300 flagError:
Matt Kraaidd19c692001-01-31 19:00:21 +0000301 error_msg_and_die( "Exactly one of 'c', 'x' or 't' must be specified");
Erik Andersen298854f2000-03-23 01:09:18 +0000302}
303
304static void
Erik Andersen6a34b532000-04-07 06:55:38 +0000305fixUpPermissions(TarInfo *header)
306{
307 struct utimbuf t;
Eric Andersen77d92682001-05-23 20:32:09 +0000308 /* Now set permissions etc. for the new file */
Erik Andersen6a34b532000-04-07 06:55:38 +0000309 chown(header->name, header->uid, header->gid);
310 chmod(header->name, header->mode);
311 /* Reset the time */
312 t.actime = time(0);
313 t.modtime = header->mtime;
314 utime(header->name, &t);
315}
316
317static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000318tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersen298854f2000-03-23 01:09:18 +0000319{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000320 size_t writeSize;
321 size_t readSize;
322 size_t actualWriteSz;
323 char buffer[BUFSIZ];
324 size_t size = header->size;
325 int outFd=fileno(stdout);
326
327 /* Open the file to be written, if a file is supposed to be written */
328 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000329 /* Create the path to the file, just in case it isn't there...
330 * This should not screw up path permissions or anything. */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000331 create_path(header->name, 0777);
Eric Andersen0c6a9702000-06-09 20:51:50 +0000332 if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY,
333 header->mode & ~S_IFMT)) < 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000334 error_msg(io_error, header->name, strerror(errno));
Eric Andersen0c6a9702000-06-09 20:51:50 +0000335 return( FALSE);
336 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000337 }
338
339 /* Write out the file, if we are supposed to be doing that */
340 while ( size > 0 ) {
341 actualWriteSz=0;
342 if ( size > sizeof(buffer) )
343 writeSize = readSize = sizeof(buffer);
344 else {
345 int mod = size % 512;
346 if ( mod != 0 )
347 readSize = size + (512 - mod);
348 else
349 readSize = size;
350 writeSize = size;
351 }
Mark Whitleyf57c9442000-12-07 19:56:48 +0000352 if ( (readSize = full_read(header->tarFd, buffer, readSize)) <= 0 ) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000353 /* Tarball seems to have a problem */
Matt Kraaidd19c692001-01-31 19:00:21 +0000354 error_msg("Unexpected EOF in archive");
Erik Andersen6a34b532000-04-07 06:55:38 +0000355 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000356 }
357 if ( readSize < writeSize )
358 writeSize = readSize;
359
360 /* Write out the file, if we are supposed to be doing that */
361 if (extractFlag==TRUE) {
362
Mark Whitleyf57c9442000-12-07 19:56:48 +0000363 if ((actualWriteSz=full_write(outFd, buffer, writeSize)) != writeSize ) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000364 /* Output file seems to have a problem */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000365 error_msg(io_error, header->name, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000366 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000367 }
Erik Andersen0817d132000-04-09 15:17:40 +0000368 } else {
369 actualWriteSz=writeSize;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000370 }
371
372 size -= actualWriteSz;
373 }
374
375 /* Now we are done writing the file out, so try
376 * and fix up the permissions and whatnot */
377 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000378 close(outFd);
Erik Andersen6a34b532000-04-07 06:55:38 +0000379 fixUpPermissions(header);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000380 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000381 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000382}
383
Erik Andersen6a34b532000-04-07 06:55:38 +0000384static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000385tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000386{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000387 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000388 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000389
Mark Whitleyf57c9442000-12-07 19:56:48 +0000390 if (create_path(header->name, header->mode) != TRUE) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000391 perror_msg("%s: Cannot mkdir", header->name);
Erik Andersen6a34b532000-04-07 06:55:38 +0000392 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000393 }
394 /* make the final component, just in case it was
Mark Whitleyf57c9442000-12-07 19:56:48 +0000395 * omitted by create_path() (which will skip the
Erik Andersen1ad302a2000-03-24 00:54:46 +0000396 * directory if it doesn't have a terminating '/') */
Eric Andersencb2a3722001-06-04 16:54:39 +0000397 if (mkdir(header->name, header->mode) < 0 && errno != EEXIST) {
Matt Kraai541ffe32001-01-13 21:46:25 +0000398 perror_msg("%s", header->name);
399 return FALSE;
Erik Andersen6acaa402000-03-26 14:03:20 +0000400 }
Matt Kraai541ffe32001-01-13 21:46:25 +0000401
Eric Andersencb2a3722001-06-04 16:54:39 +0000402 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000403 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000404}
405
Erik Andersen6a34b532000-04-07 06:55:38 +0000406static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000407tarExtractHardLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000408{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000409 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000410 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000411
412 if (link(header->linkname, header->name) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000413 perror_msg("%s: Cannot create hard link to '%s'", header->name,
414 header->linkname);
Erik Andersen6a34b532000-04-07 06:55:38 +0000415 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000416 }
417
Eric Andersen77d92682001-05-23 20:32:09 +0000418 /* Now set permissions etc. for the new directory */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000419 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000420 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000421}
422
Erik Andersen6a34b532000-04-07 06:55:38 +0000423static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000424tarExtractSymLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000425{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000426 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000427 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000428
429#ifdef S_ISLNK
430 if (symlink(header->linkname, header->name) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000431 perror_msg("%s: Cannot create symlink to '%s'", header->name,
432 header->linkname);
Erik Andersen6a34b532000-04-07 06:55:38 +0000433 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000434 }
435 /* Try to change ownership of the symlink.
436 * If libs doesn't support that, don't bother.
437 * Changing the pointed-to-file is the Wrong Thing(tm).
438 */
439#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
440 lchown(header->name, header->uid, header->gid);
441#endif
442
443 /* Do not change permissions or date on symlink,
444 * since it changes the pointed to file instead. duh. */
445#else
Matt Kraaidd19c692001-01-31 19:00:21 +0000446 error_msg("%s: Cannot create symlink to '%s': %s",
Erik Andersen6a34b532000-04-07 06:55:38 +0000447 header->name, header->linkname,
448 "symlinks not supported");
Erik Andersen1ad302a2000-03-24 00:54:46 +0000449#endif
Erik Andersen6a34b532000-04-07 06:55:38 +0000450 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000451}
452
Erik Andersen6a34b532000-04-07 06:55:38 +0000453static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000454tarExtractSpecial(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000455{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000456 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000457 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000458
459 if (S_ISCHR(header->mode) || S_ISBLK(header->mode) || S_ISSOCK(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000460 if (mknod(header->name, header->mode, makedev(header->devmajor, header->devminor)) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000461 perror_msg("%s: Cannot mknod", header->name);
Erik Andersen6a34b532000-04-07 06:55:38 +0000462 return( FALSE);
463 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000464 } else if (S_ISFIFO(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000465 if (mkfifo(header->name, header->mode) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000466 perror_msg("%s: Cannot mkfifo", header->name);
Erik Andersen6a34b532000-04-07 06:55:38 +0000467 return( FALSE);
468 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000469 }
470
Eric Andersen77d92682001-05-23 20:32:09 +0000471 /* Now set permissions etc. for the new directory */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000472 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000473 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000474}
475
Erik Andersen298854f2000-03-23 01:09:18 +0000476/* Parse the tar header and fill in the nice struct with the details */
477static int
Erik Andersen3364d782000-03-28 00:58:14 +0000478readTarHeader(struct TarHeader *rawHeader, struct TarInfo *header)
Erik Andersen298854f2000-03-23 01:09:18 +0000479{
Erik Andersene454fb62000-03-23 04:27:58 +0000480 int i;
Erik Andersen84e09e42000-04-08 20:58:35 +0000481 long chksum, sum=0;
Erik Andersene454fb62000-03-23 04:27:58 +0000482 unsigned char *s = (unsigned char *)rawHeader;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000483
Erik Andersen298854f2000-03-23 01:09:18 +0000484 header->name = rawHeader->name;
Erik Andersen84e09e42000-04-08 20:58:35 +0000485 /* Check for and relativify any absolute paths */
486 if ( *(header->name) == '/' ) {
487 static int alreadyWarned=FALSE;
488
489 while (*(header->name) == '/')
Matt Kraai7f7348b2001-05-22 14:18:03 +0000490 header->name++;
Erik Andersen84e09e42000-04-08 20:58:35 +0000491
492 if (alreadyWarned == FALSE) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000493 error_msg("Removing leading '/' from member names");
Erik Andersen84e09e42000-04-08 20:58:35 +0000494 alreadyWarned = TRUE;
495 }
496 }
497
Glenn L McGrath48081f82001-04-11 05:01:09 +0000498 header->mode = strtol(rawHeader->mode, NULL, 8);
499 header->uid = strtol(rawHeader->uid, NULL, 8);
500 header->gid = strtol(rawHeader->gid, NULL, 8);
501 header->size = strtol(rawHeader->size, NULL, 8);
502 header->mtime = strtol(rawHeader->mtime, NULL, 8);
503 chksum = strtol(rawHeader->chksum, NULL, 8);
Erik Andersen298854f2000-03-23 01:09:18 +0000504 header->type = rawHeader->typeflag;
505 header->linkname = rawHeader->linkname;
Glenn L McGrath48081f82001-04-11 05:01:09 +0000506 header->devmajor = strtol(rawHeader->devmajor, NULL, 8);
507 header->devminor = strtol(rawHeader->devminor, NULL, 8);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000508
Erik Andersen298854f2000-03-23 01:09:18 +0000509 /* Check the checksum */
Erik Andersen84e09e42000-04-08 20:58:35 +0000510 for (i = sizeof(*rawHeader); i-- != 0;) {
Erik Andersen298854f2000-03-23 01:09:18 +0000511 sum += *s++;
Erik Andersen84e09e42000-04-08 20:58:35 +0000512 }
513 /* Remove the effects of the checksum field (replace
514 * with blanks for the purposes of the checksum) */
515 s = rawHeader->chksum;
516 for (i = sizeof(rawHeader->chksum) ; i-- != 0;) {
517 sum -= *s++;
518 }
519 sum += ' ' * sizeof(rawHeader->chksum);
Erik Andersene454fb62000-03-23 04:27:58 +0000520 if (sum == chksum )
Erik Andersen298854f2000-03-23 01:09:18 +0000521 return ( TRUE);
522 return( FALSE);
523}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000524
Eric Andersen3e6ff902001-03-09 21:24:12 +0000525static int exclude_file(char **excluded_files, const char *file)
Matt Kraaibe7499c2001-01-03 17:22:10 +0000526{
527 int i;
528
529 if (excluded_files == NULL)
530 return 0;
531
532 for (i = 0; excluded_files[i] != NULL; i++) {
533 if (excluded_files[i][0] == '/') {
534 if (fnmatch(excluded_files[i], file,
535 FNM_PATHNAME | FNM_LEADING_DIR) == 0)
536 return 1;
537 } else {
538 const char *p;
539
540 for (p = file; p[0] != '\0'; p++) {
541 if ((p == file || p[-1] == '/') && p[0] != '/' &&
542 fnmatch(excluded_files[i], p,
543 FNM_PATHNAME | FNM_LEADING_DIR) == 0)
544 return 1;
545 }
546 }
547 }
548
549 return 0;
550}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000551
Eric Andersen3e6ff902001-03-09 21:24:12 +0000552static int extract_file(char **extract_files, const char *file)
Matt Kraai8f8dab92001-01-22 05:25:19 +0000553{
554 int i;
555
556 if (extract_files == NULL)
557 return 1;
558
559 for (i = 0; extract_files[i] != NULL; i++) {
560 if (fnmatch(extract_files[i], file, FNM_LEADING_DIR) == 0)
561 return 1;
562 }
563
564 return 0;
565}
566
Erik Andersen1ad302a2000-03-24 00:54:46 +0000567/*
568 * Read a tar file and extract or list the specified files within it.
569 * If the list is empty than all files are extracted or listed.
570 */
Glenn L McGrath2975a342001-04-11 16:49:07 +0000571static int readTarFile(int tarFd, int extractFlag, int listFlag,
Matt Kraaib92223b2000-09-04 08:25:42 +0000572 int tostdoutFlag, int verboseFlag, char** extractList,
573 char** excludeList)
Erik Andersen1ad302a2000-03-24 00:54:46 +0000574{
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000575 int status;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000576 int errorFlag=FALSE;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000577 int skipNextHeaderFlag=FALSE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000578 TarHeader rawHeader;
579 TarInfo header;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000580
Erik Andersen1ad302a2000-03-24 00:54:46 +0000581 /* Read the tar file, and iterate over it one file at a time */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000582 while ( (status = full_read(tarFd, (char*)&rawHeader, TAR_BLOCK_SIZE)) == TAR_BLOCK_SIZE ) {
Erik Andersene2729152000-02-18 21:34:17 +0000583
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000584 /* Try to read the header */
Erik Andersen3364d782000-03-28 00:58:14 +0000585 if ( readTarHeader(&rawHeader, &header) == FALSE ) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000586 if ( *(header.name) == '\0' ) {
587 goto endgame;
588 } else {
589 errorFlag=TRUE;
Matt Kraaidd19c692001-01-31 19:00:21 +0000590 error_msg("Bad tar header, skipping");
Erik Andersen1ad302a2000-03-24 00:54:46 +0000591 continue;
592 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000593 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000594 if ( *(header.name) == '\0' )
Matt Kraaie0244b02001-05-01 21:12:31 +0000595 continue;
Erik Andersen0817d132000-04-09 15:17:40 +0000596 header.tarFd = tarFd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000597
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000598 /* Skip funky extra GNU headers that precede long files */
599 if ( (header.type == GNULONGNAME) || (header.type == GNULONGLINK) ) {
600 skipNextHeaderFlag=TRUE;
Matt Kraaiab8f9e22000-11-18 01:28:57 +0000601 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
602 errorFlag = TRUE;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000603 continue;
604 }
605 if ( skipNextHeaderFlag == TRUE ) {
606 skipNextHeaderFlag=FALSE;
Mark Whitleyf57c9442000-12-07 19:56:48 +0000607 error_msg(name_longer_than_foo, NAME_SIZE);
Matt Kraaiab8f9e22000-11-18 01:28:57 +0000608 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
609 errorFlag = TRUE;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000610 continue;
611 }
612
Erik Andersen0817d132000-04-09 15:17:40 +0000613#if defined BB_FEATURE_TAR_EXCLUDE
Matt Kraaibe7499c2001-01-03 17:22:10 +0000614 if (exclude_file(excludeList, header.name)) {
Erik Andersen0817d132000-04-09 15:17:40 +0000615 /* There are not the droids you're looking for, move along */
Matt Kraaibe7499c2001-01-03 17:22:10 +0000616 /* If it is a regular file, pretend to extract it with
617 * the extractFlag set to FALSE, so the junk in the tarball
618 * is properly skipped over */
619 if ( header.type==REGTYPE || header.type==REGTYPE0 ) {
620 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
621 errorFlag = TRUE;
622 }
623 continue;
Erik Andersen0817d132000-04-09 15:17:40 +0000624 }
625#endif
Matt Kraaibe7499c2001-01-03 17:22:10 +0000626
Matt Kraai8f8dab92001-01-22 05:25:19 +0000627 if (!extract_file(extractList, header.name)) {
Matt Kraaib92223b2000-09-04 08:25:42 +0000628 /* There are not the droids you're looking for, move along */
Matt Kraai8f8dab92001-01-22 05:25:19 +0000629 /* If it is a regular file, pretend to extract it with
630 * the extractFlag set to FALSE, so the junk in the tarball
631 * is properly skipped over */
632 if ( header.type==REGTYPE || header.type==REGTYPE0 ) {
633 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
634 errorFlag = TRUE;
Matt Kraaib92223b2000-09-04 08:25:42 +0000635 }
Matt Kraai8f8dab92001-01-22 05:25:19 +0000636 continue;
Matt Kraaib92223b2000-09-04 08:25:42 +0000637 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000638
Matt Kraai82cfbad2000-09-15 21:18:43 +0000639 if (listFlag == TRUE) {
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000640 /* Special treatment if the list (-t) flag is on */
641 if (verboseFlag == TRUE) {
642 int len, len1;
643 char buf[35];
644 struct tm *tm = localtime (&(header.mtime));
645
Mark Whitleyf57c9442000-12-07 19:56:48 +0000646 len=printf("%s ", mode_string(header.mode));
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000647 my_getpwuid(buf, header.uid);
648 if (! *buf)
649 len+=printf("%d", header.uid);
650 else
651 len+=printf("%s", buf);
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000652 my_getgrgid(buf, header.gid);
653 if (! *buf)
654 len+=printf("/%-d ", header.gid);
655 else
656 len+=printf("/%-s ", buf);
657
658 if (header.type==CHRTYPE || header.type==BLKTYPE) {
659 len1=snprintf(buf, sizeof(buf), "%ld,%-ld ",
660 header.devmajor, header.devminor);
661 } else {
662 len1=snprintf(buf, sizeof(buf), "%lu ", (long)header.size);
663 }
664 /* Jump through some hoops to make the columns match up */
665 for(;(len+len1)<31;len++)
666 printf(" ");
667 printf(buf);
668
669 /* Use ISO 8610 time format */
670 if (tm) {
671 printf ("%04d-%02d-%02d %02d:%02d:%02d ",
672 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
673 tm->tm_hour, tm->tm_min, tm->tm_sec);
674 }
675 }
Eric Andersenfdd51032000-08-02 18:48:26 +0000676 printf("%s", header.name);
Matt Kraai82cfbad2000-09-15 21:18:43 +0000677 if (verboseFlag == TRUE) {
678 if (header.type==LNKTYPE) /* If this is a link, say so */
679 printf(" link to %s", header.linkname);
680 else if (header.type==SYMTYPE)
681 printf(" -> %s", header.linkname);
682 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000683 printf("\n");
684 }
685
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000686 /* List contents if we are supposed to do that */
687 if (verboseFlag == TRUE && extractFlag == TRUE) {
688 /* Now the normal listing */
689 FILE *vbFd = stdout;
690 if (tostdoutFlag == TRUE) // If the archive goes to stdout, verbose to stderr
691 vbFd = stderr;
692 fprintf(vbFd, "%s\n", header.name);
693 }
694
Matt Kraaif4462972000-09-01 02:50:48 +0000695 /* Remove files if we would overwrite them */
Matt Kraaida542f32000-09-01 02:53:01 +0000696 if (extractFlag == TRUE && tostdoutFlag == FALSE)
Matt Kraaif4462972000-09-01 02:50:48 +0000697 unlink(header.name);
Eric Andersencc8ed391999-10-05 16:24:54 +0000698
Erik Andersen1ad302a2000-03-24 00:54:46 +0000699 /* If we got here, we can be certain we have a legitimate
700 * header to work with. So work with it. */
701 switch ( header.type ) {
702 case REGTYPE:
703 case REGTYPE0:
704 /* If the name ends in a '/' then assume it is
705 * supposed to be a directory, and fall through */
Glenn L McGrathaf166e72001-04-29 00:50:33 +0000706 if (!last_char_is(header.name,'/')) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000707 if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE)
708 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000709 break;
710 }
711 case DIRTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000712 if (tarExtractDirectory( &header, extractFlag, tostdoutFlag)==FALSE)
713 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000714 break;
715 case LNKTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000716 if (tarExtractHardLink( &header, extractFlag, tostdoutFlag)==FALSE)
717 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000718 break;
719 case SYMTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000720 if (tarExtractSymLink( &header, extractFlag, tostdoutFlag)==FALSE)
721 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000722 break;
723 case CHRTYPE:
724 case BLKTYPE:
725 case FIFOTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000726 if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE)
727 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000728 break;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000729#if 0
730 /* Handled earlier */
731 case GNULONGNAME:
732 case GNULONGLINK:
733 skipNextHeaderFlag=TRUE;
734 break;
735#endif
Erik Andersen1ad302a2000-03-24 00:54:46 +0000736 default:
Matt Kraaidd19c692001-01-31 19:00:21 +0000737 error_msg("Unknown file type '%c' in tar file", header.type);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000738 close( tarFd);
739 return( FALSE);
740 }
741 }
742 close(tarFd);
743 if (status > 0) {
744 /* Bummer - we read a partial header */
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000745 perror_msg("Error reading tar file");
Erik Andersen1ad302a2000-03-24 00:54:46 +0000746 return ( FALSE);
747 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000748 else if (errorFlag==TRUE) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000749 error_msg( "Error exit delayed from previous errors");
Erik Andersen6a34b532000-04-07 06:55:38 +0000750 return( FALSE);
751 } else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000752 return( status);
753
754 /* Stuff to do when we are done */
755endgame:
756 close( tarFd);
757 if ( *(header.name) == '\0' ) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000758 if (errorFlag==TRUE)
Matt Kraaidd19c692001-01-31 19:00:21 +0000759 error_msg( "Error exit delayed from previous errors");
Erik Andersen6a34b532000-04-07 06:55:38 +0000760 else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000761 return( TRUE);
762 }
763 return( FALSE);
764}
765
Erik Andersen6acaa402000-03-26 14:03:20 +0000766
767#ifdef BB_FEATURE_TAR_CREATE
768
Eric Andersen3d957c82000-12-07 00:34:58 +0000769/*
770** writeTarFile(), writeFileToTarball(), and writeTarHeader() are
771** the only functions that deal with the HardLinkInfo structure.
772** Even these functions use the xxxHardLinkInfo() functions.
773*/
774typedef struct HardLinkInfo HardLinkInfo;
775struct HardLinkInfo
776{
777 HardLinkInfo *next; /* Next entry in list */
778 dev_t dev; /* Device number */
779 ino_t ino; /* Inode number */
780 short linkCount; /* (Hard) Link Count */
781 char name[1]; /* Start of filename (must be last) */
782};
783
Erik Andersen68a9ea42000-04-04 18:39:50 +0000784/* Some info to be carried along when creating a new tarball */
785struct TarBallInfo
786{
787 char* fileName; /* File name of the tarball */
788 int tarFd; /* Open-for-write file descriptor
789 for the tarball */
790 struct stat statBuf; /* Stat info for the tarball, letting
791 us know the inode and device that the
792 tarball lives, so we can avoid trying
793 to include the tarball into itself */
794 int verboseFlag; /* Whether to print extra stuff or not */
Erik Andersenecd51242000-04-08 03:08:21 +0000795 char** excludeList; /* List of files to not include */
Eric Andersen3d957c82000-12-07 00:34:58 +0000796 HardLinkInfo *hlInfoHead; /* Hard Link Tracking Information */
797 HardLinkInfo *hlInfo; /* Hard Link Info for the current file */
Erik Andersen68a9ea42000-04-04 18:39:50 +0000798};
799typedef struct TarBallInfo TarBallInfo;
800
801
Eric Andersen3d957c82000-12-07 00:34:58 +0000802/* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */
803static void
804addHardLinkInfo (HardLinkInfo **hlInfoHeadPtr, dev_t dev, ino_t ino,
805 short linkCount, const char *name)
806{
807 /* Note: hlInfoHeadPtr can never be NULL! */
808 HardLinkInfo *hlInfo;
809
810 hlInfo = (HardLinkInfo *)xmalloc(sizeof(HardLinkInfo)+strlen(name)+1);
811 if (hlInfo) {
812 hlInfo->next = *hlInfoHeadPtr;
813 *hlInfoHeadPtr = hlInfo;
814 hlInfo->dev = dev;
815 hlInfo->ino = ino;
816 hlInfo->linkCount = linkCount;
817 strcpy(hlInfo->name, name);
818 }
819 return;
820}
821
822static void
823freeHardLinkInfo (HardLinkInfo **hlInfoHeadPtr)
824{
825 HardLinkInfo *hlInfo = NULL;
826 HardLinkInfo *hlInfoNext = NULL;
827
828 if (hlInfoHeadPtr) {
829 hlInfo = *hlInfoHeadPtr;
830 while (hlInfo) {
831 hlInfoNext = hlInfo->next;
832 free(hlInfo);
833 hlInfo = hlInfoNext;
834 }
835 *hlInfoHeadPtr = NULL;
836 }
837 return;
838}
839
840/* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */
841static HardLinkInfo *
842findHardLinkInfo (HardLinkInfo *hlInfo, dev_t dev, ino_t ino)
843{
844 while(hlInfo) {
845 if ((ino == hlInfo->ino) && (dev == hlInfo->dev))
846 break;
847 hlInfo = hlInfo->next;
848 }
849 return(hlInfo);
850}
851
Erik Andersen6acaa402000-03-26 14:03:20 +0000852/* Put an octal string into the specified buffer.
853 * The number is zero and space padded and possibly null padded.
854 * Returns TRUE if successful. */
855static int putOctal (char *cp, int len, long value)
856{
857 int tempLength;
Erik Andersen6acaa402000-03-26 14:03:20 +0000858 char tempBuffer[32];
Erik Andersen5661fe02000-04-05 01:00:52 +0000859 char *tempString = tempBuffer;
Erik Andersen6acaa402000-03-26 14:03:20 +0000860
861 /* Create a string of the specified length with an initial space,
862 * leading zeroes and the octal number, and a trailing null. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000863 sprintf (tempString, "%0*lo", len - 1, value);
Erik Andersen6acaa402000-03-26 14:03:20 +0000864
865 /* If the string is too large, suppress the leading space. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000866 tempLength = strlen (tempString) + 1;
Erik Andersen6acaa402000-03-26 14:03:20 +0000867 if (tempLength > len) {
868 tempLength--;
869 tempString++;
870 }
871
872 /* If the string is still too large, suppress the trailing null. */
873 if (tempLength > len)
874 tempLength--;
875
876 /* If the string is still too large, fail. */
877 if (tempLength > len)
878 return FALSE;
879
880 /* Copy the string to the field. */
881 memcpy (cp, tempString, len);
882
883 return TRUE;
884}
885
Erik Andersen68a9ea42000-04-04 18:39:50 +0000886/* Write out a tar header for the specified file/directory/whatever */
Erik Andersen3364d782000-03-28 00:58:14 +0000887static int
Matt Kraaie80a2632000-12-19 20:45:49 +0000888writeTarHeader(struct TarBallInfo *tbInfo, const char *header_name,
889 const char *real_name, struct stat *statbuf)
Erik Andersen6acaa402000-03-26 14:03:20 +0000890{
Erik Andersen5661fe02000-04-05 01:00:52 +0000891 long chksum=0;
892 struct TarHeader header;
893 const unsigned char *cp = (const unsigned char *) &header;
894 ssize_t size = sizeof(struct TarHeader);
Eric Andersenfdd51032000-08-02 18:48:26 +0000895
Erik Andersen5661fe02000-04-05 01:00:52 +0000896 memset( &header, 0, size);
Erik Andersen3364d782000-03-28 00:58:14 +0000897
Matt Kraaie80a2632000-12-19 20:45:49 +0000898 strncpy(header.name, header_name, sizeof(header.name));
Erik Andersenecd51242000-04-08 03:08:21 +0000899
Erik Andersen5661fe02000-04-05 01:00:52 +0000900 putOctal(header.mode, sizeof(header.mode), statbuf->st_mode);
901 putOctal(header.uid, sizeof(header.uid), statbuf->st_uid);
902 putOctal(header.gid, sizeof(header.gid), statbuf->st_gid);
903 putOctal(header.size, sizeof(header.size), 0); /* Regular file size is handled later */
904 putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime);
905 strncpy(header.magic, TAR_MAGIC TAR_VERSION,
906 TAR_MAGIC_LEN + TAR_VERSION_LEN );
Erik Andersen68a9ea42000-04-04 18:39:50 +0000907
Erik Andersen84e09e42000-04-08 20:58:35 +0000908 /* Enter the user and group names (default to root if it fails) */
Erik Andersen5661fe02000-04-05 01:00:52 +0000909 my_getpwuid(header.uname, statbuf->st_uid);
Erik Andersen5661fe02000-04-05 01:00:52 +0000910 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000911 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +0000912 my_getgrgid(header.gname, statbuf->st_gid);
913 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000914 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +0000915
Eric Andersen3d957c82000-12-07 00:34:58 +0000916 if (tbInfo->hlInfo) {
917 /* This is a hard link */
918 header.typeflag = LNKTYPE;
919 strncpy(header.linkname, tbInfo->hlInfo->name, sizeof(header.linkname));
920 } else if (S_ISLNK(statbuf->st_mode)) {
Mark Whitley8a633262001-04-30 18:17:00 +0000921 char *lpath = xreadlink(real_name);
Eric Andersen28355a32001-05-07 17:48:28 +0000922 if (!lpath) /* Already printed err msg inside xreadlink() */
923 return ( FALSE);
Erik Andersen5661fe02000-04-05 01:00:52 +0000924 header.typeflag = SYMTYPE;
Mark Whitley8a633262001-04-30 18:17:00 +0000925 strncpy(header.linkname, lpath, sizeof(header.linkname));
926 free(lpath);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000927 } else if (S_ISDIR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000928 header.typeflag = DIRTYPE;
929 strncat(header.name, "/", sizeof(header.name));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000930 } else if (S_ISCHR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000931 header.typeflag = CHRTYPE;
932 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
933 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000934 } else if (S_ISBLK(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000935 header.typeflag = BLKTYPE;
936 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
937 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000938 } else if (S_ISFIFO(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000939 header.typeflag = FIFOTYPE;
940 } else if (S_ISREG(statbuf->st_mode)) {
941 header.typeflag = REGTYPE;
942 putOctal(header.size, sizeof(header.size), statbuf->st_size);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000943 } else {
Matt Kraaidd19c692001-01-31 19:00:21 +0000944 error_msg("%s: Unknown file type", real_name);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000945 return ( FALSE);
946 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000947
Eric Andersen77d92682001-05-23 20:32:09 +0000948 /* Calculate and store the checksum (i.e., the sum of all of the bytes of
Erik Andersen5661fe02000-04-05 01:00:52 +0000949 * the header). The checksum field must be filled with blanks for the
950 * calculation. The checksum field is formatted differently from the
951 * other fields: it has [6] digits, a null, then a space -- rather than
952 * digits, followed by a null like the other fields... */
953 memset(header.chksum, ' ', sizeof(header.chksum));
954 cp = (const unsigned char *) &header;
955 while (size-- > 0)
956 chksum += *cp++;
957 putOctal(header.chksum, 7, chksum);
958
959 /* Now write the header out to disk */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000960 if ((size=full_write(tbInfo->tarFd, (char*)&header, sizeof(struct TarHeader))) < 0) {
Matt Kraaie80a2632000-12-19 20:45:49 +0000961 error_msg(io_error, real_name, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +0000962 return ( FALSE);
963 }
964 /* Pad the header up to the tar block size */
965 for (; size<TAR_BLOCK_SIZE; size++) {
966 write(tbInfo->tarFd, "\0", 1);
967 }
968 /* Now do the verbose thing (or not) */
Eric Andersenfdd51032000-08-02 18:48:26 +0000969 if (tbInfo->verboseFlag==TRUE) {
970 FILE *vbFd = stdout;
971 if (tbInfo->tarFd == fileno(stdout)) // If the archive goes to stdout, verbose to stderr
972 vbFd = stderr;
973 fprintf(vbFd, "%s\n", header.name);
974 }
Erik Andersen3364d782000-03-28 00:58:14 +0000975
976 return ( TRUE);
977}
978
979
Erik Andersen68a9ea42000-04-04 18:39:50 +0000980static int writeFileToTarball(const char *fileName, struct stat *statbuf, void* userData)
Erik Andersen3364d782000-03-28 00:58:14 +0000981{
Erik Andersen68a9ea42000-04-04 18:39:50 +0000982 struct TarBallInfo *tbInfo = (struct TarBallInfo *)userData;
Matt Kraaie80a2632000-12-19 20:45:49 +0000983 const char *header_name;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000984
Eric Andersen3d957c82000-12-07 00:34:58 +0000985 /*
986 ** Check to see if we are dealing with a hard link.
987 ** If so -
988 ** Treat the first occurance of a given dev/inode as a file while
989 ** treating any additional occurances as hard links. This is done
990 ** by adding the file information to the HardLinkInfo linked list.
991 */
992 tbInfo->hlInfo = NULL;
993 if (statbuf->st_nlink > 1) {
994 tbInfo->hlInfo = findHardLinkInfo(tbInfo->hlInfoHead, statbuf->st_dev,
995 statbuf->st_ino);
996 if (tbInfo->hlInfo == NULL)
997 addHardLinkInfo (&tbInfo->hlInfoHead, statbuf->st_dev,
998 statbuf->st_ino, statbuf->st_nlink, fileName);
999 }
1000
Erik Andersen68a9ea42000-04-04 18:39:50 +00001001 /* It is against the rules to archive a socket */
1002 if (S_ISSOCK(statbuf->st_mode)) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001003 error_msg("%s: socket ignored", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001004 return( TRUE);
1005 }
1006
1007 /* It is a bad idea to store the archive we are in the process of creating,
1008 * so check the device and inode to be sure that this particular file isn't
1009 * the new tarball */
1010 if (tbInfo->statBuf.st_dev == statbuf->st_dev &&
1011 tbInfo->statBuf.st_ino == statbuf->st_ino) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001012 error_msg("%s: file is the archive; skipping", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001013 return( TRUE);
1014 }
1015
Matt Kraaie80a2632000-12-19 20:45:49 +00001016 header_name = fileName;
1017 while (header_name[0] == '/') {
Matt Kraaia1f97752000-12-19 06:24:08 +00001018 static int alreadyWarned=FALSE;
1019 if (alreadyWarned==FALSE) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001020 error_msg("Removing leading '/' from member names");
Matt Kraaia1f97752000-12-19 06:24:08 +00001021 alreadyWarned=TRUE;
1022 }
Matt Kraaie80a2632000-12-19 20:45:49 +00001023 header_name++;
Matt Kraaia1f97752000-12-19 06:24:08 +00001024 }
1025
Eric Andersen1b1cfde2000-09-24 00:54:37 +00001026 if (strlen(fileName) >= NAME_SIZE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001027 error_msg(name_longer_than_foo, NAME_SIZE);
Eric Andersen1b1cfde2000-09-24 00:54:37 +00001028 return ( TRUE);
1029 }
1030
Matt Kraaie80a2632000-12-19 20:45:49 +00001031 if (header_name[0] == '\0')
Matt Kraaia1f97752000-12-19 06:24:08 +00001032 return TRUE;
1033
1034#if defined BB_FEATURE_TAR_EXCLUDE
Matt Kraaibe7499c2001-01-03 17:22:10 +00001035 if (exclude_file(tbInfo->excludeList, header_name)) {
1036 return SKIP;
Matt Kraaia1f97752000-12-19 06:24:08 +00001037 }
1038#endif
1039
Matt Kraaie80a2632000-12-19 20:45:49 +00001040 if (writeTarHeader(tbInfo, header_name, fileName, statbuf)==FALSE) {
Erik Andersen5661fe02000-04-05 01:00:52 +00001041 return( FALSE);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001042 }
Erik Andersen5661fe02000-04-05 01:00:52 +00001043
1044 /* Now, if the file is a regular file, copy it out to the tarball */
Eric Andersen3d957c82000-12-07 00:34:58 +00001045 if ((tbInfo->hlInfo == NULL)
1046 && (S_ISREG(statbuf->st_mode))) {
Erik Andersen5661fe02000-04-05 01:00:52 +00001047 int inputFileFd;
1048 char buffer[BUFSIZ];
1049 ssize_t size=0, readSize=0;
1050
1051 /* open the file we want to archive, and make sure all is well */
1052 if ((inputFileFd = open(fileName, O_RDONLY)) < 0) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001053 error_msg("%s: Cannot open: %s", fileName, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +00001054 return( FALSE);
1055 }
1056
1057 /* write the file to the archive */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001058 while ( (size = full_read(inputFileFd, buffer, sizeof(buffer))) > 0 ) {
1059 if (full_write(tbInfo->tarFd, buffer, size) != size ) {
Erik Andersen5661fe02000-04-05 01:00:52 +00001060 /* Output file seems to have a problem */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001061 error_msg(io_error, fileName, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +00001062 return( FALSE);
1063 }
1064 readSize+=size;
1065 }
1066 if (size == -1) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001067 error_msg(io_error, fileName, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +00001068 return( FALSE);
1069 }
1070 /* Pad the file up to the tar block size */
1071 for (; (readSize%TAR_BLOCK_SIZE) != 0; readSize++) {
1072 write(tbInfo->tarFd, "\0", 1);
1073 }
1074 close( inputFileFd);
1075 }
Erik Andersen68a9ea42000-04-04 18:39:50 +00001076
1077 return( TRUE);
Erik Andersen6acaa402000-03-26 14:03:20 +00001078}
1079
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001080static int writeTarFile(const char* tarName, int verboseFlag, char **argv,
1081 char** excludeList)
Erik Andersen6acaa402000-03-26 14:03:20 +00001082{
Erik Andersen3364d782000-03-28 00:58:14 +00001083 int tarFd=-1;
Erik Andersen68a9ea42000-04-04 18:39:50 +00001084 int errorFlag=FALSE;
Erik Andersen5661fe02000-04-05 01:00:52 +00001085 ssize_t size;
Erik Andersen68a9ea42000-04-04 18:39:50 +00001086 struct TarBallInfo tbInfo;
1087 tbInfo.verboseFlag = verboseFlag;
Eric Andersen3d957c82000-12-07 00:34:58 +00001088 tbInfo.hlInfoHead = NULL;
Erik Andersen3364d782000-03-28 00:58:14 +00001089
1090 /* Make sure there is at least one file to tar up. */
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001091 if (*argv == NULL)
Matt Kraaidd19c692001-01-31 19:00:21 +00001092 error_msg_and_die("Cowardly refusing to create an empty archive");
Erik Andersen6acaa402000-03-26 14:03:20 +00001093
1094 /* Open the tar file for writing. */
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001095 if (!strcmp(tarName, "-"))
Erik Andersen68a9ea42000-04-04 18:39:50 +00001096 tbInfo.tarFd = fileno(stdout);
Erik Andersen6acaa402000-03-26 14:03:20 +00001097 else
Erik Andersen68a9ea42000-04-04 18:39:50 +00001098 tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1099 if (tbInfo.tarFd < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +00001100 perror_msg( "Error opening '%s'", tarName);
Eric Andersen3d957c82000-12-07 00:34:58 +00001101 freeHardLinkInfo(&tbInfo.hlInfoHead);
Erik Andersen6acaa402000-03-26 14:03:20 +00001102 return ( FALSE);
1103 }
Erik Andersenecd51242000-04-08 03:08:21 +00001104 tbInfo.excludeList=excludeList;
Erik Andersen68a9ea42000-04-04 18:39:50 +00001105 /* Store the stat info for the tarball's file, so
1106 * can avoid including the tarball into itself.... */
1107 if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001108 error_msg_and_die(io_error, tarName, strerror(errno));
Erik Andersen6acaa402000-03-26 14:03:20 +00001109
Erik Andersen6acaa402000-03-26 14:03:20 +00001110 /* Read the directory/files and iterate over them one at a time */
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001111 while (*argv != NULL) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001112 if (recursive_action(*argv++, TRUE, FALSE, FALSE,
Erik Andersen68a9ea42000-04-04 18:39:50 +00001113 writeFileToTarball, writeFileToTarball,
1114 (void*) &tbInfo) == FALSE) {
1115 errorFlag = TRUE;
Erik Andersen3364d782000-03-28 00:58:14 +00001116 }
Erik Andersen6acaa402000-03-26 14:03:20 +00001117 }
Erik Andersen5661fe02000-04-05 01:00:52 +00001118 /* Write two empty blocks to the end of the archive */
1119 for (size=0; size<(2*TAR_BLOCK_SIZE); size++) {
1120 write(tbInfo.tarFd, "\0", 1);
1121 }
Erik Andersen0817d132000-04-09 15:17:40 +00001122
1123 /* To be pedantically correct, we would check if the tarball
Eric Andersen3c5ee9a2000-11-14 22:15:48 +00001124 * is smaller than 20 tar blocks, and pad it if it was smaller,
Erik Andersen0817d132000-04-09 15:17:40 +00001125 * but that isn't necessary for GNU tar interoperability, and
1126 * so is considered a waste of space */
1127
Erik Andersen68a9ea42000-04-04 18:39:50 +00001128 /* Hang up the tools, close up shop, head home */
Erik Andersen6acaa402000-03-26 14:03:20 +00001129 close(tarFd);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001130 if (errorFlag == TRUE) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001131 error_msg("Error exit delayed from previous errors");
Eric Andersen3d957c82000-12-07 00:34:58 +00001132 freeHardLinkInfo(&tbInfo.hlInfoHead);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001133 return(FALSE);
1134 }
Eric Andersen3d957c82000-12-07 00:34:58 +00001135 freeHardLinkInfo(&tbInfo.hlInfoHead);
Erik Andersen6acaa402000-03-26 14:03:20 +00001136 return( TRUE);
1137}
1138
1139
1140#endif
1141