blob: c23805993a00d0625708ca8a7f9c5d0e5c7ebe46 [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 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 *
Eric Andersencc8ed391999-10-05 16:24:54 +000027 */
28
29
30#include "internal.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000031#include <stdio.h>
32#include <dirent.h>
33#include <errno.h>
34#include <fcntl.h>
35#include <signal.h>
36#include <time.h>
37
Eric Andersen50d63601999-11-09 01:47:36 +000038/* Note that tar.c expects TRUE and FALSE to be defined
39 * exactly the opposite of how they are used everywhere else.
40 * Some time this should be integrated a bit better, but this
41 * does the job for now.
42 */
Eric Andersen5556c181999-11-10 19:27:58 +000043//#undef FALSE
44//#undef TRUE
45//#define FALSE ((int) 0)
46//#define TRUE ((int) 1)
Eric Andersen50d63601999-11-09 01:47:36 +000047
Eric Andersene77ae3a1999-10-19 20:03:34 +000048
49static const char tar_usage[] =
Eric Andersenbe971d61999-11-03 16:52:50 +000050 "tar -[cxtvOf] [tarFileName] [FILE] ...\n"
51 "Create, extract, or list files from a tar file\n\n"
Eric Andersene77ae3a1999-10-19 20:03:34 +000052 "\tc=create, x=extract, t=list contents, v=verbose,\n"
53 "\tO=extract to stdout, f=tarfile or \"-\" for stdin\n";
54
55
56
Eric Andersencc8ed391999-10-05 16:24:54 +000057/*
58 * Tar file constants.
59 */
60#define TAR_BLOCK_SIZE 512
61#define TAR_NAME_SIZE 100
62
63
64/*
65 * The POSIX (and basic GNU) tar header format.
66 * This structure is always embedded in a TAR_BLOCK_SIZE sized block
67 * with zero padding. We only process this information minimally.
68 */
Eric Andersen3cf52d11999-10-12 22:26:06 +000069typedef struct {
70 char name[TAR_NAME_SIZE];
71 char mode[8];
72 char uid[8];
73 char gid[8];
74 char size[12];
75 char mtime[12];
76 char checkSum[8];
77 char typeFlag;
78 char linkName[TAR_NAME_SIZE];
79 char magic[6];
80 char version[2];
81 char uname[32];
82 char gname[32];
83 char devMajor[8];
84 char devMinor[8];
85 char prefix[155];
Eric Andersencc8ed391999-10-05 16:24:54 +000086} TarHeader;
87
88#define TAR_MAGIC "ustar"
89#define TAR_VERSION "00"
90
91#define TAR_TYPE_REGULAR '0'
92#define TAR_TYPE_HARD_LINK '1'
93#define TAR_TYPE_SOFT_LINK '2'
94
95
96/*
97 * Static data.
98 */
Eric Andersen5556c181999-11-10 19:27:58 +000099static int listFlag; //
100static int extractFlag; //
101static int createFlag; //
102static int verboseFlag; //
103static int tostdoutFlag; //
Eric Andersencc8ed391999-10-05 16:24:54 +0000104
Eric Andersen5556c181999-11-10 19:27:58 +0000105static int inHeader; // <- check me
106static int badHeader; //
107static int errorFlag; //
108static int skipFileFlag; //
109static int warnedRoot; //
110static int eofFlag; //
Eric Andersen3cf52d11999-10-12 22:26:06 +0000111static long dataCc;
112static int outFd;
113static char outName[TAR_NAME_SIZE];
Eric Andersencc8ed391999-10-05 16:24:54 +0000114
115
116/*
117 * Static data associated with the tar file.
118 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000119static const char *tarName;
120static int tarFd;
121static dev_t tarDev;
122static ino_t tarInode;
Eric Andersencc8ed391999-10-05 16:24:54 +0000123
124
125/*
126 * Local procedures to restore files from a tar file.
127 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000128static void readTarFile (int fileCount, char **fileTable);
129static void readData (const char *cp, int count);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000130static long getOctal (const char *cp, int len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000131
Eric Andersen3cf52d11999-10-12 22:26:06 +0000132static void readHeader (const TarHeader * hp,
133 int fileCount, char **fileTable);
Eric Andersencc8ed391999-10-05 16:24:54 +0000134
135
136/*
137 * Local procedures to save files into a tar file.
138 */
Eric Andersen5556c181999-11-10 19:27:58 +0000139static void saveFile (const char *fileName, int seeLinks); //
Eric Andersencc8ed391999-10-05 16:24:54 +0000140
Eric Andersen3cf52d11999-10-12 22:26:06 +0000141static void saveRegularFile (const char *fileName,
142 const struct stat *statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +0000143
Eric Andersen3cf52d11999-10-12 22:26:06 +0000144static void saveDirectory (const char *fileName,
145 const struct stat *statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +0000146
Eric Andersen3cf52d11999-10-12 22:26:06 +0000147static int wantFileName (const char *fileName,
Eric Andersen5556c181999-11-10 19:27:58 +0000148 int fileCount, char **fileTable); //
Eric Andersencc8ed391999-10-05 16:24:54 +0000149
Eric Andersen3cf52d11999-10-12 22:26:06 +0000150static void writeHeader (const char *fileName, const struct stat *statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +0000151
Eric Andersen3cf52d11999-10-12 22:26:06 +0000152static void writeTarFile (int fileCount, char **fileTable);
153static void writeTarBlock (const char *buf, int len);
Eric Andersen5556c181999-11-10 19:27:58 +0000154static int putOctal (char *cp, int len, long value); //
Eric Andersencc8ed391999-10-05 16:24:54 +0000155
156
Eric Andersen3cf52d11999-10-12 22:26:06 +0000157extern int tar_main (int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000158{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000159 const char *options;
Eric Andersencc8ed391999-10-05 16:24:54 +0000160
Eric Andersen3cf52d11999-10-12 22:26:06 +0000161 argc--;
162 argv++;
Eric Andersencc8ed391999-10-05 16:24:54 +0000163
Eric Andersenbe971d61999-11-03 16:52:50 +0000164 if (argc < 1)
165 usage( tar_usage);
Eric Andersencc8ed391999-10-05 16:24:54 +0000166
167
Eric Andersen3cf52d11999-10-12 22:26:06 +0000168 errorFlag = FALSE;
169 extractFlag = FALSE;
170 createFlag = FALSE;
171 listFlag = FALSE;
172 verboseFlag = FALSE;
173 tostdoutFlag = FALSE;
174 tarName = NULL;
175 tarDev = 0;
176 tarInode = 0;
177 tarFd = -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000178
Eric Andersen3cf52d11999-10-12 22:26:06 +0000179 /*
180 * Parse the options.
181 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000182 if (**argv == '-') {
Eric Andersen50d63601999-11-09 01:47:36 +0000183 options = (*argv++) + 1;
184 argc--;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000185 for (; *options; options++) {
186 switch (*options) {
187 case 'f':
188 if (tarName != NULL) {
189 fprintf (stderr, "Only one 'f' option allowed\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000190
Eric Andersen3cf52d11999-10-12 22:26:06 +0000191 exit (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000192 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000193
194 tarName = *argv++;
195 argc--;
196
197 break;
198
199 case 't':
200 listFlag = TRUE;
201 break;
202
203 case 'x':
204 extractFlag = TRUE;
205 break;
206
207 case 'c':
208 createFlag = TRUE;
209 break;
210
211 case 'v':
212 verboseFlag = TRUE;
213 break;
214
215 case 'O':
216 tostdoutFlag = TRUE;
217 break;
218
219 case '-':
220 break;
221
222 default:
223 fprintf (stderr, "Unknown tar flag '%c'\n", *options);
224
225 exit (FALSE);
226 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000227 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000228 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000229
Eric Andersen3cf52d11999-10-12 22:26:06 +0000230 /*
231 * Validate the options.
232 */
Eric Andersen5556c181999-11-10 19:27:58 +0000233 fprintf(stderr, "TRUE=%d FALSE=%d\n", TRUE, FALSE);
234 if (extractFlag + listFlag + createFlag != (TRUE+FALSE+FALSE)) {
Eric Andersen3cf52d11999-10-12 22:26:06 +0000235 fprintf (stderr,
236 "Exactly one of 'c', 'x' or 't' must be specified\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000237
Eric Andersen3cf52d11999-10-12 22:26:06 +0000238 exit (FALSE);
239 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000240
Eric Andersen3cf52d11999-10-12 22:26:06 +0000241 /*
242 * Do the correct type of action supplying the rest of the
243 * command line arguments as the list of files to process.
244 */
Eric Andersen5556c181999-11-10 19:27:58 +0000245 if (createFlag==TRUE)
Eric Andersen3cf52d11999-10-12 22:26:06 +0000246 writeTarFile (argc, argv);
247 else
248 readTarFile (argc, argv);
Eric Andersen5556c181999-11-10 19:27:58 +0000249 if (errorFlag==TRUE)
Eric Andersen3cf52d11999-10-12 22:26:06 +0000250 fprintf (stderr, "\n");
Eric Andersen5556c181999-11-10 19:27:58 +0000251 exit (!errorFlag);
Eric Andersencc8ed391999-10-05 16:24:54 +0000252}
253
254
255/*
256 * Read a tar file and extract or list the specified files within it.
257 * If the list is empty than all files are extracted or listed.
258 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000259static void readTarFile (int fileCount, char **fileTable)
Eric Andersencc8ed391999-10-05 16:24:54 +0000260{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000261 const char *cp;
262 int cc;
263 int inCc;
264 int blockSize;
265 char buf[BUF_SIZE];
Eric Andersencc8ed391999-10-05 16:24:54 +0000266
Eric Andersen3cf52d11999-10-12 22:26:06 +0000267 skipFileFlag = FALSE;
268 badHeader = FALSE;
269 warnedRoot = FALSE;
270 eofFlag = FALSE;
271 inHeader = TRUE;
272 inCc = 0;
273 dataCc = 0;
274 outFd = -1;
275 blockSize = sizeof (buf);
276 cp = buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000277
Eric Andersen3cf52d11999-10-12 22:26:06 +0000278 /*
279 * Open the tar file for reading.
280 */
281 if ((tarName == NULL) || !strcmp (tarName, "-")) {
282 tarFd = STDIN;
283 } else
284 tarFd = open (tarName, O_RDONLY);
285
286 if (tarFd < 0) {
287 perror (tarName);
288 errorFlag = TRUE;
289 return;
290 }
291
292 /*
293 * Read blocks from the file until an end of file header block
294 * has been seen. (A real end of file from a read is an error.)
295 */
Eric Andersen5556c181999-11-10 19:27:58 +0000296 while (eofFlag==FALSE) {
Eric Andersen3cf52d11999-10-12 22:26:06 +0000297 /*
298 * Read the next block of data if necessary.
299 * This will be a large block if possible, which we will
300 * then process in the small tar blocks.
Eric Andersencc8ed391999-10-05 16:24:54 +0000301 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000302 if (inCc <= 0) {
303 cp = buf;
304 inCc = fullRead (tarFd, buf, blockSize);
Eric Andersencc8ed391999-10-05 16:24:54 +0000305
Eric Andersen3cf52d11999-10-12 22:26:06 +0000306 if (inCc < 0) {
307 perror (tarName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000308 errorFlag = TRUE;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000309 goto done;
310 }
311
312 if (inCc == 0) {
313 fprintf (stderr,
314 "Unexpected end of file from \"%s\"", tarName);
315 errorFlag = TRUE;
316 goto done;
317 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000318 }
319
Eric Andersen3cf52d11999-10-12 22:26:06 +0000320 /*
321 * If we are expecting a header block then examine it.
Eric Andersencc8ed391999-10-05 16:24:54 +0000322 */
Eric Andersen5556c181999-11-10 19:27:58 +0000323 if (inHeader==TRUE) {
Eric Andersen3cf52d11999-10-12 22:26:06 +0000324 readHeader ((const TarHeader *) cp, fileCount, fileTable);
Eric Andersencc8ed391999-10-05 16:24:54 +0000325
Eric Andersen3cf52d11999-10-12 22:26:06 +0000326 cp += TAR_BLOCK_SIZE;
327 inCc -= TAR_BLOCK_SIZE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000328
Eric Andersen3cf52d11999-10-12 22:26:06 +0000329 continue;
Eric Andersencc8ed391999-10-05 16:24:54 +0000330 }
331
Eric Andersen3cf52d11999-10-12 22:26:06 +0000332 /*
333 * We are currently handling the data for a file.
334 * Process the minimum of the amount of data we have available
335 * and the amount left to be processed for the file.
Eric Andersencc8ed391999-10-05 16:24:54 +0000336 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000337 cc = inCc;
Eric Andersencc8ed391999-10-05 16:24:54 +0000338
Eric Andersen3cf52d11999-10-12 22:26:06 +0000339 if (cc > dataCc)
340 cc = dataCc;
341
342 readData (cp, cc);
343
344 /*
345 * If the amount left isn't an exact multiple of the tar block
346 * size then round it up to the next block boundary since there
347 * is padding at the end of the file.
Eric Andersencc8ed391999-10-05 16:24:54 +0000348 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000349 if (cc % TAR_BLOCK_SIZE)
350 cc += TAR_BLOCK_SIZE - (cc % TAR_BLOCK_SIZE);
351
352 cp += cc;
353 inCc -= cc;
354 }
355
356 done:
357 /*
358 * Close the tar file if needed.
359 */
360 if ((tarFd >= 0) && (close (tarFd) < 0))
361 perror (tarName);
362
363 /*
364 * Close the output file if needed.
365 * This is only done here on a previous error and so no
366 * message is required on errors.
367 */
368 if (tostdoutFlag == FALSE) {
369 if (outFd >= 0)
370 (void) close (outFd);
371 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000372}
373
374
375/*
376 * Examine the header block that was just read.
377 * This can specify the information for another file, or it can mark
378 * the end of the tar file.
379 */
380static void
Eric Andersen3cf52d11999-10-12 22:26:06 +0000381readHeader (const TarHeader * hp, int fileCount, char **fileTable)
Eric Andersencc8ed391999-10-05 16:24:54 +0000382{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000383 int mode;
384 int uid;
385 int gid;
386 int checkSum;
387 long size;
388 time_t mtime;
389 const char *name;
390 int cc;
391 int hardLink;
392 int softLink;
Eric Andersencc8ed391999-10-05 16:24:54 +0000393
Eric Andersen3cf52d11999-10-12 22:26:06 +0000394 /*
395 * If the block is completely empty, then this is the end of the
396 * archive file. If the name is null, then just skip this header.
397 */
398 name = hp->name;
Eric Andersencc8ed391999-10-05 16:24:54 +0000399
Eric Andersen3cf52d11999-10-12 22:26:06 +0000400 if (*name == '\0') {
401 for (cc = TAR_BLOCK_SIZE; cc > 0; cc--) {
402 if (*name++)
Eric Andersencc8ed391999-10-05 16:24:54 +0000403 return;
404 }
405
Eric Andersen3cf52d11999-10-12 22:26:06 +0000406 eofFlag = TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000407
Eric Andersen3cf52d11999-10-12 22:26:06 +0000408 return;
409 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000410
Eric Andersen3cf52d11999-10-12 22:26:06 +0000411 /*
412 * There is another file in the archive to examine.
413 * Extract the encoded information and check it.
414 */
415 mode = getOctal (hp->mode, sizeof (hp->mode));
416 uid = getOctal (hp->uid, sizeof (hp->uid));
417 gid = getOctal (hp->gid, sizeof (hp->gid));
418 size = getOctal (hp->size, sizeof (hp->size));
419 mtime = getOctal (hp->mtime, sizeof (hp->mtime));
420 checkSum = getOctal (hp->checkSum, sizeof (hp->checkSum));
Eric Andersencc8ed391999-10-05 16:24:54 +0000421
Eric Andersen3cf52d11999-10-12 22:26:06 +0000422 if ((mode < 0) || (uid < 0) || (gid < 0) || (size < 0)) {
Eric Andersen5556c181999-11-10 19:27:58 +0000423 if (badHeader==FALSE)
Eric Andersen3cf52d11999-10-12 22:26:06 +0000424 fprintf (stderr, "Bad tar header, skipping\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000425
Eric Andersen3cf52d11999-10-12 22:26:06 +0000426 badHeader = TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000427
Eric Andersen3cf52d11999-10-12 22:26:06 +0000428 return;
429 }
430
431 badHeader = FALSE;
432 skipFileFlag = FALSE;
433
434 /*
435 * Check for the file modes.
436 */
437 hardLink = ((hp->typeFlag == TAR_TYPE_HARD_LINK) ||
Eric Andersencc8ed391999-10-05 16:24:54 +0000438 (hp->typeFlag == TAR_TYPE_HARD_LINK - '0'));
439
Eric Andersen3cf52d11999-10-12 22:26:06 +0000440 softLink = ((hp->typeFlag == TAR_TYPE_SOFT_LINK) ||
Eric Andersencc8ed391999-10-05 16:24:54 +0000441 (hp->typeFlag == TAR_TYPE_SOFT_LINK - '0'));
442
Eric Andersen3cf52d11999-10-12 22:26:06 +0000443 /*
444 * Check for a directory or a regular file.
445 */
446 if (name[strlen (name) - 1] == '/')
447 mode |= S_IFDIR;
448 else if ((mode & S_IFMT) == 0)
449 mode |= S_IFREG;
Eric Andersencc8ed391999-10-05 16:24:54 +0000450
Eric Andersen3cf52d11999-10-12 22:26:06 +0000451 /*
452 * Check for absolute paths in the file.
453 * If we find any, then warn the user and make them relative.
454 */
455 if (*name == '/') {
456 while (*name == '/')
457 name++;
Eric Andersencc8ed391999-10-05 16:24:54 +0000458
Eric Andersen5556c181999-11-10 19:27:58 +0000459 if (warnedRoot==FALSE) {
Eric Andersen3cf52d11999-10-12 22:26:06 +0000460 fprintf (stderr,
461 "Absolute path detected, removing leading slashes\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000462 }
463
Eric Andersen3cf52d11999-10-12 22:26:06 +0000464 warnedRoot = TRUE;
465 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000466
Eric Andersen3cf52d11999-10-12 22:26:06 +0000467 /*
468 * See if we want this file to be restored.
469 * If not, then set up to skip it.
470 */
Eric Andersen5556c181999-11-10 19:27:58 +0000471 if (wantFileName (name, fileCount, fileTable) == FALSE) {
Eric Andersen3cf52d11999-10-12 22:26:06 +0000472 if (!hardLink && !softLink && S_ISREG (mode)) {
Eric Andersen5556c181999-11-10 19:27:58 +0000473 inHeader = (size == 0)? TRUE : FALSE;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000474 dataCc = size;
Eric Andersencc8ed391999-10-05 16:24:54 +0000475 }
476
Eric Andersen3cf52d11999-10-12 22:26:06 +0000477 skipFileFlag = TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000478
Eric Andersen3cf52d11999-10-12 22:26:06 +0000479 return;
480 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000481
Eric Andersen3cf52d11999-10-12 22:26:06 +0000482 /*
483 * This file is to be handled.
484 * If we aren't extracting then just list information about the file.
485 */
Eric Andersen5556c181999-11-10 19:27:58 +0000486 if (extractFlag==FALSE) {
487 if (verboseFlag==TRUE) {
Eric Andersen3cf52d11999-10-12 22:26:06 +0000488 printf ("%s %3d/%-d %9ld %s %s", modeString (mode),
489 uid, gid, size, timeString (mtime), name);
490 } else
491 printf ("%s", name);
Eric Andersencc8ed391999-10-05 16:24:54 +0000492
493 if (hardLink)
Eric Andersen3cf52d11999-10-12 22:26:06 +0000494 printf (" (link to \"%s\")", hp->linkName);
495 else if (softLink)
496 printf (" (symlink to \"%s\")", hp->linkName);
497 else if (S_ISREG (mode)) {
Eric Andersen5556c181999-11-10 19:27:58 +0000498 inHeader = (size == 0)? TRUE : FALSE;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000499 dataCc = size;
Eric Andersencc8ed391999-10-05 16:24:54 +0000500 }
501
Eric Andersen3cf52d11999-10-12 22:26:06 +0000502 printf ("\n");
503
504 return;
505 }
506
507 /*
508 * We really want to extract the file.
509 */
Eric Andersen5556c181999-11-10 19:27:58 +0000510 if (verboseFlag==TRUE)
Eric Andersen3cf52d11999-10-12 22:26:06 +0000511 printf ("x %s\n", name);
512
513 if (hardLink) {
514 if (link (hp->linkName, name) < 0)
515 perror (name);
516
517 return;
518 }
519
520 if (softLink) {
Eric Andersencc8ed391999-10-05 16:24:54 +0000521#ifdef S_ISLNK
Eric Andersen3cf52d11999-10-12 22:26:06 +0000522 if (symlink (hp->linkName, name) < 0)
523 perror (name);
Eric Andersencc8ed391999-10-05 16:24:54 +0000524#else
Eric Andersen3cf52d11999-10-12 22:26:06 +0000525 fprintf (stderr, "Cannot create symbolic links\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000526#endif
Eric Andersen3cf52d11999-10-12 22:26:06 +0000527 return;
528 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000529
Eric Andersen3cf52d11999-10-12 22:26:06 +0000530 /*
531 * If the file is a directory, then just create the path.
532 */
533 if (S_ISDIR (mode)) {
534 createPath (name, mode);
Eric Andersencc8ed391999-10-05 16:24:54 +0000535
Eric Andersen3cf52d11999-10-12 22:26:06 +0000536 return;
537 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000538
Eric Andersen3cf52d11999-10-12 22:26:06 +0000539 /*
540 * There is a file to write.
541 * First create the path to it if necessary with a default permission.
542 */
543 createPath (name, 0777);
Eric Andersencc8ed391999-10-05 16:24:54 +0000544
Eric Andersen5556c181999-11-10 19:27:58 +0000545 inHeader = (size == 0)? TRUE : FALSE;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000546 dataCc = size;
Eric Andersencc8ed391999-10-05 16:24:54 +0000547
Eric Andersen3cf52d11999-10-12 22:26:06 +0000548 /*
549 * Start the output file.
550 */
551 if (tostdoutFlag == TRUE)
552 outFd = STDOUT;
553 else
554 outFd = open (name, O_WRONLY | O_CREAT | O_TRUNC, mode);
Eric Andersencc8ed391999-10-05 16:24:54 +0000555
Eric Andersen3cf52d11999-10-12 22:26:06 +0000556 if (outFd < 0) {
557 perror (name);
558 skipFileFlag = TRUE;
559 return;
560 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000561
Eric Andersen3cf52d11999-10-12 22:26:06 +0000562 /*
563 * If the file is empty, then that's all we need to do.
564 */
565 if (size == 0 && tostdoutFlag == FALSE) {
566 (void) close (outFd);
567 outFd = -1;
568 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000569}
570
571
572/*
573 * Handle a data block of some specified size that was read.
574 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000575static void readData (const char *cp, int count)
Eric Andersencc8ed391999-10-05 16:24:54 +0000576{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000577 /*
578 * Reduce the amount of data left in this file.
579 * If there is no more data left, then we need to read
580 * the header again.
581 */
582 dataCc -= count;
Eric Andersencc8ed391999-10-05 16:24:54 +0000583
Eric Andersen3cf52d11999-10-12 22:26:06 +0000584 if (dataCc <= 0)
585 inHeader = TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000586
Eric Andersen3cf52d11999-10-12 22:26:06 +0000587 /*
588 * If we aren't extracting files or this file is being
589 * skipped then do nothing more.
590 */
Eric Andersen5556c181999-11-10 19:27:58 +0000591 if (extractFlag==FALSE || skipFileFlag==TRUE)
Eric Andersen3cf52d11999-10-12 22:26:06 +0000592 return;
Eric Andersencc8ed391999-10-05 16:24:54 +0000593
Eric Andersen3cf52d11999-10-12 22:26:06 +0000594 /*
595 * Write the data to the output file.
596 */
597 if (fullWrite (outFd, cp, count) < 0) {
598 perror (outName);
599 if (tostdoutFlag == FALSE) {
600 (void) close (outFd);
601 outFd = -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000602 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000603 skipFileFlag = TRUE;
604 return;
605 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000606
Eric Andersen3cf52d11999-10-12 22:26:06 +0000607 /*
608 * If the write failed, close the file and disable further
609 * writes to this file.
610 */
611 if (dataCc <= 0 && tostdoutFlag == FALSE) {
612 if (close (outFd))
613 perror (outName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000614
Eric Andersen3cf52d11999-10-12 22:26:06 +0000615 outFd = -1;
616 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000617}
618
619
620/*
621 * Write a tar file containing the specified files.
622 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000623static void writeTarFile (int fileCount, char **fileTable)
Eric Andersencc8ed391999-10-05 16:24:54 +0000624{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000625 struct stat statbuf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000626
Eric Andersen3cf52d11999-10-12 22:26:06 +0000627 /*
628 * Make sure there is at least one file specified.
629 */
630 if (fileCount <= 0) {
631 fprintf (stderr, "No files specified to be saved\n");
632 errorFlag = TRUE;
633 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000634
Eric Andersen3cf52d11999-10-12 22:26:06 +0000635 /*
636 * Create the tar file for writing.
637 */
638 if ((tarName == NULL) || !strcmp (tarName, "-")) {
639 tostdoutFlag = TRUE;
640 tarFd = STDOUT;
641 } else
642 tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0666);
Eric Andersencc8ed391999-10-05 16:24:54 +0000643
Eric Andersen3cf52d11999-10-12 22:26:06 +0000644 if (tarFd < 0) {
645 perror (tarName);
646 errorFlag = TRUE;
647 return;
648 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000649
Eric Andersen3cf52d11999-10-12 22:26:06 +0000650 /*
651 * Get the device and inode of the tar file for checking later.
652 */
653 if (fstat (tarFd, &statbuf) < 0) {
654 perror (tarName);
655 errorFlag = TRUE;
656 goto done;
657 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000658
Eric Andersen3cf52d11999-10-12 22:26:06 +0000659 tarDev = statbuf.st_dev;
660 tarInode = statbuf.st_ino;
Eric Andersencc8ed391999-10-05 16:24:54 +0000661
Eric Andersen3cf52d11999-10-12 22:26:06 +0000662 /*
663 * Append each file name into the archive file.
664 * Follow symbolic links for these top level file names.
665 */
Eric Andersen5556c181999-11-10 19:27:58 +0000666 while (errorFlag==FALSE && (fileCount-- > 0)) {
Eric Andersen3cf52d11999-10-12 22:26:06 +0000667 saveFile (*fileTable++, FALSE);
668 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000669
Eric Andersen3cf52d11999-10-12 22:26:06 +0000670 /*
671 * Now write an empty block of zeroes to end the archive.
672 */
673 writeTarBlock ("", 1);
Eric Andersencc8ed391999-10-05 16:24:54 +0000674
675
Eric Andersen3cf52d11999-10-12 22:26:06 +0000676 done:
677 /*
678 * Close the tar file and check for errors if it was opened.
679 */
680 if ((tostdoutFlag == FALSE) && (tarFd >= 0) && (close (tarFd) < 0))
681 perror (tarName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000682}
683
684
685/*
686 * Save one file into the tar file.
687 * If the file is a directory, then this will recursively save all of
688 * the files and directories within the directory. The seeLinks
689 * flag indicates whether or not we want to see symbolic links as
690 * they really are, instead of blindly following them.
691 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000692static void saveFile (const char *fileName, int seeLinks)
Eric Andersencc8ed391999-10-05 16:24:54 +0000693{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000694 int status;
695 int mode;
696 struct stat statbuf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000697
Eric Andersen5556c181999-11-10 19:27:58 +0000698 if (verboseFlag==TRUE)
Eric Andersen3cf52d11999-10-12 22:26:06 +0000699 printf ("a %s\n", fileName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000700
Eric Andersen3cf52d11999-10-12 22:26:06 +0000701 /*
702 * Check that the file name will fit in the header.
703 */
704 if (strlen (fileName) >= TAR_NAME_SIZE) {
705 fprintf (stderr, "%s: File name is too long\n", fileName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000706
Eric Andersen3cf52d11999-10-12 22:26:06 +0000707 return;
708 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000709
Eric Andersen3cf52d11999-10-12 22:26:06 +0000710 /*
711 * Find out about the file.
712 */
Eric Andersencc8ed391999-10-05 16:24:54 +0000713#ifdef S_ISLNK
Eric Andersen5556c181999-11-10 19:27:58 +0000714 if (seeLinks==TRUE)
Eric Andersen3cf52d11999-10-12 22:26:06 +0000715 status = lstat (fileName, &statbuf);
716 else
Eric Andersencc8ed391999-10-05 16:24:54 +0000717#endif
Eric Andersen3cf52d11999-10-12 22:26:06 +0000718 status = stat (fileName, &statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +0000719
Eric Andersen3cf52d11999-10-12 22:26:06 +0000720 if (status < 0) {
721 perror (fileName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000722
Eric Andersen3cf52d11999-10-12 22:26:06 +0000723 return;
724 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000725
Eric Andersen3cf52d11999-10-12 22:26:06 +0000726 /*
727 * Make sure we aren't trying to save our file into itself.
728 */
729 if ((statbuf.st_dev == tarDev) && (statbuf.st_ino == tarInode)) {
730 fprintf (stderr, "Skipping saving of archive file itself\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000731
Eric Andersen3cf52d11999-10-12 22:26:06 +0000732 return;
733 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000734
Eric Andersen3cf52d11999-10-12 22:26:06 +0000735 /*
736 * Check the type of file.
737 */
738 mode = statbuf.st_mode;
Eric Andersencc8ed391999-10-05 16:24:54 +0000739
Eric Andersen3cf52d11999-10-12 22:26:06 +0000740 if (S_ISDIR (mode)) {
741 saveDirectory (fileName, &statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +0000742
Eric Andersen3cf52d11999-10-12 22:26:06 +0000743 return;
744 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000745
Eric Andersen3cf52d11999-10-12 22:26:06 +0000746 if (S_ISREG (mode)) {
747 saveRegularFile (fileName, &statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +0000748
Eric Andersen3cf52d11999-10-12 22:26:06 +0000749 return;
750 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000751
Eric Andersen3cf52d11999-10-12 22:26:06 +0000752 /*
753 * The file is a strange type of file, ignore it.
754 */
755 fprintf (stderr, "%s: not a directory or regular file\n", fileName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000756}
757
758
759/*
760 * Save a regular file to the tar file.
761 */
762static void
Eric Andersen3cf52d11999-10-12 22:26:06 +0000763saveRegularFile (const char *fileName, const struct stat *statbuf)
Eric Andersencc8ed391999-10-05 16:24:54 +0000764{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000765 int sawEof;
766 int fileFd;
767 int cc;
768 int dataCount;
769 long fullDataCount;
770 char data[TAR_BLOCK_SIZE * 16];
Eric Andersencc8ed391999-10-05 16:24:54 +0000771
Eric Andersen3cf52d11999-10-12 22:26:06 +0000772 /*
773 * Open the file for reading.
774 */
775 fileFd = open (fileName, O_RDONLY);
776
777 if (fileFd < 0) {
778 perror (fileName);
779
780 return;
781 }
782
783 /*
784 * Write out the header for the file.
785 */
786 writeHeader (fileName, statbuf);
787
788 /*
789 * Write the data blocks of the file.
790 * We must be careful to write the amount of data that the stat
791 * buffer indicated, even if the file has changed size. Otherwise
792 * the tar file will be incorrect.
793 */
794 fullDataCount = statbuf->st_size;
795 sawEof = FALSE;
796
797 while (fullDataCount > 0) {
798 /*
799 * Get the amount to write this iteration which is
800 * the minumum of the amount left to write and the
801 * buffer size.
Eric Andersencc8ed391999-10-05 16:24:54 +0000802 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000803 dataCount = sizeof (data);
Eric Andersencc8ed391999-10-05 16:24:54 +0000804
Eric Andersen3cf52d11999-10-12 22:26:06 +0000805 if (dataCount > fullDataCount)
806 dataCount = (int) fullDataCount;
807
808 /*
809 * Read the data from the file if we haven't seen the
810 * end of file yet.
811 */
812 cc = 0;
813
Eric Andersen5556c181999-11-10 19:27:58 +0000814 if (sawEof==FALSE) {
Eric Andersen3cf52d11999-10-12 22:26:06 +0000815 cc = fullRead (fileFd, data, dataCount);
816
817 if (cc < 0) {
818 perror (fileName);
819
820 (void) close (fileFd);
821 errorFlag = TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000822
823 return;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000824 }
825
826 /*
827 * If the file ended too soon, complain and set
828 * a flag so we will zero fill the rest of it.
829 */
830 if (cc < dataCount) {
831 fprintf (stderr,
832 "%s: Short read - zero filling", fileName);
833
834 sawEof = TRUE;
835 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000836 }
837
Eric Andersen3cf52d11999-10-12 22:26:06 +0000838 /*
839 * Zero fill the rest of the data if necessary.
Eric Andersencc8ed391999-10-05 16:24:54 +0000840 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000841 if (cc < dataCount)
842 memset (data + cc, 0, dataCount - cc);
Eric Andersencc8ed391999-10-05 16:24:54 +0000843
Eric Andersen3cf52d11999-10-12 22:26:06 +0000844 /*
845 * Write the buffer to the TAR file.
Eric Andersencc8ed391999-10-05 16:24:54 +0000846 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000847 writeTarBlock (data, dataCount);
Eric Andersencc8ed391999-10-05 16:24:54 +0000848
Eric Andersen3cf52d11999-10-12 22:26:06 +0000849 fullDataCount -= dataCount;
850 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000851
Eric Andersen3cf52d11999-10-12 22:26:06 +0000852 /*
853 * Close the file.
854 */
855 if ((tostdoutFlag == FALSE) && close (fileFd) < 0)
856 fprintf (stderr, "%s: close: %s\n", fileName, strerror (errno));
Eric Andersencc8ed391999-10-05 16:24:54 +0000857}
858
859
860/*
861 * Save a directory and all of its files to the tar file.
862 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000863static void saveDirectory (const char *dirName, const struct stat *statbuf)
Eric Andersencc8ed391999-10-05 16:24:54 +0000864{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000865 DIR *dir;
866 struct dirent *entry;
867 int needSlash;
Eric Andersen1b61f411999-10-13 18:56:42 +0000868 char fullName[NAME_MAX];
Eric Andersencc8ed391999-10-05 16:24:54 +0000869
Eric Andersen3cf52d11999-10-12 22:26:06 +0000870 /*
871 * Construct the directory name as used in the tar file by appending
872 * a slash character to it.
873 */
874 strcpy (fullName, dirName);
875 strcat (fullName, "/");
Eric Andersencc8ed391999-10-05 16:24:54 +0000876
Eric Andersen3cf52d11999-10-12 22:26:06 +0000877 /*
878 * Write out the header for the directory entry.
879 */
880 writeHeader (fullName, statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +0000881
Eric Andersen3cf52d11999-10-12 22:26:06 +0000882 /*
883 * Open the directory.
884 */
885 dir = opendir (dirName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000886
Eric Andersen3cf52d11999-10-12 22:26:06 +0000887 if (dir == NULL) {
888 fprintf (stderr, "Cannot read directory \"%s\": %s\n",
889 dirName, strerror (errno));
Eric Andersencc8ed391999-10-05 16:24:54 +0000890
Eric Andersen3cf52d11999-10-12 22:26:06 +0000891 return;
892 }
893
894 /*
895 * See if a slash is needed.
896 */
897 needSlash = (*dirName && (dirName[strlen (dirName) - 1] != '/'));
898
899 /*
900 * Read all of the directory entries and check them,
901 * except for the current and parent directory entries.
902 */
Eric Andersen5556c181999-11-10 19:27:58 +0000903 while (errorFlag==FALSE && ((entry = readdir (dir)) != NULL)) {
Eric Andersen3cf52d11999-10-12 22:26:06 +0000904 if ((strcmp (entry->d_name, ".") == 0) ||
905 (strcmp (entry->d_name, "..") == 0)) {
906 continue;
Eric Andersencc8ed391999-10-05 16:24:54 +0000907 }
908
Eric Andersen3cf52d11999-10-12 22:26:06 +0000909 /*
910 * Build the full path name to the file.
Eric Andersencc8ed391999-10-05 16:24:54 +0000911 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000912 strcpy (fullName, dirName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000913
Eric Andersen3cf52d11999-10-12 22:26:06 +0000914 if (needSlash)
915 strcat (fullName, "/");
916
917 strcat (fullName, entry->d_name);
918
919 /*
920 * Write this file to the tar file, noticing whether or not
921 * the file is a symbolic link.
Eric Andersencc8ed391999-10-05 16:24:54 +0000922 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000923 saveFile (fullName, TRUE);
924 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000925
Eric Andersen3cf52d11999-10-12 22:26:06 +0000926 /*
927 * All done, close the directory.
928 */
929 closedir (dir);
Eric Andersencc8ed391999-10-05 16:24:54 +0000930}
931
932
933/*
934 * Write a tar header for the specified file name and status.
935 * It is assumed that the file name fits.
936 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000937static void writeHeader (const char *fileName, const struct stat *statbuf)
Eric Andersencc8ed391999-10-05 16:24:54 +0000938{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000939 long checkSum;
940 const unsigned char *cp;
941 int len;
942 TarHeader header;
Eric Andersencc8ed391999-10-05 16:24:54 +0000943
Eric Andersen3cf52d11999-10-12 22:26:06 +0000944 /*
945 * Zero the header block in preparation for filling it in.
946 */
947 memset ((char *) &header, 0, sizeof (header));
Eric Andersencc8ed391999-10-05 16:24:54 +0000948
Eric Andersen3cf52d11999-10-12 22:26:06 +0000949 /*
950 * Fill in the header.
951 */
952 strcpy (header.name, fileName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000953
Eric Andersen3cf52d11999-10-12 22:26:06 +0000954 strncpy (header.magic, TAR_MAGIC, sizeof (header.magic));
955 strncpy (header.version, TAR_VERSION, sizeof (header.version));
Eric Andersencc8ed391999-10-05 16:24:54 +0000956
Eric Andersen3cf52d11999-10-12 22:26:06 +0000957 putOctal (header.mode, sizeof (header.mode), statbuf->st_mode & 0777);
958 putOctal (header.uid, sizeof (header.uid), statbuf->st_uid);
959 putOctal (header.gid, sizeof (header.gid), statbuf->st_gid);
960 putOctal (header.size, sizeof (header.size), statbuf->st_size);
961 putOctal (header.mtime, sizeof (header.mtime), statbuf->st_mtime);
Eric Andersencc8ed391999-10-05 16:24:54 +0000962
Eric Andersen3cf52d11999-10-12 22:26:06 +0000963 header.typeFlag = TAR_TYPE_REGULAR;
Eric Andersencc8ed391999-10-05 16:24:54 +0000964
Eric Andersen3cf52d11999-10-12 22:26:06 +0000965 /*
966 * Calculate and store the checksum.
967 * This is the sum of all of the bytes of the header,
968 * with the checksum field itself treated as blanks.
969 */
970 memset (header.checkSum, ' ', sizeof (header.checkSum));
Eric Andersencc8ed391999-10-05 16:24:54 +0000971
Eric Andersen3cf52d11999-10-12 22:26:06 +0000972 cp = (const unsigned char *) &header;
973 len = sizeof (header);
974 checkSum = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000975
Eric Andersen3cf52d11999-10-12 22:26:06 +0000976 while (len-- > 0)
977 checkSum += *cp++;
Eric Andersencc8ed391999-10-05 16:24:54 +0000978
Eric Andersen3cf52d11999-10-12 22:26:06 +0000979 putOctal (header.checkSum, sizeof (header.checkSum), checkSum);
Eric Andersencc8ed391999-10-05 16:24:54 +0000980
Eric Andersen3cf52d11999-10-12 22:26:06 +0000981 /*
982 * Write the tar header.
983 */
984 writeTarBlock ((const char *) &header, sizeof (header));
Eric Andersencc8ed391999-10-05 16:24:54 +0000985}
986
987
988/*
989 * Write data to one or more blocks of the tar file.
990 * The data is always padded out to a multiple of TAR_BLOCK_SIZE.
991 * The errorFlag static variable is set on an error.
992 */
Eric Andersen3cf52d11999-10-12 22:26:06 +0000993static void writeTarBlock (const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000994{
Eric Andersen3cf52d11999-10-12 22:26:06 +0000995 int partialLength;
996 int completeLength;
997 char fullBlock[TAR_BLOCK_SIZE];
Eric Andersencc8ed391999-10-05 16:24:54 +0000998
Eric Andersen3cf52d11999-10-12 22:26:06 +0000999 /*
1000 * If we had a write error before, then do nothing more.
1001 */
Eric Andersen5556c181999-11-10 19:27:58 +00001002 if (errorFlag==TRUE)
Eric Andersen3cf52d11999-10-12 22:26:06 +00001003 return;
Eric Andersencc8ed391999-10-05 16:24:54 +00001004
Eric Andersen3cf52d11999-10-12 22:26:06 +00001005 /*
1006 * Get the amount of complete and partial blocks.
1007 */
1008 partialLength = len % TAR_BLOCK_SIZE;
1009 completeLength = len - partialLength;
Eric Andersencc8ed391999-10-05 16:24:54 +00001010
Eric Andersen3cf52d11999-10-12 22:26:06 +00001011 /*
1012 * Write all of the complete blocks.
1013 */
1014 if ((completeLength > 0) && !fullWrite (tarFd, buf, completeLength)) {
1015 perror (tarName);
Eric Andersencc8ed391999-10-05 16:24:54 +00001016
Eric Andersen3cf52d11999-10-12 22:26:06 +00001017 errorFlag = TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +00001018
Eric Andersen3cf52d11999-10-12 22:26:06 +00001019 return;
1020 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001021
Eric Andersen3cf52d11999-10-12 22:26:06 +00001022 /*
1023 * If there are no partial blocks left, we are done.
1024 */
1025 if (partialLength == 0)
1026 return;
Eric Andersencc8ed391999-10-05 16:24:54 +00001027
Eric Andersen3cf52d11999-10-12 22:26:06 +00001028 /*
1029 * Copy the partial data into a complete block, and pad the rest
1030 * of it with zeroes.
1031 */
1032 memcpy (fullBlock, buf + completeLength, partialLength);
1033 memset (fullBlock + partialLength, 0, TAR_BLOCK_SIZE - partialLength);
Eric Andersencc8ed391999-10-05 16:24:54 +00001034
Eric Andersen3cf52d11999-10-12 22:26:06 +00001035 /*
1036 * Write the last complete block.
1037 */
1038 if (!fullWrite (tarFd, fullBlock, TAR_BLOCK_SIZE)) {
1039 perror (tarName);
Eric Andersencc8ed391999-10-05 16:24:54 +00001040
Eric Andersen3cf52d11999-10-12 22:26:06 +00001041 errorFlag = TRUE;
1042 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001043}
1044
1045
1046/*
Eric Andersencc8ed391999-10-05 16:24:54 +00001047 * Read an octal value in a field of the specified width, with optional
1048 * spaces on both sides of the number and with an optional null character
1049 * at the end. Returns -1 on an illegal format.
1050 */
Eric Andersen3cf52d11999-10-12 22:26:06 +00001051static long getOctal (const char *cp, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +00001052{
Eric Andersen3cf52d11999-10-12 22:26:06 +00001053 long val;
Eric Andersencc8ed391999-10-05 16:24:54 +00001054
Eric Andersen3cf52d11999-10-12 22:26:06 +00001055 while ((len > 0) && (*cp == ' ')) {
1056 cp++;
1057 len--;
1058 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001059
Eric Andersen3cf52d11999-10-12 22:26:06 +00001060 if ((len == 0) || !isOctal (*cp))
1061 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +00001062
Eric Andersen3cf52d11999-10-12 22:26:06 +00001063 val = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +00001064
Eric Andersen3cf52d11999-10-12 22:26:06 +00001065 while ((len > 0) && isOctal (*cp)) {
1066 val = val * 8 + *cp++ - '0';
1067 len--;
1068 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001069
Eric Andersen3cf52d11999-10-12 22:26:06 +00001070 while ((len > 0) && (*cp == ' ')) {
1071 cp++;
1072 len--;
1073 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001074
Eric Andersen3cf52d11999-10-12 22:26:06 +00001075 if ((len > 0) && *cp)
1076 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +00001077
Eric Andersen3cf52d11999-10-12 22:26:06 +00001078 return val;
Eric Andersencc8ed391999-10-05 16:24:54 +00001079}
1080
1081
1082/*
1083 * Put an octal string into the specified buffer.
1084 * The number is zero and space padded and possibly null padded.
1085 * Returns TRUE if successful.
1086 */
Eric Andersen3cf52d11999-10-12 22:26:06 +00001087static int putOctal (char *cp, int len, long value)
Eric Andersencc8ed391999-10-05 16:24:54 +00001088{
Eric Andersen3cf52d11999-10-12 22:26:06 +00001089 int tempLength;
1090 char *tempString;
1091 char tempBuffer[32];
Eric Andersencc8ed391999-10-05 16:24:54 +00001092
Eric Andersen3cf52d11999-10-12 22:26:06 +00001093 /*
1094 * Create a string of the specified length with an initial space,
1095 * leading zeroes and the octal number, and a trailing null.
1096 */
1097 tempString = tempBuffer;
Eric Andersencc8ed391999-10-05 16:24:54 +00001098
Eric Andersen3cf52d11999-10-12 22:26:06 +00001099 sprintf (tempString, " %0*lo", len - 2, value);
Eric Andersencc8ed391999-10-05 16:24:54 +00001100
Eric Andersen3cf52d11999-10-12 22:26:06 +00001101 tempLength = strlen (tempString) + 1;
Eric Andersencc8ed391999-10-05 16:24:54 +00001102
Eric Andersen3cf52d11999-10-12 22:26:06 +00001103 /*
1104 * If the string is too large, suppress the leading space.
1105 */
1106 if (tempLength > len) {
1107 tempLength--;
1108 tempString++;
1109 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001110
Eric Andersen3cf52d11999-10-12 22:26:06 +00001111 /*
1112 * If the string is still too large, suppress the trailing null.
1113 */
1114 if (tempLength > len)
1115 tempLength--;
Eric Andersencc8ed391999-10-05 16:24:54 +00001116
Eric Andersen3cf52d11999-10-12 22:26:06 +00001117 /*
1118 * If the string is still too large, fail.
1119 */
1120 if (tempLength > len)
1121 return FALSE;
Eric Andersencc8ed391999-10-05 16:24:54 +00001122
Eric Andersen3cf52d11999-10-12 22:26:06 +00001123 /*
1124 * Copy the string to the field.
1125 */
1126 memcpy (cp, tempString, len);
Eric Andersencc8ed391999-10-05 16:24:54 +00001127
Eric Andersen3cf52d11999-10-12 22:26:06 +00001128 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +00001129}
1130
1131
1132/*
1133 * See if the specified file name belongs to one of the specified list
1134 * of path prefixes. An empty list implies that all files are wanted.
1135 * Returns TRUE if the file is selected.
1136 */
Eric Andersenf811e071999-10-09 00:25:00 +00001137static int
Eric Andersen3cf52d11999-10-12 22:26:06 +00001138wantFileName (const char *fileName, int fileCount, char **fileTable)
Eric Andersencc8ed391999-10-05 16:24:54 +00001139{
Eric Andersen3cf52d11999-10-12 22:26:06 +00001140 const char *pathName;
1141 int fileLength;
1142 int pathLength;
Eric Andersencc8ed391999-10-05 16:24:54 +00001143
Eric Andersen3cf52d11999-10-12 22:26:06 +00001144 /*
1145 * If there are no files in the list, then the file is wanted.
1146 */
1147 if (fileCount == 0)
1148 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +00001149
Eric Andersen3cf52d11999-10-12 22:26:06 +00001150 fileLength = strlen (fileName);
Eric Andersencc8ed391999-10-05 16:24:54 +00001151
Eric Andersen3cf52d11999-10-12 22:26:06 +00001152 /*
1153 * Check each of the test paths.
1154 */
1155 while (fileCount-- > 0) {
1156 pathName = *fileTable++;
Eric Andersencc8ed391999-10-05 16:24:54 +00001157
Eric Andersen3cf52d11999-10-12 22:26:06 +00001158 pathLength = strlen (pathName);
Eric Andersencc8ed391999-10-05 16:24:54 +00001159
Eric Andersen3cf52d11999-10-12 22:26:06 +00001160 if (fileLength < pathLength)
1161 continue;
Eric Andersencc8ed391999-10-05 16:24:54 +00001162
Eric Andersen3cf52d11999-10-12 22:26:06 +00001163 if (memcmp (fileName, pathName, pathLength) != 0)
1164 continue;
Eric Andersencc8ed391999-10-05 16:24:54 +00001165
Eric Andersen3cf52d11999-10-12 22:26:06 +00001166 if ((fileLength == pathLength) || (fileName[pathLength] == '/')) {
1167 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +00001168 }
Eric Andersen3cf52d11999-10-12 22:26:06 +00001169 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001170
Eric Andersen3cf52d11999-10-12 22:26:06 +00001171 return FALSE;
Eric Andersencc8ed391999-10-05 16:24:54 +00001172}
1173
1174
1175
Eric Andersencc8ed391999-10-05 16:24:54 +00001176/* END CODE */