Auto detect possible use of legacy props

* Revert: Make legacy props an option disabled by default
  (reverted from commit 75aa615767457f2b62dda65b16ded41b0e6df11f)

* Instead check for ANDROID_PROPERTY_WORKSPACE in the updater-
  binary to determine whether legacy property service can be
  used

Change-Id: If68d5cca9a2b56edcb1c73db0474668cf46d8c91
diff --git a/data.cpp b/data.cpp
index 5a90553..c0cb9b8 100644
--- a/data.cpp
+++ b/data.cpp
@@ -833,20 +833,19 @@
 #else
 	mConst.SetValue("tw_has_boot_slots", "0");
 #endif
+
 #ifdef TW_NO_LEGACY_PROPS
 	LOGINFO("TW_NO_LEGACY_PROPS := true\n");
-	mConst.SetValue("tw_enable_legacy_props", "0");
 #endif
+
 #ifdef TW_OEM_BUILD
 	LOGINFO("TW_OEM_BUILD := true\n");
 	mConst.SetValue("tw_oem_build", "1");
-	mConst.SetValue("tw_enable_legacy_props", "0");
 #else
 	mConst.SetValue("tw_oem_build", "0");
 	mPersist.SetValue("tw_app_prompt", "1");
 	mPersist.SetValue("tw_app_install_system", "1");
 	mData.SetValue("tw_app_install_status", "0"); // 0 = no status, 1 = not installed, 2 = already installed
-	mPersist.SetValue("tw_enable_legacy_props", "0");
 #endif
 
 	pthread_mutex_unlock(&m_valuesLock);
diff --git a/gui/theme/common/landscape.xml b/gui/theme/common/landscape.xml
index 5e64010..c3d89fe 100644
--- a/gui/theme/common/landscape.xml
+++ b/gui/theme/common/landscape.xml
@@ -447,12 +447,6 @@
 				<data variable="tw_install_reboot"/>
 			</checkbox>
 
-			<checkbox>
-				<placement x="%col1_x_right%" y="%row13a_y%"/>
-				<text>{@enable_legacy_props_chk=Enable legacy properties for old zips}</text>
-				<data variable="tw_enable_legacy_props"/>
-			</checkbox>
-
 			<button style="main_button_half_width">
 				<placement x="%col1_x_left%" y="%row15a_y%"/>
 				<text>{@zip_add_btn=Add more Zips}</text>
diff --git a/gui/theme/common/languages/en.xml b/gui/theme/common/languages/en.xml
index 559475c..8a1be18 100644
--- a/gui/theme/common/languages/en.xml
+++ b/gui/theme/common/languages/en.xml
@@ -82,7 +82,6 @@
 		<string name="folder">Folder:</string>
 		<string name="file">File:</string>
 		<string name="zip_sig_chk">Zip signature verification</string>
-		<string name="enable_legacy_props_chk">Enable legacy properties for old zips</string>
 		<string name="inject_twrp_chk">Inject TWRP after install</string>
 		<string name="install_reboot_chk">Reboot after installation is complete</string>
 		<string name="options_hdr">Options</string>
diff --git a/gui/theme/common/portrait.xml b/gui/theme/common/portrait.xml
index c101014..f163bea 100644
--- a/gui/theme/common/portrait.xml
+++ b/gui/theme/common/portrait.xml
@@ -393,14 +393,8 @@
 			</checkbox>
 
 			<checkbox>
-				<placement x="%indent%" y="%row15_y%"/>
-				<text>{@enable_legacy_props_chk=Enable legacy properties for old zips}</text>
-				<data variable="tw_enable_legacy_props"/>
-			</checkbox>
-
-			<checkbox>
 				<condition var1="tw_has_injecttwrp" var2="1"/>
-				<placement x="%indent%" y="%row16a_y%"/>
+				<placement x="%indent%" y="%row15_y%"/>
 				<text>{@inject_twrp_chk=Inject TWRP after install}</text>
 				<data variable="tw_inject_after_zip"/>
 			</checkbox>
diff --git a/gui/theme/common/watch.xml b/gui/theme/common/watch.xml
index 2a82345..d5ef869 100644
--- a/gui/theme/common/watch.xml
+++ b/gui/theme/common/watch.xml
@@ -555,12 +555,6 @@
 				<data variable="tw_install_reboot"/>
 			</checkbox>
 
