From 9f70843ef49d4d14a3c7b35c9deb39491734597c Mon Sep 17 00:00:00 2001 From: da3dsoul Date: Mon, 26 Dec 2016 23:29:47 -0500 Subject: [PATCH] Fix NullPointer and hopefully fix setting folder permissions --- JMMServer/MainWindow.xaml.cs | 16 ++++-------- JMMServer/Utils.cs | 50 +++++++++++++++++++++++++++++------- 2 files changed, 46 insertions(+), 20 deletions(-) diff --git a/JMMServer/MainWindow.xaml.cs b/JMMServer/MainWindow.xaml.cs index 6eb7ea1f0..eba190125 100644 --- a/JMMServer/MainWindow.xaml.cs +++ b/JMMServer/MainWindow.xaml.cs @@ -262,8 +262,6 @@ public MainWindow() txtServerPort.Text = ServerSettings.JMMServerPort; - - btnToolbarHelp.Click += new RoutedEventHandler(btnToolbarHelp_Click); btnApplyServerPort.Click += new RoutedEventHandler(btnApplyServerPort_Click); btnUpdateMediaInfo.Click += new RoutedEventHandler(btnUpdateMediaInfo_Click); @@ -332,24 +330,20 @@ public MainWindow() btnJMMDisableStartWithWindows.Click += new RoutedEventHandler(btnJMMDisableStartWithWindows_Click); btnUpdateAniDBLogin.Click += new RoutedEventHandler(btnUpdateAniDBLogin_Click); - - btnHasherClear.Click += new RoutedEventHandler(btnHasherClear_Click); btnGeneralClear.Click += new RoutedEventHandler(btnGeneralClear_Click); btnImagesClear.Click += new RoutedEventHandler(btnImagesClear_Click); - - //automaticUpdater.MenuItem = mnuCheckForUpdates; - - ServerState.Instance.LoadSettings(); - - cboLanguages.SelectionChanged += new SelectionChangedEventHandler(cboLanguages_SelectionChanged); InitCulture(); Instance = this; - // run rotator once and set 24h delay + //automaticUpdater.MenuItem = mnuCheckForUpdates; + + ServerState.Instance.LoadSettings(); + + // run rotator once and set 24h delay logrotator.Start(); StartLogRotatorTimer(); } diff --git a/JMMServer/Utils.cs b/JMMServer/Utils.cs index b0bcf7b51..094bcb034 100644 --- a/JMMServer/Utils.cs +++ b/JMMServer/Utils.cs @@ -58,16 +58,48 @@ public static void CopyTo(this Stream input, Stream output, int bufferSize = 0x1 public static void GrantAccess(string fullPath) { //C# version do not work, do not inherit permissions to childs. - Process proc = new Process(); - - proc.StartInfo.FileName = "icacls"; - proc.StartInfo.Arguments = "\"" + fullPath + "\" /grant *S-1-1-0:(OI)(CI)F /T"; - proc.StartInfo.Verb = "runas"; - proc.StartInfo.CreateNoWindow = true; - proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; - proc.StartInfo.UseShellExecute = true; - proc.Start(); + string BatchFile = Path.Combine(System.IO.Path.GetTempPath(), "GrantAccess.bat"); + int exitCode = -1; + Process proc = new Process(); + + proc.StartInfo.FileName = "cmd.exe"; + proc.StartInfo.Arguments = String.Format(@"/c {0}", BatchFile); + proc.StartInfo.Verb = "runas"; + proc.StartInfo.CreateNoWindow = true; + proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; + proc.StartInfo.UseShellExecute = true; + + try + { + StreamWriter BatchFileStream = new StreamWriter(BatchFile); + + //Cleanup previous + try + { + BatchFileStream.WriteLine("icacls\"" + fullPath + "\" /grant *S-1-1-0:(OI)(CI)F /T"); + } + finally + { + BatchFileStream.Close(); + } + + proc.Start(); + proc.WaitForExit(); + exitCode = proc.ExitCode; + proc.Close(); + File.Delete(BatchFile); + if (exitCode != 0) + { + logger.Error("Temporary batch process for granting folder write access returned error code: " + exitCode); + } + } + catch (Exception ex) + { + logger.Error(ex, ex.ToString()); + } + logger.Error("Successfully granted write permissions to " + fullPath); } + public static string CalculateSHA1(string text, Encoding enc) { byte[] buffer = enc.GetBytes(text);