blob: 389d7f02e26ae1298f540ef45c20b6be5e8caefa [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="-";
Eric Andersen091781e2001-06-21 19:30:10 +0000154 const char *cwd=NULL;
Erik Andersen0817d132000-04-09 15:17:40 +0000155#if defined BB_FEATURE_TAR_EXCLUDE
Erik Andersenecd51242000-04-08 03:08:21 +0000156 int excludeListSize=0;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000157 FILE *fileList;
158 char file[256];
Erik Andersen0817d132000-04-09 15:17:40 +0000159#endif
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000160#if defined BB_FEATURE_TAR_GZIP
Glenn L McGrath018e9e62001-03-28 07:27:26 +0000161 FILE *comp_file = NULL;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000162 int unzipFlag = FALSE;
163#endif
Erik Andersen298854f2000-03-23 01:09:18 +0000164 int listFlag = FALSE;
165 int extractFlag = FALSE;
166 int createFlag = FALSE;
167 int verboseFlag = FALSE;
168 int tostdoutFlag = FALSE;
Eric Andersen02f3b2e2000-12-01 19:04:52 +0000169 int status = FALSE;
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000170 int opt;
Glenn L McGrath018e9e62001-03-28 07:27:26 +0000171 pid_t pid;
Eric Andersencc8ed391999-10-05 16:24:54 +0000172
Erik Andersenecd51242000-04-08 03:08:21 +0000173 if (argc <= 1)
Eric Andersen67991cf2001-02-14 21:23:06 +0000174 show_usage();
Eric Andersencc8ed391999-10-05 16:24:54 +0000175
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000176 if (argv[1][0] != '-') {
177 char *tmp = xmalloc(strlen(argv[1]) + 2);
178 tmp[0] = '-';
179 strcpy(tmp + 1, argv[1]);
180 argv[1] = tmp;
181 }
182
183 while (
184#ifndef BB_FEATURE_TAR_EXCLUDE
Eric Andersen091781e2001-06-21 19:30:10 +0000185 (opt = getopt(argc, argv, "cxtzvOf:pC:"))
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000186#else
Eric Andersen091781e2001-06-21 19:30:10 +0000187 (opt = getopt_long(argc, argv, "cxtzvOf:X:pC:", longopts, NULL))
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000188#endif
189 > 0) {
190 switch (opt) {
191 case 'c':
192 if (extractFlag == TRUE || listFlag == TRUE)
193 goto flagError;
194 createFlag = TRUE;
195 break;
196 case 'x':
197 if (listFlag == TRUE || createFlag == TRUE)
198 goto flagError;
199 extractFlag = TRUE;
200 break;
201 case 't':
202 if (extractFlag == TRUE || createFlag == TRUE)
203 goto flagError;
204 listFlag = TRUE;
205 break;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000206#ifdef BB_FEATURE_TAR_GZIP
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000207 case 'z':
208 unzipFlag = TRUE;
209 break;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000210#endif
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000211 case 'v':
212 verboseFlag = TRUE;
213 break;
214 case 'O':
215 tostdoutFlag = TRUE;
216 break;
217 case 'f':
218 if (*tarName != '-')
Matt Kraaidd19c692001-01-31 19:00:21 +0000219 error_msg_and_die( "Only one 'f' option allowed");
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000220 tarName = optarg;
221 break;
Erik Andersen0817d132000-04-09 15:17:40 +0000222#if defined BB_FEATURE_TAR_EXCLUDE
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000223 case 'e':
224 excludeList=xrealloc( excludeList,
225 sizeof(char *) * (excludeListSize+2));
226 excludeList[excludeListSize] = optarg;
227 /* Tack a NULL onto the end of the list */
228 excludeList[++excludeListSize] = NULL;
229 case 'X':
230 fileList = xfopen(optarg, "r");
231 while (fgets(file, sizeof(file), fileList) != NULL) {
232 excludeList = xrealloc(excludeList,
233 sizeof(char *) * (excludeListSize+2));
Matt Kraai05e782d2001-02-01 16:49:30 +0000234 chomp(file);
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000235 excludeList[excludeListSize] = xstrdup(file);
236 /* Tack a NULL onto the end of the list */
237 excludeList[++excludeListSize] = NULL;
238 }
239 fclose(fileList);
240 break;
Erik Andersen0817d132000-04-09 15:17:40 +0000241#endif
Eric Andersenea4abff2001-06-21 15:17:59 +0000242 case 'p':
243 break;
Eric Andersen091781e2001-06-21 19:30:10 +0000244 case 'C':
245 cwd = xgetcwd((char *)cwd);
246 if (chdir(optarg)) {
247 printf("cd: %s: %s\n", optarg, strerror(errno));
248 return EXIT_FAILURE;
249 }
250 break;
Eric Andersenea4abff2001-06-21 15:17:59 +0000251 default:
Eric Andersen67991cf2001-02-14 21:23:06 +0000252 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000253 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000254 }
255
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000256 /*
Erik Andersene49d5ec2000-02-08 19:58:47 +0000257 * Do the correct type of action supplying the rest of the
258 * command line arguments as the list of files to process.
259 */
260 if (createFlag == TRUE) {
Erik Andersende552872000-01-23 01:34:05 +0000261#ifndef BB_FEATURE_TAR_CREATE
Matt Kraaidd19c692001-01-31 19:00:21 +0000262 error_msg_and_die( "This version of tar was not compiled with tar creation support.");
Erik Andersende552872000-01-23 01:34:05 +0000263#else
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000264#ifdef BB_FEATURE_TAR_GZIP
265 if (unzipFlag==TRUE)
Matt Kraaidd19c692001-01-31 19:00:21 +0000266 error_msg_and_die("Creation of compressed not internally support by tar, pipe to busybox gunzip");
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000267#endif
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000268 status = writeTarFile(tarName, verboseFlag, argv + optind, excludeList);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000269#endif
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000270 }
271 if (listFlag == TRUE || extractFlag == TRUE) {
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000272 int tarFd;
Matt Kraai3b3f5c32001-01-22 20:49:00 +0000273 if (argv[optind])
274 extractList = argv + optind;
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000275 /* Open the tar file for reading. */
276 if (!strcmp(tarName, "-"))
277 tarFd = fileno(stdin);
278 else
279 tarFd = open(tarName, O_RDONLY);
280 if (tarFd < 0)
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000281 perror_msg_and_die("Error opening '%s'", tarName);
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000282
283#ifdef BB_FEATURE_TAR_GZIP
284 /* unzip tarFd in a seperate process */
Glenn L McGrath018e9e62001-03-28 07:27:26 +0000285 if (unzipFlag == TRUE) {
286 comp_file = fdopen(tarFd, "r");
Glenn L McGrathb028e082001-07-13 06:43:03 +0000287
288 /* set the buffer size */
289 setvbuf(comp_file, NULL, _IOFBF, 0x8000);
290
Glenn L McGrath8e74bf92001-06-20 07:54:15 +0000291 if ((tarFd = fileno(gz_open(comp_file, &pid))) == EXIT_FAILURE) {
Glenn L McGrath018e9e62001-03-28 07:27:26 +0000292 error_msg_and_die("Couldnt unzip file");
293 }
Glenn L McGrath018e9e62001-03-28 07:27:26 +0000294 }
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000295#endif
296 status = readTarFile(tarFd, extractFlag, listFlag, tostdoutFlag,
Matt Kraai3e856ce2000-12-01 02:55:13 +0000297 verboseFlag, extractList, excludeList);
Glenn L McGrath018e9e62001-03-28 07:27:26 +0000298 close(tarFd);
Glenn L McGrath018e9e62001-03-28 07:27:26 +0000299#ifdef BB_FEATURE_TAR_GZIP
Glenn L McGrathae8ad352001-03-28 23:57:51 +0000300 if (unzipFlag == TRUE) {
301 gz_close(pid);
302 fclose(comp_file);
303 }
Glenn L McGrath018e9e62001-03-28 07:27:26 +0000304#endif
Glenn L McGrathae8ad352001-03-28 23:57:51 +0000305 }
Glenn L McGrath018e9e62001-03-28 07:27:26 +0000306
Eric Andersen091781e2001-06-21 19:30:10 +0000307 if (cwd)
308 chdir(cwd);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000309 if (status == TRUE)
310 return EXIT_SUCCESS;
311 else
312 return EXIT_FAILURE;
313
Erik Andersene49d5ec2000-02-08 19:58:47 +0000314 flagError:
Matt Kraaidd19c692001-01-31 19:00:21 +0000315 error_msg_and_die( "Exactly one of 'c', 'x' or 't' must be specified");
Erik Andersen298854f2000-03-23 01:09:18 +0000316}
317
318static void
Erik Andersen6a34b532000-04-07 06:55:38 +0000319fixUpPermissions(TarInfo *header)
320{
321 struct utimbuf t;
Eric Andersen77d92682001-05-23 20:32:09 +0000322 /* Now set permissions etc. for the new file */
Erik Andersen6a34b532000-04-07 06:55:38 +0000323 chown(header->name, header->uid, header->gid);
324 chmod(header->name, header->mode);
325 /* Reset the time */
326 t.actime = time(0);
327 t.modtime = header->mtime;
328 utime(header->name, &t);
329}
330
331static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000332tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersen298854f2000-03-23 01:09:18 +0000333{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000334 size_t writeSize;
335 size_t readSize;
336 size_t actualWriteSz;
Matt Kraai5710f9f2001-07-10 15:05:39 +0000337 char buffer[20 * TAR_BLOCK_SIZE];
Erik Andersen1ad302a2000-03-24 00:54:46 +0000338 size_t size = header->size;
339 int outFd=fileno(stdout);
340
341 /* Open the file to be written, if a file is supposed to be written */
342 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000343 /* Create the path to the file, just in case it isn't there...
344 * This should not screw up path permissions or anything. */
Matt Kraaiac20ce12001-08-24 19:51:54 +0000345 char *buf, *dir;
346 buf = xstrdup (header->name);
347 dir = dirname (buf);
Matt Kraaiceeff732001-06-21 19:41:37 +0000348 make_directory (dir, -1, FILEUTILS_RECUR);
Matt Kraaiac20ce12001-08-24 19:51:54 +0000349 free (buf);
Eric Andersen0c6a9702000-06-09 20:51:50 +0000350 if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY,
351 header->mode & ~S_IFMT)) < 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000352 error_msg(io_error, header->name, strerror(errno));
Eric Andersen0c6a9702000-06-09 20:51:50 +0000353 return( FALSE);
354 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000355 }
356
357 /* Write out the file, if we are supposed to be doing that */
358 while ( size > 0 ) {
359 actualWriteSz=0;
360 if ( size > sizeof(buffer) )
361 writeSize = readSize = sizeof(buffer);
362 else {
Matt Kraai5710f9f2001-07-10 15:05:39 +0000363 int mod = size % TAR_BLOCK_SIZE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000364 if ( mod != 0 )
Matt Kraai5710f9f2001-07-10 15:05:39 +0000365 readSize = size + (TAR_BLOCK_SIZE - mod);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000366 else
367 readSize = size;
368 writeSize = size;
369 }
Mark Whitleyf57c9442000-12-07 19:56:48 +0000370 if ( (readSize = full_read(header->tarFd, buffer, readSize)) <= 0 ) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000371 /* Tarball seems to have a problem */
Matt Kraaidd19c692001-01-31 19:00:21 +0000372 error_msg("Unexpected EOF in archive");
Erik Andersen6a34b532000-04-07 06:55:38 +0000373 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000374 }
375 if ( readSize < writeSize )
376 writeSize = readSize;
377
378 /* Write out the file, if we are supposed to be doing that */
379 if (extractFlag==TRUE) {
380
Mark Whitleyf57c9442000-12-07 19:56:48 +0000381 if ((actualWriteSz=full_write(outFd, buffer, writeSize)) != writeSize ) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000382 /* Output file seems to have a problem */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000383 error_msg(io_error, header->name, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000384 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000385 }
Erik Andersen0817d132000-04-09 15:17:40 +0000386 } else {
387 actualWriteSz=writeSize;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000388 }
389
390 size -= actualWriteSz;
391 }
392
393 /* Now we are done writing the file out, so try
394 * and fix up the permissions and whatnot */
395 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000396 close(outFd);
Erik Andersen6a34b532000-04-07 06:55:38 +0000397 fixUpPermissions(header);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000398 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000399 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000400}
401
Erik Andersen6a34b532000-04-07 06:55:38 +0000402static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000403tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000404{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000405 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000406 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000407
Matt Kraai623cfa92001-07-03 20:55:30 +0000408 if (make_directory(header->name, header->mode, FILEUTILS_RECUR) < 0)
Erik Andersen6a34b532000-04-07 06:55:38 +0000409 return( FALSE);
Matt Kraai541ffe32001-01-13 21:46:25 +0000410
Eric Andersencb2a3722001-06-04 16:54:39 +0000411 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000412 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000413}
414
Erik Andersen6a34b532000-04-07 06:55:38 +0000415static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000416tarExtractHardLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000417{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000418 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000419 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000420
421 if (link(header->linkname, header->name) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000422 perror_msg("%s: Cannot create hard link to '%s'", header->name,
423 header->linkname);
Erik Andersen6a34b532000-04-07 06:55:38 +0000424 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000425 }
426
Eric Andersen77d92682001-05-23 20:32:09 +0000427 /* Now set permissions etc. for the new directory */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000428 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000429 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000430}
431
Erik Andersen6a34b532000-04-07 06:55:38 +0000432static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000433tarExtractSymLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000434{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000435 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000436 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000437
438#ifdef S_ISLNK
439 if (symlink(header->linkname, header->name) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000440 perror_msg("%s: Cannot create symlink to '%s'", header->name,
441 header->linkname);
Erik Andersen6a34b532000-04-07 06:55:38 +0000442 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000443 }
444 /* Try to change ownership of the symlink.
445 * If libs doesn't support that, don't bother.
446 * Changing the pointed-to-file is the Wrong Thing(tm).
447 */
448#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
449 lchown(header->name, header->uid, header->gid);
450#endif
451
452 /* Do not change permissions or date on symlink,
453 * since it changes the pointed to file instead. duh. */
454#else
Matt Kraaidd19c692001-01-31 19:00:21 +0000455 error_msg("%s: Cannot create symlink to '%s': %s",
Erik Andersen6a34b532000-04-07 06:55:38 +0000456 header->name, header->linkname,
457 "symlinks not supported");
Erik Andersen1ad302a2000-03-24 00:54:46 +0000458#endif
Erik Andersen6a34b532000-04-07 06:55:38 +0000459 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000460}
461
Erik Andersen6a34b532000-04-07 06:55:38 +0000462static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000463tarExtractSpecial(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000464{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000465 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000466 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000467
468 if (S_ISCHR(header->mode) || S_ISBLK(header->mode) || S_ISSOCK(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000469 if (mknod(header->name, header->mode, makedev(header->devmajor, header->devminor)) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000470 perror_msg("%s: Cannot mknod", header->name);
Erik Andersen6a34b532000-04-07 06:55:38 +0000471 return( FALSE);
472 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000473 } else if (S_ISFIFO(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000474 if (mkfifo(header->name, header->mode) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000475 perror_msg("%s: Cannot mkfifo", header->name);
Erik Andersen6a34b532000-04-07 06:55:38 +0000476 return( FALSE);
477 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000478 }
479
Eric Andersen77d92682001-05-23 20:32:09 +0000480 /* Now set permissions etc. for the new directory */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000481 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000482 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000483}
484
Erik Andersen298854f2000-03-23 01:09:18 +0000485/* Parse the tar header and fill in the nice struct with the details */
486static int
Erik Andersen3364d782000-03-28 00:58:14 +0000487readTarHeader(struct TarHeader *rawHeader, struct TarInfo *header)
Erik Andersen298854f2000-03-23 01:09:18 +0000488{
Erik Andersene454fb62000-03-23 04:27:58 +0000489 int i;
Erik Andersen84e09e42000-04-08 20:58:35 +0000490 long chksum, sum=0;
Erik Andersene454fb62000-03-23 04:27:58 +0000491 unsigned char *s = (unsigned char *)rawHeader;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000492
Erik Andersen298854f2000-03-23 01:09:18 +0000493 header->name = rawHeader->name;
Erik Andersen84e09e42000-04-08 20:58:35 +0000494 /* Check for and relativify any absolute paths */
495 if ( *(header->name) == '/' ) {
496 static int alreadyWarned=FALSE;
497
498 while (*(header->name) == '/')
Matt Kraai7f7348b2001-05-22 14:18:03 +0000499 header->name++;
Erik Andersen84e09e42000-04-08 20:58:35 +0000500
501 if (alreadyWarned == FALSE) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000502 error_msg("Removing leading '/' from member names");
Erik Andersen84e09e42000-04-08 20:58:35 +0000503 alreadyWarned = TRUE;
504 }
505 }
506
Glenn L McGrath48081f82001-04-11 05:01:09 +0000507 header->mode = strtol(rawHeader->mode, NULL, 8);
508 header->uid = strtol(rawHeader->uid, NULL, 8);
509 header->gid = strtol(rawHeader->gid, NULL, 8);
510 header->size = strtol(rawHeader->size, NULL, 8);
511 header->mtime = strtol(rawHeader->mtime, NULL, 8);
512 chksum = strtol(rawHeader->chksum, NULL, 8);
Erik Andersen298854f2000-03-23 01:09:18 +0000513 header->type = rawHeader->typeflag;
514 header->linkname = rawHeader->linkname;
Glenn L McGrath48081f82001-04-11 05:01:09 +0000515 header->devmajor = strtol(rawHeader->devmajor, NULL, 8);
516 header->devminor = strtol(rawHeader->devminor, NULL, 8);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000517
Erik Andersen298854f2000-03-23 01:09:18 +0000518 /* Check the checksum */
Erik Andersen84e09e42000-04-08 20:58:35 +0000519 for (i = sizeof(*rawHeader); i-- != 0;) {
Erik Andersen298854f2000-03-23 01:09:18 +0000520 sum += *s++;
Erik Andersen84e09e42000-04-08 20:58:35 +0000521 }
522 /* Remove the effects of the checksum field (replace
523 * with blanks for the purposes of the checksum) */
524 s = rawHeader->chksum;
525 for (i = sizeof(rawHeader->chksum) ; i-- != 0;) {
526 sum -= *s++;
527 }
528 sum += ' ' * sizeof(rawHeader->chksum);
Erik Andersene454fb62000-03-23 04:27:58 +0000529 if (sum == chksum )
Erik Andersen298854f2000-03-23 01:09:18 +0000530 return ( TRUE);
531 return( FALSE);
532}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000533
Eric Andersen3e6ff902001-03-09 21:24:12 +0000534static int exclude_file(char **excluded_files, const char *file)
Matt Kraaibe7499c2001-01-03 17:22:10 +0000535{
536 int i;
537
538 if (excluded_files == NULL)
539 return 0;
540
541 for (i = 0; excluded_files[i] != NULL; i++) {
542 if (excluded_files[i][0] == '/') {
543 if (fnmatch(excluded_files[i], file,
544 FNM_PATHNAME | FNM_LEADING_DIR) == 0)
545 return 1;
546 } else {
547 const char *p;
548
549 for (p = file; p[0] != '\0'; p++) {
550 if ((p == file || p[-1] == '/') && p[0] != '/' &&
551 fnmatch(excluded_files[i], p,
552 FNM_PATHNAME | FNM_LEADING_DIR) == 0)
553 return 1;
554 }
555 }
556 }
557
558 return 0;
559}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000560
Eric Andersen3e6ff902001-03-09 21:24:12 +0000561static int extract_file(char **extract_files, const char *file)
Matt Kraai8f8dab92001-01-22 05:25:19 +0000562{
563 int i;
564
565 if (extract_files == NULL)
566 return 1;
567
568 for (i = 0; extract_files[i] != NULL; i++) {
569 if (fnmatch(extract_files[i], file, FNM_LEADING_DIR) == 0)
570 return 1;
571 }
572
573 return 0;
574}
575
Erik Andersen1ad302a2000-03-24 00:54:46 +0000576/*
577 * Read a tar file and extract or list the specified files within it.
578 * If the list is empty than all files are extracted or listed.
579 */
Glenn L McGrath2975a342001-04-11 16:49:07 +0000580static int readTarFile(int tarFd, int extractFlag, int listFlag,
Matt Kraaib92223b2000-09-04 08:25:42 +0000581 int tostdoutFlag, int verboseFlag, char** extractList,
582 char** excludeList)
Erik Andersen1ad302a2000-03-24 00:54:46 +0000583{
Glenn L McGrath46f44d22000-12-10 01:57:30 +0000584 int status;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000585 int errorFlag=FALSE;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000586 int skipNextHeaderFlag=FALSE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000587 TarHeader rawHeader;
588 TarInfo header;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000589
Erik Andersen1ad302a2000-03-24 00:54:46 +0000590 /* Read the tar file, and iterate over it one file at a time */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000591 while ( (status = full_read(tarFd, (char*)&rawHeader, TAR_BLOCK_SIZE)) == TAR_BLOCK_SIZE ) {
Erik Andersene2729152000-02-18 21:34:17 +0000592
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000593 /* Try to read the header */
Erik Andersen3364d782000-03-28 00:58:14 +0000594 if ( readTarHeader(&rawHeader, &header) == FALSE ) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000595 if ( *(header.name) == '\0' ) {
596 goto endgame;
597 } else {
598 errorFlag=TRUE;
Matt Kraaidd19c692001-01-31 19:00:21 +0000599 error_msg("Bad tar header, skipping");
Erik Andersen1ad302a2000-03-24 00:54:46 +0000600 continue;
601 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000602 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000603 if ( *(header.name) == '\0' )
Matt Kraaie0244b02001-05-01 21:12:31 +0000604 continue;
Erik Andersen0817d132000-04-09 15:17:40 +0000605 header.tarFd = tarFd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000606
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000607 /* Skip funky extra GNU headers that precede long files */
608 if ( (header.type == GNULONGNAME) || (header.type == GNULONGLINK) ) {
609 skipNextHeaderFlag=TRUE;
Matt Kraaiab8f9e22000-11-18 01:28:57 +0000610 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
611 errorFlag = TRUE;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000612 continue;
613 }
614 if ( skipNextHeaderFlag == TRUE ) {
615 skipNextHeaderFlag=FALSE;
Mark Whitleyf57c9442000-12-07 19:56:48 +0000616 error_msg(name_longer_than_foo, NAME_SIZE);
Matt Kraaiab8f9e22000-11-18 01:28:57 +0000617 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
618 errorFlag = TRUE;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000619 continue;
620 }
621
Erik Andersen0817d132000-04-09 15:17:40 +0000622#if defined BB_FEATURE_TAR_EXCLUDE
Matt Kraaibe7499c2001-01-03 17:22:10 +0000623 if (exclude_file(excludeList, header.name)) {
Erik Andersen0817d132000-04-09 15:17:40 +0000624 /* There are not the droids you're looking for, move along */
Matt Kraaibe7499c2001-01-03 17:22:10 +0000625 /* If it is a regular file, pretend to extract it with
626 * the extractFlag set to FALSE, so the junk in the tarball
627 * is properly skipped over */
628 if ( header.type==REGTYPE || header.type==REGTYPE0 ) {
629 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
630 errorFlag = TRUE;
631 }
632 continue;
Erik Andersen0817d132000-04-09 15:17:40 +0000633 }
634#endif
Matt Kraaibe7499c2001-01-03 17:22:10 +0000635
Matt Kraai8f8dab92001-01-22 05:25:19 +0000636 if (!extract_file(extractList, header.name)) {
Matt Kraaib92223b2000-09-04 08:25:42 +0000637 /* There are not the droids you're looking for, move along */
Matt Kraai8f8dab92001-01-22 05:25:19 +0000638 /* If it is a regular file, pretend to extract it with
639 * the extractFlag set to FALSE, so the junk in the tarball
640 * is properly skipped over */
641 if ( header.type==REGTYPE || header.type==REGTYPE0 ) {
642 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
643 errorFlag = TRUE;
Matt Kraaib92223b2000-09-04 08:25:42 +0000644 }
Matt Kraai8f8dab92001-01-22 05:25:19 +0000645 continue;
Matt Kraaib92223b2000-09-04 08:25:42 +0000646 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000647
Matt Kraai82cfbad2000-09-15 21:18:43 +0000648 if (listFlag == TRUE) {
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000649 /* Special treatment if the list (-t) flag is on */
650 if (verboseFlag == TRUE) {
651 int len, len1;
652 char buf[35];
653 struct tm *tm = localtime (&(header.mtime));
654
Mark Whitleyf57c9442000-12-07 19:56:48 +0000655 len=printf("%s ", mode_string(header.mode));
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000656 my_getpwuid(buf, header.uid);
657 if (! *buf)
658 len+=printf("%d", header.uid);
659 else
660 len+=printf("%s", buf);
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000661 my_getgrgid(buf, header.gid);
662 if (! *buf)
663 len+=printf("/%-d ", header.gid);
664 else
665 len+=printf("/%-s ", buf);
666
667 if (header.type==CHRTYPE || header.type==BLKTYPE) {
668 len1=snprintf(buf, sizeof(buf), "%ld,%-ld ",
669 header.devmajor, header.devminor);
670 } else {
671 len1=snprintf(buf, sizeof(buf), "%lu ", (long)header.size);
672 }
673 /* Jump through some hoops to make the columns match up */
674 for(;(len+len1)<31;len++)
675 printf(" ");
676 printf(buf);
677
678 /* Use ISO 8610 time format */
679 if (tm) {
680 printf ("%04d-%02d-%02d %02d:%02d:%02d ",
681 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
682 tm->tm_hour, tm->tm_min, tm->tm_sec);
683 }
684 }
Eric Andersenfdd51032000-08-02 18:48:26 +0000685 printf("%s", header.name);
Matt Kraai82cfbad2000-09-15 21:18:43 +0000686 if (verboseFlag == TRUE) {
687 if (header.type==LNKTYPE) /* If this is a link, say so */
688 printf(" link to %s", header.linkname);
689 else if (header.type==SYMTYPE)
690 printf(" -> %s", header.linkname);
691 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000692 printf("\n");
693 }
694
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000695 /* List contents if we are supposed to do that */
696 if (verboseFlag == TRUE && extractFlag == TRUE) {
697 /* Now the normal listing */
698 FILE *vbFd = stdout;
699 if (tostdoutFlag == TRUE) // If the archive goes to stdout, verbose to stderr
700 vbFd = stderr;
701 fprintf(vbFd, "%s\n", header.name);
702 }
703
Matt Kraaif4462972000-09-01 02:50:48 +0000704 /* Remove files if we would overwrite them */
Matt Kraaida542f32000-09-01 02:53:01 +0000705 if (extractFlag == TRUE && tostdoutFlag == FALSE)
Matt Kraaif4462972000-09-01 02:50:48 +0000706 unlink(header.name);
Eric Andersencc8ed391999-10-05 16:24:54 +0000707
Erik Andersen1ad302a2000-03-24 00:54:46 +0000708 /* If we got here, we can be certain we have a legitimate
709 * header to work with. So work with it. */
710 switch ( header.type ) {
711 case REGTYPE:
712 case REGTYPE0:
713 /* If the name ends in a '/' then assume it is
714 * supposed to be a directory, and fall through */
Glenn L McGrathaf166e72001-04-29 00:50:33 +0000715 if (!last_char_is(header.name,'/')) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000716 if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE)
717 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000718 break;
719 }
720 case DIRTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000721 if (tarExtractDirectory( &header, extractFlag, tostdoutFlag)==FALSE)
722 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000723 break;
724 case LNKTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000725 if (tarExtractHardLink( &header, extractFlag, tostdoutFlag)==FALSE)
726 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000727 break;
728 case SYMTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000729 if (tarExtractSymLink( &header, extractFlag, tostdoutFlag)==FALSE)
730 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000731 break;
732 case CHRTYPE:
733 case BLKTYPE:
734 case FIFOTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000735 if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE)
736 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000737 break;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000738#if 0
739 /* Handled earlier */
740 case GNULONGNAME:
741 case GNULONGLINK:
742 skipNextHeaderFlag=TRUE;
743 break;
744#endif
Erik Andersen1ad302a2000-03-24 00:54:46 +0000745 default:
Matt Kraaidd19c692001-01-31 19:00:21 +0000746 error_msg("Unknown file type '%c' in tar file", header.type);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000747 close( tarFd);
748 return( FALSE);
749 }
750 }
751 close(tarFd);
752 if (status > 0) {
753 /* Bummer - we read a partial header */
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000754 perror_msg("Error reading tar file");
Erik Andersen1ad302a2000-03-24 00:54:46 +0000755 return ( FALSE);
756 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000757 else if (errorFlag==TRUE) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000758 error_msg( "Error exit delayed from previous errors");
Erik Andersen6a34b532000-04-07 06:55:38 +0000759 return( FALSE);
760 } else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000761 return( status);
762
763 /* Stuff to do when we are done */
764endgame:
765 close( tarFd);
766 if ( *(header.name) == '\0' ) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000767 if (errorFlag==TRUE)
Matt Kraaidd19c692001-01-31 19:00:21 +0000768 error_msg( "Error exit delayed from previous errors");
Erik Andersen6a34b532000-04-07 06:55:38 +0000769 else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000770 return( TRUE);
771 }
772 return( FALSE);
773}
774
Erik Andersen6acaa402000-03-26 14:03:20 +0000775
776#ifdef BB_FEATURE_TAR_CREATE
777
Eric Andersen3d957c82000-12-07 00:34:58 +0000778/*
779** writeTarFile(), writeFileToTarball(), and writeTarHeader() are
780** the only functions that deal with the HardLinkInfo structure.
781** Even these functions use the xxxHardLinkInfo() functions.
782*/
783typedef struct HardLinkInfo HardLinkInfo;
784struct HardLinkInfo
785{
786 HardLinkInfo *next; /* Next entry in list */
787 dev_t dev; /* Device number */
788 ino_t ino; /* Inode number */
789 short linkCount; /* (Hard) Link Count */
790 char name[1]; /* Start of filename (must be last) */
791};
792
Erik Andersen68a9ea42000-04-04 18:39:50 +0000793/* Some info to be carried along when creating a new tarball */
794struct TarBallInfo
795{
796 char* fileName; /* File name of the tarball */
797 int tarFd; /* Open-for-write file descriptor
798 for the tarball */
799 struct stat statBuf; /* Stat info for the tarball, letting
800 us know the inode and device that the
801 tarball lives, so we can avoid trying
802 to include the tarball into itself */
803 int verboseFlag; /* Whether to print extra stuff or not */
Erik Andersenecd51242000-04-08 03:08:21 +0000804 char** excludeList; /* List of files to not include */
Eric Andersen3d957c82000-12-07 00:34:58 +0000805 HardLinkInfo *hlInfoHead; /* Hard Link Tracking Information */
806 HardLinkInfo *hlInfo; /* Hard Link Info for the current file */
Erik Andersen68a9ea42000-04-04 18:39:50 +0000807};
808typedef struct TarBallInfo TarBallInfo;
809
810
Eric Andersen3d957c82000-12-07 00:34:58 +0000811/* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */
812static void
813addHardLinkInfo (HardLinkInfo **hlInfoHeadPtr, dev_t dev, ino_t ino,
814 short linkCount, const char *name)
815{
816 /* Note: hlInfoHeadPtr can never be NULL! */
817 HardLinkInfo *hlInfo;
818
819 hlInfo = (HardLinkInfo *)xmalloc(sizeof(HardLinkInfo)+strlen(name)+1);
820 if (hlInfo) {
821 hlInfo->next = *hlInfoHeadPtr;
822 *hlInfoHeadPtr = hlInfo;
823 hlInfo->dev = dev;
824 hlInfo->ino = ino;
825 hlInfo->linkCount = linkCount;
826 strcpy(hlInfo->name, name);
827 }
828 return;
829}
830
831static void
832freeHardLinkInfo (HardLinkInfo **hlInfoHeadPtr)
833{
834 HardLinkInfo *hlInfo = NULL;
835 HardLinkInfo *hlInfoNext = NULL;
836
837 if (hlInfoHeadPtr) {
838 hlInfo = *hlInfoHeadPtr;
839 while (hlInfo) {
840 hlInfoNext = hlInfo->next;
841 free(hlInfo);
842 hlInfo = hlInfoNext;
843 }
844 *hlInfoHeadPtr = NULL;
845 }
846 return;
847}
848
849/* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */
850static HardLinkInfo *
851findHardLinkInfo (HardLinkInfo *hlInfo, dev_t dev, ino_t ino)
852{
853 while(hlInfo) {
854 if ((ino == hlInfo->ino) && (dev == hlInfo->dev))
855 break;
856 hlInfo = hlInfo->next;
857 }
858 return(hlInfo);
859}
860
Erik Andersen6acaa402000-03-26 14:03:20 +0000861/* Put an octal string into the specified buffer.
862 * The number is zero and space padded and possibly null padded.
863 * Returns TRUE if successful. */
864static int putOctal (char *cp, int len, long value)
865{
866 int tempLength;
Erik Andersen6acaa402000-03-26 14:03:20 +0000867 char tempBuffer[32];
Erik Andersen5661fe02000-04-05 01:00:52 +0000868 char *tempString = tempBuffer;
Erik Andersen6acaa402000-03-26 14:03:20 +0000869
870 /* Create a string of the specified length with an initial space,
871 * leading zeroes and the octal number, and a trailing null. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000872 sprintf (tempString, "%0*lo", len - 1, value);
Erik Andersen6acaa402000-03-26 14:03:20 +0000873
874 /* If the string is too large, suppress the leading space. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000875 tempLength = strlen (tempString) + 1;
Erik Andersen6acaa402000-03-26 14:03:20 +0000876 if (tempLength > len) {
877 tempLength--;
878 tempString++;
879 }
880
881 /* If the string is still too large, suppress the trailing null. */
882 if (tempLength > len)
883 tempLength--;
884
885 /* If the string is still too large, fail. */
886 if (tempLength > len)
887 return FALSE;
888
889 /* Copy the string to the field. */
890 memcpy (cp, tempString, len);
891
892 return TRUE;
893}
894
Erik Andersen68a9ea42000-04-04 18:39:50 +0000895/* Write out a tar header for the specified file/directory/whatever */
Erik Andersen3364d782000-03-28 00:58:14 +0000896static int
Matt Kraaie80a2632000-12-19 20:45:49 +0000897writeTarHeader(struct TarBallInfo *tbInfo, const char *header_name,
898 const char *real_name, struct stat *statbuf)
Erik Andersen6acaa402000-03-26 14:03:20 +0000899{
Erik Andersen5661fe02000-04-05 01:00:52 +0000900 long chksum=0;
901 struct TarHeader header;
902 const unsigned char *cp = (const unsigned char *) &header;
903 ssize_t size = sizeof(struct TarHeader);
Eric Andersenfdd51032000-08-02 18:48:26 +0000904
Erik Andersen5661fe02000-04-05 01:00:52 +0000905 memset( &header, 0, size);
Erik Andersen3364d782000-03-28 00:58:14 +0000906
Matt Kraaie80a2632000-12-19 20:45:49 +0000907 strncpy(header.name, header_name, sizeof(header.name));
Erik Andersenecd51242000-04-08 03:08:21 +0000908
Erik Andersen5661fe02000-04-05 01:00:52 +0000909 putOctal(header.mode, sizeof(header.mode), statbuf->st_mode);
910 putOctal(header.uid, sizeof(header.uid), statbuf->st_uid);
911 putOctal(header.gid, sizeof(header.gid), statbuf->st_gid);
912 putOctal(header.size, sizeof(header.size), 0); /* Regular file size is handled later */
913 putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime);
914 strncpy(header.magic, TAR_MAGIC TAR_VERSION,
915 TAR_MAGIC_LEN + TAR_VERSION_LEN );
Erik Andersen68a9ea42000-04-04 18:39:50 +0000916
Erik Andersen84e09e42000-04-08 20:58:35 +0000917 /* Enter the user and group names (default to root if it fails) */
Erik Andersen5661fe02000-04-05 01:00:52 +0000918 my_getpwuid(header.uname, statbuf->st_uid);
Erik Andersen5661fe02000-04-05 01:00:52 +0000919 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000920 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +0000921 my_getgrgid(header.gname, statbuf->st_gid);
922 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000923 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +0000924
Eric Andersen3d957c82000-12-07 00:34:58 +0000925 if (tbInfo->hlInfo) {
926 /* This is a hard link */
927 header.typeflag = LNKTYPE;
928 strncpy(header.linkname, tbInfo->hlInfo->name, sizeof(header.linkname));
929 } else if (S_ISLNK(statbuf->st_mode)) {
Mark Whitley8a633262001-04-30 18:17:00 +0000930 char *lpath = xreadlink(real_name);
Eric Andersen28355a32001-05-07 17:48:28 +0000931 if (!lpath) /* Already printed err msg inside xreadlink() */
932 return ( FALSE);
Erik Andersen5661fe02000-04-05 01:00:52 +0000933 header.typeflag = SYMTYPE;
Mark Whitley8a633262001-04-30 18:17:00 +0000934 strncpy(header.linkname, lpath, sizeof(header.linkname));
935 free(lpath);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000936 } else if (S_ISDIR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000937 header.typeflag = DIRTYPE;
938 strncat(header.name, "/", sizeof(header.name));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000939 } else if (S_ISCHR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000940 header.typeflag = CHRTYPE;
941 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
942 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000943 } else if (S_ISBLK(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000944 header.typeflag = BLKTYPE;
945 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
946 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000947 } else if (S_ISFIFO(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000948 header.typeflag = FIFOTYPE;
949 } else if (S_ISREG(statbuf->st_mode)) {
950 header.typeflag = REGTYPE;
951 putOctal(header.size, sizeof(header.size), statbuf->st_size);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000952 } else {
Matt Kraaidd19c692001-01-31 19:00:21 +0000953 error_msg("%s: Unknown file type", real_name);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000954 return ( FALSE);
955 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000956
Eric Andersen77d92682001-05-23 20:32:09 +0000957 /* Calculate and store the checksum (i.e., the sum of all of the bytes of
Erik Andersen5661fe02000-04-05 01:00:52 +0000958 * the header). The checksum field must be filled with blanks for the
959 * calculation. The checksum field is formatted differently from the
960 * other fields: it has [6] digits, a null, then a space -- rather than
961 * digits, followed by a null like the other fields... */
962 memset(header.chksum, ' ', sizeof(header.chksum));
963 cp = (const unsigned char *) &header;
964 while (size-- > 0)
965 chksum += *cp++;
966 putOctal(header.chksum, 7, chksum);
967
968 /* Now write the header out to disk */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000969 if ((size=full_write(tbInfo->tarFd, (char*)&header, sizeof(struct TarHeader))) < 0) {
Matt Kraaie80a2632000-12-19 20:45:49 +0000970 error_msg(io_error, real_name, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +0000971 return ( FALSE);
972 }
973 /* Pad the header up to the tar block size */
974 for (; size<TAR_BLOCK_SIZE; size++) {
975 write(tbInfo->tarFd, "\0", 1);
976 }
977 /* Now do the verbose thing (or not) */
Eric Andersenfdd51032000-08-02 18:48:26 +0000978 if (tbInfo->verboseFlag==TRUE) {
979 FILE *vbFd = stdout;
980 if (tbInfo->tarFd == fileno(stdout)) // If the archive goes to stdout, verbose to stderr
981 vbFd = stderr;
982 fprintf(vbFd, "%s\n", header.name);
983 }
Erik Andersen3364d782000-03-28 00:58:14 +0000984
985 return ( TRUE);
986}
987
988
Erik Andersen68a9ea42000-04-04 18:39:50 +0000989static int writeFileToTarball(const char *fileName, struct stat *statbuf, void* userData)
Erik Andersen3364d782000-03-28 00:58:14 +0000990{
Erik Andersen68a9ea42000-04-04 18:39:50 +0000991 struct TarBallInfo *tbInfo = (struct TarBallInfo *)userData;
Matt Kraaie80a2632000-12-19 20:45:49 +0000992 const char *header_name;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000993
Eric Andersen3d957c82000-12-07 00:34:58 +0000994 /*
995 ** Check to see if we are dealing with a hard link.
996 ** If so -
997 ** Treat the first occurance of a given dev/inode as a file while
998 ** treating any additional occurances as hard links. This is done
999 ** by adding the file information to the HardLinkInfo linked list.
1000 */
1001 tbInfo->hlInfo = NULL;
1002 if (statbuf->st_nlink > 1) {
1003 tbInfo->hlInfo = findHardLinkInfo(tbInfo->hlInfoHead, statbuf->st_dev,
1004 statbuf->st_ino);
1005 if (tbInfo->hlInfo == NULL)
1006 addHardLinkInfo (&tbInfo->hlInfoHead, statbuf->st_dev,
1007 statbuf->st_ino, statbuf->st_nlink, fileName);
1008 }
1009
Erik Andersen68a9ea42000-04-04 18:39:50 +00001010 /* It is against the rules to archive a socket */
1011 if (S_ISSOCK(statbuf->st_mode)) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001012 error_msg("%s: socket ignored", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001013 return( TRUE);
1014 }
1015
1016 /* It is a bad idea to store the archive we are in the process of creating,
1017 * so check the device and inode to be sure that this particular file isn't
1018 * the new tarball */
1019 if (tbInfo->statBuf.st_dev == statbuf->st_dev &&
1020 tbInfo->statBuf.st_ino == statbuf->st_ino) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001021 error_msg("%s: file is the archive; skipping", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001022 return( TRUE);
1023 }
1024
Matt Kraaie80a2632000-12-19 20:45:49 +00001025 header_name = fileName;
1026 while (header_name[0] == '/') {
Matt Kraaia1f97752000-12-19 06:24:08 +00001027 static int alreadyWarned=FALSE;
1028 if (alreadyWarned==FALSE) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001029 error_msg("Removing leading '/' from member names");
Matt Kraaia1f97752000-12-19 06:24:08 +00001030 alreadyWarned=TRUE;
1031 }
Matt Kraaie80a2632000-12-19 20:45:49 +00001032 header_name++;
Matt Kraaia1f97752000-12-19 06:24:08 +00001033 }
1034
Eric Andersen1b1cfde2000-09-24 00:54:37 +00001035 if (strlen(fileName) >= NAME_SIZE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001036 error_msg(name_longer_than_foo, NAME_SIZE);
Eric Andersen1b1cfde2000-09-24 00:54:37 +00001037 return ( TRUE);
1038 }
1039
Matt Kraaie80a2632000-12-19 20:45:49 +00001040 if (header_name[0] == '\0')
Matt Kraaia1f97752000-12-19 06:24:08 +00001041 return TRUE;
1042
1043#if defined BB_FEATURE_TAR_EXCLUDE
Matt Kraaibe7499c2001-01-03 17:22:10 +00001044 if (exclude_file(tbInfo->excludeList, header_name)) {
1045 return SKIP;
Matt Kraaia1f97752000-12-19 06:24:08 +00001046 }
1047#endif
1048
Matt Kraaie80a2632000-12-19 20:45:49 +00001049 if (writeTarHeader(tbInfo, header_name, fileName, statbuf)==FALSE) {
Erik Andersen5661fe02000-04-05 01:00:52 +00001050 return( FALSE);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001051 }
Erik Andersen5661fe02000-04-05 01:00:52 +00001052
1053 /* Now, if the file is a regular file, copy it out to the tarball */
Eric Andersen3d957c82000-12-07 00:34:58 +00001054 if ((tbInfo->hlInfo == NULL)
1055 && (S_ISREG(statbuf->st_mode))) {
Erik Andersen5661fe02000-04-05 01:00:52 +00001056 int inputFileFd;
1057 char buffer[BUFSIZ];
1058 ssize_t size=0, readSize=0;
1059
1060 /* open the file we want to archive, and make sure all is well */
1061 if ((inputFileFd = open(fileName, O_RDONLY)) < 0) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001062 error_msg("%s: Cannot open: %s", fileName, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +00001063 return( FALSE);
1064 }
1065
1066 /* write the file to the archive */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001067 while ( (size = full_read(inputFileFd, buffer, sizeof(buffer))) > 0 ) {
1068 if (full_write(tbInfo->tarFd, buffer, size) != size ) {
Erik Andersen5661fe02000-04-05 01:00:52 +00001069 /* Output file seems to have a problem */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001070 error_msg(io_error, fileName, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +00001071 return( FALSE);
1072 }
1073 readSize+=size;
1074 }
1075 if (size == -1) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001076 error_msg(io_error, fileName, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +00001077 return( FALSE);
1078 }
1079 /* Pad the file up to the tar block size */
1080 for (; (readSize%TAR_BLOCK_SIZE) != 0; readSize++) {
1081 write(tbInfo->tarFd, "\0", 1);
1082 }
1083 close( inputFileFd);
1084 }
Erik Andersen68a9ea42000-04-04 18:39:50 +00001085
1086 return( TRUE);
Erik Andersen6acaa402000-03-26 14:03:20 +00001087}
1088
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001089static int writeTarFile(const char* tarName, int verboseFlag, char **argv,
1090 char** excludeList)
Erik Andersen6acaa402000-03-26 14:03:20 +00001091{
Erik Andersen3364d782000-03-28 00:58:14 +00001092 int tarFd=-1;
Erik Andersen68a9ea42000-04-04 18:39:50 +00001093 int errorFlag=FALSE;
Erik Andersen5661fe02000-04-05 01:00:52 +00001094 ssize_t size;
Erik Andersen68a9ea42000-04-04 18:39:50 +00001095 struct TarBallInfo tbInfo;
1096 tbInfo.verboseFlag = verboseFlag;
Eric Andersen3d957c82000-12-07 00:34:58 +00001097 tbInfo.hlInfoHead = NULL;
Erik Andersen3364d782000-03-28 00:58:14 +00001098
1099 /* Make sure there is at least one file to tar up. */
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001100 if (*argv == NULL)
Matt Kraaidd19c692001-01-31 19:00:21 +00001101 error_msg_and_die("Cowardly refusing to create an empty archive");
Erik Andersen6acaa402000-03-26 14:03:20 +00001102
1103 /* Open the tar file for writing. */
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001104 if (!strcmp(tarName, "-"))
Erik Andersen68a9ea42000-04-04 18:39:50 +00001105 tbInfo.tarFd = fileno(stdout);
Erik Andersen6acaa402000-03-26 14:03:20 +00001106 else
Erik Andersen68a9ea42000-04-04 18:39:50 +00001107 tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1108 if (tbInfo.tarFd < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +00001109 perror_msg( "Error opening '%s'", tarName);
Eric Andersen3d957c82000-12-07 00:34:58 +00001110 freeHardLinkInfo(&tbInfo.hlInfoHead);
Erik Andersen6acaa402000-03-26 14:03:20 +00001111 return ( FALSE);
1112 }
Erik Andersenecd51242000-04-08 03:08:21 +00001113 tbInfo.excludeList=excludeList;
Erik Andersen68a9ea42000-04-04 18:39:50 +00001114 /* Store the stat info for the tarball's file, so
1115 * can avoid including the tarball into itself.... */
1116 if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001117 error_msg_and_die(io_error, tarName, strerror(errno));
Erik Andersen6acaa402000-03-26 14:03:20 +00001118
Erik Andersen6acaa402000-03-26 14:03:20 +00001119 /* Read the directory/files and iterate over them one at a time */
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001120 while (*argv != NULL) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001121 if (recursive_action(*argv++, TRUE, FALSE, FALSE,
Erik Andersen68a9ea42000-04-04 18:39:50 +00001122 writeFileToTarball, writeFileToTarball,
1123 (void*) &tbInfo) == FALSE) {
1124 errorFlag = TRUE;
Erik Andersen3364d782000-03-28 00:58:14 +00001125 }
Erik Andersen6acaa402000-03-26 14:03:20 +00001126 }
Erik Andersen5661fe02000-04-05 01:00:52 +00001127 /* Write two empty blocks to the end of the archive */
1128 for (size=0; size<(2*TAR_BLOCK_SIZE); size++) {
1129 write(tbInfo.tarFd, "\0", 1);
1130 }
Erik Andersen0817d132000-04-09 15:17:40 +00001131
1132 /* To be pedantically correct, we would check if the tarball
Eric Andersen3c5ee9a2000-11-14 22:15:48 +00001133 * is smaller than 20 tar blocks, and pad it if it was smaller,
Erik Andersen0817d132000-04-09 15:17:40 +00001134 * but that isn't necessary for GNU tar interoperability, and
1135 * so is considered a waste of space */
1136
Erik Andersen68a9ea42000-04-04 18:39:50 +00001137 /* Hang up the tools, close up shop, head home */
Erik Andersen6acaa402000-03-26 14:03:20 +00001138 close(tarFd);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001139 if (errorFlag == TRUE) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001140 error_msg("Error exit delayed from previous errors");
Eric Andersen3d957c82000-12-07 00:34:58 +00001141 freeHardLinkInfo(&tbInfo.hlInfoHead);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001142 return(FALSE);
1143 }
Eric Andersen3d957c82000-12-07 00:34:58 +00001144 freeHardLinkInfo(&tbInfo.hlInfoHead);
Erik Andersen6acaa402000-03-26 14:03:20 +00001145 return( TRUE);
1146}
1147
1148
1149#endif
1150