Fix compressed backups with split archives

Change-Id: I8b9197a18cd83c304e5d4ed29ef55ba67e4e42f7
diff --git a/twrpTar.cpp b/twrpTar.cpp
index d192f2c..c18b062 100644
--- a/twrpTar.cpp
+++ b/twrpTar.cpp
@@ -69,6 +69,8 @@
 	tar_type.openfunc = open;
 	tar_type.closefunc = close;
 	tar_type.readfunc = read;
+	input_fd = -1;
+	output_fd = -1;
 }
 
 twrpTar::~twrpTar(void) {
@@ -915,10 +917,12 @@
 			// pigz Child
 			close(pipes[1]);
 			close(pipes[2]);
-			close(0);
-			dup2(pipes[0], 0);
-			close(1);
-			dup2(pipes[3], 1);
+			int stdinfd = fileno(stdin);
+			int stdoutfd = fileno(stdout);
+			close(stdinfd);
+			dup2(pipes[0], stdinfd);
+			close(stdoutfd);
+			dup2(pipes[3], stdoutfd);
 			if (execlp("pigz", "pigz", "-", NULL) < 0) {
 				LOGINFO("execlp pigz ERROR!\n");
 				gui_err("backup_error=Error creating backup.");
@@ -943,10 +947,12 @@
 				close(pipes[0]);
 				close(pipes[1]);
 				close(pipes[3]);
-				close(0);
-				dup2(pipes[2], 0);
-				close(1);
-				dup2(output_fd, 1);
+				int stdinfd = fileno(stdin);
+				int stdoutfd = fileno(stdout);
+				close(stdinfd);
+				dup2(pipes[2], stdinfd);
+				close(stdoutfd);
+				dup2(output_fd, stdoutfd);
 				if (execlp("openaes", "openaes", "enc", "--key", password.c_str(), NULL) < 0) {
 					LOGINFO("execlp openaes ERROR!\n");
 					gui_err("backup_error=Error creating backup.");
@@ -1007,8 +1013,8 @@
 		} else if (pigz_pid == 0) {
 			// Child
 			close(pigzfd[1]);   // close unused output pipe
-			dup2(pigzfd[0], 0); // remap stdin
-			dup2(output_fd, 1); // remap stdout to output file
+			dup2(pigzfd[0], fileno(stdin)); // remap stdin
+			dup2(output_fd, fileno(stdout)); // remap stdout to output file
 			if (execlp("pigz", "pigz", "-", NULL) < 0) {
 				LOGINFO("execlp pigz ERROR!\n");
 				gui_err("backup_error=Error creating backup.");
@@ -1057,8 +1063,8 @@
 		} else if (oaes_pid == 0) {
 			// Child
 			close(oaesfd[1]);   // close unused
-			dup2(oaesfd[0], 0); // remap stdin
-			dup2(output_fd, 1); // remap stdout to output file
+			dup2(oaesfd[0], fileno(stdin)); // remap stdin
+			dup2(output_fd, fileno(stdout)); // remap stdout to output file
 			if (execlp("openaes", "openaes", "enc", "--key", password.c_str(), NULL) < 0) {
 				LOGINFO("execlp openaes ERROR!\n");
 				gui_err("backup_error=Error creating backup.");
@@ -1147,10 +1153,12 @@
 			close(pipes[0]); // Close pipes that are not used by this child
 			close(pipes[2]);
 			close(pipes[3]);
-			close(0);
-			dup2(input_fd, 0);
-			close(1);
-			dup2(pipes[1], 1);
+			int stdinfd = fileno(stdin);
+			int stdoutfd = fileno(stdout);
+			close(stdinfd);
+			dup2(input_fd, stdinfd);
+			close(stdoutfd);
+			dup2(pipes[1], stdoutfd);
 			if (execlp("openaes", "openaes", "dec", "--key", password.c_str(), NULL) < 0) {
 				LOGINFO("execlp openaes ERROR!\n");
 				gui_err("restore_error=Error during restore process.");
@@ -1173,10 +1181,12 @@
 				// pigz Child
 				close(pipes[1]); // Close pipes not used by this child
 				close(pipes[2]);
-				close(0);
-				dup2(pipes[0], 0);
-				close(1);
-				dup2(pipes[3], 1);
+				int stdinfd = fileno(stdin);
+				int stdoutfd = fileno(stdout);
+				close(stdinfd);
+				dup2(pipes[0], stdinfd);
+				close(stdoutfd);
+				dup2(pipes[3], stdoutfd);
 				if (execlp("pigz", "pigz", "-d", "-c", NULL) < 0) {
 					LOGINFO("execlp pigz ERROR!\n");
 					gui_err("restore_error=Error during restore process.");
@@ -1226,9 +1236,10 @@
 		} else if (oaes_pid == 0) {
 			// Child
 			close(oaesfd[0]); // Close unused pipe
-			close(0);   // close stdin
-			dup2(oaesfd[1], 1); // remap stdout
-			dup2(input_fd, 0); // remap input fd to stdin
+			int stdinfd = fileno(stdin);
+			close(stdinfd);   // close stdin
+			dup2(oaesfd[1], fileno(stdout)); // remap stdout
+			dup2(input_fd, stdinfd); // remap input fd to stdin
 			if (execlp("openaes", "openaes", "dec", "--key", password.c_str(), NULL) < 0) {
 				LOGINFO("execlp openaes ERROR!\n");
 				gui_err("restore_error=Error during restore process.");
@@ -1281,8 +1292,8 @@
 		} else if (pigz_pid == 0) {
 			// Child
 			close(pigzfd[0]);
-			dup2(pigzfd[1], 1); // remap stdout
-			dup2(input_fd, 0); // remap input fd to stdin
+			dup2(pigzfd[1], fileno(stdout)); // remap stdout
+			dup2(input_fd, fileno(stdin)); // remap input fd to stdin
 			if (execlp("pigz", "pigz", "-d", "-c", NULL) < 0) {
 				close(pigzfd[1]);
 				close(input_fd);
@@ -1396,8 +1407,10 @@
 		if (!twadbbu::Write_TWEOF())
 			return -1;
 	}
-	close(input_fd);
-	close(output_fd);
+	if (input_fd >= 0)
+		close(input_fd);
+	if (output_fd >= 0)
+		close(output_fd);
 	return 0;
 }