Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019 ~ 2023 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2019 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand All @@ -12,16 +12,22 @@
#include <QApt/Backend>
#include <QApt/DebFile>
#include <QApt/Transaction>
#include <QLoggingCategory>

Check warning on line 15 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/driverinstaller.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QLoggingCategory> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 15 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/driverinstaller.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QLoggingCategory> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QProcess>

Check warning on line 16 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/driverinstaller.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QProcess> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 16 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/driverinstaller.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QProcess> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QTimer>

Check warning on line 17 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/driverinstaller.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QTimer> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 17 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/driverinstaller.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QTimer> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QFile>

Check warning on line 18 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/driverinstaller.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QFile> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 18 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/driverinstaller.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QFile> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QFileInfo>

Check warning on line 19 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/driverinstaller.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QFileInfo> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 19 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/driverinstaller.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QFileInfo> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <unistd.h>

Check warning on line 21 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/driverinstaller.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <unistd.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 21 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/driverinstaller.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <unistd.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <fstream>

Check warning on line 22 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/driverinstaller.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <fstream> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 22 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/driverinstaller.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <fstream> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string>
#include <vector>

Check warning on line 24 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/driverinstaller.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <vector> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 24 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/driverinstaller.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <vector> not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace DDLog;

const QString DriverInstaller::DEVICE_REPO_PATH = "/etc/apt/sources.list.d/devicemanager.list";
const QString DriverInstaller::DRIVER_REPO_PATH = "/etc/apt/sources.list.d/driver.list";

const int MAX_DPKGRUNING_TEST = 20;
const int TEST_TIME_INTERVAL = 2000;

Expand Down Expand Up @@ -134,9 +140,13 @@

void DriverInstaller::doOperate(const QString &package, const QString &version)
{
// 按需创建驱动仓库源文件,仅在 driver.list 中不存在 pro-driver-packages 时创建
ensureDriverRepoSource();

if (!initBackend()) {
emit errorOccurred(EC_NULL);
qCInfo(appLog) << "DRIVER_LOG : ************************** 初始化backend失败";
cleanupTempSource();
return;
}

Expand All @@ -145,6 +155,7 @@
if (nullptr == p) {
emit errorOccurred(EC_NOTFOUND);
qCInfo(appLog) << "DRIVER_LOG : ************************** 安装包不存在";
cleanupTempSource();
return;
}

Expand All @@ -154,6 +165,7 @@
emit errorOccurred(EC_NOTFOUND);
delete p;
p = nullptr;
cleanupTempSource();
return;
}

Expand All @@ -164,6 +176,7 @@
if (nullptr == mp_Trans) {
emit errorOccurred(EC_NULL);
qCInfo(appLog) << "DRIVER_LOG : ************************** installPackages";
cleanupTempSource();
return;
}

Expand Down Expand Up @@ -192,6 +205,9 @@
qCInfo(appLog) << "DRIVER_LOG : ************************** 安装结束 结束状态码" << status;
qCInfo(appLog) << "DRIVER_LOG : ************************** 安装结束 结束错误码" << code;

// 清理本次创建的临时源文件
cleanupTempSource();

mp_Trans->disconnect();
mp_Trans->deleteLater();
mp_Trans = nullptr;
Expand Down Expand Up @@ -221,4 +237,59 @@

mp_Trans->run();
}

void DriverInstaller::ensureDriverRepoSource()
{
// 重置临时源标记,避免因前一次安装失败导致残留标记
m_tempSourceCreated = false;

// 优先检查系统自带的 driver.list 中是否已包含驱动仓库源
QFile fileDriver(DRIVER_REPO_PATH);
if (fileDriver.open(QIODevice::ReadOnly)) {
QString info = fileDriver.readAll();
QStringList lines = info.split("\n");
foreach (QString line, lines) {
if (line.contains("pro-driver-packages")) {
fileDriver.close();
return;
}
}
fileDriver.close();
}

// 使用 O_EXCL 原子创建文件,避免 TOCTOU 竞争条件(CWE-367)
// NewOnly 保证文件不存在时才创建,若已存在则 open 失败,无需先检查
QFile file(DEVICE_REPO_PATH);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QFile::NewOnly)) {
// 文件已存在属于正常情况(第三方创建),静默返回
if (!QFile::exists(DEVICE_REPO_PATH)) {
qCInfo(appLog) << "DRIVER_LOG : 创建驱动源文件失败:" << file.errorString();
}
return;
}

file.write("deb https://pro-driver-packages.uniontech.com eagle non-free\n");
file.close();
m_tempSourceCreated = true;
qCInfo(appLog) << "DRIVER_LOG : 创建临时驱动源文件:" << DEVICE_REPO_PATH;

// 新源创建后需执行 apt update 更新包索引,
// 否则 QApt::Backend 无法识别该源的软件包
// 使用列表传参绕过 shell 解析,避免命令注入风险
QProcess process;
process.start("apt", QStringList() << "update");
process.waitForFinished(50000);
qCInfo(appLog) << "DRIVER_LOG : 临时源 apt update 完成";
}

