2021-08-06 10:20:03 +08:00
|
|
|
|
/**
|
|
|
|
|
* brief 程序比较小,将接口层和控制层合到一起;主要做一些任务分发,状态监控等一些简单工作;复杂的耗时的逻辑放到任务中moveToThread中去执行
|
|
|
|
|
*/
|
|
|
|
|
|
2021-10-26 09:24:05 +08:00
|
|
|
|
#include <QTimer>
|
2022-01-18 17:35:26 +08:00
|
|
|
|
#include <QDateTime>
|
2022-06-10 16:28:29 +08:00
|
|
|
|
#include <QDBusArgument>
|
|
|
|
|
#include <QDBusMessage>
|
|
|
|
|
#include <QDBusPendingReply>
|
|
|
|
|
#include <QDBusMessage>
|
|
|
|
|
#include <QDBusUnixFileDescriptor>
|
|
|
|
|
#include <QDBusConnection>
|
2021-08-06 10:20:03 +08:00
|
|
|
|
#include <mutex>
|
2021-12-28 16:51:35 +08:00
|
|
|
|
#include <sys/reboot.h>
|
2021-08-06 10:20:03 +08:00
|
|
|
|
#include "mybackupmanager.h"
|
|
|
|
|
#include "../common/utils.h"
|
|
|
|
|
#include "mymountproxy.h"
|
|
|
|
|
#include "workerfactory.h"
|
2022-01-18 17:35:26 +08:00
|
|
|
|
#include "parsebackuplist.h"
|
2021-08-06 10:20:03 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 构造函数
|
|
|
|
|
*/
|
|
|
|
|
MyBackupManager::MyBackupManager()
|
|
|
|
|
{
|
|
|
|
|
// 注册BackupWrapper类型,之后qdbus接口才能使用
|
|
|
|
|
BackupWrapper::registerMetaType();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 析构函数
|
|
|
|
|
*/
|
|
|
|
|
MyBackupManager::~MyBackupManager()
|
|
|
|
|
{
|
|
|
|
|
if (workerThread.isRunning()) {
|
|
|
|
|
workerThread.quit();
|
|
|
|
|
workerThread.wait();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 挂载backup分区,兼容提供给自动更新模块的老接口
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
int MyBackupManager::Mount_backup_partition()
|
|
|
|
|
{
|
2021-10-26 09:24:05 +08:00
|
|
|
|
m_needUmount = false;
|
2021-08-06 10:20:03 +08:00
|
|
|
|
MyMountProxy mymount;
|
|
|
|
|
return int(mymount.mountBackupPartition());
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-26 09:24:05 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief 卸载backup分区
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
bool MyBackupManager::umountBackupPartition()
|
|
|
|
|
{
|
|
|
|
|
m_needUmount = true;
|
|
|
|
|
return umountBackup();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 卸载backup分区
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
bool MyBackupManager::umountBackup()
|
|
|
|
|
{
|
|
|
|
|
// /backup分区卸载
|
|
|
|
|
if (m_needUmount && m_isActive) {
|
|
|
|
|
QTimer::singleShot(5*1000, this, &MyBackupManager::umountBackup);
|
|
|
|
|
return true;
|
|
|
|
|
} else if (m_needUmount) {
|
|
|
|
|
MyMountProxy mymount;
|
|
|
|
|
return mymount.umountBackupPartition();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-26 14:24:58 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief 试用模式重新挂载分区
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
bool MyBackupManager::remountPartitionsForCasperMode()
|
|
|
|
|
{
|
|
|
|
|
MyMountProxy mymount;
|
|
|
|
|
return mymount.remountPartitionsForCasperMode();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-06 10:20:03 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief 环境检测
|
|
|
|
|
* @param backupWrapper,参数
|
2021-08-24 18:08:18 +08:00
|
|
|
|
* @return 0,正确启动;非0,出现错误
|
2021-08-06 10:20:03 +08:00
|
|
|
|
*/
|
|
|
|
|
int MyBackupManager::checkEnv(const BackupWrapper& backupWrapper)
|
|
|
|
|
{
|
2021-10-26 09:24:05 +08:00
|
|
|
|
qDebug("MyBackupManager::checkEnv invoke begin");
|
2021-08-06 10:20:03 +08:00
|
|
|
|
if (m_isActive || !lock(backupWrapper.m_frontUid)) {
|
|
|
|
|
emit sendEnvCheckResult(int(BackupResult::LOCK_PROGRAM_FAIL));
|
|
|
|
|
return int(BackupResult::LOCK_PROGRAM_FAIL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Worker* worker = WorkerFactory::createWorker(backupWrapper.m_type, backupWrapper.m_iPosition);
|
|
|
|
|
if (nullptr == worker) {
|
|
|
|
|
emit sendEnvCheckResult(int(BackupResult::NO_FOUND_DEALCLASS));
|
|
|
|
|
return int(BackupResult::NO_FOUND_DEALCLASS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
worker->setParam(backupWrapper);
|
2021-08-24 18:08:18 +08:00
|
|
|
|
connect(worker, &Worker::checkResult, this, [&] (int result) {
|
|
|
|
|
emit this->sendEnvCheckResult(result);
|
|
|
|
|
this->finished();
|
|
|
|
|
});
|
2021-08-06 10:20:03 +08:00
|
|
|
|
worker->moveToThread(&workerThread);
|
2021-11-15 13:36:00 +08:00
|
|
|
|
connect(&workerThread, &MyThread::started, worker, &Worker::checkEnv);
|
|
|
|
|
connect(&workerThread, &MyThread::finished, worker, &Worker::deleteLater);
|
2021-08-06 10:20:03 +08:00
|
|
|
|
|
2021-08-17 10:07:35 +08:00
|
|
|
|
workerThread.start();
|
|
|
|
|
|
2021-10-26 09:24:05 +08:00
|
|
|
|
qDebug("MyBackupManager::checkEnv invoke end");
|
2021-08-06 10:20:03 +08:00
|
|
|
|
return int(BackupResult::BACKUP_RESULT_INIT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 备份
|
|
|
|
|
* @param backupWrapper
|
2021-08-24 18:08:18 +08:00
|
|
|
|
* @return 0,正确启动备份;非0,出现错误
|
2021-08-06 10:20:03 +08:00
|
|
|
|
*/
|
|
|
|
|
int MyBackupManager::goBackup(const BackupWrapper& backupWrapper)
|
|
|
|
|
{
|
2021-12-09 10:00:07 +08:00
|
|
|
|
qDebug("MyBackupManager::goBackup invoke begin");
|
2021-08-24 18:08:18 +08:00
|
|
|
|
if (m_isActive || !lock(backupWrapper.m_frontUid)) {
|
|
|
|
|
emit sendEnvCheckResult(int(BackupResult::LOCK_PROGRAM_FAIL));
|
|
|
|
|
return int(BackupResult::LOCK_PROGRAM_FAIL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Worker* worker = WorkerFactory::createWorker(backupWrapper.m_type, backupWrapper.m_iPosition);
|
|
|
|
|
if (nullptr == worker) {
|
|
|
|
|
emit sendEnvCheckResult(int(BackupResult::NO_FOUND_DEALCLASS));
|
|
|
|
|
return int(BackupResult::NO_FOUND_DEALCLASS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
worker->setParam(backupWrapper);
|
|
|
|
|
connect(worker, &Worker::checkResult, this, [&](int result) {
|
|
|
|
|
emit this->sendEnvCheckResult(result);
|
2021-11-20 17:48:23 +08:00
|
|
|
|
|
|
|
|
|
switch (result) {
|
|
|
|
|
case int(BackupResult::CHECK_ENV_SUCCESS) :
|
|
|
|
|
case int(BackupResult::MKSQUASHFS_START_SUCCESS) :
|
|
|
|
|
case int(BackupResult::BACKUP_START_SUCCESS) :
|
2022-01-18 17:35:26 +08:00
|
|
|
|
case int(BackupResult::START_CANCEL) :
|
2021-11-20 17:48:23 +08:00
|
|
|
|
break;
|
|
|
|
|
default:
|
2021-08-24 18:08:18 +08:00
|
|
|
|
this->finished();
|
2021-11-20 17:48:23 +08:00
|
|
|
|
break;
|
2021-08-24 18:08:18 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
2021-08-26 20:12:48 +08:00
|
|
|
|
connect(worker, &Worker::progress, this, [&](int rate) {
|
|
|
|
|
emit this->progress(int(BackupState::WORKING), rate);
|
|
|
|
|
});
|
2021-08-24 18:08:18 +08:00
|
|
|
|
connect(worker, &Worker::workResult, this, [&] (bool result) {
|
2021-11-17 11:47:15 +08:00
|
|
|
|
emit this->backupFinished(result);
|
2021-08-24 18:08:18 +08:00
|
|
|
|
this->finished();
|
|
|
|
|
});
|
|
|
|
|
worker->moveToThread(&workerThread);
|
2021-11-15 13:36:00 +08:00
|
|
|
|
connect(&workerThread, &MyThread::started, worker, &Worker::doWork);
|
|
|
|
|
connect(&workerThread, &MyThread::finished, worker, &Worker::deleteLater);
|
|
|
|
|
connect(&workerThread, &MyThread::cancelWork, worker, &Worker::cancel);
|
2021-08-24 18:08:18 +08:00
|
|
|
|
|
|
|
|
|
workerThread.start();
|
|
|
|
|
|
2021-12-09 10:00:07 +08:00
|
|
|
|
qDebug("MyBackupManager::goBackup invoke end");
|
2021-08-24 18:08:18 +08:00
|
|
|
|
return int(BackupResult::BACKUP_RESULT_INIT);
|
2021-08-06 10:20:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 还原
|
|
|
|
|
* @param backupWrapper
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
int MyBackupManager::goRestore(const BackupWrapper& backupWrapper)
|
|
|
|
|
{
|
2021-12-09 10:00:07 +08:00
|
|
|
|
qDebug("MyBackupManager::goRestore invoke begin");
|
|
|
|
|
if (m_isActive || !lock(backupWrapper.m_frontUid)) {
|
|
|
|
|
emit sendEnvCheckResult(int(BackupResult::LOCK_PROGRAM_FAIL));
|
|
|
|
|
return int(BackupResult::LOCK_PROGRAM_FAIL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Worker* worker = WorkerFactory::createWorker(backupWrapper.m_type, backupWrapper.m_iPosition);
|
|
|
|
|
if (nullptr == worker) {
|
|
|
|
|
emit sendEnvCheckResult(int(BackupResult::NO_FOUND_DEALCLASS));
|
|
|
|
|
return int(BackupResult::NO_FOUND_DEALCLASS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
worker->setParam(backupWrapper);
|
|
|
|
|
connect(worker, &Worker::checkResult, this, [&](int result) {
|
|
|
|
|
emit this->sendEnvCheckResult(result);
|
|
|
|
|
|
|
|
|
|
switch (result) {
|
|
|
|
|
case int(BackupResult::CHECK_ENV_SUCCESS) :
|
2021-12-28 16:51:35 +08:00
|
|
|
|
case int(BackupResult::RESTORE_START_SUCCESS) :
|
2021-12-09 10:00:07 +08:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
this->finished();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
connect(worker, &Worker::progress, this, [&](int rate) {
|
|
|
|
|
emit this->progress(int(BackupState::WORKING), rate);
|
|
|
|
|
});
|
|
|
|
|
connect(worker, &Worker::workResult, this, [&] (bool result) {
|
2021-12-28 16:51:35 +08:00
|
|
|
|
emit this->sendRestoreResult(result);
|
2021-12-09 10:00:07 +08:00
|
|
|
|
this->finished();
|
|
|
|
|
});
|
|
|
|
|
worker->moveToThread(&workerThread);
|
|
|
|
|
connect(&workerThread, &MyThread::started, worker, &Worker::doWork);
|
|
|
|
|
connect(&workerThread, &MyThread::finished, worker, &Worker::deleteLater);
|
|
|
|
|
|
|
|
|
|
workerThread.start();
|
|
|
|
|
|
|
|
|
|
qDebug("MyBackupManager::goRestore invoke end");
|
|
|
|
|
return int(BackupResult::BACKUP_RESULT_INIT);
|
2021-08-06 10:20:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 删除备份
|
|
|
|
|
* @param backupWrapper
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
int MyBackupManager::deleteBackupPoint(const BackupWrapper& backupWrapper)
|
|
|
|
|
{
|
2021-12-09 10:00:07 +08:00
|
|
|
|
qDebug("MyBackupManager::deleteBackupPoint invoke begin");
|
2021-12-06 14:55:00 +08:00
|
|
|
|
if (m_isActive || !lock(backupWrapper.m_frontUid)) {
|
|
|
|
|
emit sendEnvCheckResult(int(BackupResult::LOCK_PROGRAM_FAIL));
|
|
|
|
|
return int(BackupResult::LOCK_PROGRAM_FAIL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Worker* worker = WorkerFactory::createWorker(backupWrapper.m_type, backupWrapper.m_iPosition);
|
|
|
|
|
if (nullptr == worker) {
|
|
|
|
|
emit sendEnvCheckResult(int(BackupResult::NO_FOUND_DEALCLASS));
|
|
|
|
|
return int(BackupResult::NO_FOUND_DEALCLASS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
worker->setParam(backupWrapper);
|
|
|
|
|
connect(worker, &Worker::workResult, this, [&] (bool result) {
|
|
|
|
|
emit this->sendDeleteResult(result);
|
|
|
|
|
this->finished();
|
|
|
|
|
});
|
|
|
|
|
worker->moveToThread(&workerThread);
|
|
|
|
|
connect(&workerThread, &MyThread::started, worker, &Worker::doWork);
|
|
|
|
|
connect(&workerThread, &MyThread::finished, worker, &Worker::deleteLater);
|
|
|
|
|
|
|
|
|
|
workerThread.start();
|
|
|
|
|
|
2021-12-09 10:00:07 +08:00
|
|
|
|
qDebug("MyBackupManager::deleteBackupPoint invoke end");
|
2021-12-06 14:55:00 +08:00
|
|
|
|
return int(BackupResult::BACKUP_RESULT_INIT);
|
2021-08-06 10:20:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief ghost镜像
|
|
|
|
|
* @param backupWrapper
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
int MyBackupManager::ghostBackup(const BackupWrapper& backupWrapper)
|
|
|
|
|
{
|
2022-01-13 14:06:40 +08:00
|
|
|
|
qDebug("MyBackupManager::ghostBackup invoke begin");
|
|
|
|
|
if (m_isActive || !lock(backupWrapper.m_frontUid)) {
|
|
|
|
|
emit sendEnvCheckResult(int(BackupResult::LOCK_PROGRAM_FAIL));
|
|
|
|
|
return int(BackupResult::LOCK_PROGRAM_FAIL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Worker* worker = WorkerFactory::createWorker(backupWrapper.m_type, backupWrapper.m_iPosition);
|
|
|
|
|
if (nullptr == worker) {
|
|
|
|
|
emit sendEnvCheckResult(int(BackupResult::NO_FOUND_DEALCLASS));
|
|
|
|
|
return int(BackupResult::NO_FOUND_DEALCLASS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
worker->setParam(backupWrapper);
|
|
|
|
|
connect(worker, &Worker::checkResult, this, [&](int result) {
|
|
|
|
|
emit this->sendEnvCheckResult(result);
|
|
|
|
|
|
|
|
|
|
switch (result) {
|
|
|
|
|
case int(BackupResult::CHECK_ENV_SUCCESS) :
|
|
|
|
|
case int(BackupResult::GHOST_START_SUCCESS) :
|
2022-01-19 18:08:43 +08:00
|
|
|
|
case int(BackupResult::MKSQUASHFS_DO_SUCCESS) :
|
2022-01-18 17:35:26 +08:00
|
|
|
|
case int(BackupResult::START_CANCEL) :
|
2022-01-13 14:06:40 +08:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
this->finished();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
connect(worker, &Worker::progress, this, [&](int rate) {
|
|
|
|
|
emit this->progress(int(BackupState::WORKING), rate);
|
|
|
|
|
});
|
|
|
|
|
connect(worker, &Worker::workResult, this, [&] (bool result) {
|
|
|
|
|
emit this->sendGhostBackupResult(result);
|
|
|
|
|
this->finished();
|
|
|
|
|
});
|
|
|
|
|
worker->moveToThread(&workerThread);
|
|
|
|
|
connect(&workerThread, &MyThread::started, worker, &Worker::doWork);
|
|
|
|
|
connect(&workerThread, &MyThread::finished, worker, &Worker::deleteLater);
|
|
|
|
|
connect(&workerThread, &MyThread::cancelWork, worker, &Worker::cancel);
|
|
|
|
|
|
|
|
|
|
workerThread.start();
|
|
|
|
|
|
|
|
|
|
qDebug("MyBackupManager::ghostBackup invoke end");
|
|
|
|
|
return int(BackupResult::BACKUP_RESULT_INIT);
|
2021-08-06 10:20:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 控制面板调用的备份接口,重构暂时先兼容以前的老接口
|
2023-03-20 18:37:26 +08:00
|
|
|
|
* @param backupName 备份名称
|
|
|
|
|
* @param createNote 创建时备注
|
|
|
|
|
* @param incNote 增量备注
|
2021-08-06 10:20:03 +08:00
|
|
|
|
* @param frontUserName 前端用户名
|
|
|
|
|
* @param frontUid 前端用户id
|
|
|
|
|
*/
|
2023-03-20 18:37:26 +08:00
|
|
|
|
void MyBackupManager::autoBackUpForSystemUpdate_noreturn(const QString& backupName, const QString& createNote, const QString& incNote, const QString& frontUserName, int frontUid)
|
2021-08-06 10:20:03 +08:00
|
|
|
|
{
|
2022-01-18 17:35:26 +08:00
|
|
|
|
qDebug("MyBackupManager::autoBackUpForSystemUpdate_noreturn invoke begin");
|
2021-08-06 10:20:03 +08:00
|
|
|
|
|
2022-01-18 17:35:26 +08:00
|
|
|
|
if (m_isActive || !lock(frontUid)) {
|
|
|
|
|
emit sendStartBackupResult(int(BackupResult::LOCK_PROGRAM_FAIL));
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BackupWrapper backupWrapper;
|
|
|
|
|
backupWrapper.m_uuid = AUTO_BACKUP_UUID;
|
|
|
|
|
QString xmlPath = Utils::getSysRootPath() + BACKUP_XML_PATH;
|
|
|
|
|
xmlPath.replace("//", "/");
|
|
|
|
|
ParseBackupList parseXml(xmlPath);
|
|
|
|
|
ParseBackupList::BackupPoint backupPoint = parseXml.findBackupPointByUuid(backupWrapper.m_uuid);
|
2023-03-20 18:37:26 +08:00
|
|
|
|
if (backupName.isEmpty()) {
|
2022-01-18 17:35:26 +08:00
|
|
|
|
if (backupPoint.m_backupName.isEmpty())
|
|
|
|
|
backupWrapper.m_backupName = QDateTime::currentDateTime().toString("yy-MM-dd hh:mm:ss");
|
|
|
|
|
else
|
|
|
|
|
backupWrapper.m_backupName = backupPoint.m_backupName;
|
|
|
|
|
} else {
|
2023-03-20 18:37:26 +08:00
|
|
|
|
backupWrapper.m_backupName = backupName;
|
2022-01-18 17:35:26 +08:00
|
|
|
|
}
|
|
|
|
|
backupWrapper.m_backupPaths << "/";
|
|
|
|
|
backupWrapper.m_backupExcludePaths = Utils::getFromExcludePathsFile();
|
2022-07-28 18:49:35 +08:00
|
|
|
|
backupWrapper.m_backupExcludePaths << "/home";
|
|
|
|
|
backupWrapper.m_backupExcludePaths << "/root";
|
|
|
|
|
backupWrapper.m_backupExcludePaths << "/data/home";
|
|
|
|
|
backupWrapper.m_backupExcludePaths << "/data/root";
|
2022-01-18 17:35:26 +08:00
|
|
|
|
backupWrapper.m_type = BackupType::BACKUP_SYSTEM;
|
|
|
|
|
backupWrapper.m_iPosition = BackupPosition::LOCAL;
|
|
|
|
|
backupWrapper.m_frontUserName = frontUserName;
|
|
|
|
|
backupWrapper.m_frontUid = frontUid;
|
2023-03-20 18:37:26 +08:00
|
|
|
|
backupWrapper.m_note = createNote.isEmpty() ? incNote : createNote;
|
2022-01-18 17:35:26 +08:00
|
|
|
|
|
|
|
|
|
Worker* worker = WorkerFactory::createWorker(backupWrapper.m_type, backupWrapper.m_iPosition);
|
|
|
|
|
if (nullptr == worker) {
|
|
|
|
|
emit sendStartBackupResult(int(BackupResult::NO_FOUND_DEALCLASS));
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
worker->setParam(backupWrapper);
|
|
|
|
|
connect(worker, &Worker::checkResult, this, [&](int result) {
|
|
|
|
|
switch (result) {
|
|
|
|
|
case int(BackupResult::CHECK_ENV_SUCCESS) :
|
|
|
|
|
case int(BackupResult::MKSQUASHFS_START_SUCCESS) :
|
|
|
|
|
case int(BackupResult::BACKUP_START_SUCCESS) :
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2022-06-15 15:04:38 +08:00
|
|
|
|
emit this->sendStartBackupResult(result);
|
2022-01-18 17:35:26 +08:00
|
|
|
|
this->finished();
|
2023-03-20 18:37:26 +08:00
|
|
|
|
if (!Utils::isRunning(BACKUP_CLI_NAME)) {
|
2022-01-18 17:35:26 +08:00
|
|
|
|
this->umountBackupPartition();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
connect(worker, &Worker::progress, this, [&](int rate) {
|
|
|
|
|
emit this->sendRate(int(BackupState::WORKING), rate);
|
|
|
|
|
});
|
|
|
|
|
connect(worker, &Worker::workResult, this, [&] (bool result) {
|
|
|
|
|
emit this->sendBackupResult(result);
|
|
|
|
|
this->finished();
|
2023-03-20 18:37:26 +08:00
|
|
|
|
if (!Utils::isRunning(BACKUP_CLI_NAME)) {
|
2022-01-18 17:35:26 +08:00
|
|
|
|
this->umountBackupPartition();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
worker->moveToThread(&workerThread);
|
|
|
|
|
connect(&workerThread, &MyThread::started, worker, &Worker::doWork);
|
|
|
|
|
connect(&workerThread, &MyThread::finished, worker, &Worker::deleteLater);
|
|
|
|
|
|
|
|
|
|
workerThread.start();
|
|
|
|
|
|
|
|
|
|
qDebug("MyBackupManager::autoBackUpForSystemUpdate_noreturn invoke end");
|
2021-08-06 10:20:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-20 18:37:26 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief 控制面板调用的系统自动备份点还原接口
|
|
|
|
|
* @note 此接口是后来加的,系统更新模块意图在更新失败后直接调用还原接口进行还原
|
|
|
|
|
*/
|
|
|
|
|
void MyBackupManager::autoRestoreForSystemUpdate(const QString& frontUserName, int frontUid)
|
|
|
|
|
{
|
|
|
|
|
qDebug("MyBackupManager::autoRestoreForSystemUpdate invoke begin");
|
|
|
|
|
if (m_isActive || !lock(frontUid)) {
|
|
|
|
|
emit sendAutoRestoreResult(false);
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
Mount_backup_partition();
|
|
|
|
|
|
|
|
|
|
BackupWrapper backupWrapper;
|
|
|
|
|
backupWrapper.m_uuid = AUTO_BACKUP_UUID;
|
|
|
|
|
backupWrapper.m_type = BackupType::RESTORE_SYSTEM;
|
|
|
|
|
backupWrapper.m_iPosition = BackupPosition::LOCAL;
|
|
|
|
|
backupWrapper.m_frontUserName = frontUserName;
|
|
|
|
|
backupWrapper.m_frontUid = frontUid;
|
|
|
|
|
|
|
|
|
|
Worker* worker = WorkerFactory::createWorker(backupWrapper.m_type, backupWrapper.m_iPosition);
|
|
|
|
|
if (nullptr == worker) {
|
|
|
|
|
emit sendAutoRestoreResult(false);
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
worker->setParam(backupWrapper);
|
|
|
|
|
connect(worker, &Worker::checkResult, this, [&](int result) {
|
|
|
|
|
switch (result) {
|
|
|
|
|
case int(BackupResult::CHECK_ENV_SUCCESS) :
|
|
|
|
|
case int(BackupResult::RESTORE_START_SUCCESS) :
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
emit this->sendAutoRestoreResult(false);
|
|
|
|
|
this->finished();
|
|
|
|
|
if (!Utils::isRunning(BACKUP_CLI_NAME)) {
|
|
|
|
|
this->umountBackupPartition();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
connect(worker, &Worker::progress, this, [&](int rate) {
|
|
|
|
|
emit this->sendRate(int(BackupState::WORKING), rate);
|
|
|
|
|
});
|
|
|
|
|
connect(worker, &Worker::workResult, this, [&] (bool result) {
|
|
|
|
|
emit this->sendAutoRestoreResult(result);
|
|
|
|
|
this->finished();
|
|
|
|
|
if (!Utils::isRunning(BACKUP_CLI_NAME)) {
|
|
|
|
|
this->umountBackupPartition();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
worker->moveToThread(&workerThread);
|
|
|
|
|
connect(&workerThread, &MyThread::started, worker, &Worker::doWork);
|
|
|
|
|
connect(&workerThread, &MyThread::finished, worker, &Worker::deleteLater);
|
|
|
|
|
|
|
|
|
|
workerThread.start();
|
|
|
|
|
|
|
|
|
|
qDebug("MyBackupManager::autoRestoreForSystemUpdate invoke end");
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-06 10:20:03 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief 控制面板调用的获取系统备份接口
|
|
|
|
|
* @param state
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
QString MyBackupManager::getBackupCommentForSystemUpdate(QString& state)
|
|
|
|
|
{
|
2022-01-18 17:35:26 +08:00
|
|
|
|
QString xmlPath = Utils::getSysRootPath() + BACKUP_XML_PATH;
|
|
|
|
|
xmlPath.replace("//", "/");
|
|
|
|
|
ParseBackupList parseXml(xmlPath);
|
|
|
|
|
ParseBackupList::BackupPoint backupPoint = parseXml.findBackupPointByUuid(AUTO_BACKUP_UUID);
|
|
|
|
|
state = backupPoint.m_state;
|
|
|
|
|
return backupPoint.m_backupName;
|
2021-08-06 10:20:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 获取备份状态
|
|
|
|
|
* @param isActive,是否活动状态
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
int MyBackupManager::getBackupState(bool& isActive)
|
|
|
|
|
{
|
2021-11-09 15:44:26 +08:00
|
|
|
|
isActive = m_isActive;
|
|
|
|
|
return int(m_backupState);
|
2021-08-06 10:20:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 取消操作
|
|
|
|
|
*/
|
2021-12-28 16:51:35 +08:00
|
|
|
|
int MyBackupManager::cancel()
|
2021-08-06 10:20:03 +08:00
|
|
|
|
{
|
2021-11-15 13:36:00 +08:00
|
|
|
|
if (workerThread.isRunning())
|
2021-11-15 13:57:04 +08:00
|
|
|
|
emit workerThread.cancelWork();
|
2022-01-25 14:38:00 +08:00
|
|
|
|
else
|
|
|
|
|
return 1;
|
2021-11-15 13:36:00 +08:00
|
|
|
|
|
2021-08-06 10:20:03 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-28 16:51:35 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief 重启操作
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
int MyBackupManager::reboot()
|
|
|
|
|
{
|
|
|
|
|
return ::reboot(RB_AUTOBOOT);;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-06 10:20:03 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief 任务结束
|
|
|
|
|
*/
|
|
|
|
|
void MyBackupManager::finished()
|
|
|
|
|
{
|
|
|
|
|
if (workerThread.isRunning()) {
|
|
|
|
|
workerThread.quit();
|
|
|
|
|
workerThread.wait();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unlock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 锁定应用
|
|
|
|
|
* @param frontUid,锁定应用的用户id
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
bool MyBackupManager::lock(int frontUid)
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<spinlock_mutex> lock(m_mutex);
|
|
|
|
|
|
|
|
|
|
int lock_file_fd = Utils::lockProgram(frontUid);
|
|
|
|
|
if (lock_file_fd < 0) {
|
|
|
|
|
Utils::rmLockFile();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-10 16:28:29 +08:00
|
|
|
|
// 用于替换上面的文件锁的,对应需求8688
|
|
|
|
|
// if (!inhibit())
|
|
|
|
|
// return false;
|
|
|
|
|
inhibit();
|
|
|
|
|
|
2021-08-06 10:20:03 +08:00
|
|
|
|
m_fdLockFile = lock_file_fd;
|
|
|
|
|
m_backupState = BackupState::PREPARING;
|
|
|
|
|
m_isActive = true;
|
|
|
|
|
|
2021-10-26 09:24:05 +08:00
|
|
|
|
setKysecStatus(false);
|
|
|
|
|
|
2021-08-06 10:20:03 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-10 16:28:29 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief 阻止session关机用dbus新接口Inhibit来替换锁文件的方式
|
|
|
|
|
* Inhibti函数有四个参数,按顺序分别为:
|
|
|
|
|
what:需要阻止的是哪些过程,如关机、重启;可以输入的值有:”shutdown”(会同时阻止关机重启),”sleep”(会同时阻止睡眠休眠),需要同时阻止关机和睡眠可以输入 ”shutdown:sleep”
|
|
|
|
|
who:是谁要阻止,调用该接口的程序名,如”ukui-sessions”
|
|
|
|
|
why:阻止的原因,简单描述即可,如”package installing”
|
|
|
|
|
mode:阻止的方式;可输入的值有”block”(无限期阻止,等到主动取消阻止才会取消),”delay”(超时阻止,超时一段时间后不管阻止锁,直接执行)
|
|
|
|
|
建议使用”block”的方式阻止,加锁后记得解锁
|
|
|
|
|
可以使用同服务下ListInhibitors方法获取当前的inhibit锁,测试验证是否成功注册
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
bool MyBackupManager::inhibit()
|
|
|
|
|
{
|
|
|
|
|
QDBusMessage message;
|
|
|
|
|
message = QDBusMessage::createMethodCall("org.freedesktop.login1",
|
|
|
|
|
"/org/freedesktop/login1",
|
|
|
|
|
"org.freedesktop.login1.Manager",
|
|
|
|
|
QStringLiteral("Inhibit"));
|
|
|
|
|
QString why("The backup and restore tool is busy, please wait.");
|
|
|
|
|
// 区分中英文
|
|
|
|
|
QString locale = QLocale::system().name();
|
|
|
|
|
if (locale == "zh_CN") {
|
|
|
|
|
why = QString("备份还原工具正在忙碌,请稍等");
|
|
|
|
|
} else if (locale == "bo_CN") {
|
|
|
|
|
why = QString("གྲབས་ཉར་སོར་ཆུད་ལག་ཆ་བྲེལ་བཞིན་ཡོད་།ཏོག་ཙམ་སྒུག་རོགས་།");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// QVariantList({what, who, why, mode})
|
|
|
|
|
QVariantList args;
|
|
|
|
|
args << QString("shutdown:sleep");
|
|
|
|
|
args << QString("backup-daemon");
|
|
|
|
|
args << why;
|
|
|
|
|
args << QString("block");
|
|
|
|
|
message.setArguments(args);
|
|
|
|
|
|
|
|
|
|
QDBusPendingReply<QDBusUnixFileDescriptor> reply = QDBusConnection::systemBus().call(message);
|
|
|
|
|
|
|
|
|
|
if (!reply.isValid()) {
|
|
|
|
|
qDebug() << "inhibit faile!";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
QDBusUnixFileDescriptor inhibitFileDescriptor = reply.value();
|
|
|
|
|
inhibitFileDescriptor.swap(m_inhibitFileDescriptor);
|
|
|
|
|
qDebug() << "inhibit success!";
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MyBackupManager::uninhibit()
|
|
|
|
|
{
|
|
|
|
|
if (!m_inhibitFileDescriptor.isValid()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qDebug() << "uninhibit success!";
|
|
|
|
|
m_inhibitFileDescriptor = QDBusUnixFileDescriptor();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-06 10:20:03 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief 解锁应用
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
bool MyBackupManager::unlock()
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<spinlock_mutex> lock(m_mutex);
|
|
|
|
|
|
2021-10-26 09:24:05 +08:00
|
|
|
|
setKysecStatus(true);
|
|
|
|
|
|
2021-08-06 10:20:03 +08:00
|
|
|
|
Utils::unLockProgram(m_fdLockFile);
|
2022-06-10 16:28:29 +08:00
|
|
|
|
uninhibit();
|
2021-08-06 10:20:03 +08:00
|
|
|
|
|
|
|
|
|
m_fdLockFile = -1;
|
|
|
|
|
m_backupState = BackupState::BACKUP_STATE_INIT;
|
|
|
|
|
m_isActive = false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-10-26 09:24:05 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 设置安全状态
|
|
|
|
|
* @param status,true-开启;false-关闭
|
|
|
|
|
* @note status=true时不一定会开启,取决于关闭时安全状态;
|
|
|
|
|
* 使用此方法时注意:一定要先setKysecStatus(false);才能setKysecStatus(true);
|
|
|
|
|
* 使用场景:备份/还原等业务时先关闭安全保护,业务完成后再重启安全保护
|
|
|
|
|
*/
|
|
|
|
|
void MyBackupManager::setKysecStatus(bool status)
|
|
|
|
|
{
|
2022-10-19 17:02:42 +08:00
|
|
|
|
// openkylin平台中不开源安全模块,删掉安全模块的调用
|
|
|
|
|
Q_UNUSED(status)
|
2021-10-26 09:24:05 +08:00
|
|
|
|
}
|