aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core.WindowsInstaller/Bind/FileSystemManager.cs
blob: fe65cceff03085219f199a9e61e91aa05c28c28c (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// 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.WindowsInstaller.Bind
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using WixToolset.Extensibility;

    internal class FileSystemManager
    {
        public FileSystemManager(IEnumerable<IFileSystemExtension> fileSystemExtensions)
        {
            this.Extensions = fileSystemExtensions;
        }

        private IEnumerable<IFileSystemExtension> Extensions { get; }

        public bool CompareFiles(string firstPath, string secondPath)
        {
            foreach (var extension in this.Extensions)
            {
                var compared = extension.CompareFiles(firstPath, secondPath);
                if (compared.HasValue)
                {
                    return compared.Value;
                }
            }

            return BuiltinCompareFiles(firstPath, secondPath);
        }

        private static bool BuiltinCompareFiles(string firstPath, string secondPath)
        {
            if (String.Equals(firstPath, secondPath, StringComparison.OrdinalIgnoreCase))
            {
                return true;
            }

            using (var firstStream = File.OpenRead(firstPath))
            using (var secondStream = File.OpenRead(secondPath))
            {
                if (firstStream.Length != secondStream.Length)
                {
                    return false;
                }

                // Using a larger buffer than the default buffer of 4 * 1024 used by FileStream.ReadByte improves performance.
                // The buffer size is based on user feedback. Based on performance results, a better buffer size may be determined.
                var firstBuffer = new byte[16 * 1024];
                var secondBuffer = new byte[16 * 1024];

                var firstReadLength = 0;
                do
                {
                    firstReadLength = firstStream.Read(firstBuffer, 0, firstBuffer.Length);
                    var secondReadLength = secondStream.Read(secondBuffer, 0, secondBuffer.Length);

                    if (firstReadLength != secondReadLength)
                    {
                        return false;
                    }

                    for (var i = 0; i < firstReadLength; ++i)
                    {
                        if (firstBuffer[i] != secondBuffer[i])
                        {
                            return false;
                        }
                    }
                } while (0 < firstReadLength);
            }

            return true;
        }
    }
}