Update adb sideload feature

Change-Id: Ib503d0e87434e1a28430779302d6565211c26592
diff --git a/adb_install.cpp b/adb_install.cpp
index c731d1c..77b9a8c 100644
--- a/adb_install.cpp
+++ b/adb_install.cpp
@@ -36,7 +36,7 @@
 
 static RecoveryUI* ui = NULL;
 
-static void
+void
 set_usb_driver(bool enabled) {
     int fd = open("/sys/class/android_usb/android0/enable", O_WRONLY);
     if (fd < 0) {
@@ -68,7 +68,7 @@
 }
 
 
-static void
+void
 maybe_restart_adbd() {
     char value[PROPERTY_VALUE_MAX+1];
     int len = property_get("ro.debuggable", value, NULL);
@@ -128,12 +128,14 @@
                 break;
             }
         }
-        printf("FIX ME: need to make adb sideload actually install the file!\n");
-        //result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, install_file, false);
+        property_set("tw_sideload_file", FUSE_SIDELOAD_HOST_PATHNAME);
+        // Install is handled elsewhere in TWRP
+        result = 5; //install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, install_file, false);
         break;
     }
 
-    if (!waited) {
+    // We do this elsewhere in TWRP
+    /*if (!waited) {
         // Calling stat() on this magic filename signals the minadbd
         // subprocess to shut down.
         stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
@@ -143,25 +145,26 @@
         // you just have to 'adb sideload' a file that's not a valid
         // package, like "/dev/null".
         waitpid(child, &status, 0);
-    }
+    }*/
 
-    if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
-        if (WEXITSTATUS(status) == 3) {
-            printf("\nYou need adb 1.0.32 or newer to sideload\nto this device.\n\n");
-        } else if (!WIFSIGNALED(status)) {
-            printf("status %d\n", WEXITSTATUS(status));
+    if (result != 5) {
+        stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
+        waitpid(child, &status, 0);
+        result = -1;
+        if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
+            if (WEXITSTATUS(status) == 3) {
+                printf("\nYou need adb 1.0.32 or newer to sideload\nto this device.\n\n");
+                result = -2;
+            } else if (!WIFSIGNALED(status)) {
+                printf("status %d\n", WEXITSTATUS(status));
+            }
         }
-    }
-    set_usb_driver(false);
-    maybe_restart_adbd();
+	    set_usb_driver(false);
+	    maybe_restart_adbd();
+	    return result;
+	} else {
+        return 0;
+	}
 
-    if (stat(install_file, &st) != 0) {
-        if (errno == ENOENT) {
-            printf("No package received.\n");
-        } else {
-            printf("Error reading package:\n  %s\n", strerror(errno));
-        }
-        return -1;
-    }
-	return 0;
+	return -1; // This should not happen
 }
diff --git a/adb_install.h b/adb_install.h
index a7cb336..604c61d 100644
--- a/adb_install.h
+++ b/adb_install.h
@@ -19,6 +19,8 @@
 
 //class RecoveryUI;
 
+void set_usb_driver(bool enabled);
+void maybe_restart_adbd();
 int apply_from_adb(const char* install_file);
 
 #endif
diff --git a/gui/action.cpp b/gui/action.cpp
index 612e990..6b8565a 100644
--- a/gui/action.cpp
+++ b/gui/action.cpp
@@ -51,6 +51,7 @@
 #include "../twinstall.h"
 #include "cutils/properties.h"
 #include "../minadbd/adb.h"
+#include "../adb_install.h"
 
 int TWinstall_zip(const char* path, int* wipe_cache);
 void run_script(const char *str1, const char *str2, const char *str3, const char *str4, const char *str5, const char *str6, const char *str7, int request_confirm);
