fix(enable): use sysfs to detect wireless NIC instead of name prefix#668
fix(enable): use sysfs to detect wireless NIC instead of name prefix#668GongHeng2017 wants to merge 1 commit into
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: GongHeng2017 The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Reviewer's guide (collapsed on small PRs)Reviewer's GuideRefactors wireless network interface detection in EnableUtils to rely on sysfs entries instead of hardcoded interface name prefixes, and encapsulates the new detection logic in a private helper while updating metadata headers. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The
isWirelesshelper can be simplified to a single return statement (e.g.,return wirelessInfo.exists() || phyInfo.exists();) to avoid the explicitif/elseand keep the code concise. - Consider using
QFile::exists()directly instead of constructingQFileInfoobjects inisWireless, since you only need to check existence and not other file metadata.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The `isWireless` helper can be simplified to a single return statement (e.g., `return wirelessInfo.exists() || phyInfo.exists();`) to avoid the explicit `if/else` and keep the code concise.
- Consider using `QFile::exists()` directly instead of constructing `QFileInfo` objects in `isWireless`, since you only need to check existence and not other file metadata.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Replace hardcoded name prefix matching (wlan/wlp) with sysfs path checking (/sys/class/net/<name>/wireless and phy80211) for more reliable wireless NIC detection. 使用 sysfs 路径检测替代硬编码名称前缀匹配来判断无线网卡,提升检测准确性。 Log: 修复无线网卡检测逻辑,使用sysfs替代名称前缀匹配 PMS: https://pms.uniontech.com/bug-view-362409.html Influence: 无线网卡启用/禁用操作不再依赖接口名称前缀,支持所有符合内核规范的无线网卡接口名称。
96b9844 to
82e248f
Compare
deepin pr auto review这份代码提交的主要目的是优化无线网卡禁用/启用的逻辑,将原来通过正则解析 以下是对该 diff 的详细代码审查,涵盖语法逻辑、代码质量、代码性能和代码安全四个方面: 一、 语法与逻辑
二、 代码质量
三、 代码性能
四、 代码安全
改进后的代码建议针对以上审查意见,修改后的核心代码部分如下: // enableutils.cpp
bool EnableUtils::ioctlOperateNetworkLogicalName(const QString &logicalName, bool enable)
{
if (isWireless(logicalName)) { // Wireless LAN
// 第一步:获取 wiphy 编号
QProcess iwProcess;
iwProcess.start("iw", QStringList() << "dev" << logicalName << "info");
if (!iwProcess.waitForFinished()) {
qCritical() << "Failed to execute 'iw' command or timed out.";
return false;
}
if (iwProcess.exitCode() != 0) {
qCritical() << "'iw' command returned error:" << iwProcess.readAllStandardError();
return false;
}
QString iwOutput = QString::fromUtf8(iwProcess.readAllStandardOutput());
QRegularExpression wiphyRe("wiphy (\\d+)");
QRegularExpressionMatch wiphyMatch = wiphyRe.match(iwOutput);
if (!wiphyMatch.hasMatch()) {
qCritical() << "Failed to parse wiphy number from iw output.";
return false;
}
QString phyNum = wiphyMatch.captured(1);
// 第二步:获取 rfkill 设备编号
QString rfkillId;
QString phyPath = QString("/sys/class/ieee80211/phy%1").arg(phyNum);
QDir phyDir(phyPath);
if (!phyDir.exists()) {
qCritical() << "Phy path does not exist:" << phyPath;
return false;
}
// 查找 rfkill 目录
QStringList rfkillDirs = phyDir.entryList(QStringList() << "rfkill*", QDir::Dirs);
if (rfkillDirs.isEmpty()) {
qCritical() << "No rfkill directory found under" << phyPath; // 修复拼写 unbder -> under
return false;
}
// 读取 index 文件
QString rfkillIndexPath = phyPath + "/" + rfkillDirs.first() + "/index";
QFile indexFile(rfkillIndexPath);
if (indexFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
rfkillId = indexFile.readLine().trimmed();
indexFile.close();
} else {
qCritical() << "Failed to read rfkill index from" << rfkillIndexPath; // 修复拼写 Failded -> Failed
return false;
}
// 第三步:执行 rfkill block/unblock
if (!rfkillId.isEmpty()) {
QProcess rfkillProcess;
rfkillProcess.start("rfkill", QStringList() << (enable ? "unblock" : "block") << rfkillId);
rfkillProcess.waitForFinished();
int ret = rfkillProcess.exitCode();
if (ret != 0) {
qCritical() << "Failed to block/unblock wifi: error code: " << ret;
}
} else {
qCritical() << "Empty rfkill ID";
return false; // 【关键修复】空 ID 时应终止操作,避免状态不一致
}
// 第四步:执行 ifconfig up/down
// ... (后续代码保持不变)// isWireless 函数优化
bool EnableUtils::isWireless(const QString &logicalName)
{
QFileInfo wirelessInfo(QString("/sys/class/net/%1/wireless").arg(logicalName));
QFileInfo phyInfo(QString("/sys/class/net/%1/phy80211").arg(logicalName));
// 简化逻辑
return wirelessInfo.exists() || phyInfo.exists();
}总结:此次代码重构方向十分正确,通过利用 Linux sysfs 文件系统替代命令行解析,提高了代码的健壮性和可维护性。只需注意修复拼写错误、完善错误处理的闭环(特别是空 rfkillId 时的 return false),以及增加对 |
Replace hardcoded name prefix matching (wlan/wlp) with sysfs path checking (/sys/class/net//wireless and phy80211) for more reliable wireless NIC detection.
使用 sysfs 路径检测替代硬编码名称前缀匹配来判断无线网卡,提升检测准确性。
Log: 修复无线网卡检测逻辑,使用sysfs替代名称前缀匹配
PMS: https://pms.uniontech.com/bug-view-362409.html
Influence: 无线网卡启用/禁用操作不再依赖接口名称前缀,支持所有符合内核规范的无线网卡接口名称。
Summary by Sourcery
Detect wireless network interfaces via sysfs paths instead of hardcoded name prefixes when enabling or disabling devices.
Bug Fixes:
Enhancements: