forked from OpenAtom-Linyaps/linyaps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnvidia_driver_detector.cpp
More file actions
253 lines (199 loc) · 8.19 KB
/
Copy pathnvidia_driver_detector.cpp
File metadata and controls
253 lines (199 loc) · 8.19 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// SPDX-FileCopyrightText: 2025 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "nvidia_driver_detector.h"
#include "linglong/package/version.h"
#include "linglong/utils/cmd.h"
#include "linglong/utils/error/error.h"
#include "linglong/utils/log/log.h"
#include <nlohmann/json.hpp>
#include <algorithm>
#include <filesystem>
#include <fstream>
namespace linglong::driver::detect {
NVIDIADriverDetector::NVIDIADriverDetector(std::string versionFilePath)
: versionFilePath_(std::move(versionFilePath))
{
}
utils::error::Result<GraphicsDriverInfo> NVIDIADriverDetector::detect()
{
LINGLONG_TRACE("NVIDIA driver detection started");
// Get driver version
auto version = getDriverVersion();
if (version.empty()) {
return LINGLONG_ERR("Failed to get NVIDIA driver version");
}
auto linglongPackageName = getDriverIdentify() + "." + version;
// Check if package exists in repo
auto packageInfoResult = getPackageInfoFromRemoteRepo(linglongPackageName);
if (!packageInfoResult) {
return LINGLONG_ERR("Failed to get package info from remote repo: "
+ packageInfoResult.error().message());
}
auto installedResult = checkPackageInstalled(linglongPackageName);
if (!installedResult) {
return LINGLONG_ERR("Failed to check if package is installed: "
+ installedResult.error().message());
}
if (*installedResult) {
if (!packageInfoResult->first) {
LogW("NVIDIA driver package is installed locally but not found in remote repo: {}",
linglongPackageName);
return LINGLONG_ERR("Cannot find NVIDIA driver package in remote repo.");
}
auto upgradableResult = checkPackageUpgradable(linglongPackageName);
if (!upgradableResult) {
return LINGLONG_ERR("Failed to check if package is upgradable: "
+ upgradableResult.error().message());
}
if (*upgradableResult) {
LogD("NVIDIA driver package can be upgraded : {}, install from remote repo.",
linglongPackageName);
return packageInfoResult->second;
}
return LINGLONG_ERR("NVIDIA driver package is already installed and up-to-date.");
}
if (!packageInfoResult->first) {
return LINGLONG_ERR("NVIDIA driver package not found in remote repo");
}
LogD("NVIDIA driver package is not installed, install from remote repo.");
return packageInfoResult->second;
}
utils::error::Result<std::pair<bool, GraphicsDriverInfo>>
NVIDIADriverDetector::getPackageInfoFromRemoteRepo(const std::string &packageName)
{
using json = nlohmann::json;
LINGLONG_TRACE("Check if NVIDIA driver package exists in repository");
GraphicsDriverInfo driverInfo;
driverInfo.identify = getDriverIdentify();
driverInfo.packageName = packageName;
// Execute ll-cli search command to check driver package existence
auto ret = linglong::utils::Cmd("ll-cli").exec({ "--json", "search", packageName });
if (!ret) {
return LINGLONG_ERR("Search command failed: " + ret.error().message());
}
bool found = false;
try {
json j = json::parse(*ret);
for (const auto &[category, apps] : j.items()) {
if (!apps.is_array())
continue;
for (const auto &item : apps) {
auto it = item.find("version");
if (it == item.end() || !it->is_string())
continue;
std::string_view currentVersionStr = it->get<std::string_view>();
if (!found || compareVersions(currentVersionStr, driverInfo.packageVersion)) {
driverInfo.repoName = category;
driverInfo.packageVersion = currentVersionStr;
found = true;
}
}
}
} catch (const nlohmann::json::parse_error &e) {
return LINGLONG_ERR("Failed to parse search result JSON: " + std::string(e.what()));
}
return std::make_pair(found, driverInfo);
}
utils::error::Result<bool>
NVIDIADriverDetector::checkPackageInstalled(const std::string &packageName)
{
LINGLONG_TRACE("Check if NVIDIA driver package is installed");
auto installedInfo = getInstalledGraphicsDriverInfo(packageName);
if (!installedInfo) {
return LINGLONG_ERR(installedInfo);
}
return installedInfo->first;
}
utils::error::Result<bool>
NVIDIADriverDetector::checkPackageUpgradable(const std::string &packageName)
{
LINGLONG_TRACE("Check if NVIDIA driver package is upgradable");
auto upgradableDriverInfo = getPackageInfoFromRemoteRepo(packageName);
if (!upgradableDriverInfo) {
return LINGLONG_ERR("Failed to get upgradable package info: "
+ upgradableDriverInfo.error().message());
}
if (!upgradableDriverInfo->first) {
return LINGLONG_ERR("NVIDIA driver package not found in remote repo");
}
auto installedDriverInfo = getInstalledGraphicsDriverInfo(packageName);
if (!installedDriverInfo) {
return LINGLONG_ERR("Failed to get installed package info: "
+ installedDriverInfo.error().message());
}
if (!installedDriverInfo->first) {
return LINGLONG_ERR("NVIDIA driver package is not installed");
}
LogD("{} current version:{}, latest version {}",
packageName,
installedDriverInfo->second.packageVersion,
upgradableDriverInfo->second.packageVersion);
return compareVersions(upgradableDriverInfo->second.packageVersion,
installedDriverInfo->second.packageVersion);
}
std::string NVIDIADriverDetector::getDriverVersion()
{
std::string version;
if (!std::filesystem::exists(versionFilePath_)) {
LogW("NVIDIA version file not found: {}", versionFilePath_);
return version;
}
std::ifstream versionFile(versionFilePath_);
if (versionFile) {
versionFile >> version;
std::replace(version.begin(), version.end(), '.', '-');
}
return version;
}
utils::error::Result<std::pair<bool, GraphicsDriverInfo>>
NVIDIADriverDetector::getInstalledGraphicsDriverInfo(const std::string &packageName) const
{
using json = nlohmann::json;
LINGLONG_TRACE("Get installed NVIDIA graphics driver info");
GraphicsDriverInfo driverInfo;
driverInfo.identify = getDriverIdentify();
driverInfo.packageName = packageName;
auto listResult = linglong::utils::Cmd("ll-cli").exec({ "--json", "list", "--type=extension" });
if (!listResult) {
return LINGLONG_ERR(listResult);
}
bool installed = false;
try {
json installedExtensions = json::parse(*listResult);
if (!installedExtensions.is_array()) {
return LINGLONG_ERR("Invalid list result JSON: expected an array");
}
for (const auto &item : installedExtensions) {
if (!item.is_object())
continue;
auto idIter = item.find("id");
if (idIter == item.end() || !idIter->is_string()
|| idIter->get<std::string_view>() != packageName) {
continue;
}
auto versionIter = item.find("version");
if (versionIter == item.end() || !versionIter->is_string()) {
return LINGLONG_ERR("Installed package found but version field is missing: "
+ packageName);
}
driverInfo.packageVersion = versionIter->get<std::string_view>();
installed = true;
break;
}
} catch (const nlohmann::json::parse_error &e) {
return LINGLONG_ERR("Failed to parse installed package JSON: " + std::string(e.what()));
}
return std::make_pair(installed, driverInfo);
}
bool NVIDIADriverDetector::compareVersions(std::string_view v1, std::string_view v2) const noexcept
{
auto ver1 = package::Version::parse(std::string(v1));
auto ver2 = package::Version::parse(std::string(v2));
if (!ver1 || !ver2) {
LogW("Failed to parse versions: {} , {}", v1, v2);
return false;
}
return v1 > v2;
}
} // namespace linglong::driver::detect