Skip to content

Commit a6d0679

Browse files
committed
驱动更新实现
1 parent 2566e73 commit a6d0679

File tree

18 files changed

+232
-112
lines changed

18 files changed

+232
-112
lines changed

src/c#/GeneralUpdate.Bowl/Bowl.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace GeneralUpdate.Bowl;
77
/// <summary>
88
/// Surveillance Main Program.
99
/// </summary>
10-
public class Bowl
10+
public sealed class Bowl
1111
{
1212
private IStrategy _strategy;
1313

@@ -27,6 +27,9 @@ private void CreateStrategy()
2727
{
2828
_strategy = new LinuxStrategy();
2929
}
30+
31+
if (_strategy == null)
32+
throw new PlatformNotSupportedException("Unsupported operating system");
3033
}
3134

3235
public Bowl SetParameter(MonitorParameter parameter)

src/c#/GeneralUpdate.Bowl/Strategys/AbstractStrategy.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ public abstract class AbstractStrategy : IStrategy
1111
private readonly IReadOnlyList<string> _sensitiveCharacter = new List<string>
1212
{
1313
"Exit",
14-
"exit"
14+
"exit",
15+
"EXIT"
1516
};
1617

1718
public virtual void Launch()

src/c#/GeneralUpdate.Bowl/Strategys/LinuxStrategy.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ private void Install()
6666

6767
private string GetPacketName()
6868
{
69-
string packageFileName = string.Empty;
70-
LinuxSystem system = GetSystem();
69+
var packageFileName = string.Empty;
70+
var system = GetSystem();
7171
if (_rocdumpAmd64.Contains(system.Name))
7272
{
7373
packageFileName = $"procdump_3.3.0_amd64.deb";
@@ -107,9 +107,7 @@ private LinuxSystem GetSystem()
107107

108108
return new LinuxSystem(distro, version);
109109
}
110-
else
111-
{
112-
throw new FileNotFoundException("Cannot determine the Linux distribution. The /etc/os-release file does not exist.");
113-
}
110+
111+
throw new FileNotFoundException("Cannot determine the Linux distribution. The /etc/os-release file does not exist.");
114112
}
115113
}
Lines changed: 93 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,39 @@
1-
using System.IO;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.IO;
5+
using System.Linq;
26
using System.Text;
37

48
namespace GeneralUpdate.Core.Driver
59
{
610
/// <summary>
711
/// When the /export-driver command backs up a driver, it backs up the driver package along with all its dependencies, such as associated library files and other related files.
812
/// </summary>
9-
public class BackupDriverCommand : IDriverCommand
13+
public class BackupDriverCommand(DriverInformation information) : DriverCommand
1014
{
11-
private DriverInformation _information;
15+
private readonly string _driverExtension = $"*{information.DriverFileExtension}";
1216

13-
public BackupDriverCommand(DriverInformation information) => _information = information;
14-
15-
public void Execute()
17+
public override void Execute()
1618
{
19+
var uninstalledDrivers = Directory.GetFiles(information.DriverDirectory, _driverExtension, SearchOption.AllDirectories).ToList();
20+
var installedDrivers = GetInstalledDrivers(information.FieldMappings);
21+
var tempDrivers = installedDrivers.Where(a => uninstalledDrivers.Any(b => string.Equals(a.OriginalName, Path.GetFileName(b)))).ToList();
22+
information.Drivers = tempDrivers;
23+
24+
//Export the backup according to the driver name.
25+
if (Directory.Exists(information.OutPutDirectory))
26+
{
27+
Directory.Delete(information.OutPutDirectory, true);
28+
}
29+
30+
Directory.CreateDirectory(information.OutPutDirectory);
31+
1732
/*
1833
* Back up the specified list of drives.
1934
*/
20-
foreach (var driver in _information.Drivers)
35+
foreach (var driver in tempDrivers)
2136
{
22-
//Export the backup according to the driver name.
23-
if (Directory.Exists(_information.OutPutDirectory))
24-
Directory.Delete(_information.OutPutDirectory, true);
25-
26-
Directory.CreateDirectory(_information.OutPutDirectory);
2737
/*
2838
* If no test driver files are available, you can run the following command to export all installed driver files.
2939
* (1) dism /online /export-driver /destination:"D:\packet\cache\"
@@ -32,14 +42,82 @@ public void Execute()
3242
* The following code example exports the specified driver to the specified directory.
3343
* pnputil /export-driver oemXX.inf D:\packet\cache
3444
*/
45+
var path = Path.Combine(information.OutPutDirectory, driver.PublishedName);
3546
var command = new StringBuilder("/c pnputil /export-driver ")
36-
.Append(driver)
47+
.Append(driver.PublishedName)
3748
.Append(' ')
38-
.Append(_information.OutPutDirectory)
49+
.Append(path)
3950
.ToString();
40-
51+
52+
if (!Directory.Exists(path))
53+
{
54+
Directory.CreateDirectory(path);
55+
}
56+
4157
CommandExecutor.ExecuteCommand(command);
4258
}
4359
}
60+
61+
private IEnumerable<DriverInfo> GetInstalledDrivers(Dictionary<string, string> fieldMappings)
62+
{
63+
var drivers = new List<DriverInfo>();
64+
var process = new Process();
65+
process.StartInfo.FileName = "pnputil";
66+
process.StartInfo.Arguments = "/enum-drivers";
67+
process.StartInfo.RedirectStandardOutput = true;
68+
process.StartInfo.UseShellExecute = false;
69+
process.StartInfo.CreateNoWindow = true;
70+
process.Start();
71+
72+
var output = process.StandardOutput.ReadToEnd();
73+
process.WaitForExit();
74+
75+
var lines = output.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
76+
77+
DriverInfo currentDriver = null;
78+
foreach (var line in lines)
79+
{
80+
if (line.StartsWith(fieldMappings["PublishedName"]))
81+
{
82+
if (currentDriver != null)
83+
{
84+
drivers.Add(currentDriver);
85+
}
86+
currentDriver = new ();
87+
currentDriver.PublishedName = line.Split(new[] { ':' }, 2)[1].Trim();
88+
}
89+
else if (line.StartsWith(fieldMappings["OriginalName"]) && currentDriver != null)
90+
{
91+
currentDriver.OriginalName = line.Split(new[] { ':' }, 2)[1].Trim();
92+
}
93+
else if (line.StartsWith(fieldMappings["Provider"]) && currentDriver != null)
94+
{
95+
currentDriver.Provider = line.Split(new[] { ':' }, 2)[1].Trim();
96+
}
97+
else if (line.StartsWith(fieldMappings["ClassName"]) && currentDriver != null)
98+
{
99+
currentDriver.ClassName = line.Split(new[] { ':' }, 2)[1].Trim();
100+
}
101+
else if (line.StartsWith(fieldMappings["ClassGUID"]) && currentDriver != null)
102+
{
103+
currentDriver.ClassGUID = line.Split(new[] { ':' }, 2)[1].Trim();
104+
}
105+
else if (line.StartsWith(fieldMappings["Version"]) && currentDriver != null)
106+
{
107+
currentDriver.Version = line.Split(new[] { ':' }, 2)[1].Trim();
108+
}
109+
else if (line.StartsWith(fieldMappings["Signer"]) && currentDriver != null)
110+
{
111+
currentDriver.Signer = line.Split(new[] { ':' }, 2)[1].Trim();
112+
}
113+
}
114+
115+
if (currentDriver != null)
116+
{
117+
drivers.Add(currentDriver);
118+
}
119+
120+
return drivers;
121+
}
44122
}
45123
}

