提交 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 @@
// 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,6 +44,8 @@ public static class HostWriter
BinaryUtils.CopyFile(appHostSourceFilePath, appHostDestinationFilePath);
try
{
// Re-write the destination apphost with the proper contents.
bool appHostIsPEImage = false;
using (var memoryMappedFile = MemoryMappedFile.CreateFromFile(appHostDestinationFilePath))
......@@ -83,6 +86,27 @@ public static class HostWriter
// Memory-mapped write does not updating last write time
File.SetLastWriteTimeUtc(appHostDestinationFilePath, DateTime.UtcNow);
}
catch (Exception ex)
{
FailedToDeleteApphostException failedToDeleteApphostException = null;
// Delete the destination file so we don't leave an unmodified apphost
try
{
File.Delete(appHostDestinationFilePath);
}
catch (Exception failedToDeleteEx) when (failedToDeleteEx is IOException || failedToDeleteEx is UnauthorizedAccessException)
{
failedToDeleteApphostException = new FailedToDeleteApphostException(failedToDeleteEx.Message);
}
if (failedToDeleteApphostException != null)
{
throw new AggregateException(ex, failedToDeleteApphostException);
}
throw;
}
}
/// <summary>
/// Set the current AppHost as a single-file bundle.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册