Merge "Move DataManager functions out of twrpTar.cpp" into android-4.4
diff --git a/partition.cpp b/partition.cpp
index 57a7d8d..7427192 100644
--- a/partition.cpp
+++ b/partition.cpp
@@ -53,6 +53,7 @@
 }
 #ifdef HAVE_SELINUX
 #include "selinux/selinux.h"
+#include <selinux/label.h>
 #endif
 
 using namespace std;
@@ -1344,17 +1345,25 @@
 		return false;
 
 #if defined(HAVE_SELINUX) && defined(USE_EXT4)
+	int ret;
+	char *secontext = NULL;
+
 	gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
-	if (make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle) != 0) {
+
+	if (selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
+		LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
+		ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
+	} else {
+		ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
+	}
+	if (ret != 0) {
 		LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
 		return false;
 	} else {
-		#ifdef HAVE_SELINUX
 		string sedir = Mount_Point + "/lost+found";
 		PartitionManager.Mount_By_Path(sedir.c_str(), true);
 		rmdir(sedir.c_str());
 		mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
-		#endif
 		return true;
 	}
 #else
diff --git a/prebuilt/install-recovery.sh b/prebuilt/install-recovery.sh
index 9b43745..f62cf0b 100755
--- a/prebuilt/install-recovery.sh
+++ b/prebuilt/install-recovery.sh
@@ -5,15 +5,13 @@
 # to launch it from init.rc
 
 # Launches SuperSU in daemon mode only on Android 4.3+.
-# Nothing will happen on 4.2.x or older.
+# Nothing will happen on 4.2.x or older, unless SELinux+Enforcing.
 # If you want to force loading the daemon, use "--daemon" instead
 
 /system/xbin/daemonsu --auto-daemon &
 
-# Some apps like to run stuff from this script as well, but the
-# installer will have done "chattr +i" on this file to prevent
-# accidental modification and deletion. In your code, just search 
-# this file for "install-recovery-2.sh", and if present, write
-# there instead.
+# Some apps like to run stuff from this script as well, that will 
+# obviously break root - in your code, just search this file
+# for "install-recovery-2.sh", and if present, write there instead.
 
 /system/etc/install-recovery-2.sh
diff --git a/prebuilt/su b/prebuilt/su
index da77f0a..9415427 100755
--- a/prebuilt/su
+++ b/prebuilt/su
Binary files differ
diff --git a/twrp-functions.cpp b/twrp-functions.cpp
index df71743..bbd3c4c 100644
--- a/twrp-functions.cpp
+++ b/twrp-functions.cpp
@@ -559,13 +559,21 @@
 	if (!PartitionManager.Mount_By_Path("/system", true))
 		return false;
 
+	string propvalue = System_Property_Get("ro.build.version.sdk");
+	string su_perms = "6755";
+	if (!propvalue.empty()) {
+		int sdk_version = atoi(propvalue.c_str());
+		if (sdk_version >= 18)
+			su_perms = "0755";
+	}
+
 	string file = "/system/bin/su";
 	if (TWFunc::Path_Exists(file)) {
 		if (chown(file.c_str(), 0, 0) != 0) {
 			LOGERR("Failed to chown '%s'\n", file.c_str());
 			return false;
 		}
-		if (tw_chmod(file, "6755") != 0) {
+		if (tw_chmod(file, su_perms) != 0) {
 			LOGERR("Failed to chmod '%s'\n", file.c_str());
 			return false;
 		}
@@ -576,7 +584,7 @@
 			LOGERR("Failed to chown '%s'\n", file.c_str());
 			return false;
 		}
-		if (tw_chmod(file, "6755") != 0) {
+		if (tw_chmod(file, su_perms) != 0) {
 			LOGERR("Failed to chmod '%s'\n", file.c_str());
 			return false;
 		}
@@ -587,7 +595,7 @@
 			LOGERR("Failed to chown '%s'\n", file.c_str());
 			return false;
 		}
-		if (tw_chmod(file, "6755") != 0) {
+		if (tw_chmod(file, "0755") != 0) {
 			LOGERR("Failed to chmod '%s'\n", file.c_str());
 			return false;
 		}
@@ -598,7 +606,7 @@
 			LOGERR("Failed to chown '%s'\n", file.c_str());
 			return false;
 		}
-		if (tw_chmod(file, "6755") != 0) {
+		if (tw_chmod(file, su_perms) != 0) {
 			LOGERR("Failed to chmod '%s'\n", file.c_str());
 			return false;
 		}
@@ -980,50 +988,56 @@
 	return Current_Date;
 }
 
-void TWFunc::Auto_Generate_Backup_Name() {
+string TWFunc::System_Property_Get(string Prop_Name) {
 	bool mount_state = PartitionManager.Is_Mounted_By_Path("/system");
 	std::vector<string> buildprop;
-	if (!PartitionManager.Mount_By_Path("/system", true)) {
-		DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date());
-		return;
-	}
+	string propvalue;
+	if (!PartitionManager.Mount_By_Path("/system", true))
+		return propvalue;
 	if (TWFunc::read_file("/system/build.prop", buildprop) != 0) {
-		LOGINFO("Unable to open /system/build.prop for getting backup name.\n");
+		LOGINFO("Unable to open /system/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);
-		return;
+		return propvalue;
 	}
 	int line_count = buildprop.size();
 	int index;
 	size_t start_pos = 0, end_pos;
-	string propname, propvalue;
+	string propname;
 	for (index = 0; index < line_count; index++) {
 		end_pos = buildprop.at(index).find("=", start_pos);
 		propname = buildprop.at(index).substr(start_pos, end_pos);
-		if (propname == "ro.build.display.id") {
+		if (propname == Prop_Name) {
 			propvalue = buildprop.at(index).substr(end_pos + 1, buildprop.at(index).size());
-			string Backup_Name = Get_Current_Date();
-			Backup_Name += " " + propvalue;
-			if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
-				Backup_Name.resize(MAX_BACKUP_NAME_LEN);
-			// Trailing spaces cause problems on some file systems, so remove them
-			string space_check, space = " ";
-			space_check = Backup_Name.substr(Backup_Name.size() - 1, 1);
-			while (space_check == space) {
-				Backup_Name.resize(Backup_Name.size() - 1);
-				space_check = Backup_Name.substr(Backup_Name.size() - 1, 1);
-			}
-			DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
-			break;
+			if (!mount_state)
+				PartitionManager.UnMount_By_Path("/system", false);
+			return propvalue;
 		}
 	}
-	if (propvalue.empty()) {
-		LOGINFO("ro.build.display.id not found in build.prop\n");
-		DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date());
-	}
 	if (!mount_state)
 		PartitionManager.UnMount_By_Path("/system", false);
+	return propvalue;
+}
+
+void TWFunc::Auto_Generate_Backup_Name() {
+	string propvalue = System_Property_Get("ro.build.display.id");
+	if (propvalue.empty()) {
+		DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date());
+		return;
+	}
+	string Backup_Name = Get_Current_Date();
+	Backup_Name += " " + propvalue;
+	if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
+		Backup_Name.resize(MAX_BACKUP_NAME_LEN);
+	// Trailing spaces cause problems on some file systems, so remove them
+	string space_check, space = " ";
+	space_check = Backup_Name.substr(Backup_Name.size() - 1, 1);
+	while (space_check == space) {
+		Backup_Name.resize(Backup_Name.size() - 1);
+		space_check = Backup_Name.substr(Backup_Name.size() - 1, 1);
+	}
+	DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
 }
 
 void TWFunc::Fixup_Time_On_Boot()
diff --git a/twrp-functions.hpp b/twrp-functions.hpp
index 587d772..50bfe2c 100644
--- a/twrp-functions.hpp
+++ b/twrp-functions.hpp
@@ -74,6 +74,7 @@
 	static int Try_Decrypting_File(string fn, string password); // -1 for some error, 0 for failed to decrypt, 1 for decrypted, 3 for decrypted and found gzip format
 	static bool Try_Decrypting_Backup(string Restore_Path, string Password); // true for success, false for failed to decrypt
 	static int Wait_For_Child(pid_t pid, int *status, string Child_Name); // Waits for pid to exit and checks exit status
+	static string System_Property_Get(string Prop_Name);                // Returns value of Prop_Name from reading /system/build.prop
 	static string Get_Current_Date(void);                               // Returns the current date in ccyy-m-dd--hh-nn-ss format
 	static void Auto_Generate_Backup_Name();                            // Populates TW_BACKUP_NAME with a backup name based on current date and ro.build.display.id from /system/build.prop
 	static void Fixup_Time_On_Boot(); // Fixes time on devices which need it