Merge "twrp: use ANDROID_ROOT environment variable" into android-9.0
diff --git a/gui/action.cpp b/gui/action.cpp
index fb05d77..d708dd4 100644
--- a/gui/action.cpp
+++ b/gui/action.cpp
@@ -1819,14 +1819,14 @@
 int GUIAction::mountsystemtoggle(std::string arg)
 {
 	int op_status = 0;
-	bool remount_system = PartitionManager.Is_Mounted_By_Path("/system");
+	bool remount_system = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path());
 	bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
 
 	operation_start("Toggle System Mount");
-	if (!PartitionManager.UnMount_By_Path("/system", true)) {
+	if (!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
 		op_status = 1; // fail
 	} else {
-		TWPartition* Part = PartitionManager.Find_Partition_By_Path("/system");
+		TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
 		if (Part) {
 			if (arg == "0") {
 				DataManager::SetValue("tw_mount_system_ro", 0);
@@ -1910,9 +1910,9 @@
 			DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed or do not install
 			goto exit;
 		}
-		if (PartitionManager.Mount_By_Path("/system", false)) {
-			string base_path = "/system";
-			if (TWFunc::Path_Exists("/system/system"))
+		if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), false)) {
+			string base_path = PartitionManager.Get_Android_Root_Path();
+			if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
 				base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
 			string install_path = base_path + "/priv-app";
 			if (!TWFunc::Path_Exists(install_path))
@@ -2007,9 +2007,9 @@
 				sync();
 			}
 		} else {
-			if (PartitionManager.Mount_By_Path("/system", true)) {
-				string base_path = "/system";
-				if (TWFunc::Path_Exists("/system/system"))
+			if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
+				string base_path = PartitionManager.Get_Android_Root_Path();
+				if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
 					base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
 				string install_path = base_path + "/priv-app";
 				string context = "u:object_r:system_file:s0";
@@ -2034,7 +2034,7 @@
 						}
 						sync();
 						sync();
-						PartitionManager.UnMount_By_Path("/system", true);
+						PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true);
 						op_status = 0;
 					} else {
 						LOGERR("Error making app directory '%s': %s\n", strerror(errno));
diff --git a/openrecoveryscript.cpp b/openrecoveryscript.cpp
index d7e4a23..86c90a6 100644
--- a/openrecoveryscript.cpp
+++ b/openrecoveryscript.cpp
@@ -147,8 +147,8 @@
 				// Wipe
 				if (strcmp(value, "cache") == 0 || strcmp(value, "/cache") == 0) {
 					PartitionManager.Wipe_By_Path("/cache");
-				} else if (strcmp(value, "system") == 0 || strcmp(value, "/system") == 0) {
-					PartitionManager.Wipe_By_Path("/system");
+				} else if (strcmp(value, PartitionManager.Get_Android_Root_Path().c_str()) == 0 || strcmp(value, PartitionManager.Get_Android_Root_Path().c_str()) == 0) {
+					PartitionManager.Wipe_By_Path(PartitionManager.Get_Android_Root_Path());
 				} else if (strcmp(value, "dalvik") == 0 || strcmp(value, "dalvick") == 0 || strcmp(value, "dalvikcache") == 0 || strcmp(value, "dalvickcache") == 0) {
 					PartitionManager.Wipe_Dalvik_Cache();
 				} else if (strcmp(value, "data") == 0 || strcmp(value, "/data") == 0 || strcmp(value, "factory") == 0 || strcmp(value, "factoryreset") == 0) {
@@ -667,14 +667,14 @@
 
 int OpenRecoveryScript::remountrw(void)
 {
-	bool remount_system = PartitionManager.Is_Mounted_By_Path("/system");
+	bool remount_system = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path());
 	int op_status;
 	TWPartition* Part;
 
-	if (!PartitionManager.UnMount_By_Path("/system", true)) {
+	if (!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
 		op_status = 1; // fail
 	} else {
-		Part = PartitionManager.Find_Partition_By_Path("/system");
+		Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
 		if (Part) {
 			DataManager::SetValue("tw_mount_system_ro", 0);
 			Part->Change_Mount_Read_Only(false);
diff --git a/partition.cpp b/partition.cpp
index e7b3d2d..1bdb91a 100644
--- a/partition.cpp
+++ b/partition.cpp
@@ -430,7 +430,7 @@
 	} else if (Is_File_System(Fstab_File_System)) {
 		Find_Actual_Block_Device();
 		Setup_File_System(Display_Error);
-		if (Mount_Point == "/system") {
+		if (Mount_Point == PartitionManager.Get_Android_Root_Path()) {
 			Display_Name = "System";
 			Backup_Display_Name = Display_Name;
 			Storage_Name = Display_Name;
@@ -1504,7 +1504,7 @@
 		int never_unmount_system;
 
 		DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
-		if (never_unmount_system == 1 && Mount_Point == "/system")
+		if (never_unmount_system == 1 && Mount_Point == PartitionManager.Get_Android_Root_Path())
 			return true; // Never unmount system if you're not supposed to unmount it
 
 		if (Is_Storage && MTP_Storage_ID > 0)
@@ -2624,7 +2624,7 @@
 		ret = true;
 #ifdef HAVE_CAPABILITIES
 	// Restore capabilities to the run-as binary
-	if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
+	if (Mount_Point == PartitionManager.Get_Android_Root_Path() && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
 		struct vfs_cap_data cap_data;
 		uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
 
diff --git a/partitionmanager.cpp b/partitionmanager.cpp
index 5c8a437..da407fa 100644
--- a/partitionmanager.cpp
+++ b/partitionmanager.cpp
@@ -1104,7 +1104,7 @@
 		}
 	}
 	TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, gui_parse_text("{@updating_system_details}"));
-	UnMount_By_Path("/system", false);
+	UnMount_By_Path(Get_Android_Root_Path(), false);
 	Update_System_Details();
 	UnMount_Main_Partitions();
 	time(&rStop);
@@ -1490,7 +1490,7 @@
 	for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
 		(*iter)->Update_Size(true);
 		if ((*iter)->Can_Be_Mounted) {
-			if ((*iter)->Mount_Point == "/system") {
+			if ((*iter)->Mount_Point == Get_Android_Root_Path()) {
 				int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
 				DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
 			} else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
@@ -1859,7 +1859,7 @@
 
 	TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
 
-	UnMount_By_Path("/system", true);
+	UnMount_By_Path(Get_Android_Root_Path(), true);
 	if (!datamedia)
 		UnMount_By_Path("/data", true);
 
@@ -2726,6 +2726,13 @@
 	return Active_Slot_Display;
 }
 
+string TWPartitionManager::Get_Android_Root_Path() {
+	std::string Android_Root = getenv("ANDROID_ROOT");
+	if (Android_Root == "")
+		Android_Root = "/system";
+	return Android_Root;
+}
+
 void TWPartitionManager::Remove_Uevent_Devices(const string& Mount_Point) {
 	std::vector<TWPartition*>::iterator iter;
 
diff --git a/partitions.hpp b/partitions.hpp
index d780fe5..094c718 100644
--- a/partitions.hpp
+++ b/partitions.hpp
@@ -332,6 +332,7 @@
 	void Set_Active_Slot(const string& Slot);                                 // Sets the active slot to A or B
 	string Get_Active_Slot_Suffix();                                          // Returns active slot _a or _b
 	string Get_Active_Slot_Display();                                         // Returns active slot A or B for display purposes
+	string Get_Android_Root_Path();                                           // Returns path of ANDROID_ROOT environment variable
 	struct pollfd uevent_pfd;                                                 // Used for uevent code
 	void Remove_Uevent_Devices(const string& sysfs_path);                     // Removes subpartitions from the Partitions vector for a matched uevent device
 	void Handle_Uevent(const Uevent_Block_Data& uevent_data);                 // Handle uevent data
diff --git a/twrp-functions.cpp b/twrp-functions.cpp
index 40205c9..2635934 100644
--- a/twrp-functions.cpp
+++ b/twrp-functions.cpp
@@ -407,7 +407,7 @@
 void TWFunc::install_htc_dumlock(void) {
 	int need_libs = 0;
 
-	if (!PartitionManager.Mount_By_Path("/system", true))
+	if (!PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true))
 		return;
 
 	if (!PartitionManager.Mount_By_Path("/data", true))
@@ -809,19 +809,19 @@
 }
 
 string TWFunc::System_Property_Get(string Prop_Name) {
-	bool mount_state = PartitionManager.Is_Mounted_By_Path("/system");
+	bool mount_state = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path());
 	std::vector<string> buildprop;
 	string propvalue;
-	if (!PartitionManager.Mount_By_Path("/system", true))
+	if (!PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true))
 		return propvalue;
 	string prop_file = "/system/build.prop";
 	if (!TWFunc::Path_Exists(prop_file))
-		prop_file = "/system/system/build.prop"; // for devices with system as a root file system (e.g. Pixel)
+		prop_file = PartitionManager.Get_Android_Root_Path() + "/system/build.prop"; // for devices with system as a root file system (e.g. Pixel)
 	if (TWFunc::read_file(prop_file, buildprop) != 0) {
-		LOGINFO("Unable to open /system/build.prop for getting '%s'.\n", Prop_Name.c_str());
+		LOGINFO("Unable to open build.prop for getting '%s'.\n", Prop_Name.c_str());
 		DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date());
 		if (!mount_state)
-			PartitionManager.UnMount_By_Path("/system", false);
+			PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), false);
 		return propvalue;
 	}
 	int line_count = buildprop.size();
@@ -834,12 +834,12 @@
 		if (propname == Prop_Name) {
 			propvalue = buildprop.at(index).substr(end_pos + 1, buildprop.at(index).size());
 			if (!mount_state)
-				PartitionManager.UnMount_By_Path("/system", false);
+				PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), false);
 			return propvalue;
 		}
 	}
 	if (!mount_state)
