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

591 lines
18 KiB
C++
Raw Normal View History

2022-11-01 10:40:05 +08:00
/**
* brief moveToThread中去执行
*/
#include <QTimer>
#include <QDateTime>
#include <QDBusArgument>
#include <QDBusMessage>
#include <QDBusPendingReply>
#include <QDBusMessage>
#include <QDBusUnixFileDescriptor>
#include <QDBusConnection>
#include <mutex>
#include <sys/reboot.h>
#include <kysec/status.h>
#include "mybackupmanager.h"
#include "../common/utils.h"
#include "mymountproxy.h"
#include "workerfactory.h"
#include "parsebackuplist.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()
{
m_needUmount = false;
MyMountProxy mymount;
return int(mymount.mountBackupPartition());
}
/**
* @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;
}
/**
* @brief
* @param backupWrapper
* @return 00
*/
int MyBackupManager::checkEnv(const BackupWrapper& backupWrapper)
{
qDebug("MyBackupManager::checkEnv 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);
this->finished();
});
worker->moveToThread(&workerThread);
connect(&workerThread, &MyThread::started, worker, &Worker::checkEnv);
connect(&workerThread, &MyThread::finished, worker, &Worker::deleteLater);
workerThread.start();
qDebug("MyBackupManager::checkEnv invoke end");
return int(BackupResult::BACKUP_RESULT_INIT);
}
/**
* @brief
* @param backupWrapper
* @return 00
*/
int MyBackupManager::goBackup(const BackupWrapper& backupWrapper)
{
qDebug("MyBackupManager::goBackup 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::MKSQUASHFS_START_SUCCESS) :
case int(BackupResult::BACKUP_START_SUCCESS) :
case int(BackupResult::START_CANCEL) :
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->backupFinished(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::goBackup invoke end");
return int(BackupResult::BACKUP_RESULT_INIT);
}
/**
* @brief
* @param backupWrapper
* @return
*/
int MyBackupManager::goRestore(const BackupWrapper& backupWrapper)
{
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) :
case int(BackupResult::RESTORE_START_SUCCESS) :
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->sendRestoreResult(result);
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);
}
/**
* @brief
* @param backupWrapper
* @return
*/
int MyBackupManager::deleteBackupPoint(const BackupWrapper& backupWrapper)
{
qDebug("MyBackupManager::deleteBackupPoint 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::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();
qDebug("MyBackupManager::deleteBackupPoint invoke end");
return int(BackupResult::BACKUP_RESULT_INIT);
}
/**
* @brief ghost镜像
* @param backupWrapper
* @return
*/
int MyBackupManager::ghostBackup(const BackupWrapper& backupWrapper)
{
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) :
case int(BackupResult::MKSQUASHFS_DO_SUCCESS) :
case int(BackupResult::START_CANCEL) :
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);
}
/**
* @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)
{
qDebug("MyBackupManager::autoBackUpForSystemUpdate_noreturn invoke begin");
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);
if (autobackup_name.isEmpty()) {
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 {
backupWrapper.m_backupName = autobackup_name;
}
backupWrapper.m_backupPaths << "/";
backupWrapper.m_backupExcludePaths = Utils::getFromExcludePathsFile();
backupWrapper.m_backupExcludePaths << "/home";
backupWrapper.m_backupExcludePaths << "/root";
backupWrapper.m_backupExcludePaths << "/data/home";
backupWrapper.m_backupExcludePaths << "/data/root";
backupWrapper.m_type = BackupType::BACKUP_SYSTEM;
backupWrapper.m_iPosition = BackupPosition::LOCAL;
backupWrapper.m_frontUserName = frontUserName;
backupWrapper.m_frontUid = frontUid;
backupWrapper.m_note = create_note.isEmpty() ? inc_note : create_note;
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:
emit this->sendStartBackupResult(result);
this->finished();
if (!Utils::isRunning("kybackup")) {
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();
if (!Utils::isRunning("kybackup")) {
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");
}
/**
* @brief
* @param state
* @return
*/
QString MyBackupManager::getBackupCommentForSystemUpdate(QString& state)
{
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;
}
/**
* @brief
* @param isActive
* @return
*/
int MyBackupManager::getBackupState(bool& isActive)
{
isActive = m_isActive;
return int(m_backupState);
}
/**
* @brief
*/
int MyBackupManager::cancel()
{
if (workerThread.isRunning())
emit workerThread.cancelWork();
else
return 1;
return 0;
}
/**
* @brief
* @return
*/
int MyBackupManager::reboot()
{
return ::reboot(RB_AUTOBOOT);;
}
/**
* @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;
}
// 用于替换上面的文件锁的对应需求8688
// if (!inhibit())
// return false;
inhibit();
m_fdLockFile = lock_file_fd;
m_backupState = BackupState::PREPARING;
m_isActive = true;
setKysecStatus(false);
return true;
}
/**
* @brief session关机用dbus新接口Inhibit来替换锁文件的方式
* Inhibti函数有四个参数
what:shutdown()sleep shutdown:sleep
who:ukui-sessions
whypackage 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();
}
/**
* @brief
* @return bool
*/
bool MyBackupManager::unlock()
{
std::lock_guard<spinlock_mutex> lock(m_mutex);
setKysecStatus(true);
Utils::unLockProgram(m_fdLockFile);
uninhibit();
m_fdLockFile = -1;
m_backupState = BackupState::BACKUP_STATE_INIT;
m_isActive = false;
return true;
}
/**
* @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);
// 开启安全防护
// QProcess::execute("systemctl start kysec-init.service");
} else {
m_bOpenKysec = false;
}
if(Utils::isRunning("kysec-sync-daemon")){
m_bStartKysecDeamon = true;
Utils::setKysecDaemon(false);
// 停止安全防护
// QProcess::execute("systemctl stop kysec-init.service");
} else {
m_bStartKysecDeamon = false;
}
}
}