/// <summary> /// Executes application logic /// </summary> /// <param name="args">Commandline arguments</param> public int ExecuteAsync(string[] args) { string hostname = (args.Any()) ? args.First() : "localhost"; _client = new RemoteExecutorClient(hostname); _client.ProcessWriteToStdOut += OnOutputDataReceived; _client.ProcessWriteToStdErr += OnErrorDataReceived; _client.ProcessExited += delegate(int exitcode) { _client.ProcessWriteToStdOut -= OnOutputDataReceived; _client.ProcessWriteToStdErr -= OnErrorDataReceived; _processExitCode = exitcode; }; _client.Connect(); _client.AutoReconnectEnabled = true; _client.StartProcessAsync(@"cmd", ""); // Start backgroundworker which reads the STDIN from console // In this way the program is not blocked by Console.Read() method // and is closed after hosted process has exited _inputReader.RunWorkerAsync(); // Wait for exit of process while (!_processExitCode.HasValue) { } _client.Disconnect(); // ReSharper disable once PossibleInvalidOperationException return((_processExitCode.HasValue) ? _processExitCode.Value : -1000); }
/// <summary> /// Connects to the running Eraser instance for erasures. /// </summary> /// <returns>The connectin with the remote instance.</returns> private static RemoteExecutorClient CommandConnect() { try { RemoteExecutorClient result = new RemoteExecutorClient(); result.Run(); if (!result.IsConnected) { //The client cannot connect to the server. This probably means //that the server process isn't running. Start an instance. Process eraserInstance = Process.Start( Assembly.GetExecutingAssembly().Location, "/quiet"); eraserInstance.WaitForInputIdle(); //Wait for the server to be initialised. for (int i = 0; !result.IsConnected; ++i) { Thread.Sleep(100); result.Run(); //After 10s, we should probably give up. if (i > 100) { throw new IOException(S._("Eraser cannot connect to the running " + "instance for erasures.")); } } } return(result); } catch (UnauthorizedAccessException e) { //We can't connect to the pipe because the other instance of Eraser //is running with higher privileges than this instance. throw new UnauthorizedAccessException(S._("Another instance of Eraser " + "is already running but it is running with higher privileges than " + "this instance of Eraser. Tasks cannot be added in this manner.\n\n" + "Close the running instance of Eraser and start it again without " + "administrator privileges, or run the command again as an " + "administrator.", e)); } }
private void ImportTaskList() { ImportTaskListCommandLine cmdLine = (ImportTaskListCommandLine)Arguments; try { using (RemoteExecutorClient client = new RemoteExecutorClient()) { client.Run(); if (!client.IsConnected) { Process eraserInstance = Process.Start( Assembly.GetExecutingAssembly().Location, "--quiet"); eraserInstance.WaitForInputIdle(); client.Run(); if (!client.IsConnected) throw new IOException("Eraser cannot connect to the running " + "instance for erasures."); } foreach (string path in cmdLine.Files) using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read)) client.Tasks.LoadFromStream(stream); } } catch (UnauthorizedAccessException e) { throw new UnauthorizedAccessException("Another instance of Eraser " + "is already running but it is running with higher privileges than " + "this instance of Eraser. Tasks cannot be added in this manner.\n\n" + "Close the running instance of Eraser and start it again without " + "administrator privileges, or run the command again as an " + "administrator.", e); } }
private void AddTask() { AddTaskCommandLine taskArgs = (AddTaskCommandLine)Arguments; Task task = new Task(); ErasureMethod method = taskArgs.ErasureMethod == Guid.Empty ? ErasureMethodManager.Default : ErasureMethodManager.GetInstance(taskArgs.ErasureMethod); foreach (ErasureTarget target in taskArgs.Targets) { target.Method = method; task.Targets.Add(target); } if (task.Targets.Count == 0) throw new ArgumentException("Tasks must contain at least one erasure target."); task.Schedule = taskArgs.Schedule; try { using (RemoteExecutorClient client = new RemoteExecutorClient()) { client.Run(); if (!client.IsConnected) { Process eraserInstance = Process.Start( Assembly.GetExecutingAssembly().Location, "--quiet"); eraserInstance.WaitForInputIdle(); client.Run(); if (!client.IsConnected) throw new IOException("Eraser cannot connect to the running " + "instance for erasures."); } client.Tasks.Add(task); } } catch (UnauthorizedAccessException e) { throw new UnauthorizedAccessException("Another instance of Eraser " + "is already running but it is running with higher privileges than " + "this instance of Eraser. Tasks cannot be added in this manner.\n\n" + "Close the running instance of Eraser and start it again without " + "administrator privileges, or run the command again as an " + "administrator.", e); } }
private static void CommandAddTask(ConsoleArguments arg) { AddTaskArguments arguments = (AddTaskArguments)arg; Task task = new Task(); ErasureMethod method = arguments.ErasureMethod == Guid.Empty ? ErasureMethodManager.Default : ErasureMethodManager.GetInstance(arguments.ErasureMethod); switch (arguments.Schedule.ToUpperInvariant()) { case "NOW": task.Schedule = Schedule.RunNow; break; case "MANUALLY": task.Schedule = Schedule.RunManually; break; case "RESTART": task.Schedule = Schedule.RunOnRestart; break; default: throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Unknown schedule type: {0}", arguments.Schedule), "--schedule"); } List<string> trueValues = new List<string>(new string[] { "yes", "true" }); string[] strings = new string[] { "(?<recycleBin>recyclebin)", "unused=(?<unusedVolume>.*)(?<unusedTips>,clusterTips(=(?<unusedTipsValue>true|false))?)?", "dir=(?<directoryName>.*)(?<directoryParams>(?<directoryExcludeMask>,-[^,]+)|(?<directoryIncludeMask>,\\+[^,]+)|(?<directoryDeleteIfEmpty>,deleteIfEmpty(=(?<directoryDeleteIfEmptyValue>true|false))?))*", "file=(?<fileName>.*)" }; Regex regex = new Regex(string.Join("|", strings), RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.RightToLeft); foreach (string argument in arguments.PositionalArguments) { Match match = regex.Match(argument); if (match.Captures.Count == 0) { Console.WriteLine("Unknown argument: {0}, skipped.", argument); continue; } ErasureTarget target = null; if (match.Groups["recycleBin"].Success) { target = new RecycleBinTarget(); } else if (match.Groups["unusedVolume"].Success) { UnusedSpaceTarget unusedSpaceTarget = new UnusedSpaceTarget(); target = unusedSpaceTarget; unusedSpaceTarget.Drive = match.Groups["unusedVolume"].Value; if (!match.Groups["unusedTips"].Success) unusedSpaceTarget.EraseClusterTips = false; else if (!match.Groups["unusedTipsValue"].Success) unusedSpaceTarget.EraseClusterTips = true; else unusedSpaceTarget.EraseClusterTips = trueValues.IndexOf(match.Groups["unusedTipsValue"].Value) != -1; } else if (match.Groups["directoryName"].Success) { FolderTarget folderTarget = new FolderTarget(); target = folderTarget; folderTarget.Path = match.Groups["directoryName"].Value; if (!match.Groups["directoryDeleteIfEmpty"].Success) folderTarget.DeleteIfEmpty = false; else if (!match.Groups["directoryDeleteIfEmptyValue"].Success) folderTarget.DeleteIfEmpty = true; else folderTarget.DeleteIfEmpty = trueValues.IndexOf(match.Groups["directoryDeleteIfEmptyValue"].Value) != -1; if (match.Groups["directoryExcludeMask"].Success) folderTarget.ExcludeMask += match.Groups["directoryExcludeMask"].Value.Remove(0, 2) + ' '; if (match.Groups["directoryIncludeMask"].Success) folderTarget.IncludeMask += match.Groups["directoryIncludeMask"].Value.Remove(0, 2) + ' '; } else if (match.Groups["fileName"].Success) { FileTarget fileTarget = new FileTarget(); target = fileTarget; fileTarget.Path = match.Groups["fileName"].Value; } if (target == null) continue; target.Method = method; task.Targets.Add(target); } if (task.Targets.Count == 0) throw new ArgumentException("Tasks must contain at least one erasure target."); try { using (RemoteExecutorClient client = new RemoteExecutorClient()) { client.Run(); if (!client.IsConnected) { Process eraserInstance = Process.Start( Assembly.GetExecutingAssembly().Location, "--quiet"); eraserInstance.WaitForInputIdle(); client.Run(); if (!client.IsConnected) throw new IOException("Eraser cannot connect to the running " + "instance for erasures."); } client.Tasks.Add(task); } } catch (UnauthorizedAccessException e) { throw new UnauthorizedAccessException("Another instance of Eraser " + "is already running but it is running with higher privileges than " + "this instance of Eraser. Tasks cannot be added in this manner.\n\n" + "Close the running instance of Eraser and start it again without " + "administrator privileges, or run the command again as an " + "administrator.", e); } }