-			<checkbox>
-				<placement x="%indent%" y="%row5a_y%"/>
-				<text>{@enable_legacy_props_chk=Enable legacy properties for old zips}</text>
-				<data variable="tw_enable_legacy_props"/>
-			</checkbox>
-
 			<button>
 				<placement x="%col1_x_left%" y="%row11_y%"/>
 				<highlight color="%highlight_color%"/>
diff --git a/twinstall.cpp b/twinstall.cpp
index b0b6f3d..0aaab45 100644
--- a/twinstall.cpp
+++ b/twinstall.cpp
@@ -16,10 +16,16 @@
 	along with TWRP.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
 #include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <limits.h>
+#include <sys/mman.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
 #include <unistd.h>
@@ -167,19 +173,51 @@
 	return INSTALL_SUCCESS;
 }
 
+static bool update_binary_has_legacy_properties(const char *binary) {
+	const char str_to_match[] = "ANDROID_PROPERTY_WORKSPACE";
+	int len_to_match = sizeof(str_to_match) - 1;
+	bool found = false;
+
+	int fd = open(binary, O_RDONLY);
+	if (fd < 0) {
+		LOGINFO("has_legacy_properties: Could not open %s: %s!\n", binary, strerror(errno));
+		return false;
+	}
+
+	struct stat finfo;
+	if (fstat(fd, &finfo) < 0) {
+		LOGINFO("has_legacy_properties: Could not fstat %d: %s!\n", fd, strerror(errno));
+		close(fd);
+		return false;
+	}
+
+	void *data = mmap(NULL, finfo.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+	if (data == MAP_FAILED) {
+		LOGINFO("has_legacy_properties: mmap (size=%lld) failed: %s!\n", finfo.st_size, strerror(errno));
+	} else {
+		if (memmem(data, finfo.st_size, str_to_match, len_to_match)) {
+			LOGINFO("has_legacy_properties: Found legacy property match!\n");
+			found = true;
+		}
+		munmap(data, finfo.st_size);
+	}
+	close(fd);
+
+	return found;
+}
+
 static int Run_Update_Binary(const char *path, ZipWrap *Zip, int* wipe_cache, zip_type ztype) {
 	int ret_val, pipe_fd[2], status, zip_verify;
 	char buffer[1024];
 	FILE* child_data;
 
 #ifndef TW_NO_LEGACY_PROPS
-	if (DataManager::GetIntValue("tw_enable_legacy_props") != 0) {
-		/* Set legacy properties */
-		if (switch_to_legacy_properties() != 0) {
-			LOGERR("Legacy property environment did not initialize successfully. Properties may not be detected.\n");
-		} else {
-			LOGINFO("Legacy property environment initialized.\n");
-		}
+	if (!update_binary_has_legacy_properties(TMP_UPDATER_BINARY_PATH)) {
+		LOGINFO("Legacy property environment not used in updater.\n");
+	} else if (switch_to_legacy_properties() != 0) { /* Set legacy properties */
+		LOGERR("Legacy property environment did not initialize successfully. Properties may not be detected.\n");
+	} else {
+		LOGINFO("Legacy property environment initialized.\n");
 	}
 #endif
 
@@ -260,14 +298,12 @@
 	int waitrc = TWFunc::Wait_For_Child(pid, &status, "Updater");
 
 #ifndef TW_NO_LEGACY_PROPS
-	if (DataManager::GetIntValue("tw_enable_legacy_props") != 0) {
-		/* Unset legacy properties */
-		if (legacy_props_path_modified) {
-			if (switch_to_new_properties() != 0) {
-				LOGERR("Legacy property environment did not disable successfully. Legacy properties may still be in use.\n");
-			} else {
-				LOGINFO("Legacy property environment disabled.\n");
-			}
+	/* Unset legacy properties */
+	if (legacy_props_path_modified) {
+		if (switch_to_new_properties() != 0) {
+			LOGERR("Legacy property environment did not disable successfully. Legacy properties may still be in use.\n");
+		} else {
+			LOGINFO("Legacy property environment disabled.\n");
 		}
 	}
 #endif