Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 18 additions & 21 deletions src/installer/managed/Microsoft.NET.HostModel/ResourceUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ namespace Microsoft.NET.HostModel
/// </summary>
public sealed class ResourceUpdater : IDisposable
{
private readonly FileStream stream;
private readonly FileStream _stream;
private readonly PEReader _reader;
private ResourceData _resourceData;
private readonly bool leaveOpen;
private readonly bool _leaveOpen;

/// <summary>
/// Create a resource updater for the given PE file.
Expand All @@ -39,18 +39,24 @@ public ResourceUpdater(string peFile)
/// </summary>
public ResourceUpdater(FileStream stream, bool leaveOpen = false)
{
this.stream = stream;
this.leaveOpen = leaveOpen;
#if NET
ArgumentNullException.ThrowIfNull(stream);
#endif

_stream = stream;
_leaveOpen = leaveOpen;
try
{
this.stream.Seek(0, SeekOrigin.Begin);
_reader = new PEReader(this.stream, PEStreamOptions.LeaveOpen);
_stream.Seek(0, SeekOrigin.Begin);
_reader = new PEReader(_stream, PEStreamOptions.LeaveOpen);
_resourceData = new ResourceData(_reader);
}
catch (Exception)
{
if (!leaveOpen)
this.stream?.Dispose();
{
_stream.Dispose();
}
throw;
}
}
Expand Down Expand Up @@ -185,12 +191,12 @@ public void Update()

int trailingSectionVirtualStart = rsrcVirtualAddress + rsrcOriginalVirtualSize;
int trailingSectionStart = rsrcPointerToRawData + rsrcOriginalRawDataSize;
int trailingSectionLength = (int)(stream.Length - trailingSectionStart);
int trailingSectionLength = (int)(_stream.Length - trailingSectionStart);

bool needsMoveTrailingSections = !isRsrcIsLastSection && delta > 0;
long finalImageSize = trailingSectionStart + Math.Max(delta, 0) + trailingSectionLength;

using (var mmap = MemoryMappedFile.CreateFromFile(stream, null, finalImageSize, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, true))
using (var mmap = MemoryMappedFile.CreateFromFile(_stream, null, finalImageSize, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, true))
using (MemoryMappedViewAccessor accessor = mmap.CreateViewAccessor(0, finalImageSize, MemoryMappedFileAccess.ReadWrite))
{
int peSignatureOffset = ReadI32(accessor, PEOffsets.DosStub.PESignatureOffset);
Expand Down Expand Up @@ -327,19 +333,10 @@ private static void ThrowExceptionForInvalidUpdate()

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

private void Dispose(bool disposing)
{
if (disposing)
_reader.Dispose();
if (!_leaveOpen)
{
_reader.Dispose();
if (!leaveOpen)
{
stream.Dispose();
}
_stream.Dispose();
}
}
}
Expand Down