get_args: process /data/cache/command

Change-Id: I744cdc5a72f53fd1224de242e30ff261e6104900
diff --git a/etc/init.rc b/etc/init.rc
index 362687f..9c7d348 100644
--- a/etc/init.rc
+++ b/etc/init.rc
@@ -41,7 +41,7 @@
 
     mkdir /system
     mkdir /data
-    mkdir /cache
+    symlink /data/cache /cache
     mkdir /sideload
     mkdir /mnt/system
     mount tmpfs tmpfs /tmp
diff --git a/install/get_args.cpp b/install/get_args.cpp
index c1fcc96..92f6b10 100755
--- a/install/get_args.cpp
+++ b/install/get_args.cpp
@@ -1,8 +1,6 @@
 #include "twinstall/get_args.h"
 
 std::string stage;
-bool has_cache = false;
-static constexpr const char* COMMAND_FILE = "/cache/recovery/command";
 
 // command line args come from, in decreasing precedence:
 //   - the actual command line
@@ -12,6 +10,7 @@
   CHECK_GT(*argc, 0);
 
   bootloader_message boot = {};
+
   std::string err;
   if (!read_bootloader_message(&boot, &err)) {
     LOG(ERROR) << err;
@@ -54,22 +53,6 @@
     }
   }
 
-  // --- if that doesn't work, try the command file (if we have /cache).
-  if (args.size() == 1 && has_cache) {
-    std::string content;
-    if (ensure_path_mounted(COMMAND_FILE) == 0 &&
-        android::base::ReadFileToString(COMMAND_FILE, &content)) {
-      std::vector<std::string> tokens = android::base::Split(content, "\n");
-      // All the arguments in COMMAND_FILE are needed (unlike the BCB message,
-      // COMMAND_FILE doesn't use filename as the first argument).
-      for (auto it = tokens.begin(); it != tokens.end(); it++) {
-        // Skip empty and '\0'-filled tokens.
-        if (!it->empty() && (*it)[0] != '\0') args.push_back(std::move(*it));
-      }
-      LOG(INFO) << "Got " << args.size() << " arguments from " << COMMAND_FILE;
-    }
-  }
-
   // Write the arguments (excluding the filename in args[0]) back into the
   // bootloader control block. So the device will always boot into recovery to
   // finish the pending work, until finish_recovery() is called.
