/* Put an octal string into the specified buffer.
* The number is zero padded and possibly null terminated.
- * Returns TRUE if successful. - DISABLED (no caller ever checked) */
-/* FIXME: we leave field untouched if value doesn't fit. */
-/* This is not good - what will happen at untar time?? */
-static void putOctal(char *cp, int len, long long value)
+ * Stores low-order bits only if whole value does not fit. */
+static void putOctal(char *cp, int len, off_t value)
{
- int tempLength;
- /* long long for the sake of storing lengths of 4Gb+ files */
- /* (we are bust anyway after 64Gb: it doesn't fit into the field) */
- char tempBuffer[sizeof(long long)*3+1];
+ char tempBuffer[sizeof(off_t)*3+1];
char *tempString = tempBuffer;
+ int width;
- /* Create a string of the specified length with
- * leading zeroes and the octal number, and a trailing null. */
- tempLength = sprintf(tempBuffer, "%0*llo", len - 1, value);
-
- /* If the string is too large, suppress leading 0's. */
- /* If that is not enough, drop trailing null. */
- tempLength -= len; /* easier to do checks */
- while (tempLength >= 0) {
- if (tempString[0] != '0') {
- if (!tempLength) {
- /* 1234 barely fits in 4 chars (w/o EOL '\0') */
- break;
- }
- /* 12345 doesn't fit into 4 chars */
- return /*FALSE*/;
- }
- tempLength--; /* still have leading '0', */
- tempString++; /* can afford to drop it but retain EOL '\0' */
- }
+ width = sprintf(tempBuffer, "%0*"OFF_FMT"o", len, value);
+ tempString += (width - len);
+
+ /* If string has leading zeroes, we can drop one */
+ /* and field will have trailing '\0' */
+ /* (increases chances of compat with other tars) */
+ if (tempString[0] == '0')
+ tempString++;
- /* Copy the string to the field. */
+ /* Copy the string to the field */
memcpy(cp, tempString, len);
- /*return TRUE;*/
+}
+#define PUT_OCTAL(a, b) putOctal((a), sizeof(a), (b))
+
+static void chksum_and_xwrite(int fd, struct TarHeader* hp)
+{
+ const unsigned char *cp;
+ int chksum, size;
+
+ strcpy(hp->magic, "ustar ");
+
+ /* Calculate and store the checksum (i.e., the sum of all of the bytes of
+ * the header). The checksum field must be filled with blanks for the
+ * calculation. The checksum field is formatted differently from the
+ * other fields: it has 6 digits, a null, then a space -- rather than
+ * digits, followed by a null like the other fields... */
+ memset(hp->chksum, ' ', sizeof(hp->chksum));
+ cp = (const unsigned char *) hp;
+ chksum = 0;
+ size = sizeof(*hp);
+ do { chksum += *cp++; } while (--size);
+ putOctal(hp->chksum, sizeof(hp->chksum)-1, chksum);
+
+ /* Now write the header out to disk */
+ xwrite(fd, hp, sizeof(*hp));
}
+#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
+static void writeLongname(int fd, int type, const char *name, int dir)
+{
+ static const struct {
+ char mode[8]; /* 100-107 */
+ char uid[8]; /* 108-115 */
+ char gid[8]; /* 116-123 */
+ char size[12]; /* 124-135 */
+ char mtime[12]; /* 136-147 */
+ } prefilled = {
+ "0000000",
+ "0000000",
+ "0000000",
+ "00000000000",
+ "00000000000",
+ };
+ struct TarHeader header;
+ int size;
+
+ dir = !!dir; /* normalize: 0/1 */
+ size = strlen(name) + 1 + dir; /* GNU tar uses strlen+1 */
+ /* + dir: account for possible '/' */
+
+ memset(&header, 0, sizeof(header));
+ strcpy(header.name, "././@LongLink");
+ memcpy(header.mode, prefilled.mode, sizeof(prefilled));
+ PUT_OCTAL(header.size, size);
+ header.typeflag = type;
+ chksum_and_xwrite(fd, &header);
+
+ /* Write filename[/] and pad the block. */
+ /* dir=0: writes 'name<NUL>', pads */
+ /* dir=1: writes 'name', writes '/<NUL>', pads */
+ dir *= 2;
+ xwrite(fd, name, size - dir);
+ xwrite(fd, "/", dir);
+ size = (-size) & (TAR_BLOCK_SIZE-1);
+ memset(&header, 0, size);
+ xwrite(fd, &header, size);
+}
+#endif
+
/* Write out a tar header for the specified file/directory/whatever */
void BUG_tar_header_size(void);
static int writeTarHeader(struct TarBallInfo *tbInfo,
const char *header_name, const char *fileName, struct stat *statbuf)
{
struct TarHeader header;
- const unsigned char *cp;
- int chksum;
- int size;
if (sizeof(header) != 512)
BUG_tar_header_size();
- bzero(&header, sizeof(struct TarHeader));
+ memset(&header, 0, sizeof(struct TarHeader));
- safe_strncpy(header.name, header_name, sizeof(header.name));
+ strncpy(header.name, header_name, sizeof(header.name));
/* POSIX says to mask mode with 07777. */
- putOctal(header.mode, sizeof(header.mode), statbuf->st_mode & 07777);
- putOctal(header.uid, sizeof(header.uid), statbuf->st_uid);
- putOctal(header.gid, sizeof(header.gid), statbuf->st_gid);
+ PUT_OCTAL(header.mode, statbuf->st_mode & 07777);
+ PUT_OCTAL(header.uid, statbuf->st_uid);
+ PUT_OCTAL(header.gid, statbuf->st_gid);
memset(header.size, '0', sizeof(header.size)-1); /* Regular file size is handled later */
- putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime);
- strcpy(header.magic, "ustar ");
+ PUT_OCTAL(header.mtime, statbuf->st_mtime);
/* Enter the user and group names */
safe_strncpy(header.uname, get_cached_username(statbuf->st_uid), sizeof(header.uname));
header.typeflag = LNKTYPE;
strncpy(header.linkname, tbInfo->hlInfo->name,
sizeof(header.linkname));
+#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
+ /* Write out long linkname if needed */
+ if (header.linkname[sizeof(header.linkname)-1])
+ writeLongname(tbInfo->tarFd, GNULONGLINK,
+ tbInfo->hlInfo->name, 0);
+#endif
} else if (S_ISLNK(statbuf->st_mode)) {
char *lpath = xreadlink(fileName);
if (!lpath) /* Already printed err msg inside xreadlink() */
return FALSE;
header.typeflag = SYMTYPE;
strncpy(header.linkname, lpath, sizeof(header.linkname));
+#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
+ /* Write out long linkname if needed */
+ if (header.linkname[sizeof(header.linkname)-1])
+ writeLongname(tbInfo->tarFd, GNULONGLINK, lpath, 0);
+#else
/* If it is larger than 100 bytes, bail out */
- if (header.linkname[sizeof(header.linkname)-1] /* at least 100? */
- && lpath[sizeof(header.linkname)] /* and 101th is also not zero */
- ) {
+ if (header.linkname[sizeof(header.linkname)-1]) {
free(lpath);
bb_error_msg("names longer than "NAME_SIZE_STR" chars not supported");
return FALSE;
}
+#endif
free(lpath);
} else if (S_ISDIR(statbuf->st_mode)) {
header.typeflag = DIRTYPE;
- strncat(header.name, "/", sizeof(header.name));
+ /* Append '/' only if there is a space for it */
+ if (!header.name[sizeof(header.name)-1])
+ header.name[strlen(header.name)] = '/';
} else if (S_ISCHR(statbuf->st_mode)) {
header.typeflag = CHRTYPE;
- putOctal(header.devmajor, sizeof(header.devmajor),
- major(statbuf->st_rdev));
- putOctal(header.devminor, sizeof(header.devminor),
- minor(statbuf->st_rdev));
+ PUT_OCTAL(header.devmajor, major(statbuf->st_rdev));
+ PUT_OCTAL(header.devminor, minor(statbuf->st_rdev));
} else if (S_ISBLK(statbuf->st_mode)) {
header.typeflag = BLKTYPE;
- putOctal(header.devmajor, sizeof(header.devmajor),
- major(statbuf->st_rdev));
- putOctal(header.devminor, sizeof(header.devminor),
- minor(statbuf->st_rdev));
+ PUT_OCTAL(header.devmajor, major(statbuf->st_rdev));
+ PUT_OCTAL(header.devminor, minor(statbuf->st_rdev));
} else if (S_ISFIFO(statbuf->st_mode)) {
header.typeflag = FIFOTYPE;
} else if (S_ISREG(statbuf->st_mode)) {
+ if (sizeof(statbuf->st_size) > 4
+ && statbuf->st_size > (off_t)0777777777777LL
+ ) {
+ bb_error_msg_and_die("cannot store file '%s' "
+ "of size %"OFF_FMT"d, aborting",
+ fileName, statbuf->st_size);
+ }
header.typeflag = REGTYPE;
- putOctal(header.size, sizeof(header.size), statbuf->st_size);
+ PUT_OCTAL(header.size, statbuf->st_size);
} else {
bb_error_msg("%s: unknown file type", fileName);
return FALSE;
}
- /* Calculate and store the checksum (i.e., the sum of all of the bytes of
- * the header). The checksum field must be filled with blanks for the
- * calculation. The checksum field is formatted differently from the
- * other fields: it has [6] digits, a null, then a space -- rather than
- * digits, followed by a null like the other fields... */
- memset(header.chksum, ' ', sizeof(header.chksum));
- cp = (const unsigned char *) &header;
- chksum = 0;
- size = sizeof(struct TarHeader);
- do { chksum += *cp++; } while (--size);
- putOctal(header.chksum, sizeof(header.chksum)-1, chksum);
+#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
+ /* Write out long name if needed */
+ /* (we, like GNU tar, output long linkname *before* long name) */
+ if (header.name[sizeof(header.name)-1])
+ writeLongname(tbInfo->tarFd, GNULONGNAME,
+ header_name, S_ISDIR(statbuf->st_mode));
+#endif
/* Now write the header out to disk */
- xwrite(tbInfo->tarFd, &header, sizeof(struct TarHeader));
+ chksum_and_xwrite(tbInfo->tarFd, &header);
/* Now do the verbose thing (or not) */
-
if (tbInfo->verboseFlag) {
FILE *vbFd = stdout;
if (tbInfo->tarFd == STDOUT_FILENO) /* If the archive goes to stdout, verbose to stderr */
vbFd = stderr;
-
- fprintf(vbFd, "%s\n", header.name);
+ /* GNU "tar cvvf" prints "extended" listing a-la "ls -l" */
+ /* We don't have such excesses here: for us "v" == "vv" */
+ /* '/' is probably a GNUism */
+ fprintf(vbFd, "%s%s\n", header_name,
+ S_ISDIR(statbuf->st_mode) ? "/" : "");
}
return TRUE;
header_name++;
}
+#if !ENABLE_FEATURE_TAR_GNU_EXTENSIONS
if (strlen(fileName) >= NAME_SIZE) {
bb_error_msg("names longer than "NAME_SIZE_STR" chars not supported");
return TRUE;
}
+#endif
if (header_name[0] == '\0')
return TRUE;
/* If it was a regular file, write out the body */
if (inputFileFd >= 0) {
- off_t readSize = 0;
-
- /* write the file to the archive */
- readSize = bb_copyfd_size(inputFileFd, tbInfo->tarFd, statbuf->st_size);
- if (readSize != statbuf->st_size) {
- /* Deadly. We record size into header first, */
- /* and then write out file. If file shrinks in between, */
- /* tar will be corrupted. So bail out. */
- /* NB: GNU tar 1.16 warns and pads with zeroes */
- /* or even seeks back and updates header */
- bb_error_msg_and_die("short read from %s, aborting", fileName);
- }
+ size_t readSize;
+ /* Wwrite the file to the archive. */
+ /* We record size into header first, */
+ /* and then write out file. If file shrinks in between, */
+ /* tar will be corrupted. So we don't allow for that. */
+ /* NB: GNU tar 1.16 warns and pads with zeroes */
+ /* or even seeks back and updates header */
+ bb_copyfd_exact_size(inputFileFd, tbInfo->tarFd, statbuf->st_size);
+ ////off_t readSize;
+ ////readSize = bb_copyfd_size(inputFileFd, tbInfo->tarFd, statbuf->st_size);
+ ////if (readSize != statbuf->st_size && readSize >= 0) {
+ //// bb_error_msg_and_die("short read from %s, aborting", fileName);
+ ////}
+
/* Check that file did not grow in between? */
- /* if (safe_read(inputFileFd,1) == 1) warn but continue? */
+ /* if (safe_read(inputFileFd, 1) == 1) warn but continue? */
+
close(inputFileFd);
/* Pad the file up to the tar block size */
/* (a few tricks here in the name of code size) */
- readSize = (-(int)readSize) & (TAR_BLOCK_SIZE-1);
- bzero(bb_common_bufsiz1, readSize);
+ readSize = (-(int)statbuf->st_size) & (TAR_BLOCK_SIZE-1);
+ memset(bb_common_bufsiz1, 0, readSize);
xwrite(tbInfo->tarFd, bb_common_bufsiz1, readSize);
}
volatile int vfork_exec_errno = 0;
char *zip_exec = (gzip == 1) ? "gzip" : "bzip2";
-
if (pipe(gzipDataPipe) < 0 || pipe(gzipStatusPipe) < 0)
bb_perror_msg_and_die("pipe");
include = include->link;
}
/* Write two empty blocks to the end of the archive */
- bzero(bb_common_bufsiz1, 2*TAR_BLOCK_SIZE);
+ memset(bb_common_bufsiz1, 0, 2*TAR_BLOCK_SIZE);
xwrite(tbInfo.tarFd, bb_common_bufsiz1, 2*TAR_BLOCK_SIZE);
/* To be pedantically correct, we would check if the tarball
if (errorFlag)
bb_error_msg("error exit delayed from previous errors");
- if (gzipPid && waitpid(gzipPid, NULL, 0) == -1)
- bb_error_msg("waitpid failed");
-
- return !errorFlag;
+ if (gzipPid) {
+ int status;
+ if (waitpid(gzipPid, &status, 0) == -1)
+ bb_perror_msg("waitpid");
+ else if (!WIFEXITED(status) || WEXITSTATUS(status))
+ /* gzip was killed or has exited with nonzero! */
+ errorFlag = TRUE;
+ }
+ return errorFlag;
}
#else
int writeTarFile(const int tar_fd, const int verboseFlag,
cur = cur->link;
free(tmp);
while ((line = xmalloc_getline(src_stream)) != NULL) {
- char *filename_ptr = last_char_is(line, '/');
- if (filename_ptr > line)
- *filename_ptr = '\0';
+ /* kill trailing '/' unless the string is just "/" */
+ char *cp = last_char_is(line, '/');
+ if (cp > line)
+ *cp = '\0';
llist_add_to(&newlist, line);
}
fclose(src_stream);
#define get_header_tar_Z 0
#endif
+#ifdef CHECK_FOR_CHILD_EXITCODE
+/* Looks like it isn't needed - tar detects malformed (truncated)
+ * archive if e.g. bunzip2 fails */
+static int child_error;
+
+static void handle_SIGCHLD(int status)
+{
+ /* Actually, 'status' is a signo. We reuse it for other needs */
+
+ /* Wait for any child without blocking */
+ if (waitpid(-1, &status, WNOHANG) < 0)
+ /* wait failed?! I'm confused... */
+ return;
+
+ if (WIFEXITED(status) && WEXITSTATUS(status)==0)
+ /* child exited with 0 */
+ return;
+ /* Cannot happen?
+ if(!WIFSIGNALED(status) && !WIFEXITED(status)) return; */
+ child_error = 1;
+}
+#endif
+
enum {
OPTBIT_KEEP_OLD = 7,
USE_FEATURE_TAR_CREATE( OPTBIT_CREATE ,)
# if ENABLE_FEATURE_TAR_FROM
{ "files-from", 1, NULL, 'T' },
{ "exclude-from", 1, NULL, 'X' },
- { "exclude", 1, NULL, 0xfd },
# endif
# if ENABLE_FEATURE_TAR_GZIP
{ "gzip", 0, NULL, 'z' },
# if ENABLE_FEATURE_TAR_COMPRESS
{ "compress", 0, NULL, 'Z' },
# endif
- { "no-same-owner", 0, NULL, 0xfe },
- { "no-same-permissions",0, NULL, 0xff },
+ { "no-same-owner", 0, NULL, 0xfd },
+ { "no-same-permissions",0, NULL, 0xfe },
+ /* --exclude takes next bit position in option mask, */
+ /* therefore we have to either put it _after_ --no-same-perm */
+ /* or add OPT[BIT]_EXCLUDE before OPT[BIT]_NOPRESERVE_OWN */
+# if ENABLE_FEATURE_TAR_FROM
+ { "exclude", 1, NULL, 0xff },
+# endif
{ 0, 0, 0, 0 }
};
-#else
-#define tar_long_options 0
#endif
int tar_main(int argc, char **argv)
char *base_dir = NULL;
const char *tar_filename = "-";
unsigned opt;
+ int verboseFlag = 0;
llist_t *excludes = NULL;
/* Initialise default values */
/* Prepend '-' to the first argument if required */
opt_complementary = "--:" // first arg is options
+ "tt:vv:" // count -t,-v
"?:" // bail out with usage instead of error return
"X::T::" // cumulative lists
- "\xfd::" // cumulative lists for --exclude
+ "\xff::" // cumulative lists for --exclude
USE_FEATURE_TAR_CREATE("c:") "t:x:" // at least one of these is reqd
USE_FEATURE_TAR_CREATE("c--tx:t--cx:x--ct") // mutually exclusive
SKIP_FEATURE_TAR_CREATE("t--x:x--t"); // mutually exclusive
- if (ENABLE_FEATURE_TAR_LONG_OPTIONS)
- applet_long_options = tar_long_options;
+#if ENABLE_FEATURE_TAR_LONG_OPTIONS
+ applet_long_options = tar_long_options;
+#endif
opt = getopt32(argc, argv,
"txC:f:Opvk"
USE_FEATURE_TAR_CREATE( "ch" )
&tar_filename, // -f filename
USE_FEATURE_TAR_FROM(&(tar_handle->accept),) // T
USE_FEATURE_TAR_FROM(&(tar_handle->reject),) // X
- USE_FEATURE_TAR_FROM(&excludes ) // --exclude
+ USE_FEATURE_TAR_FROM(&excludes ,) // --exclude
+ &verboseFlag, // combined count for -t and -v
+ &verboseFlag // combined count for -t and -v
);
- if (opt & OPT_TEST) {
- if (tar_handle->action_header == header_list
- || tar_handle->action_header == header_verbose_list
- ) {
- tar_handle->action_header = header_verbose_list;
- } else
- tar_handle->action_header = header_list;
- }
- if ((opt & OPT_EXTRACT) && tar_handle->action_data != data_extract_to_stdout)
+ if (verboseFlag) tar_handle->action_header = header_verbose_list;
+ if (verboseFlag == 1) tar_handle->action_header = header_list;
+
+ if (opt & OPT_EXTRACT)
tar_handle->action_data = data_extract_all;
if (opt & OPT_2STDOUT)
tar_handle->action_data = data_extract_to_stdout;
- if (opt & OPT_VERBOSE) {
- if (tar_handle->action_header == header_list
- || tar_handle->action_header == header_verbose_list
- ) {
- tar_handle->action_header = header_verbose_list;
- } else
- tar_handle->action_header = header_list;
- }
if (opt & OPT_KEEP_OLD)
tar_handle->flags &= ~ARCHIVE_EXTRACT_UNCONDITIONAL;
/* Setup an array of filenames to work with */
/* TODO: This is the same as in ar, separate function ? */
while (optind < argc) {
- char *filename_ptr = last_char_is(argv[optind], '/');
- if (filename_ptr > argv[optind])
- *filename_ptr = '\0';
-
- llist_add_to(&(tar_handle->accept), argv[optind]);
+ /* kill trailing '/' unless the string is just "/" */
+ char *cp = last_char_is(argv[optind], '/');
+ if (cp > argv[optind])
+ *cp = '\0';
+ llist_add_to(&tar_handle->accept, argv[optind]);
optind++;
}
+ tar_handle->accept = rev_llist(tar_handle->accept);
if (tar_handle->accept || tar_handle->reject)
tar_handle->filter = filter_accept_reject_list;
flags = O_RDONLY;
}
- if (tar_filename[0] == '-' && !tar_filename[1]) {
+ if (LONE_DASH(tar_filename)) {
tar_handle->src_fd = fileno(tar_stream);
tar_handle->seek = seek_by_read;
} else {
- tar_handle->src_fd = xopen3(tar_filename, flags, 0666);
+ tar_handle->src_fd = xopen(tar_filename, flags);
}
}
if (base_dir)
xchdir(base_dir);
+#ifdef CHECK_FOR_CHILD_EXITCODE
+ /* We need to know whether child (gzip/bzip/etc) exits abnormally */
+ signal(SIGCHLD, handle_SIGCHLD);
+#endif
+
/* create an archive */
if (opt & OPT_CREATE) {
- int verboseFlag = FALSE;
int zipMode = 0;
-
if (ENABLE_FEATURE_TAR_GZIP && get_header_ptr == get_header_tar_gz)
zipMode = 1;
if (ENABLE_FEATURE_TAR_BZIP2 && get_header_ptr == get_header_tar_bz2)
zipMode = 2;
-
- if (tar_handle->action_header == header_list
- || tar_handle->action_header == header_verbose_list
- ) {
- verboseFlag = TRUE;
- }
- writeTarFile(tar_handle->src_fd, verboseFlag, opt & OPT_DEREFERENCE,
- tar_handle->accept,
- tar_handle->reject, zipMode);
/* NB: writeTarFile() closes tar_handle->src_fd */
- return EXIT_SUCCESS;
+ return writeTarFile(tar_handle->src_fd, verboseFlag, opt & OPT_DEREFERENCE,
+ tar_handle->accept,
+ tar_handle->reject, zipMode);
}
while (get_header_ptr(tar_handle) == EXIT_SUCCESS)