Fix image flashing

PS2: full_filename is not a dir

PS3: use a consistent format of always assuming directory
     is missing the trailing / + fix whitespace alignment

Change-Id: Ib963473ae10571b3d069b326d024ca04c7224dda
(cherry picked from commit fa4ff144374474c541351d153549ad11d4396614)
diff --git a/gui/action.cpp b/gui/action.cpp
index a917d2b..a96e15a 100644
--- a/gui/action.cpp
+++ b/gui/action.cpp
@@ -1737,18 +1737,11 @@
 {
 	int op_status = 0;
 
-	PartitionSettings part_settings;
 	operation_start("Flash Image");
 	string path, filename;
 	DataManager::GetValue("tw_zip_location", path);
 	DataManager::GetValue("tw_file", filename);
-	part_settings.Backup_Folder = path + "/" + filename;
-	unsigned long long total_bytes = TWFunc::Get_File_Size(part_settings.Backup_Folder);
-	ProgressTracking progress(total_bytes);
-	part_settings.progress = &progress;
-	part_settings.adbbackup = false;
-	part_settings.PM_Method = PM_RESTORE;
-	if (PartitionManager.Flash_Image(&part_settings))
+	if (PartitionManager.Flash_Image(path, filename))
 		op_status = 0; // success
 	else
 		op_status = 1; // fail
diff --git a/partition.cpp b/partition.cpp
index d34b465..f22a283 100644
--- a/partition.cpp
+++ b/partition.cpp
@@ -1662,7 +1662,7 @@
 
 bool TWPartition::Restore(PartitionSettings *part_settings) {
 	TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
-	LOGINFO("Restore filename is: %s%s\n", part_settings->Backup_Folder.c_str(), Backup_FileName.c_str());
+	LOGINFO("Restore filename is: %s/%s\n", part_settings->Backup_Folder.c_str(), Backup_FileName.c_str());
 
 	string Restore_File_System = Get_Restore_File_System(part_settings);
 
@@ -2167,7 +2167,7 @@
 #endif
 
 	Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
-	Full_FileName = part_settings->Backup_Folder + Backup_FileName;
+	Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
 	tar.has_data_media = Has_Data_Media;
 	tar.part_settings = part_settings;
 	tar.setdir(Backup_Path);
@@ -2227,7 +2227,7 @@
 		if (part_settings->adbbackup)
 			destfn = TW_ADB_BACKUP;
 		else
-			destfn = part_settings->Backup_Folder + Backup_FileName;
+			destfn = part_settings->Backup_Folder + "/" + Backup_FileName;
 	}
 	else {
 		destfn = Actual_Block_Device;
@@ -2448,7 +2448,7 @@
 	if (part_settings->adbbackup)
 		Full_FileName = TW_ADB_RESTORE;
 	else
-		Full_FileName = part_settings->Backup_Folder + Backup_FileName;
+		Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
 
 	if (Restore_File_System == "emmc") {
 		if (!part_settings->adbbackup)
@@ -2607,10 +2607,7 @@
 bool TWPartition::Flash_Image(PartitionSettings *part_settings) {
 	string Restore_File_System, full_filename;
 
-	if (part_settings->Part != NULL)
-		full_filename = part_settings->Backup_Folder + "/" + Backup_FileName;
-	else
-		full_filename = part_settings->Backup_Folder; // Flash image action from GUI
+	full_filename = part_settings->Backup_Folder + "/" + Backup_FileName;
 
 	LOGINFO("Image filename is: %s\n", Backup_FileName.c_str());
 
diff --git a/partitionmanager.cpp b/partitionmanager.cpp
index 738f6ce..b04cc9a 100644
--- a/partitionmanager.cpp
+++ b/partitionmanager.cpp
@@ -486,7 +486,7 @@
 
 	if (part_settings->Part == NULL)
 		return false;
-	string Full_File = part_settings->Backup_Folder + part_settings->Part->Backup_FileName;
+	string Full_File = part_settings->Backup_Folder + "/" + part_settings->Part->Backup_FileName;
 	twrpDigest md5sum;
 
 	if (!part_settings->generate_md5)
@@ -622,7 +622,7 @@
 		if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
 			continue;
 
-		string path = Backup_Folder + p->d_name;
+		string path = Backup_Folder + "/" + p->d_name;
 
 		size_t dot = path.find_last_of(".") + 1;
 		if (path.substr(dot) == "win" || path.substr(dot) == "md5" || path.substr(dot) == "info") {
@@ -647,7 +647,7 @@
 	if (tar_fork_pid != 0) {
 		DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
 		DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
-		Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
+		Full_Backup_Path = Backup_Folder + "/" + Backup_Name;
 		LOGINFO("Killing pid: %d\n", tar_fork_pid);
 		kill(tar_fork_pid, SIGUSR2);
 		while (kill(tar_fork_pid, 0) == 0) {
@@ -714,9 +714,9 @@
 	}
 
 	LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
-	part_settings.Backup_Folder = part_settings.Backup_Folder + "/" + Backup_Name + "/";
+	part_settings.Backup_Folder = part_settings.Backup_Folder + "/" + Backup_Name;
 
-	LOGINFO("Full_Backup_Path is: '%s'\n", part_settings.Backup_Folder.c_str());
+	LOGINFO("Backup_Folder is: '%s'\n", part_settings.Backup_Folder.c_str());
 
 	LOGINFO("Calculating backup details...\n");
 	DataManager::GetValue("tw_backup_list", Backup_List);
@@ -864,7 +864,7 @@
 	Update_System_Details();
 	UnMount_Main_Partitions();
 	gui_msg(Msg(msg::kHighlight, "backup_completed=[BACKUP COMPLETED IN {1} SECONDS]")(total_time)); // the end
-	string backup_log = part_settings.Backup_Folder + "recovery.log";
+	string backup_log = part_settings.Backup_Folder + "/recovery.log";
 	TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
 	tw_set_default_metadata(backup_log.c_str());
 
@@ -2248,13 +2248,13 @@
 	return false;
 }
 
-bool TWPartitionManager::Flash_Image(PartitionSettings *part_settings) {
+bool TWPartitionManager::Flash_Image(string& path, string& filename) {
 	int check, partition_count = 0;
 	TWPartition* flash_part = NULL;
 	string Flash_List, flash_path, full_filename;
 	size_t start_pos = 0, end_pos = 0;
 
-	full_filename = part_settings->Backup_Folder;
+	full_filename = path + "/" + filename;
 
 	gui_msg("image_flash_start=[IMAGE FLASH STARTED]");
 	gui_msg(Msg("img_to_flash=Image to flash: '{1}'")(full_filename));
@@ -2269,6 +2269,14 @@
 		}
 	}
 
+	PartitionSettings part_settings;
+	part_settings.Backup_Folder = path;
+	unsigned long long total_bytes = TWFunc::Get_File_Size(full_filename);
+	ProgressTracking progress(total_bytes);
+	part_settings.progress = &progress;
+	part_settings.adbbackup = false;
+	part_settings.PM_Method = PM_RESTORE;
+
 	gui_msg("calc_restore=Calculating restore details...");
 	DataManager::GetValue("tw_flash_partition", Flash_List);
 	if (!Flash_List.empty()) {
@@ -2298,7 +2306,8 @@
 
 	DataManager::SetProgress(0.0);
 	if (flash_part) {
-		if (!flash_part->Flash_Image(part_settings))
+		flash_part->Backup_FileName = filename;
+		if (!flash_part->Flash_Image(&part_settings))
 			return false;
 	} else {
 		gui_err("invalid_flash=Invalid flash partition specified.");
diff --git a/partitions.hpp b/partitions.hpp
index aba1465..e301d9c 100644
--- a/partitions.hpp
+++ b/partitions.hpp
@@ -35,29 +35,29 @@
 	unsigned int selected;
 };
 
-enum PartitionManager_Op {                                                         // PartitionManager Restore Mode for Raw_Read_Write()
+enum PartitionManager_Op {                                                        // PartitionManager Restore Mode for Raw_Read_Write()
 	PM_BACKUP = 0,
 	PM_RESTORE = 1,
 };
 
 class TWPartition;
 
-struct PartitionSettings {                                                         // Settings for backup session
-	TWPartition* Part;                                                         // Partition to pass to the partition backup loop
-	std::string Backup_Folder;                                                 // Path to restore folder
-	bool adbbackup;                                                            // tell the system we are backing up over adb
-	bool adb_compression;                                                      // 0 == uncompressed, 1 == compressed
-	bool generate_md5;                                                         // tell system to create md5 for partitions
-	uint64_t total_restore_size;                                               // Total size of restored backup
-	uint64_t img_bytes_remaining;                                              // remaining img/emmc bytes to backup for progress indicator
-	uint64_t file_bytes_remaining;                                             // remaining file bytes to backup for progress indicator
-	uint64_t img_time;                                                         // used to calculate how fast we backup images
-	uint64_t file_time;                                                        // used to calculate how fast we backup files
-	uint64_t img_bytes;                                                        // total image bytes of all emmc partitions
-	uint64_t file_bytes;                                                       // total file bytes of all file based partitions
-	int partition_count;                                                       // Number of partitions to restore
-	ProgressTracking *progress;                                                // Keep track of progress in GUI	
-	enum PartitionManager_Op PM_Method;                                        // Current operation of backup or restore
+struct PartitionSettings {                                                        // Settings for backup session
+	TWPartition* Part;                                                        // Partition to pass to the partition backup loop
+	std::string Backup_Folder;                                                // Path to restore folder
+	bool adbbackup;                                                           // tell the system we are backing up over adb
+	bool adb_compression;                                                     // 0 == uncompressed, 1 == compressed
+	bool generate_md5;                                                        // tell system to create md5 for partitions
+	uint64_t total_restore_size;                                              // Total size of restored backup
+	uint64_t img_bytes_remaining;                                             // remaining img/emmc bytes to backup for progress indicator
+	uint64_t file_bytes_remaining;                                            // remaining file bytes to backup for progress indicator
+	uint64_t img_time;                                                        // used to calculate how fast we backup images
+	uint64_t file_time;                                                       // used to calculate how fast we backup files
+	uint64_t img_bytes;                                                       // total image bytes of all emmc partitions
+	uint64_t file_bytes;                                                      // total file bytes of all file based partitions
+	int partition_count;                                                      // Number of partitions to restore
+	ProgressTracking *progress;                                               // Keep track of progress in GUI
+	enum PartitionManager_Op PM_Method;                                       // Current operation of backup or restore
 };
 
 enum Backup_Method_enum {
@@ -92,7 +92,7 @@
 	bool Backup(PartitionSettings *part_settings, pid_t *tar_fork_pid);       // Backs up the partition to the folder specified
 	bool Check_MD5(PartitionSettings *part_settings);                         // Checks MD5 of a backup
 	bool Restore(PartitionSettings *part_settings);                           // Restores the partition using the backup folder provided
-	unsigned long long Get_Restore_Size(PartitionSettings *part_settings);// Returns the overall restore size of the backup
+	unsigned long long Get_Restore_Size(PartitionSettings *part_settings);    // Returns the overall restore size of the backup
 	string Backup_Method_By_Name();                                           // Returns a string of the backup method for human readable output
 	bool Decrypt(string Password);                                            // Decrypts the partition, return 0 for failure and -1 for success
 	bool Wipe_Encryption();                                                   // Ignores wipe commands for /data/media devices and formats the original block device
@@ -283,15 +283,15 @@
 	void Decrypt_Adopted();                                                   // Attempt to identy and decrypt any adopted storage partitions
 	void Remove_Partition_By_Path(string Path);                               // Removes / erases a partition entry from the partition list
 
-	bool Flash_Image(PartitionSettings *part_settings);                        // Flashes an image to a selected partition from the partition list
-	bool Restore_Partition(struct PartitionSettings *part_settings);                  // Restore the partitions based on type
+	bool Flash_Image(string& path, string& filename);                         // Flashes an image to a selected partition from the partition list
+	bool Restore_Partition(struct PartitionSettings *part_settings);          // Restore the partitions based on type
 	TWAtomicInt stop_backup;
 
 private:
 	void Setup_Settings_Storage_Partition(TWPartition* Part);                 // Sets up settings storage
 	void Setup_Android_Secure_Location(TWPartition* Part);                    // Sets up .android_secure if needed
-	bool Make_MD5(struct PartitionSettings *part_settings); // Generates an MD5 after a backup is made
-	bool Backup_Partition(struct PartitionSettings *part_settings);                  // Backup the partitions based on type
+	bool Make_MD5(struct PartitionSettings *part_settings);                   // Generates an MD5 after a backup is made
+	bool Backup_Partition(struct PartitionSettings *part_settings);           // Backup the partitions based on type
 	void Output_Partition(TWPartition* Part);                                 // Outputs partition details to the log
 	TWPartition* Find_Partition_By_MTP_Storage_ID(unsigned int Storage_ID);   // Returns a pointer to a partition based on MTP Storage ID
 	bool Add_Remove_MTP_Storage(TWPartition* Part, int message_type);         // Adds or removes an MTP Storage partition