diff --git a/openrecoveryscript.cpp b/openrecoveryscript.cpp
index 4d1b146..c2ff0fe 100755
--- a/openrecoveryscript.cpp
+++ b/openrecoveryscript.cpp
@@ -1,5 +1,5 @@
 /*
-	Copyright 2003 to 2017 TeamWin
+	Copyright 2003 to 2021 TeamWin
 	This file is part of TWRP/TeamWin Recovery Project.
 
 	TWRP is free software: you can redistribute it and/or modify
diff --git a/partition.cpp b/partition.cpp
index 09b7908..3efa9bf 100755
--- a/partition.cpp
+++ b/partition.cpp
@@ -1,5 +1,5 @@
 /*
-	Copyright 2013 to 2020 TeamWin
+	Copyright 2013 to 2021 TeamWin
 	This file is part of TWRP/TeamWin Recovery Project.
 
 	TWRP is free software: you can redistribute it and/or modify
@@ -1779,6 +1779,14 @@
 	return true;
 }
 
+bool TWPartition::Wipe_Data_Cache(void) {
+	if (!Mount(true))
+		return false;
+	gui_msg(Msg("wiping=Wiping {1}")(Mount_Point + "/cache/"));
+	TWFunc::removeDir(Mount_Point + "/cache/", true);
+	return true;
+}
+
 bool TWPartition::Can_Repair() {
 	if (Mount_Read_Only)
 		return false;
diff --git a/partitionmanager.cpp b/partitionmanager.cpp
index 00fd1c2..5e98b06 100755
--- a/partitionmanager.cpp
+++ b/partitionmanager.cpp
@@ -1,5 +1,5 @@
 /*
-	Copyright 2014 to 2020 TeamWin
+	Copyright 2014 to 2021 TeamWin
 	This file is part of TWRP/TeamWin Recovery Project.
 
 	TWRP is free software: you can redistribute it and/or modify
@@ -63,6 +63,7 @@
 #include "twcommon.h"
 #include "partitions.hpp"
 #include "data.hpp"
+#include "startupArgs.hpp"
 #include "twrp-functions.hpp"
 #include "fixContexts.hpp"
 #include "exclude.hpp"
@@ -1401,6 +1402,16 @@
 
 	if (Local_Path == "/system")
 		Local_Path = Get_Android_Root_Path();
+	if (Path == "/cache") {
+		TWPartition* cache = Find_Partition_By_Path("/cache");
+		if (cache == nullptr) {
+			TWPartition* dat = Find_Partition_By_Path("/data");
+			if (dat) {
+				dat->Wipe_Data_Cache();
+				found = true;
+			}
+		}
+	}
 	// Iterate through all partitions
 	for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
 		if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
@@ -1766,6 +1777,16 @@
 	TWPartition* dat = Find_Partition_By_Path("/data");
 
 	if (dat != NULL) {
+		// reparse for /cache/recovery/command
+		static constexpr const char* COMMAND_FILE = "/data/cache/command";
+		if (TWFunc::Path_Exists(COMMAND_FILE)) {
+			startupArgs startup;
+			std::string content;
+			TWFunc::read_file(COMMAND_FILE, content);
+			std::vector<std::string> args = {content};
+			startup.processRecoveryArgs(args, 0);
+		}
+
 		DataManager::SetValue(TW_IS_DECRYPTED, 1);
 		dat->Is_Decrypted = true;
 		if (!Block_Device.empty()) {
diff --git a/partitions.hpp b/partitions.hpp
index 2a8ae7d..936d99b 100755
--- a/partitions.hpp
+++ b/partitions.hpp
@@ -1,5 +1,5 @@
 /*
-	Copyright 2014 to 2017 TeamWin
+	Copyright 2014 to 2021 TeamWin
 	This file is part of TWRP/TeamWin Recovery Project.
 
 	TWRP is free software: you can redistribute it and/or modify
@@ -130,6 +130,7 @@
 	bool Wipe(string New_File_System);                                        // Wipes the partition
 	bool Wipe();                                                              // Wipes the partition
 	bool Wipe_AndSec();                                                       // Wipes android secure
+	bool Wipe_Data_Cache();                                                   // Wipe /data/cache with devices that have no cache partition
 	bool Can_Repair();                                                        // Checks to see if we have everything needed to be able to repair the current file system
 	uint64_t Get_Max_FileSize();                                              // get partition maxFileSie
 	bool Repair();                                                            // Repairs the current file system
diff --git a/startupArgs.cpp b/startupArgs.cpp
index 4fe6ec0..f385e27 100755
--- a/startupArgs.cpp
+++ b/startupArgs.cpp
@@ -1,5 +1,5 @@
 /*
-	Copyright 2012-2020 TeamWin
+	Copyright 2012-2021 TeamWin
 	This file is part of TWRP/TeamWin Recovery Project.
 
 	TWRP is free software: you can redistribute it and/or modify
@@ -24,6 +24,13 @@
 
 	LOGINFO("Startup Commands: ");
 	for (index = 1; index < args.size(); index++) {
+		if (!processRecoveryArgs(args, index))
+			break;
+	}
+	printf("\n");
+}
+
+bool startupArgs::processRecoveryArgs(std::vector<std::string> args, int index) {
 		if (args[index].find(RESCUE_PARTY) != std::string::npos) {
 		      gui_print("\n\n");
 		      gui_msg(Msg(msg::kError, "rescue_party0=Android Rescue Party trigger! Possible solutions? Either:"));
@@ -53,7 +60,7 @@
 				std::string ORSCommand = "install " + arg;
 				SkipDecryption = arg.find("@") == 1;
 				if (!OpenRecoveryScript::Insert_ORS_Command(ORSCommand))
-					break;
+					return false;
 			}
 		} else if (args[index].find(SEND_INTENT) != std::string::npos) {
 			std::string::size_type eq_pos = args[index].find("=");
@@ -65,17 +72,16 @@
 			}
 		} else if (args[index].find(WIPE_DATA) != std::string::npos) {
 			if (!OpenRecoveryScript::Insert_ORS_Command("wipe data\n"))
-				break;
+				return false;
 		} else if (args[index].find(WIPE_CACHE) != std::string::npos) {
 			if (!OpenRecoveryScript::Insert_ORS_Command("wipe cache\n"))
-				break;
+				return false;
 		} else if (args[index].find(NANDROID) != std::string::npos) {
 			DataManager::SetValue(TW_BACKUP_NAME, gui_parse_text("{@auto_generate}"));
 			if (!OpenRecoveryScript::Insert_ORS_Command("backup BSDCAE\n"))
-				break;
+				return false;
 		}
-	}
-	printf("\n");
+		return true;
 }
 
 bool startupArgs::Should_Skip_Decryption() {
diff --git a/startupArgs.hpp b/startupArgs.hpp
index f01a411..d388f55 100755
--- a/startupArgs.hpp
+++ b/startupArgs.hpp
@@ -45,6 +45,7 @@
 	bool Should_Skip_Decryption();
 	std::string Get_Intent();
 	bool Get_Fastboot_Mode();
+	bool processRecoveryArgs(std::vector<std::string> args, int index);
 
 private:
 	bool SkipDecryption = false;
diff --git a/twrpinstall/include/twinstall/get_args.h b/twrpinstall/include/twinstall/get_args.h
index 712b790..fb9bb43 100755
--- a/twrpinstall/include/twinstall/get_args.h
+++ b/twrpinstall/include/twinstall/get_args.h
@@ -4,7 +4,6 @@
 #include <android-base/file.h>
 #include <android-base/logging.h>
 #include <android-base/strings.h>
-
 #include "recovery_utils/roots.h"
 
 #include "bootloader_message/include/bootloader_message/bootloader_message.h"