Merge "Move DataManager functions out of twrpTar.cpp" into android-4.4
diff --git a/partition.cpp b/partition.cpp
index b597695..7427192 100644
--- a/partition.cpp
+++ b/partition.cpp
@@ -1593,6 +1593,9 @@
 		tar.use_encryption = use_encryption;
 		if (Use_Userdata_Encryption)
 			tar.userdata_encryption = use_encryption;
+		string Password;
+		DataManager::GetValue("tw_backup_password", Password);
+		tar.setpassword(Password);
 	} else {
 		use_encryption = false;
 	}
@@ -1681,37 +1684,18 @@
 		return false;
 
 	Full_FileName = restore_folder + "/" + Backup_FileName;
-	/*if (!TWFunc::Path_Exists(Full_FileName)) {
-		if (!TWFunc::Path_Exists(Full_FileName)) {
-			// Backup is multiple archives
-			LOGINFO("Backup is multiple archives.\n");
-			sprintf(split_index, "%03i", index);
-			Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
-			while (TWFunc::Path_Exists(Full_FileName)) {
-				index++;
-				gui_print("Restoring archive %i...\n", index);
-				LOGINFO("Restoring '%s'...\n", Full_FileName.c_str());
-				twrpTar tar;
-				tar.setdir("/");
-				tar.setfn(Full_FileName);
-				if (tar.extractTarFork() != 0)
-					return false;
-				sprintf(split_index, "%03i", index);
-				Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
-			}
-			if (index == 0) {
-				LOGERR("Error locating restore file: '%s'\n", Full_FileName.c_str());
-				return false;
-			}
-		}
-	} else {*/
-		twrpTar tar;
-		tar.setdir(Backup_Path);
-		tar.setfn(Full_FileName);
-		tar.backup_name = Backup_Name;
-		if (tar.extractTarFork() != 0)
-			return false;
-	//}
+	twrpTar tar;
+	tar.setdir(Backup_Path);
+	tar.setfn(Full_FileName);
+	tar.backup_name = Backup_Name;
+#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
+	string Password;
+	DataManager::GetValue("tw_restore_password", Password);
+	if (!Password.empty())
+		tar.setpassword(Password);
+#endif
+	if (tar.extractTarFork() != 0)
+		return false;
 	return true;
 }
 
diff --git a/twrpTar.cpp b/twrpTar.cpp
index 824356a..ee9d9dc 100644
--- a/twrpTar.cpp
+++ b/twrpTar.cpp
@@ -21,7 +21,6 @@
 	#include "libtar/libtar.h"
 	#include "twrpTar.h"
 	#include "tarWrite.h"
-	#include "libcrecovery/common.h"
 }
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -39,7 +38,6 @@
 #include <sys/mman.h>
 #include "twrpTar.hpp"
 #include "twcommon.h"
-#include "data.hpp"
 #include "variables.h"
 #include "twrp-functions.hpp"
 
@@ -77,6 +75,10 @@
 	Total_Backup_Size = backup_size;
 }
 
+void twrpTar::setpassword(string pass) {
+	password = pass;
+}
+
 int twrpTar::createTarFork() {
 	int status = 0;
 	pid_t pid, rc_pid;
@@ -529,9 +531,7 @@
 		int ret = extractTar();
 		return ret;
 	} else if (Archive_Current_Type == 2) {
-		string Password;
-		DataManager::GetValue("tw_restore_password", Password);
-		int ret = TWFunc::Try_Decrypting_File(tarfn, Password);
+		int ret = TWFunc::Try_Decrypting_File(tarfn, password);
 		if (ret < 1) {
 			LOGERR("Failed to decrypt tar file '%s'\n", tarfn.c_str());
 			return -1;
@@ -675,13 +675,11 @@
 	char* charTarFile = (char*) tarfn.c_str();
 	char* charRootDir = (char*) tardir.c_str();
 	static tartype_t type = { open, close, read, write_tar };
-	string Password;
 
 	if (use_encryption && use_compression) {
 		// Compressed and encrypted
 		Archive_Current_Type = 3;
 		LOGINFO("Using encryption and compression...\n");
-		DataManager::GetValue("tw_backup_password", Password);
 		int i, pipes[4];
 
 		if (pipe(pipes) < 0) {
@@ -738,7 +736,7 @@
 				dup2(pipes[2], 0);
 				close(1);
 				dup2(output_fd, 1);
-				if (execlp("openaes", "openaes", "enc", "--key", Password.c_str(), NULL) < 0) {
+				if (execlp("openaes", "openaes", "enc", "--key", password.c_str(), NULL) < 0) {
 					LOGERR("execlp openaes ERROR!\n");
 					close(pipes[2]);
 					close(output_fd);
@@ -806,7 +804,6 @@
 		// Encrypted
 		Archive_Current_Type = 2;
 		LOGINFO("Using encryption...\n");
-		DataManager::GetValue("tw_backup_password", Password);
 		int oaesfd[2];
 		pipe(oaesfd);
 		oaes_pid = fork();
@@ -826,7 +823,7 @@
 			}
 			dup2(oaesfd[0], 0); // remap stdin
 			dup2(output_fd, 1); // remap stdout to output file
-			if (execlp("openaes", "openaes", "enc", "--key", Password.c_str(), NULL) < 0) {
+			if (execlp("openaes", "openaes", "enc", "--key", password.c_str(), NULL) < 0) {
 				LOGERR("execlp openaes ERROR!\n");
 				close(output_fd);
 				close(oaesfd[0]);
@@ -861,7 +858,6 @@
 
 	if (Archive_Current_Type == 3) {
 		LOGINFO("Opening encrypted and compressed backup...\n");
-		DataManager::GetValue("tw_restore_password", Password);
 		int i, pipes[4];
 
 		if (pipe(pipes) < 0) {
@@ -894,7 +890,7 @@
 			dup2(input_fd, 0);
 			close(1);
 			dup2(pipes[1], 1);
-			if (execlp("openaes", "openaes", "dec", "--key", Password.c_str(), NULL) < 0) {
+			if (execlp("openaes", "openaes", "dec", "--key", password.c_str(), NULL) < 0) {
 				LOGERR("execlp openaes ERROR!\n");
 				close(input_fd);
 				close(pipes[1]);
@@ -938,7 +934,6 @@
 		}
 	} else if (Archive_Current_Type == 2) {
 		LOGINFO("Opening encrypted backup...\n");
-		DataManager::GetValue("tw_restore_password", Password);
 		int oaesfd[2];
 
 		pipe(oaesfd);
@@ -960,7 +955,7 @@
 			close(0);   // close stdin
 			dup2(oaesfd[1], 1); // remap stdout
 			dup2(input_fd, 0); // remap input fd to stdin
-			if (execlp("openaes", "openaes", "dec", "--key", Password.c_str(), NULL) < 0) {
+			if (execlp("openaes", "openaes", "dec", "--key", password.c_str(), NULL) < 0) {
 				LOGERR("execlp openaes ERROR!\n");
 				close(input_fd);
 				close(oaesfd[1]);
diff --git a/twrpTar.hpp b/twrpTar.hpp
index 17f6959..02d4b80 100644
--- a/twrpTar.hpp
+++ b/twrpTar.hpp
@@ -51,6 +51,8 @@
 	void setfn(string fn);
 	void setdir(string dir);
 	void setsize(unsigned long long backup_size);
+	void setpassword(string pass);
+	
 	unsigned long long uncompressedSize();
 
 public:
@@ -89,6 +91,7 @@
 	string tardir;
 	string tarfn;
 	string basefn;
+	string password;
 
 	vector <string> tarexclude;