void DriverInstaller::cleanupTempSource()
{
if (!m_tempSourceCreated) {
return;
}

QFile::remove(DEVICE_REPO_PATH);
m_tempSourceCreated = false;
qCWarning(appLog) << "DRIVER_LOG : 清理临时驱动源文件:" << DEVICE_REPO_PATH;
}
#endif // DISABLE_DRIVER
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019 ~ 2023 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2019 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -27,7 +27,11 @@
public:
explicit DriverInstaller(QObject *parent = nullptr);

// 驱动仓库源文件路径
static const QString DEVICE_REPO_PATH;
static const QString DRIVER_REPO_PATH;

public slots:

Check warning on line 34 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/driverinstaller.h

View workflow job for this annotation

GitHub Actions / cppcheck

There is an unknown macro here somewhere. Configuration is required. If slots is a macro then please configure it.

Check warning on line 34 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/driverinstaller.h

View workflow job for this annotation

GitHub Actions / static-check / static-check

There is an unknown macro here somewhere. Configuration is required. If slots is a macro then please configure it.
/**
* @brief installPackage 安装包
* @param package 包名
Expand Down Expand Up @@ -69,11 +73,23 @@
* @param version
*/
void doOperate(const QString &package, const QString &version);

/**
* @brief ensureDriverRepoSource 按需创建驱动仓库源文件
* 仅在 driver.list 中不存在 pro-driver-packages 时创建 devicemanager.list
*/
void ensureDriverRepoSource();

/**
* @brief cleanupTempSource 清理本次创建的临时源文件
*/
void cleanupTempSource();
private:
QApt::Backend *mp_Backend = nullptr;
QApt::Transaction *mp_Trans = nullptr;
int m_iRuningTestCount = 0;
bool m_Cancel;
bool m_tempSourceCreated = false; // 是否在本次安装中创建了临时源
};

#endif // DRIVERINSTALLER_H
Expand Down
47 changes: 1 addition & 46 deletions deepin-devicemanager-server/deepin-deviceinfo/src/mainjob.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019 ~ 2023 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2019 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand All @@ -10,22 +10,17 @@
#include "controlinterface.h"
#include "DDLog.h"

#include <QMutex>

Check warning on line 13 in deepin-devicemanager-server/deepin-deviceinfo/src/mainjob.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QMutex> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 13 in deepin-devicemanager-server/deepin-deviceinfo/src/mainjob.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QMutex> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QProcess>
#include <QFile>
#include <QLoggingCategory>
#include <QTimer>
#include <QDBusConnection>

#include <DSysInfo>

using namespace DDLog;

static QMutex mainJobMutex;
static bool s_ServerIsUpdating = false;
static bool s_ClientIsUpdating = false;
const QString DEVICE_REPO_PATH = "/etc/apt/sources.list.d/devicemanager.list";
const QString DRIVER_REPO_PATH = "/etc/apt/sources.list.d/driver.list";

MainJob::MainJob(const char *name, QObject *parent)
: QObject(parent)
Expand All @@ -43,13 +38,6 @@

// 在驱动管理延迟加载1000ms
QTimer::singleShot(1000, this, [ = ]() {
#ifndef DISABLE_DRIVER
DTK_CORE_NAMESPACE::DSysInfo::UosEdition type = DTK_CORE_NAMESPACE::DSysInfo::uosEditionType();
if (DTK_CORE_NAMESPACE::DSysInfo::UosCommunity != type ) {
initDriverRepoSource();
}
#endif

sqlCopytoKernel();

connect(m_deviceInterface, &DeviceInterface::sigUpdate, this, &MainJob::slotUsbChanged);
Expand Down Expand Up @@ -126,39 +114,6 @@
executeClientInstruction("DETECT");
}

void MainJob::initDriverRepoSource()
{
QFile fileDriver(DRIVER_REPO_PATH);
if (fileDriver.open(QIODevice::ReadOnly)) {
QString info = fileDriver.readAll();
QStringList lines = info.split("\n");
foreach (QString line, lines) {
if (line.contains("pro-driver-packages")) {
fileDriver.close();
return;
}
}
fileDriver.close();
}

QFile file(DEVICE_REPO_PATH);
if (QFile::exists(DEVICE_REPO_PATH)) {
return;
}
if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) {
qCInfo(appLog) << file.errorString();
return;
}

file.write("deb https://pro-driver-packages.uniontech.com eagle non-free\n");
file.close();

QString cmd = "apt update";
QProcess process;
process.start(cmd);
process.waitForFinished(-1);
}

void MainJob::updateAllDevice()
{
PERF_PRINT_BEGIN("POINT-01", "MainJob::updateAllDevice()");
Expand Down
7 changes: 1 addition & 6 deletions deepin-devicemanager-server/deepin-deviceinfo/src/mainjob.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019 ~ 2023 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2019 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -53,11 +53,6 @@ private slots:
* @brief sqlCopytoKernel
*/
void sqlCopytoKernel();
/**
* @brief initDriverRepoSource : 初始化驱动仓库
* @return : 无
*/
void initDriverRepoSource();
/**
* @brief updateAllDevice
*/
Expand Down
Loading