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>();
environmentVariables.Add("SLUNK_PASSPHRASE", password);
- environmentVariables.Add("SLUNK_KEEP_INCOMPLETE", Convert.ToInt32(options.HasValue ? options.Value.keepIncompleteFiles : false).ToString());
- environmentVariables.Add("SLUNK_THREADS", Math.Max(0, Math.Min(32, options.HasValue ? options.Value.threadCount : 0)).ToString());
- return await ExecAsnyc(m_executableFile.Name, new string[] { GetCommandLineModeString(mode), inputFile, outputFile }, environmentVariables);
+ environmentVariables.Add("SLUNK_KEEP_INCOMPLETE", Convert.ToString(Convert.ToInt32(options.HasValue ? options.Value.keepIncompleteFiles : false)));
+ 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[] { GetCommandString(mode), inputFile, outputFile }, Path.GetDirectoryName(outputFile), environmentVariables);
}
public override void Dispose()
return false;
}
- private static string GetCommandLineModeString(Mode mode)
+ private static string GetCommandString(Mode mode)
{
switch(mode)
{
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
// =============================================================================
}
}
+ 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)
{
return (filePath.Length > 1) && IsUsEnglishLetter(filePath[0]) && (filePath[1] == Path.VolumeSeparatorChar);
// 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();
if (m_disposed)
m_running = true;
try
{
- return await DoExecAsnyc(executablePath, arguments, environmentVariables);
+ return await DoExecAsnyc(executablePath, arguments, workingDirectory, environmentVariables);
}
finally
{
}
}
- 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())
{
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);
}
}
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
{
- StartProcess(m_process, executablePath, arguments, environmentVariables, m_priorityClass);
+ StartProcess(m_process, executablePath, arguments, workingDirectory, environmentVariables, m_priorityClass);
}
catch (Exception err)
{
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
{
- StartProcess(process, executablePath, arguments, environmentVariables, priorityClass);
+ StartProcess(process, executablePath, arguments, workingDirectory, environmentVariables, priorityClass);
}
catch (Exception err)
{
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.Arguments = CreateArgumentList(arguments);
+ process.StartInfo.WorkingDirectory = string.IsNullOrEmpty(workingDirectory) ? GetWorkingDirectory(executablePath) : workingDirectory;
SetupEnvironment(process.StartInfo.EnvironmentVariables, executablePath, environmentVariables);
- process.StartInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
process.Start();
SetProcessPriority(process, priorityClass);
}
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)
{
- string baseDirectory = Path.GetDirectoryName(executablePath);
- dictionary["PATH"] = string.IsNullOrEmpty(baseDirectory) ? Environment.SystemDirectory : baseDirectory;
+ dictionary["PATH"] = PathUtils.CreatePathSpec(Path.GetDirectoryName(executablePath));
if (!ReferenceEquals(environmentVariables, null))
{
foreach (KeyValuePair<string, string> entry in environmentVariables)