Repository navigation
Expand file tree
/
Copy pathDriverStoreRepository.cs
More file actions
158 lines (134 loc) · 5.52 KB
/
Copy pathDriverStoreRepository.cs
File metadata and controls
158 lines (134 loc) · 5.52 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
namespace Rapr.Utils
{
public class DriverStoreContent
{
public string InfName { get; set; }
public string FolderPath { get; set; }
public string Content { get; set; }
public long EstimateSize { get; set; }
}
public class DriverStoreRepository
{
private static readonly Regex DriverStoreRepositoryDirNameRegex = new Regex(@"^(.+\.inf)_.+$", RegexOptions.Compiled);
private static readonly string SystemRoot = Environment.ExpandEnvironmentVariables("%SystemRoot%");
private static readonly string SystemRootInf = Path.Combine(SystemRoot, "INF");
private static readonly string DriverStoreFileRepository = Path.Combine(SystemRoot, @"system32\DriverStore\FileRepository");
private readonly DirectoryInfo driverStoreFileRepository = new DirectoryInfo(DriverStoreFileRepository);
private readonly List<DriverStoreContent> driverStoreContents = new List<DriverStoreContent>();
private readonly IEnumerator<DirectoryInfo> directoryInfoEnumerator;
public DriverStoreRepository()
{
this.directoryInfoEnumerator = this.driverStoreFileRepository.EnumerateDirectories().GetEnumerator();
}
public void FindInfInfo(string infName, out string originalInfName, out string driverFolderLocation, out long estimateSize)
{
originalInfName = "[Unknown]";
driverFolderLocation = string.Empty;
estimateSize = -1;
try
{
if (!string.IsNullOrEmpty(infName))
{
string content = GetSystemRootInfContent(infName);
if (!string.IsNullOrEmpty(content))
{
DriverStoreContent driverStoreContent = this.FindInfInfo(content);
if (driverStoreContent != null)
{
originalInfName = driverStoreContent.InfName;
driverFolderLocation = driverStoreContent.FolderPath;
estimateSize = driverStoreContent.EstimateSize;
}
}
}
}
catch (UnauthorizedAccessException)
{
// Ignore this entry if we don't have access to it.
}
}
private DriverStoreContent FindInfInfo(string content)
{
for (int i = 0; i < this.driverStoreContents.Count; i++)
{
if (this.driverStoreContents[i].Content == content)
{
DriverStoreContent driverStoreContent = this.driverStoreContents[i];
this.driverStoreContents.RemoveAt(i);
return driverStoreContent;
}
}
while (this.directoryInfoEnumerator.MoveNext())
{
var item = this.directoryInfoEnumerator.Current;
Match match = DriverStoreRepositoryDirNameRegex.Match(item.Name);
if (match.Success)
{
string infName = match.Groups[1].Value;
string infPath = Path.Combine(item.FullName, infName);
if (File.Exists(infPath))
{
try
{
string infContent = File.ReadAllText(infPath);
long estimateSize = GetFolderSize(item);
DriverStoreContent driverStoreContent = new DriverStoreContent()
{
InfName = infName,
FolderPath = item.FullName,
Content = infContent,
EstimateSize = estimateSize
};
if (infContent == content)
{
return driverStoreContent;
}
else
{
this.driverStoreContents.Add(driverStoreContent);
}
}
catch (UnauthorizedAccessException)
{
// Ignore this entry if we don't have access to it.
}
}
}
}
return null;
}
private static string GetSystemRootInfContent(string infName)
{
string content = null;
string filePath = Path.Combine(SystemRootInf, infName);
if (File.Exists(filePath))
{
content = File.ReadAllText(filePath);
}
return content;
}
public static long GetFolderSize(DirectoryInfo directory)
{
long size = 0;
if (directory == null)
{
throw new ArgumentNullException(nameof(directory));
}
// Add file sizes.
foreach (FileInfo fileInfo in directory.GetFiles())
{
size += fileInfo.Length;
}
// Add subdirectory sizes.
foreach (DirectoryInfo dirInfo in directory.GetDirectories())
{
size += GetFolderSize(dirInfo);
}
return size;
}
}
}