GUI: Small improvement to setting up the PATH and working directory of the child process.
This commit is contained in:
parent
eb7b5e0db1
commit
e4f28439c3
@ -42,7 +42,7 @@ namespace com.muldersoft.slunkcrypt.gui.process
|
|||||||
using (FileStream executableFile = ExecutableHelper.GetExecutableFile())
|
using (FileStream executableFile = ExecutableHelper.GetExecutableFile())
|
||||||
{
|
{
|
||||||
string[] arguments = new string[] { COMMAND_PASSWRD, string.Format("{0:D}", length) };
|
string[] arguments = new string[] { COMMAND_PASSWRD, string.Format("{0:D}", length) };
|
||||||
Tuple<int, string[]> result = await ProcessRunner.ExecAsnyc(executableFile.Name, arguments, null, ProcessPriorityClass.BelowNormal, TIMEOUT);
|
Tuple<int, string[]> result = await ProcessRunner.ExecAsnyc(executableFile.Name, arguments, null, null, ProcessPriorityClass.BelowNormal, TIMEOUT);
|
||||||
if (result.Item1 == 0)
|
if (result.Item1 == 0)
|
||||||
{
|
{
|
||||||
foreach (string password in result.Item2)
|
foreach (string password in result.Item2)
|
||||||
|
@ -52,11 +52,15 @@ namespace com.muldersoft.slunkcrypt.gui.process
|
|||||||
|
|
||||||
public async Task<int> ExecuteAsync(Mode mode, string inputFile, string outputFile, string password, SlunkOptions? options = null)
|
public async Task<int> ExecuteAsync(Mode mode, string inputFile, string outputFile, string password, SlunkOptions? options = null)
|
||||||
{
|
{
|
||||||
|
if ((!EnumHelper.IsDefined(mode)) || string.IsNullOrWhiteSpace(inputFile) || string.IsNullOrWhiteSpace(outputFile) || string.IsNullOrWhiteSpace(password))
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Invalid SlunkCrypt parameters!");
|
||||||
|
}
|
||||||
Dictionary<string, string> environmentVariables = new Dictionary<string, string>();
|
Dictionary<string, string> environmentVariables = new Dictionary<string, string>();
|
||||||
environmentVariables.Add("SLUNK_PASSPHRASE", password);
|
environmentVariables.Add("SLUNK_PASSPHRASE", password);
|
||||||
environmentVariables.Add("SLUNK_KEEP_INCOMPLETE", Convert.ToInt32(options.HasValue ? options.Value.keepIncompleteFiles : false).ToString());
|
environmentVariables.Add("SLUNK_KEEP_INCOMPLETE", Convert.ToString(Convert.ToInt32(options.HasValue ? options.Value.keepIncompleteFiles : false)));
|
||||||
environmentVariables.Add("SLUNK_THREADS", Math.Max(0, Math.Min(32, options.HasValue ? options.Value.threadCount : 0)).ToString());
|
environmentVariables.Add("SLUNK_THREADS", Convert.ToString(Math.Max(0, Math.Min(32, options.HasValue ? options.Value.threadCount : 0))));
|
||||||
return await ExecAsnyc(m_executableFile.Name, new string[] { GetCommandLineModeString(mode), inputFile, outputFile }, environmentVariables);
|
return await ExecAsnyc(m_executableFile.Name, new string[] { GetCommandString(mode), inputFile, outputFile }, Path.GetDirectoryName(outputFile), environmentVariables);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Dispose()
|
public override void Dispose()
|
||||||
@ -106,7 +110,7 @@ namespace com.muldersoft.slunkcrypt.gui.process
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string GetCommandLineModeString(Mode mode)
|
private static string GetCommandString(Mode mode)
|
||||||
{
|
{
|
||||||
switch(mode)
|
switch(mode)
|
||||||
{
|
{
|
||||||
|
@ -81,6 +81,7 @@
|
|||||||
<Compile Include="Controls\PasswordToggleBox.xaml.cs">
|
<Compile Include="Controls\PasswordToggleBox.xaml.cs">
|
||||||
<DependentUpon>PasswordToggleBox.xaml</DependentUpon>
|
<DependentUpon>PasswordToggleBox.xaml</DependentUpon>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
<Compile Include="Utilities\EnumHelper.cs" />
|
||||||
<Compile Include="Process\ExecutableHelper.cs" />
|
<Compile Include="Process\ExecutableHelper.cs" />
|
||||||
<Compile Include="Utilities\ApplicationConfig.cs" />
|
<Compile Include="Utilities\ApplicationConfig.cs" />
|
||||||
<Compile Include="Utilities\CPUFeatures.cs" />
|
<Compile Include="Utilities\CPUFeatures.cs" />
|
||||||
|
24
gui/Utilities/EnumHelper.cs
Normal file
24
gui/Utilities/EnumHelper.cs
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/******************************************************************************/
|
||||||
|
/* SlunkCrypt, by LoRd_MuldeR <MuldeR2@GMX.de> */
|
||||||
|
/* This work has been released under the CC0 1.0 Universal license! */
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace com.muldersoft.slunkcrypt.gui.utils
|
||||||
|
{
|
||||||
|
public static class EnumHelper
|
||||||
|
{
|
||||||
|
public static bool IsDefined<T>(T value) where T : struct, IConvertible
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return Enum.IsDefined(typeof(T), value);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -104,6 +104,17 @@ namespace com.muldersoft.slunkcrypt.gui.utils
|
|||||||
catch { }
|
catch { }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static string CreatePathSpec(params string[] entries)
|
||||||
|
{
|
||||||
|
StringBuilder pathBuilder = new StringBuilder();
|
||||||
|
foreach (string pathEntry in entries)
|
||||||
|
{
|
||||||
|
AppendPathEntry(pathBuilder, pathEntry);
|
||||||
|
}
|
||||||
|
AppendPathEntry(pathBuilder, Environment.SystemDirectory);
|
||||||
|
return pathBuilder.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
// Internal methods
|
// Internal methods
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
@ -180,6 +191,25 @@ namespace com.muldersoft.slunkcrypt.gui.utils
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void AppendPathEntry(StringBuilder builder, string pathEntry)
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrWhiteSpace(pathEntry))
|
||||||
|
{
|
||||||
|
if (builder.Length > 0)
|
||||||
|
{
|
||||||
|
builder.Append(Path.PathSeparator);
|
||||||
|
}
|
||||||
|
if (pathEntry.IndexOf(Path.PathSeparator) == -1)
|
||||||
|
{
|
||||||
|
builder.Append(pathEntry.Trim());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
builder.Append('"').Append(pathEntry.Trim()).Append('"');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static bool HasDrivePrefix(string filePath)
|
private static bool HasDrivePrefix(string filePath)
|
||||||
{
|
{
|
||||||
return (filePath.Length > 1) && IsUsEnglishLetter(filePath[0]) && (filePath[1] == Path.VolumeSeparatorChar);
|
return (filePath.Length > 1) && IsUsEnglishLetter(filePath[0]) && (filePath[1] == Path.VolumeSeparatorChar);
|
||||||
|
@ -110,7 +110,7 @@ namespace com.muldersoft.slunkcrypt.gui.utils
|
|||||||
// Public methods
|
// Public methods
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
|
|
||||||
public async Task<int> ExecAsnyc(string executablePath, string[] arguments = null, IReadOnlyDictionary<string, string> environmentVariables = null)
|
public async Task<int> ExecAsnyc(string executablePath, string[] arguments = null, string workingDirectory = null, IReadOnlyDictionary<string, string> environmentVariables = null)
|
||||||
{
|
{
|
||||||
m_dispatcher.VerifyAccess();
|
m_dispatcher.VerifyAccess();
|
||||||
if (m_disposed)
|
if (m_disposed)
|
||||||
@ -124,7 +124,7 @@ namespace com.muldersoft.slunkcrypt.gui.utils
|
|||||||
m_running = true;
|
m_running = true;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return await DoExecAsnyc(executablePath, arguments, environmentVariables);
|
return await DoExecAsnyc(executablePath, arguments, workingDirectory, environmentVariables);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@ -133,13 +133,13 @@ namespace com.muldersoft.slunkcrypt.gui.utils
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<Tuple<int, string[]>> ExecAsnyc(string executableFile, string[] arguments = null, IReadOnlyDictionary<string, string> environmentVariables = null, ProcessPriorityClass? priorityClass = null, TimeSpan? timeout = null)
|
public static async Task<Tuple<int, string[]>> ExecAsnyc(string executableFile, string[] arguments = null, string workingDirectory = null, IReadOnlyDictionary<string, string> environmentVariables = null, ProcessPriorityClass? priorityClass = null, TimeSpan? timeout = null)
|
||||||
{
|
{
|
||||||
|
|
||||||
using (Process process = new Process())
|
using (Process process = new Process())
|
||||||
{
|
{
|
||||||
Task<int> hasExitedTask = InitializeProcess(process);
|
Task<int> hasExitedTask = InitializeProcess(process);
|
||||||
return await DoExecAsnyc(process, hasExitedTask, executableFile, arguments, environmentVariables, priorityClass, timeout);
|
return await DoExecAsnyc(process, hasExitedTask, executableFile, arguments, workingDirectory, environmentVariables, priorityClass, timeout);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -195,11 +195,11 @@ namespace com.muldersoft.slunkcrypt.gui.utils
|
|||||||
return new ProcessExitHandler(process).Task;
|
return new ProcessExitHandler(process).Task;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<int> DoExecAsnyc(string executablePath, string[] arguments, IReadOnlyDictionary<string, string> environmentVariables)
|
private async Task<int> DoExecAsnyc(string executablePath, string[] arguments, string workingDirectory, IReadOnlyDictionary<string, string> environmentVariables)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
StartProcess(m_process, executablePath, arguments, environmentVariables, m_priorityClass);
|
StartProcess(m_process, executablePath, arguments, workingDirectory, environmentVariables, m_priorityClass);
|
||||||
}
|
}
|
||||||
catch (Exception err)
|
catch (Exception err)
|
||||||
{
|
{
|
||||||
@ -208,11 +208,11 @@ namespace com.muldersoft.slunkcrypt.gui.utils
|
|||||||
return await WaitForExit();
|
return await WaitForExit();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task<Tuple<int,string[]>> DoExecAsnyc(Process process, Task<int> hasExited, string executablePath, string[] arguments, IReadOnlyDictionary<string, string> environmentVariables, ProcessPriorityClass? priorityClass, TimeSpan? timeout)
|
private static async Task<Tuple<int,string[]>> DoExecAsnyc(Process process, Task<int> hasExited, string executablePath, string[] arguments, string workingDirectory, IReadOnlyDictionary<string, string> environmentVariables, ProcessPriorityClass? priorityClass, TimeSpan? timeout)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
StartProcess(process, executablePath, arguments, environmentVariables, priorityClass);
|
StartProcess(process, executablePath, arguments, workingDirectory, environmentVariables, priorityClass);
|
||||||
}
|
}
|
||||||
catch (Exception err)
|
catch (Exception err)
|
||||||
{
|
{
|
||||||
@ -222,12 +222,12 @@ namespace com.muldersoft.slunkcrypt.gui.utils
|
|||||||
return Tuple.Create(hasExited.Result, outputLines);
|
return Tuple.Create(hasExited.Result, outputLines);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void StartProcess(Process process, string executablePath, string[] arguments, IReadOnlyDictionary<string, string> environmentVariables, ProcessPriorityClass? priorityClass)
|
private static void StartProcess(Process process, string executablePath, string[] arguments, string workingDirectory, IReadOnlyDictionary<string, string> environmentVariables, ProcessPriorityClass? priorityClass)
|
||||||
{
|
{
|
||||||
process.StartInfo.FileName = executablePath;
|
process.StartInfo.FileName = executablePath;
|
||||||
process.StartInfo.Arguments = CreateArgumentList(arguments);
|
process.StartInfo.Arguments = CreateArgumentList(arguments);
|
||||||
|
process.StartInfo.WorkingDirectory = string.IsNullOrEmpty(workingDirectory) ? GetWorkingDirectory(executablePath) : workingDirectory;
|
||||||
SetupEnvironment(process.StartInfo.EnvironmentVariables, executablePath, environmentVariables);
|
SetupEnvironment(process.StartInfo.EnvironmentVariables, executablePath, environmentVariables);
|
||||||
process.StartInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
|
|
||||||
process.Start();
|
process.Start();
|
||||||
SetProcessPriority(process, priorityClass);
|
SetProcessPriority(process, priorityClass);
|
||||||
}
|
}
|
||||||
@ -390,10 +390,23 @@ namespace com.muldersoft.slunkcrypt.gui.utils
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static string GetWorkingDirectory(string executablePath)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string directory = Path.GetDirectoryName(executablePath);
|
||||||
|
if (!string.IsNullOrWhiteSpace(directory))
|
||||||
|
{
|
||||||
|
return directory;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
return AppDomain.CurrentDomain.BaseDirectory;
|
||||||
|
}
|
||||||
|
|
||||||
private static void SetupEnvironment(StringDictionary dictionary, string executablePath, IReadOnlyDictionary<string, string> environmentVariables)
|
private static void SetupEnvironment(StringDictionary dictionary, string executablePath, IReadOnlyDictionary<string, string> environmentVariables)
|
||||||
{
|
{
|
||||||
string baseDirectory = Path.GetDirectoryName(executablePath);
|
dictionary["PATH"] = PathUtils.CreatePathSpec(Path.GetDirectoryName(executablePath));
|
||||||
dictionary["PATH"] = string.IsNullOrEmpty(baseDirectory) ? Environment.SystemDirectory : baseDirectory;
|
|
||||||
if (!ReferenceEquals(environmentVariables, null))
|
if (!ReferenceEquals(environmentVariables, null))
|
||||||
{
|
{
|
||||||
foreach (KeyValuePair<string, string> entry in environmentVariables)
|
foreach (KeyValuePair<string, string> entry in environmentVariables)
|
||||||
|
Loading…
Reference in New Issue
Block a user