fix getting and setting contexts in selinux

recreate lost+found with selinux contexts

Change-Id: I19e8696d47319dfb641520956c8a27c2a796a0c3
diff --git a/libtar/append.c b/libtar/append.c
index 3a8bfc6..dcd8649 100644
--- a/libtar/append.c
+++ b/libtar/append.c
@@ -92,29 +92,22 @@
 
 #ifdef HAVE_SELINUX
 	/* get selinux context */
-	if(t->options & TAR_STORE_SELINUX)
-	{
-		if(t->th_buf.selinux_context != NULL)
-		{
+	if(t->options & TAR_STORE_SELINUX) {
+		if(t->th_buf.selinux_context != NULL) {
 			free(t->th_buf.selinux_context);
 			t->th_buf.selinux_context = NULL;
 		}
 
 		security_context_t selinux_context = NULL;
-		if(getfilecon(realname, &selinux_context) >= 0)
-		{
+		if (lgetfilecon(realname, &selinux_context) >= 0) {
 			t->th_buf.selinux_context = strdup(selinux_context);
+			printf("setting selinux context: %s\n", selinux_context);
 			freecon(selinux_context);
 		}
 		else
-		{
-#ifdef DEBUG
 			perror("Failed to get selinux context");
-#endif
-		}
 	}
 #endif
-
 	/* check if it's a hardlink */
 #ifdef DEBUG
 	puts("    tar_append_file(): checking inode cache for hardlink...");
diff --git a/libtar/extract.c b/libtar/extract.c
index d19ba85..8081aa2 100644
--- a/libtar/extract.c
+++ b/libtar/extract.c
@@ -161,8 +161,9 @@
 #ifdef DEBUG
 		printf("    Restoring SELinux context %s to file %s\n", t->th_buf.selinux_context, realname);
 #endif
-		if(setfilecon(realname, t->th_buf.selinux_context) < 0)
+		if (lsetfilecon(realname, t->th_buf.selinux_context) < 0) {
 			fprintf(stderr, "Failed to restore SELinux context %s!\n", strerror(errno));
+		}
 	}
 #endif
 
diff --git a/partition.cpp b/partition.cpp
index c1c99f6..10cfb9f 100644
--- a/partition.cpp
+++ b/partition.cpp
@@ -53,6 +53,9 @@
 	#include "make_ext4fs.h"
 #endif
 }
+#ifdef HAVE_SELINUX
+#include "selinux/selinux.h"
+#endif
 
 using namespace std;
 
@@ -1272,6 +1275,12 @@
 		LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
 		return false;
 	} else {
+		#ifdef HAVE_SELINUX
+		string sedir = Mount_Point + "/lost+found";
+		PartitionManager.Mount_By_Path(sedir.c_str(), true);
+		rmdir(sedir.c_str());
+		mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
+		#endif
 		return true;
 	}
 #else
diff --git a/partitionmanager.cpp b/partitionmanager.cpp
index ca93a2a..445e4b5 100644
--- a/partitionmanager.cpp
+++ b/partitionmanager.cpp
@@ -901,7 +901,6 @@
 			end_pos = Restore_List.find(";", start_pos);
 		}
 	}
-
 	TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
 	Update_System_Details();
 	UnMount_Main_Partitions();
diff --git a/twrp.cpp b/twrp.cpp
index 765cad3..56ffedb 100644
--- a/twrp.cpp
+++ b/twrp.cpp
@@ -104,10 +104,18 @@
 	gui_loadResources();
 
 #ifdef HAVE_SELINUX
+	if (TWFunc::Path_Exists("/prebuilt_file_contexts")) {
+		if (TWFunc::Path_Exists("/file_contexts")) {
+			printf("Renaming regular /file_contexts -> /file_contexts.bak\n");
+			rename("/file_contexts", "/file_contexts.bak");
+		}
+		printf("Moving /prebuilt_file_contexts -> /file_contexts\n");
+		rename("/prebuilt_file_contexts", "/file_contexts");
+	}
 	struct selinux_opt selinux_options[] = {
 		{ SELABEL_OPT_PATH, "/file_contexts" }
 	};
-    selinux_handle = selabel_open(SELABEL_CTX_FILE, selinux_options, 1);
+	selinux_handle = selabel_open(SELABEL_CTX_FILE, selinux_options, 1);
 	if (!selinux_handle)
 		printf("No file contexts for SELinux\n");
 	else
diff --git a/twrpTar.cpp b/twrpTar.cpp
index 76c23c8..eba482e 100644
--- a/twrpTar.cpp
+++ b/twrpTar.cpp
@@ -34,6 +34,7 @@
 #include <sstream>
 #include <vector>
 #include <dirent.h>
+#include <libgen.h>
 #include <sys/mman.h>
 #include "twrpTar.hpp"
 #include "twcommon.h"
@@ -532,7 +533,7 @@
 			continue; // Skip /data/media
 		if (de->d_type == DT_BLK || de->d_type == DT_CHR)
 			continue;
-		if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0 && strcmp(de->d_name, "lost+foud") != 0)
+		if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0)
 		{
 			unsigned long long folder_size = TWFunc::Get_Folder_Size(FileName, false);
 			if (Archive_Current_Size + folder_size > MAX_ARCHIVE_SIZE) {
@@ -723,6 +724,10 @@
 			if (strcmp(de->d_name, ".") != 0) {
 				subfolder += de->d_name;
 			} else {
+				std::string parentDir = basename(subfolder.c_str());
+				LOGINFO("parentDir: %s\n", parentDir.c_str());
+				if (!parentDir.compare("lost+found"))
+					continue;
 				LOGINFO("tarDirs addFile '%s' including root: %i\n", subfolder.c_str(), include_root);
 				if (addFile(subfolder, include_root) != 0)
 					return -1;