Skip to content

Commit

Permalink
Will now copy to store.
Browse files Browse the repository at this point in the history
  • Loading branch information
krisdb2009 committed Jun 21, 2019
1 parent 554b6af commit 425acae
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 63 deletions.
6 changes: 3 additions & 3 deletions SuperGrate/Classes/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
66 changes: 66 additions & 0 deletions SuperGrate/Classes/Copy.cs
Original file line number Diff line number Diff line change
@@ -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.");
}
}
}
48 changes: 0 additions & 48 deletions SuperGrate/Classes/CopyUSMT.cs

This file was deleted.

35 changes: 25 additions & 10 deletions SuperGrate/Classes/USMT.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,7 +8,7 @@ namespace SuperGrate
class USMT
{
public static bool Running = false;
public static Task<bool> Do(USMTMode Mode, string SID)
public static Task Do(USMTMode Mode, string SID)
{
string exec = "";
string configParams = "";
Expand Down Expand Up @@ -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<bool> HaltUSMT()
Expand All @@ -52,7 +54,21 @@ public static Task<bool> HaltUSMT()
return false;
});
}
static private Task<bool> StartRemoteProcess(string Target, string CLI, string CurrentDirectory)
public static Task<bool> 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();
Expand All @@ -63,10 +79,9 @@ static private Task<bool> 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<bool> KillRemoteProcess(string Target, string ImageName)
static private Task KillRemoteProcess(string Target, string ImageName)
{
return StartRemoteProcess(Target, "taskkill.exe /T /F /IM " + ImageName, @"C:\");
}
Expand Down
2 changes: 1 addition & 1 deletion SuperGrate/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion SuperGrate/SuperGrate.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Classes\Config.cs" />
<Compile Include="Classes\CopyUSMT.cs" />
<Compile Include="Classes\Copy.cs" />
<Compile Include="Classes\Logger.cs" />
<Compile Include="Classes\Misc.cs" />
<Compile Include="Classes\USMT.cs" />
Expand Down

0 comments on commit 425acae

Please sign in to comment.