Fix wiping problems and save ecrypt key

Fix problems with the wiping of data/media devices
Save and restore the ecryptfs key for external sdcard on Samsung
encrypted devices.
diff --git a/partition.cpp b/partition.cpp
index 4a7a900..44b00ab 100644
--- a/partition.cpp
+++ b/partition.cpp
@@ -760,6 +760,13 @@
 	if (Mount_Point == "/cache")
 		tmplog_offset = 0;
 
+#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
+	if (Mount_Point == "/data" && Mount(false)) {
+		if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
+			TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
+	}
+#endif
+
 	if (Has_Data_Media)
 		return Wipe_Data_Without_Wiping_Media();
 
@@ -775,7 +782,7 @@
 		wiped = Wipe_EXT23(New_File_System);
 	else if (New_File_System == "vfat")
 		wiped = Wipe_FAT();
-	if (New_File_System == "exfat")
+	else if (New_File_System == "exfat")
 		wiped = Wipe_EXFAT();
 	else if (New_File_System == "yaffs2")
 		wiped = Wipe_MTD();
@@ -785,6 +792,14 @@
 	}
 
 	if (wiped) {
+#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
+		if (Mount_Point == "/data" && Mount(false)) {
+			if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
+				Make_Dir("/data/system", true);
+				TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
+			}
+		}
+#endif
 		Setup_File_System(false);
 		if (Is_Encrypted && !Is_Decrypted) {
 			// just wiped an encrypted partition back to its unencrypted state
@@ -1178,10 +1193,23 @@
 			
 			dir = "/data/";
 			dir.append(de->d_name);
-			TWFunc::removeDir(dir, false);
+			if (de->d_type == DT_DIR) {
+				TWFunc::removeDir(dir, false);
+			} else if (de->d_type == DT_REG || de->d_type == DT_LNK) {
+				if (!unlink(dir.c_str()))
+					LOGI("Unable to unlink '%s'\n", dir.c_str());
+			}
 		}
 		closedir(d);
 		ui_print("Done.\n");
+#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
+		if (Mount_Point == "/data" && Mount(false)) {
+			if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
+				Make_Dir("/data/system", true);
+				TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
+			}
+		}
+#endif
 		return true;
 	}
 	ui_print("Dirent failed to open /data, error!\n");
diff --git a/twrp-functions.cpp b/twrp-functions.cpp
index 53a13f6..d7596b8 100644
--- a/twrp-functions.cpp
+++ b/twrp-functions.cpp
@@ -401,38 +401,38 @@
 }
 
 int TWFunc::removeDir(const string path, bool skipParent) {
-        DIR *d = opendir(path.c_str());
-        int r = 0;
-        string new_path;
+	DIR *d = opendir(path.c_str());
+	int r = 0;
+	string new_path;
 
-        if (d == NULL) {
-                LOGE("Error opening '%s'\n", path.c_str());
-                return -1;
-        }
+	if (d == NULL) {
+		LOGE("Error opening '%s'\n", path.c_str());
+		return -1;
+	}
 
-        if (d) {
-                struct dirent *p;
-                while (!r && (p = readdir(d))) {
+	if (d) {
+		struct dirent *p;
+		while (!r && (p = readdir(d))) {
 			LOGI("checking :%s\n", p->d_name);
-                        if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
-                                continue;
-                        new_path = path + "/";
-                        new_path.append(p->d_name);
-                        if (p->d_type == DT_DIR) {
-                                r = removeDir(new_path, true);
-                                if (!r) {
+			if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
+				continue;
+			new_path = path + "/";
+			new_path.append(p->d_name);
+			if (p->d_type == DT_DIR) {
+				r = removeDir(new_path, true);
+				if (!r) {
 					if (p->d_type == DT_DIR) 
 						r = rmdir(new_path.c_str());
 					else
 						LOGI("Unable to removeDir '%s': %s\n", new_path.c_str(), strerror(errno));
 				}
-                        } else {
-                                r = unlink(new_path.c_str());
-                                if (!r)
-                                        LOGI("Unable to unlink '%s'\n", new_path.c_str());
-                        }
-                }
-                closedir(d);
+			} else if (p->d_type == DT_REG || p->d_type == DT_LNK) {
+				r = unlink(new_path.c_str());
+				if (!r)
+					LOGI("Unable to unlink '%s'\n", new_path.c_str());
+			}
+		}
+		closedir(d);
 
 		if (!r) { 
 			if (skipParent)
@@ -440,8 +440,8 @@
 			else
 				r = rmdir(path.c_str());
 		}
-        }
-        return r;
+	}
+	return r;
 }
 
 int TWFunc::copy_file(string src, string dst, int mode) {