partial void copyFile(Foundation.NSObject sender) { if (textFieldFrom.StringValue == "" || textFieldTo.StringValue == "") { textFieldResults.StringValue = "Must set 'from' and 'to' paths"; return; } // Prepare a task object NSTask task = new NSTask(); task.LaunchPath = "/bin/cp"; string[] args = {textFieldFrom.StringValue, textFieldTo.StringValue}; task.Arguments = args; // Create a pipe to read from NSPipe outPipe = new NSPipe(); task.StandardOutput = outPipe; // Start the process task.Launch(); // Read the output NSData data = outPipe.ReadHandle.ReadDataToEndOfFile(); // Make sure the task terminates normally task.WaitUntilExit(); int status = task.TerminationStatus; // Check status if (status != 0) { textFieldResults.StringValue = "Copy Failed: " + status; return; } textFieldResults.StringValue = String.Format("{0} copied to {1}", textFieldFrom.StringValue, textFieldTo.StringValue); }
public override void ShellSyncCore(string path, string[] arguments, string autoWriteStdin, out string stdout, out string stderr, out int exitCode) { if (autoWriteStdin != "") { throw new Exception("ShellSyncCore::AutoWriteStdin not supported in macOS"); // Never need yet, used only in Linux } try { var pipeOut = new NSPipe(); var pipeErr = new NSPipe(); var t = new NSTask(); t.LaunchPath = path; t.Arguments = arguments; t.StandardOutput = pipeOut; t.StandardError = pipeErr; t.Launch(); t.WaitUntilExit(); //t.Release(); t.Dispose(); NSFileHandle fileOut = pipeOut.ReadHandle; stdout = fileOut.ReadDataToEndOfFile().ToString(); fileOut.CloseFile(); NSFileHandle fileErr = pipeErr.ReadHandle; stderr = fileErr.ReadDataToEndOfFile().ToString(); fileErr.CloseFile(); exitCode = t.TerminationStatus; } catch (Exception ex) { stdout = ""; stderr = "Error: " + ex.Message; exitCode = -1; } }
partial void moveFile(Foundation.NSObject sender) { if (textFieldFrom.StringValue == "" || textFieldTo.StringValue == "") { textFieldResults.StringValue = "Must set 'from' and 'to' paths"; return; } // Prepare a task object NSTask task = new NSTask(); task.LaunchPath = "/bin/mv"; string[] args = { textFieldFrom.StringValue, textFieldTo.StringValue }; task.Arguments = args; // Create a pipe to read from NSPipe outPipe = new NSPipe(); task.StandardOutput = outPipe; // Start the process task.Launch(); // Read the output NSData data = outPipe.ReadHandle.ReadDataToEndOfFile(); // Make sure the task terminates normally task.WaitUntilExit(); int status = task.TerminationStatus; // Check status if (status != 0) { textFieldResults.StringValue = "Move Failed: " + status; return; } textFieldResults.StringValue = String.Format("{0} moved to {1}", textFieldFrom.StringValue, textFieldTo.StringValue); }
public override void ShellSync(string path, string[] arguments, out string stdout, out string stderr, out int exitCode) { try { var pipeOut = new NSPipe(); var pipeErr = new NSPipe(); var t = new NSTask(); t.LaunchPath = path; t.Arguments = arguments; t.StandardOutput = pipeOut; t.StandardError = pipeErr; t.Launch(); t.WaitUntilExit(); //t.Release(); t.Dispose(); NSFileHandle fileOut = pipeOut.ReadHandle; stdout = fileOut.ReadDataToEndOfFile().ToString(); fileOut.CloseFile(); NSFileHandle fileErr = pipeErr.ReadHandle; stderr = fileErr.ReadDataToEndOfFile().ToString(); fileErr.CloseFile(); exitCode = t.TerminationStatus; } catch (Exception ex) { stdout = ""; stderr = "Error: " + ex.Message; exitCode = -1; } }
public static string RunTask(string task, params string[] args) { var r = string.Empty; try { var pipeOut = new NSPipe(); var t = new NSTask(); t.LaunchPath = task; if (args != null) { t.Arguments = args; } var path = "/usr/local/bin"; var env = new NSMutableDictionary(); env.SetValueForKey(new NSString(path), new NSString("PATH")); t.Environment = env; t.StandardOutput = pipeOut; t.StandardError = pipeOut; t.Launch(); t.WaitUntilExit(); //t.Release (); r = pipeOut.ReadHandle.ReadDataToEndOfFile().ToString(); } catch (Exception ex) { Console.WriteLine(task + " failed: " + ex); } return(r); }
public static Process[] GetProcesses() { // spin up the objective-c runtime ObjectiveCRuntime.LoadFramework("Cocoa"); ObjectiveCRuntime.Initialize(); NSAutoreleasePool pool = new NSAutoreleasePool(); // Create our process NSTask getPSTask = new NSTask(); NSPipe getPSStandardOut = new NSPipe(); getPSTask.StandardOutput = getPSStandardOut; getPSTask.LaunchPath = @"/bin/ps"; // add some arguments NSString getPSargumentString = new NSString("-ax"); NSArray getPSarguments = NSArray.ArrayWithObject(getPSargumentString); getPSTask.Arguments = getPSarguments; // We have liftoff getPSTask.Launch(); getPSTask.WaitUntilExit(); // Parse the output NSData getPSoutput = getPSStandardOut.FileHandleForReading.ReadDataToEndOfFile; NSString getPSoutString = new NSString(getPSoutput, NSStringEncoding.NSUTF8StringEncoding); // Split the string of output in to a list of processes string[] getPSsplitString = getPSoutString.ToString().Split(Environment.NewLine.ToCharArray()); // Remove the first and last line of the output string[] processListAsStrings = new string[getPSsplitString.Length - 2]; for (int x = 1; x < getPSsplitString.Length - 1; x++) { processListAsStrings[x - 1] = getPSsplitString[x]; } Process[] processes = new Process[processListAsStrings.Length]; for (int i = 0; i < processes.Length; i++) { string cleanString = RemoveExtraSpaces(processListAsStrings[i]); string[] processDetails = cleanString.Split(' '); Int32 procID = Convert.ToInt32(processDetails[0]); string procName = string.Empty; for (int j = 4; j < processDetails.Length; j++) { if (j == 4) { procName = procName + processDetails[j]; } else { procName = procName + " " + processDetails[j]; } } //Console.WriteLine(procID.ToString() + procName); processes[i] = new Process(procID, procName); } // Dipose our Objective-C objects, gotta love reference counting pool.Release(); return(processes); }
public void ExtractRAR(MainWindow window, NSTableView TableView, NSIndexSet nSelRows, string rarFile, string extractPath) { clsIOPrefs ioPrefs = new clsIOPrefs (); string txtRAR = ioPrefs.GetStringValue ("CaminhoRAR"); if (txtRAR.Length > 0) { if (nSelRows.Count > 0) { ProgressWindowController sheet = null; TableView.InvokeOnMainThread (delegate { sheet = new ProgressWindowController (); sheet.ShowSheet (window); }); sheet.InvokeOnMainThread(delegate{ sheet.ProgressBarMinValue = 0; sheet.ProgressBarMaxValue = nSelRows.Count; }); double conta = 0; bool Cancela = false; nuint[] nRows = nSelRows.ToArray (); ViewArquivosDataSource datasource = null; TableView.InvokeOnMainThread(delegate { datasource = (ViewArquivosDataSource)TableView.DataSource; }); foreach (int lRow in nRows) { string NomeArq = datasource.ViewArquivos [lRow].Nome; ++conta; sheet.InvokeOnMainThread(delegate{ sheet.LabelArqValue = "Extraindo arquivo: " + NomeArq; sheet.ProgressBarValue = conta; }); string[] launchArgs = { "x", rarFile, NomeArq, extractPath }; NSPipe pipeOut = new NSPipe (); NSTask t = new NSTask (); t.LaunchPath = txtRAR; t.Arguments = launchArgs; t.StandardOutput = pipeOut; t.Launch (); t.WaitUntilExit(); //string txtRET = pipeOut.ReadHandle.ReadDataToEndOfFile ().ToString (); sheet.InvokeOnMainThread(delegate{ Cancela = sheet.Canceled; }); if(Cancela) { break; } } sheet.InvokeOnMainThread (delegate { sheet.CloseSheet (); sheet = null; }); if (!Cancela) { TableView.InvokeOnMainThread (delegate { NSAlert alert = new NSAlert () { AlertStyle = NSAlertStyle.Informational, InformativeText = "Arquivo(s) extraido(s) com sucesso !", MessageText = "Extrair arquivos", }; alert.RunSheetModal(window); }); } } } }
public void Write(Action <NSFileHandle> writeHandler) { if (writeHandler == null) { throw new ArgumentNullException(nameof(writeHandler)); } var args = new List <string> { "-extauth" }; if (Create) { args.Add("-c"); } if (Mode > 0) { args.Add("-m"); args.Add("0" + Convert.ToString(Mode, 8)); } args.Add("-w"); args.Add(Path); var task = new NSTask { LaunchPath = "/usr/libexec/authopen", Arguments = args.ToArray() }; var stdinPipe = new NSPipe(); var stderrPipe = new NSPipe(); task.StandardInput = stdinPipe; task.StandardError = stderrPipe; try { task.Launch(); stdinPipe.WriteHandle.WriteData(Authorization.MakeExternalForm()); writeHandler(stdinPipe.WriteHandle); } finally { stdinPipe.WriteHandle.CloseFile(); } task.WaitUntilExit(); try { if (task.TerminationReason == NSTaskTerminationReason.Exit && task.TerminationStatus == 0) { return; } var errorMessage = stderrPipe .ReadHandle .ReadDataToEndOfFile() .ToString() .Trim(); if (string.IsNullOrEmpty(errorMessage)) { errorMessage = $"authopen failed with termination reason {task.TerminationReason} " + $"and termination status {task.TerminationStatus}"; } throw new Exception(errorMessage); } finally { stderrPipe.ReadHandle.CloseFile(); } }
public override bool ReadFromUrl(NSUrl url, string typeName, out NSError outError) { outError = null; // Which files are we getting the zipinfo for? string filename = url.Path; string lPath = ""; string flags = ""; Console.WriteLine("Type Name: {0}", typeName); switch (typeName) { case "public.zip-archive": lPath = "/usr/bin/zipinfo"; flags = "-1"; break; case "public.tar-archive": lPath = "/usr/bin/tar"; flags = "tf"; break; case "org.gnu.gnu-zip-tar-archive": lPath = "/usr/bin/tar"; flags = "tzf"; break; default: NSDictionary eDict = NSDictionary.FromObjectAndKey(new NSString("Archive type not supported"), NSError.LocalizedFailureReasonErrorKey); outError = NSError.FromDomain(NSError.OsStatusErrorDomain,0, eDict); break; } // Prepare a task object NSTask task = new NSTask(); task.LaunchPath = lPath; string[] args = {flags, filename}; task.Arguments = args; // Create a pipe to read from NSPipe outPipe = new NSPipe(); task.StandardOutput = outPipe; // Start the process task.Launch(); // Read the output NSData data = outPipe.ReadHandle.ReadDataToEndOfFile(); // Make sure the task terminates normally task.WaitUntilExit(); int status = task.TerminationStatus; // Check status if (status != 0) { if (outError != null) { NSDictionary eDict = NSDictionary.FromObjectAndKey(new NSString("zipinfo failed"), NSError.LocalizedFailureReasonErrorKey); outError = NSError.FromDomain(NSError.OsStatusErrorDomain,0, eDict); } return false; } // Convert to a string string aString = NSString.FromData(data,NSStringEncoding.UTF8).ToString(); // Break the string into lines MyDocument.filenames = aString.Split(new char[]{'\n'}); Console.WriteLine(MyDocument.filenames); // In case of revert // tableView.ReloadData(); return true; }
public override bool ReadFromUrl(NSUrl url, string typeName, out NSError outError) { outError = null; // Which files are we getting the zipinfo for? string filename = url.Path; string lPath = ""; string flags = ""; Console.WriteLine("Type Name: {0}", typeName); switch (typeName) { case "public.zip-archive": lPath = "/usr/bin/zipinfo"; flags = "-1"; break; case "public.tar-archive": lPath = "/usr/bin/tar"; flags = "tf"; break; case "org.gnu.gnu-zip-tar-archive": lPath = "/usr/bin/tar"; flags = "tzf"; break; default: NSDictionary eDict = NSDictionary.FromObjectAndKey(new NSString("Archive type not supported"), NSError.LocalizedFailureReasonErrorKey); outError = NSError.FromDomain(NSError.OsStatusErrorDomain, 0, eDict); break; } // Prepare a task object NSTask task = new NSTask(); task.LaunchPath = lPath; string[] args = { flags, filename }; task.Arguments = args; // Create a pipe to read from NSPipe outPipe = new NSPipe(); task.StandardOutput = outPipe; // Start the process task.Launch(); // Read the output NSData data = outPipe.ReadHandle.ReadDataToEndOfFile(); // Make sure the task terminates normally task.WaitUntilExit(); int status = task.TerminationStatus; // Check status if (status != 0) { if (outError != null) { NSDictionary eDict = NSDictionary.FromObjectAndKey(new NSString("zipinfo failed"), NSError.LocalizedFailureReasonErrorKey); outError = NSError.FromDomain(NSError.OsStatusErrorDomain, 0, eDict); } return(false); } // Convert to a string string aString = NSString.FromData(data, NSStringEncoding.UTF8).ToString(); // Break the string into lines MyDocument.filenames = aString.Split(new char[] { '\n' }); Console.WriteLine(MyDocument.filenames); // In case of revert // tableView.ReloadData(); return(true); }
public void ExtractRAR(MainWindow window, NSTableView TableView, NSIndexSet nSelRows, string rarFile, string extractPath) { clsIOPrefs ioPrefs = new clsIOPrefs(); string txtRAR = ioPrefs.GetStringValue("CaminhoRAR"); if (txtRAR.Length > 0) { if (nSelRows.Count > 0) { ProgressWindowController sheet = null; TableView.InvokeOnMainThread(delegate { sheet = new ProgressWindowController(); sheet.ShowSheet(window); }); sheet.InvokeOnMainThread(delegate { sheet.ProgressBarMinValue = 0; sheet.ProgressBarMaxValue = nSelRows.Count; }); double conta = 0; bool Cancela = false; nuint[] nRows = nSelRows.ToArray(); ViewArquivosDataSource datasource = null; TableView.InvokeOnMainThread(delegate { datasource = (ViewArquivosDataSource)TableView.DataSource; }); foreach (int lRow in nRows) { string NomeArq = datasource.ViewArquivos [lRow].Nome; ++conta; sheet.InvokeOnMainThread(delegate { sheet.LabelArqValue = "Extraindo arquivo: " + NomeArq; sheet.ProgressBarValue = conta; }); string[] launchArgs = { "x", rarFile, NomeArq, extractPath }; NSPipe pipeOut = new NSPipe(); NSTask t = new NSTask(); t.LaunchPath = txtRAR; t.Arguments = launchArgs; t.StandardOutput = pipeOut; t.Launch(); t.WaitUntilExit(); //string txtRET = pipeOut.ReadHandle.ReadDataToEndOfFile ().ToString (); sheet.InvokeOnMainThread(delegate { Cancela = sheet.Canceled; }); if (Cancela) { break; } } sheet.InvokeOnMainThread(delegate { sheet.CloseSheet(); sheet = null; }); if (!Cancela) { TableView.InvokeOnMainThread(delegate { NSAlert alert = new NSAlert() { AlertStyle = NSAlertStyle.Informational, InformativeText = "Arquivo(s) extraido(s) com sucesso !", MessageText = "Extrair arquivos", }; alert.RunSheetModal(window); }); } } } }