提交 1f519998 编写于 作者: W William Li

Delete output apphost upon failure of the CreateAppHost task.

Port https://github.com/dotnet/sdk/pull/3427 by @peterhuene with my edit

This commit deletes the output apphost when the CreateAppHost task fails from
an exception.

Partially fixes dotnet/core-setup#2989

-------------
* deletes the output apphost when the `CreateAppHost` task fails from
an exception.

* aggreates the Exception for future logging from deleting the failed apphost


Commit migrated from https://github.com/dotnet/core-setup/commit/7f0eb2869f2f37a899cdd02b7bbc4bd78e4e7038
上级 6cc33d03
// 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
{
/// <summary>
/// Failed to delete apphost when trying to delete incomplete appphost
/// </summary>
public class FailedToDeleteApphostException : AppHostUpdateException
{
public string ExceptionMessage { get; }
public FailedToDeleteApphostException(string exceptionMessage)
{
ExceptionMessage = exceptionMessage;
}
}
}
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System; using System;
using System.Diagnostics;
using System.IO; using System.IO;
using System.IO.MemoryMappedFiles; using System.IO.MemoryMappedFiles;
using System.Text; using System.Text;
...@@ -43,45 +44,68 @@ public static class HostWriter ...@@ -43,45 +44,68 @@ public static class HostWriter
BinaryUtils.CopyFile(appHostSourceFilePath, appHostDestinationFilePath); BinaryUtils.CopyFile(appHostSourceFilePath, appHostDestinationFilePath);
// Re-write the destination apphost with the proper contents. try
bool appHostIsPEImage = false;
using (var memoryMappedFile = MemoryMappedFile.CreateFromFile(appHostDestinationFilePath))
{ {
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 (windowsGraphicalUserInterface)
{
if (!appHostIsPEImage)
{ {
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 File.Delete(appHostDestinationFilePath);
new ResourceUpdater(appHostDestinationFilePath)
.AddResourcesFromPEImage(assemblyToCopyResorcesFrom)
.Update();
} }
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 if (failedToDeleteApphostException != null)
File.SetLastWriteTimeUtc(appHostDestinationFilePath, DateTime.UtcNow); {
throw new AggregateException(ex, failedToDeleteApphostException);
}
throw;
}
} }
/// <summary> /// <summary>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册