blob: 0fa09ffd86834ad95df1da6f1e30e25e0c18927c [file] [log] [blame]
Eric Andersencc8ed391999-10-05 16:24:54 +00001/*
Eric Andersenc4996011999-10-20 22:08:37 +00002 * Mini tar implementation for busybox based on code taken from sash.
3 *
Eric Andersencc8ed391999-10-05 16:24:54 +00004 * Copyright (c) 1999 by David I. Bell
5 * Permission is granted to use, distribute, or modify this source,
6 * provided that this copyright notice remains intact.
7 *
Eric Andersencc8ed391999-10-05 16:24:54 +00008 * Permission to distribute this code under the GPL has been granted.
Eric Andersenc4996011999-10-20 22:08:37 +00009 *
Eric Andersen3cf52d11999-10-12 22:26:06 +000010 * Modified for busybox by Erik Andersen <andersee@debian.org>
Eric Andersenc4996011999-10-20 22:08:37 +000011 * Adjusted to grok stdin/stdout options.
12 *
Eric Andersen96bcfd31999-11-12 01:30:18 +000013 * Modified to handle device special files by Matt Porter
14 * <porter@debian.org>
15 *
Eric Andersenc4996011999-10-20 22:08:37 +000016 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
Eric Andersencc8ed391999-10-05 16:24:54 +000030 */
31
32
33#include "internal.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000034#include <stdio.h>
35#include <dirent.h>
36#include <errno.h>
37#include <fcntl.h>
38#include <signal.h>
39#include <time.h>
Erik Andersen7dc16072000-01-04 01:10:25 +000040#include <utime.h>
Eric Andersen96bcfd31999-11-12 01:30:18 +000041#include <sys/types.h>
Eric Andersen08b10341999-11-19 02:38:58 +000042#include <sys/sysmacros.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000043
Eric Andersene77ae3a1999-10-19 20:03:34 +000044
Erik Andersen05100cd2000-01-16 01:30:52 +000045#ifdef BB_FEATURE_TAR_CREATE
46
Eric Andersene77ae3a1999-10-19 20:03:34 +000047static const char tar_usage[] =
Eric Andersend73dc5b1999-11-10 23:13:02 +000048"tar -[cxtvOf] [tarFileName] [FILE] ...\n\n"
Erik Andersen05100cd2000-01-16 01:30:52 +000049"Create, extract, or list files from a tar file.\n\n"
Eric Andersend73dc5b1999-11-10 23:13:02 +000050"Options:\n"
51"\tc=create, x=extract, t=list contents, v=verbose,\n"
52"\tO=extract to stdout, f=tarfile or \"-\" for stdin\n";
Eric Andersene77ae3a1999-10-19 20:03:34 +000053
Erik Andersen05100cd2000-01-16 01:30:52 +000054#else
55
56static const char tar_usage[] =
Erik Andersen5509af72000-01-23 18:19:02 +000057"tar -[xtvOf] [tarFileName] [FILE] ...\n\n"
Erik Andersen05100cd2000-01-16 01:30:52 +000058"Extract, or list files stored in a tar file. This\n"
59"version of tar does not support creation of tar files.\n\n"
60"Options:\n"
61"\tx=extract, t=list contents, v=verbose,\n"
62"\tO=extract to stdout, f=tarfile or \"-\" for stdin\n";
63
64#endif
Eric Andersene77ae3a1999-10-19 20:03:34 +000065
66
Eric Andersencc8ed391999-10-05 16:24:54 +000067/*
68 * Tar file constants.
69 */
70#define TAR_BLOCK_SIZE 512
71#define TAR_NAME_SIZE 100
72
73
74/*
75 * The POSIX (and basic GNU) tar header format.
76 * This structure is always embedded in a TAR_BLOCK_SIZE sized block
77 * with zero padding. We only process this information minimally.
78 */
Eric Andersen3cf52d11999-10-12 22:26:06 +000079typedef struct {
80 char name[TAR_NAME_SIZE];
81 char mode[8];
82 char uid[8];
83 char gid[8];
84 char size[12];
85 char mtime[12];
86 char checkSum[8];
87 char typeFlag;
88 char linkName[TAR_NAME_SIZE];
89 char magic[6];
90 char version[2];
91 char uname[32];
92 char gname[32];
93 char devMajor[8];
94 char devMinor[8];
95 char prefix[155];
Eric Andersencc8ed391999-10-05 16:24:54 +000096} TarHeader;
97
98#define TAR_MAGIC "ustar"
99#define TAR_VERSION "00"
100
101#define TAR_TYPE_REGULAR '0'
102#define TAR_TYPE_HARD_LINK '1'
103#define TAR_TYPE_SOFT_LINK '2'
104
105
106/*
107 * Static data.
108 */
Eric Andersend73dc5b1999-11-10 23:13:02 +0000109static int listFlag;
110static int extractFlag;
111static int createFlag;
112static int verboseFlag;
113static int tostdoutFlag;
Eric Andersencc8ed391999-10-05 16:24:54 +0000114
Eric Andersen5556c181999-11-10 19:27:58 +0000115static int inHeader; // <- check me
Eric Andersend73dc5b1999-11-10 23:13:02 +0000116static int badHeader;
117static int errorFlag;
118static int skipFileFlag;
119static int warnedRoot;
120static int eofFlag;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000121static long dataCc;
122static int outFd;
Erik Andersen7dc16072000-01-04 01:10:25 +0000123static const char *outName;
Eric Andersencc8ed391999-10-05 16:24:54 +0000124
Erik Andersen7dc16072000-01-04 01:10:25 +0000125static int mode;
126static int uid;
127static int gid;
128static time_t mtime;
Eric Andersencc8ed391999-10-05 16:24:54 +0000129
130/*
131 * Static data associated with the tar file.
132 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000133static const char *tarName;
134static int tarFd;
135static dev_t tarDev;
136static ino_t tarInode;
Eric Andersencc8ed391999-10-05 16:24:54 +0000137
138
139/*
140 * Local procedures to restore files from a tar file.
141 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000142static void readTarFile (int fileCount, char **fileTable);
143static void readData (const char *cp, int count);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000144static long getOctal (const char *cp, int len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000145
Eric Andersen3cf52d11999-10-12 22:26:06 +0000146static void readHeader (const TarHeader * hp,
147 int fileCount, char **fileTable);
Eric Andersencc8ed391999-10-05 16:24:54 +0000148
Erik Andersen05100cd2000-01-16 01:30:52 +0000149static int wantFileName (const char *fileName,
150 int fileCount, char **fileTable);
Eric Andersencc8ed391999-10-05 16:24:54 +0000151
Erik Andersen05100cd2000-01-16 01:30:52 +0000152#ifdef BB_FEATURE_TAR_CREATE
Eric Andersencc8ed391999-10-05 16:24:54 +0000153/*
154 * Local procedures to save files into a tar file.
155 */
Eric Andersend73dc5b1999-11-10 23:13:02 +0000156static void saveFile (const char *fileName, int seeLinks);
Eric Andersencc8ed391999-10-05 16:24:54 +0000157
Eric Andersen3cf52d11999-10-12 22:26:06 +0000158static void saveRegularFile (const char *fileName,
159 const struct stat *statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +0000160
Eric Andersen3cf52d11999-10-12 22:26:06 +0000161static void saveDirectory (const char *fileName,
162 const struct stat *statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +0000163
Eric Andersen3cf52d11999-10-12 22:26:06 +0000164static void writeHeader (const char *fileName, const struct stat *statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +0000165
Eric Andersen3cf52d11999-10-12 22:26:06 +0000166static void writeTarFile (int fileCount, char **fileTable);
167static void writeTarBlock (const char *buf, int len);
Eric Andersend73dc5b1999-11-10 23:13:02 +0000168static int putOctal (char *cp, int len, long value);
Eric Andersencc8ed391999-10-05 16:24:54 +0000169
Erik Andersen05100cd2000-01-16 01:30:52 +0000170#endif
171
Eric Andersencc8ed391999-10-05 16:24:54 +0000172
Eric Andersen3cf52d11999-10-12 22:26:06 +0000173extern int tar_main (int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000174{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000175 const char *options;
Eric Andersencc8ed391999-10-05 16:24:54 +0000176
Eric Andersen3cf52d11999-10-12 22:26:06 +0000177 argc--;
178 argv++;
Eric Andersencc8ed391999-10-05 16:24:54 +0000179
Eric Andersenbe971d61999-11-03 16:52:50 +0000180 if (argc < 1)
181 usage( tar_usage);
Eric Andersencc8ed391999-10-05 16:24:54 +0000182
183
Eric Andersen3cf52d11999-10-12 22:26:06 +0000184 errorFlag = FALSE;
185 extractFlag = FALSE;
186 createFlag = FALSE;
187 listFlag = FALSE;
188 verboseFlag = FALSE;
189 tostdoutFlag = FALSE;
190 tarName = NULL;
191 tarDev = 0;
192 tarInode = 0;
193 tarFd = -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000194
Eric Andersen3cf52d11999-10-12 22:26:06 +0000195 /*
196 * Parse the options.
197 */
Erik Andersende552872000-01-23 01:34:05 +0000198 if (**argv == '-')
Eric Andersen50d63601999-11-09 01:47:36 +0000199 options = (*argv++) + 1;
Erik Andersende552872000-01-23 01:34:05 +0000200 else
201 options = (*argv++);
202 argc--;
Eric Andersencc8ed391999-10-05 16:24:54 +0000203
Erik Andersende552872000-01-23 01:34:05 +0000204 for (; *options; options++) {
205 switch (*options) {
206 case 'f':
207 if (tarName != NULL) {
208 fprintf (stderr, "Only one 'f' option allowed\n");
Eric Andersen3cf52d11999-10-12 22:26:06 +0000209
210 exit (FALSE);
211 }
Erik Andersende552872000-01-23 01:34:05 +0000212
213 tarName = *argv++;
214 argc--;
215
216 break;
217
218 case 't':
219 if (extractFlag == TRUE || createFlag == TRUE )
220 goto flagError;
221 listFlag = TRUE;
222 break;
223
224 case 'x':
225 if (listFlag == TRUE || createFlag == TRUE )
226 goto flagError;
227 extractFlag = TRUE;
228 break;
229 case 'c':
230 if (extractFlag == TRUE || listFlag == TRUE)
231 goto flagError;
232 createFlag = TRUE;
233 break;
234
235 case 'v':
236 verboseFlag = TRUE;
237 break;
238
239 case 'O':
240 tostdoutFlag = TRUE;
241 break;
242
243 case '-':
244 usage( tar_usage);
245 break;
246
247 default:
248 fprintf (stderr, "Unknown tar flag '%c'\n"
249 "Try `tar --help' for more information\n",
250 *options);
251 exit (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000252 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000253 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000254
Eric Andersen3cf52d11999-10-12 22:26:06 +0000255 /*
Eric Andersen3cf52d11999-10-12 22:26:06 +0000256 * Do the correct type of action supplying the rest of the
257 * command line arguments as the list of files to process.
258 */
Erik Andersende552872000-01-23 01:34:05 +0000259 if (createFlag==TRUE) {
260#ifndef BB_FEATURE_TAR_CREATE
261 fprintf (stderr, "This version of tar was not compiled with tar creation support.\n" );
262 exit (FALSE);
263#else
Eric Andersen3cf52d11999-10-12 22:26:06 +0000264 writeTarFile (argc, argv);
Erik Andersen05100cd2000-01-16 01:30:52 +0000265#endif
Erik Andersende552872000-01-23 01:34:05 +0000266 } else {
Eric Andersen3cf52d11999-10-12 22:26:06 +0000267 readTarFile (argc, argv);
Erik Andersende552872000-01-23 01:34:05 +0000268 }
269 if (errorFlag==TRUE) {
Eric Andersen3cf52d11999-10-12 22:26:06 +0000270 fprintf (stderr, "\n");
Erik Andersende552872000-01-23 01:34:05 +0000271 }
Eric Andersen5556c181999-11-10 19:27:58 +0000272 exit (!errorFlag);
Erik Andersende552872000-01-23 01:34:05 +0000273
274flagError:
275 fprintf (stderr, "Exactly one of 'c', 'x' or 't' must be specified\n");
276 exit (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000277}
278
279
280/*
281 * Read a tar file and extract or list the specified files within it.
282 * If the list is empty than all files are extracted or listed.
283 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000284static void readTarFile (int fileCount, char **fileTable)
Eric Andersencc8ed391999-10-05 16:24:54 +0000285{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000286 const char *cp;
287 int cc;
288 int inCc;
289 int blockSize;
290 char buf[BUF_SIZE];
Eric Andersencc8ed391999-10-05 16:24:54 +0000291
Eric Andersen3cf52d11999-10-12 22:26:06 +0000292 skipFileFlag = FALSE;
293 badHeader = FALSE;
294 warnedRoot = FALSE;
295 eofFlag = FALSE;
296 inHeader = TRUE;
297 inCc = 0;
298 dataCc = 0;
299 outFd = -1;
300 blockSize = sizeof (buf);
301 cp = buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000302
Eric Andersen3cf52d11999-10-12 22:26:06 +0000303 /*
304 * Open the tar file for reading.
305 */
306 if ((tarName == NULL) || !strcmp (tarName, "-")) {
Eric Andersen08b10341999-11-19 02:38:58 +0000307 tarFd = fileno(stdin);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000308 } else
309 tarFd = open (tarName, O_RDONLY);
310
311 if (tarFd < 0) {
312 perror (tarName);
313 errorFlag = TRUE;
314 return;
315 }
316
317 /*
318 * Read blocks from the file until an end of file header block
319 * has been seen. (A real end of file from a read is an error.)
320 */
Eric Andersen5556c181999-11-10 19:27:58 +0000321 while (eofFlag==FALSE) {
Eric Andersen3cf52d11999-10-12 22:26:06 +0000322 /*
323 * Read the next block of data if necessary.
324 * This will be a large block if possible, which we will
325 * then process in the small tar blocks.
Eric Andersencc8ed391999-10-05 16:24:54 +0000326 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000327 if (inCc <= 0) {
328 cp = buf;
329 inCc = fullRead (tarFd, buf, blockSize);
Eric Andersencc8ed391999-10-05 16:24:54 +0000330
Eric Andersen3cf52d11999-10-12 22:26:06 +0000331 if (inCc < 0) {
332 perror (tarName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000333 errorFlag = TRUE;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000334 goto done;
335 }
336
337 if (inCc == 0) {
338 fprintf (stderr,
339 "Unexpected end of file from \"%s\"", tarName);
340 errorFlag = TRUE;
341 goto done;
342 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000343 }
344
Eric Andersen3cf52d11999-10-12 22:26:06 +0000345 /*
346 * If we are expecting a header block then examine it.
Eric Andersencc8ed391999-10-05 16:24:54 +0000347 */
Eric Andersen5556c181999-11-10 19:27:58 +0000348 if (inHeader==TRUE) {
Eric Andersen3cf52d11999-10-12 22:26:06 +0000349 readHeader ((const TarHeader *) cp, fileCount, fileTable);
Eric Andersencc8ed391999-10-05 16:24:54 +0000350
Eric Andersen3cf52d11999-10-12 22:26:06 +0000351 cp += TAR_BLOCK_SIZE;
352 inCc -= TAR_BLOCK_SIZE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000353
Eric Andersen3cf52d11999-10-12 22:26:06 +0000354 continue;
Eric Andersencc8ed391999-10-05 16:24:54 +0000355 }
356
Eric Andersen3cf52d11999-10-12 22:26:06 +0000357 /*
358 * We are currently handling the data for a file.
359 * Process the minimum of the amount of data we have available
360 * and the amount left to be processed for the file.
Eric Andersencc8ed391999-10-05 16:24:54 +0000361 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000362 cc = inCc;
Eric Andersencc8ed391999-10-05 16:24:54 +0000363
Eric Andersen3cf52d11999-10-12 22:26:06 +0000364 if (cc > dataCc)
365 cc = dataCc;
366
367 readData (cp, cc);
368
369 /*
370 * If the amount left isn't an exact multiple of the tar block
371 * size then round it up to the next block boundary since there
372 * is padding at the end of the file.
Eric Andersencc8ed391999-10-05 16:24:54 +0000373 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000374 if (cc % TAR_BLOCK_SIZE)
375 cc += TAR_BLOCK_SIZE - (cc % TAR_BLOCK_SIZE);
376
377 cp += cc;
378 inCc -= cc;
379 }
380
381 done:
382 /*
383 * Close the tar file if needed.
384 */
385 if ((tarFd >= 0) && (close (tarFd) < 0))
386 perror (tarName);
387
388 /*
389 * Close the output file if needed.
390 * This is only done here on a previous error and so no
391 * message is required on errors.
392 */
393 if (tostdoutFlag == FALSE) {
Erik Andersen7dc16072000-01-04 01:10:25 +0000394 if (outFd >= 0) {
395 close (outFd);
396 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000397 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000398}
399
400
401/*
402 * Examine the header block that was just read.
403 * This can specify the information for another file, or it can mark
404 * the end of the tar file.
405 */
406static void
Eric Andersen3cf52d11999-10-12 22:26:06 +0000407readHeader (const TarHeader * hp, int fileCount, char **fileTable)
Eric Andersencc8ed391999-10-05 16:24:54 +0000408{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000409 int checkSum;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000410 int cc;
411 int hardLink;
412 int softLink;
Eric Andersen96bcfd31999-11-12 01:30:18 +0000413 int devFileFlag;
Erik Andersen7dc16072000-01-04 01:10:25 +0000414 unsigned int major;
415 unsigned int minor;
416 long size;
417 struct utimbuf utb;
Eric Andersencc8ed391999-10-05 16:24:54 +0000418
Eric Andersen3cf52d11999-10-12 22:26:06 +0000419 /*
420 * If the block is completely empty, then this is the end of the
421 * archive file. If the name is null, then just skip this header.
422 */
Erik Andersen7dc16072000-01-04 01:10:25 +0000423 outName = hp->name;
Eric Andersencc8ed391999-10-05 16:24:54 +0000424
Erik Andersen7dc16072000-01-04 01:10:25 +0000425 if (*outName == '\0') {
Eric Andersen3cf52d11999-10-12 22:26:06 +0000426 for (cc = TAR_BLOCK_SIZE; cc > 0; cc--) {
Erik Andersen7dc16072000-01-04 01:10:25 +0000427 if (*outName++)
Eric Andersencc8ed391999-10-05 16:24:54 +0000428 return;
429 }
430
Eric Andersen3cf52d11999-10-12 22:26:06 +0000431 eofFlag = TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000432
Eric Andersen3cf52d11999-10-12 22:26:06 +0000433 return;
434 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000435
Eric Andersen3cf52d11999-10-12 22:26:06 +0000436 /*
437 * There is another file in the archive to examine.
438 * Extract the encoded information and check it.
439 */
440 mode = getOctal (hp->mode, sizeof (hp->mode));
441 uid = getOctal (hp->uid, sizeof (hp->uid));
442 gid = getOctal (hp->gid, sizeof (hp->gid));
443 size = getOctal (hp->size, sizeof (hp->size));
444 mtime = getOctal (hp->mtime, sizeof (hp->mtime));
445 checkSum = getOctal (hp->checkSum, sizeof (hp->checkSum));
Eric Andersen96bcfd31999-11-12 01:30:18 +0000446 major = getOctal (hp->devMajor, sizeof (hp->devMajor));
447 minor = getOctal (hp->devMinor, sizeof (hp->devMinor));
Eric Andersencc8ed391999-10-05 16:24:54 +0000448
Eric Andersen3cf52d11999-10-12 22:26:06 +0000449 if ((mode < 0) || (uid < 0) || (gid < 0) || (size < 0)) {
Eric Andersen5556c181999-11-10 19:27:58 +0000450 if (badHeader==FALSE)
Eric Andersen3cf52d11999-10-12 22:26:06 +0000451 fprintf (stderr, "Bad tar header, skipping\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000452
Eric Andersen3cf52d11999-10-12 22:26:06 +0000453 badHeader = TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000454
Eric Andersen3cf52d11999-10-12 22:26:06 +0000455 return;
456 }
457
458 badHeader = FALSE;
459 skipFileFlag = FALSE;
Eric Andersen96bcfd31999-11-12 01:30:18 +0000460 devFileFlag = FALSE;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000461
462 /*
463 * Check for the file modes.
464 */
465 hardLink = ((hp->typeFlag == TAR_TYPE_HARD_LINK) ||
Eric Andersencc8ed391999-10-05 16:24:54 +0000466 (hp->typeFlag == TAR_TYPE_HARD_LINK - '0'));
467
Eric Andersen3cf52d11999-10-12 22:26:06 +0000468 softLink = ((hp->typeFlag == TAR_TYPE_SOFT_LINK) ||
Eric Andersencc8ed391999-10-05 16:24:54 +0000469 (hp->typeFlag == TAR_TYPE_SOFT_LINK - '0'));
470
Eric Andersen3cf52d11999-10-12 22:26:06 +0000471 /*
Eric Andersen96bcfd31999-11-12 01:30:18 +0000472 * Check for a directory.
Eric Andersen3cf52d11999-10-12 22:26:06 +0000473 */
Erik Andersen7dc16072000-01-04 01:10:25 +0000474 if (outName[strlen (outName) - 1] == '/')
Eric Andersen3cf52d11999-10-12 22:26:06 +0000475 mode |= S_IFDIR;
Eric Andersencc8ed391999-10-05 16:24:54 +0000476
Eric Andersen3cf52d11999-10-12 22:26:06 +0000477 /*
478 * Check for absolute paths in the file.
479 * If we find any, then warn the user and make them relative.
480 */
Erik Andersen7dc16072000-01-04 01:10:25 +0000481 if (*outName == '/') {
482 while (*outName == '/')
483 outName++;
Eric Andersencc8ed391999-10-05 16:24:54 +0000484
Eric Andersen5556c181999-11-10 19:27:58 +0000485 if (warnedRoot==FALSE) {
Eric Andersen3cf52d11999-10-12 22:26:06 +0000486 fprintf (stderr,
487 "Absolute path detected, removing leading slashes\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000488 }
489
Eric Andersen3cf52d11999-10-12 22:26:06 +0000490 warnedRoot = TRUE;
491 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000492
Eric Andersen3cf52d11999-10-12 22:26:06 +0000493 /*
494 * See if we want this file to be restored.
495 * If not, then set up to skip it.
496 */
Erik Andersen7dc16072000-01-04 01:10:25 +0000497 if (wantFileName (outName, fileCount, fileTable) == FALSE) {
Eric Andersen96bcfd31999-11-12 01:30:18 +0000498 if ( !hardLink && !softLink && (S_ISREG (mode) || S_ISCHR (mode)
499 || S_ISBLK (mode) || S_ISSOCK(mode) || S_ISFIFO(mode) ) ) {
Eric Andersen5556c181999-11-10 19:27:58 +0000500 inHeader = (size == 0)? TRUE : FALSE;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000501 dataCc = size;
Eric Andersencc8ed391999-10-05 16:24:54 +0000502 }
503
Eric Andersen3cf52d11999-10-12 22:26:06 +0000504 skipFileFlag = TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000505
Eric Andersen3cf52d11999-10-12 22:26:06 +0000506 return;
507 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000508
Eric Andersen3cf52d11999-10-12 22:26:06 +0000509 /*
510 * This file is to be handled.
511 * If we aren't extracting then just list information about the file.
512 */
Eric Andersen5556c181999-11-10 19:27:58 +0000513 if (extractFlag==FALSE) {
514 if (verboseFlag==TRUE) {
Eric Andersen03018f71999-11-29 04:29:13 +0000515 printf ("%s %3d/%-d ", modeString (mode), uid, gid);
516 if( S_ISCHR (mode) || S_ISBLK (mode) )
517 printf ("%4d,%4d %s ", major,minor, timeString (mtime));
518 else
519 printf ("%9ld %s ", size, timeString (mtime));
520 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000521 printf ("%s", outName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000522
523 if (hardLink)
Eric Andersen3cf52d11999-10-12 22:26:06 +0000524 printf (" (link to \"%s\")", hp->linkName);
525 else if (softLink)
526 printf (" (symlink to \"%s\")", hp->linkName);
Eric Andersen96bcfd31999-11-12 01:30:18 +0000527 else if (S_ISREG (mode) || S_ISCHR (mode) || S_ISBLK (mode) ||
528 S_ISSOCK(mode) || S_ISFIFO(mode) ) {
Eric Andersen5556c181999-11-10 19:27:58 +0000529 inHeader = (size == 0)? TRUE : FALSE;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000530 dataCc = size;
Eric Andersencc8ed391999-10-05 16:24:54 +0000531 }
532
Eric Andersen3cf52d11999-10-12 22:26:06 +0000533 printf ("\n");
534
535 return;
536 }
537
538 /*
539 * We really want to extract the file.
540 */
Eric Andersen5556c181999-11-10 19:27:58 +0000541 if (verboseFlag==TRUE)
Erik Andersen7dc16072000-01-04 01:10:25 +0000542 printf ("x %s\n", outName);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000543
544 if (hardLink) {
Erik Andersen06936df2000-01-23 02:14:20 +0000545 if (link (hp->linkName, outName) < 0) {
Erik Andersen7dc16072000-01-04 01:10:25 +0000546 perror (outName);
Erik Andersen06936df2000-01-23 02:14:20 +0000547 return;
548 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000549 /* Set the file time */
550 utb.actime = mtime;
551 utb.modtime = mtime;
552 utime (outName, &utb);
553 /* Set the file permissions */
554 chown(outName, uid, gid);
555 chmod(outName, mode);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000556 return;
557 }
558
559 if (softLink) {
Eric Andersencc8ed391999-10-05 16:24:54 +0000560#ifdef S_ISLNK
Erik Andersen06936df2000-01-23 02:14:20 +0000561 if (symlink (hp->linkName, outName) < 0) {
Erik Andersen7dc16072000-01-04 01:10:25 +0000562 perror (outName);
Erik Andersen06936df2000-01-23 02:14:20 +0000563 return;
564 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000565 /* Try to change ownership of the symlink.
566 * If libs doesn't support that, don't bother.
567 * Changing the pointed-to file is the Wrong Thing(tm).
568 */
569#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
570 lchown(outName, uid, gid);
571#endif
572
573 /* Do not change permissions or date on symlink,
574 * since it changes the pointed to file instead. duh. */
Eric Andersencc8ed391999-10-05 16:24:54 +0000575#else
Eric Andersen3cf52d11999-10-12 22:26:06 +0000576 fprintf (stderr, "Cannot create symbolic links\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000577#endif
Eric Andersen3cf52d11999-10-12 22:26:06 +0000578 return;
579 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000580
Eric Andersen03018f71999-11-29 04:29:13 +0000581 /* Set the umask for this process so it doesn't
582 * screw things up. */
583 umask(0);
584
Eric Andersen3cf52d11999-10-12 22:26:06 +0000585 /*
586 * If the file is a directory, then just create the path.
587 */
588 if (S_ISDIR (mode)) {
Erik Andersen06936df2000-01-23 02:14:20 +0000589 if (createPath (outName, mode)==TRUE) {
590 /* Set the file time */
591 utb.actime = mtime;
592 utb.modtime = mtime;
593 utime (outName, &utb);
594 /* Set the file permissions */
595 chown(outName, uid, gid);
596 chmod(outName, mode);
597 return;
598 }
Erik Andersence5b4662000-01-27 19:50:47 +0000599 return;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000600 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000601
Eric Andersen3cf52d11999-10-12 22:26:06 +0000602 /*
603 * There is a file to write.
Eric Andersen03018f71999-11-29 04:29:13 +0000604 * First create the path to it if necessary with default permissions.
Eric Andersen3cf52d11999-10-12 22:26:06 +0000605 */
Erik Andersen7dc16072000-01-04 01:10:25 +0000606 createPath (outName, 0777);
Eric Andersencc8ed391999-10-05 16:24:54 +0000607
Eric Andersen5556c181999-11-10 19:27:58 +0000608 inHeader = (size == 0)? TRUE : FALSE;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000609 dataCc = size;
Eric Andersencc8ed391999-10-05 16:24:54 +0000610
Eric Andersen3cf52d11999-10-12 22:26:06 +0000611 /*
612 * Start the output file.
613 */
614 if (tostdoutFlag == TRUE)
Eric Andersen08b10341999-11-19 02:38:58 +0000615 outFd = fileno(stdout);
Eric Andersen96bcfd31999-11-12 01:30:18 +0000616 else {
617 if ( S_ISCHR(mode) || S_ISBLK(mode) || S_ISSOCK(mode) ) {
618 devFileFlag = TRUE;
Erik Andersen7dc16072000-01-04 01:10:25 +0000619 outFd = mknod (outName, mode, makedev(major, minor) );
Eric Andersen96bcfd31999-11-12 01:30:18 +0000620 }
621 else if (S_ISFIFO(mode) ) {
Eric Andersenb6a44b81999-11-13 04:47:09 +0000622 devFileFlag = TRUE;
Erik Andersen7dc16072000-01-04 01:10:25 +0000623 outFd = mkfifo(outName, mode);
Eric Andersen96bcfd31999-11-12 01:30:18 +0000624 } else {
Erik Andersen7dc16072000-01-04 01:10:25 +0000625 outFd = open (outName, O_WRONLY | O_CREAT | O_TRUNC, mode);
Eric Andersen96bcfd31999-11-12 01:30:18 +0000626 }
Erik Andersen00266d31999-12-28 00:17:46 +0000627 if (outFd < 0) {
Erik Andersen7dc16072000-01-04 01:10:25 +0000628 perror (outName);
Erik Andersen00266d31999-12-28 00:17:46 +0000629 skipFileFlag = TRUE;
630 return;
631 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000632 /* Set the file time */
633 utb.actime = mtime;
634 utb.modtime = mtime;
635 utime (outName, &utb);
636 /* Set the file permissions */
637 chown(outName, uid, gid);
638 chmod(outName, mode);
Eric Andersen96bcfd31999-11-12 01:30:18 +0000639 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000640
Eric Andersencc8ed391999-10-05 16:24:54 +0000641
Eric Andersen3cf52d11999-10-12 22:26:06 +0000642 /*
643 * If the file is empty, then that's all we need to do.
644 */
Eric Andersen96bcfd31999-11-12 01:30:18 +0000645 if (size == 0 && (tostdoutFlag == FALSE) && (devFileFlag == FALSE)) {
Erik Andersen7dc16072000-01-04 01:10:25 +0000646 close (outFd);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000647 outFd = -1;
648 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000649}
650
651
652/*
653 * Handle a data block of some specified size that was read.
654 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000655static void readData (const char *cp, int count)
Eric Andersencc8ed391999-10-05 16:24:54 +0000656{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000657 /*
658 * Reduce the amount of data left in this file.
659 * If there is no more data left, then we need to read
660 * the header again.
661 */
662 dataCc -= count;
Eric Andersencc8ed391999-10-05 16:24:54 +0000663
Eric Andersen3cf52d11999-10-12 22:26:06 +0000664 if (dataCc <= 0)
665 inHeader = TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000666
Eric Andersen3cf52d11999-10-12 22:26:06 +0000667 /*
668 * If we aren't extracting files or this file is being
669 * skipped then do nothing more.
670 */
Eric Andersen5556c181999-11-10 19:27:58 +0000671 if (extractFlag==FALSE || skipFileFlag==TRUE)
Eric Andersen3cf52d11999-10-12 22:26:06 +0000672 return;
Eric Andersencc8ed391999-10-05 16:24:54 +0000673
Eric Andersen3cf52d11999-10-12 22:26:06 +0000674 /*
675 * Write the data to the output file.
676 */
677 if (fullWrite (outFd, cp, count) < 0) {
678 perror (outName);
679 if (tostdoutFlag == FALSE) {
Erik Andersen7dc16072000-01-04 01:10:25 +0000680 close (outFd);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000681 outFd = -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000682 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000683 skipFileFlag = TRUE;
684 return;
685 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000686
Eric Andersen3cf52d11999-10-12 22:26:06 +0000687 /*
Erik Andersen7dc16072000-01-04 01:10:25 +0000688 * Check if we are done writing to the file now.
Eric Andersen3cf52d11999-10-12 22:26:06 +0000689 */
690 if (dataCc <= 0 && tostdoutFlag == FALSE) {
Erik Andersen7dc16072000-01-04 01:10:25 +0000691 struct utimbuf utb;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000692 if (close (outFd))
693 perror (outName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000694
Erik Andersen7dc16072000-01-04 01:10:25 +0000695 /* Set the file time */
696 utb.actime = mtime;
697 utb.modtime = mtime;
698 utime (outName, &utb);
699 /* Set the file permissions */
700 chown(outName, uid, gid);
701 chmod(outName, mode);
702
Eric Andersen3cf52d11999-10-12 22:26:06 +0000703 outFd = -1;
704 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000705}
706
707
708/*
Erik Andersen05100cd2000-01-16 01:30:52 +0000709 * See if the specified file name belongs to one of the specified list
710 * of path prefixes. An empty list implies that all files are wanted.
711 * Returns TRUE if the file is selected.
712 */
713static int
714wantFileName (const char *fileName, int fileCount, char **fileTable)
715{
716 const char *pathName;
717 int fileLength;
718 int pathLength;
719
720 /*
721 * If there are no files in the list, then the file is wanted.
722 */
723 if (fileCount == 0)
724 return TRUE;
725
726 fileLength = strlen (fileName);
727
728 /*
729 * Check each of the test paths.
730 */
731 while (fileCount-- > 0) {
732 pathName = *fileTable++;
733
734 pathLength = strlen (pathName);
735
736 if (fileLength < pathLength)
737 continue;
738
739 if (memcmp (fileName, pathName, pathLength) != 0)
740 continue;
741
742 if ((fileLength == pathLength) || (fileName[pathLength] == '/')) {
743 return TRUE;
744 }
745 }
746
747 return FALSE;
748}
749
750/*
751 * Read an octal value in a field of the specified width, with optional
752 * spaces on both sides of the number and with an optional null character
753 * at the end. Returns -1 on an illegal format.
754 */
755static long getOctal (const char *cp, int len)
756{
757 long val;
758
759 while ((len > 0) && (*cp == ' ')) {
760 cp++;
761 len--;
762 }
763
764 if ((len == 0) || !isOctal (*cp))
765 return -1;
766
767 val = 0;
768
769 while ((len > 0) && isOctal (*cp)) {
770 val = val * 8 + *cp++ - '0';
771 len--;
772 }
773
774 while ((len > 0) && (*cp == ' ')) {
775 cp++;
776 len--;
777 }
778
779 if ((len > 0) && *cp)
780 return -1;
781
782 return val;
783}
784
785
786
787
788/* From here to the end of the file is the tar writing stuff.
789 * If you do not have BB_FEATURE_TAR_CREATE defined, this will
790 * not be built.
791 * */
792#ifdef BB_FEATURE_TAR_CREATE
793
794/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000795 * Write a tar file containing the specified files.
796 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000797static void writeTarFile (int fileCount, char **fileTable)
Eric Andersencc8ed391999-10-05 16:24:54 +0000798{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000799 struct stat statbuf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000800
Eric Andersen3cf52d11999-10-12 22:26:06 +0000801 /*
802 * Make sure there is at least one file specified.
803 */
804 if (fileCount <= 0) {
805 fprintf (stderr, "No files specified to be saved\n");
806 errorFlag = TRUE;
807 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000808
Eric Andersen3cf52d11999-10-12 22:26:06 +0000809 /*
810 * Create the tar file for writing.
811 */
812 if ((tarName == NULL) || !strcmp (tarName, "-")) {
813 tostdoutFlag = TRUE;
Eric Andersen08b10341999-11-19 02:38:58 +0000814 tarFd = fileno(stdout);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000815 } else
816 tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0666);
Eric Andersencc8ed391999-10-05 16:24:54 +0000817
Eric Andersen3cf52d11999-10-12 22:26:06 +0000818 if (tarFd < 0) {
819 perror (tarName);
820 errorFlag = TRUE;
821 return;
822 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000823
Eric Andersen3cf52d11999-10-12 22:26:06 +0000824 /*
825 * Get the device and inode of the tar file for checking later.
826 */
827 if (fstat (tarFd, &statbuf) < 0) {
828 perror (tarName);
829 errorFlag = TRUE;
830 goto done;
831 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000832
Eric Andersen3cf52d11999-10-12 22:26:06 +0000833 tarDev = statbuf.st_dev;
834 tarInode = statbuf.st_ino;
Eric Andersen03018f71999-11-29 04:29:13 +0000835
Eric Andersen3cf52d11999-10-12 22:26:06 +0000836 /*
837 * Append each file name into the archive file.
838 * Follow symbolic links for these top level file names.
839 */
Eric Andersen5556c181999-11-10 19:27:58 +0000840 while (errorFlag==FALSE && (fileCount-- > 0)) {
Eric Andersen3cf52d11999-10-12 22:26:06 +0000841 saveFile (*fileTable++, FALSE);
842 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000843
Eric Andersen3cf52d11999-10-12 22:26:06 +0000844 /*
845 * Now write an empty block of zeroes to end the archive.
846 */
847 writeTarBlock ("", 1);
Eric Andersencc8ed391999-10-05 16:24:54 +0000848
849
Eric Andersen3cf52d11999-10-12 22:26:06 +0000850 done:
851 /*
852 * Close the tar file and check for errors if it was opened.
853 */
854 if ((tostdoutFlag == FALSE) && (tarFd >= 0) && (close (tarFd) < 0))
855 perror (tarName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000856}
857
Eric Andersencc8ed391999-10-05 16:24:54 +0000858/*
859 * Save one file into the tar file.
860 * If the file is a directory, then this will recursively save all of
861 * the files and directories within the directory. The seeLinks
862 * flag indicates whether or not we want to see symbolic links as
863 * they really are, instead of blindly following them.
864 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000865static void saveFile (const char *fileName, int seeLinks)
Eric Andersencc8ed391999-10-05 16:24:54 +0000866{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000867 int status;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000868 struct stat statbuf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000869
Eric Andersen5556c181999-11-10 19:27:58 +0000870 if (verboseFlag==TRUE)
Eric Andersen3cf52d11999-10-12 22:26:06 +0000871 printf ("a %s\n", fileName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000872
Eric Andersen3cf52d11999-10-12 22:26:06 +0000873 /*
874 * Check that the file name will fit in the header.
875 */
876 if (strlen (fileName) >= TAR_NAME_SIZE) {
877 fprintf (stderr, "%s: File name is too long\n", fileName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000878
Eric Andersen3cf52d11999-10-12 22:26:06 +0000879 return;
880 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000881
Eric Andersen3cf52d11999-10-12 22:26:06 +0000882 /*
883 * Find out about the file.
884 */
Eric Andersencc8ed391999-10-05 16:24:54 +0000885#ifdef S_ISLNK
Eric Andersen5556c181999-11-10 19:27:58 +0000886 if (seeLinks==TRUE)
Eric Andersen3cf52d11999-10-12 22:26:06 +0000887 status = lstat (fileName, &statbuf);
888 else
Eric Andersencc8ed391999-10-05 16:24:54 +0000889#endif
Eric Andersen3cf52d11999-10-12 22:26:06 +0000890 status = stat (fileName, &statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +0000891
Eric Andersen3cf52d11999-10-12 22:26:06 +0000892 if (status < 0) {
893 perror (fileName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000894
Eric Andersen3cf52d11999-10-12 22:26:06 +0000895 return;
896 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000897
Eric Andersen3cf52d11999-10-12 22:26:06 +0000898 /*
899 * Make sure we aren't trying to save our file into itself.
900 */
901 if ((statbuf.st_dev == tarDev) && (statbuf.st_ino == tarInode)) {
902 fprintf (stderr, "Skipping saving of archive file itself\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000903
Eric Andersen3cf52d11999-10-12 22:26:06 +0000904 return;
905 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000906
Eric Andersen3cf52d11999-10-12 22:26:06 +0000907 /*
908 * Check the type of file.
909 */
910 mode = statbuf.st_mode;
Eric Andersencc8ed391999-10-05 16:24:54 +0000911
Eric Andersen3cf52d11999-10-12 22:26:06 +0000912 if (S_ISDIR (mode)) {
913 saveDirectory (fileName, &statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +0000914
Eric Andersen3cf52d11999-10-12 22:26:06 +0000915 return;
916 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000917 if (S_ISREG (mode)) {
918 saveRegularFile (fileName, &statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +0000919
Eric Andersen3cf52d11999-10-12 22:26:06 +0000920 return;
921 }
Eric Andersen96bcfd31999-11-12 01:30:18 +0000922
923 /* Some day add support for tarring these up... but not today. :) */
924// if (S_ISLNK(mode) || S_ISFIFO(mode) || S_ISBLK(mode) || S_ISCHR (mode) ) {
925// fprintf (stderr, "%s: This version of tar can't store this type of file\n", fileName);
926// }
Eric Andersencc8ed391999-10-05 16:24:54 +0000927
Eric Andersen3cf52d11999-10-12 22:26:06 +0000928 /*
929 * The file is a strange type of file, ignore it.
930 */
931 fprintf (stderr, "%s: not a directory or regular file\n", fileName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000932}
933
934
935/*
936 * Save a regular file to the tar file.
937 */
938static void
Eric Andersen3cf52d11999-10-12 22:26:06 +0000939saveRegularFile (const char *fileName, const struct stat *statbuf)
Eric Andersencc8ed391999-10-05 16:24:54 +0000940{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000941 int sawEof;
942 int fileFd;
943 int cc;
944 int dataCount;
945 long fullDataCount;
946 char data[TAR_BLOCK_SIZE * 16];
Eric Andersencc8ed391999-10-05 16:24:54 +0000947
Eric Andersen3cf52d11999-10-12 22:26:06 +0000948 /*
949 * Open the file for reading.
950 */
951 fileFd = open (fileName, O_RDONLY);
952
953 if (fileFd < 0) {
954 perror (fileName);
955
956 return;
957 }
958
959 /*
960 * Write out the header for the file.
961 */
962 writeHeader (fileName, statbuf);
963
964 /*
965 * Write the data blocks of the file.
966 * We must be careful to write the amount of data that the stat
967 * buffer indicated, even if the file has changed size. Otherwise
968 * the tar file will be incorrect.
969 */
970 fullDataCount = statbuf->st_size;
971 sawEof = FALSE;
972
973 while (fullDataCount > 0) {
974 /*
975 * Get the amount to write this iteration which is
976 * the minumum of the amount left to write and the
977 * buffer size.
Eric Andersencc8ed391999-10-05 16:24:54 +0000978 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000979 dataCount = sizeof (data);
Eric Andersencc8ed391999-10-05 16:24:54 +0000980
Eric Andersen3cf52d11999-10-12 22:26:06 +0000981 if (dataCount > fullDataCount)
982 dataCount = (int) fullDataCount;
983
984 /*
985 * Read the data from the file if we haven't seen the
986 * end of file yet.
987 */
988 cc = 0;
989
Eric Andersen5556c181999-11-10 19:27:58 +0000990 if (sawEof==FALSE) {
Eric Andersen3cf52d11999-10-12 22:26:06 +0000991 cc = fullRead (fileFd, data, dataCount);
992
993 if (cc < 0) {
994 perror (fileName);
995
996 (void) close (fileFd);
997 errorFlag = TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000998
999 return;
Eric Andersen3cf52d11999-10-12 22:26:06 +00001000 }
1001
1002 /*
1003 * If the file ended too soon, complain and set
1004 * a flag so we will zero fill the rest of it.
1005 */
1006 if (cc < dataCount) {
1007 fprintf (stderr,
1008 "%s: Short read - zero filling", fileName);
1009
1010 sawEof = TRUE;
1011 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001012 }
1013
Eric Andersen3cf52d11999-10-12 22:26:06 +00001014 /*
1015 * Zero fill the rest of the data if necessary.
Eric Andersencc8ed391999-10-05 16:24:54 +00001016 */
Eric Andersen3cf52d11999-10-12 22:26:06 +00001017 if (cc < dataCount)
1018 memset (data + cc, 0, dataCount - cc);
Eric Andersencc8ed391999-10-05 16:24:54 +00001019
Eric Andersen3cf52d11999-10-12 22:26:06 +00001020 /*
1021 * Write the buffer to the TAR file.
Eric Andersencc8ed391999-10-05 16:24:54 +00001022 */
Eric Andersen3cf52d11999-10-12 22:26:06 +00001023 writeTarBlock (data, dataCount);
Eric Andersencc8ed391999-10-05 16:24:54 +00001024
Eric Andersen3cf52d11999-10-12 22:26:06 +00001025 fullDataCount -= dataCount;
1026 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001027
Eric Andersen3cf52d11999-10-12 22:26:06 +00001028 /*
1029 * Close the file.
1030 */
1031 if ((tostdoutFlag == FALSE) && close (fileFd) < 0)
1032 fprintf (stderr, "%s: close: %s\n", fileName, strerror (errno));
Eric Andersencc8ed391999-10-05 16:24:54 +00001033}
1034
1035
1036/*
1037 * Save a directory and all of its files to the tar file.
1038 */
Eric Andersen3cf52d11999-10-12 22:26:06 +00001039static void saveDirectory (const char *dirName, const struct stat *statbuf)
Eric Andersencc8ed391999-10-05 16:24:54 +00001040{
Eric Andersen3cf52d11999-10-12 22:26:06 +00001041 DIR *dir;
1042 struct dirent *entry;
1043 int needSlash;
Eric Andersen1b61f411999-10-13 18:56:42 +00001044 char fullName[NAME_MAX];
Eric Andersencc8ed391999-10-05 16:24:54 +00001045
Eric Andersen3cf52d11999-10-12 22:26:06 +00001046 /*
1047 * Construct the directory name as used in the tar file by appending
1048 * a slash character to it.
1049 */
1050 strcpy (fullName, dirName);
1051 strcat (fullName, "/");
Eric Andersencc8ed391999-10-05 16:24:54 +00001052
Eric Andersen3cf52d11999-10-12 22:26:06 +00001053 /*
1054 * Write out the header for the directory entry.
1055 */
1056 writeHeader (fullName, statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +00001057
Eric Andersen3cf52d11999-10-12 22:26:06 +00001058 /*
1059 * Open the directory.
1060 */
1061 dir = opendir (dirName);
Eric Andersencc8ed391999-10-05 16:24:54 +00001062
Eric Andersen3cf52d11999-10-12 22:26:06 +00001063 if (dir == NULL) {
1064 fprintf (stderr, "Cannot read directory \"%s\": %s\n",
1065 dirName, strerror (errno));
Eric Andersencc8ed391999-10-05 16:24:54 +00001066
Eric Andersen3cf52d11999-10-12 22:26:06 +00001067 return;
1068 }
1069
1070 /*
1071 * See if a slash is needed.
1072 */
1073 needSlash = (*dirName && (dirName[strlen (dirName) - 1] != '/'));
1074
1075 /*
1076 * Read all of the directory entries and check them,
1077 * except for the current and parent directory entries.
1078 */
Eric Andersen5556c181999-11-10 19:27:58 +00001079 while (errorFlag==FALSE && ((entry = readdir (dir)) != NULL)) {
Eric Andersen3cf52d11999-10-12 22:26:06 +00001080 if ((strcmp (entry->d_name, ".") == 0) ||
1081 (strcmp (entry->d_name, "..") == 0)) {
1082 continue;
Eric Andersencc8ed391999-10-05 16:24:54 +00001083 }
1084
Eric Andersen3cf52d11999-10-12 22:26:06 +00001085 /*
1086 * Build the full path name to the file.
Eric Andersencc8ed391999-10-05 16:24:54 +00001087 */
Eric Andersen3cf52d11999-10-12 22:26:06 +00001088 strcpy (fullName, dirName);
Eric Andersencc8ed391999-10-05 16:24:54 +00001089
Eric Andersen3cf52d11999-10-12 22:26:06 +00001090 if (needSlash)
1091 strcat (fullName, "/");
1092
1093 strcat (fullName, entry->d_name);
1094
1095 /*
1096 * Write this file to the tar file, noticing whether or not
1097 * the file is a symbolic link.
Eric Andersencc8ed391999-10-05 16:24:54 +00001098 */
Eric Andersen3cf52d11999-10-12 22:26:06 +00001099 saveFile (fullName, TRUE);
1100 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001101
Eric Andersen3cf52d11999-10-12 22:26:06 +00001102 /*
1103 * All done, close the directory.
1104 */
1105 closedir (dir);
Eric Andersencc8ed391999-10-05 16:24:54 +00001106}
1107
1108
1109/*
1110 * Write a tar header for the specified file name and status.
1111 * It is assumed that the file name fits.
1112 */
Eric Andersen3cf52d11999-10-12 22:26:06 +00001113static void writeHeader (const char *fileName, const struct stat *statbuf)
Eric Andersencc8ed391999-10-05 16:24:54 +00001114{
Eric Andersen3cf52d11999-10-12 22:26:06 +00001115 long checkSum;
1116 const unsigned char *cp;
1117 int len;
1118 TarHeader header;
Eric Andersencc8ed391999-10-05 16:24:54 +00001119
Eric Andersen3cf52d11999-10-12 22:26:06 +00001120 /*
1121 * Zero the header block in preparation for filling it in.
1122 */
1123 memset ((char *) &header, 0, sizeof (header));
Eric Andersencc8ed391999-10-05 16:24:54 +00001124
Eric Andersen3cf52d11999-10-12 22:26:06 +00001125 /*
1126 * Fill in the header.
1127 */
1128 strcpy (header.name, fileName);
Eric Andersencc8ed391999-10-05 16:24:54 +00001129
Eric Andersen3cf52d11999-10-12 22:26:06 +00001130 strncpy (header.magic, TAR_MAGIC, sizeof (header.magic));
1131 strncpy (header.version, TAR_VERSION, sizeof (header.version));
Eric Andersencc8ed391999-10-05 16:24:54 +00001132
Eric Andersen3cf52d11999-10-12 22:26:06 +00001133 putOctal (header.mode, sizeof (header.mode), statbuf->st_mode & 0777);
1134 putOctal (header.uid, sizeof (header.uid), statbuf->st_uid);
1135 putOctal (header.gid, sizeof (header.gid), statbuf->st_gid);
1136 putOctal (header.size, sizeof (header.size), statbuf->st_size);
1137 putOctal (header.mtime, sizeof (header.mtime), statbuf->st_mtime);
Eric Andersencc8ed391999-10-05 16:24:54 +00001138
Eric Andersen3cf52d11999-10-12 22:26:06 +00001139 header.typeFlag = TAR_TYPE_REGULAR;
Eric Andersencc8ed391999-10-05 16:24:54 +00001140
Eric Andersen3cf52d11999-10-12 22:26:06 +00001141 /*
1142 * Calculate and store the checksum.
1143 * This is the sum of all of the bytes of the header,
1144 * with the checksum field itself treated as blanks.
1145 */
1146 memset (header.checkSum, ' ', sizeof (header.checkSum));
Eric Andersencc8ed391999-10-05 16:24:54 +00001147
Eric Andersen3cf52d11999-10-12 22:26:06 +00001148 cp = (const unsigned char *) &header;
1149 len = sizeof (header);
1150 checkSum = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +00001151
Eric Andersen3cf52d11999-10-12 22:26:06 +00001152 while (len-- > 0)
1153 checkSum += *cp++;
Eric Andersencc8ed391999-10-05 16:24:54 +00001154
Eric Andersen3cf52d11999-10-12 22:26:06 +00001155 putOctal (header.checkSum, sizeof (header.checkSum), checkSum);
Eric Andersencc8ed391999-10-05 16:24:54 +00001156
Eric Andersen3cf52d11999-10-12 22:26:06 +00001157 /*
1158 * Write the tar header.
1159 */
1160 writeTarBlock ((const char *) &header, sizeof (header));
Eric Andersencc8ed391999-10-05 16:24:54 +00001161}
1162
1163
1164/*
1165 * Write data to one or more blocks of the tar file.
1166 * The data is always padded out to a multiple of TAR_BLOCK_SIZE.
1167 * The errorFlag static variable is set on an error.
1168 */
Eric Andersen3cf52d11999-10-12 22:26:06 +00001169static void writeTarBlock (const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +00001170{
Eric Andersen3cf52d11999-10-12 22:26:06 +00001171 int partialLength;
1172 int completeLength;
1173 char fullBlock[TAR_BLOCK_SIZE];
Eric Andersencc8ed391999-10-05 16:24:54 +00001174
Eric Andersen3cf52d11999-10-12 22:26:06 +00001175 /*
1176 * If we had a write error before, then do nothing more.
1177 */
Eric Andersen5556c181999-11-10 19:27:58 +00001178 if (errorFlag==TRUE)
Eric Andersen3cf52d11999-10-12 22:26:06 +00001179 return;
Eric Andersencc8ed391999-10-05 16:24:54 +00001180
Eric Andersen3cf52d11999-10-12 22:26:06 +00001181 /*
1182 * Get the amount of complete and partial blocks.
1183 */
1184 partialLength = len % TAR_BLOCK_SIZE;
1185 completeLength = len - partialLength;
Eric Andersencc8ed391999-10-05 16:24:54 +00001186
Eric Andersen3cf52d11999-10-12 22:26:06 +00001187 /*
1188 * Write all of the complete blocks.
1189 */
1190 if ((completeLength > 0) && !fullWrite (tarFd, buf, completeLength)) {
1191 perror (tarName);
Eric Andersencc8ed391999-10-05 16:24:54 +00001192
Eric Andersen3cf52d11999-10-12 22:26:06 +00001193 errorFlag = TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +00001194
Eric Andersen3cf52d11999-10-12 22:26:06 +00001195 return;
1196 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001197
Eric Andersen3cf52d11999-10-12 22:26:06 +00001198 /*
1199 * If there are no partial blocks left, we are done.
1200 */
1201 if (partialLength == 0)
1202 return;
Eric Andersencc8ed391999-10-05 16:24:54 +00001203
Eric Andersen3cf52d11999-10-12 22:26:06 +00001204 /*
1205 * Copy the partial data into a complete block, and pad the rest
1206 * of it with zeroes.
1207 */
1208 memcpy (fullBlock, buf + completeLength, partialLength);
1209 memset (fullBlock + partialLength, 0, TAR_BLOCK_SIZE - partialLength);
Eric Andersencc8ed391999-10-05 16:24:54 +00001210
Eric Andersen3cf52d11999-10-12 22:26:06 +00001211 /*
1212 * Write the last complete block.
1213 */
1214 if (!fullWrite (tarFd, fullBlock, TAR_BLOCK_SIZE)) {
1215 perror (tarName);
Eric Andersencc8ed391999-10-05 16:24:54 +00001216
Eric Andersen3cf52d11999-10-12 22:26:06 +00001217 errorFlag = TRUE;
1218 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001219}
1220
1221
1222/*
Eric Andersencc8ed391999-10-05 16:24:54 +00001223 * Put an octal string into the specified buffer.
1224 * The number is zero and space padded and possibly null padded.
1225 * Returns TRUE if successful.
1226 */
Eric Andersen3cf52d11999-10-12 22:26:06 +00001227static int putOctal (char *cp, int len, long value)
Eric Andersencc8ed391999-10-05 16:24:54 +00001228{
Eric Andersen3cf52d11999-10-12 22:26:06 +00001229 int tempLength;
1230 char *tempString;
1231 char tempBuffer[32];
Eric Andersencc8ed391999-10-05 16:24:54 +00001232
Eric Andersen3cf52d11999-10-12 22:26:06 +00001233 /*
1234 * Create a string of the specified length with an initial space,
1235 * leading zeroes and the octal number, and a trailing null.
1236 */
1237 tempString = tempBuffer;
Eric Andersencc8ed391999-10-05 16:24:54 +00001238
Eric Andersen3cf52d11999-10-12 22:26:06 +00001239 sprintf (tempString, " %0*lo", len - 2, value);
Eric Andersencc8ed391999-10-05 16:24:54 +00001240
Eric Andersen3cf52d11999-10-12 22:26:06 +00001241 tempLength = strlen (tempString) + 1;
Eric Andersencc8ed391999-10-05 16:24:54 +00001242
Eric Andersen3cf52d11999-10-12 22:26:06 +00001243 /*
1244 * If the string is too large, suppress the leading space.
1245 */
1246 if (tempLength > len) {
1247 tempLength--;
1248 tempString++;
1249 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001250
Eric Andersen3cf52d11999-10-12 22:26:06 +00001251 /*
1252 * If the string is still too large, suppress the trailing null.
1253 */
1254 if (tempLength > len)
1255 tempLength--;
Eric Andersencc8ed391999-10-05 16:24:54 +00001256
Eric Andersen3cf52d11999-10-12 22:26:06 +00001257 /*
1258 * If the string is still too large, fail.
1259 */
1260 if (tempLength > len)
1261 return FALSE;
Eric Andersencc8ed391999-10-05 16:24:54 +00001262
Eric Andersen3cf52d11999-10-12 22:26:06 +00001263 /*
1264 * Copy the string to the field.
1265 */
1266 memcpy (cp, tempString, len);
Eric Andersencc8ed391999-10-05 16:24:54 +00001267
Eric Andersen3cf52d11999-10-12 22:26:06 +00001268 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +00001269}
Erik Andersen05100cd2000-01-16 01:30:52 +00001270#endif
Eric Andersencc8ed391999-10-05 16:24:54 +00001271
Eric Andersencc8ed391999-10-05 16:24:54 +00001272/* END CODE */