src/c#/GeneralUpdate.Core/Driver/CommandExecutor.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ namespace GeneralUpdate.Core.Driver
88
/// </summary>
99
public class CommandExecutor
1010
{
11+
private CommandExecutor()
12+
{ }
13+
1114
public static void ExecuteCommand(string command)
1215
{
1316
/*
@@ -28,7 +31,9 @@ Update the driver regularly to ensure that the driver is compatible with the cur
2831
WindowStyle = ProcessWindowStyle.Hidden,
2932
FileName = "cmd.exe",
3033
Arguments = command,
31-
UseShellExecute = true,
34+
UseShellExecute = false,
35+
RedirectStandardOutput = true,
36+
RedirectStandardError = true,
3237
Verb = "runas"
3338
};
3439

@@ -39,6 +44,17 @@ Update the driver regularly to ensure that the driver is compatible with the cur
3944
process.Start();
4045
process.WaitForExit();
4146

47+
// 读取标准输出
48+
var output = process.StandardOutput.ReadToEnd();
49+
Debug.WriteLine(output);
50+
51+
// 读取错误输出
52+
var error = process.StandardError.ReadToEnd();
53+
if (!string.IsNullOrEmpty(error))
54+
{
55+
Debug.WriteLine("Error: " + error);
56+
}
57+
4258
if (process.ExitCode != 0)
4359
throw new ApplicationException($"Operation failed code: {process.ExitCode}");
4460
}
Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
using System.Text;
22

3-
namespace GeneralUpdate.Core.Driver
3+
namespace GeneralUpdate.Core.Driver;
4+
5+
public class DeleteDriverCommand(DriverInformation information) : DriverCommand
46
{
5-
public class DeleteDriverCommand : IDriverCommand
7+
public override void Execute()
68
{
7-
private DriverInformation _information;
8-
9-
public DeleteDriverCommand(DriverInformation information) => _information = information;
10-
11-
public void Execute()
9+
//Before installing the driver, delete the driver that has been installed on the local system. Otherwise, an exception may occur.
10+
foreach (var driver in information.Drivers)
1211
{
13-
//Before installing the driver, delete the driver that has been installed on the local system. Otherwise, an exception may occur.
14-
foreach (var driver in _information.Drivers)
15-
{
16-
var command = new StringBuilder("/c pnputil /delete-driver ")
17-
.Append(driver)
18-
.ToString();
19-
CommandExecutor.ExecuteCommand(command);
20-
}
12+
var command = new StringBuilder("/c pnputil /delete-driver ")
13+
.Append(driver.PublishedName)
14+
.ToString();
15+
CommandExecutor.ExecuteCommand(command);
2116
}
2217
}
2318
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using GeneralUpdate.Common;
6+
7+
namespace GeneralUpdate.Core.Driver
8+
{
9+
public abstract class DriverCommand
10+
{
11+
public abstract void Execute();
12+
13+
/// <summary>
14+
/// Search for driver files.
15+
/// </summary>
16+
/// <param name="patchPath"></param>
17+
/// <returns></returns>
18+
protected static IEnumerable<FileInfo> SearchDrivers(string patchPath, string fileExtension)
19+
{
20+
var files = GeneralFileManager.GetAllfiles(patchPath);
21+
return files.Where(x => x.FullName.EndsWith(fileExtension)).ToList();
22+
}
23+
}
24+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace GeneralUpdate.Core.Driver;
2+
3+
public class DriverInfo
4+
{
5+
public string PublishedName { get; set; }
6+
public string OriginalName { get; set; }
7+
public string Provider { get; set; }
8+
public string ClassName { get; set; }
9+
public string ClassGUID { get; set; }
10+
public string Version { get; set; }
11+
public string Signer { get; set; }
12+
}

0 commit comments

Comments
 (0)