Improve backup and restore

Add in archive splitting support
diff --git a/partition.cpp b/partition.cpp
index 5217227..27f855e 100644
--- a/partition.cpp
+++ b/partition.cpp
@@ -683,7 +683,6 @@
 	int index = 0;
 
 	Full_Filename = restore_folder + "/" + Backup_FileName;
-	LOGI("Full_Filename: '%s'\n", Full_Filename.c_str());
 	if (!TWFunc::Path_Exists(Full_Filename)) {
 		// This is a split archive, we presume
 		sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
@@ -694,8 +693,8 @@
 			}
 			index++;
 			sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
-			LOGI("Full_Filename: '%s'\n", Full_Filename.c_str());
 		}
+		return true;
 	} else {
 		// Single file archive
 		if (TWFunc::Check_MD5(Full_Filename) == 0) {
@@ -708,7 +707,6 @@
 }
 
 bool TWPartition::Restore(string restore_folder) {
-	ui_print("Restoring %s...\n", Display_Name.c_str());
 	if (Backup_Method == FILES)
 		return Restore_Tar(restore_folder);
 	else if (Backup_Method == DD)
@@ -995,9 +993,11 @@
 }
 
 bool TWPartition::Backup_Tar(string backup_folder) {
-	char back_name[255];
-	string Full_FileName, Tar_Args, Command;
-	int use_compression;
+	char back_name[255], split_index[5];
+	string Full_FileName, Split_FileName, Tar_Args, Command;
+	int use_compression, index, backup_count;
+	struct stat st;
+	unsigned long long total_bsize = 0;
 
 	if (!Mount(true))
 		return false;
@@ -1013,12 +1013,33 @@
 	sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
 	Backup_FileName = back_name;
 
-	Full_FileName = backup_folder + "/" + Backup_FileName;
 	if (Backup_Size > MAX_ARCHIVE_SIZE) {
 		// This backup needs to be split into multiple archives
-		LOGE("Multiple archive splitting is not implemented yet!\n");
-		return false;
+		ui_print("Breaking backup file into multiple archives...\nGenerating file lists\n");
+		sprintf(back_name, "%s", Mount_Point.c_str());
+		backup_count = make_file_list(back_name);
+		if (backup_count < 1) {
+			LOGE("Error generating file list!\n");
+			return false;
+		}
+		for (index=0; index<backup_count; index++) {
+			sprintf(split_index, "%03i", index);
+			Full_FileName = backup_folder + "/" + Backup_FileName + split_index;
+			Command = "tar " + Tar_Args + " -f '" + Full_FileName + "' -T /tmp/list/filelist" + split_index;
+			LOGI("Backup command: '%s'\n", Command.c_str());
+			ui_print("Backup archive %i of %i...\n", (index + 1), backup_count);
+			system(Command.c_str()); // sending backup command formed earlier above
+
+			if (stat(Full_FileName.c_str(), &st) != 0 || st.st_size == 0) {
+				LOGE("File size is zero bytes. Aborting...\n\n"); // oh noes! file size is 0, abort! abort!
+				return false;
+			}
+			total_bsize += st.st_size;
+		}
+		ui_print(" * Total size: %llu bytes.\n", total_bsize);
+		system("cd /tmp && rm -rf list");
 	} else {
+		Full_FileName = backup_folder + "/" + Backup_FileName;
 		if (Has_Data_Media)
 			Command = "cd " + Mount_Point + " && tar " + Tar_Args + " ./ --exclude='media*' -f '" + Full_FileName + "'";
 		else
@@ -1068,6 +1089,8 @@
 bool TWPartition::Restore_Tar(string restore_folder) {
 	size_t first_period, second_period;
 	string Restore_File_System, Full_FileName, Command;
+	int index = 0;
+	char split_index[5];
 
 	LOGI("Restore filename is: %s\n", Backup_FileName.c_str());
 
@@ -1093,12 +1116,26 @@
 	if (!Mount(true))
 		return false;
 
-	Full_FileName = restore_folder + "/" + Backup_FileName;
 	ui_print("Restoring %s...\n", Display_Name.c_str());
+	Full_FileName = restore_folder + "/" + Backup_FileName;
 	if (!TWFunc::Path_Exists(Full_FileName)) {
-		// This backup is multiple archives
-		LOGE("Multiple archive not implemented yet.\n");
-		return false;
+		// Backup is multiple archives
+		LOGI("Backup is multiple archives.\n");
+		sprintf(split_index, "%03i", index);
+		Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
+		while (TWFunc::Path_Exists(Full_FileName)) {
+			ui_print("Restoring archive %i...\n", index + 1);
+			Command = "cd " + Mount_Point + " && tar -xf '" + Full_FileName + "'";
+			LOGI("Restore command: '%s'\n", Command.c_str());
+			system(Command.c_str());
+			index++;
+			sprintf(split_index, "%03i", index);
+			Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
+		}
+		if (index == 0) {
+			LOGE("Error locating restore file: '%s'\n", Full_FileName.c_str());
+			return false;
+		}
 	} else {
 		Command = "cd " + Mount_Point + " && tar -xf '" + Full_FileName + "'";
 		LOGI("Restore command: '%s'\n", Command.c_str());
diff --git a/partitionmanager.cpp b/partitionmanager.cpp
index a166783..855b311 100644
--- a/partitionmanager.cpp
+++ b/partitionmanager.cpp
@@ -334,21 +334,19 @@
 	char command[512];
 	string Full_File = Backup_Folder + Backup_Filename;
 
-	if (!generate_md5) {
-		LOGI("MD5 disabled\n");
+	if (!generate_md5)
 		return true;
-	}
 
-	ui_print(" * Generating md5...\n");
+	ui_print(" * Generating md5...");
 
 	if (TWFunc::Path_Exists(Full_File)) {
 		sprintf(command, "cd '%s' && md5sum %s > %s.md5",Backup_Folder.c_str(), Backup_Filename.c_str(), Backup_Filename.c_str());
 		LOGI("MD5 command is: '%s'\n", command);
 		if (system(command) == 0) {
-			ui_print("....MD5 Created.\n");
+			ui_print("MD5 Created.\n");
 			return true;
 		} else {
-			ui_print("....MD5 Error.\n");
+			ui_print("MD5 Error!\n");
 			return false;
 		}
 	} else {
@@ -356,21 +354,21 @@
 		int index = 0;
 
 		sprintf(filename, "%s%03i", Full_File.c_str(), index);
-		while (TWFunc::Path_Exists(filename)) {
+		while (TWFunc::Path_Exists(filename) == true) {
 			sprintf(command, "cd '%s' && md5sum %s%03i > %s%03i.md5",Backup_Folder.c_str(), Backup_Filename.c_str(), index, Backup_Filename.c_str(), index);
 			LOGI("MD5 command is: '%s'\n", command);
-			if (system(command) == 0) {
-				ui_print("....MD5 Created.\n");
-			} else {
-				ui_print("....MD5 Error.\n");
+			if (system(command) != 0) {
+				ui_print("MD5 Error.\n");
 				return false;
 			}
 			index++;
+			sprintf(filename, "%s%03i", Full_File.c_str(), index);
 		}
 		if (index == 0) {
 			LOGE("Backup file: '%s' not found!\n", filename);
 			return false;
 		}
+		ui_print("MD5 Created.\n");
 	}
 	return true;
 }
@@ -427,10 +425,9 @@
 		return false;
 
 	DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
-	if (do_md5 != 0) {
-		LOGI("MD5 creation enabled.\n");
+	if (do_md5 == 0)
 		do_md5 = true;
-	}
+
 	DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
 	DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
 	if (Backup_Name == "(Current Date)" || Backup_Name == "0") {
@@ -663,192 +660,178 @@
     return true;
 }
 
+bool TWPartitionManager::Restore_Partition(TWPartition* Part, string Restore_Name) {
+	time_t Start, Stop;
+	time(&Start);
+	if (!Part->Restore(Restore_Name))
+		return false;
+	time(&Stop);
+	ui_print("[%s done (%d seconds)]\n\n", Part->Display_Name.c_str(), (int)difftime(Stop, Start));
+	return true;
+}
+
 int TWPartitionManager::Run_Restore(string Restore_Name) {
-	int check, restore_sys, restore_data, restore_cache, restore_boot, restore_andsec, restore_sdext, restore_sp1, restore_sp2, restore_sp3;
-	TWPartition* Part;
+	int check_md5, check, partition_count = 0;
+	TWPartition* restore_sys = NULL;
+	TWPartition* restore_data = NULL;
+	TWPartition* restore_cache = NULL;
+	TWPartition* restore_boot = NULL;
+	TWPartition* restore_andsec = NULL;
+	TWPartition* restore_sdext = NULL;
+	TWPartition* restore_sp1 = NULL;
+	TWPartition* restore_sp2 = NULL;
+	TWPartition* restore_sp3 = NULL;
+	time_t rStart, rStop;
+	time(&rStart);
 
-	DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check);
-	DataManager::GetValue(TW_RESTORE_SYSTEM_VAR, restore_sys);
-	DataManager::GetValue(TW_RESTORE_DATA_VAR, restore_data);
-	DataManager::GetValue(TW_RESTORE_CACHE_VAR, restore_cache);
-	DataManager::GetValue(TW_RESTORE_BOOT_VAR, restore_boot);
-	DataManager::GetValue(TW_RESTORE_ANDSEC_VAR, restore_andsec);
-	DataManager::GetValue(TW_RESTORE_SDEXT_VAR, restore_sdext);
-	DataManager::GetValue(TW_RESTORE_SP1_VAR, restore_sp1);
-	DataManager::GetValue(TW_RESTORE_SP2_VAR, restore_sp2);
-	DataManager::GetValue(TW_RESTORE_SP3_VAR, restore_sp3);
+	ui_print("\n[RESTORE STARTED]\n\n");
+	ui_print("Restore folder: '%s'\n", Restore_Name.c_str());
 
+	if (!Mount_Current_Storage(true))
+		return false;
+
+	DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
+	DataManager::GetValue(TW_RESTORE_SYSTEM_VAR, check);
 	if (check > 0) {
+		restore_sys = Find_Partition_By_Path("/system");
+		if (restore_sys == NULL) {
+			LOGE("Unable to locate system partition.\n");
+			return false;
+		}
+		partition_count++;
+	}
+	DataManager::GetValue(TW_RESTORE_DATA_VAR, check);
+	if (check > 0) {
+		restore_data = Find_Partition_By_Path("/data");
+		if (restore_data == NULL) {
+			LOGE("Unable to locate data partition.\n");
+			return false;
+		}
+		partition_count++;
+	}
+	DataManager::GetValue(TW_RESTORE_CACHE_VAR, check);
+	if (check > 0) {
+		restore_cache = Find_Partition_By_Path("/cache");
+		if (restore_cache == NULL) {
+			LOGE("Unable to locate cache partition.\n");
+			return false;
+		}
+		partition_count++;
+	}
+	DataManager::GetValue(TW_RESTORE_BOOT_VAR, check);
+	if (check > 0) {
+		restore_boot = Find_Partition_By_Path("/boot");
+		if (restore_boot == NULL) {
+			LOGE("Unable to locate boot partition.\n");
+			return false;
+		}
+		partition_count++;
+	}
+	DataManager::GetValue(TW_RESTORE_ANDSEC_VAR, check);
+	if (check > 0) {
+		restore_andsec = Find_Partition_By_Path("/and-sec");
+		if (restore_andsec == NULL) {
+			LOGE("Unable to locate android secure partition.\n");
+			return false;
+		}
+		partition_count++;
+	}
+	DataManager::GetValue(TW_RESTORE_SDEXT_VAR, check);
+	if (check > 0) {
+		restore_sdext = Find_Partition_By_Path("/sd-ext");
+		if (restore_sdext == NULL) {
+			LOGE("Unable to locate sd-ext partition.\n");
+			return false;
+		}
+		partition_count++;
+	}
+#ifdef SP1_NAME
+	DataManager::GetValue(TW_RESTORE_SP1_VAR, check);
+	if (check > 0) {
+		restore_sp1 = Find_Partition_By_Path(SP1_NAME);
+		if (restore_sp1 == NULL) {
+			LOGE("Unable to locate %s partition.\n", SP1_NAME);
+			return false;
+		}
+		partition_count++;
+	}
+#endif
+#ifdef SP2_NAME
+	DataManager::GetValue(TW_RESTORE_SP2_VAR, check);
+	if (check > 0) {
+		restore_sp2 = Find_Partition_By_Path(SP2_NAME);
+		if (restore_sp2 == NULL) {
+			LOGE("Unable to locate %s partition.\n", SP2_NAME);
+			return false;
+		}
+		partition_count++;
+	}
+#endif
+#ifdef SP3_NAME
+	DataManager::GetValue(TW_RESTORE_SP3_VAR, check);
+	if (check > 0) {
+		restore_sp3 = Find_Partition_By_Path(SP3_NAME);
+		if (restore_sp3 == NULL) {
+			LOGE("Unable to locate %s partition.\n", SP3_NAME);
+			return false;
+		}
+		partition_count++;
+	}
+#endif
+
+	if (partition_count == 0) {
+		LOGE("No partitions selected for restore.\n");
+		return false;
+	}
+
+	if (check_md5 > 0) {
 		// Check MD5 files first before restoring to ensure that all of them match before starting a restore
-		if (restore_sys > 0) {
-			Part = Find_Partition_By_Path("/system");
-			if (Part) {
-				if (!Part->Check_MD5(Restore_Name))
-					return false;
-			} else
-				LOGE("Restore: Unable to locate system partition.\n");
-		}
+		ui_print("Verifying MD5...\n");
+		if (restore_sys != NULL && !restore_sys->Check_MD5(Restore_Name))
+			return false;
+		if (restore_data != NULL && !restore_data->Check_MD5(Restore_Name))
+			return false;
+		if (restore_cache != NULL && !restore_cache->Check_MD5(Restore_Name))
+			return false;
+		if (restore_boot != NULL && !restore_boot->Check_MD5(Restore_Name))
+			return false;
+		if (restore_andsec != NULL && !restore_andsec->Check_MD5(Restore_Name))
+			return false;
+		if (restore_sdext != NULL && !restore_sdext->Check_MD5(Restore_Name))
+			return false;
+		if (restore_sp1 != NULL && !restore_sp1->Check_MD5(Restore_Name))
+			return false;
+		if (restore_sp2 != NULL && !restore_sp2->Check_MD5(Restore_Name))
+			return false;
+		if (restore_sp3 != NULL && !restore_sp3->Check_MD5(Restore_Name))
+			return false;
+		ui_print("Done verifying MD5.\n");
+	} else
+			ui_print("Skipping MD5 check based on user setting.\n");
 
-		if (restore_data > 0) {
-			Part = Find_Partition_By_Path("/data");
-			if (Part) {
-				if (!Part->Check_MD5(Restore_Name))
-					return false;
-			} else
-				LOGE("Restore: Unable to locate data partition.\n");
-		}
+	ui_print("Restoring %i partitions...\n", partition_count);
+	if (restore_sys != NULL && !Restore_Partition(restore_sys, Restore_Name))
+		return false;
+	if (restore_data != NULL && !Restore_Partition(restore_data, Restore_Name))
+		return false;
+	if (restore_cache != NULL && !Restore_Partition(restore_cache, Restore_Name))
+		return false;
+	if (restore_boot != NULL && !Restore_Partition(restore_boot, Restore_Name))
+		return false;
+	if (restore_andsec != NULL && !Restore_Partition(restore_andsec, Restore_Name))
+		return false;
+	if (restore_sdext != NULL && !Restore_Partition(restore_sdext, Restore_Name))
+		return false;
+	if (restore_sp1 != NULL && !Restore_Partition(restore_sp1, Restore_Name))
+		return false;
+	if (restore_sp2 != NULL && !Restore_Partition(restore_sp2, Restore_Name))
+		return false;
+	if (restore_sp3 != NULL && !Restore_Partition(restore_sp3, Restore_Name))
+		return false;
 
-		if (restore_cache > 0) {
-			Part = Find_Partition_By_Path("/cache");
-			if (Part) {
-				if (!Part->Check_MD5(Restore_Name))
-					return false;
-			} else
-				LOGE("Restore: Unable to locate cache partition.\n");
-		}
-
-		if (restore_boot > 0) {
-			Part = Find_Partition_By_Path("/boot");
-			if (Part) {
-				if (!Part->Check_MD5(Restore_Name))
-					return false;
-			} else
-				LOGE("Restore: Unable to locate boot partition.\n");
-		}
-
-		if (restore_andsec > 0) {
-			Part = Find_Partition_By_Path("/.android_secure");
-			if (Part) {
-				if (!Part->Check_MD5(Restore_Name))
-					return false;
-			} else
-				LOGE("Restore: Unable to locate android_secure partition.\n");
-		}
-
-		if (restore_sdext > 0) {
-			Part = Find_Partition_By_Path("/sd-ext");
-			if (Part) {
-				if (!Part->Check_MD5(Restore_Name))
-					return false;
-			} else
-				LOGE("Restore: Unable to locate sd-ext partition.\n");
-		}
-#ifdef SP1_NAME
-		if (restore_sp1 > 0) {
-			Part = Find_Partition_By_Path(TWFunc::Get_Root_Path(SP1_NAME));
-			if (Part) {
-				if (!Part->Check_MD5(Restore_Name))
-					return false;
-			} else
-				LOGE("Restore: Unable to locate %s partition.\n", SP1_NAME);
-		}
-#endif
-#ifdef SP2_NAME
-		if (restore_sp2 > 0) {
-			Part = Find_Partition_By_Path(TWFunc::Get_Root_Path(SP2_NAME));
-			if (Part) {
-				if (!Part->Check_MD5(Restore_Name))
-					return false;
-			} else
-				LOGE("Restore: Unable to locate %s partition.\n", SP2_NAME);
-		}
-#endif
-#ifdef SP3_NAME
-		if (restore_sp3 > 0) {
-			Part = Find_Partition_By_Path(TWFunc::Get_Root_Path(SP3_NAME));
-			if (Part) {
-				if (!Part->Check_MD5(Restore_Name))
-					return false;
-			} else
-				LOGE("Restore: Unable to locate %s partition.\n", SP3_NAME);
-		}
-#endif
-	}
-
-	if (restore_sys > 0) {
-		Part = Find_Partition_By_Path("/system");
-		if (Part) {
-			if (!Part->Restore(Restore_Name))
-				return false;
-		} else
-			LOGE("Restore: Unable to locate system partition.\n");
-	}
-
-	if (restore_data > 0) {
-		Part = Find_Partition_By_Path("/data");
-		if (Part) {
-			if (!Part->Restore(Restore_Name))
-				return false;
-		} else
-			LOGE("Restore: Unable to locate data partition.\n");
-	}
-
-	if (restore_cache > 0) {
-		Part = Find_Partition_By_Path("/cache");
-		if (Part) {
-			if (!Part->Restore(Restore_Name))
-				return false;
-		} else
-			LOGE("Restore: Unable to locate cache partition.\n");
-	}
-
-	if (restore_boot > 0) {
-		Part = Find_Partition_By_Path("/boot");
-		if (Part) {
-			if (!Part->Restore(Restore_Name))
-				return false;
-		} else
-			LOGE("Restore: Unable to locate boot partition.\n");
-	}
-
-	if (restore_andsec > 0) {
-		Part = Find_Partition_By_Path("/.android_secure");
-		if (Part) {
-			if (!Part->Restore(Restore_Name))
-				return false;
-		} else
-			LOGE("Restore: Unable to locate android_secure partition.\n");
-	}
-
-	if (restore_sdext > 0) {
-		Part = Find_Partition_By_Path("/sd-ext");
-		if (Part) {
-			if (!Part->Restore(Restore_Name))
-				return false;
-		} else
-			LOGE("Restore: Unable to locate sd-ext partition.\n");
-	}
-#ifdef SP1_NAME
-	if (restore_sp1 > 0) {
-		Part = Find_Partition_By_Path(TWFunc::Get_Root_Path(SP1_NAME));
-		if (Part) {
-			if (!Part->Restore(Restore_Name))
-				return false;
-		} else
-			LOGE("Restore: Unable to locate %s partition.\n", SP1_NAME);
-	}
-#endif
-#ifdef SP2_NAME
-	if (restore_sp2 > 0) {
-		Part = Find_Partition_By_Path(TWFunc::Get_Root_Path(SP2_NAME));
-		if (Part) {
-			if (!Part->Restore(Restore_Name))
-				return false;
-		} else
-			LOGE("Restore: Unable to locate %s partition.\n", SP2_NAME);
-	}
-#endif
-#ifdef SP3_NAME
-	if (restore_sp3 > 0) {
-		Part = Find_Partition_By_Path(TWFunc::Get_Root_Path(SP3_NAME));
-		if (Part) {
-			if (!Part->Restore(Restore_Name))
-				return false;
-		} else
-			LOGE("Restore: Unable to locate %s partition.\n", SP3_NAME);
-	}
-#endif
 	Update_System_Details();
+	time(&rStop);
+	ui_print("[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
 	return true;
 }
 
@@ -1247,4 +1230,4 @@
 	__system("./sbin/fix_permissions.sh");
 	ui_print("Done.\n\n");
 	return true;
-}
\ No newline at end of file
+}
diff --git a/partitions.hpp b/partitions.hpp
index 36138d1..ada2f33 100644
--- a/partitions.hpp
+++ b/partitions.hpp
@@ -174,6 +174,7 @@
 private:
 	bool Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename); // Generates an MD5 after a backup is made
 	bool Backup_Partition(TWPartition* Part, string Backup_Folder, bool generate_md5, unsigned long long* img_bytes_remaining, unsigned long long* file_bytes_remaining, unsigned long *img_time, unsigned long *file_time);
+	bool Restore_Partition(TWPartition* Part, string Restore_Name);
 
 private:
 	std::vector<TWPartition*> Partitions;                                     // Vector list of all partitions