@@ -1257,30 +1258,42 @@
 			} else {
 				int wipe_cache = 0;
 				int wipe_dalvik = 0;
-				string Sideload_File;
 
-				if (!PartitionManager.Mount_Current_Storage(false)) {
-					gui_print("Using RAM for sideload storage.\n");
-					Sideload_File = "/tmp/sideload.zip";
-				} else {
-					Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
-				}
-				if (TWFunc::Path_Exists(Sideload_File)) {
-					unlink(Sideload_File.c_str());
-				}
 				gui_print("Starting ADB sideload feature...\n");
 				DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
-				ret = apply_from_adb(Sideload_File.c_str());
+				ret = apply_from_adb("/");
 				DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
+				char file_prop[PROPERTY_VALUE_MAX];
+				property_get("tw_sideload_file", file_prop, "error");
 				if (ret != 0) {
 					ret = 1; // failure
-				} else if (TWinstall_zip(Sideload_File.c_str(), &wipe_cache) == 0) {
-					if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
-						PartitionManager.Wipe_By_Path("/cache");
-					if (wipe_dalvik)
-						PartitionManager.Wipe_Dalvik_Cache();
+					if (ret == -2)
+						gui_print("You need adb 1.0.32 or newer to sideload to this device.\n");
 				} else {
-					ret = 1; // failure
+					if (TWinstall_zip(file_prop, &wipe_cache) == 0) {
+						if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
+							PartitionManager.Wipe_By_Path("/cache");
+						if (wipe_dalvik)
+							PartitionManager.Wipe_Dalvik_Cache();
+					} else {
+						ret = 1; // failure
+					}
+					set_usb_driver(false);
+					maybe_restart_adbd();
+				}
+				if (strcmp(file_prop, "error") != 0) {
+					struct stat st;
+					stat("/sideload/exit", &st);
+					int child_pid, status;
+					char child_prop[PROPERTY_VALUE_MAX];
+					property_get("tw_child_pid", child_prop, "error");
+					if (strcmp(child_prop, "error") == 0) {
+						LOGERR("Unable to get child ID from prop\n");
+					} else {
+						child_pid = atoi(child_prop);
+						LOGINFO("Waiting for child sideload process to exit.\n");
+						waitpid(child_pid, &status, 0);
+					}
 				}
 				if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
 					operation_start("ReinjectTWRP");
@@ -1304,18 +1317,19 @@
 		}
 		if (function == "adbsideloadcancel")
 		{
-			int child_pid;
+			int child_pid, status;
 			char child_prop[PROPERTY_VALUE_MAX];
-			string Sideload_File;
-			Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
-			unlink(Sideload_File.c_str());
+			struct stat st;
+			DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
+			gui_print("Cancelling ADB sideload...\n");
+			stat("/sideload/exit", &st);
+			sleep(1);
 			property_get("tw_child_pid", child_prop, "error");
 			if (strcmp(child_prop, "error") == 0) {
 				LOGERR("Unable to get child ID from prop\n");
 				return 0;
 			}
 			child_pid = atoi(child_prop);
-			gui_print("Cancelling ADB sideload...\n");
 			kill(child_pid, SIGTERM);
 			DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
 			return 0;
diff --git a/minadbd/usb_linux_client.c b/minadbd/usb_linux_client.c
index 29bab15..ec32bcf 100644
--- a/minadbd/usb_linux_client.c
+++ b/minadbd/usb_linux_client.c
@@ -256,7 +256,7 @@
         close_on_exec(fd);
     }
 
-    D("[ usb_init - starting thread ]\n");
+    printf("[ usb_init - starting thread ]\n");
     if(adb_thread_create(&tid, usb_adb_open_thread, h)){
         fatal_errno("cannot create usb thread");
         fprintf(stderr, "cannot create the usb thread()\n");
diff --git a/twinstall.cpp b/twinstall.cpp
index 595c4d2..4e478d4 100644
--- a/twinstall.cpp
+++ b/twinstall.cpp
@@ -257,14 +257,22 @@
 	string strpath = path;
 	ZipArchive Zip;
 
-	gui_print("Installing '%s'...\nChecking for MD5 file...\n", path);
-	md5sum.setfn(strpath);
-	md5_return = md5sum.verify_md5digest();
-	if (md5_return == -2) { // md5 did not match
-		LOGERR("Aborting zip install\n");
+	if (strcmp(path, "error") == 0) {
+		LOGERR("Failed to get adb sideload file: '%s'\n", path);
 		return INSTALL_CORRUPT;
 	}
 
+	gui_print("Installing '%s'...\n", path);
+	if (strlen(path) < 9 || strncmp(path, "/sideload", 9) != 0) {
+		gui_print("Checking for MD5 file...\n");
+		md5sum.setfn(strpath);
+		md5_return = md5sum.verify_md5digest();
+		if (md5_return == -2) { // md5 did not match
+			LOGERR("Aborting zip install\n");
+			return INSTALL_CORRUPT;
+		}
+	}
+
 #ifndef TW_OEM_BUILD
 	DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify);
 #endif