aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core.Burn/Inscribe/InscribeBundleEngineCommand.cs
blob: 26af056b9c37c6a98b88d8b2fba63e2cc7442312 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// 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.

namespace WixToolset.Core.Burn.Inscribe
{
    using System;
    using System.IO;
    using WixToolset.Core.Burn.Bundles;
    using WixToolset.Extensibility;

    internal class InscribeBundleEngineCommand
    {
        public InscribeBundleEngineCommand(IInscribeContext context)
        {
            this.Context = context;
        }

        private IInscribeContext Context { get; }

        public bool Execute()
        {
            string tempFile = Path.Combine(this.Context.IntermediateFolder, "bundle_engine_unsigned.exe");

            using (BurnReader reader = BurnReader.Open(this.Context.InputFilePath))
            using (FileStream writer = File.Open(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read | FileShare.Delete))
            {
                reader.Stream.Seek(0, SeekOrigin.Begin);

                byte[] buffer = new byte[4 * 1024];
                int total = 0;
                int read = 0;
                do
                {
                    read = Math.Min(buffer.Length, (int)reader.EngineSize - total);

                    read = reader.Stream.Read(buffer, 0, read);
                    writer.Write(buffer, 0, read);

                    total += read;
                } while (total < reader.EngineSize && 0 < read);

                if (total != reader.EngineSize)
                {
                    throw new InvalidOperationException("Failed to copy engine out of bundle.");
                }

                // TODO: update writer with detached container signatures.
            }

            Directory.CreateDirectory(Path.GetDirectoryName(this.Context.OutputFile));
            if (File.Exists(this.Context.OutputFile))
            {
                File.Delete(this.Context.OutputFile);
            }

            File.Move(tempFile, this.Context.OutputFile);
            WixToolset.Core.Native.NativeMethods.ResetAcls(new string[] { this.Context.OutputFile }, 1);

            return true;
        }
    }
}