sdcard: only bind mount sdcard after successful preparation of data

Change-Id: I589f48a019f90a60ad48f93c5f5555aae4b8a5c9
diff --git a/partition.cpp b/partition.cpp
index 7e6fd55..1d68c24 100755
--- a/partition.cpp
+++ b/partition.cpp
@@ -658,8 +658,6 @@
 	UnMount(false);
 
 #ifdef TW_INCLUDE_CRYPTO
-	if (datamedia)
-		Setup_Data_Media();
 	Can_Be_Encrypted = true;
 	char crypto_blkdev[255];
 	property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
@@ -667,6 +665,8 @@
 		Set_FBE_Status();
 		Decrypted_Block_Device = crypto_blkdev;
 		LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
+		if (datamedia)
+			Setup_Data_Media();
 		DataManager::SetValue(TW_IS_ENCRYPTED, 0);
 	} else if (!Mount(false)) {
 		if (Is_Present) {
@@ -683,6 +683,8 @@
 					DataManager::SetValue("tw_crypto_pwtype_0", cryptfs_get_password_type());
 					DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
 					DataManager::SetValue("tw_crypto_display", "");
+					if (datamedia)
+						Setup_Data_Media();
 				} else {
 					gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
 				}
@@ -695,6 +697,8 @@
 			Primary_Block_Device.c_str(), Mount_Point.c_str());
 		}
 	} else {
+		if (Is_Mounted())
+			UnMount(true);
 		Set_FBE_Status();
 		int is_device_fbe;
 		DataManager::GetValue(TW_IS_FBE, is_device_fbe);
@@ -705,6 +709,9 @@
 				LOGERR("Unable to decrypt FBE device\n");
 		} else {
 			DataManager::SetValue(TW_IS_ENCRYPTED, 0);
+			if (datamedia)
+				Setup_Data_Media();
+
 		}
 	}
 	if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
@@ -1194,7 +1201,8 @@
 			Make_Dir("/sdcard", false);
 			Symlink_Mount_Point = "/sdcard";
 		}
-		if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
+		Mount(false);
+		if (TWFunc::Path_Exists(Mount_Point + "/media/0")) {
 			Storage_Path = Mount_Point + "/media/0";
 			Symlink_Path = Storage_Path;
 			DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
@@ -1605,7 +1613,7 @@
 	if (Removable)
 		Update_Size(Display_Error);
 
-	if (!Symlink_Mount_Point.empty() && Symlink_Mount_Point != "/sdcard") {
+	if (!Symlink_Mount_Point.empty()) {
 		if (!Bind_Mount(false))
 			return false;
 	}
diff --git a/partitionmanager.cpp b/partitionmanager.cpp
index 8d1b6c4..b24beb3 100755
--- a/partitionmanager.cpp
+++ b/partitionmanager.cpp
@@ -421,8 +421,12 @@
 	TWPartition* Decrypt_Data = Find_Partition_By_Path("/data");
 	if (Decrypt_Data && Decrypt_Data->Is_Encrypted && !Decrypt_Data->Is_Decrypted) {
 		Set_Crypto_State();
-		if (!Decrypt_Data->Key_Directory.empty() && Mount_By_Path(Decrypt_Data->Key_Directory, false)) {
-		Set_Crypto_Type("file");
+		TWPartition* Key_Directory_Partition = Find_Partition_By_Path(Decrypt_Data->Key_Directory);
+		if (Key_Directory_Partition != nullptr)
+			if (!Key_Directory_Partition->Is_Mounted())
+				Mount_By_Path(Decrypt_Data->Key_Directory, false);
+		if (!Decrypt_Data->Key_Directory.empty()) {
+			Set_Crypto_Type("file");
 #ifdef TW_INCLUDE_FBE_METADATA_DECRYPT
 #ifdef USE_FSCRYPT
 			if (fscrypt_mount_metadata_encrypted(Decrypt_Data->Actual_Block_Device, Decrypt_Data->Mount_Point, false)) {
@@ -477,8 +481,7 @@
 			}
 		}
 	}
-	if (Decrypt_Data && (!Decrypt_Data->Is_Encrypted || Decrypt_Data->Is_Decrypted) &&
-	Decrypt_Data->Mount(false)) {
+	if (Decrypt_Data && (!Decrypt_Data->Is_Encrypted || Decrypt_Data->Is_Decrypted)) {
 		Decrypt_Adopted();
 	}
 #endif
@@ -1817,8 +1820,10 @@
 		DataManager::LoadTWRPFolderInfo();
 		Update_System_Details();
 		Output_Partition(dat);
-		if (!dat->Bind_Mount(false))
-			LOGERR("Unable to bind mount /sdcard to %s\n", dat->Storage_Path.c_str());
+		if (!android::base::StartsWith(dat->Actual_Block_Device, "/dev/block/mmcblk")) {
+			if (!dat->Bind_Mount(false))
+				LOGERR("Unable to bind mount /sdcard to %s\n", dat->Storage_Path.c_str());
+		}
 	} else
 		LOGERR("Unable to locate data partition.\n");
 }