#include <utime.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
-
-
-static const char tar_usage[] =
-#ifdef BB_FEATURE_TAR_CREATE
- "tar -[cxtvO] "
-#else
- "tar -[xtvO] "
-#endif
-#if defined BB_FEATURE_TAR_EXCLUDE
- "[--exclude File] "
-#endif
- "[-f tarFile] [FILE] ...\n"
-#ifndef BB_FEATURE_TRIVIAL_HELP
- "\nCreate, extract, or list files from a tar file. Note that\n"
- "this version of tar treats hard links as separate files.\n\n"
- "Main operation mode:\n"
-#ifdef BB_FEATURE_TAR_CREATE
- "\tc\t\tcreate\n"
-#endif
- "\tx\t\textract\n"
- "\tt\t\tlist\n"
- "\nFile selection:\n"
- "\tf\t\tname of tarfile or \"-\" for stdin\n"
- "\tO\t\textract to stdout\n"
-#if defined BB_FEATURE_TAR_EXCLUDE
- "\t--exclude\tfile to exclude\n"
-#endif
- "\nInformative output:\n"
- "\tv\t\tverbosely list files processed\n"
-#endif
- ;
+#include <getopt.h>
/* Tar file constants */
#ifndef MAJOR
/* 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);
int verboseFlag, int argc, char **argv, char** excludeList);
#endif
+static struct option longopts[] =
+{
+#ifdef BB_FEATURE_TAR_EXCLUDE
+ {"exclude",required_argument,NULL,'e'},
+#endif
+ {NULL,0,NULL,0}
+};
extern int tar_main(int argc, char **argv)
{
int createFlag = FALSE;
int verboseFlag = FALSE;
int tostdoutFlag = FALSE;
- int stopIt;
+ int opt;
if (argc <= 1)
usage(tar_usage);
- /* Parse any options */
- while (--argc > 0 && strspn(*(++argv), "-cxt") >0 ) {
- stopIt=FALSE;
- while (stopIt==FALSE && *argv && **argv) {
- switch (**argv) {
- case 'f':
- if (--argc == 0) {
- fatalError( "Option requires an argument: No file specified\n");
- }
- if (*tarName != '-')
- fatalError( "Only one 'f' option allowed\n");
- tarName = *(++argv);
- if (tarName == NULL)
- fatalError( "Option requires an argument: No file specified\n");
- stopIt=TRUE;
- break;
-
- case 't':
- if (extractFlag == TRUE || createFlag == TRUE)
+ /* do normal option parsing */
+ while ((opt = getopt_long(argc, argv, "cxtvOf:", longopts, NULL)) != EOF) {
+ switch (opt) {
+ case 'c':
+ if (extractFlag == TRUE || listFlag == TRUE)
goto flagError;
- listFlag = TRUE;
+ createFlag = TRUE;
break;
-
case 'x':
if (listFlag == TRUE || createFlag == TRUE)
goto flagError;
extractFlag = TRUE;
break;
- case 'c':
- if (extractFlag == TRUE || listFlag == TRUE)
+ case 't':
+ if (extractFlag == TRUE || createFlag == TRUE)
goto flagError;
- createFlag = TRUE;
+ listFlag = TRUE;
break;
-
case 'v':
verboseFlag = TRUE;
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;
break;
- case '-':
#if defined BB_FEATURE_TAR_EXCLUDE
- if (strcmp(*argv, "-exclude")==0) {
- if (--argc == 0) {
- fatalError( "Option requires an argument: No file specified\n");
- }
- excludeList=realloc( excludeList, sizeof(char**) * (excludeListSize+2));
- excludeList[excludeListSize] = *(++argv);
- /* Remove leading "/"s */
- if (*excludeList[excludeListSize] =='/') {
- excludeList[excludeListSize] = (excludeList[excludeListSize])+1;
- }
- if (excludeList[excludeListSize++] == NULL)
- fatalError( "Option requires an argument: No file specified\n");
- /* Tack a NULL onto the end of the list */
- excludeList[excludeListSize] = NULL;
- stopIt=TRUE;
- break;
- }
-#endif
+ case 'e':
+ excludeList=xrealloc( excludeList, sizeof(char**) * (excludeListSize+2));
+ excludeList[excludeListSize] = optarg;
+ /* Remove leading "/"s */
+ if (*excludeList[excludeListSize] =='/')
+ excludeList[excludeListSize] = (excludeList[excludeListSize])+1;
+ /* Tack a NULL onto the end of the list */
+ excludeList[++excludeListSize] = NULL;
break;
-
+#endif
default:
- fatalError( "Unknown tar flag '%c'\n"
- "Try `tar --help' for more information\n", **argv);
- }
- ++(*argv);
+ usage(tar_usage);
}
}
#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, argv, excludeList));
+ exit(writeTarFile(tarName, tostdoutFlag, verboseFlag, argc-optind, &argv[optind], excludeList));
#endif
}
if (listFlag == TRUE || extractFlag == TRUE) {
- exit(readTarFile(tarName, extractFlag, listFlag, tostdoutFlag, verboseFlag, excludeList));
+ exit(readTarFile(tarName, extractFlag, listFlag, tostdoutFlag, verboseFlag, &argv[optind], excludeList));
}
flagError:
/* Open the file to be written, if a file is supposed to be written */
if (extractFlag==TRUE && tostdoutFlag==FALSE) {
- if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY, header->mode & ~S_IFMT)) < 0)
- errorMsg(io_error, header->name, strerror(errno));
/* Create the path to the file, just in case it isn't there...
* This should not screw up path permissions or anything. */
createPath(header->name, 0777);
+ if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY,
+ header->mode & ~S_IFMT)) < 0) {
+ errorMsg(io_error, header->name, strerror(errno));
+ return( FALSE);
+ }
}
/* Write out the file, if we are supposed to be doing that */
}
if ( (readSize = fullRead(header->tarFd, buffer, readSize)) <= 0 ) {
/* Tarball seems to have a problem */
- errorMsg("tar: Unexpected EOF in archive\n");
+ errorMsg("Unexpected EOF in archive\n");
return( FALSE);
}
if ( readSize < writeSize )
return( TRUE);
if (createPath(header->name, header->mode) != TRUE) {
- errorMsg("tar: %s: Cannot mkdir: %s\n",
+ errorMsg("%s: Cannot mkdir: %s\n",
header->name, strerror(errno));
return( FALSE);
}
return( TRUE);
if (link(header->linkname, header->name) < 0) {
- errorMsg("tar: %s: Cannot create hard link to '%s': %s\n",
+ errorMsg("%s: Cannot create hard link to '%s': %s\n",
header->name, header->linkname, strerror(errno));
return( FALSE);
}
#ifdef S_ISLNK
if (symlink(header->linkname, header->name) < 0) {
- errorMsg("tar: %s: Cannot create symlink to '%s': %s\n",
+ errorMsg("%s: Cannot create symlink to '%s': %s\n",
header->name, header->linkname, strerror(errno));
return( FALSE);
}
/* Do not change permissions or date on symlink,
* since it changes the pointed to file instead. duh. */
#else
- errorMsg("tar: %s: Cannot create symlink to '%s': %s\n",
+ errorMsg("%s: Cannot create symlink to '%s': %s\n",
header->name, header->linkname,
"symlinks not supported");
#endif
if (S_ISCHR(header->mode) || S_ISBLK(header->mode) || S_ISSOCK(header->mode)) {
if (mknod(header->name, header->mode, makedev(header->devmajor, header->devminor)) < 0) {
- errorMsg("tar: %s: Cannot mknod: %s\n",
+ errorMsg("%s: Cannot mknod: %s\n",
header->name, strerror(errno));
return( FALSE);
}
} else if (S_ISFIFO(header->mode)) {
if (mkfifo(header->name, header->mode) < 0) {
- errorMsg("tar: %s: Cannot mkfifo: %s\n",
+ errorMsg("%s: Cannot mkfifo: %s\n",
header->name, strerror(errno));
return( FALSE);
}
++*(header->name);
if (alreadyWarned == FALSE) {
- errorMsg("tar: Removing leading '/' from member names\n");
+ errorMsg("Removing leading '/' from member names\n");
alreadyWarned = TRUE;
}
}
* 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;
TarHeader rawHeader;
TarInfo header;
-#if defined BB_FEATURE_TAR_EXCLUDE
char** tmpList;
-#endif
/* Open the tar file for reading. */
if (!strcmp(tarName, "-"))
continue;
}
#endif
+ 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;
+ }
+ }
/* Special treatment if the list (-t) flag is on */
if (verboseFlag == TRUE && extractFlag == FALSE) {
int len, len1;
}
}
/* List contents if we are supposed to do that */
- if (verboseFlag == TRUE || listFlag == TRUE) {
+ if (verboseFlag == TRUE && listFlag != TRUE) {
/* Now the normal listing */
- printf("%s", header.name);
+ FILE *vbFd = stdout;
+ if (tostdoutFlag == TRUE) // If the archive goes to stdout, verbose to stderr
+ vbFd = stderr;
+ fprintf(vbFd, "%s\n", header.name);
}
+
if (verboseFlag == TRUE && listFlag == TRUE) {
- /* If this is a link, say so */
- if (header.type==LNKTYPE)
+ 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);
- }
- if (verboseFlag == TRUE || listFlag == TRUE) {
printf("\n");
}
- /* Remove any clutter lying in our way */
- unlink( header.name);
+ /* 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. */
return ( FALSE);
}
else if (errorFlag==TRUE) {
- errorMsg( "tar: Error exit delayed from previous errors\n");
+ errorMsg( "Error exit delayed from previous errors\n");
return( FALSE);
} else
return( status);
close( tarFd);
if ( *(header.name) == '\0' ) {
if (errorFlag==TRUE)
- errorMsg( "tar: Error exit delayed from previous errors\n");
+ errorMsg( "Error exit delayed from previous errors\n");
else
return( TRUE);
}
#endif
const unsigned char *cp = (const unsigned char *) &header;
ssize_t size = sizeof(struct TarHeader);
-
+
memset( &header, 0, size);
if (*fileName=='/') {
static int alreadyWarned=FALSE;
if (alreadyWarned==FALSE) {
- errorMsg("tar: Removing leading '/' from member names\n");
+ errorMsg("Removing leading '/' from member names\n");
alreadyWarned=TRUE;
}
strncpy(header.name, fileName+1, sizeof(header.name));
/* WARNING/NOTICE: I break Hard Links */
if (S_ISLNK(statbuf->st_mode)) {
+ int link_size=0;
char buffer[BUFSIZ];
header.typeflag = SYMTYPE;
- if ( readlink(fileName, buffer, sizeof(buffer) - 1) < 0) {
+ link_size = readlink(fileName, buffer, sizeof(buffer) - 1);
+ if ( link_size < 0) {
errorMsg("Error reading symlink '%s': %s\n", header.name, strerror(errno));
return ( FALSE);
}
+ buffer[link_size] = '\0';
strncpy(header.linkname, buffer, sizeof(header.linkname));
} else if (S_ISDIR(statbuf->st_mode)) {
header.typeflag = DIRTYPE;
header.typeflag = REGTYPE;
putOctal(header.size, sizeof(header.size), statbuf->st_size);
} else {
- errorMsg("tar: %s: Unknown file type\n", fileName);
+ errorMsg("%s: Unknown file type\n", fileName);
return ( FALSE);
}
write(tbInfo->tarFd, "\0", 1);
}
/* Now do the verbose thing (or not) */
- if (tbInfo->verboseFlag==TRUE)
- fprintf(stdout, "%s\n", header.name);
+ if (tbInfo->verboseFlag==TRUE) {
+ FILE *vbFd = stdout;
+ if (tbInfo->tarFd == fileno(stdout)) // If the archive goes to stdout, verbose to stderr
+ vbFd = stderr;
+ fprintf(vbFd, "%s\n", header.name);
+ }
return ( TRUE);
}
/* It is against the rules to archive a socket */
if (S_ISSOCK(statbuf->st_mode)) {
- errorMsg("tar: %s: socket ignored\n", fileName);
+ errorMsg("%s: socket ignored\n", fileName);
return( TRUE);
}
* the new tarball */
if (tbInfo->statBuf.st_dev == statbuf->st_dev &&
tbInfo->statBuf.st_ino == statbuf->st_ino) {
- errorMsg("tar: %s: file is the archive; skipping\n", fileName);
+ errorMsg("%s: file is the archive; skipping\n", fileName);
return( TRUE);
}
/* open the file we want to archive, and make sure all is well */
if ((inputFileFd = open(fileName, O_RDONLY)) < 0) {
- errorMsg("tar: %s: Cannot open: %s\n", fileName, strerror(errno));
+ errorMsg("%s: Cannot open: %s\n", fileName, strerror(errno));
return( FALSE);
}
/* Make sure there is at least one file to tar up. */
if (argc <= 0)
- fatalError("tar: Cowardly refusing to create an empty archive\n");
+ fatalError("Cowardly refusing to create an empty archive\n");
/* Open the tar file for writing. */
if (tostdoutFlag == TRUE)
else
tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (tbInfo.tarFd < 0) {
- errorMsg( "tar: Error opening '%s': %s\n", tarName, strerror(errno));
+ errorMsg( "Error opening '%s': %s\n", tarName, strerror(errno));
return ( FALSE);
}
tbInfo.excludeList=excludeList;
/* Hang up the tools, close up shop, head home */
close(tarFd);
if (errorFlag == TRUE) {
- errorMsg("tar: Error exit delayed from previous errors\n");
+ errorMsg("Error exit delayed from previous errors\n");
return(FALSE);
}
return( TRUE);