Set data/recovery for Settings: complementary code

After https://gerrit.twrp.me/c/android_bootable_recovery/+/5670 some functions were not working properly, such as change TWRP folder and custom themes.

Signed-off-by: Fernando Oliveira <fernandoaju78@gmail.com>
Change-Id: Ifaa777e528a6ca68e628d1719ba6d232851d338d
diff --git a/gui/action.cpp b/gui/action.cpp
index c795a01..9c0ad17 100755
--- a/gui/action.cpp
+++ b/gui/action.cpp
@@ -34,6 +34,7 @@
 #include <dirent.h>
 #include <private/android_filesystem_config.h>
 #include <android-base/properties.h>
+#include <fstream>
 
 #include <string>
 #include <sstream>
@@ -2347,6 +2348,10 @@
 	if (ret) {
 		DataManager::SetValue(TW_RECOVERY_FOLDER_VAR, '/' + arg);
 		DataManager::SetValue(TW_BACKUPS_FOLDER_VAR, newBackupFolder);
+		//Creates an empty file that marks which folder is TWRP with the renamed new name, after reboot.
+		string path= newFolder + "/.twrpcf";
+		std::ofstream twrpcf(path);
+		twrpcf.close();
 	}
 	operation_end((int)!ret);
 	return 0;
diff --git a/gui/gui.cpp b/gui/gui.cpp
index 5c7d27e..bb80632 100755
--- a/gui/gui.cpp
+++ b/gui/gui.cpp
@@ -799,7 +799,7 @@
 	{
 		std::string theme_path;
 
-		theme_path = DataManager::GetSettingsStoragePath();
+		theme_path = DataManager::GetCurrentStoragePath();
 		if (!PartitionManager.Mount_Settings_Storage(false))
 		{
 			int retry_count = 5;
@@ -849,7 +849,7 @@
 		return -1;
 	}
 
-	std::string theme_path = DataManager::GetSettingsStoragePath();
+	std::string theme_path = DataManager::GetCurrentStoragePath();
 	theme_path += TWFunc::Check_For_TwrpFolder() + "/theme/ui.zip";
 	// Check for a custom theme
 	if (TWFunc::Path_Exists(theme_path)) {
diff --git a/gui/pages.cpp b/gui/pages.cpp
index 8b9ce46..23d8e56 100755
--- a/gui/pages.cpp
+++ b/gui/pages.cpp
@@ -1505,7 +1505,7 @@
 		return 0;
 
 	mReloadTheme = false;
-	theme_path = DataManager::GetSettingsStoragePath();
+	theme_path = DataManager::GetCurrentStoragePath();
 	if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
 		LOGERR("Unable to mount %s during gui_reload_theme function.\n", theme_path.c_str());
 		ret_val = 1;
diff --git a/partitionmanager.cpp b/partitionmanager.cpp
index b7e4eec..4ff8711 100755
--- a/partitionmanager.cpp
+++ b/partitionmanager.cpp
@@ -3105,6 +3105,8 @@
 		}
 	}
 
+	//Devices without encryption do not run the Post_Decrypt function so the "data/recovery" folder was not being created on these devices
+	DataManager::SetValue("tw_settings_path", TW_STORAGE_PATH);
 	LOGINFO("Decrypt adopted storage starting\n");
 	char* xmlFile = PageManager::LoadFileToBuffer(path, NULL);
 	xml_document<> *doc = NULL;
diff --git a/twrp-functions.cpp b/twrp-functions.cpp
index 51d252a..38f59ef 100755
--- a/twrp-functions.cpp
+++ b/twrp-functions.cpp
@@ -1449,7 +1449,7 @@
 			type = Get_D_Type_From_Stat(fullPath);
 		}
 
-		if (type == DT_DIR && Path_Exists(fullPath + '/' + TW_SETTINGS_FILE)) {
+		if (type == DT_DIR && Path_Exists(fullPath + "/.twrpcf")) {
 			if ('/' + name == TW_DEFAULT_RECOVERY_FOLDER) {
 				oldFolder = name;
 			} else {
@@ -1462,6 +1462,9 @@
 
 	if (oldFolder == "" && customTWRPFolders.empty()) {
 		LOGINFO("No recovery folder found. Using default folder.\n");
+		//Creates the TWRP folder if it does not exist on the device and if the folder has not been changed to a new name
+		mainPath += TW_DEFAULT_RECOVERY_FOLDER;
+		mkdir(mainPath.c_str(), 0777);
 		goto exit;
 	} else if (customTWRPFolders.empty()) {
 		LOGINFO("No custom recovery folder found. Using TWRP as default.\n");