exFAT improvements, fixes

Move Exec_Cmd to libcrecovery __popen
Provide opt out build flag for exFAT
Default fstype to exfat on external storage if exfat support is
present and fstype is vfat or auto
Fix invalid unmount errors
Improve handling of unencrypted sdcards on Samsung devices
diff --git a/Android.mk b/Android.mk
index 795b19d..3cfd1fb 100644
--- a/Android.mk
+++ b/Android.mk
@@ -62,7 +62,7 @@
 LOCAL_STATIC_LIBRARIES :=
 LOCAL_SHARED_LIBRARIES :=
 
-LOCAL_STATIC_LIBRARIES += libmtdutils
+LOCAL_STATIC_LIBRARIES += libmtdutils libcrecovery
 LOCAL_STATIC_LIBRARIES += libminadbd libminzip libunz
 LOCAL_STATIC_LIBRARIES += libminuitwrp libpixelflinger_static libpng libjpegtwrp libgui
 LOCAL_SHARED_LIBRARIES += libz libc libstlport libcutils libstdc++ libmincrypt libext4_utils libtar
@@ -282,10 +282,7 @@
     $(commands_recovery_local_path)/crypto/cryptsettings/Android.mk \
     $(commands_recovery_local_path)/crypto/cryptfs/Android.mk \
     $(commands_recovery_local_path)/libcrecovery/Android.mk \
-    $(commands_recovery_local_path)/twmincrypt/Android.mk \
-    $(commands_recovery_local_path)/exfat/mkfs/Android.mk \
-    $(commands_recovery_local_path)/fuse/Android.mk \
-    $(commands_recovery_local_path)/exfat/libexfat/Android.mk
+    $(commands_recovery_local_path)/twmincrypt/Android.mk
 
 ifeq ($(TW_INCLUDE_CRYPTO_SAMSUNG), true)
     include $(commands_recovery_local_path)/crypto/libcrypt_samsung/Android.mk
@@ -295,8 +292,11 @@
     include $(commands_recovery_local_path)/crypto/fs_mgr/Android.mk
 endif
 
-ifeq ($(TW_INCLUDE_FUSE_EXFAT), true)
-    include $(commands_recovery_local_path)/exfat/exfat-fuse/Android.mk
+ifneq ($(TW_NO_EXFAT), true)
+    include $(commands_recovery_local_path)/exfat/exfat-fuse/Android.mk \
+            $(commands_recovery_local_path)/exfat/mkfs/Android.mk \
+            $(commands_recovery_local_path)/fuse/Android.mk \
+            $(commands_recovery_local_path)/exfat/libexfat/Android.mk
 endif
 
 commands_recovery_local_path :=
diff --git a/partition.cpp b/partition.cpp
index e419579..16bc5dc 100644
--- a/partition.cpp
+++ b/partition.cpp
@@ -274,7 +274,6 @@
 			Is_Storage = true;
 			Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
 			Removable = true;
-		}
 #else
 		if (Mount_Point == "/sdcard") {
 			Is_Storage = true;
@@ -284,8 +283,13 @@
 			Setup_AndSec();
 			Mount_Storage_Retry();
 #endif
-		}
 #endif
+			// blkid cannot detect exfat so we force exfat at the start if exfat support is present
+			if (TWFunc::Path_Exists("/sbin/exfat-fuse") && (Fstab_File_System == "vfat" || Fstab_File_System == "auto")) {
+				Fstab_File_System = "exfat";
+				Current_File_System = Fstab_File_System;
+			}
+		}
 #ifdef TW_INTERNAL_STORAGE_PATH
 		if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
 			Is_Storage = true;
@@ -718,7 +722,10 @@
 					LOGI("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
 			} else {
 				LOGI("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
+				Is_Decrypted = true;
 			}
+		} else {
+			Is_Decrypted = false;
 		}
 #endif
 		if (Removable)
@@ -758,7 +765,8 @@
 		if (!Symlink_Mount_Point.empty())
 			umount(Symlink_Mount_Point.c_str());
 
-		if (umount(Mount_Point.c_str()) != 0) {
+		umount(Mount_Point.c_str());
+		if (Is_Mounted()) {
 			if (Display_Error)
 				LOGE("Unable to unmount '%s'\n", Mount_Point.c_str());
 			else
diff --git a/prebuilt/Android.mk b/prebuilt/Android.mk
index 5737431..5e78b34 100644
--- a/prebuilt/Android.mk
+++ b/prebuilt/Android.mk
@@ -44,10 +44,10 @@
 RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libstlport.so
 RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libmincrypt.so
 RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libext4_utils.so
-RELINK_SOURCE_FILES += $(TARGET_RECOVERY_ROOT_OUT)/sbin/mkexfatfs
 
-ifeq ($(TW_INCLUDE_FUSE_EXFAT), true)
+ifneq ($(TW_NO_EXFAT), true)
     RELINK_SOURCE_FILES += $(TARGET_RECOVERY_ROOT_OUT)/sbin/exfat-fuse
+    RELINK_SOURCE_FILES += $(TARGET_RECOVERY_ROOT_OUT)/sbin/mkexfatfs
 endif
 ifeq ($(TW_INCLUDE_BLOBPACK), true)
     RELINK_SOURCE_FILES += $(TARGET_RECOVERY_ROOT_OUT)/sbin/blobpack
diff --git a/twrp-functions.cpp b/twrp-functions.cpp
index 5473c8d..0c4471a 100644
--- a/twrp-functions.cpp
+++ b/twrp-functions.cpp
@@ -21,20 +21,28 @@
 #include "bootloader.h"
 #include "variables.h"
 
+extern "C" {
+	#include "libcrecovery/common.h"
+}
+
 
 /* Execute a command */
 
 int TWFunc::Exec_Cmd(string cmd, string &result) {
 	FILE* exec;
-	char buffer[128];
+	char buffer[130];
 	int ret = 0;
-	exec = popen(cmd.c_str(), "r");
+	exec = __popen(cmd.c_str(), "r");
 	if (!exec) return -1;
 	while(!feof(exec)) {
-		if (fgets(buffer, 128, exec) != NULL)
+		memset(&buffer, 0, sizeof(buffer));
+		if (fgets(buffer, 128, exec) != NULL) {
+			buffer[128] = '\n';
+			buffer[129] = NULL;
 			result += buffer;
+		}
 	}
-	ret = pclose(exec);
+	ret = __pclose(exec);
 	return ret;
 }