tar: fix multiple -t and/or -v options handling.

do not process list of files to tar up in reverse order.
This commit is contained in:
Denis Vlasenko 2006-11-24 21:55:55 +00:00
parent d031ffa623
commit 3feb2fc535
2 changed files with 28 additions and 44 deletions

View File

@ -276,13 +276,13 @@ static int writeTarHeader(struct TarBallInfo *tbInfo,
xwrite(tbInfo->tarFd, &header, sizeof(struct TarHeader)); xwrite(tbInfo->tarFd, &header, sizeof(struct TarHeader));
/* Now do the verbose thing (or not) */ /* Now do the verbose thing (or not) */
if (tbInfo->verboseFlag) { if (tbInfo->verboseFlag) {
FILE *vbFd = stdout; FILE *vbFd = stdout;
if (tbInfo->tarFd == STDOUT_FILENO) /* If the archive goes to stdout, verbose to stderr */ if (tbInfo->tarFd == STDOUT_FILENO) /* If the archive goes to stdout, verbose to stderr */
vbFd = stderr; vbFd = stderr;
/* GNU "tar cvvf" prints "extended" listing a-la "ls -l" */
/* We don't have such excesses here: for us "v" == "vv" */
fprintf(vbFd, "%s\n", header.name); fprintf(vbFd, "%s\n", header.name);
} }
@ -549,9 +549,10 @@ static llist_t *append_file_list_to_list(llist_t *list)
cur = cur->link; cur = cur->link;
free(tmp); free(tmp);
while ((line = xmalloc_getline(src_stream)) != NULL) { while ((line = xmalloc_getline(src_stream)) != NULL) {
char *filename_ptr = last_char_is(line, '/'); /* kill trailing '/' unless the string is just "/" */
if (filename_ptr > line) char *cp = last_char_is(line, '/');
*filename_ptr = '\0'; if (cp > line)
*cp = '\0';
llist_add_to(&newlist, line); llist_add_to(&newlist, line);
} }
fclose(src_stream); fclose(src_stream);
@ -664,6 +665,7 @@ int tar_main(int argc, char **argv)
char *base_dir = NULL; char *base_dir = NULL;
const char *tar_filename = "-"; const char *tar_filename = "-";
unsigned opt; unsigned opt;
int verboseFlag = 0;
llist_t *excludes = NULL; llist_t *excludes = NULL;
/* Initialise default values */ /* Initialise default values */
@ -674,6 +676,7 @@ int tar_main(int argc, char **argv)
/* Prepend '-' to the first argument if required */ /* Prepend '-' to the first argument if required */
opt_complementary = "--:" // first arg is options opt_complementary = "--:" // first arg is options
"tt:vv:" // count -t,-v
"?:" // bail out with usage instead of error return "?:" // bail out with usage instead of error return
"X::T::" // cumulative lists "X::T::" // cumulative lists
"\xfd::" // cumulative lists for --exclude "\xfd::" // cumulative lists for --exclude
@ -695,31 +698,20 @@ int tar_main(int argc, char **argv)
&tar_filename, // -f filename &tar_filename, // -f filename
USE_FEATURE_TAR_FROM(&(tar_handle->accept),) // T USE_FEATURE_TAR_FROM(&(tar_handle->accept),) // T
USE_FEATURE_TAR_FROM(&(tar_handle->reject),) // X 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 (verboseFlag) tar_handle->action_header = header_verbose_list;
if (tar_handle->action_header == header_list if (verboseFlag == 1) 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 ((opt & OPT_EXTRACT) && tar_handle->action_data != data_extract_to_stdout)
tar_handle->action_data = data_extract_all; tar_handle->action_data = data_extract_all;
if (opt & OPT_2STDOUT) if (opt & OPT_2STDOUT)
tar_handle->action_data = data_extract_to_stdout; 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) if (opt & OPT_KEEP_OLD)
tar_handle->flags &= ~ARCHIVE_EXTRACT_UNCONDITIONAL; tar_handle->flags &= ~ARCHIVE_EXTRACT_UNCONDITIONAL;
@ -762,13 +754,14 @@ int tar_main(int argc, char **argv)
/* Setup an array of filenames to work with */ /* Setup an array of filenames to work with */
/* TODO: This is the same as in ar, separate function ? */ /* TODO: This is the same as in ar, separate function ? */
while (optind < argc) { while (optind < argc) {
char *filename_ptr = last_char_is(argv[optind], '/'); /* kill trailing '/' unless the string is just "/" */
if (filename_ptr > argv[optind]) char *cp = last_char_is(argv[optind], '/');
*filename_ptr = '\0'; if (cp > argv[optind])
*cp = '\0';
llist_add_to(&(tar_handle->accept), argv[optind]); llist_add_to(&tar_handle->accept, argv[optind]);
optind++; optind++;
} }
tar_handle->accept = rev_llist(tar_handle->accept);
if (tar_handle->accept || tar_handle->reject) if (tar_handle->accept || tar_handle->reject)
tar_handle->filter = filter_accept_reject_list; tar_handle->filter = filter_accept_reject_list;
@ -805,22 +798,14 @@ int tar_main(int argc, char **argv)
/* create an archive */ /* create an archive */
if (opt & OPT_CREATE) { if (opt & OPT_CREATE) {
int verboseFlag = FALSE;
int zipMode = 0; int zipMode = 0;
if (ENABLE_FEATURE_TAR_GZIP && get_header_ptr == get_header_tar_gz) if (ENABLE_FEATURE_TAR_GZIP && get_header_ptr == get_header_tar_gz)
zipMode = 1; zipMode = 1;
if (ENABLE_FEATURE_TAR_BZIP2 && get_header_ptr == get_header_tar_bz2) if (ENABLE_FEATURE_TAR_BZIP2 && get_header_ptr == get_header_tar_bz2)
zipMode = 2; 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, writeTarFile(tar_handle->src_fd, verboseFlag, opt & OPT_DEREFERENCE,
tar_handle->accept, tar_handle->accept,
tar_handle->reject, zipMode); tar_handle->reject, zipMode);
/* NB: writeTarFile() closes tar_handle->src_fd */ /* NB: writeTarFile() closes tar_handle->src_fd */
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -7,20 +7,19 @@
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
*/ */
#include <string.h>
#include "libbb.h" #include "libbb.h"
/* Find out if the last character of a string matches the one given Don't /* Find out if the last character of a string matches the one given Don't
* underrun the buffer if the string length is 0. Also avoids a possible * underrun the buffer if the string length is 0. Also avoids a possible
* space-hogging inline of strlen() per usage. * space-hogging inline of strlen() per usage.
*/ */
char * last_char_is(const char *s, int c) char* last_char_is(const char *s, int c)
{ {
char *sret = (char *)s; char *sret;
if (sret) { if (s) {
sret = strrchr(sret, c); sret = strrchr(s, c);
if(sret != NULL && *(sret+1) != 0) if (sret && !sret[1])
sret = NULL; return sret;
} }
return sret; return NULL;
} }