Brand new version of xargs. Tested thoroughly by Kent Robotti. (Domo arigato,
[oweals/busybox.git] / tar.c
diff --git a/tar.c b/tar.c
index cab53aa108607eae33f3a569de5ee568c44b8e1a..07c0e71052db07e69764057245dbf0e8dd982601 100644 (file)
--- a/tar.c
+++ b/tar.c
  */
 
 
-#include "internal.h"
+#include "busybox.h"
 #define BB_DECLARE_EXTERN
 #define bb_need_io_error
+#define bb_need_name_longer_than_foo
 #include "messages.c"
 #include <stdio.h>
 #include <dirent.h>
@@ -49,6 +50,7 @@
 #include <utime.h>
 #include <sys/types.h>
 #include <sys/sysmacros.h>
+#include <getopt.h>
 
 /* Tar file constants  */
 #ifndef MAJOR
 #define MINOR(dev) ((dev)&0xff)
 #endif
 
+#define NAME_SIZE      100
 
 /* POSIX tar Header Block, from POSIX 1003.1-1990  */
 struct TarHeader
 {
                                 /* byte offset */
-       char name[100];               /*   0-99 */
+       char name[NAME_SIZE];         /*   0-99 */
        char mode[8];                 /* 100-107 */
        char uid[8];                  /* 108-115 */
        char gid[8];                  /* 116-123 */
@@ -69,7 +72,7 @@ struct TarHeader
        char mtime[12];               /* 136-147 */
        char chksum[8];               /* 148-155 */
        char typeflag;                /* 156-156 */
-       char linkname[100];           /* 157-256 */
+       char linkname[NAME_SIZE];     /* 157-256 */
        char magic[6];                /* 257-262 */
        char version[2];              /* 263-264 */
        char uname[32];               /* 265-296 */
@@ -101,6 +104,8 @@ enum TarFileType
        DIRTYPE  = '5',            /* directory */
        FIFOTYPE = '6',            /* FIFO special */
        CONTTYPE = '7',            /* reserved */
+       GNULONGLINK = 'K',         /* GNU long (>100 chars) link name */
+       GNULONGNAME = 'L',         /* GNU long (>100 chars) file name */
 };
 typedef enum TarFileType TarFileType;
 
@@ -125,17 +130,17 @@ typedef struct TarInfo TarInfo;
 
 /* Local procedures to restore files from a tar file.  */
 static int readTarFile(const char* tarName, int extractFlag, int listFlag, 
-               int tostdoutFlag, int verboseFlag, char** excludeList);
+               int tostdoutFlag, int verboseFlag, char** extractList,
+               char** excludeList);
 
 
 
 #ifdef BB_FEATURE_TAR_CREATE
 /* Local procedures to save files into a tar file.  */
-static int writeTarFile(const char* tarName, int tostdoutFlag, 
-               int verboseFlag, int argc, char **argv, char** excludeList);
+static int writeTarFile(const char* tarName, int verboseFlag, char **argv,
+               char** excludeList);
 #endif
 
