Skip to content

Commit

Permalink
feat: 设备管理器驱动备份还原service端功能
Browse files Browse the repository at this point in the history
设备管理器驱动备份还原service端功能

Log: 设备管理器驱动备份还原service

Task: https://pms.uniontech.com/task-view-285255.html
  • Loading branch information
jeffshuai committed Sep 14, 2023
1 parent bd14608 commit b967e5d
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ if (DISABLE_DRIVER)
add_definitions(-DDISABLE_DRIVER)
endif()

set(DISABLE_POLKIT false CACHE BOOL "disable polkit debug")
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_definitions(-DDISABLE_POLKIT)
endif()

add_subdirectory(${CMAKE_SOURCE_DIR}/deepin-devicemanager)
add_subdirectory(${CMAKE_SOURCE_DIR}/deepin-devicemanager-server)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ static int getPidByName(const QString &taskName)

bool ControlInterface::getUserAuthorPasswd()
{
#ifdef DISABLE_POLKIT
return true;
#endif
int pid = getPidByName("deepin-devicemanager");
if (pid >= 0) {
Authority::Result result = Authority::instance()->checkAuthorizationSync("com.deepin.deepin-devicemanager.checkAuthentication",
Expand Down Expand Up @@ -305,6 +308,23 @@ bool ControlInterface::unInstallPrinter(const QString &vendor, const QString &mo
}
return mp_drivermanager->uninstallPrinter(vendor, model);
}

bool ControlInterface::bankupDeb(const QString &debpath)
{
if (!getUserAuthorPasswd()) {

Check warning on line 314 in deepin-devicemanager-server/deepin-devicecontrol/src/controlinterface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Condition '!getUserAuthorPasswd()' is always false

Check warning on line 314 in deepin-devicemanager-server/deepin-devicecontrol/src/controlinterface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Condition '!getUserAuthorPasswd()' is always false

Check warning on line 314 in deepin-devicemanager-server/deepin-devicecontrol/src/controlinterface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Condition '!getUserAuthorPasswd()' is always false

Check warning on line 314 in deepin-devicemanager-server/deepin-devicecontrol/src/controlinterface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Condition '!getUserAuthorPasswd()' is always false
return false;
}
return mp_drivermanager->bankupDeb(debpath);
}

bool ControlInterface::delDeb(const QString &debname)
{
if (!getUserAuthorPasswd()) {

Check warning on line 322 in deepin-devicemanager-server/deepin-devicecontrol/src/controlinterface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Condition '!getUserAuthorPasswd()' is always false

Check warning on line 322 in deepin-devicemanager-server/deepin-devicecontrol/src/controlinterface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Condition '!getUserAuthorPasswd()' is always false

Check warning on line 322 in deepin-devicemanager-server/deepin-devicecontrol/src/controlinterface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Condition '!getUserAuthorPasswd()' is always false

Check warning on line 322 in deepin-devicemanager-server/deepin-devicecontrol/src/controlinterface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Condition '!getUserAuthorPasswd()' is always false
return false;
}
return mp_drivermanager->delDeb(debname);
}

#endif
bool ControlInterface::authorizedEnable(const QString &hclass, const QString &name, const QString &path, const QString &unique_id, bool enable_device, const QString strDriver)

Check warning on line 329 in deepin-devicemanager-server/deepin-devicecontrol/src/controlinterface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Parameter 'strDriver' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.

Check warning on line 329 in deepin-devicemanager-server/deepin-devicecontrol/src/controlinterface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Parameter 'strDriver' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.

Check warning on line 329 in deepin-devicemanager-server/deepin-devicecontrol/src/controlinterface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Parameter 'strDriver' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.

Check warning on line 329 in deepin-devicemanager-server/deepin-devicecontrol/src/controlinterface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Parameter 'strDriver' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ public slots:
Q_SCRIPTABLE bool isArchMatched(const QString &filePath);
Q_SCRIPTABLE bool isDebValid(const QString &filePath);
Q_SCRIPTABLE bool unInstallPrinter(const QString &vendor, const QString &model);
Q_SCRIPTABLE bool bankupDeb(const QString &debpath); //debpath格式须是: “/tmp/xx/debname/debname_ver_xx.deb”
Q_SCRIPTABLE bool delDeb(const QString &debname);

#endif

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@
#include <QThread>
#include <QDBusInterface>
#include <QNetworkConfigurationManager>
#include <QDir>
#include <QFileInfoList>
#include <QDebug>

#include <fstream>
#include <string>
#include <vector>


#define DB_PATH "/usr/share/deepin-devicemanager" //driver deb backup path
#define SD_KEY_excat "excat"
#define SD_KEY_ver "version"
#define SD_KEY_code "client_code"
Expand Down Expand Up @@ -625,4 +628,114 @@ bool DriverManager::unInstallPrinter(const QString &packageName)

return !printerHasInstalled(packageName);
}

static bool qdelDirectory(QString toDir)
{
QDir toDir_(toDir);

//如果目的文件夹不存在则创建
if (!toDir_.exists()) {
return true;
}
//获取当前路径下的所有文件名
QFileInfoList fileInfoList = toDir_.entryInfoList(QDir::NoDotAndDotDot);
foreach (QFileInfo fileInfo, fileInfoList) {
//拷贝子目录
if (fileInfo.isDir()) {
//递归调用拷贝
if (!qdelDirectory(toDir_.filePath(fileInfo.fileName())))
return false;
} else
toDir_.remove(fileInfo.fileName());

}
return true;
}
/*********************************************************************/
/*功能:拷贝文件夹
qCopyDirectory -- 拷贝目录
fromDir : 源目录
toDir : 目标目录
bCoverIfFileExists : ture:同名时覆盖 false:同名时返回false,终止拷贝
返回: ture拷贝成功 false:拷贝未完成*/
/***********************************************************************/
static bool qCopyDirectory(QString fromDir, QString toDir, bool bCoverIfFileExists)
{
QDir formDir_(fromDir);
QDir toDir_(toDir);

//如果目的文件夹不存在则创建
if (!toDir_.exists()) {
if (!toDir_.mkpath(toDir))
return false;
}

//获取当前路径下的所有文件名
QStringList nameFiltes;
nameFiltes << "*.deb" ;
QFileInfoList fileInfoList = formDir_.entryInfoList(nameFiltes, QDir::Dirs | QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot);
foreach (const QFileInfo &fileInfo, fileInfoList) {
//拷贝子目录
if (fileInfo.isDir()) {
//递归调用拷贝
if (!qCopyDirectory(fileInfo.filePath(), toDir_.filePath(fileInfo.fileName()), true))
return false;
}
//拷贝子文件
else {
if (bCoverIfFileExists && toDir_.exists(fileInfo.fileName())) {
toDir_.remove(fileInfo.fileName());
}
if (!QFile::copy(fileInfo.filePath(), toDir_.filePath(fileInfo.fileName()))) {
return false;
}
}
}
return true;
}

bool DriverManager::delDeb(const QString &debname)
{
QString backupPath = QString("%1/driver/%2").arg(DB_PATH).arg(debname);
QDir destdir(backupPath);
if (destdir.exists()) {
qdelDirectory(backupPath);
destdir.rmdir(destdir.absolutePath());
}
return (!destdir.exists())
}

/**
* @brief DriverManager::backupDeb backup 驱动
* @param modulename 驱动deb模块名
* @return true:成功 false:失败
*/

bool DriverManager::backupDeb(const QString &debpath)

Check warning on line 714 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/drivermanager.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'backupDeb' is never used.

Check warning on line 714 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/drivermanager.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'backupDeb' is never used.

Check warning on line 714 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/drivermanager.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'backupDeb' is never used.

Check warning on line 714 in deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/drivermanager.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'backupDeb' is never used.
{
QDir formDir_(debpath);
if (!formDir_.exists()) { //检查传入路径是否存在
return false;
}

QString fromPath = debpath;
int cnt = debpath.length();
int i = debpath.lastIndexOf("/");
if ((1 > cnt) || (1 > i)) { //检查传入路径是否合规 /tmp/xx/debname/debname_ver_xx.deb
return false;
}
QString debname = debpath.right(cnt - i - 1);

QString backupPath = QString("%1/driver/%2").arg(DB_PATH).arg(debname);
QDir destdir(backupPath);

if (destdir.exists()) { //检查备份路径是否存在
if (qdelDirectory(backupPath)) { // delete first
if (!destdir.mkpath(destdir.absolutePath())) // mkdir
return false;
}
}
return qCopyDirectory(fromPath, backupPath, true);
}

#endif // DISABLE_DRIVER
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class DriverManager : public QObject
bool isArchMatched(const QString &path);
//判断包是否有效
bool isDebValid(const QString &filePath);
bool backupDeb(const QString &debpath);
bool delDeb(const QString &debname);

private:
void initConnections();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<defaults>
<allow_any>no</allow_any>
<allow_inactive>no</allow_inactive>
<allow_active>auth_self</allow_active>
<allow_active>auth_admin_keep</allow_active>
</defaults>
<description xml:lang="en_US">Check Authentication</description>
<message xml:lang="en_US">Authentication is required to perform this action</message>
Expand Down

0 comments on commit b967e5d

Please sign in to comment.