diff --git a/SuperGrate/Classes/Config.cs b/SuperGrate/Classes/Config.cs index c376c1b..95c355b 100644 --- a/SuperGrate/Classes/Config.cs +++ b/SuperGrate/Classes/Config.cs @@ -17,15 +17,15 @@ public static void GenerateConfig() new XComment("ScanState.exe & LoadState.exe CLI Parameters: https://docs.microsoft.com/en-us/windows/deployment/usmt/usmt-command-line-syntax"), new XElement("ScanStateParameters", "/config:Config_SettingsOnly.xml /i:MigUser.xml /r:3 /o"), new XElement("LoadStateParameters", "/config:Config_SettingsOnly.xml /i:MigUser.xml /r:3") - )).Save(@".\SuperGrate\SuperGrate.xml"); + )).Save(@".\SuperGrate.xml"); } public static void LoadConfig() { - if(!File.Exists(@".\SuperGrate\SuperGrate.xml")) + if(!File.Exists(@".\SuperGrate.xml")) { GenerateConfig(); } - XDocument config = XDocument.Load(@".\SuperGrate\SuperGrate.xml"); + XDocument config = XDocument.Load(@".\SuperGrate.xml"); XElement root = config.Element("SuperGrate"); MigrationStorePath = root.Element("MigrationStorePath").Value; ScanStateParameters = root.Element("ScanStateParameters").Value; diff --git a/SuperGrate/Classes/Copy.cs b/SuperGrate/Classes/Copy.cs new file mode 100644 index 0000000..4efc66a --- /dev/null +++ b/SuperGrate/Classes/Copy.cs @@ -0,0 +1,66 @@ +using System.IO; +using System.Threading.Tasks; + +namespace SuperGrate +{ + class Copy + { + public static void CopyFile(string Source, string Destination) + { + FileInfo file = new FileInfo(Source); + FileInfo destination = new FileInfo(Destination); + const int bufferSize = 1024 * 1024; + byte[] buffer = new byte[bufferSize], buffer2 = new byte[bufferSize]; + bool swap = false; + int progress = 0, reportedProgress = 0, read = 0; + long len = file.Length; + float flen = len; + Task writer = null; + using (var source = file.OpenRead()) + using (var dest = destination.OpenWrite()) + { + dest.SetLength(source.Length); + for (long size = 0; size < len; size += read) + { + if ((progress = ((int)((size / flen) * 100))) != reportedProgress) + { + Logger.UpdateProgress(reportedProgress = progress); + } + read = source.Read(swap ? buffer : buffer2, 0, bufferSize); + writer?.Wait(); + writer = dest.WriteAsync(swap ? buffer : buffer2, 0, read); + swap = !swap; + } + writer?.Wait(); + } + } + public static void CopyFolder(string Source, string Destination) + { + Logger.Information("Copying (" + Source + ") to (" + Destination + ")."); + DirectoryInfo source = new DirectoryInfo(Source); + FileInfo[] sourceFiles = source.GetFiles("*", SearchOption.AllDirectories); + string lastStrippedPath = null; + Logger.UpdateProgress(0); + int progress = 0; + foreach (FileInfo file in sourceFiles) + { + string strippedFullPath = file.FullName.Replace(source.FullName, ""); + string strippedPath = strippedFullPath.Replace(file.Name, ""); + if (strippedPath != lastStrippedPath) + { + string targetDirectory = Path.Combine(Destination, strippedPath); + if (!Directory.Exists(targetDirectory)) + { + Directory.CreateDirectory(targetDirectory); + } + lastStrippedPath = strippedPath; + } + Logger.Verbose("Copying: " + strippedFullPath); + file.CopyTo(Path.Combine(Destination, strippedFullPath), true); + Logger.Verbose("Copied."); + Logger.UpdateProgress(progress++, sourceFiles.Length); + } + Logger.Success("Copied."); + } + } +} \ No newline at end of file diff --git a/SuperGrate/Classes/CopyUSMT.cs b/SuperGrate/Classes/CopyUSMT.cs deleted file mode 100644 index d8ba0ff..0000000 --- a/SuperGrate/Classes/CopyUSMT.cs +++ /dev/null @@ -1,48 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.IO; - -namespace SuperGrate -{ - class CopyUSMT - { - public static Task Do() - { - return Task.Run(() => { - Copy(@".\SuperGrate\USMT\", @"\\" + Main.SourceComputer + @"\C$\SuperGrate\"); - return true; - }); - } - private static void Copy(string Source, string Destination) - { - Logger.Information("Copying (" + Source + ") to (" + Destination + ")."); - DirectoryInfo source = new DirectoryInfo(Source); - FileInfo[] sourceFiles = source.GetFiles("*", SearchOption.AllDirectories); - string lastStrippedPath = null; - Logger.UpdateProgress(0); - int progress = 0; - foreach(FileInfo file in sourceFiles) - { - string strippedFullPath = file.FullName.Replace(source.FullName, ""); - string strippedPath = strippedFullPath.Replace(file.Name, ""); - if(strippedPath != lastStrippedPath) - { - string targetDirectory = Path.Combine(Destination, strippedPath); - if (!Directory.Exists(targetDirectory)) - { - Directory.CreateDirectory(targetDirectory); - } - lastStrippedPath = strippedPath; - } - Logger.Verbose("Copying: " + strippedFullPath); - file.CopyTo(Path.Combine(Destination, strippedFullPath), true); - Logger.Verbose("Copied."); - Logger.UpdateProgress(progress++, sourceFiles.Length); - } - Logger.Success("Copied."); - } - } -} diff --git a/SuperGrate/Classes/USMT.cs b/SuperGrate/Classes/USMT.cs index a91a9f6..0f75880 100644 --- a/SuperGrate/Classes/USMT.cs +++ b/SuperGrate/Classes/USMT.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Threading.Tasks; using System.Management; using System.IO; using System.Diagnostics; @@ -12,7 +8,7 @@ namespace SuperGrate class USMT { public static bool Running = false; - public static Task Do(USMTMode Mode, string SID) + public static Task Do(USMTMode Mode, string SID) { string exec = ""; string configParams = ""; @@ -43,7 +39,13 @@ await StartRemoteProcess(target, StartWatchLog(target, "SuperGrate.log"); StartWatchLog(target, "SuperGrate.progress"); await WaitForUsmtExit(target, exec.Replace(".exe", "")); - return false; + await UploadToStore(SID); + }); + } + public static Task CopyUSMT() + { + return Task.Run(() => { + Copy.CopyFolder(@".\USMT\", @"\\" + Main.SourceComputer + @"\C$\SuperGrate\"); }); } public static Task HaltUSMT() @@ -52,7 +54,21 @@ public static Task HaltUSMT() return false; }); } - static private Task StartRemoteProcess(string Target, string CLI, string CurrentDirectory) + public static Task CleanUSMT() + { + return Task.Run(async () => { + return false; + }); + } + private static Task UploadToStore(string SID) + { + return Task.Run(() => { + string Destination = Config.MigrationStorePath + @"\" + SID + @"\"; + Directory.CreateDirectory(Destination); + Copy.CopyFile(@"C:\SuperGrate\USMT\USMT.MIG", Destination + "USMT.MIG"); + }); + } + static private Task StartRemoteProcess(string Target, string CLI, string CurrentDirectory) { return Task.Run(() => { ConnectionOptions conOps = new ConnectionOptions(); @@ -63,10 +79,9 @@ static private Task StartRemoteProcess(string Target, string CLI, string C ManagementPath mPath = new ManagementPath("Win32_Process"); ManagementClass mClass = new ManagementClass(mScope, mPath, null); mClass.InvokeMethod("Create", new object[] { CLI, CurrentDirectory }); - return true; }); } - static private Task KillRemoteProcess(string Target, string ImageName) + static private Task KillRemoteProcess(string Target, string ImageName) { return StartRemoteProcess(Target, "taskkill.exe /T /F /IM " + ImageName, @"C:\"); } diff --git a/SuperGrate/Main.cs b/SuperGrate/Main.cs index dbc13b7..f8f5aa1 100644 --- a/SuperGrate/Main.cs +++ b/SuperGrate/Main.cs @@ -37,7 +37,7 @@ private void Main_Load(object sender, EventArgs e) private async void BtStartStop_Click(object sender, EventArgs e) { tblMainLayout.Enabled = false; - await CopyUSMT.Do(); + await USMT.CopyUSMT(); await USMT.Do(USMTMode.ScanState, SelectedSIDs[0]); tblMainLayout.Enabled = true; } diff --git a/SuperGrate/SuperGrate.csproj b/SuperGrate/SuperGrate.csproj index 8a98be9..dbc59ff 100644 --- a/SuperGrate/SuperGrate.csproj +++ b/SuperGrate/SuperGrate.csproj @@ -56,7 +56,7 @@ - +