-
 extern int tar_main(int argc, char **argv)
 {
        char** excludeList=NULL;
@@ -148,14 +153,18 @@ extern int tar_main(int argc, char **argv)
        int createFlag   = FALSE;
        int verboseFlag  = FALSE;
        int tostdoutFlag = FALSE;
-       int opt;
+       int firstOpt = TRUE;
+       int stopIt;
+                                                                                                                                                  
 
        if (argc <= 1)
                usage(tar_usage);
 
-       /* do normal option parsing */
-       while ((opt = getopt(argc, argv, "cxtvOf:-:")) > 0) {
-               switch (opt) {
+       while (*(++argv) && (**argv == '-' || firstOpt == TRUE)) {
+               firstOpt=FALSE;
+               stopIt=FALSE;
+               while (stopIt==FALSE && **argv) {
+                       switch (*((*argv)++)) {
                                case 'c':
                                        if (extractFlag == TRUE || listFlag == TRUE)
                                                goto flagError;
@@ -176,37 +185,36 @@ extern int tar_main(int argc, char **argv)
                                        break;
                                case 'O':
                                        tostdoutFlag = TRUE;
-                                       tarName = "-";
                                        break;                                  
                                case 'f':
                                        if (*tarName != '-')
                                                fatalError( "Only one 'f' option allowed\n");
-                                       tarName = optarg;
-                                       if (!strcmp(tarName, "-") && createFlag == TRUE)
-                                               tostdoutFlag = TRUE;
+                                       tarName = *(++argv);
+                                       if (tarName == NULL)
+                                               fatalError( "Option requires an argument: No file specified\n");
+                                       stopIt=TRUE;
                                        break;
-                               case '-':
 #if defined BB_FEATURE_TAR_EXCLUDE
-                                       if (strcmp(optarg, "exclude")==0) {
-                                               if (argv[optind]==NULL)
-                                                       fatalError( "option `--exclude' requires an argument\n");
-                                               excludeList=realloc( excludeList, sizeof(char**) * (excludeListSize+2));
-                                               excludeList[excludeListSize] = argv[optind];
+                               case 'e':
+                                       if (strcmp(*argv, "exclude")==0) {
+                                               excludeList=xrealloc( excludeList, sizeof(char**) * (excludeListSize+2));
+                                               excludeList[excludeListSize] = *(++argv);
+                                               if (excludeList[excludeListSize] == NULL)
+                                                       fatalError( "Option requires an argument: No file specified\n");
                                                /* Remove leading "/"s */
-                                               if (*excludeList[excludeListSize] =='/') {
+                                               if (*excludeList[excludeListSize] =='/')
                                                        excludeList[excludeListSize] = (excludeList[excludeListSize])+1;
-                                               }
                                                /* Tack a NULL onto the end of the list */
-                                               excludeList[excludeListSize] = NULL;
-                                               optind++;
+                                               excludeList[++excludeListSize] = NULL;
+                                               stopIt=TRUE;
                                                break;
                                        }
 #endif
-                                       fatalError( "Unknown tar flag '%s'\n" 
-                                                       "Try `tar --help' for more information\n", optarg);
+                               case '-':
+                                               break;
                                default:
-                                       fatalError( "Unknown tar flag '%c'\n" 
-                                                       "Try `tar --help' for more information\n", **argv);
+                                       usage(tar_usage);
+                       }
                }
        }
 
@@ -218,11 +226,12 @@ extern int tar_main(int argc, char **argv)
 #ifndef BB_FEATURE_TAR_CREATE
                fatalError( "This version of tar was not compiled with tar creation support.\n");
 #else
-               exit(writeTarFile(tarName, tostdoutFlag, verboseFlag, argc-optind, &argv[optind], excludeList));
+               exit(writeTarFile(tarName, verboseFlag, argv, excludeList));
 #endif
        }
        if (listFlag == TRUE || extractFlag == TRUE) {
-               exit(readTarFile(tarName, extractFlag, listFlag, tostdoutFlag, verboseFlag, excludeList));
+               exit(readTarFile(tarName, extractFlag, listFlag, tostdoutFlag,
+                                       verboseFlag, argv, excludeList));
        }
 
   flagError:
@@ -477,15 +486,15 @@ readTarHeader(struct TarHeader *rawHeader, struct TarInfo *header)
  * If the list is empty than all files are extracted or listed.
  */
 static int readTarFile(const char* tarName, int extractFlag, int listFlag, 
-               int tostdoutFlag, int verboseFlag, char** excludeList)
+               int tostdoutFlag, int verboseFlag, char** extractList,
+               char** excludeList)
 {
        int status, tarFd=-1;
        int errorFlag=FALSE;
+       int skipNextHeaderFlag=FALSE;
        TarHeader rawHeader;
        TarInfo header;
-#if defined BB_FEATURE_TAR_EXCLUDE
        char** tmpList;
-#endif
 
        /* Open the tar file for reading.  */
        if (!strcmp(tarName, "-"))
@@ -504,7 +513,7 @@ static int readTarFile(const char* tarName, int extractFlag, int listFlag,
        /* Read the tar file, and iterate over it one file at a time */
        while ( (status = fullRead(tarFd, (char*)&rawHeader, TAR_BLOCK_SIZE)) == TAR_BLOCK_SIZE ) {
 
-               /* First, try to read the header */
+               /* Try to read the header */
                if ( readTarHeader(&rawHeader, &header) == FALSE ) {
                        if ( *(header.name) == '\0' ) {
                                goto endgame;
@@ -518,6 +527,19 @@ static int readTarFile(const char* tarName, int extractFlag, int listFlag,
                                goto endgame;
                header.tarFd = tarFd;
 
+               /* Skip funky extra GNU headers that precede long files */
+               if ( (header.type == GNULONGNAME) || (header.type == GNULONGLINK) ) {
+                       skipNextHeaderFlag=TRUE;
+                       tarExtractRegularFile(&header, FALSE, FALSE);
+                       continue;
+               }
+               if ( skipNextHeaderFlag == TRUE ) { 
+                       skipNextHeaderFlag=FALSE;
+                       errorMsg(name_longer_than_foo, NAME_SIZE); 
+                       tarExtractRegularFile(&header, FALSE, FALSE);
+                       continue;
+               }
+
 #if defined BB_FEATURE_TAR_EXCLUDE
                {
                        int skipFlag=FALSE;
@@ -544,46 +566,79 @@ static int readTarFile(const char* tarName, int extractFlag, int listFlag,
                                continue;
                }
 #endif
-               /* Special treatment if the list (-t) flag is on */
-               if (verboseFlag == TRUE && extractFlag == FALSE) {
-                       int len, len1;
-                       char buf[35];
-                       struct tm *tm = localtime (&(header.mtime));
-
-                       len=printf("%s ", modeString(header.mode));
-                       memset(buf, 0, 8*sizeof(char));
-                       my_getpwuid(buf, header.uid);
-                       if (! *buf)
-                               len+=printf("%d", header.uid);
-                       else
-                               len+=printf("%s", buf);
-                       memset(buf, 0, 8*sizeof(char));
-                       my_getgrgid(buf, header.gid);
-                       if (! *buf)
-                               len+=printf("/%-d ", header.gid);
-                       else
-                               len+=printf("/%-s ", buf);
+               if (*extractList != NULL) {
+                       int skipFlag = TRUE;
+                       for (tmpList = extractList; *tmpList != NULL; tmpList++) {
+                               if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
+                                                       header.name[strlen(header.name)-1]=='/'
+                                                       && strncmp( *tmpList, header.name, 
+                                                               MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
+                                       /* If it is a regular file, pretend to extract it with
+                                        * the extractFlag set to FALSE, so the junk in the tarball
+                                        * is properly skipped over */
+                                       skipFlag = FALSE;
+                                       break;
+                               }
+                       }
+                       /* There are not the droids you're looking for, move along */
+                       if (skipFlag == TRUE) {
+                               if ( header.type==REGTYPE || header.type==REGTYPE0 )
+                                               tarExtractRegularFile(&header, FALSE, FALSE);
+                               continue;
+                       }
+               }
 
-                       if (header.type==CHRTYPE || header.type==BLKTYPE) {
-                               len1=snprintf(buf, sizeof(buf), "%ld,%-ld ", 
-                                               header.devmajor, header.devminor);
-                       } else {
-                               len1=snprintf(buf, sizeof(buf), "%lu ", (long)header.size);
+               if (listFlag == TRUE) {
+                       /* Special treatment if the list (-t) flag is on */
+                       if (verboseFlag == TRUE) {
+                               int len, len1;
+                               char buf[35];
+                               struct tm *tm = localtime (&(header.mtime));
+
+                               len=printf("%s ", modeString(header.mode));
+                               memset(buf, 0, 8*sizeof(char));
+                               my_getpwuid(buf, header.uid);
+                               if (! *buf)
+                                       len+=printf("%d", header.uid);
+                               else
+                                       len+=printf("%s", buf);
+                               memset(buf, 0, 8*sizeof(char));
+                               my_getgrgid(buf, header.gid);
+                               if (! *buf)
+                                       len+=printf("/%-d ", header.gid);
+                               else
+                                       len+=printf("/%-s ", buf);
+
+                               if (header.type==CHRTYPE || header.type==BLKTYPE) {
+                                       len1=snprintf(buf, sizeof(buf), "%ld,%-ld ", 
+                                                       header.devmajor, header.devminor);
+                               } else {
+                                       len1=snprintf(buf, sizeof(buf), "%lu ", (long)header.size);
+                               }
+                               /* Jump through some hoops to make the columns match up */
+                               for(;(len+len1)<31;len++)
+                                       printf(" ");
+                               printf(buf);
+
+                               /* Use ISO 8610 time format */
+                               if (tm) { 
+                                       printf ("%04d-%02d-%02d %02d:%02d:%02d ", 
+                                                       tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, 
+                                                       tm->tm_hour, tm->tm_min, tm->tm_sec);
+                               }
                        }
-                       /* Jump through some hoops to make the columns match up */
-                       for(;(len+len1)<31;len++)
-                               printf(" ");
-                       printf(buf);
-
-                       /* Use ISO 8610 time format */
-                       if (tm) { 
-                               printf ("%04d-%02d-%02d %02d:%02d:%02d ", 
-                                               tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, 
-                                               tm->tm_hour, tm->tm_min, tm->tm_sec);
+                       printf("%s", header.name);
+                       if (verboseFlag == TRUE) {
+                               if (header.type==LNKTYPE)       /* If this is a link, say so */
+                                       printf(" link to %s", header.linkname);
+                               else if (header.type==SYMTYPE)
+                                       printf(" -> %s", header.linkname);
                        }
+                       printf("\n");
                }
+
                /* List contents if we are supposed to do that */
-               if (verboseFlag == TRUE && listFlag != TRUE) {
+               if (verboseFlag == TRUE && extractFlag == TRUE) {
                        /* Now the normal listing */
                        FILE *vbFd = stdout;
                        if (tostdoutFlag == TRUE)       // If the archive goes to stdout, verbose to stderr
@@ -591,18 +646,9 @@ static int readTarFile(const char* tarName, int extractFlag, int listFlag,
                        fprintf(vbFd, "%s\n", header.name);
                }
                        
-               if (verboseFlag == TRUE && listFlag == TRUE) {
-                       printf("%s", header.name);
-                       if (header.type==LNKTYPE)       /* If this is a link, say so */
-                               printf(" link to %s", header.linkname);
-                       else if (header.type==SYMTYPE)
-                               printf(" -> %s", header.linkname);
-                       printf("\n");
-               }
-
-               /* Remove any clutter lying in our way */
-               if (extractFlag == TRUE)        /* .. but only if we are extracting (as */
-                       unlink( header.name);   /* opposed to listing) (rob@sysgo.de)   */
+               /* Remove files if we would overwrite them */
+               if (extractFlag == TRUE && tostdoutFlag == FALSE)
+                       unlink(header.name);
 
                /* If we got here, we can be certain we have a legitimate 
                 * header to work with.  So work with it.  */
@@ -634,7 +680,15 @@ static int readTarFile(const char* tarName, int extractFlag, int listFlag,
                                if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE)
                                        errorFlag=TRUE;
                                break;
+#if 0
+                       /* Handled earlier */
+                       case GNULONGNAME:
+                       case GNULONGLINK:
+                               skipNextHeaderFlag=TRUE;
+                               break;
+#endif
                        default:
+                               errorMsg("Unknown file type '%c' in tar file\n", header.type);
                                close( tarFd);
                                return( FALSE);
                }
@@ -860,6 +914,11 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf, void*
                return( TRUE);
        }
 
+       if (strlen(fileName) >= NAME_SIZE) {
+               errorMsg(name_longer_than_foo, NAME_SIZE);
+               return ( TRUE);
+       }
+
        if (writeTarHeader(tbInfo, fileName, statbuf)==FALSE) {
                return( FALSE);
        } 
@@ -899,8 +958,8 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf, void*
        return( TRUE);
 }
 
-static int writeTarFile(const char* tarName, int tostdoutFlag, 
-               int verboseFlag, int argc, char **argv, char** excludeList)
+static int writeTarFile(const char* tarName, int verboseFlag, char **argv,
+               char** excludeList)
 {
        int tarFd=-1;
        int errorFlag=FALSE;
@@ -909,11 +968,11 @@ static int writeTarFile(const char* tarName, int tostdoutFlag,
        tbInfo.verboseFlag = verboseFlag;
 
        /* Make sure there is at least one file to tar up.  */
-       if (argc <= 0)
+       if (*argv == NULL)
                fatalError("Cowardly refusing to create an empty archive\n");
 
        /* Open the tar file for writing.  */
-       if (tostdoutFlag == TRUE)
+       if (!strcmp(tarName, "-"))
                tbInfo.tarFd = fileno(stdout);
        else
                tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
@@ -932,7 +991,7 @@ static int writeTarFile(const char* tarName, int tostdoutFlag,
        umask(0);
 
        /* Read the directory/files and iterate over them one at a time */
-       while (argc-- > 0) {
+       while (*argv != NULL) {
                if (recursiveAction(*argv++, TRUE, FALSE, FALSE,
                                        writeFileToTarball, writeFileToTarball, 
                                        (void*) &tbInfo) == FALSE) {
@@ -945,7 +1004,7 @@ static int writeTarFile(const char* tarName, int tostdoutFlag,
        }
 
        /* To be pedantically correct, we would check if the tarball
-        * is smaller then 20 tar blocks, and pad it if it was smaller,
+        * is smaller than 20 tar blocks, and pad it if it was smaller,
         * but that isn't necessary for GNU tar interoperability, and
         * so is considered a waste of space */