Skip to content

Commit

Permalink
Fix possible InvalidOperationException and IOException in the update …
Browse files Browse the repository at this point in the history
…installer
  • Loading branch information
Dominic Beger committed Mar 13, 2018
1 parent cd905a0 commit 720e563
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 34 deletions.
15 changes: 15 additions & 0 deletions nUpdate.Internal/Core/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.IO;

namespace nUpdate.Internal.Core
{
internal static class Extensions
{
public static void Empty(this DirectoryInfo directory)
{
foreach (var file in directory.GetFiles())
file.Delete();
foreach (var subDirectory in directory.GetDirectories())
subDirectory.Delete(true);
}
}
}
1 change: 1 addition & 0 deletions nUpdate.Internal/nUpdate.Internal.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<ItemGroup>
<Compile Include="Core\Architecture.cs" />
<Compile Include="Core\DevelopmentalStage.cs" />
<Compile Include="Core\Extensions.cs" />
<Compile Include="Core\IconHelper.cs" />
<Compile Include="Core\SizeHelper.cs" />
<Compile Include="Core\IDeepCopy.cs" />
Expand Down
12 changes: 6 additions & 6 deletions nUpdate.Shared/Updating/UpdateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ namespace nUpdate.Updating
/// Provides functionality to update .NET-applications.
/// </summary>
public partial class UpdateManager : IDisposable

{
private readonly string _applicationUpdateDirectory = Path.Combine(Path.GetTempPath(), "nUpdate",
Application.ProductName);
Expand Down Expand Up @@ -58,7 +57,7 @@ public partial class UpdateManager : IDisposable
/// <remarks>
/// The public key can be found in the overview of your project when you're opening it in nUpdate Administration.
/// If you have problems inserting the data (or if you want to save time) you can scroll down there and follow the
/// steps of the category "Copy data" which will automatically generate the necessray code for you.
/// steps of the category "Copy data" which will automatically generate the necessary code for you.
/// </remarks>
public UpdateManager(Uri updateConfigurationFileUri, string publicKey,
CultureInfo languageCulture = null, UpdateVersion currentVersion = null)
Expand Down Expand Up @@ -309,12 +308,13 @@ public void DeletePackages()

private void Initialize()
{
if (Directory.Exists(Path.Combine(Path.GetTempPath(), "nUpdate")))
return;

try
{
Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "nUpdate", Application.ProductName));
var updateDirDirectoryInfo = new DirectoryInfo(_applicationUpdateDirectory);
if (updateDirDirectoryInfo.Exists)
updateDirDirectoryInfo.Empty();
else
updateDirDirectoryInfo.Create();
}
catch (Exception ex)
{
Expand Down
38 changes: 10 additions & 28 deletions nUpdate.UpdateInstaller/Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ public void RunUpdate()
}
catch (Exception ex)
{
Popup.ShowPopup(SystemIcons.Error, "Error while initializing the graphic user interface.", ex,
Popup.ShowPopup(SystemIcons.Error, "Error while initializing the graphical user interface.", ex,
PopupButtons.Ok);
return;
}

ThreadPool.QueueUserWorkItem(arg => RunUpdateAsync());
try
{
_progressReporter.Initialize();
Expand All @@ -51,21 +50,23 @@ public void RunUpdate()
_progressReporter.InitializingFail(ex);
_progressReporter.Terminate();
}

ThreadPool.QueueUserWorkItem(arg => RunUpdateAsync());
}

/// <summary>
/// Loads the GUI either from a given external assembly, if one is set, or otherwise, from the integrated GUI.
/// Loads the GUI either from a given external assembly, if one is set, or otherwise, uses the integrated GUI.
/// </summary>
/// <returns>
/// Returns a new instance of the given object that implements the
/// <see cref="nUpdate.UpdateInstaller.Client.GuiInterface.IProgressReporter" />-interface.
/// <see cref="IProgressReporter" />-interface.
/// </returns>
private IProgressReporter GetProgressReporter()
{
Assembly assembly = (string.IsNullOrEmpty(Program.ExternalGuiAssemblyPath) ||
!File.Exists(Program.ExternalGuiAssemblyPath)
Assembly assembly = string.IsNullOrEmpty(Program.ExternalGuiAssemblyPath) ||
!File.Exists(Program.ExternalGuiAssemblyPath)
? Assembly.GetExecutingAssembly()
: Assembly.LoadFrom(Program.ExternalGuiAssemblyPath));
: Assembly.LoadFrom(Program.ExternalGuiAssemblyPath);
IServiceProvider provider = ServiceProviderHelper.CreateServiceProvider(assembly);
return (IProgressReporter) provider.GetService(typeof (IProgressReporter));
}
Expand Down Expand Up @@ -96,7 +97,6 @@ private void RunUpdateAsync()
catch (Exception ex)
{
_progressReporter.Fail(ex);
CleanUp();
_progressReporter.Terminate();
if (!Program.IsHostApplicationClosed || !Program.RestartHostApplication)
return;
Expand Down Expand Up @@ -418,7 +418,6 @@ private void RunUpdateAsync()
catch (Exception ex)
{
_progressReporter.Fail(ex);
CleanUp();
_progressReporter.Terminate();
if (!Program.IsHostApplicationClosed || !Program.RestartHostApplication)
return;
Expand All @@ -441,8 +440,7 @@ private void RunUpdateAsync()
return;
}
}

CleanUp();

if (Program.IsHostApplicationClosed && Program.RestartHostApplication)
{
var p = new Process
Expand All @@ -463,22 +461,7 @@ private void RunUpdateAsync()
}
_progressReporter.Terminate();
}

/// <summary>
/// Cleans up all resources.
/// </summary>
private void CleanUp()
{
try
{
Directory.Delete(Directory.GetParent(Program.PackageFilePaths.First()).FullName, true);
}
catch (Exception ex)
{
_progressReporter.Fail(ex);
}
}


/// <summary>
/// Performs a recursive copy of a given directory.
/// </summary>
Expand Down Expand Up @@ -529,7 +512,6 @@ private void CopyDirectoryRecursively(string sourceDirName, string destDirName)
catch (Exception ex)
{
_progressReporter.Fail(ex);
CleanUp();
_progressReporter.Terminate();
if (!Program.IsHostApplicationClosed)
return;
Expand Down

0 comments on commit 720e563

Please sign in to comment.