*/
-#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>
#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 */
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 */
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;
#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);
-#endif
-
-static struct option longopts[] =
-{
-#ifdef BB_FEATURE_TAR_EXCLUDE
- {"exclude",required_argument,NULL,'e'},
+static int writeTarFile(const char* tarName, int verboseFlag, char **argv,
+ char** excludeList);
#endif
- {NULL,0,NULL,0}
-};
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_long(argc, argv, "cxtvOf:", longopts, NULL)) != EOF) {
- 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;
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;
#if defined BB_FEATURE_TAR_EXCLUDE
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;
+ 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] =='/')
+ excludeList[excludeListSize] = (excludeList[excludeListSize])+1;
+ /* Tack a NULL onto the end of the list */
+ excludeList[++excludeListSize] = NULL;
+ stopIt=TRUE;
+ break;
+ }
#endif
+ case '-':
+ break;
default:
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-optind, &argv[optind], excludeList));
+ exit(writeTarFile(tarName, verboseFlag, argv, excludeList));
#endif
}
if (listFlag == TRUE || extractFlag == TRUE) {
- exit(readTarFile(tarName, extractFlag, listFlag, tostdoutFlag, verboseFlag, &argv[optind], excludeList));
+ exit(readTarFile(tarName, extractFlag, listFlag, tostdoutFlag,
+ verboseFlag, argv, excludeList));
}
flagError:
{
int status, tarFd=-1;
int errorFlag=FALSE;
+ int skipNextHeaderFlag=FALSE;
TarHeader rawHeader;
TarInfo header;
char** tmpList;
/* 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;
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;
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);
}
return( TRUE);
}
+ if (strlen(fileName) >= NAME_SIZE) {
+ errorMsg(name_longer_than_foo, NAME_SIZE);
+ return ( TRUE);
+ }
+
if (writeTarHeader(tbInfo, fileName, statbuf)==FALSE) {
return( FALSE);
}
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;
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);
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) {
}
/* 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 */