Update SuperSU to 2.40

The SuperSU install process is getting rather complicated, so we
will modify the install script slightly and use it to install
all the things and stop trying to check to see if root permissions
have been lost like we used to do.

Change-Id: Ibdb64c909d2bd16e12bb35b4f31f572cc29add47
diff --git a/twrp-functions.cpp b/twrp-functions.cpp
index b51024c..4a7b340 100644
--- a/twrp-functions.cpp
+++ b/twrp-functions.cpp
@@ -737,127 +737,6 @@
 	return 0;
 }
 
-int TWFunc::Check_su_Perms(void) {
-	struct stat st;
-	int ret = 0;
-
-	if (!PartitionManager.Mount_By_Path("/system", false))
-		return 0;
-
-	// Check to ensure that perms are 6755 for all 3 file locations
-	if (stat("/system/bin/su", &st) == 0) {
-		if ((st.st_mode & (S_ISUID | S_ISGID | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) != (S_ISUID | S_ISGID | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) || st.st_uid != 0 || st.st_gid != 0) {
-			ret = 1;
-		}
-	}
-	if (stat("/system/xbin/su", &st) == 0) {
-		if ((st.st_mode & (S_ISUID | S_ISGID | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) != (S_ISUID | S_ISGID | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) || st.st_uid != 0 || st.st_gid != 0) {
-			ret += 2;
-		}
-	}
-	if (stat("/system/bin/.ext/.su", &st) == 0) {
-		if ((st.st_mode & (S_ISUID | S_ISGID | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) != (S_ISUID | S_ISGID | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) || st.st_uid != 0 || st.st_gid != 0) {
-			ret += 4;
-		}
-	}
-	return ret;
-}
-
-bool TWFunc::Fix_su_Perms(void) {
-	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, su_perms) != 0) {
-			LOGERR("Failed to chmod '%s'\n", file.c_str());
-			return false;
-		}
-	}
-	file = "/system/xbin/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, su_perms) != 0) {
-			LOGERR("Failed to chmod '%s'\n", file.c_str());
-			return false;
-		}
-	}
-	file = "/system/xbin/daemonsu";
-	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, "0755") != 0) {
-			LOGERR("Failed to chmod '%s'\n", file.c_str());
-			return false;
-		}
-	}
-	file = "/system/bin/.ext/.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, su_perms) != 0) {
-			LOGERR("Failed to chmod '%s'\n", file.c_str());
-			return false;
-		}
-	}
-	file = "/system/etc/install-recovery.sh";
-	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, "0755") != 0) {
-			LOGERR("Failed to chmod '%s'\n", file.c_str());
-			return false;
-		}
-	}
-	file = "/system/etc/init.d/99SuperSUDaemon";
-	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, "0755") != 0) {
-			LOGERR("Failed to chmod '%s'\n", file.c_str());
-			return false;
-		}
-	}
-	file = "/system/app/Superuser.apk";
-	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, "0644") != 0) {
-			LOGERR("Failed to chmod '%s'\n", file.c_str());
-			return false;
-		}
-	}
-	sync();
-	if (!PartitionManager.UnMount_By_Path("/system", true))
-		return false;
-	return true;
-}
-
 int TWFunc::tw_chmod(const string& fn, const string& mode) {
 	long mask = 0;
 	std::string::size_type n = mode.length();
@@ -978,43 +857,7 @@
 	if (!PartitionManager.Mount_By_Path("/system", true))
 		return false;
 
-	TWFunc::Exec_Cmd("/sbin/chattr -i /system/xbin/su");
-	if (copy_file("/supersu/su", "/system/xbin/su", 0755) != 0) {
-		LOGERR("Failed to copy su binary to /system/bin\n");
-		return false;
-	}
-	if (!Path_Exists("/system/bin/.ext")) {
-		mkdir("/system/bin/.ext", 0777);
-	}
-	TWFunc::Exec_Cmd("/sbin/chattr -i /system/bin/.ext/su");
-	if (copy_file("/supersu/su", "/system/bin/.ext/su", 0755) != 0) {
-		LOGERR("Failed to copy su binary to /system/bin/.ext/su\n");
-		return false;
-	}
-	TWFunc::Exec_Cmd("/sbin/chattr -i /system/xbin/daemonsu");
-	if (copy_file("/supersu/su", "/system/xbin/daemonsu", 0755) != 0) {
-		LOGERR("Failed to copy su binary to /system/xbin/daemonsu\n");
-		return false;
-	}
-	if (Path_Exists("/system/etc/init.d")) {
-		TWFunc::Exec_Cmd("/sbin/chattr -i /system/etc/init.d/99SuperSUDaemon");
-		if (copy_file("/supersu/99SuperSUDaemon", "/system/etc/init.d/99SuperSUDaemon", 0755) != 0) {
-			LOGERR("Failed to copy 99SuperSUDaemon to /system/etc/init.d/99SuperSUDaemon\n");
-			return false;
-		}
-	} else {
-		TWFunc::Exec_Cmd("/sbin/chattr -i /system/etc/install-recovery.sh");
-		if (copy_file("/supersu/install-recovery.sh", "/system/etc/install-recovery.sh", 0755) != 0) {
-			LOGERR("Failed to copy install-recovery.sh to /system/etc/install-recovery.sh\n");
-			return false;
-		}
-	}
-	if (copy_file("/supersu/Superuser.apk", "/system/app/Superuser.apk", 0644) != 0) {
-		LOGERR("Failed to copy Superuser app to /system/app\n");
-		return false;
-	}
-	if (!Fix_su_Perms())
-		return false;
+	check_and_run_script("/supersu/install-supersu.sh", "SuperSU");
 	return true;
 }