X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=archival%2Ftar.c;h=4e763a404476118ab884f87971e2df5ecc81dcc4;hb=56f16b42c93af18fbb984e8d6384c03e5405e3ae;hp=06851e854aee6978dd346dabe9773d761112f5a9;hpb=27cb6846d7951a6d5a9616aa845a58ff21d6f41e;p=oweals%2Fbusybox.git diff --git a/archival/tar.c b/archival/tar.c index 06851e854..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,26 +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 { - unlink(tarName); - tbInfo.tarFd = open(tarName, O_WRONLY | O_CREAT | O_EXCL, 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) { @@ -526,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; @@ -606,8 +594,28 @@ static llist_t *append_file_list_to_list(llist_t *list) } #endif +#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"; -static const char tar_options[]="ctxjT:X:C:f:Opvz"; #define CTX_CREATE 1 #define CTX_TEST 2 #define CTX_EXTRACT 4 @@ -620,6 +628,8 @@ static const char tar_options[]="ctxjT:X:C:f:Opvz"; #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) { @@ -644,7 +654,7 @@ int tar_main(int argc, char **argv) /* Initialise default values */ tar_handle = init_handle(); - tar_handle->flags = ARCHIVE_CREATE_LEADING_DIRS | ARCHIVE_PRESERVE_DATE; + 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, @@ -658,44 +668,62 @@ int tar_main(int argc, char **argv) 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 ((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; - } + tar_handle->action_data = data_extract_all; + } if(opt & TAR_OPT_2STDOUT) { /* To stdout */ - tar_handle->action_data = data_extract_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 ((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; + } } -#ifdef CONFIG_FEATURE_TAR_GZIP - if(opt & TAR_OPT_GZIP) { - get_header_ptr = get_header_tar_gz; + if (opt & TAR_OPT_KEEP_OLD) { + tar_handle->flags &= ~ARCHIVE_EXTRACT_UNCONDITIONAL; } + + if(opt & TAR_OPT_GZIP) { +#ifdef CONFIG_FEATURE_TAR_GZIP + get_header_ptr = get_header_tar_gz; +#else + bb_show_usage(); #endif + } + if(opt & TAR_OPT_BZIP2) { #ifdef CONFIG_FEATURE_TAR_BZIP2 - if(opt & TAR_OPT_GZIP) { - get_header_ptr = get_header_tar_bz2; + get_header_ptr = get_header_tar_bz2; +#else + bb_show_usage(); +#endif } + if(opt & TAR_OPT_UNCOMPRESS) { +#ifdef CONFIG_FEATURE_TAR_COMPRESS + get_header_ptr = get_header_tar_Z; +#else + bb_show_usage(); #endif -#ifdef CONFIG_FEATURE_TAR_EXCLUDE + } 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 */ @@ -713,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) { @@ -724,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 */