aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2018-08-04 17:48:59 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2018-08-04 17:48:59 +0200
commit45d68c3749d29339494eb38bb4d5fe3ea189ac77 (patch)
treecee3c9de6b51e8082e86df4a88e0b1a14726637d
parent3f2e963768cd9ceb52988db2848ecf0b930f3f44 (diff)
downloadbusybox-w32-45d68c3749d29339494eb38bb4d5fe3ea189ac77.tar.gz
busybox-w32-45d68c3749d29339494eb38bb4d5fe3ea189ac77.tar.bz2
busybox-w32-45d68c3749d29339494eb38bb4d5fe3ea189ac77.zip
tar: make source code a bit more terse, no logic changes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--archival/tar.c57
1 files changed, 25 insertions, 32 deletions
diff --git a/archival/tar.c b/archival/tar.c
index ae1ccc756..9239d8ee4 100644
--- a/archival/tar.c
+++ b/archival/tar.c
@@ -596,34 +596,30 @@ static int FAST_FUNC writeFileToTarball(const char *fileName, struct stat *statb
596/* Don't inline: vfork scares gcc and pessimizes code */ 596/* Don't inline: vfork scares gcc and pessimizes code */
597static void NOINLINE vfork_compressor(int tar_fd, const char *gzip) 597static void NOINLINE vfork_compressor(int tar_fd, const char *gzip)
598{ 598{
599 pid_t gzipPid;
600
601 // On Linux, vfork never unpauses parent early, although standard 599 // On Linux, vfork never unpauses parent early, although standard
602 // allows for that. Do we want to waste bytes checking for it? 600 // allows for that. Do we want to waste bytes checking for it?
603# define WAIT_FOR_CHILD 0 601# define WAIT_FOR_CHILD 0
604 volatile int vfork_exec_errno = 0; 602 volatile int vfork_exec_errno = 0;
605 struct fd_pair gzipDataPipe; 603 struct fd_pair data;
606# if WAIT_FOR_CHILD 604# if WAIT_FOR_CHILD
607 struct fd_pair gzipStatusPipe; 605 struct fd_pair status;
608 xpiped_pair(gzipStatusPipe); 606 xpiped_pair(status);
609# endif 607# endif
610 xpiped_pair(gzipDataPipe); 608 xpiped_pair(data);
611 609
612 signal(SIGPIPE, SIG_IGN); /* we only want EPIPE on errors */ 610 signal(SIGPIPE, SIG_IGN); /* we only want EPIPE on errors */
613 611
614 gzipPid = xvfork(); 612 if (xvfork() == 0) {
615
616 if (gzipPid == 0) {
617 /* child */ 613 /* child */
618 /* NB: close _first_, then move fds! */ 614 /* NB: close _first_, then move fds! */
619 close(gzipDataPipe.wr); 615 close(data.wr);
620# if WAIT_FOR_CHILD 616# if WAIT_FOR_CHILD
621 close(gzipStatusPipe.rd); 617 close(status.rd);
622 /* gzipStatusPipe.wr will close only on exec - 618 /* status.wr will close only on exec -
623 * parent waits for this close to happen */ 619 * parent waits for this close to happen */
624 fcntl(gzipStatusPipe.wr, F_SETFD, FD_CLOEXEC); 620 fcntl(status.wr, F_SETFD, FD_CLOEXEC);
625# endif 621# endif
626 xmove_fd(gzipDataPipe.rd, 0); 622 xmove_fd(data.rd, 0);
627 xmove_fd(tar_fd, 1); 623 xmove_fd(tar_fd, 1);
628 /* exec gzip/bzip2 program/applet */ 624 /* exec gzip/bzip2 program/applet */
629 BB_EXECLP(gzip, gzip, "-f", (char *)0); 625 BB_EXECLP(gzip, gzip, "-f", (char *)0);
@@ -632,20 +628,18 @@ static void NOINLINE vfork_compressor(int tar_fd, const char *gzip)
632 } 628 }
633 629
634 /* parent */ 630 /* parent */
635 xmove_fd(gzipDataPipe.wr, tar_fd); 631 xmove_fd(data.wr, tar_fd);
636 close(gzipDataPipe.rd); 632 close(data.rd);
637# if WAIT_FOR_CHILD 633# if WAIT_FOR_CHILD
638 close(gzipStatusPipe.wr); 634 close(status.wr);
639 while (1) { 635 while (1) {
640 char buf;
641 int n;
642
643 /* Wait until child execs (or fails to) */ 636 /* Wait until child execs (or fails to) */
644 n = full_read(gzipStatusPipe.rd, &buf, 1); 637 char buf;
638 int n = full_read(status.rd, &buf, 1);
645 if (n < 0 /* && errno == EAGAIN */) 639 if (n < 0 /* && errno == EAGAIN */)
646 continue; /* try it again */ 640 continue; /* try it again */
647 } 641 }
648 close(gzipStatusPipe.rd); 642 close(status.rd);
649# endif 643# endif
650 if (vfork_exec_errno) { 644 if (vfork_exec_errno) {
651 errno = vfork_exec_errno; 645 errno = vfork_exec_errno;
@@ -1155,17 +1149,16 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
1155 if (LONE_DASH(tar_filename)) { 1149 if (LONE_DASH(tar_filename)) {
1156 tar_handle->src_fd = tar_fd; 1150 tar_handle->src_fd = tar_fd;
1157 tar_handle->seek = seek_by_read; 1151 tar_handle->seek = seek_by_read;
1152 } else
1153 if (ENABLE_FEATURE_TAR_AUTODETECT
1154 && flags == O_RDONLY
1155 && !(opt & OPT_ANY_COMPRESS)
1156 ) {
1157 tar_handle->src_fd = open_zipped(tar_filename, /*fail_if_not_compressed:*/ 0);
1158 if (tar_handle->src_fd < 0)
1159 bb_perror_msg_and_die("can't open '%s'", tar_filename);
1158 } else { 1160 } else {
1159 if (ENABLE_FEATURE_TAR_AUTODETECT 1161 tar_handle->src_fd = xopen(tar_filename, flags);
1160 && flags == O_RDONLY
1161 && !(opt & OPT_ANY_COMPRESS)
1162 ) {
1163 tar_handle->src_fd = open_zipped(tar_filename, /*fail_if_not_compressed:*/ 0);
1164 if (tar_handle->src_fd < 0)
1165 bb_perror_msg_and_die("can't open '%s'", tar_filename);
1166 } else {
1167 tar_handle->src_fd = xopen(tar_filename, flags);
1168 }
1169 } 1162 }
1170 } 1163 }
1171 1164