diff options
author | Jeremie Laval <jeremie.laval@gmail.com> | 2012-03-13 21:12:37 +0400 |
---|---|---|
committer | Jeremie Laval <jeremie.laval@gmail.com> | 2012-03-13 21:12:37 +0400 |
commit | 8b1b62877edd788b9c928d3647653a2b720aa12d (patch) | |
tree | 37225af7266649d46fdf60ca93249734faeebfec /RootLauncher.cs | |
parent | 552cd23a2e24a2b10dc34ad65c9f7f8eda6ec168 (diff) |
[macdoc] Propagate root launcher error and report them to the user
Diffstat (limited to 'RootLauncher.cs')
-rw-r--r-- | RootLauncher.cs | 50 |
1 files changed, 42 insertions, 8 deletions
diff --git a/RootLauncher.cs b/RootLauncher.cs index d879197..8e2d8d4 100644 --- a/RootLauncher.cs +++ b/RootLauncher.cs @@ -1,9 +1,39 @@ using System; +using System.IO; using System.Diagnostics; using System.Runtime.InteropServices; namespace macdoc { + public enum AuthorizationResultCode + { + Success = 0, + InvalidSet = -60001, + InvalidRef = -60002, + InvalidTag = -60003, + InvalidPointer = -60004, + Denied = -60005, + Canceled = -60006, + InteractionNotAllowed = -60007, + Internal = -60008, + ExternalizeNotAllowed = -60009, + InternalizeNotAllowed = -60010, + InvalidFlags = -60011, + ToolExecuteFailure = -60031, + ToolEnvironmentError = -60032, + BadAddress = -60033, + FileNotFound = -60035 // Addition + } + + public class RootLauncherException : ApplicationException + { + public RootLauncherException (string message) : base (message) + { + } + + public AuthorizationResultCode ResultCode { get; set; } + } + // You were in need of some harsh reality? You have come to the right place public static class RootLauncher { @@ -11,20 +41,24 @@ namespace macdoc public static void LaunchExternalTool (string toolPath) { + if (!File.Exists (toolPath)) + throw new RootLauncherException ("[Launcher] Error, the tool doesn't exist and can't be launched") { ResultCode = AuthorizationResultCode.FileNotFound }; + IntPtr authReference = IntPtr.Zero; - int result = AuthorizationCreate (IntPtr.Zero, IntPtr.Zero, 0, out authReference); - if (result != 0) { - Console.WriteLine ("Error while creating Auth Reference: {0}", result); - return; - } - AuthorizationExecuteWithPrivileges (authReference, toolPath, 0, new string[] { null }, IntPtr.Zero); + AuthorizationResultCode result = AuthorizationCreate (IntPtr.Zero, IntPtr.Zero, 0, out authReference); + if (result != AuthorizationResultCode.Success) + throw new RootLauncherException ("[Launcher] Error while creating Auth Reference") { ResultCode = result }; + + result = AuthorizationExecuteWithPrivileges (authReference, toolPath, 0, new string[] { null }, IntPtr.Zero); + if (result != AuthorizationResultCode.Success) + throw new RootLauncherException ("[Launcher] Error while executing") { ResultCode = result }; } [DllImport (SecurityFramework)] - extern static int AuthorizationCreate (IntPtr autorizationRights, IntPtr environment, int authFlags, out IntPtr authRef); + extern static AuthorizationResultCode AuthorizationCreate (IntPtr autorizationRights, IntPtr environment, int authFlags, out IntPtr authRef); [DllImport (SecurityFramework)] - extern static int AuthorizationExecuteWithPrivileges (IntPtr authRef, string pathToTool, int authFlags, string[] args, IntPtr pipe); + extern static AuthorizationResultCode AuthorizationExecuteWithPrivileges (IntPtr authRef, string pathToTool, int authFlags, string[] args, IntPtr pipe); } public static class UrlLauncher |