Add support to change TWRP Folder
Change-Id: Id3b3b46d492bf83d1d9d3f535c880ea9d15b4107
diff --git a/twrp-functions.cpp b/twrp-functions.cpp
index 9bbb700..20f2e95 100755
--- a/twrp-functions.cpp
+++ b/twrp-functions.cpp
@@ -1414,4 +1414,97 @@
}
return true;
}
+
+string TWFunc::Check_For_TwrpFolder() {
+ string oldFolder = "";
+ vector<string> customTWRPFolders;
+ string mainPath = DataManager::GetCurrentStoragePath();
+ DIR* d;
+ struct dirent* de;
+
+ if(DataManager::GetIntValue(TW_IS_ENCRYPTED)) {
+ goto exit;
+ }
+
+
+ d = opendir(mainPath.c_str());
+ if (d == NULL) {
+ goto exit;
+ }
+
+ while ((de = readdir(d)) != NULL) {
+ string name = de->d_name;
+ string fullPath = mainPath + '/' + name;
+ unsigned char type = de->d_type;
+
+ if(name == "." || name == "..") continue;
+
+ if(type == DT_UNKNOWN) {
+ type = Get_D_Type_From_Stat(fullPath);
+ }
+
+ if(type == DT_DIR && Path_Exists(fullPath + '/' + TW_SETTINGS_FILE)) {
+ if('/' + name == TW_DEFAULT_RECOVERY_FOLDER){
+ oldFolder = name;
+ } else {
+ customTWRPFolders.push_back(name);
+ }
+ }
+ }
+
+ closedir(d);
+
+ if(oldFolder == "" && customTWRPFolders.empty()) {
+ LOGINFO("No recovery folder found. Using default folder.\n");
+ goto exit;
+ } else if(customTWRPFolders.empty()) {
+ LOGINFO("No custom recovery folder found. Using TWRP as default.\n");
+ goto exit;
+ } else {
+ if(customTWRPFolders.size() > 1) {
+ LOGINFO("More than one custom recovery folder found. Using first one from the list.\n");
+ } else {
+ LOGINFO("One custom recovery folder found.\n");
+ }
+ string customPath = '/' + customTWRPFolders.at(0);
+
+ if(Path_Exists(mainPath + TW_DEFAULT_RECOVERY_FOLDER)) {
+ string oldBackupFolder = mainPath + TW_DEFAULT_RECOVERY_FOLDER + "/BACKUPS/" + DataManager::GetStrValue("device_id");
+ string newBackupFolder = mainPath + customPath + "/BACKUPS/" + DataManager::GetStrValue("device_id");
+
+ if(Path_Exists(oldBackupFolder)) {
+ vector<string> backups;
+ d = opendir(oldBackupFolder.c_str());
+
+ if (d != NULL) {
+ while ((de = readdir(d)) != NULL) {
+ string name = de->d_name;
+ unsigned char type = de->d_type;
+
+ if(name == "." || name == "..") continue;
+
+ if(type == DT_UNKNOWN) {
+ type = Get_D_Type_From_Stat(mainPath + '/' + name);
+ }
+
+ if(type == DT_DIR) {
+ backups.push_back(name);
+ }
+ }
+ closedir(d);
+ }
+
+ for(auto it = backups.begin(); it != backups.end(); it++) {
+ Exec_Cmd("mv -f \"" + oldBackupFolder + '/' + *it + "\" \"" + newBackupFolder + '/' + *it + (Path_Exists(newBackupFolder + '/' + *it) ? "_new\"" : "\""));
+ }
+ }
+ Exec_Cmd("rm -rf \"" + mainPath + TW_DEFAULT_RECOVERY_FOLDER + '\"');
+ }
+
+ return customPath;
+ }
+
+exit:
+ return TW_DEFAULT_RECOVERY_FOLDER;
+}
#endif // ndef BUILD_TWRPTAR_MAIN