e1beee117927c54b36e728c0930a2bca33605f86
[oweals/busybox.git] / archival / tar.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini tar implementation for busybox 
4  *
5  * Note, that as of BusyBox-0.43, tar has been completely rewritten from the
6  * ground up.  It still has remnents of the old code lying about, but it is
7  * very different now (i.e. cleaner, less global variables, etc)
8  *
9  * Copyright (C) 2000 by Lineo, inc.
10  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
11  *
12  * Based in part in the tar implementation in sash
13  *  Copyright (c) 1999 by David I. Bell
14  *  Permission is granted to use, distribute, or modify this source,
15  *  provided that this copyright notice remains intact.
16  *  Permission to distribute sash derived code under the GPL has been granted.
17  *
18  * Based in part on the tar implementation from busybox-0.28
19  *  Copyright (C) 1995 Bruce Perens
20  *  This is free software under the GNU General Public License.
21  *
22  * This program is free software; you can redistribute it and/or modify
23  * it under the terms of the GNU General Public License as published by
24  * the Free Software Foundation; either version 2 of the License, or
25  * (at your option) any later version.
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30  * General Public License for more details.
31  *
32  * You should have received a copy of the GNU General Public License
33  * along with this program; if not, write to the Free Software
34  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35  *
36  */
37
38
39 #include "busybox.h"
40 #define BB_DECLARE_EXTERN
41 #define bb_need_io_error
42 #define bb_need_name_longer_than_foo
43 #include "messages.c"
44 #include <stdio.h>
45 #include <dirent.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <signal.h>
49 #include <time.h>
50 #include <utime.h>
51 #include <sys/types.h>
52 #include <sys/sysmacros.h>
53 #include <getopt.h>
54
55 #ifdef BB_FEATURE_TAR_GZIP
56 extern int unzip(int in, int out);
57 extern int gunzip_init();
58 #endif
59
60 /* Tar file constants  */
61 #ifndef MAJOR
62 #define MAJOR(dev) (((dev)>>8)&0xff)
63 #define MINOR(dev) ((dev)&0xff)
64 #endif
65
66 #define NAME_SIZE       100
67
68 /* POSIX tar Header Block, from POSIX 1003.1-1990  */
69 struct TarHeader
70 {
71                                 /* byte offset */
72         char name[NAME_SIZE];         /*   0-99 */
73         char mode[8];                 /* 100-107 */
74         char uid[8];                  /* 108-115 */
75         char gid[8];                  /* 116-123 */
76         char size[12];                /* 124-135 */
77         char mtime[12];               /* 136-147 */
78         char chksum[8];               /* 148-155 */
79         char typeflag;                /* 156-156 */
80         char linkname[NAME_SIZE];     /* 157-256 */
81         char magic[6];                /* 257-262 */
82         char version[2];              /* 263-264 */
83         char uname[32];               /* 265-296 */
84         char gname[32];               /* 297-328 */
85         char devmajor[8];             /* 329-336 */
86         char devminor[8];             /* 337-344 */
87         char prefix[155];             /* 345-499 */
88         char padding[12];             /* 500-512 (pad to exactly the TAR_BLOCK_SIZE) */
89 };
90 typedef struct TarHeader TarHeader;
91
92
93 /* A few useful constants */
94 #define TAR_MAGIC          "ustar"        /* ustar and a null */
95 #define TAR_VERSION        "  "           /* Be compatable with GNU tar format */
96 #define TAR_MAGIC_LEN       6
97 #define TAR_VERSION_LEN     2
98 #define TAR_BLOCK_SIZE      512
99
100 /* A nice enum with all the possible tar file content types */
101 enum TarFileType 
102 {
103         REGTYPE  = '0',            /* regular file */
104         REGTYPE0 = '\0',           /* regular file (ancient bug compat)*/
105         LNKTYPE  = '1',            /* hard link */
106         SYMTYPE  = '2',            /* symbolic link */
107         CHRTYPE  = '3',            /* character special */
108         BLKTYPE  = '4',            /* block special */
109         DIRTYPE  = '5',            /* directory */
110         FIFOTYPE = '6',            /* FIFO special */
111         CONTTYPE = '7',            /* reserved */
112         GNULONGLINK = 'K',         /* GNU long (>100 chars) link name */
113         GNULONGNAME = 'L',         /* GNU long (>100 chars) file name */
114 };
115 typedef enum TarFileType TarFileType;
116
117 /* This struct ignores magic, non-numeric user name, 
118  * non-numeric group name, and the checksum, since
119  * these are all ignored by BusyBox tar. */ 
120 struct TarInfo
121 {
122         int              tarFd;          /* An open file descriptor for reading from the tarball */
123         char *           name;           /* File name */
124         mode_t           mode;           /* Unix mode, including device bits. */
125         uid_t            uid;            /* Numeric UID */
126         gid_t            gid;            /* Numeric GID */
127         size_t           size;           /* Size of file */
128         time_t           mtime;          /* Last-modified time */
129         enum TarFileType type;           /* Regular, directory, link, etc */
130         char *           linkname;       /* Name for symbolic and hard links */
131         long             devmajor;       /* Major number for special device */
132         long             devminor;       /* Minor number for special device */
133 };
134 typedef struct TarInfo TarInfo;
135
136 /* Local procedures to restore files from a tar file.  */
137 static int readTarFile(int tarFd, int extractFlag, int listFlag, 
138                 int tostdoutFlag, int verboseFlag, char** extractList,
139                 char** excludeList);
140
141 #ifdef BB_FEATURE_TAR_CREATE
142 /* Local procedures to save files into a tar file.  */
143 static int writeTarFile(const char* tarName, int verboseFlag, char **argv,
144                 char** excludeList);
145 #endif
146
147 #ifdef BB_FEATURE_TAR_GZIP
148 /* Signal handler for when child gzip process dies...  */
149 void child_died()
150 {
151         fflush(stdout);
152         fflush(stderr);
153         exit(EXIT_FAILURE);
154 }
155
156 static int tar_unzip_init(int tarFd)
157 {
158         int child_pid;
159         static int unzip_pipe[2];
160         /* Cope if child dies... Otherwise we block forever in read()... */
161         signal(SIGCHLD, child_died);
162
163         if (pipe(unzip_pipe)!=0)
164                 error_msg_and_die("pipe error\n");
165                         
166         if ( (child_pid = fork()) == -1)
167                 error_msg_and_die("fork failure\n");
168
169         if (child_pid==0) {
170                 /* child process */
171                 gunzip_init();
172                 unzip(tarFd, unzip_pipe[1]);
173                 exit(EXIT_SUCCESS);
174         }
175         else
176                 /* return fd of uncompressed data to parent process */
177                 return(unzip_pipe[0]);
178 }
179 #endif
180
181 extern int tar_main(int argc, char **argv)
182 {
183         char** excludeList=NULL;
184         char** extractList=NULL;
185         const char *tarName="-";
186 #if defined BB_FEATURE_TAR_EXCLUDE
187         int excludeListSize=0;
188         char *excludeFileName ="-";
189         FILE *fileList;
190         char file[256];
191 #endif
192 #if defined BB_FEATURE_TAR_GZIP
193         int unzipFlag    = FALSE;
194 #endif
195         int listFlag     = FALSE;
196         int extractFlag  = FALSE;
197         int createFlag   = FALSE;
198         int verboseFlag  = FALSE;
199         int tostdoutFlag = FALSE;
200         int status       = FALSE;
201         int firstOpt     = TRUE;
202         int stopIt;
203
204         if (argc <= 1)
205                 usage(tar_usage);
206
207         while (*(++argv) && (**argv == '-' || firstOpt == TRUE)) {
208                 firstOpt=FALSE;
209                 stopIt=FALSE;
210                 while (stopIt==FALSE && **argv) {
211                         switch (*((*argv)++)) {
212                                 case 'c':
213                                         if (extractFlag == TRUE || listFlag == TRUE)
214                                                 goto flagError;
215                                         createFlag = TRUE;
216                                         break;
217                                 case 'x':
218                                         if (listFlag == TRUE || createFlag == TRUE)
219                                                 goto flagError;
220                                         extractFlag = TRUE;
221                                         break;
222                                 case 't':
223                                         if (extractFlag == TRUE || createFlag == TRUE)
224                                                 goto flagError;
225                                         listFlag = TRUE;
226                                         break;
227 #ifdef BB_FEATURE_TAR_GZIP
228                                 case 'z':
229                                         unzipFlag = TRUE;
230                                         break;
231 #endif
232                                 case 'v':
233                                         verboseFlag = TRUE;
234                                         break;
235                                 case 'O':
236                                         tostdoutFlag = TRUE;
237                                         break;                                  
238                                 case 'f':
239                                         if (*tarName != '-')
240                                                 error_msg_and_die( "Only one 'f' option allowed\n");
241                                         tarName = *(++argv);
242                                         if (tarName == NULL)
243                                                 error_msg_and_die( "Option requires an argument: No file specified\n");
244                                         stopIt=TRUE;
245                                         break;
246 #if defined BB_FEATURE_TAR_EXCLUDE
247                                 case 'e':
248                                         if (strcmp(*argv, "xclude")==0) {
249                                                 excludeList=xrealloc( excludeList,
250                                                                 sizeof(char *) * (excludeListSize+2));
251                                                 excludeList[excludeListSize] = *(++argv);
252                                                 if (excludeList[excludeListSize] == NULL)
253                                                         error_msg_and_die( "Option requires an argument: No file specified\n");
254                                                 /* Remove leading "/"s */
255                                                 if (*excludeList[excludeListSize] =='/')
256                                                         excludeList[excludeListSize] = (excludeList[excludeListSize])+1;
257                                                 /* Tack a NULL onto the end of the list */
258                                                 excludeList[++excludeListSize] = NULL;
259                                                 stopIt=TRUE;
260                                                 break;
261                                         }
262                                 case 'X':
263                                         if (*excludeFileName != '-')
264                                                 error_msg_and_die("Only one 'X' option allowed\n");
265                                         excludeFileName = *(++argv);
266                                         if (excludeFileName == NULL)
267                                                 error_msg_and_die("Option requires an argument: No file specified\n");
268                                         fileList = fopen (excludeFileName, "r");
269                                         if (! fileList)
270                                                 error_msg_and_die("Exclude file: file not found\n");
271                                         while (fgets(file, sizeof(file), fileList) != NULL) {
272                                                 excludeList = xrealloc(excludeList,
273                                                                 sizeof(char *) * (excludeListSize+2));
274                                                 if (file[strlen(file)-1] == '\n')
275                                                         file[strlen(file)-1] = '\0';
276                                                 excludeList[excludeListSize] = xstrdup(file);
277                                                 /* Remove leading "/"s */
278                                                 while (*excludeList[excludeListSize] == '/')
279                                                         excludeList[excludeListSize]++;
280                                                 /* Tack a NULL onto the end of the list */
281                                                 excludeList[++excludeListSize] = NULL;
282                                         }
283                                         fclose(fileList);
284                                         stopIt=TRUE;
285                                         break;
286 #endif
287                                 case '-':
288                                                 break;
289                                 default:
290                                         usage(tar_usage);
291                         }
292                 }
293         }
294
295         /* 
296          * Do the correct type of action supplying the rest of the
297          * command line arguments as the list of files to process.
298          */
299         if (createFlag == TRUE) {
300 #ifndef BB_FEATURE_TAR_CREATE
301                 error_msg_and_die( "This version of tar was not compiled with tar creation support.\n");
302 #else
303 #ifdef BB_FEATURE_TAR_GZIP
304                 if (unzipFlag==TRUE)
305                         error_msg_and_die("Creation of compressed not internally support by tar, pipe to busybox gunzip\n");
306 #endif
307                 status = writeTarFile(tarName, verboseFlag, argv, excludeList);
308 #endif
309         }
310         if (listFlag == TRUE || extractFlag == TRUE) {
311                 int tarFd;
312                 if (*argv)
313                         extractList = argv;
314                 /* Open the tar file for reading.  */
315                 if (!strcmp(tarName, "-"))
316                         tarFd = fileno(stdin);
317                 else
318                         tarFd = open(tarName, O_RDONLY);
319                 if (tarFd < 0)
320                         error_msg_and_die( "Error opening '%s': %s\n", tarName, strerror(errno));
321
322 #ifdef BB_FEATURE_TAR_GZIP      
323                 /* unzip tarFd in a seperate process */
324                 if (unzipFlag == TRUE)
325                         tarFd = tar_unzip_init(tarFd);
326 #endif                  
327                 status = readTarFile(tarFd, extractFlag, listFlag, tostdoutFlag,
328                                         verboseFlag, extractList, excludeList);
329         }
330
331         if (status == TRUE)
332                 return EXIT_SUCCESS;
333         else
334                 return EXIT_FAILURE;
335
336   flagError:
337         error_msg_and_die( "Exactly one of 'c', 'x' or 't' must be specified\n");
338 }
339                                         
340 static void
341 fixUpPermissions(TarInfo *header)
342 {
343         struct utimbuf t;
344         /* Now set permissions etc for the new file */
345         chown(header->name, header->uid, header->gid);
346         chmod(header->name, header->mode);
347         /* Reset the time */
348         t.actime = time(0);
349         t.modtime = header->mtime;
350         utime(header->name, &t);
351 }
352                                 
353 static int
354 tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag)
355 {
356         size_t  writeSize;
357         size_t  readSize;
358         size_t  actualWriteSz;
359         char    buffer[BUFSIZ];
360         size_t  size = header->size;
361         int outFd=fileno(stdout);
362
363         /* Open the file to be written, if a file is supposed to be written */
364         if (extractFlag==TRUE && tostdoutFlag==FALSE) {
365                 /* Create the path to the file, just in case it isn't there...
366                  * This should not screw up path permissions or anything. */
367                 create_path(header->name, 0777);
368                 if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY, 
369                                                 header->mode & ~S_IFMT)) < 0) {
370                         error_msg(io_error, header->name, strerror(errno)); 
371                         return( FALSE);
372                 }
373         }
374
375         /* Write out the file, if we are supposed to be doing that */
376         while ( size > 0 ) {
377                 actualWriteSz=0;
378                 if ( size > sizeof(buffer) )
379                         writeSize = readSize = sizeof(buffer);
380                 else {
381                         int mod = size % 512;
382                         if ( mod != 0 )
383                                 readSize = size + (512 - mod);
384                         else
385                                 readSize = size;
386                         writeSize = size;
387                 }
388                 if ( (readSize = full_read(header->tarFd, buffer, readSize)) <= 0 ) {
389                         /* Tarball seems to have a problem */
390                         error_msg("Unexpected EOF in archive\n"); 
391                         return( FALSE);
392                 }
393                 if ( readSize < writeSize )
394                         writeSize = readSize;
395
396                 /* Write out the file, if we are supposed to be doing that */
397                 if (extractFlag==TRUE) {
398
399                         if ((actualWriteSz=full_write(outFd, buffer, writeSize)) != writeSize ) {
400                                 /* Output file seems to have a problem */
401                                 error_msg(io_error, header->name, strerror(errno)); 
402                                 return( FALSE);
403                         }
404                 } else {
405                         actualWriteSz=writeSize;
406                 }
407
408                 size -= actualWriteSz;
409         }
410
411         /* Now we are done writing the file out, so try 
412          * and fix up the permissions and whatnot */
413         if (extractFlag==TRUE && tostdoutFlag==FALSE) {
414                 close(outFd);
415                 fixUpPermissions(header);
416         }
417         return( TRUE);
418 }
419
420 static int
421 tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag)
422 {
423
424         if (extractFlag==FALSE || tostdoutFlag==TRUE)
425                 return( TRUE);
426
427         if (create_path(header->name, header->mode) != TRUE) {
428                 error_msg("%s: Cannot mkdir: %s\n", 
429                                 header->name, strerror(errno)); 
430                 return( FALSE);
431         }
432         /* make the final component, just in case it was
433          * omitted by create_path() (which will skip the
434          * directory if it doesn't have a terminating '/') */
435         if (mkdir(header->name, header->mode) == 0) {
436                 fixUpPermissions(header);
437         }
438         return( TRUE);
439 }
440
441 static int
442 tarExtractHardLink(TarInfo *header, int extractFlag, int tostdoutFlag)
443 {
444         if (extractFlag==FALSE || tostdoutFlag==TRUE)
445                 return( TRUE);
446
447         if (link(header->linkname, header->name) < 0) {
448                 error_msg("%s: Cannot create hard link to '%s': %s\n", 
449                                 header->name, header->linkname, strerror(errno)); 
450                 return( FALSE);
451         }
452
453         /* Now set permissions etc for the new directory */
454         fixUpPermissions(header);
455         return( TRUE);
456 }
457
458 static int
459 tarExtractSymLink(TarInfo *header, int extractFlag, int tostdoutFlag)
460 {
461         if (extractFlag==FALSE || tostdoutFlag==TRUE)
462                 return( TRUE);
463
464 #ifdef  S_ISLNK
465         if (symlink(header->linkname, header->name) < 0) {
466                 error_msg("%s: Cannot create symlink to '%s': %s\n", 
467                                 header->name, header->linkname, strerror(errno)); 
468                 return( FALSE);
469         }
470         /* Try to change ownership of the symlink.
471          * If libs doesn't support that, don't bother.
472          * Changing the pointed-to-file is the Wrong Thing(tm).
473          */
474 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
475         lchown(header->name, header->uid, header->gid);
476 #endif
477
478         /* Do not change permissions or date on symlink,
479          * since it changes the pointed to file instead.  duh. */
480 #else
481         error_msg("%s: Cannot create symlink to '%s': %s\n", 
482                         header->name, header->linkname, 
483                         "symlinks not supported"); 
484 #endif
485         return( TRUE);
486 }
487
488 static int
489 tarExtractSpecial(TarInfo *header, int extractFlag, int tostdoutFlag)
490 {
491         if (extractFlag==FALSE || tostdoutFlag==TRUE)
492                 return( TRUE);
493
494         if (S_ISCHR(header->mode) || S_ISBLK(header->mode) || S_ISSOCK(header->mode)) {
495                 if (mknod(header->name, header->mode, makedev(header->devmajor, header->devminor)) < 0) {
496                         error_msg("%s: Cannot mknod: %s\n",
497                                 header->name, strerror(errno)); 
498                         return( FALSE);
499                 }
500         } else if (S_ISFIFO(header->mode)) {
501                 if (mkfifo(header->name, header->mode) < 0) {
502                         error_msg("%s: Cannot mkfifo: %s\n",
503                                 header->name, strerror(errno)); 
504                         return( FALSE);
505                 }
506         }
507
508         /* Now set permissions etc for the new directory */
509         fixUpPermissions(header);
510         return( TRUE);
511 }
512
513 /* Read an octal value in a field of the specified width, with optional
514  * spaces on both sides of the number and with an optional null character
515  * at the end.  Returns -1 on an illegal format.  */
516 static long getOctal(const char *cp, int size)
517 {
518         long val = 0;
519
520         for(;(size > 0) && (*cp == ' '); cp++, size--);
521         if ((size == 0) || !is_octal(*cp))
522                 return -1;
523         for(; (size > 0) && is_octal(*cp); size--) {
524                 val = val * 8 + *cp++ - '0';
525         }
526         for (;(size > 0) && (*cp == ' '); cp++, size--);
527         if ((size > 0) && *cp)
528                 return -1;
529         return val;
530 }
531
532
533 /* Parse the tar header and fill in the nice struct with the details */
534 static int
535 readTarHeader(struct TarHeader *rawHeader, struct TarInfo *header)
536 {
537         int i;
538         long chksum, sum=0;
539         unsigned char *s = (unsigned char *)rawHeader;
540
541         header->name  = rawHeader->name;
542         /* Check for and relativify any absolute paths */
543         if ( *(header->name) == '/' ) {
544                 static int alreadyWarned=FALSE;
545
546                 while (*(header->name) == '/')
547                         ++*(header->name);
548
549                 if (alreadyWarned == FALSE) {
550                         error_msg("Removing leading '/' from member names\n");
551                         alreadyWarned = TRUE;
552                 }
553         }
554
555         header->mode  = getOctal(rawHeader->mode, sizeof(rawHeader->mode));
556         header->uid   =  getOctal(rawHeader->uid, sizeof(rawHeader->uid));
557         header->gid   =  getOctal(rawHeader->gid, sizeof(rawHeader->gid));
558         header->size  = getOctal(rawHeader->size, sizeof(rawHeader->size));
559         header->mtime = getOctal(rawHeader->mtime, sizeof(rawHeader->mtime));
560         chksum = getOctal(rawHeader->chksum, sizeof(rawHeader->chksum));
561         header->type  = rawHeader->typeflag;
562         header->linkname  = rawHeader->linkname;
563         header->devmajor  = getOctal(rawHeader->devmajor, sizeof(rawHeader->devmajor));
564         header->devminor  = getOctal(rawHeader->devminor, sizeof(rawHeader->devminor));
565
566         /* Check the checksum */
567         for (i = sizeof(*rawHeader); i-- != 0;) {
568                 sum += *s++;
569         }
570         /* Remove the effects of the checksum field (replace 
571          * with blanks for the purposes of the checksum) */
572         s = rawHeader->chksum;
573         for (i = sizeof(rawHeader->chksum) ; i-- != 0;) {
574                 sum -= *s++;
575         }
576         sum += ' ' * sizeof(rawHeader->chksum);
577         if (sum == chksum )
578                 return ( TRUE);
579         return( FALSE);
580 }
581
582
583 /*
584  * Read a tar file and extract or list the specified files within it.
585  * If the list is empty than all files are extracted or listed.
586  */
587 extern int readTarFile(int tarFd, int extractFlag, int listFlag, 
588                 int tostdoutFlag, int verboseFlag, char** extractList,
589                 char** excludeList)
590 {
591         int status;
592         int errorFlag=FALSE;
593         int skipNextHeaderFlag=FALSE;
594         TarHeader rawHeader;
595         TarInfo header;
596         char** tmpList;
597
598         /* Set the umask for this process so it doesn't 
599          * screw up permission setting for us later. */
600         umask(0);
601
602         /* Read the tar file, and iterate over it one file at a time */
603         while ( (status = full_read(tarFd, (char*)&rawHeader, TAR_BLOCK_SIZE)) == TAR_BLOCK_SIZE ) {
604
605                 /* Try to read the header */
606                 if ( readTarHeader(&rawHeader, &header) == FALSE ) {
607                         if ( *(header.name) == '\0' ) {
608                                 goto endgame;
609                         } else {
610                                 errorFlag=TRUE;
611                                 error_msg("Bad tar header, skipping\n");
612                                 continue;
613                         }
614                 }
615                 if ( *(header.name) == '\0' )
616                                 goto endgame;
617                 header.tarFd = tarFd;
618
619                 /* Skip funky extra GNU headers that precede long files */
620                 if ( (header.type == GNULONGNAME) || (header.type == GNULONGLINK) ) {
621                         skipNextHeaderFlag=TRUE;
622                         if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
623                                 errorFlag = TRUE;
624                         continue;
625                 }
626                 if ( skipNextHeaderFlag == TRUE ) { 
627                         skipNextHeaderFlag=FALSE;
628                         error_msg(name_longer_than_foo, NAME_SIZE); 
629                         if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
630                                 errorFlag = TRUE;
631                         continue;
632                 }
633
634 #if defined BB_FEATURE_TAR_EXCLUDE
635                 {
636                         int skipFlag=FALSE;
637                         /* Check for excluded files....  */
638                         for (tmpList=excludeList; tmpList && *tmpList; tmpList++) {
639                                 /* Do some extra hoop jumping for when directory names
640                                  * end in '/' but the entry in tmpList doesn't */
641                                 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
642                                                         header.name[strlen(header.name)-1]=='/'
643                                                         && strncmp( *tmpList, header.name, 
644                                                                 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
645                                         /* If it is a regular file, pretend to extract it with
646                                          * the extractFlag set to FALSE, so the junk in the tarball
647                                          * is properly skipped over */
648                                         if ( header.type==REGTYPE || header.type==REGTYPE0 ) {
649                                                 if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
650                                                         errorFlag = TRUE;
651                                         }
652                                         skipFlag=TRUE;
653                                         break;
654                                 }
655                         }
656                         /* There are not the droids you're looking for, move along */
657                         if (skipFlag==TRUE)
658                                 continue;
659                 }
660 #endif
661                 if (extractList != NULL) {
662                         int skipFlag = TRUE;
663                         for (tmpList = extractList; *tmpList != NULL; tmpList++) {
664                                 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
665                                                         header.name[strlen(header.name)-1]=='/'
666                                                         && strncmp( *tmpList, header.name, 
667                                                                 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
668                                         /* If it is a regular file, pretend to extract it with
669                                          * the extractFlag set to FALSE, so the junk in the tarball
670                                          * is properly skipped over */
671                                         skipFlag = FALSE;
672                                         memmove(extractList+1, extractList,
673                                                                 sizeof(*extractList)*(tmpList-extractList));
674                                         extractList++;
675                                         break;
676                                 }
677                         }
678                         /* There are not the droids you're looking for, move along */
679                         if (skipFlag == TRUE) {
680                                 if ( header.type==REGTYPE || header.type==REGTYPE0 )
681                                         if (tarExtractRegularFile(&header, FALSE, FALSE) == FALSE)
682                                                 errorFlag = TRUE;
683                                 continue;
684                         }
685                 }
686
687                 if (listFlag == TRUE) {
688                         /* Special treatment if the list (-t) flag is on */
689                         if (verboseFlag == TRUE) {
690                                 int len, len1;
691                                 char buf[35];
692                                 struct tm *tm = localtime (&(header.mtime));
693
694                                 len=printf("%s ", mode_string(header.mode));
695                                 my_getpwuid(buf, header.uid);
696                                 if (! *buf)
697                                         len+=printf("%d", header.uid);
698                                 else
699                                         len+=printf("%s", buf);
700                                 my_getgrgid(buf, header.gid);
701                                 if (! *buf)
702                                         len+=printf("/%-d ", header.gid);
703                                 else
704                                         len+=printf("/%-s ", buf);
705
706                                 if (header.type==CHRTYPE || header.type==BLKTYPE) {
707                                         len1=snprintf(buf, sizeof(buf), "%ld,%-ld ", 
708                                                         header.devmajor, header.devminor);
709                                 } else {
710                                         len1=snprintf(buf, sizeof(buf), "%lu ", (long)header.size);
711                                 }
712                                 /* Jump through some hoops to make the columns match up */
713                                 for(;(len+len1)<31;len++)
714                                         printf(" ");
715                                 printf(buf);
716
717                                 /* Use ISO 8610 time format */
718                                 if (tm) { 
719                                         printf ("%04d-%02d-%02d %02d:%02d:%02d ", 
720                                                         tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, 
721                                                         tm->tm_hour, tm->tm_min, tm->tm_sec);
722                                 }
723                         }
724                         printf("%s", header.name);
725                         if (verboseFlag == TRUE) {
726                                 if (header.type==LNKTYPE)       /* If this is a link, say so */
727                                         printf(" link to %s", header.linkname);
728                                 else if (header.type==SYMTYPE)
729                                         printf(" -> %s", header.linkname);
730                         }
731                         printf("\n");
732                 }
733
734                 /* List contents if we are supposed to do that */
735                 if (verboseFlag == TRUE && extractFlag == TRUE) {
736                         /* Now the normal listing */
737                         FILE *vbFd = stdout;
738                         if (tostdoutFlag == TRUE)       // If the archive goes to stdout, verbose to stderr
739                                 vbFd = stderr;
740                         fprintf(vbFd, "%s\n", header.name);
741                 }
742                         
743                 /* Remove files if we would overwrite them */
744                 if (extractFlag == TRUE && tostdoutFlag == FALSE)
745                         unlink(header.name);
746
747                 /* If we got here, we can be certain we have a legitimate 
748                  * header to work with.  So work with it.  */
749                 switch ( header.type ) {
750                         case REGTYPE:
751                         case REGTYPE0:
752                                 /* If the name ends in a '/' then assume it is
753                                  * supposed to be a directory, and fall through */
754                                 if (header.name[strlen(header.name)-1] != '/') {
755                                         if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE)
756                                                 errorFlag=TRUE;
757                                         break;
758                                 }
759                         case DIRTYPE:
760                                 if (tarExtractDirectory( &header, extractFlag, tostdoutFlag)==FALSE)
761                                         errorFlag=TRUE;
762                                 break;
763                         case LNKTYPE:
764                                 if (tarExtractHardLink( &header, extractFlag, tostdoutFlag)==FALSE)
765                                         errorFlag=TRUE;
766                                 break;
767                         case SYMTYPE:
768                                 if (tarExtractSymLink( &header, extractFlag, tostdoutFlag)==FALSE)
769                                         errorFlag=TRUE;
770                                 break;
771                         case CHRTYPE:
772                         case BLKTYPE:
773                         case FIFOTYPE:
774                                 if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE)
775                                         errorFlag=TRUE;
776                                 break;
777 #if 0
778                         /* Handled earlier */
779                         case GNULONGNAME:
780                         case GNULONGLINK:
781                                 skipNextHeaderFlag=TRUE;
782                                 break;
783 #endif
784                         default:
785                                 error_msg("Unknown file type '%c' in tar file\n", header.type);
786                                 close( tarFd);
787                                 return( FALSE);
788                 }
789         }
790         close(tarFd);
791         if (status > 0) {
792                 /* Bummer - we read a partial header */
793                 error_msg( "Error reading tar file: %s\n", strerror(errno));
794                 return ( FALSE);
795         }
796         else if (errorFlag==TRUE) {
797                 error_msg( "Error exit delayed from previous errors\n");
798                 return( FALSE);
799         } else 
800                 return( status);
801
802         /* Stuff to do when we are done */
803 endgame:
804         close( tarFd);
805         if (extractList != NULL) {
806                 for (; *extractList != NULL; extractList++) {
807                         error_msg("%s: Not found in archive\n", *extractList);
808                         errorFlag = TRUE;
809                 }
810         }
811         if ( *(header.name) == '\0' ) {
812                 if (errorFlag==TRUE)
813                         error_msg( "Error exit delayed from previous errors\n");
814                 else
815                         return( TRUE);
816         } 
817         return( FALSE);
818 }
819
820
821 #ifdef BB_FEATURE_TAR_CREATE
822
823 /*
824 ** writeTarFile(),  writeFileToTarball(), and writeTarHeader() are
825 ** the only functions that deal with the HardLinkInfo structure.
826 ** Even these functions use the xxxHardLinkInfo() functions.
827 */
828 typedef struct HardLinkInfo HardLinkInfo;
829 struct HardLinkInfo
830 {
831         HardLinkInfo *next;           /* Next entry in list */
832         dev_t dev;                    /* Device number */
833         ino_t ino;                    /* Inode number */
834         short linkCount;              /* (Hard) Link Count */
835         char name[1];                 /* Start of filename (must be last) */
836 };
837
838 /* Some info to be carried along when creating a new tarball */
839 struct TarBallInfo
840 {
841         char* fileName;               /* File name of the tarball */
842         int tarFd;                    /* Open-for-write file descriptor
843                                                                          for the tarball */
844         struct stat statBuf;          /* Stat info for the tarball, letting
845                                                                          us know the inode and device that the
846                                                                          tarball lives, so we can avoid trying 
847                                                                          to include the tarball into itself */
848         int verboseFlag;              /* Whether to print extra stuff or not */
849         char** excludeList;           /* List of files to not include */
850         HardLinkInfo *hlInfoHead;     /* Hard Link Tracking Information */
851         HardLinkInfo *hlInfo;         /* Hard Link Info for the current file */
852 };
853 typedef struct TarBallInfo TarBallInfo;
854
855
856 /* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */
857 static void
858 addHardLinkInfo (HardLinkInfo **hlInfoHeadPtr, dev_t dev, ino_t ino,
859                 short linkCount, const char *name)
860 {
861         /* Note: hlInfoHeadPtr can never be NULL! */
862         HardLinkInfo *hlInfo;
863
864         hlInfo = (HardLinkInfo *)xmalloc(sizeof(HardLinkInfo)+strlen(name)+1);
865         if (hlInfo) {
866                 hlInfo->next = *hlInfoHeadPtr;
867                 *hlInfoHeadPtr = hlInfo;
868                 hlInfo->dev = dev;
869                 hlInfo->ino = ino;
870                 hlInfo->linkCount = linkCount;
871                 strcpy(hlInfo->name, name);
872         }
873         return;
874 }
875
876 static void
877 freeHardLinkInfo (HardLinkInfo **hlInfoHeadPtr)
878 {
879         HardLinkInfo *hlInfo = NULL;
880         HardLinkInfo *hlInfoNext = NULL;
881
882         if (hlInfoHeadPtr) {
883                 hlInfo = *hlInfoHeadPtr;
884                 while (hlInfo) {
885                         hlInfoNext = hlInfo->next;
886                         free(hlInfo);
887                         hlInfo = hlInfoNext;
888                 }
889                 *hlInfoHeadPtr = NULL;
890         }
891         return;
892 }
893
894 /* Might be faster (and bigger) if the dev/ino were stored in numeric order;) */
895 static HardLinkInfo *
896 findHardLinkInfo (HardLinkInfo *hlInfo, dev_t dev, ino_t ino)
897 {
898         while(hlInfo) {
899                 if ((ino == hlInfo->ino) && (dev == hlInfo->dev))
900                         break;
901                 hlInfo = hlInfo->next;
902         }
903         return(hlInfo);
904 }
905
906 /* Put an octal string into the specified buffer.
907  * The number is zero and space padded and possibly null padded.
908  * Returns TRUE if successful.  */ 
909 static int putOctal (char *cp, int len, long value)
910 {
911         int tempLength;
912         char tempBuffer[32];
913         char *tempString = tempBuffer;
914
915         /* Create a string of the specified length with an initial space,
916          * leading zeroes and the octal number, and a trailing null.  */
917         sprintf (tempString, "%0*lo", len - 1, value);
918
919         /* If the string is too large, suppress the leading space.  */
920         tempLength = strlen (tempString) + 1;
921         if (tempLength > len) {
922                 tempLength--;
923                 tempString++;
924         }
925
926         /* If the string is still too large, suppress the trailing null.  */
927         if (tempLength > len)
928                 tempLength--;
929
930         /* If the string is still too large, fail.  */
931         if (tempLength > len)
932                 return FALSE;
933
934         /* Copy the string to the field.  */
935         memcpy (cp, tempString, len);
936
937         return TRUE;
938 }
939
940 /* Write out a tar header for the specified file/directory/whatever */
941 static int
942 writeTarHeader(struct TarBallInfo *tbInfo, const char *fileName, struct stat *statbuf)
943 {
944         long chksum=0;
945         struct TarHeader header;
946 #if defined BB_FEATURE_TAR_EXCLUDE
947         char** tmpList;
948 #endif
949         const unsigned char *cp = (const unsigned char *) &header;
950         ssize_t size = sizeof(struct TarHeader);
951                 
952         memset( &header, 0, size);
953
954         if (*fileName=='/') {
955                 static int alreadyWarned=FALSE;
956                 if (alreadyWarned==FALSE) {
957                         error_msg("Removing leading '/' from member names\n");
958                         alreadyWarned=TRUE;
959                 }
960                 strncpy(header.name, fileName+1, sizeof(header.name)); 
961         }
962         else {
963                 strncpy(header.name, fileName, sizeof(header.name)); 
964         }
965
966 #if defined BB_FEATURE_TAR_EXCLUDE
967         /* Check for excluded files....  */
968         for (tmpList=tbInfo->excludeList; tmpList && *tmpList; tmpList++) {
969                 /* Do some extra hoop jumping for when directory names
970                  * end in '/' but the entry in tmpList doesn't */
971                 if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
972                                         header.name[strlen(header.name)-1]=='/'
973                                         && strncmp( *tmpList, header.name, 
974                                                 MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
975                         /* Set the mode to something that is not a regular file, thereby
976                          * faking out writeTarFile into thinking that nothing further need
977                          * be done for this file.  Yes, I know this is ugly, but it works. */
978                         statbuf->st_mode = 0;
979                         return( TRUE);
980                 }
981         }
982 #endif
983
984         putOctal(header.mode, sizeof(header.mode), statbuf->st_mode);
985         putOctal(header.uid, sizeof(header.uid), statbuf->st_uid);
986         putOctal(header.gid, sizeof(header.gid), statbuf->st_gid);
987         putOctal(header.size, sizeof(header.size), 0); /* Regular file size is handled later */
988         putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime);
989         strncpy(header.magic, TAR_MAGIC TAR_VERSION, 
990                         TAR_MAGIC_LEN + TAR_VERSION_LEN );
991
992         /* Enter the user and group names (default to root if it fails) */
993         my_getpwuid(header.uname, statbuf->st_uid);
994         if (! *header.uname)
995                 strcpy(header.uname, "root");
996         my_getgrgid(header.gname, statbuf->st_gid);
997         if (! *header.uname)
998                 strcpy(header.uname, "root");
999
1000         if (tbInfo->hlInfo) {
1001                 /* This is a hard link */
1002                 header.typeflag = LNKTYPE;
1003                 strncpy(header.linkname, tbInfo->hlInfo->name, sizeof(header.linkname));
1004         } else if (S_ISLNK(statbuf->st_mode)) {
1005                 int link_size=0;
1006                 char buffer[BUFSIZ];
1007                 header.typeflag  = SYMTYPE;
1008                 link_size = readlink(fileName, buffer, sizeof(buffer) - 1);
1009                 if ( link_size < 0) {
1010                         error_msg("Error reading symlink '%s': %s\n", header.name, strerror(errno));
1011                         return ( FALSE);
1012                 }
1013                 buffer[link_size] = '\0';
1014                 strncpy(header.linkname, buffer, sizeof(header.linkname)); 
1015         } else if (S_ISDIR(statbuf->st_mode)) {
1016                 header.typeflag  = DIRTYPE;
1017                 strncat(header.name, "/", sizeof(header.name)); 
1018         } else if (S_ISCHR(statbuf->st_mode)) {
1019                 header.typeflag  = CHRTYPE;
1020                 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
1021                 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
1022         } else if (S_ISBLK(statbuf->st_mode)) {
1023                 header.typeflag  = BLKTYPE;
1024                 putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev));
1025                 putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev));
1026         } else if (S_ISFIFO(statbuf->st_mode)) {
1027                 header.typeflag  = FIFOTYPE;
1028         } else if (S_ISREG(statbuf->st_mode)) {
1029                 header.typeflag  = REGTYPE;
1030                 putOctal(header.size, sizeof(header.size), statbuf->st_size);
1031         } else {
1032                 error_msg("%s: Unknown file type\n", fileName);
1033                 return ( FALSE);
1034         }
1035
1036         /* Calculate and store the checksum (i.e. the sum of all of the bytes of
1037          * the header).  The checksum field must be filled with blanks for the
1038          * calculation.  The checksum field is formatted differently from the
1039          * other fields: it has [6] digits, a null, then a space -- rather than
1040          * digits, followed by a null like the other fields... */
1041         memset(header.chksum, ' ', sizeof(header.chksum));
1042         cp = (const unsigned char *) &header;
1043         while (size-- > 0)
1044                 chksum += *cp++;
1045         putOctal(header.chksum, 7, chksum);
1046         
1047         /* Now write the header out to disk */
1048         if ((size=full_write(tbInfo->tarFd, (char*)&header, sizeof(struct TarHeader))) < 0) {
1049                 error_msg(io_error, fileName, strerror(errno)); 
1050                 return ( FALSE);
1051         }
1052         /* Pad the header up to the tar block size */
1053         for (; size<TAR_BLOCK_SIZE; size++) {
1054                 write(tbInfo->tarFd, "\0", 1);
1055         }
1056         /* Now do the verbose thing (or not) */
1057         if (tbInfo->verboseFlag==TRUE) {
1058                 FILE *vbFd = stdout;
1059                 if (tbInfo->tarFd == fileno(stdout))    // If the archive goes to stdout, verbose to stderr
1060                         vbFd = stderr;
1061                 fprintf(vbFd, "%s\n", header.name);
1062         }
1063
1064         return ( TRUE);
1065 }
1066
1067
1068 static int writeFileToTarball(const char *fileName, struct stat *statbuf, void* userData)
1069 {
1070         struct TarBallInfo *tbInfo = (struct TarBallInfo *)userData;
1071
1072         /*
1073         ** Check to see if we are dealing with a hard link.
1074         ** If so -
1075         ** Treat the first occurance of a given dev/inode as a file while
1076         ** treating any additional occurances as hard links.  This is done
1077         ** by adding the file information to the HardLinkInfo linked list.
1078         */
1079         tbInfo->hlInfo = NULL;
1080         if (statbuf->st_nlink > 1) {
1081                 tbInfo->hlInfo = findHardLinkInfo(tbInfo->hlInfoHead, statbuf->st_dev, 
1082                                 statbuf->st_ino);
1083                 if (tbInfo->hlInfo == NULL)
1084                         addHardLinkInfo (&tbInfo->hlInfoHead, statbuf->st_dev,
1085                                         statbuf->st_ino, statbuf->st_nlink, fileName);
1086         }
1087
1088         /* It is against the rules to archive a socket */
1089         if (S_ISSOCK(statbuf->st_mode)) {
1090                 error_msg("%s: socket ignored\n", fileName);
1091                 return( TRUE);
1092         }
1093
1094         /* It is a bad idea to store the archive we are in the process of creating,
1095          * so check the device and inode to be sure that this particular file isn't
1096          * the new tarball */
1097         if (tbInfo->statBuf.st_dev == statbuf->st_dev &&
1098                         tbInfo->statBuf.st_ino == statbuf->st_ino) {
1099                 error_msg("%s: file is the archive; skipping\n", fileName);
1100                 return( TRUE);
1101         }
1102
1103         if (strlen(fileName) >= NAME_SIZE) {
1104                 error_msg(name_longer_than_foo, NAME_SIZE);
1105                 return ( TRUE);
1106         }
1107
1108         if (writeTarHeader(tbInfo, fileName, statbuf)==FALSE) {
1109                 return( FALSE);
1110         } 
1111
1112         /* Now, if the file is a regular file, copy it out to the tarball */
1113         if ((tbInfo->hlInfo == NULL)
1114         &&  (S_ISREG(statbuf->st_mode))) {
1115                 int  inputFileFd;
1116                 char buffer[BUFSIZ];
1117                 ssize_t size=0, readSize=0;
1118
1119                 /* open the file we want to archive, and make sure all is well */
1120                 if ((inputFileFd = open(fileName, O_RDONLY)) < 0) {
1121                         error_msg("%s: Cannot open: %s\n", fileName, strerror(errno));
1122                         return( FALSE);
1123                 }
1124                 
1125                 /* write the file to the archive */
1126                 while ( (size = full_read(inputFileFd, buffer, sizeof(buffer))) > 0 ) {
1127                         if (full_write(tbInfo->tarFd, buffer, size) != size ) {
1128                                 /* Output file seems to have a problem */
1129                                 error_msg(io_error, fileName, strerror(errno)); 
1130                                 return( FALSE);
1131                         }
1132                         readSize+=size;
1133                 }
1134                 if (size == -1) {
1135                         error_msg(io_error, fileName, strerror(errno)); 
1136                         return( FALSE);
1137                 }
1138                 /* Pad the file up to the tar block size */
1139                 for (; (readSize%TAR_BLOCK_SIZE) != 0; readSize++) {
1140                         write(tbInfo->tarFd, "\0", 1);
1141                 }
1142                 close( inputFileFd);
1143         }
1144
1145         return( TRUE);
1146 }
1147
1148 static int writeTarFile(const char* tarName, int verboseFlag, char **argv,
1149                 char** excludeList)
1150 {
1151         int tarFd=-1;
1152         int errorFlag=FALSE;
1153         ssize_t size;
1154         struct TarBallInfo tbInfo;
1155         tbInfo.verboseFlag = verboseFlag;
1156         tbInfo.hlInfoHead = NULL;
1157
1158         /* Make sure there is at least one file to tar up.  */
1159         if (*argv == NULL)
1160                 error_msg_and_die("Cowardly refusing to create an empty archive\n");
1161
1162         /* Open the tar file for writing.  */
1163         if (!strcmp(tarName, "-"))
1164                 tbInfo.tarFd = fileno(stdout);
1165         else
1166                 tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1167         if (tbInfo.tarFd < 0) {
1168                 error_msg( "Error opening '%s': %s\n", tarName, strerror(errno));
1169                 freeHardLinkInfo(&tbInfo.hlInfoHead);
1170                 return ( FALSE);
1171         }
1172         tbInfo.excludeList=excludeList;
1173         /* Store the stat info for the tarball's file, so
1174          * can avoid including the tarball into itself....  */
1175         if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0)
1176                 error_msg_and_die(io_error, tarName, strerror(errno)); 
1177
1178         /* Set the umask for this process so it doesn't 
1179          * screw up permission setting for us later. */
1180         umask(0);
1181
1182         /* Read the directory/files and iterate over them one at a time */
1183         while (*argv != NULL) {
1184                 if (recursive_action(*argv++, TRUE, FALSE, FALSE,
1185                                         writeFileToTarball, writeFileToTarball, 
1186                                         (void*) &tbInfo) == FALSE) {
1187                         errorFlag = TRUE;
1188                 }
1189         }
1190         /* Write two empty blocks to the end of the archive */
1191         for (size=0; size<(2*TAR_BLOCK_SIZE); size++) {
1192                 write(tbInfo.tarFd, "\0", 1);
1193         }
1194
1195         /* To be pedantically correct, we would check if the tarball
1196          * is smaller than 20 tar blocks, and pad it if it was smaller,
1197          * but that isn't necessary for GNU tar interoperability, and
1198          * so is considered a waste of space */
1199
1200         /* Hang up the tools, close up shop, head home */
1201         close(tarFd);
1202         if (errorFlag == TRUE) {
1203                 error_msg("Error exit delayed from previous errors\n");
1204                 freeHardLinkInfo(&tbInfo.hlInfoHead);
1205                 return(FALSE);
1206         }
1207         freeHardLinkInfo(&tbInfo.hlInfoHead);
1208         return( TRUE);
1209 }
1210
1211
1212 #endif
1213