2021-08-06 10:20:03 +08:00
|
|
|
|
/**
|
|
|
|
|
* brief 程序比较小,将接口层和控制层合到一起;主要做一些任务分发,状态监控等一些简单工作;复杂的耗时的逻辑放到任务中moveToThread中去执行
|
|
|
|
|
*/
|
|
|
|
|
|
2021-10-26 09:24:05 +08:00
|
|
|
|
#include <QTimer>
|
2021-08-06 10:20:03 +08:00
|
|
|
|
#include <mutex>
|
2021-10-26 09:24:05 +08:00
|
|
|
|
#include <kysec/status.h>
|
2021-08-06 10:20:03 +08:00
|
|
|
|
#include "mybackupmanager.h"
|
|
|
|
|
#include "../common/utils.h"
|
|
|
|
|
#include "mymountproxy.h"
|
|
|
|
|
#include "workerfactory.h"
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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;
|
|
|
|
|
}
|
|
|
|
|
|
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-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);
|
|
|
|
|
if (result != int(BackupResult::CHECK_ENV_SUCCESS)) {
|
|
|
|
|
this->finished();
|
|
|
|
|
}
|
|
|
|
|
});
|
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) {
|
|
|
|
|
emit this->sendBackupResult(result);
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
return int(BackupResult::BACKUP_RESULT_INIT);
|
2021-08-06 10:20:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 还原
|
|
|
|
|
* @param backupWrapper
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
int MyBackupManager::goRestore(const BackupWrapper& backupWrapper)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(backupWrapper)
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 删除备份
|
|
|
|
|
* @param backupWrapper
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
int MyBackupManager::deleteBackupPoint(const BackupWrapper& backupWrapper)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(backupWrapper)
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief ghost镜像
|
|
|
|
|
* @param backupWrapper
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
int MyBackupManager::ghostBackup(const BackupWrapper& backupWrapper)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(backupWrapper)
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 控制面板调用的备份接口,重构暂时先兼容以前的老接口
|
|
|
|
|
* @param autobackup_name 备份名称
|
|
|
|
|
* @param create_note 创建时备注
|
|
|
|
|
* @param inc_note 增量备注
|
|
|
|
|
* @param frontUserName 前端用户名
|
|
|
|
|
* @param frontUid 前端用户id
|
|
|
|
|
*/
|
|
|
|
|
void MyBackupManager::autoBackUpForSystemUpdate_noreturn(const QString& autobackup_name, const QString& create_note, const QString& inc_note, const QString& frontUserName, int frontUid)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(autobackup_name)
|
|
|
|
|
Q_UNUSED(create_note)
|
|
|
|
|
Q_UNUSED(inc_note)
|
|
|
|
|
Q_UNUSED(frontUserName)
|
|
|
|
|
Q_UNUSED(frontUid)
|
|
|
|
|
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 控制面板调用的获取系统备份接口
|
|
|
|
|
* @param state
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
QString MyBackupManager::getBackupCommentForSystemUpdate(QString& state)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(state)
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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-08-24 18:08:18 +08:00
|
|
|
|
int MyBackupManager::cancel(const BackupWrapper& backupWrapper)
|
2021-08-06 10:20:03 +08:00
|
|
|
|
{
|
2021-08-26 20:12:48 +08:00
|
|
|
|
Q_UNUSED(backupWrapper)
|
2021-11-15 13:36:00 +08:00
|
|
|
|
|
|
|
|
|
if (workerThread.isRunning())
|
2021-11-15 13:57:04 +08:00
|
|
|
|
emit workerThread.cancelWork();
|
2021-11-15 13:36:00 +08:00
|
|
|
|
|
2021-08-06 10:20:03 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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);
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
if (status) {
|
|
|
|
|
if (m_bOpenKysec)
|
|
|
|
|
Utils::setKysecStatus(true);
|
|
|
|
|
m_bOpenKysec = false;
|
|
|
|
|
|
|
|
|
|
if (m_bStartKysecDeamon)
|
|
|
|
|
Utils::setKysecDaemon(true);
|
|
|
|
|
m_bStartKysecDeamon = false;
|
|
|
|
|
} else {
|
|
|
|
|
if (KYSEC_STATUS_ENABLED == kysec_getstatus()) {
|
|
|
|
|
// 安全保护已开启
|
|
|
|
|
m_bOpenKysec = true;
|
|
|
|
|
Utils::setKysecStatus(false);
|
|
|
|
|
} else {
|
|
|
|
|
m_bOpenKysec = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(Utils::isRunning("kysec-sync-daemon")){
|
|
|
|
|
m_bStartKysecDeamon = true;
|
|
|
|
|
Utils::setKysecDaemon(false);
|
|
|
|
|
} else {
|
|
|
|
|
m_bStartKysecDeamon = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|