diff options
Diffstat (limited to 'archival')
-rw-r--r-- | archival/tar.c | 67 |
1 files changed, 37 insertions, 30 deletions
diff --git a/archival/tar.c b/archival/tar.c index 2f0c83b27..72b4c9952 100644 --- a/archival/tar.c +++ b/archival/tar.c | |||
@@ -308,8 +308,9 @@ static inline int writeTarHeader(struct TarBallInfo *tbInfo, | |||
308 | if (tbInfo->verboseFlag) { | 308 | if (tbInfo->verboseFlag) { |
309 | FILE *vbFd = stdout; | 309 | FILE *vbFd = stdout; |
310 | 310 | ||
311 | if (tbInfo->verboseFlag == 2) /* If the archive goes to stdout, verbose to stderr */ | 311 | if (tbInfo->tarFd == fileno(stdout)) /* If the archive goes to stdout, verbose to stderr */ |
312 | vbFd = stderr; | 312 | vbFd = stderr; |
313 | |||
313 | fprintf(vbFd, "%s\n", header.name); | 314 | fprintf(vbFd, "%s\n", header.name); |
314 | } | 315 | } |
315 | 316 | ||
@@ -445,7 +446,7 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf, | |||
445 | return (TRUE); | 446 | return (TRUE); |
446 | } | 447 | } |
447 | 448 | ||
448 | static inline int writeTarFile(const char *tarName, const int verboseFlag, | 449 | static inline int writeTarFile(const int tar_fd, const int verboseFlag, |
449 | const llist_t *include, const llist_t *exclude, const int gzip) | 450 | const llist_t *include, const llist_t *exclude, const int gzip) |
450 | { | 451 | { |
451 | #ifdef CONFIG_FEATURE_TAR_GZIP | 452 | #ifdef CONFIG_FEATURE_TAR_GZIP |
@@ -466,26 +467,14 @@ static inline int writeTarFile(const char *tarName, const int verboseFlag, | |||
466 | bb_error_msg_and_die("Cowardly refusing to create an empty archive"); | 467 | bb_error_msg_and_die("Cowardly refusing to create an empty archive"); |
467 | } | 468 | } |
468 | 469 | ||
469 | /* Open the tar file for writing. */ | 470 | fchmod(tar_fd, 0644); |
470 | if (tarName == NULL || (tarName[0] == '-' && tarName[1] == '\0')) { | 471 | tbInfo.tarFd = tar_fd; |
471 | tbInfo.tarFd = fileno(stdout); | 472 | tbInfo.verboseFlag = verboseFlag; |
472 | tbInfo.verboseFlag = verboseFlag ? 2 : 0; | ||
473 | } else { | ||
474 | unlink(tarName); | ||
475 | tbInfo.tarFd = open(tarName, O_WRONLY | O_CREAT | O_EXCL, 0644); | ||
476 | tbInfo.verboseFlag = verboseFlag ? 1 : 0; | ||
477 | } | ||
478 | |||
479 | if (tbInfo.tarFd < 0) { | ||
480 | bb_perror_msg("%s: Cannot open", tarName); | ||
481 | freeHardLinkInfo(&tbInfo.hlInfoHead); | ||
482 | return (FALSE); | ||
483 | } | ||
484 | 473 | ||
485 | /* Store the stat info for the tarball's file, so | 474 | /* Store the stat info for the tarball's file, so |
486 | * can avoid including the tarball into itself.... */ | 475 | * can avoid including the tarball into itself.... */ |
487 | if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0) | 476 | if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0) |
488 | bb_error_msg_and_die(bb_msg_io_error, tarName); | 477 | bb_perror_msg_and_die("Couldnt stat tar file"); |
489 | 478 | ||
490 | #ifdef CONFIG_FEATURE_TAR_GZIP | 479 | #ifdef CONFIG_FEATURE_TAR_GZIP |
491 | if (gzip) { | 480 | if (gzip) { |
@@ -713,6 +702,35 @@ int tar_main(int argc, char **argv) | |||
713 | tar_handle->filter = filter_accept_reject_list; | 702 | tar_handle->filter = filter_accept_reject_list; |
714 | } | 703 | } |
715 | 704 | ||
705 | /* Open the tar file */ | ||
706 | { | ||
707 | FILE *tar_stream; | ||
708 | int flags; | ||
709 | |||
710 | #ifdef CONFIG_FEATURE_TAR_CREATE | ||
711 | if (ctx_flag == CTX_CREATE) { | ||
712 | tar_stream = stdout; | ||
713 | flags = O_WRONLY | O_CREAT | O_EXCL; | ||
714 | unlink(tar_filename); | ||
715 | } else | ||
716 | #endif | ||
717 | { | ||
718 | tar_stream = stdin; | ||
719 | flags = O_RDONLY; | ||
720 | } | ||
721 | |||
722 | if ((tar_filename[0] == '-') && (tar_filename[1] == '\0')) { | ||
723 | tar_handle->src_fd = fileno(tar_stream); | ||
724 | tar_handle->seek = seek_by_char; | ||
725 | } else { | ||
726 | tar_handle->src_fd = bb_xopen(tar_filename, flags); | ||
727 | } | ||
728 | } | ||
729 | |||
730 | if ((base_dir) && (chdir(base_dir))) { | ||
731 | bb_perror_msg_and_die("Couldnt chdir to %s", base_dir); | ||
732 | } | ||
733 | |||
716 | #ifdef CONFIG_FEATURE_TAR_CREATE | 734 | #ifdef CONFIG_FEATURE_TAR_CREATE |
717 | /* create an archive */ | 735 | /* create an archive */ |
718 | if (ctx_flag == CTX_CREATE) { | 736 | if (ctx_flag == CTX_CREATE) { |
@@ -734,22 +752,11 @@ int tar_main(int argc, char **argv) | |||
734 | (tar_handle->action_header == header_verbose_list)) { | 752 | (tar_handle->action_header == header_verbose_list)) { |
735 | verboseFlag = TRUE; | 753 | verboseFlag = TRUE; |
736 | } | 754 | } |
737 | writeTarFile(tar_filename, verboseFlag, tar_handle->accept, | 755 | writeTarFile(tar_handle->src_fd, verboseFlag, tar_handle->accept, |
738 | tar_handle->reject, gzipFlag); | 756 | tar_handle->reject, gzipFlag); |
739 | } else | 757 | } else |
740 | #endif /* CONFIG_FEATURE_TAR_CREATE */ | 758 | #endif /* CONFIG_FEATURE_TAR_CREATE */ |
741 | { | 759 | { |
742 | if ((tar_filename[0] == '-') && (tar_filename[1] == '\0')) { | ||
743 | tar_handle->src_fd = fileno(stdin); | ||
744 | tar_handle->seek = seek_by_char; | ||
745 | } else { | ||
746 | tar_handle->src_fd = bb_xopen(tar_filename, O_RDONLY); | ||
747 | } | ||
748 | |||
749 | if ((base_dir) && (chdir(base_dir))) { | ||
750 | bb_perror_msg_and_die("Couldnt chdir"); | ||
751 | } | ||
752 | |||
753 | while (get_header_ptr(tar_handle) == EXIT_SUCCESS); | 760 | while (get_header_ptr(tar_handle) == EXIT_SUCCESS); |
754 | 761 | ||
755 | /* Ckeck that every file that should have been extracted was */ | 762 | /* Ckeck that every file that should have been extracted was */ |