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

213 lines
4.6 KiB
C++
Raw Normal View History

2021-08-06 10:20:03 +08:00
/**
* brief moveToThread中去执行
*/
#include <mutex>
#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()
{
MyMountProxy mymount;
return int(mymount.mountBackupPartition());
}
/**
* @brief
* @param backupWrapper
* @return
*/
int MyBackupManager::checkEnv(const BackupWrapper& backupWrapper)
{
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, &MyBackupManager::sendEnvCheckResult);
connect(worker, &Worker::checkResult, this, &MyBackupManager::finished);
worker->moveToThread(&workerThread);
connect(&workerThread, &QThread::started, worker, &Worker::checkEnv);
connect(&workerThread, &QThread::finished, worker, &Worker::deleteLater);
return int(BackupResult::BACKUP_RESULT_INIT);
}
/**
* @brief
* @param backupWrapper
* @return
*/
int MyBackupManager::goBackup(const BackupWrapper& backupWrapper)
{
Q_UNUSED(backupWrapper)
return 0;
}
/**
* @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)
{
Q_UNUSED(isActive)
return 0;
}
/**
* @brief
*/
int MyBackupManager::cancel()
{
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;
return true;
}
/**
* @brief
* @return bool
*/
bool MyBackupManager::unlock()
{
std::lock_guard<spinlock_mutex> lock(m_mutex);
Utils::unLockProgram(m_fdLockFile);
m_fdLockFile = -1;
m_backupState = BackupState::BACKUP_STATE_INIT;
m_isActive = false;
return true;
}