From 0d33f4ead18e05ff71f309a24950bbe1222009fb Mon Sep 17 00:00:00 2001 From: DanielSvoboda Date: Fri, 26 May 2023 18:07:09 -0300 Subject: [PATCH] Updater Fix/Improvements (#606) * Updater Fix/Improvements In SetAccessRule(): A call to the Directory.SetAccessControl method was added to apply the changes made to the DirectorySecurity object. Additionally, the function does not handle possible exceptions that may occur during code execution. This version of the function attempts to set the access rule for the specified directory and, if an error occurs during the process, prints an error message on the screen. This helps to identify and fix possible problems that may occur during code execution. In Install(): The function checks if the destination directory exists before attempting to delete it. Additionally, a destDir variable was created to store the path of the 'destination directory' and a destFile variable to store the path of the 'destination file'. This makes the code a little more readable. In Application_Idle(): A MessageBox.Show was included to display a dialog box with the message "Updates are available. Do you want to update now?" and two buttons: "Yes" and "No". If the user clicks on the "Yes" button, the UpdateNotifcationClick method is called to start the update process. Otherwise, the dialog box is closed and no action is taken. The dialog box will only be shown once for each available update, as the UsePrompt variable is set to false after the dialog box is shown for the first time. when trying to update, updater.exe crashed and presented the following error, as shown in the print below: unhandled exception: Unable to create a file that already exists. * Updater Fix/Improvements --- Toolbox/MainForm.cs | 9 +++++++++ Updater/Program.cs | 35 ++++++++++++++++++++++++----------- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/Toolbox/MainForm.cs b/Toolbox/MainForm.cs index 53078713..1a9d8dad 100644 --- a/Toolbox/MainForm.cs +++ b/Toolbox/MainForm.cs @@ -188,9 +188,18 @@ namespace Toolbox UpdateProgram.CommitList.Count > 0) { updateToolstrip.Enabled = true; + UsePrompt = false; + + // Shows an on-screen message that updates are available + DialogResult result = MessageBox.Show("Updates are available. Do you want to update now?", "Update Available", MessageBoxButtons.YesNo); + if (result == DialogResult.Yes) + { + UpdateNotifcationClick(); + } } } + private void UpdateNotifcationClick() { if (UpdateProgram.CommitList.Count <= 0) diff --git a/Updater/Program.cs b/Updater/Program.cs index 996dd2e8..1723fa43 100644 --- a/Updater/Program.cs +++ b/Updater/Program.cs @@ -89,14 +89,18 @@ namespace Updater SetAccessRule(dir); string dirName = new DirectoryInfo(dir).Name; + string destDir = Path.Combine(folderDir, dirName + @"\"); if (!dirName.Equals("Hashes", StringComparison.CurrentCultureIgnoreCase) // Let's keep the users custom hashes in tact - && Directory.Exists(Path.Combine(folderDir, dirName + @"\"))) + && Directory.Exists(destDir)) { - Directory.Delete(Path.Combine(folderDir, dirName + @"\"), true); + Directory.Delete(destDir, true); } - Directory.Move(dir, Path.Combine(folderDir, dirName + @"\")); + if (Directory.Exists(destDir)) + Directory.Delete(destDir, true); + + Directory.Move(dir, destDir); } foreach (string file in Directory.GetFiles("master/")) { @@ -107,21 +111,30 @@ namespace Updater SetAccessRule(file); SetAccessRule(folderDir); - if (File.Exists(Path.Combine(folderDir, Path.GetFileName(file)))) - { - File.Delete(Path.Combine(folderDir, Path.GetFileName(file))); - } - File.Move(file, Path.Combine(folderDir, Path.GetFileName(file))); + string destFile = Path.Combine(folderDir, Path.GetFileName(file)); + if (File.Exists(destFile)) + File.Delete(destFile); + + File.Move(file, destFile); } } static void SetAccessRule(string directory) { - System.Security.AccessControl.DirectorySecurity sec = System.IO.Directory.GetAccessControl(directory); - FileSystemAccessRule accRule = new FileSystemAccessRule(Environment.UserDomainName + "\\" + Environment.UserName, FileSystemRights.FullControl, AccessControlType.Allow); - sec.AddAccessRule(accRule); + try + { + System.Security.AccessControl.DirectorySecurity sec = System.IO.Directory.GetAccessControl(directory); + FileSystemAccessRule accRule = new FileSystemAccessRule(Environment.UserDomainName + "\\" + Environment.UserName, FileSystemRights.FullControl, AccessControlType.Allow); + sec.AddAccessRule(accRule); + Directory.SetAccessControl(directory, sec); + } + catch (Exception ex) + { + Console.WriteLine($"Failed to set access rule for directory '{directory}': {ex.Message}"); + } } + static void Download(string CompileDate) { foreach (Release latest in releases)