-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathComparer.cs
More file actions
201 lines (161 loc) · 6.72 KB
/
Comparer.cs
File metadata and controls
201 lines (161 loc) · 6.72 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
namespace WindowsBuildIdentifier
{
public class Comparer
{
public static void CompareBuilds(string xml1, string xml2)
{
Console.WriteLine("Reading 1");
FileItem[] build1 = Deserialize(xml1);
Console.WriteLine("Reading 2");
FileItem[] build2 = Deserialize(xml2);
Console.WriteLine("Getting paths 1");
HashSet<string> build1Paths = build1.Select(x => Sanitize(x.Location)).Where(x => !ExcludedFromChecks(x)).ToHashSet();
Console.WriteLine("Getting paths 2");
HashSet<string> build2Paths = build2.Select(x => Sanitize(x.Location)).Where(x => !ExcludedFromChecks(x)).ToHashSet();
Console.WriteLine("Getting common paths");
HashSet<string> commonPaths = build1Paths.Intersect(build2Paths).ToHashSet();
Console.WriteLine("Getting unique paths 1");
HashSet<string> uniqueBuild1 = build1Paths.Where(x => !commonPaths.Contains(x)).ToHashSet();
Console.WriteLine("Getting unique paths 2");
HashSet<string> uniqueBuild2 = build2Paths.Where(x => !commonPaths.Contains(x)).ToHashSet();
Console.WriteLine("Getting common resources");
HashSet<string> commonResources = commonPaths.Where(x => x.Contains(@"\.rsrc\")).ToHashSet();
Console.WriteLine($"{commonPaths.Count} common paths");
Console.WriteLine($"{commonResources.Count} common resources");
Console.WriteLine($"{uniqueBuild1.Count} unique 1");
Console.WriteLine($"{uniqueBuild2.Count} unique 2");
Console.WriteLine("Processing common resources");
HashSet<string> modifiedResources = new();
long total = commonResources.Count;
long counter = 0;
foreach (string resource in commonResources)
{
Console.Title = $"Processing {counter}/{total}";
FileItem item1 = GetFileItem(build1, resource);
FileItem item2 = GetFileItem(build2, resource);
if (item1.Hash != null)
{
if (item1.Hash.Sha1 != item2.Hash.Sha1)
{
Console.WriteLine("modified");
modifiedResources.Add(item1.Location);
}
}
counter++;
}
Console.WriteLine();
Console.WriteLine("Files unique to build 1");
Console.WriteLine();
foreach (string file in uniqueBuild1)
{
Console.WriteLine(file);
}
Console.WriteLine();
Console.WriteLine("Files unique to build 2");
Console.WriteLine();
foreach (string file in uniqueBuild2)
{
Console.WriteLine(file);
}
Console.WriteLine();
Console.WriteLine("Modified resources between both builds");
Console.WriteLine();
foreach (string file in modifiedResources)
{
Console.WriteLine(file);
}
}
private static string Sanitize(string path1)
{
if (path1.Contains(@"installedrepository\", StringComparison.OrdinalIgnoreCase))
{
string fold1T = path1.ToLower().Split(@"installedrepository\")[0];
string fold1 = path1.ToLower().Split(@"installedrepository\")[1];
int number = fold1.Count(x => x == '_');
if (number < 1)
{
return path1;
}
string[] fsplit1 = fold1.Split(@"\");
string san1 = string.Join("_", fsplit1[0].Split("_")[..^1]);
if (fsplit1.Length < 2)
{
return san1;
}
string f1 = fold1T + "installedrepository\\" + san1 + "\\" + string.Join("\\", fsplit1[1..]);
return f1;
}
if (path1.Contains(@"build\filerepository\", StringComparison.OrdinalIgnoreCase))
{
string fold1T = path1.ToLower().Split(@"build\filerepository\")[0];
string fold1 = path1.ToLower().Split(@"build\filerepository\")[1];
int number = fold1.Count(x => x == '_');
if (number < 1)
{
return path1;
}
string[] fsplit1 = fold1.Split(@"\");
string san1 = string.Join("_", fsplit1[0].Split("_")[..^1]);
if (fsplit1.Length < 2)
{
return san1;
}
string f1 = fold1T + "build\\filerepository\\" + san1 + "\\" + string.Join("\\", fsplit1[1..]);
return f1;
}
if (path1.Contains(@"Windows\winsxs\", StringComparison.OrdinalIgnoreCase))
{
string fold1T = path1.ToLower().Split(@"windows\winsxs\")[0];
string fold1 = path1.ToLower().Split(@"windows\winsxs\")[1];
int number = fold1.Count(x => x == '_');
if (number < 1)
{
return path1;
}
string[] fsplit1 = fold1.Split(@"\");
string san1 = string.Join("_", fsplit1[0].Split("_")[..^1]);
if (fsplit1.Length < 2)
{
return san1;
}
string f1 = fold1T + "windows\\winsxs\\" + san1 + "\\" + string.Join("\\", fsplit1[1..]);
return f1;
}
return path1;
}
private static FileItem GetFileItem(FileItem[] array, string path)
{
return array.First(x => Sanitize(x.Location) == path);
}
private static bool ExcludedFromChecks(string path)
{
string index = @"install.wim\3\";
if (!path.Contains(index, StringComparison.OrdinalIgnoreCase))
{
return true;
}
if (path.Contains(@"\.rsrc\"))
{
if (path.EndsWith("version.txt"))
{
return true;
}
}
return false;
}
private static FileItem[] Deserialize(string path)
{
XmlSerializer deserializer = new(typeof(FileItem[]));
TextReader reader = new StreamReader(path);
object obj = deserializer.Deserialize(reader);
FileItem[] xmlData = (FileItem[])obj;
reader.Close();
return xmlData;
}
}
}