| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | namespace WixToolset.Core.Burn.Inscribe |
| 4 | { |
| 5 | using System; |
| 6 | using System.IO; |
| 7 | using WixToolset.Core.Burn.Bundles; |
| 8 | using WixToolset.Extensibility.Services; |
| 9 | |
| 10 | internal class InscribeBundleEngineCommand |
| 11 | { |
| 12 | public InscribeBundleEngineCommand(IServiceProvider serviceProvider, string inputPath, string outputPath, string intermediateFolder) |
| 13 | { |
| 14 | this.Messaging = serviceProvider.GetService<IMessaging>(); |
| 15 | this.FileSystem = serviceProvider.GetService<IFileSystem>(); |
| 16 | this.IntermediateFolder = intermediateFolder; |
| 17 | this.InputFilePath = inputPath; |
| 18 | this.OutputFile = outputPath; |
| 19 | } |
| 20 | |
| 21 | private IMessaging Messaging { get; } |
| 22 | |
| 23 | private IFileSystem FileSystem { get; } |
| 24 | |
| 25 | private string IntermediateFolder { get; } |
| 26 | |
| 27 | private string InputFilePath { get; } |
| 28 | |
| 29 | private string OutputFile { get; } |
| 30 | |
| 31 | public void Execute() |
| 32 | { |
| 33 | var tempFile = Path.Combine(this.IntermediateFolder, "bundle_engine_unsigned.exe"); |
| 34 | |
| 35 | using (var reader = BurnReader.Open(this.Messaging, this.FileSystem, this.InputFilePath)) |
| 36 | using (var writer = this.FileSystem.OpenFile(null, tempFile, FileMode.Create, FileAccess.Write, FileShare.Read | FileShare.Delete)) |
| 37 | { |
| 38 | reader.Stream.Seek(0, SeekOrigin.Begin); |
| 39 | |
| 40 | var buffer = new byte[8 * 1024]; |
| 41 | var total = 0; |
| 42 | var read = 0; |
| 43 | do |
| 44 | { |
| 45 | read = Math.Min(buffer.Length, (int)reader.EngineSize - total); |
| 46 | |
| 47 | read = reader.Stream.Read(buffer, 0, read); |
| 48 | writer.Write(buffer, 0, read); |
| 49 | |
| 50 | total += read; |
| 51 | } while (total < reader.EngineSize && 0 < read); |
| 52 | |
| 53 | if (total != reader.EngineSize) |
| 54 | { |
| 55 | throw new InvalidOperationException("Failed to copy engine out of bundle."); |
| 56 | } |
| 57 | |
| 58 | // TODO: update writer with detached container signatures. |
| 59 | } |
| 60 | |
| 61 | this.FileSystem.MoveFile(null, tempFile, this.OutputFile); |
| 62 | } |
| 63 | } |
| 64 | } |