log storage: change AB device log storage to /data/recovery

This patchset is now exlcuding /data/cache from backup, and removing it
from the wipe exclusion list to prevent errors on restore and wiping
/data.

This patchset changes the directory from /data/cache to /data/recovery for TWRP
to store logs and other file information. This will help with AB
devices that don't want recovery to mess with /data/cache.

This patchset will make ORS use /data/cache for AB devices.

Remove policy code because we don't need to set a policy for the
recovery directory.

Change-Id: I1d140276314fd34804a72297d6e25a52e58210a0
diff --git a/partition.cpp b/partition.cpp
index 183283d..e53fb78 100755
--- a/partition.cpp
+++ b/partition.cpp
@@ -28,10 +28,8 @@
 #include <unistd.h>
 #include <dirent.h>
 #include <fcntl.h>
-#include <grp.h>
 #include <iostream>
 #include <libgen.h>
-#include <pwd.h>
 #include <zlib.h>
 #include <sstream>
 
@@ -713,8 +711,7 @@
 	}
 	if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
 		Setup_Data_Media();
-		if (!TWFunc::Is_Mount_Wiped("/data"))
-			Recreate_Media_Folder();
+		Recreate_Media_Folder();
 	}
 #else
 	if (datamedia) {
@@ -1190,6 +1187,8 @@
 		DataManager::SetValue("tw_has_internal", 1);
 		DataManager::SetValue("tw_has_data_media", 1);
 		backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
+		backup_exclusions.add_absolute_dir("/data/per_boot"); // DJ9,14Jan2020 - exclude this dir to prevent "error 255" on AOSP ROMs that create and lock it
+		backup_exclusions.add_absolute_dir("/data/cache");
 		wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold"); // adopted storage keys
 		ExcludeAll(Mount_Point + "/system/storage.xml");
 	} else {
@@ -1658,7 +1657,6 @@
 bool TWPartition::Wipe(string New_File_System) {
 	bool wiped = false, update_crypt = false, recreate_media = true;
 	int check;
-	fscrypt_encryption_policy policy;
 
 	if (!Can_Be_Wiped) {
 		gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
@@ -1670,10 +1668,10 @@
 
 	if (Has_Data_Media && Current_File_System == New_File_System) {
 		wiped = Wipe_Data_Without_Wiping_Media();
-		if (Mount_Point == "/data" && TWFunc::get_cache_dir() == AB_CACHE_DIR && !TWFunc::Is_Mount_Wiped("/data")) {
-			bool created = Recreate_AB_Cache_Dir(policy);
-			if (created)
-				gui_msg(Msg(msg::kWarning, "fbe_wipe_msg=WARNING: {1} wiped. FBE device should be booted into Android and not Recovery to set initial FBE policy after wipe.")(TWFunc::get_cache_dir()));
+		if (Mount_Point == "/data" && TWFunc::get_log_dir() == DATA_LOGS_DIR) {
+			bool created = PartitionManager.Recreate_Logs_Dir();
+			if (!created)
+				LOGERR("Unable to create log directory for TWRP\n");
 		}
 		recreate_media = false;
 	} else {
@@ -2031,10 +2029,7 @@
 		// }
 		DataManager::SetValue(TW_IS_ENCRYPTED, 0);
 #ifndef TW_OEM_BUILD
-		if (Is_FBE)
-			gui_msg(Msg(msg::kWarning, "fbe_wipe_msg=WARNING: {1} wiped. FBE device should be booted into Android and not Recovery to set initial FBE policy after wipe.")(TWFunc::get_cache_dir()));
-		else
-			gui_msg("format_data_msg=You may need to reboot recovery to be able to use /data again.");
+		gui_msg("format_data_msg=You may need to reboot recovery to be able to use /data again.");
 #endif
 		ret = true;
 		if (!Key_Directory.empty())
@@ -2443,59 +2438,6 @@
 #endif // ifdef TW_OEM_BUILD
 }
 
-bool TWPartition::Recreate_AB_Cache_Dir(const fscrypt_encryption_policy &policy) {
-	struct passwd pd;
-	struct passwd *pwdptr = &pd;
-	struct passwd *tempPd;
-	char pwdBuf[512];
-	int uid = 0, gid = 0;
-
-	if ((getpwnam_r("system", pwdptr, pwdBuf, sizeof(pwdBuf), &tempPd)) != 0) {
-		LOGERR("unable to get system user id\n");
-		return false;
-	} else {
-		struct group grp;
-		struct group *grpptr = &grp;
-		struct group *tempGrp;
-		char grpBuf[512];
-
-		if ((getgrnam_r("cache", grpptr, grpBuf, sizeof(grpBuf), &tempGrp)) != 0) {
-			LOGERR("unable to get cache group id\n");
-			return false;
-		} else {
-			uid = pd.pw_uid;
-			gid = grp.gr_gid;
-
-			if (!TWFunc::Create_Dir_Recursive(AB_CACHE_DIR, S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP, uid, gid)) {
-				LOGERR("Unable to recreate %s\n", AB_CACHE_DIR);
-				return false;
-			}
-			if (setfilecon(AB_CACHE_DIR, "u:object_r:cache_file:s0") != 0) {	
-				LOGERR("Unable to set contexts for %s\n", AB_CACHE_DIR);
-				return false;
-			}
-			char policy_hex[FS_KEY_DESCRIPTOR_SIZE_HEX];
-			policy_to_hex(policy.master_key_descriptor, policy_hex);
-			LOGINFO("setting policy for %s: %s\n", policy_hex, AB_CACHE_DIR);
-			if (sizeof(policy.master_key_descriptor) > 0) {
-				if (!TWFunc::Set_Encryption_Policy(AB_CACHE_DIR, policy)) {
-					LOGERR("Unable to set encryption policy for %s\n", AB_CACHE_DIR);
-					LOGINFO("Removing %s\n", AB_CACHE_DIR);
-					int ret = TWFunc::removeDir(AB_CACHE_DIR, true);
-					if (ret == -1) {
-						LOGERR("Unable to remove %s\n", AB_CACHE_DIR);
-					}
-					return false;
-				}
-			} else {
-				LOGERR("Not setting empty policy to %s\n", AB_CACHE_DIR);
-				return false;
-			}
-		}
-	}
-	return true;
-}
-
 bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
 	string dir;