yhkylin-backup-tools/backup-daemon/mybackupmanager.cpp

325 lines
7.9 KiB
C++
Raw Normal View History

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 00
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 00
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();
}
});
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
{
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 frontUidid
* @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;
}
}
}