Skip to content

Commit

Permalink
Delete output apphost upon failure of the CreateAppHost task.
Browse files Browse the repository at this point in the history
This commit deletes the output apphost when the `CreateAppHost` task fails from
an exception.

Partially fixes #2989.
  • Loading branch information
Peter Huene committed Jul 16, 2019
1 parent 74b343a commit 7eaa9b6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public void ItFailsToEmbedAppBinaryIfHashIsWrong()
.Contain(sourceAppHostMock)
.And
.Contain(AppBinaryPathPlaceholder);

File.Exists(destinationFilePath).Should().BeFalse();
}
}

Expand All @@ -88,6 +90,8 @@ public void ItFailsToEmbedTooLongAppBinaryPath()
.Message
.Should()
.Contain(appBinaryFilePath);

File.Exists(destinationFilePath).Should().BeFalse();
}
}

Expand Down Expand Up @@ -161,6 +165,8 @@ public void ItFailsToSetGUISubsystemWithWrongDefault()
.Message
.Should()
.Contain(sourceAppHostMock);

File.Exists(destinationFilePath).Should().BeFalse();
}
}

Expand Down
63 changes: 36 additions & 27 deletions src/Tasks/Microsoft.NET.Build.Tasks/AppHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,45 +52,54 @@ public static void Create(
// Copy apphost to destination path so it inherits the same attributes/permissions.
File.Copy(appHostSourceFilePath, appHostDestinationFilePath, overwrite: true);

// 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))
{
SearchAndReplace(accessor, AppBinaryPathPlaceholderSearchValue, bytesToWrite, appHostSourceFilePath);
using (MemoryMappedViewAccessor accessor = memoryMappedFile.CreateViewAccessor())
{
SearchAndReplace(accessor, AppBinaryPathPlaceholderSearchValue, bytesToWrite, appHostSourceFilePath);

appHostIsPEImage = IsPEImage(accessor);
appHostIsPEImage = IsPEImage(accessor);

if (windowsGraphicalUserInterface)
{
if (!appHostIsPEImage)
if (windowsGraphicalUserInterface)
{
throw new BuildErrorException(Strings.AppHostNotWindows, appHostSourceFilePath);
}
if (!appHostIsPEImage)
{
throw new BuildErrorException(Strings.AppHostNotWindows, appHostSourceFilePath);
}

SetWindowsGraphicalUserInterfaceBit(accessor, appHostSourceFilePath);
SetWindowsGraphicalUserInterfaceBit(accessor, appHostSourceFilePath);
}
}
}
}

if (intermediateAssembly != null && appHostIsPEImage)
{
if (ResourceUpdater.IsSupportedOS())
{
// Copy resources from managed dll to the apphost
new ResourceUpdater(appHostDestinationFilePath)
.AddResourcesFromPEImage(intermediateAssembly)
.Update();
}
else if (log != null)
if (intermediateAssembly != null && appHostIsPEImage)
{
log.LogWarning(Strings.AppHostCustomizationRequiresWindowsHostWarning);
if (ResourceUpdater.IsSupportedOS())
{
// Copy resources from managed dll to the apphost
new ResourceUpdater(appHostDestinationFilePath)
.AddResourcesFromPEImage(intermediateAssembly)
.Update();
}
else if (log != null)
{
log.LogWarning(Strings.AppHostCustomizationRequiresWindowsHostWarning);
}
}
}

// Memory-mapped write does not updating last write time
File.SetLastWriteTimeUtc(appHostDestinationFilePath, DateTime.UtcNow);
// Memory-mapped write does not updating last write time
File.SetLastWriteTimeUtc(appHostDestinationFilePath, DateTime.UtcNow);
}
catch (Exception)
{
// Delete the destination file so we don't leave an unmodified apphost
File.Delete(appHostDestinationFilePath);
throw;
}
}

// See: https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm
Expand Down

0 comments on commit 7eaa9b6

Please sign in to comment.