X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=archival%2Ftar.c;h=4e763a404476118ab884f87971e2df5ecc81dcc4;hb=56f16b42c93af18fbb984e8d6384c03e5405e3ae;hp=c39d5787fee108e9d9bd5dfda8bf26218e0c5537;hpb=cad5364599eb5062d59e0c397ed638ddd61a8d5d;p=oweals%2Fbusybox.git diff --git a/archival/tar.c b/archival/tar.c index c39d5787f..4e763a404 100644 --- a/archival/tar.c +++ b/archival/tar.c @@ -9,8 +9,7 @@ * ground up. It still has remnents of the old code lying about, but it is * very different now (i.e., cleaner, less global variables, etc.) * - * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen - * Copyright (C) 1999-2002 by Erik Andersen + * Copyright (C) 1999-2003 by Erik Andersen * * Based in part in the tar implementation in sash * Copyright (c) 1999 by David I. Bell @@ -309,8 +308,9 @@ static inline int writeTarHeader(struct TarBallInfo *tbInfo, if (tbInfo->verboseFlag) { FILE *vbFd = stdout; - if (tbInfo->verboseFlag == 2) /* If the archive goes to stdout, verbose to stderr */ + if (tbInfo->tarFd == fileno(stdout)) /* If the archive goes to stdout, verbose to stderr */ vbFd = stderr; + fprintf(vbFd, "%s\n", header.name); } @@ -446,7 +446,7 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf, return (TRUE); } -static inline int writeTarFile(const char *tarName, const int verboseFlag, +static inline int writeTarFile(const int tar_fd, const int verboseFlag, const llist_t *include, const llist_t *exclude, const int gzip) { #ifdef CONFIG_FEATURE_TAR_GZIP @@ -467,25 +467,14 @@ static inline int writeTarFile(const char *tarName, const int verboseFlag, bb_error_msg_and_die("Cowardly refusing to create an empty archive"); } - /* Open the tar file for writing. */ - if (tarName == NULL || (tarName[0] == '-' && tarName[1] == '\0')) { - tbInfo.tarFd = fileno(stdout); - tbInfo.verboseFlag = verboseFlag ? 2 : 0; - } else { - tbInfo.tarFd = open(tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644); - tbInfo.verboseFlag = verboseFlag ? 1 : 0; - } - - if (tbInfo.tarFd < 0) { - bb_perror_msg("%s: Cannot open", tarName); - freeHardLinkInfo(&tbInfo.hlInfoHead); - return (FALSE); - } + fchmod(tar_fd, 0644); + tbInfo.tarFd = tar_fd; + tbInfo.verboseFlag = verboseFlag; /* Store the stat info for the tarball's file, so * can avoid including the tarball into itself.... */ if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0) - bb_error_msg_and_die(bb_msg_io_error, tarName); + bb_perror_msg_and_die("Couldnt stat tar file"); #ifdef CONFIG_FEATURE_TAR_GZIP if (gzip) { @@ -525,7 +514,7 @@ static inline int writeTarFile(const char *tarName, const int verboseFlag, while (1) { char buf; - int n = read(gzipStatusPipe[0], &buf, 1); + int n = bb_full_read(gzipStatusPipe[0], &buf, 1); if (n == 0 && vfork_exec_errno != 0) { errno = vfork_exec_errno; @@ -583,22 +572,64 @@ static inline int writeTarFile(const char *tarName, const int verboseFlag, #endif /* tar_create */ #ifdef CONFIG_FEATURE_TAR_EXCLUDE -static llist_t *append_file_list_to_list(const char *filename, llist_t *list) +static llist_t *append_file_list_to_list(llist_t *list) { - FILE *src_stream = bb_xfopen(filename, "r"); + FILE *src_stream; + llist_t *cur = list; + llist_t *tmp; char *line; + llist_t *newlist = NULL; + + while(cur) { + src_stream = bb_xfopen(cur->data, "r"); + tmp = cur; + cur = cur->link; + free(tmp); while((line = bb_get_chomped_line_from_file(src_stream)) != NULL) { - list = llist_add_to(list, line); + newlist = llist_add_to(newlist, line); } fclose(src_stream); + } + return newlist; +} +#endif - return (list); +#ifdef CONFIG_FEATURE_TAR_COMPRESS +static char get_header_tar_Z(archive_handle_t *archive_handle) +{ + /* Cant lseek over pipe's */ + archive_handle->seek = seek_by_char; + + /* do the decompression, and cleanup */ + if ((bb_xread_char(archive_handle->src_fd) != 0x1f) || (bb_xread_char(archive_handle->src_fd) != 0x9d)) { + bb_error_msg_and_die("Invalid magic"); + } + + archive_handle->src_fd = open_transformer(archive_handle->src_fd, uncompress); + archive_handle->offset = 0; + while (get_header_tar(archive_handle) == EXIT_SUCCESS); + + /* Can only do one file at a time */ + return(EXIT_FAILURE); } #endif +static const char tar_options[]="ctxjT:X:C:f:OpvzkZ"; + #define CTX_CREATE 1 #define CTX_TEST 2 #define CTX_EXTRACT 4 +#define TAR_OPT_BZIP2 8 +#define TAR_OPT_INCLUDE 16 +#define TAR_OPT_EXCLUDE 32 +#define TAR_OPT_BASEDIR 64 +#define TAR_OPT_ARNAME 128 +#define TAR_OPT_2STDOUT 256 +#define TAR_OPT_P 512 +#define TAR_OPT_VERBOSE 1024 +#define TAR_OPT_GZIP 2048 +#define TAR_OPT_KEEP_OLD 4096 +#define TAR_OPT_UNCOMPRESS 8192 int tar_main(int argc, char **argv) { @@ -615,89 +646,84 @@ int tar_main(int argc, char **argv) /* Prepend '-' to the first argument if required */ if (argv[1][0] != '-') { - char *tmp = xmalloc(strlen(argv[1]) + 2); - tmp[0] = '-'; - strcpy(tmp + 1, argv[1]); + char *tmp; + + bb_xasprintf(&tmp, "-%s", argv[1]); argv[1] = tmp; } /* Initialise default values */ tar_handle = init_handle(); - tar_handle->flags = ARCHIVE_CREATE_LEADING_DIRS; - - while ((opt = getopt(argc, argv, "cjtxT:X:C:f:Opvz")) != -1) { - switch (opt) { - /* One and only one of these is required */ -#ifdef CONFIG_FEATURE_TAR_CREATE - case 'c': - ctx_flag |= CTX_CREATE; - break; -#endif - case 't': - ctx_flag |= CTX_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; - } - break; - case 'x': - ctx_flag |= CTX_EXTRACT; + tar_handle->flags = ARCHIVE_CREATE_LEADING_DIRS | ARCHIVE_PRESERVE_DATE | ARCHIVE_EXTRACT_UNCONDITIONAL; + + bb_opt_complementaly = "c~tx:t~cx:x~ct:X*"; + opt = bb_getopt_ulflags(argc, argv, tar_options, + NULL, /* T: arg is ignored by default + a list is an include list */ + &(tar_handle->reject), + &base_dir, /* Change to dir */ + &tar_filename); /* archive filename */ + /* Check one and only one context option was given */ + if(opt & 0x80000000UL) + bb_show_usage(); + ctx_flag = opt & (CTX_CREATE | CTX_TEST | CTX_EXTRACT); + if(ctx_flag & CTX_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(ctx_flag & CTX_EXTRACT) { + if (tar_handle->action_data != data_extract_to_stdout) tar_handle->action_data = data_extract_all; - break; + } + if(opt & TAR_OPT_2STDOUT) { + /* To stdout */ + tar_handle->action_data = data_extract_to_stdout; + } + if(opt & TAR_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 & TAR_OPT_KEEP_OLD) { + tar_handle->flags &= ~ARCHIVE_EXTRACT_UNCONDITIONAL; + } - /* These are optional */ - /* Exclude or Include files listed in */ -#ifdef CONFIG_FEATURE_TAR_EXCLUDE - case 'X': - tar_handle->reject = - append_file_list_to_list(optarg, tar_handle->reject); - break; -#endif - case 'T': - /* by default a list is an include list */ - break; - case 'C': /* Change to dir */ - base_dir = optarg; - break; - case 'f': /* archive filename */ - tar_filename = optarg; - break; - case 'O': /* To stdout */ - tar_handle->action_data = data_extract_to_stdout; - break; - case 'p': - tar_handle->flags |= ARCHIVE_PRESERVE_DATE; - break; - case 'v': - 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; - } - break; + if(opt & TAR_OPT_GZIP) { #ifdef CONFIG_FEATURE_TAR_GZIP - case 'z': - get_header_ptr = get_header_tar_gz; - break; + get_header_ptr = get_header_tar_gz; +#else + bb_show_usage(); #endif + } + if(opt & TAR_OPT_BZIP2) { #ifdef CONFIG_FEATURE_TAR_BZIP2 - case 'j': - get_header_ptr = get_header_tar_bz2; - break; + get_header_ptr = get_header_tar_bz2; +#else + bb_show_usage(); #endif - default: - bb_show_usage(); - } } - - /* Check one and only one context option was given */ - if ((ctx_flag != CTX_CREATE) && (ctx_flag != CTX_TEST) && (ctx_flag != CTX_EXTRACT)) { + if(opt & TAR_OPT_UNCOMPRESS) { +#ifdef CONFIG_FEATURE_TAR_COMPRESS + get_header_ptr = get_header_tar_Z; +#else bb_show_usage(); +#endif + } + if(opt & TAR_OPT_EXCLUDE) { +#ifdef CONFIG_FEATURE_TAR_EXCLUDE + tar_handle->reject = append_file_list_to_list(tar_handle->reject); +#else + bb_show_usage(); +#endif } - /* Check if we are reading from stdin */ if ((argv[optind]) && (*argv[optind] == '-')) { /* Default is to read from stdin, so just skip to next arg */ @@ -715,6 +741,35 @@ int tar_main(int argc, char **argv) tar_handle->filter = filter_accept_reject_list; } + /* Open the tar file */ + { + FILE *tar_stream; + int flags; + +#ifdef CONFIG_FEATURE_TAR_CREATE + if (ctx_flag == CTX_CREATE) { + tar_stream = stdout; + flags = O_WRONLY | O_CREAT | O_EXCL; + unlink(tar_filename); + } else +#endif + { + tar_stream = stdin; + flags = O_RDONLY; + } + + if ((tar_filename[0] == '-') && (tar_filename[1] == '\0')) { + tar_handle->src_fd = fileno(tar_stream); + tar_handle->seek = seek_by_char; + } else { + tar_handle->src_fd = bb_xopen(tar_filename, flags); + } + } + + if ((base_dir) && (chdir(base_dir))) { + bb_perror_msg_and_die("Couldnt chdir to %s", base_dir); + } + #ifdef CONFIG_FEATURE_TAR_CREATE /* create an archive */ if (ctx_flag == CTX_CREATE) { @@ -726,26 +781,21 @@ int tar_main(int argc, char **argv) gzipFlag = TRUE; } # endif /* CONFIG_FEATURE_TAR_GZIP */ +# ifdef CONFIG_FEATURE_TAR_BZIP2 + if (get_header_ptr == get_header_tar_bz2) { + bb_error_msg_and_die("Creating bzip2 compressed archives is not currently supported."); + } +# endif /* CONFIG_FEATURE_TAR_BZIP2 */ - if (tar_handle->action_header == header_verbose_list) { + if ((tar_handle->action_header == header_list) || + (tar_handle->action_header == header_verbose_list)) { verboseFlag = TRUE; } - writeTarFile(tar_filename, verboseFlag, tar_handle->accept, + writeTarFile(tar_handle->src_fd, verboseFlag, tar_handle->accept, tar_handle->reject, gzipFlag); } else #endif /* CONFIG_FEATURE_TAR_CREATE */ { - if ((tar_filename[0] == '-') && (tar_filename[1] == '\0')) { - tar_handle->src_fd = fileno(stdin); - tar_handle->seek = seek_by_char; - } else { - tar_handle->src_fd = bb_xopen(tar_filename, O_RDONLY); - } - - if ((base_dir) && (chdir(base_dir))) { - bb_perror_msg_and_die("Couldnt chdir"); - } - while (get_header_ptr(tar_handle) == EXIT_SUCCESS); /* Ckeck that every file that should have been extracted was */