Add basic error checking to legacy property init

Let init and rename funcitons return success or failure values.

Change-Id: Ieed86cac8a0dcfd770a89dacc57fd306e7a6ad8d
diff --git a/twinstall.cpp b/twinstall.cpp
index b3b9b77..5b32427 100644
--- a/twinstall.cpp
+++ b/twinstall.cpp
@@ -53,29 +53,48 @@
 
 static const char* properties_path = "/dev/__properties__";
 static const char* properties_path_renamed = "/dev/__properties_kk__";
+static bool legacy_props_env_initd = false;
+static bool legacy_props_path_modified = false;
 
-static void switch_to_legacy_properties()
+static int switch_to_legacy_properties()
 {
-	char tmp[32];
-	int propfd, propsz;
-	legacy_properties_init();
-	legacy_get_property_workspace(&propfd, &propsz);
-	sprintf(tmp, "%d,%d", dup(propfd), propsz);
-	setenv("ANDROID_PROPERTY_WORKSPACE", tmp, 1);
+	if (!legacy_props_env_initd) {
+		if (legacy_properties_init() != 0)
+			return -1;
+
+		char tmp[32];
+		int propfd, propsz;
+		legacy_get_property_workspace(&propfd, &propsz);
+		sprintf(tmp, "%d,%d", dup(propfd), propsz);
+		setenv("ANDROID_PROPERTY_WORKSPACE", tmp, 1);
+		legacy_props_env_initd = true;
+	}
 
 	if (TWFunc::Path_Exists(properties_path)) {
 		// hide real properties so that the updater uses the envvar to find the legacy format properties
-		if (rename(properties_path, properties_path_renamed) != 0)
-			LOGERR("Renaming properties failed: %s (assertions in old installers may fail)\n", strerror(errno));
+		if (rename(properties_path, properties_path_renamed) != 0) {
+			LOGERR("Renaming %s failed: %s\n", properties_path, strerror(errno));
+			return -1;
+		} else {
+			legacy_props_path_modified = true;
+		}
 	}
+
+	return 0;
 }
 
-static void switch_to_new_properties()
+static int switch_to_new_properties()
 {
 	if (TWFunc::Path_Exists(properties_path_renamed)) {
-		if (rename(properties_path_renamed, properties_path) != 0)
-			LOGERR("Restoring properties failed: %s\n", strerror(errno));
+		if (rename(properties_path_renamed, properties_path) != 0) {
+			LOGERR("Renaming %s failed: %s\n", properties_path_renamed, strerror(errno));
+			return -1;
+		} else {
+			legacy_props_path_modified = false;
+		}
 	}
+
+	return 0;
 }
 
 static int Run_Update_Binary(const char *path, ZipArchive *Zip, int* wipe_cache) {
@@ -143,6 +162,13 @@
 	}
 	mzCloseZipArchive(Zip);
 
+	/* 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");
+	}
+
 	pipe(pipe_fd);
 
 	args[0] = Temp_Binary.c_str();
@@ -155,7 +181,6 @@
 
 	pid_t pid = fork();
 	if (pid == 0) {
-		switch_to_legacy_properties();
 		close(pipe_fd[0]);
 		execve(Temp_Binary.c_str(), (char* const*)args, environ);
 		printf("E:Can't execute '%s'\n", Temp_Binary.c_str());
@@ -204,7 +229,16 @@
 	fclose(child_data);
 
 	waitpid(pid, &status, 0);
-	switch_to_new_properties();
+
+	/* 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");
+		}
+	}
+
 	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
 		LOGERR("Error executing updater binary in zip '%s'\n", path);
 		return INSTALL_ERROR;