From cfc7d4b26577891fde4e8083babfc333563de8c9 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Sun, 12 Nov 2017 01:29:24 -0500 Subject: [PATCH 1/3] Greatly speeds up directory copying and deletion --- TGServerService/Program.cs | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/TGServerService/Program.cs b/TGServerService/Program.cs index 5184c9f194..df51009843 100644 --- a/TGServerService/Program.cs +++ b/TGServerService/Program.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.IO; using System.ServiceProcess; +using System.Threading.Tasks; namespace TGServerService { @@ -38,7 +39,7 @@ namespace TGServerService /// The directory to delete /// If , an empty will remain instead of being deleted fully. Incompatible with /// If any files or directories in the root level of match anything in this of s, they won't be deleted. Incompatible with - public static void DeleteDirectory(string path, bool ContentsOnly = false, IList excludeRoot = null) + public static async void DeleteDirectory(string path, bool ContentsOnly = false, IList excludeRoot = null) { var di = new DirectoryInfo(path); if (!di.Exists) @@ -48,7 +49,7 @@ namespace TGServerService excludeRoot[I] = excludeRoot[I].ToLower(); if (CheckDeleteSymlinkDir(di)) return; - NormalizeAndDelete(di, excludeRoot); + await NormalizeAndDelete(di, excludeRoot); if (!ContentsOnly) { if (excludeRoot != null && excludeRoot.Count > 0) @@ -78,24 +79,28 @@ namespace TGServerService /// /// of the directory to empty /// Lowercase file and directory names to skip while emptying this level. Not passed forward - static void NormalizeAndDelete(DirectoryInfo dir, IList excludeRoot) + static async Task NormalizeAndDelete(DirectoryInfo dir, IList excludeRoot) { + var fileTask = Task.Factory.StartNew(() => + { + foreach (var file in dir.GetFiles()) + { + if (excludeRoot != null && excludeRoot.Contains(file.Name.ToLower())) + continue; + file.Attributes = FileAttributes.Normal; + file.Delete(); + } + }); foreach (var subDir in dir.GetDirectories()) { if (excludeRoot != null && excludeRoot.Contains(subDir.Name.ToLower())) continue; if (CheckDeleteSymlinkDir(subDir)) continue; - NormalizeAndDelete(subDir, null); + await NormalizeAndDelete(subDir, null); subDir.Delete(true); } - foreach (var file in dir.GetFiles()) - { - if (excludeRoot != null && excludeRoot.Contains(file.Name.ToLower())) - continue; - file.Attributes = FileAttributes.Normal; - file.Delete(); - } + await fileTask; } /// @@ -105,7 +110,7 @@ namespace TGServerService /// The destination directory /// List of files and directories to ignore while copying /// If no error will be thrown if does not exist - public static void CopyDirectory(string sourceDirName, string destDirName, IList ignore = null, bool ignoreIfNotExists = false) + public static async void CopyDirectory(string sourceDirName, string destDirName, IList ignore = null, bool ignoreIfNotExists = false) { IList realIgnore; if (ignore != null) @@ -116,7 +121,7 @@ namespace TGServerService } else realIgnore = null; - CopyDirectoryImpl(sourceDirName, destDirName, realIgnore, ignoreIfNotExists); + await CopyDirectoryImpl(sourceDirName, destDirName, realIgnore, ignoreIfNotExists); } /// @@ -126,7 +131,7 @@ namespace TGServerService /// The destination directory /// List of lowercase files and directories to ignore while copying /// If no error will be thrown if does not exist - static void CopyDirectoryImpl(string sourceDirName, string destDirName, IList ignore, bool ignoreIfNotExists) { + static async Task CopyDirectoryImpl(string sourceDirName, string destDirName, IList ignore, bool ignoreIfNotExists) { // If the destination directory doesn't exist, create it. if (!Directory.Exists(destDirName)) { @@ -156,14 +161,16 @@ namespace TGServerService file.CopyTo(temppath, true); } + var tasks = new List(); // copy them and their contents to new location. foreach (DirectoryInfo subdir in dirs) { if (ignore != null && ignore.Contains(subdir.Name.ToLower())) continue; string temppath = Path.Combine(destDirName, subdir.Name); - CopyDirectoryImpl(subdir.FullName, temppath, ignore, false); + tasks.Add(CopyDirectoryImpl(subdir.FullName, temppath, ignore, false)); } + await Task.WhenAll(tasks); } /// From c5965f7dbd2111582d62c32448dc1ef0890cdef5 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Sun, 12 Nov 2017 01:37:28 -0500 Subject: [PATCH 2/3] Make directory deletion more async --- TGServerService/Program.cs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/TGServerService/Program.cs b/TGServerService/Program.cs index df51009843..e8e749689d 100644 --- a/TGServerService/Program.cs +++ b/TGServerService/Program.cs @@ -49,7 +49,7 @@ namespace TGServerService excludeRoot[I] = excludeRoot[I].ToLower(); if (CheckDeleteSymlinkDir(di)) return; - await NormalizeAndDelete(di, excludeRoot); + await NormalizeAndDelete(di, excludeRoot, false); if (!ContentsOnly) { if (excludeRoot != null && excludeRoot.Count > 0) @@ -79,10 +79,10 @@ namespace TGServerService /// /// of the directory to empty /// Lowercase file and directory names to skip while emptying this level. Not passed forward - static async Task NormalizeAndDelete(DirectoryInfo dir, IList excludeRoot) + static async Task NormalizeAndDelete(DirectoryInfo dir, IList excludeRoot, bool deleteRoot) { - var fileTask = Task.Factory.StartNew(() => - { + var tasks = new List { Task.Factory.StartNew(() => + { foreach (var file in dir.GetFiles()) { if (excludeRoot != null && excludeRoot.Contains(file.Name.ToLower())) @@ -90,17 +90,19 @@ namespace TGServerService file.Attributes = FileAttributes.Normal; file.Delete(); } - }); + }) }; + foreach (var subDir in dir.GetDirectories()) { if (excludeRoot != null && excludeRoot.Contains(subDir.Name.ToLower())) continue; if (CheckDeleteSymlinkDir(subDir)) continue; - await NormalizeAndDelete(subDir, null); - subDir.Delete(true); + tasks.Add(NormalizeAndDelete(subDir, null, true)); } - await fileTask; + await Task.WhenAll(tasks); + if(deleteRoot) + dir.Delete(true); } /// From ab3a23b0b02e6dc90f3503a6fb2a399d94d4eb59 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Sun, 12 Nov 2017 01:38:27 -0500 Subject: [PATCH 3/3] Fix XMLDoc comment --- TGServerService/Program.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/TGServerService/Program.cs b/TGServerService/Program.cs index e8e749689d..1394802800 100644 --- a/TGServerService/Program.cs +++ b/TGServerService/Program.cs @@ -79,6 +79,7 @@ namespace TGServerService /// /// of the directory to empty /// Lowercase file and directory names to skip while emptying this level. Not passed forward + /// If , will be deleted before the function exits static async Task NormalizeAndDelete(DirectoryInfo dir, IList excludeRoot, bool deleteRoot) { var tasks = new List { Task.Factory.StartNew(() =>