blob: 7e56fb99ab848f77eeeafdf74bdbadde41e3030e [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002/*
Erik Andersen68a9ea42000-04-04 18:39:50 +00003 * Mini tar implementation for busybox
Erik Andersen6acaa402000-03-26 14:03:20 +00004 *
Erik Andersen61677fe2000-04-13 01:18:56 +00005 * Note, that as of BusyBox-0.43, tar has been completely rewritten from the
6 * ground up. It still has remnents of the old code lying about, but it is
7 * very different now (i.e. cleaner, less global variables, etc)
Eric Andersenc4996011999-10-20 22:08:37 +00008 *
Erik Andersen68a9ea42000-04-04 18:39:50 +00009 * Copyright (C) 2000 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 Andersen3570a342000-09-25 21:45:58 +000039#include "busybox.h"
Erik Andersen1ad302a2000-03-24 00:54:46 +000040#define BB_DECLARE_EXTERN
41#define bb_need_io_error
Eric Andersen3c5ee9a2000-11-14 22:15:48 +000042#define bb_need_name_longer_than_foo
Erik Andersen1ad302a2000-03-24 00:54:46 +000043#include "messages.c"
Eric Andersencc8ed391999-10-05 16:24:54 +000044#include <stdio.h>
45#include <dirent.h>
46#include <errno.h>
47#include <fcntl.h>
48#include <signal.h>
49#include <time.h>
Erik Andersen7dc16072000-01-04 01:10:25 +000050#include <utime.h>
Eric Andersen96bcfd31999-11-12 01:30:18 +000051#include <sys/types.h>
Eric Andersen08b10341999-11-19 02:38:58 +000052#include <sys/sysmacros.h>
Matt Kraai43c8c382000-09-04 16:51:55 +000053#include <getopt.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
Eric Andersen1b1cfde2000-09-24 00:54:37 +000061#define NAME_SIZE 100
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 */
Erik Andersen298854f2000-03-23 01:09:18 +000091#define TAR_MAGIC_LEN 6
92#define TAR_VERSION_LEN 2
Erik Andersen298854f2000-03-23 01:09:18 +000093#define TAR_BLOCK_SIZE 512
94
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 */
124 enum TarFileType type; /* Regular, directory, link, etc */
125 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. */
Erik Andersen298854f2000-03-23 01:09:18 +0000132static int readTarFile(const char* tarName, 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
136
Eric Andersencc8ed391999-10-05 16:24:54 +0000137
Erik Andersen05100cd2000-01-16 01:30:52 +0000138#ifdef BB_FEATURE_TAR_CREATE
Erik Andersen6acaa402000-03-26 14:03:20 +0000139/* Local procedures to save files into a tar file. */
Matt Kraaid8ad76c2000-11-08 02:35:47 +0000140static int writeTarFile(const char* tarName, int verboseFlag, char **argv,
141 char** excludeList);
Erik Andersen05100cd2000-01-16 01:30:52 +0000142#endif
143
Erik Andersene49d5ec2000-02-08 19:58:47 +0000144extern int tar_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000145{
Erik Andersenecd51242000-04-08 03:08:21 +0000146 char** excludeList=NULL;
Matt Kraaic119ab92000-11-30 04:44:54 +0000147 char** extractList=NULL;
Erik Andersen0817d132000-04-09 15:17:40 +0000148#if defined BB_FEATURE_TAR_EXCLUDE
Erik Andersenecd51242000-04-08 03:08:21 +0000149 int excludeListSize=0;
Eric Andersen8cede002000-12-04 18:51:09 +0000150 char *excludeFileName ="-";
151 FILE *fileList;
152 char file[256];
Erik Andersen0817d132000-04-09 15:17:40 +0000153#endif
Eric Andersen108a4432000-05-30 19:05:57 +0000154 const char *tarName="-";
Erik Andersen298854f2000-03-23 01:09:18 +0000155 int listFlag = FALSE;
156 int extractFlag = FALSE;
157 int createFlag = FALSE;
158 int verboseFlag = FALSE;
159 int tostdoutFlag = FALSE;
Eric Andersen02f3b2e2000-12-01 19:04:52 +0000160 int status = FALSE;
161 int firstOpt = TRUE;
Eric Andersen46a98df2000-09-19 21:35:14 +0000162 int stopIt;
163
Eric Andersencc8ed391999-10-05 16:24:54 +0000164
Erik Andersenecd51242000-04-08 03:08:21 +0000165 if (argc <= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000166 usage(tar_usage);
Eric Andersencc8ed391999-10-05 16:24:54 +0000167
Matt Kraaid8ad76c2000-11-08 02:35:47 +0000168 while (*(++argv) && (**argv == '-' || firstOpt == TRUE)) {
Eric Andersen0102a9f2000-09-23 22:36:24 +0000169 firstOpt=FALSE;
Eric Andersen46a98df2000-09-19 21:35:14 +0000170 stopIt=FALSE;
Matt Kraaid8ad76c2000-11-08 02:35:47 +0000171 while (stopIt==FALSE && **argv) {
172 switch (*((*argv)++)) {
Erik Andersenecd51242000-04-08 03:08:21 +0000173 case 'c':
174 if (extractFlag == TRUE || listFlag == TRUE)
175 goto flagError;
176 createFlag = TRUE;
177 break;
Eric Andersenfdd51032000-08-02 18:48:26 +0000178 case 'x':
179 if (listFlag == TRUE || createFlag == TRUE)
180 goto flagError;
181 extractFlag = TRUE;
182 break;
183 case 't':
184 if (extractFlag == TRUE || createFlag == TRUE)
185 goto flagError;
186 listFlag = TRUE;
187 break;
Erik Andersenecd51242000-04-08 03:08:21 +0000188 case 'v':
189 verboseFlag = TRUE;
190 break;
Erik Andersenecd51242000-04-08 03:08:21 +0000191 case 'O':
192 tostdoutFlag = TRUE;
Eric Andersenfdd51032000-08-02 18:48:26 +0000193 break;
194 case 'f':
195 if (*tarName != '-')
196 fatalError( "Only one 'f' option allowed\n");
Eric Andersen46a98df2000-09-19 21:35:14 +0000197 tarName = *(++argv);
198 if (tarName == NULL)
199 fatalError( "Option requires an argument: No file specified\n");
Eric Andersen46a98df2000-09-19 21:35:14 +0000200 stopIt=TRUE;
Erik Andersenecd51242000-04-08 03:08:21 +0000201 break;
Erik Andersen0817d132000-04-09 15:17:40 +0000202#if defined BB_FEATURE_TAR_EXCLUDE
Matt Kraai43c8c382000-09-04 16:51:55 +0000203 case 'e':
Eric Andersen8cede002000-12-04 18:51:09 +0000204 if (strcmp(*argv, "xclude")==0) {
Eric Andersen46a98df2000-09-19 21:35:14 +0000205 excludeList=xrealloc( excludeList, sizeof(char**) * (excludeListSize+2));
206 excludeList[excludeListSize] = *(++argv);
Matt Kraaid8ad76c2000-11-08 02:35:47 +0000207 if (excludeList[excludeListSize] == NULL)
208 fatalError( "Option requires an argument: No file specified\n");
Eric Andersen46a98df2000-09-19 21:35:14 +0000209 /* Remove leading "/"s */
210 if (*excludeList[excludeListSize] =='/')
211 excludeList[excludeListSize] = (excludeList[excludeListSize])+1;
212 /* Tack a NULL onto the end of the list */
213 excludeList[++excludeListSize] = NULL;
214 stopIt=TRUE;
215 break;
216 }
Eric Andersen8cede002000-12-04 18:51:09 +0000217 case 'X':
218 if (*excludeFileName != '-')
219 fatalError("Only one 'X' option allowed\n");
220 excludeFileName = *(++argv);
221 if (excludeFileName == NULL)
222 fatalError("Option requires an argument: No file specified\n");
223 fileList = fopen (excludeFileName, "rt");
224 if (! fileList)
225 fatalError("Exclude file: file not found\n");
226 while (!feof(fileList)) {
227 fscanf(fileList, "%s", file);
228 excludeList=xrealloc( excludeList, sizeof(char**) * (excludeListSize+2));
229 excludeList[excludeListSize] = malloc(sizeof(char) * (strlen(file)+1));
230 strcpy(excludeList[excludeListSize],file);
231 /* Remove leading "/"s */
232 if (*excludeList[excludeListSize] == '/')
233 excludeList[excludeListSize] = (excludeList[excludeListSize])+1;
234 /* Tack a NULL onto the end of the list */
235 excludeList[++excludeListSize] = NULL;
236 }
237
238 fclose(fileList);
239 stopIt=TRUE;
240 break;
Erik Andersen0817d132000-04-09 15:17:40 +0000241#endif
Eric Andersen46a98df2000-09-19 21:35:14 +0000242 case '-':
243 break;
Erik Andersenecd51242000-04-08 03:08:21 +0000244 default:
Matt Kraai43c8c382000-09-04 16:51:55 +0000245 usage(tar_usage);
Eric Andersen46a98df2000-09-19 21:35:14 +0000246 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000247 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000248 }
249
Erik Andersene49d5ec2000-02-08 19:58:47 +0000250 /*
251 * Do the correct type of action supplying the rest of the
252 * command line arguments as the list of files to process.
253 */
254 if (createFlag == TRUE) {
Erik Andersende552872000-01-23 01:34:05 +0000255#ifndef BB_FEATURE_TAR_CREATE
Erik Andersen298854f2000-03-23 01:09:18 +0000256 fatalError( "This version of tar was not compiled with tar creation support.\n");
Erik Andersende552872000-01-23 01:34:05 +0000257#else
Matt Kraai3e856ce2000-12-01 02:55:13 +0000258 status = writeTarFile(tarName, verboseFlag, argv, excludeList);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000259#endif
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000260 }
261 if (listFlag == TRUE || extractFlag == TRUE) {
Matt Kraaic119ab92000-11-30 04:44:54 +0000262 if (*argv)
263 extractList = argv;
Matt Kraai3e856ce2000-12-01 02:55:13 +0000264 status = readTarFile(tarName, extractFlag, listFlag, tostdoutFlag,
265 verboseFlag, extractList, excludeList);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000266 }
Erik Andersende552872000-01-23 01:34:05 +0000267
Matt Kraai3e856ce2000-12-01 02:55:13 +0000268 if (status == TRUE)
269 return EXIT_SUCCESS;
270 else
271 return EXIT_FAILURE;
272
Erik Andersene49d5ec2000-02-08 19:58:47 +0000273 flagError:
Erik Andersen298854f2000-03-23 01:09:18 +0000274 fatalError( "Exactly one of 'c', 'x' or 't' must be specified\n");
275}
276
277static void
Erik Andersen6a34b532000-04-07 06:55:38 +0000278fixUpPermissions(TarInfo *header)
279{
280 struct utimbuf t;
281 /* Now set permissions etc for the new file */
282 chown(header->name, header->uid, header->gid);
283 chmod(header->name, header->mode);
284 /* Reset the time */
285 t.actime = time(0);
286 t.modtime = header->mtime;
287 utime(header->name, &t);
288}
289
290static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000291tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersen298854f2000-03-23 01:09:18 +0000292{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000293 size_t writeSize;
294 size_t readSize;
295 size_t actualWriteSz;
296 char buffer[BUFSIZ];
297 size_t size = header->size;
298 int outFd=fileno(stdout);
299
300 /* Open the file to be written, if a file is supposed to be written */
301 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000302 /* Create the path to the file, just in case it isn't there...
303 * This should not screw up path permissions or anything. */
304 createPath(header->name, 0777);
Eric Andersen0c6a9702000-06-09 20:51:50 +0000305 if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY,
306 header->mode & ~S_IFMT)) < 0) {
307 errorMsg(io_error, header->name, strerror(errno));
308 return( FALSE);
309 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000310 }
311
312 /* Write out the file, if we are supposed to be doing that */
313 while ( size > 0 ) {
314 actualWriteSz=0;
315 if ( size > sizeof(buffer) )
316 writeSize = readSize = sizeof(buffer);
317 else {
318 int mod = size % 512;
319 if ( mod != 0 )
320 readSize = size + (512 - mod);
321 else
322 readSize = size;
323 writeSize = size;
324 }
325 if ( (readSize = fullRead(header->tarFd, buffer, readSize)) <= 0 ) {
326 /* Tarball seems to have a problem */
Matt Kraaibe84cd42000-07-12 17:02:35 +0000327 errorMsg("Unexpected EOF in archive\n");
Erik Andersen6a34b532000-04-07 06:55:38 +0000328 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000329 }
330 if ( readSize < writeSize )
331 writeSize = readSize;
332
333 /* Write out the file, if we are supposed to be doing that */
334 if (extractFlag==TRUE) {
335
336 if ((actualWriteSz=fullWrite(outFd, buffer, writeSize)) != writeSize ) {
337 /* Output file seems to have a problem */
338 errorMsg(io_error, header->name, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000339 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000340 }
Erik Andersen0817d132000-04-09 15:17:40 +0000341 } else {
342 actualWriteSz=writeSize;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000343 }
344
345 size -= actualWriteSz;
346 }
347
348 /* Now we are done writing the file out, so try
349 * and fix up the permissions and whatnot */
350 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000351 close(outFd);
Erik Andersen6a34b532000-04-07 06:55:38 +0000352 fixUpPermissions(header);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000353 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000354 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000355}
356
Erik Andersen6a34b532000-04-07 06:55:38 +0000357static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000358tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000359{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000360
361 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000362 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000363
364 if (createPath(header->name, header->mode) != TRUE) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000365 errorMsg("%s: Cannot mkdir: %s\n",
Erik Andersen6a34b532000-04-07 06:55:38 +0000366 header->name, strerror(errno));
367 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000368 }
369 /* make the final component, just in case it was
370 * omitted by createPath() (which will skip the
371 * directory if it doesn't have a terminating '/') */
Erik Andersen6acaa402000-03-26 14:03:20 +0000372 if (mkdir(header->name, header->mode) == 0) {
373 fixUpPermissions(header);
374 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000375 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000376}
377
Erik Andersen6a34b532000-04-07 06:55:38 +0000378static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000379tarExtractHardLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000380{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000381 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000382 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000383
384 if (link(header->linkname, header->name) < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000385 errorMsg("%s: Cannot create hard link to '%s': %s\n",
Erik Andersen5661fe02000-04-05 01:00:52 +0000386 header->name, header->linkname, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000387 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000388 }
389
390 /* Now set permissions etc for the new directory */
391 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000392 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000393}
394
Erik Andersen6a34b532000-04-07 06:55:38 +0000395static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000396tarExtractSymLink(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000397{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000398 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000399 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000400
401#ifdef S_ISLNK
402 if (symlink(header->linkname, header->name) < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000403 errorMsg("%s: Cannot create symlink to '%s': %s\n",
Erik Andersen5661fe02000-04-05 01:00:52 +0000404 header->name, header->linkname, strerror(errno));
Erik Andersen6a34b532000-04-07 06:55:38 +0000405 return( FALSE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000406 }
407 /* Try to change ownership of the symlink.
408 * If libs doesn't support that, don't bother.
409 * Changing the pointed-to-file is the Wrong Thing(tm).
410 */
411#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
412 lchown(header->name, header->uid, header->gid);
413#endif
414
415 /* Do not change permissions or date on symlink,
416 * since it changes the pointed to file instead. duh. */
417#else
Matt Kraaibe84cd42000-07-12 17:02:35 +0000418 errorMsg("%s: Cannot create symlink to '%s': %s\n",
Erik Andersen6a34b532000-04-07 06:55:38 +0000419 header->name, header->linkname,
420 "symlinks not supported");
Erik Andersen1ad302a2000-03-24 00:54:46 +0000421#endif
Erik Andersen6a34b532000-04-07 06:55:38 +0000422 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000423}
424
Erik Andersen6a34b532000-04-07 06:55:38 +0000425static int
Erik Andersen1ad302a2000-03-24 00:54:46 +0000426tarExtractSpecial(TarInfo *header, int extractFlag, int tostdoutFlag)
Erik Andersene454fb62000-03-23 04:27:58 +0000427{
Erik Andersen1ad302a2000-03-24 00:54:46 +0000428 if (extractFlag==FALSE || tostdoutFlag==TRUE)
Erik Andersen6a34b532000-04-07 06:55:38 +0000429 return( TRUE);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000430
431 if (S_ISCHR(header->mode) || S_ISBLK(header->mode) || S_ISSOCK(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000432 if (mknod(header->name, header->mode, makedev(header->devmajor, header->devminor)) < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000433 errorMsg("%s: Cannot mknod: %s\n",
Erik Andersen6a34b532000-04-07 06:55:38 +0000434 header->name, strerror(errno));
435 return( FALSE);
436 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000437 } else if (S_ISFIFO(header->mode)) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000438 if (mkfifo(header->name, header->mode) < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000439 errorMsg("%s: Cannot mkfifo: %s\n",
Erik Andersen6a34b532000-04-07 06:55:38 +0000440 header->name, strerror(errno));
441 return( FALSE);
442 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000443 }
444
445 /* Now set permissions etc for the new directory */
446 fixUpPermissions(header);
Erik Andersen6a34b532000-04-07 06:55:38 +0000447 return( TRUE);
Erik Andersene454fb62000-03-23 04:27:58 +0000448}
449
Erik Andersen1ad302a2000-03-24 00:54:46 +0000450/* Read an octal value in a field of the specified width, with optional
Erik Andersen298854f2000-03-23 01:09:18 +0000451 * spaces on both sides of the number and with an optional null character
Erik Andersen1ad302a2000-03-24 00:54:46 +0000452 * at the end. Returns -1 on an illegal format. */
Erik Andersen298854f2000-03-23 01:09:18 +0000453static long getOctal(const char *cp, int size)
Eric Andersencc8ed391999-10-05 16:24:54 +0000454{
Erik Andersen298854f2000-03-23 01:09:18 +0000455 long val = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000456
Erik Andersen298854f2000-03-23 01:09:18 +0000457 for(;(size > 0) && (*cp == ' '); cp++, size--);
458 if ((size == 0) || !isOctal(*cp))
459 return -1;
460 for(; (size > 0) && isOctal(*cp); size--) {
461 val = val * 8 + *cp++ - '0';
Eric Andersencc8ed391999-10-05 16:24:54 +0000462 }
Erik Andersen298854f2000-03-23 01:09:18 +0000463 for (;(size > 0) && (*cp == ' '); cp++, size--);
464 if ((size > 0) && *cp)
465 return -1;
466 return val;
467}
Eric Andersencc8ed391999-10-05 16:24:54 +0000468
Erik Andersen1ad302a2000-03-24 00:54:46 +0000469
Erik Andersen298854f2000-03-23 01:09:18 +0000470/* Parse the tar header and fill in the nice struct with the details */
471static int
Erik Andersen3364d782000-03-28 00:58:14 +0000472readTarHeader(struct TarHeader *rawHeader, struct TarInfo *header)
Erik Andersen298854f2000-03-23 01:09:18 +0000473{
Erik Andersene454fb62000-03-23 04:27:58 +0000474 int i;
Erik Andersen84e09e42000-04-08 20:58:35 +0000475 long chksum, sum=0;
Erik Andersene454fb62000-03-23 04:27:58 +0000476 unsigned char *s = (unsigned char *)rawHeader;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000477
Erik Andersen298854f2000-03-23 01:09:18 +0000478 header->name = rawHeader->name;
Erik Andersen84e09e42000-04-08 20:58:35 +0000479 /* Check for and relativify any absolute paths */
480 if ( *(header->name) == '/' ) {
481 static int alreadyWarned=FALSE;
482
483 while (*(header->name) == '/')
484 ++*(header->name);
485
486 if (alreadyWarned == FALSE) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000487 errorMsg("Removing leading '/' from member names\n");
Erik Andersen84e09e42000-04-08 20:58:35 +0000488 alreadyWarned = TRUE;
489 }
490 }
491
Erik Andersen298854f2000-03-23 01:09:18 +0000492 header->mode = getOctal(rawHeader->mode, sizeof(rawHeader->mode));
493 header->uid = getOctal(rawHeader->uid, sizeof(rawHeader->uid));
494 header->gid = getOctal(rawHeader->gid, sizeof(rawHeader->gid));
495 header->size = getOctal(rawHeader->size, sizeof(rawHeader->size));
496 header->mtime = getOctal(rawHeader->mtime, sizeof(rawHeader->mtime));
497 chksum = getOctal(rawHeader->chksum, sizeof(rawHeader->chksum));
498 header->type = rawHeader->typeflag;
499 header->linkname = rawHeader->linkname;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000500 header->devmajor = getOctal(rawHeader->devmajor, sizeof(rawHeader->devmajor));
501 header->devminor = getOctal(rawHeader->devminor, sizeof(rawHeader->devminor));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000502
Erik Andersen298854f2000-03-23 01:09:18 +0000503 /* Check the checksum */
Erik Andersen84e09e42000-04-08 20:58:35 +0000504 for (i = sizeof(*rawHeader); i-- != 0;) {
Erik Andersen298854f2000-03-23 01:09:18 +0000505 sum += *s++;
Erik Andersen84e09e42000-04-08 20:58:35 +0000506 }
507 /* Remove the effects of the checksum field (replace
508 * with blanks for the purposes of the checksum) */
509 s = rawHeader->chksum;
510 for (i = sizeof(rawHeader->chksum) ; i-- != 0;) {
511 sum -= *s++;
512 }
513 sum += ' ' * sizeof(rawHeader->chksum);
Erik Andersene454fb62000-03-23 04:27:58 +0000514 if (sum == chksum )
Erik Andersen298854f2000-03-23 01:09:18 +0000515 return ( TRUE);
516 return( FALSE);
517}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000518
Erik Andersene49d5ec2000-02-08 19:58:47 +0000519
Erik Andersen1ad302a2000-03-24 00:54:46 +0000520/*
521 * Read a tar file and extract or list the specified files within it.
522 * If the list is empty than all files are extracted or listed.
523 */
524static int readTarFile(const char* tarName, int extractFlag, int listFlag,
Matt Kraaib92223b2000-09-04 08:25:42 +0000525 int tostdoutFlag, int verboseFlag, char** extractList,
526 char** excludeList)
Erik Andersen1ad302a2000-03-24 00:54:46 +0000527{
Erik Andersen0817d132000-04-09 15:17:40 +0000528 int status, tarFd=-1;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000529 int errorFlag=FALSE;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000530 int skipNextHeaderFlag=FALSE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000531 TarHeader rawHeader;
532 TarInfo header;
Erik Andersen0817d132000-04-09 15:17:40 +0000533 char** tmpList;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000534
Erik Andersen1ad302a2000-03-24 00:54:46 +0000535 /* Open the tar file for reading. */
536 if (!strcmp(tarName, "-"))
537 tarFd = fileno(stdin);
538 else
539 tarFd = open(tarName, O_RDONLY);
540 if (tarFd < 0) {
Erik Andersendeea0482000-03-25 23:09:29 +0000541 errorMsg( "Error opening '%s': %s\n", tarName, strerror(errno));
Erik Andersen1ad302a2000-03-24 00:54:46 +0000542 return ( FALSE);
Erik Andersen06936df2000-01-23 02:14:20 +0000543 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000544
Erik Andersene49d5ec2000-02-08 19:58:47 +0000545 /* Set the umask for this process so it doesn't
Erik Andersen1ad302a2000-03-24 00:54:46 +0000546 * screw up permission setting for us later. */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000547 umask(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000548
Erik Andersen1ad302a2000-03-24 00:54:46 +0000549 /* Read the tar file, and iterate over it one file at a time */
550 while ( (status = fullRead(tarFd, (char*)&rawHeader, TAR_BLOCK_SIZE)) == TAR_BLOCK_SIZE ) {
Erik Andersene2729152000-02-18 21:34:17 +0000551
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000552 /* Try to read the header */
Erik Andersen3364d782000-03-28 00:58:14 +0000553 if ( readTarHeader(&rawHeader, &header) == FALSE ) {
Erik Andersen1ad302a2000-03-24 00:54:46 +0000554 if ( *(header.name) == '\0' ) {
555 goto endgame;
556 } else {
557 errorFlag=TRUE;
558 errorMsg("Bad tar header, skipping\n");
559 continue;
560 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000561 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000562 if ( *(header.name) == '\0' )
563 goto endgame;
Erik Andersen0817d132000-04-09 15:17:40 +0000564 header.tarFd = tarFd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000565
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000566 /* Skip funky extra GNU headers that precede long files */
567 if ( (header.type == GNULONGNAME) || (header.type == GNULONGLINK) ) {
568 skipNextHeaderFlag=TRUE;
Matt Kraaiab8f9e22000-11-18 01:28:57 +0000569 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
570 errorFlag = TRUE;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000571 continue;
572 }
573 if ( skipNextHeaderFlag == TRUE ) {
574 skipNextHeaderFlag=FALSE;
Eric Andersen3c5ee9a2000-11-14 22:15:48 +0000575 errorMsg(name_longer_than_foo, NAME_SIZE);
Matt Kraaiab8f9e22000-11-18 01:28:57 +0000576 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
577 errorFlag = TRUE;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000578 continue;
579 }
580
Erik Andersen0817d132000-04-09 15:17:40 +0000581#if defined BB_FEATURE_TAR_EXCLUDE
582 {
583 int skipFlag=FALSE;
584 /* Check for excluded files.... */
585 for (tmpList=excludeList; tmpList && *tmpList; tmpList++) {
586 /* Do some extra hoop jumping for when directory names
587 * end in '/' but the entry in tmpList doesn't */
588 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
589 header.name[strlen(header.name)-1]=='/'
590 && strncmp( *tmpList, header.name,
591 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
592 /* If it is a regular file, pretend to extract it with
593 * the extractFlag set to FALSE, so the junk in the tarball
594 * is properly skipped over */
595 if ( header.type==REGTYPE || header.type==REGTYPE0 ) {
Matt Kraaiab8f9e22000-11-18 01:28:57 +0000596 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
597 errorFlag = TRUE;
Erik Andersen0817d132000-04-09 15:17:40 +0000598 }
599 skipFlag=TRUE;
600 break;
601 }
602 }
603 /* There are not the droids you're looking for, move along */
604 if (skipFlag==TRUE)
605 continue;
606 }
607#endif
Matt Kraaic119ab92000-11-30 04:44:54 +0000608 if (extractList != NULL) {
Matt Kraaib92223b2000-09-04 08:25:42 +0000609 int skipFlag = TRUE;
610 for (tmpList = extractList; *tmpList != NULL; tmpList++) {
611 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
612 header.name[strlen(header.name)-1]=='/'
613 && strncmp( *tmpList, header.name,
614 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
615 /* If it is a regular file, pretend to extract it with
616 * the extractFlag set to FALSE, so the junk in the tarball
617 * is properly skipped over */
618 skipFlag = FALSE;
Matt Kraaic119ab92000-11-30 04:44:54 +0000619 memmove(extractList+1, extractList,
620 sizeof(*extractList)*(tmpList-extractList));
621 extractList++;
Matt Kraaib92223b2000-09-04 08:25:42 +0000622 break;
623 }
624 }
625 /* There are not the droids you're looking for, move along */
626 if (skipFlag == TRUE) {
627 if ( header.type==REGTYPE || header.type==REGTYPE0 )
Matt Kraaiab8f9e22000-11-18 01:28:57 +0000628 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
629 errorFlag = TRUE;
Matt Kraaib92223b2000-09-04 08:25:42 +0000630 continue;
631 }
632 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000633
Matt Kraai82cfbad2000-09-15 21:18:43 +0000634 if (listFlag == TRUE) {
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000635 /* Special treatment if the list (-t) flag is on */
636 if (verboseFlag == TRUE) {
637 int len, len1;
638 char buf[35];
639 struct tm *tm = localtime (&(header.mtime));
640
641 len=printf("%s ", modeString(header.mode));
642 memset(buf, 0, 8*sizeof(char));
643 my_getpwuid(buf, header.uid);
644 if (! *buf)
645 len+=printf("%d", header.uid);
646 else
647 len+=printf("%s", buf);
648 memset(buf, 0, 8*sizeof(char));
649 my_getgrgid(buf, header.gid);
650 if (! *buf)
651 len+=printf("/%-d ", header.gid);
652 else
653 len+=printf("/%-s ", buf);
654
655 if (header.type==CHRTYPE || header.type==BLKTYPE) {
656 len1=snprintf(buf, sizeof(buf), "%ld,%-ld ",
657 header.devmajor, header.devminor);
658 } else {
659 len1=snprintf(buf, sizeof(buf), "%lu ", (long)header.size);
660 }
661 /* Jump through some hoops to make the columns match up */
662 for(;(len+len1)<31;len++)
663 printf(" ");
664 printf(buf);
665
666 /* Use ISO 8610 time format */
667 if (tm) {
668 printf ("%04d-%02d-%02d %02d:%02d:%02d ",
669 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
670 tm->tm_hour, tm->tm_min, tm->tm_sec);
671 }
672 }
Eric Andersenfdd51032000-08-02 18:48:26 +0000673 printf("%s", header.name);
Matt Kraai82cfbad2000-09-15 21:18:43 +0000674 if (verboseFlag == TRUE) {
675 if (header.type==LNKTYPE) /* If this is a link, say so */
676 printf(" link to %s", header.linkname);
677 else if (header.type==SYMTYPE)
678 printf(" -> %s", header.linkname);
679 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000680 printf("\n");
681 }
682
Matt Kraai6fc2a7d2000-09-15 22:23:41 +0000683 /* List contents if we are supposed to do that */
684 if (verboseFlag == TRUE && extractFlag == TRUE) {
685 /* Now the normal listing */
686 FILE *vbFd = stdout;
687 if (tostdoutFlag == TRUE) // If the archive goes to stdout, verbose to stderr
688 vbFd = stderr;
689 fprintf(vbFd, "%s\n", header.name);
690 }
691
Matt Kraaif4462972000-09-01 02:50:48 +0000692 /* Remove files if we would overwrite them */
Matt Kraaida542f32000-09-01 02:53:01 +0000693 if (extractFlag == TRUE && tostdoutFlag == FALSE)
Matt Kraaif4462972000-09-01 02:50:48 +0000694 unlink(header.name);
Eric Andersencc8ed391999-10-05 16:24:54 +0000695
Erik Andersen1ad302a2000-03-24 00:54:46 +0000696 /* If we got here, we can be certain we have a legitimate
697 * header to work with. So work with it. */
698 switch ( header.type ) {
699 case REGTYPE:
700 case REGTYPE0:
701 /* If the name ends in a '/' then assume it is
702 * supposed to be a directory, and fall through */
703 if (header.name[strlen(header.name)-1] != '/') {
Erik Andersen6a34b532000-04-07 06:55:38 +0000704 if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE)
705 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000706 break;
707 }
708 case DIRTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000709 if (tarExtractDirectory( &header, extractFlag, tostdoutFlag)==FALSE)
710 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000711 break;
712 case LNKTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000713 if (tarExtractHardLink( &header, extractFlag, tostdoutFlag)==FALSE)
714 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000715 break;
716 case SYMTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000717 if (tarExtractSymLink( &header, extractFlag, tostdoutFlag)==FALSE)
718 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000719 break;
720 case CHRTYPE:
721 case BLKTYPE:
722 case FIFOTYPE:
Erik Andersen6a34b532000-04-07 06:55:38 +0000723 if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE)
724 errorFlag=TRUE;
Erik Andersen1ad302a2000-03-24 00:54:46 +0000725 break;
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000726#if 0
727 /* Handled earlier */
728 case GNULONGNAME:
729 case GNULONGLINK:
730 skipNextHeaderFlag=TRUE;
731 break;
732#endif
Erik Andersen1ad302a2000-03-24 00:54:46 +0000733 default:
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000734 errorMsg("Unknown file type '%c' in tar file\n", header.type);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000735 close( tarFd);
736 return( FALSE);
737 }
738 }
739 close(tarFd);
740 if (status > 0) {
741 /* Bummer - we read a partial header */
Erik Andersendeea0482000-03-25 23:09:29 +0000742 errorMsg( "Error reading '%s': %s\n", tarName, strerror(errno));
Erik Andersen1ad302a2000-03-24 00:54:46 +0000743 return ( FALSE);
744 }
Erik Andersen6a34b532000-04-07 06:55:38 +0000745 else if (errorFlag==TRUE) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000746 errorMsg( "Error exit delayed from previous errors\n");
Erik Andersen6a34b532000-04-07 06:55:38 +0000747 return( FALSE);
748 } else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000749 return( status);
750
751 /* Stuff to do when we are done */
752endgame:
753 close( tarFd);
Matt Kraaic119ab92000-11-30 04:44:54 +0000754 if (extractList != NULL) {
755 for (; *extractList != NULL; extractList++) {
756 errorMsg("%s: Not found in archive\n", *extractList);
757 errorFlag = TRUE;
758 }
759 }
Erik Andersen1ad302a2000-03-24 00:54:46 +0000760 if ( *(header.name) == '\0' ) {
Erik Andersen6a34b532000-04-07 06:55:38 +0000761 if (errorFlag==TRUE)
Matt Kraaibe84cd42000-07-12 17:02:35 +0000762 errorMsg( "Error exit delayed from previous errors\n");
Erik Andersen6a34b532000-04-07 06:55:38 +0000763 else
Erik Andersen1ad302a2000-03-24 00:54:46 +0000764 return( TRUE);
765 }
766 return( FALSE);
767}
768
Erik Andersen6acaa402000-03-26 14:03:20 +0000769
770#ifdef BB_FEATURE_TAR_CREATE
771
Erik Andersen68a9ea42000-04-04 18:39:50 +0000772/* Some info to be carried along when creating a new tarball */
773struct TarBallInfo
774{
775 char* fileName; /* File name of the tarball */
776 int tarFd; /* Open-for-write file descriptor
777 for the tarball */
778 struct stat statBuf; /* Stat info for the tarball, letting
779 us know the inode and device that the
780 tarball lives, so we can avoid trying
781 to include the tarball into itself */
782 int verboseFlag; /* Whether to print extra stuff or not */
Erik Andersenecd51242000-04-08 03:08:21 +0000783 char** excludeList; /* List of files to not include */
Erik Andersen68a9ea42000-04-04 18:39:50 +0000784};
785typedef struct TarBallInfo TarBallInfo;
786
787
Erik Andersen6acaa402000-03-26 14:03:20 +0000788/* Put an octal string into the specified buffer.
789 * The number is zero and space padded and possibly null padded.
790 * Returns TRUE if successful. */
791static int putOctal (char *cp, int len, long value)
792{
793 int tempLength;
Erik Andersen6acaa402000-03-26 14:03:20 +0000794 char tempBuffer[32];
Erik Andersen5661fe02000-04-05 01:00:52 +0000795 char *tempString = tempBuffer;
Erik Andersen6acaa402000-03-26 14:03:20 +0000796
797 /* Create a string of the specified length with an initial space,
798 * leading zeroes and the octal number, and a trailing null. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000799 sprintf (tempString, "%0*lo", len - 1, value);
Erik Andersen6acaa402000-03-26 14:03:20 +0000800
801 /* If the string is too large, suppress the leading space. */
Erik Andersen5661fe02000-04-05 01:00:52 +0000802 tempLength = strlen (tempString) + 1;
Erik Andersen6acaa402000-03-26 14:03:20 +0000803 if (tempLength > len) {
804 tempLength--;
805 tempString++;
806 }
807
808 /* If the string is still too large, suppress the trailing null. */
809 if (tempLength > len)
810 tempLength--;
811
812 /* If the string is still too large, fail. */
813 if (tempLength > len)
814 return FALSE;
815
816 /* Copy the string to the field. */
817 memcpy (cp, tempString, len);
818
819 return TRUE;
820}
821
Erik Andersen68a9ea42000-04-04 18:39:50 +0000822/* Write out a tar header for the specified file/directory/whatever */
Erik Andersen3364d782000-03-28 00:58:14 +0000823static int
Erik Andersen5661fe02000-04-05 01:00:52 +0000824writeTarHeader(struct TarBallInfo *tbInfo, const char *fileName, struct stat *statbuf)
Erik Andersen6acaa402000-03-26 14:03:20 +0000825{
Erik Andersen5661fe02000-04-05 01:00:52 +0000826 long chksum=0;
827 struct TarHeader header;
Erik Andersen0817d132000-04-09 15:17:40 +0000828#if defined BB_FEATURE_TAR_EXCLUDE
Erik Andersenecd51242000-04-08 03:08:21 +0000829 char** tmpList;
Erik Andersen0817d132000-04-09 15:17:40 +0000830#endif
Erik Andersen5661fe02000-04-05 01:00:52 +0000831 const unsigned char *cp = (const unsigned char *) &header;
832 ssize_t size = sizeof(struct TarHeader);
Eric Andersenfdd51032000-08-02 18:48:26 +0000833
Erik Andersen5661fe02000-04-05 01:00:52 +0000834 memset( &header, 0, size);
Erik Andersen3364d782000-03-28 00:58:14 +0000835
Erik Andersen68a9ea42000-04-04 18:39:50 +0000836 if (*fileName=='/') {
837 static int alreadyWarned=FALSE;
838 if (alreadyWarned==FALSE) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000839 errorMsg("Removing leading '/' from member names\n");
Erik Andersen68a9ea42000-04-04 18:39:50 +0000840 alreadyWarned=TRUE;
841 }
Erik Andersen84e09e42000-04-08 20:58:35 +0000842 strncpy(header.name, fileName+1, sizeof(header.name));
Erik Andersen3364d782000-03-28 00:58:14 +0000843 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000844 else {
Erik Andersen84e09e42000-04-08 20:58:35 +0000845 strncpy(header.name, fileName, sizeof(header.name));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000846 }
Erik Andersen84e09e42000-04-08 20:58:35 +0000847
Erik Andersen0817d132000-04-09 15:17:40 +0000848#if defined BB_FEATURE_TAR_EXCLUDE
849 /* Check for excluded files.... */
Erik Andersenecd51242000-04-08 03:08:21 +0000850 for (tmpList=tbInfo->excludeList; tmpList && *tmpList; tmpList++) {
Erik Andersen0817d132000-04-09 15:17:40 +0000851 /* Do some extra hoop jumping for when directory names
852 * end in '/' but the entry in tmpList doesn't */
853 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
854 header.name[strlen(header.name)-1]=='/'
855 && strncmp( *tmpList, header.name,
856 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
857 /* Set the mode to something that is not a regular file, thereby
858 * faking out writeTarFile into thinking that nothing further need
859 * be done for this file. Yes, I know this is ugly, but it works. */
860 statbuf->st_mode = 0;
861 return( TRUE);
862 }
Erik Andersenecd51242000-04-08 03:08:21 +0000863 }
Erik Andersen0817d132000-04-09 15:17:40 +0000864#endif
Erik Andersenecd51242000-04-08 03:08:21 +0000865
Erik Andersen5661fe02000-04-05 01:00:52 +0000866 putOctal(header.mode, sizeof(header.mode), statbuf->st_mode);
867 putOctal(header.uid, sizeof(header.uid), statbuf->st_uid);
868 putOctal(header.gid, sizeof(header.gid), statbuf->st_gid);
869 putOctal(header.size, sizeof(header.size), 0); /* Regular file size is handled later */
870 putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime);
871 strncpy(header.magic, TAR_MAGIC TAR_VERSION,
872 TAR_MAGIC_LEN + TAR_VERSION_LEN );
Erik Andersen68a9ea42000-04-04 18:39:50 +0000873
Erik Andersen84e09e42000-04-08 20:58:35 +0000874 /* Enter the user and group names (default to root if it fails) */
Erik Andersen5661fe02000-04-05 01:00:52 +0000875 my_getpwuid(header.uname, statbuf->st_uid);
Erik Andersen5661fe02000-04-05 01:00:52 +0000876 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000877 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +0000878 my_getgrgid(header.gname, statbuf->st_gid);
879 if (! *header.uname)
Erik Andersen84e09e42000-04-08 20:58:35 +0000880 strcpy(header.uname, "root");
Erik Andersen5661fe02000-04-05 01:00:52 +0000881
Erik Andersen95c1c1e2000-04-14 21:45:29 +0000882 /* WARNING/NOTICE: I break Hard Links */
Erik Andersen68a9ea42000-04-04 18:39:50 +0000883 if (S_ISLNK(statbuf->st_mode)) {
Eric Andersen3adffb72000-06-26 10:54:06 +0000884 int link_size=0;
Erik Andersen5661fe02000-04-05 01:00:52 +0000885 char buffer[BUFSIZ];
886 header.typeflag = SYMTYPE;
Eric Andersen3adffb72000-06-26 10:54:06 +0000887 link_size = readlink(fileName, buffer, sizeof(buffer) - 1);
888 if ( link_size < 0) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000889 errorMsg("Error reading symlink '%s': %s\n", header.name, strerror(errno));
890 return ( FALSE);
891 }
Eric Andersen3adffb72000-06-26 10:54:06 +0000892 buffer[link_size] = '\0';
Erik Andersen95c1c1e2000-04-14 21:45:29 +0000893 strncpy(header.linkname, buffer, sizeof(header.linkname));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000894 } else if (S_ISDIR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000895 header.typeflag = DIRTYPE;
896 strncat(header.name, "/", sizeof(header.name));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000897 } else if (S_ISCHR(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000898 header.typeflag = CHRTYPE;
899 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
900 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000901 } else if (S_ISBLK(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000902 header.typeflag = BLKTYPE;
903 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
904 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
Erik Andersen68a9ea42000-04-04 18:39:50 +0000905 } else if (S_ISFIFO(statbuf->st_mode)) {
Erik Andersen5661fe02000-04-05 01:00:52 +0000906 header.typeflag = FIFOTYPE;
907 } else if (S_ISREG(statbuf->st_mode)) {
908 header.typeflag = REGTYPE;
909 putOctal(header.size, sizeof(header.size), statbuf->st_size);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000910 } else {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000911 errorMsg("%s: Unknown file type\n", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000912 return ( FALSE);
913 }
Erik Andersen68a9ea42000-04-04 18:39:50 +0000914
Erik Andersen5661fe02000-04-05 01:00:52 +0000915 /* Calculate and store the checksum (i.e. the sum of all of the bytes of
916 * the header). The checksum field must be filled with blanks for the
917 * calculation. The checksum field is formatted differently from the
918 * other fields: it has [6] digits, a null, then a space -- rather than
919 * digits, followed by a null like the other fields... */
920 memset(header.chksum, ' ', sizeof(header.chksum));
921 cp = (const unsigned char *) &header;
922 while (size-- > 0)
923 chksum += *cp++;
924 putOctal(header.chksum, 7, chksum);
925
926 /* Now write the header out to disk */
927 if ((size=fullWrite(tbInfo->tarFd, (char*)&header, sizeof(struct TarHeader))) < 0) {
928 errorMsg(io_error, fileName, strerror(errno));
929 return ( FALSE);
930 }
931 /* Pad the header up to the tar block size */
932 for (; size<TAR_BLOCK_SIZE; size++) {
933 write(tbInfo->tarFd, "\0", 1);
934 }
935 /* Now do the verbose thing (or not) */
Eric Andersenfdd51032000-08-02 18:48:26 +0000936 if (tbInfo->verboseFlag==TRUE) {
937 FILE *vbFd = stdout;
938 if (tbInfo->tarFd == fileno(stdout)) // If the archive goes to stdout, verbose to stderr
939 vbFd = stderr;
940 fprintf(vbFd, "%s\n", header.name);
941 }
Erik Andersen3364d782000-03-28 00:58:14 +0000942
943 return ( TRUE);
944}
945
946
Erik Andersen68a9ea42000-04-04 18:39:50 +0000947static int writeFileToTarball(const char *fileName, struct stat *statbuf, void* userData)
Erik Andersen3364d782000-03-28 00:58:14 +0000948{
Erik Andersen68a9ea42000-04-04 18:39:50 +0000949 struct TarBallInfo *tbInfo = (struct TarBallInfo *)userData;
Erik Andersen68a9ea42000-04-04 18:39:50 +0000950
951 /* It is against the rules to archive a socket */
952 if (S_ISSOCK(statbuf->st_mode)) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000953 errorMsg("%s: socket ignored\n", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000954 return( TRUE);
955 }
956
957 /* It is a bad idea to store the archive we are in the process of creating,
958 * so check the device and inode to be sure that this particular file isn't
959 * the new tarball */
960 if (tbInfo->statBuf.st_dev == statbuf->st_dev &&
961 tbInfo->statBuf.st_ino == statbuf->st_ino) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000962 errorMsg("%s: file is the archive; skipping\n", fileName);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000963 return( TRUE);
964 }
965
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000966 if (strlen(fileName) >= NAME_SIZE) {
Eric Andersen3c5ee9a2000-11-14 22:15:48 +0000967 errorMsg(name_longer_than_foo, NAME_SIZE);
Eric Andersen1b1cfde2000-09-24 00:54:37 +0000968 return ( TRUE);
969 }
970
Erik Andersen5661fe02000-04-05 01:00:52 +0000971 if (writeTarHeader(tbInfo, fileName, statbuf)==FALSE) {
972 return( FALSE);
Erik Andersen68a9ea42000-04-04 18:39:50 +0000973 }
Erik Andersen5661fe02000-04-05 01:00:52 +0000974
975 /* Now, if the file is a regular file, copy it out to the tarball */
976 if (S_ISREG(statbuf->st_mode)) {
977 int inputFileFd;
978 char buffer[BUFSIZ];
979 ssize_t size=0, readSize=0;
980
981 /* open the file we want to archive, and make sure all is well */
982 if ((inputFileFd = open(fileName, O_RDONLY)) < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000983 errorMsg("%s: Cannot open: %s\n", fileName, strerror(errno));
Erik Andersen5661fe02000-04-05 01:00:52 +0000984 return( FALSE);
985 }
986
987 /* write the file to the archive */
988 while ( (size = fullRead(inputFileFd, buffer, sizeof(buffer))) > 0 ) {
989 if (fullWrite(tbInfo->tarFd, buffer, size) != size ) {
990 /* Output file seems to have a problem */
991 errorMsg(io_error, fileName, strerror(errno));
992 return( FALSE);
993 }
994 readSize+=size;
995 }
996 if (size == -1) {
997 errorMsg(io_error, fileName, strerror(errno));
998 return( FALSE);
999 }
1000 /* Pad the file up to the tar block size */
1001 for (; (readSize%TAR_BLOCK_SIZE) != 0; readSize++) {
1002 write(tbInfo->tarFd, "\0", 1);
1003 }
1004 close( inputFileFd);
1005 }
Erik Andersen68a9ea42000-04-04 18:39:50 +00001006
1007 return( TRUE);
Erik Andersen6acaa402000-03-26 14:03:20 +00001008}
1009
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001010static int writeTarFile(const char* tarName, int verboseFlag, char **argv,
1011 char** excludeList)
Erik Andersen6acaa402000-03-26 14:03:20 +00001012{
Erik Andersen3364d782000-03-28 00:58:14 +00001013 int tarFd=-1;
Erik Andersen68a9ea42000-04-04 18:39:50 +00001014 int errorFlag=FALSE;
Erik Andersen5661fe02000-04-05 01:00:52 +00001015 ssize_t size;
Erik Andersen68a9ea42000-04-04 18:39:50 +00001016 struct TarBallInfo tbInfo;
1017 tbInfo.verboseFlag = verboseFlag;
Erik Andersen3364d782000-03-28 00:58:14 +00001018
1019 /* Make sure there is at least one file to tar up. */
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001020 if (*argv == NULL)
Matt Kraaibe84cd42000-07-12 17:02:35 +00001021 fatalError("Cowardly refusing to create an empty archive\n");
Erik Andersen6acaa402000-03-26 14:03:20 +00001022
1023 /* Open the tar file for writing. */
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001024 if (!strcmp(tarName, "-"))
Erik Andersen68a9ea42000-04-04 18:39:50 +00001025 tbInfo.tarFd = fileno(stdout);
Erik Andersen6acaa402000-03-26 14:03:20 +00001026 else
Erik Andersen68a9ea42000-04-04 18:39:50 +00001027 tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1028 if (tbInfo.tarFd < 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +00001029 errorMsg( "Error opening '%s': %s\n", tarName, strerror(errno));
Erik Andersen6acaa402000-03-26 14:03:20 +00001030 return ( FALSE);
1031 }
Erik Andersenecd51242000-04-08 03:08:21 +00001032 tbInfo.excludeList=excludeList;
Erik Andersen68a9ea42000-04-04 18:39:50 +00001033 /* Store the stat info for the tarball's file, so
1034 * can avoid including the tarball into itself.... */
1035 if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0)
Erik Andersen3364d782000-03-28 00:58:14 +00001036 fatalError(io_error, tarName, strerror(errno));
Erik Andersen6acaa402000-03-26 14:03:20 +00001037
1038 /* Set the umask for this process so it doesn't
1039 * screw up permission setting for us later. */
1040 umask(0);
1041
1042 /* Read the directory/files and iterate over them one at a time */
Matt Kraaid8ad76c2000-11-08 02:35:47 +00001043 while (*argv != NULL) {
Erik Andersen3364d782000-03-28 00:58:14 +00001044 if (recursiveAction(*argv++, TRUE, FALSE, FALSE,
Erik Andersen68a9ea42000-04-04 18:39:50 +00001045 writeFileToTarball, writeFileToTarball,
1046 (void*) &tbInfo) == FALSE) {
1047 errorFlag = TRUE;
Erik Andersen3364d782000-03-28 00:58:14 +00001048 }
Erik Andersen6acaa402000-03-26 14:03:20 +00001049 }
Erik Andersen5661fe02000-04-05 01:00:52 +00001050 /* Write two empty blocks to the end of the archive */
1051 for (size=0; size<(2*TAR_BLOCK_SIZE); size++) {
1052 write(tbInfo.tarFd, "\0", 1);
1053 }
Erik Andersen0817d132000-04-09 15:17:40 +00001054
1055 /* To be pedantically correct, we would check if the tarball
Eric Andersen3c5ee9a2000-11-14 22:15:48 +00001056 * is smaller than 20 tar blocks, and pad it if it was smaller,
Erik Andersen0817d132000-04-09 15:17:40 +00001057 * but that isn't necessary for GNU tar interoperability, and
1058 * so is considered a waste of space */
1059
Erik Andersen68a9ea42000-04-04 18:39:50 +00001060 /* Hang up the tools, close up shop, head home */
Erik Andersen6acaa402000-03-26 14:03:20 +00001061 close(tarFd);
Erik Andersen68a9ea42000-04-04 18:39:50 +00001062 if (errorFlag == TRUE) {
Matt Kraaibe84cd42000-07-12 17:02:35 +00001063 errorMsg("Error exit delayed from previous errors\n");
Erik Andersen68a9ea42000-04-04 18:39:50 +00001064 return(FALSE);
1065 }
Erik Andersen6acaa402000-03-26 14:03:20 +00001066 return( TRUE);
1067}
1068
1069
1070#endif
1071