diff --git a/src/installer/managed/Microsoft.NET.HostModel/AppHost/FailedToDeleteApphostException.cs b/src/installer/managed/Microsoft.NET.HostModel/AppHost/FailedToDeleteApphostException.cs new file mode 100644 index 0000000000000000000000000000000000000000..c033a3953692972b82659670baf50e91db2e7dd1 --- /dev/null +++ b/src/installer/managed/Microsoft.NET.HostModel/AppHost/FailedToDeleteApphostException.cs @@ -0,0 +1,21 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; + +namespace Microsoft.NET.HostModel.AppHost +{ + /// + /// Failed to delete apphost when trying to delete incomplete appphost + /// + public class FailedToDeleteApphostException : AppHostUpdateException + { + public string ExceptionMessage { get; } + public FailedToDeleteApphostException(string exceptionMessage) + { + ExceptionMessage = exceptionMessage; + } + } +} + diff --git a/src/installer/managed/Microsoft.NET.HostModel/AppHost/HostWriter.cs b/src/installer/managed/Microsoft.NET.HostModel/AppHost/HostWriter.cs index 8d2a4fcf83433867fcd475919c5486e4f086b179..e3a9177fcbf238e2b3723789064f3aa760ae5198 100644 --- a/src/installer/managed/Microsoft.NET.HostModel/AppHost/HostWriter.cs +++ b/src/installer/managed/Microsoft.NET.HostModel/AppHost/HostWriter.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; +using System.Diagnostics; using System.IO; using System.IO.MemoryMappedFiles; using System.Text; @@ -43,45 +44,68 @@ public static class HostWriter BinaryUtils.CopyFile(appHostSourceFilePath, appHostDestinationFilePath); - // Re-write the destination apphost with the proper contents. - bool appHostIsPEImage = false; - using (var memoryMappedFile = MemoryMappedFile.CreateFromFile(appHostDestinationFilePath)) + try { - using (MemoryMappedViewAccessor accessor = memoryMappedFile.CreateViewAccessor()) + // Re-write the destination apphost with the proper contents. + bool appHostIsPEImage = false; + using (var memoryMappedFile = MemoryMappedFile.CreateFromFile(appHostDestinationFilePath)) { - BinaryUtils.SearchAndReplace(accessor, AppBinaryPathPlaceholderSearchValue, bytesToWrite); + using (MemoryMappedViewAccessor accessor = memoryMappedFile.CreateViewAccessor()) + { + BinaryUtils.SearchAndReplace(accessor, AppBinaryPathPlaceholderSearchValue, bytesToWrite); - appHostIsPEImage = BinaryUtils.IsPEImage(accessor); + appHostIsPEImage = BinaryUtils.IsPEImage(accessor); - if (windowsGraphicalUserInterface) - { - if (!appHostIsPEImage) + if (windowsGraphicalUserInterface) { - throw new AppHostNotPEFileException(); + if (!appHostIsPEImage) + { + throw new AppHostNotPEFileException(); + } + + BinaryUtils.SetWindowsGraphicalUserInterfaceBit(accessor); } + } + } - BinaryUtils.SetWindowsGraphicalUserInterfaceBit(accessor); + if (assemblyToCopyResorcesFrom != null && appHostIsPEImage) + { + if (ResourceUpdater.IsSupportedOS()) + { + // Copy resources from managed dll to the apphost + new ResourceUpdater(appHostDestinationFilePath) + .AddResourcesFromPEImage(assemblyToCopyResorcesFrom) + .Update(); + } + else + { + throw new AppHostCustomizationUnsupportedOSException(); } } - } - if (assemblyToCopyResorcesFrom != null && appHostIsPEImage) + // Memory-mapped write does not updating last write time + File.SetLastWriteTimeUtc(appHostDestinationFilePath, DateTime.UtcNow); + } + catch (Exception ex) { - if (ResourceUpdater.IsSupportedOS()) + FailedToDeleteApphostException failedToDeleteApphostException = null; + // Delete the destination file so we don't leave an unmodified apphost + try { - // Copy resources from managed dll to the apphost - new ResourceUpdater(appHostDestinationFilePath) - .AddResourcesFromPEImage(assemblyToCopyResorcesFrom) - .Update(); + File.Delete(appHostDestinationFilePath); } - else + catch (Exception failedToDeleteEx) when (failedToDeleteEx is IOException || failedToDeleteEx is UnauthorizedAccessException) { - throw new AppHostCustomizationUnsupportedOSException(); + failedToDeleteApphostException = new FailedToDeleteApphostException(failedToDeleteEx.Message); } - } - // Memory-mapped write does not updating last write time - File.SetLastWriteTimeUtc(appHostDestinationFilePath, DateTime.UtcNow); + if (failedToDeleteApphostException != null) + { + throw new AggregateException(ex, failedToDeleteApphostException); + } + + throw; + } } ///