-		PartitionManager.UnMount_By_Path("/system", false);
+		PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), false);
 	return propvalue;
 }
 
@@ -1095,14 +1095,14 @@
 }
 
 void TWFunc::Disable_Stock_Recovery_Replace(void) {
-	if (PartitionManager.Mount_By_Path("/system", false)) {
+	if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), false)) {
 		// Disable flashing of stock recovery
 		if (TWFunc::Path_Exists("/system/recovery-from-boot.p")) {
 			rename("/system/recovery-from-boot.p", "/system/recovery-from-boot.bak");
 			gui_msg("rename_stock=Renamed stock recovery file in /system to prevent the stock ROM from replacing TWRP.");
 			sync();
 		}
-		PartitionManager.UnMount_By_Path("/system", false);
+		PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), false);
 	}
 }
 
diff --git a/twrp.cpp b/twrp.cpp
index 3102efd..2399c0b 100644
--- a/twrp.cpp
+++ b/twrp.cpp
@@ -333,7 +333,7 @@
 
 #ifndef TW_OEM_BUILD
 	// Check if system has never been changed
-	TWPartition* sys = PartitionManager.Find_Partition_By_Path("/system");
+	TWPartition* sys = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
 	TWPartition* ven = PartitionManager.Find_Partition_By_Path("/vendor");
 
 	if (sys) {
diff --git a/twrpAdbBuFifo.cpp b/twrpAdbBuFifo.cpp
index f98eb05..22bfa40 100644
--- a/twrpAdbBuFifo.cpp
+++ b/twrpAdbBuFifo.cpp
@@ -309,7 +309,7 @@
 					part_settings.Part->Set_Backup_FileName(Backup_FileName);
 					PartitionManager.Set_Restore_Files(path);
 
-					if (path.compare("/system") == 0) {
+					if (path.compare(PartitionManager.Get_Android_Root_Path()) == 0) {
 						if (part_settings.Part->Is_Read_Only()) {
 							if (!twadbbu::Write_TWERROR())
 								LOGERR("Unable to write to TWRP ADB Backup.\n");