yhkylin-backup-tools/backup-daemon/myprocess/mountbackupprocess.cpp

120 lines
2.9 KiB
C++
Raw Normal View History

2022-11-01 10:40:05 +08:00
#ifndef MOUNTBACKUPPROCESS_CPP
#define MOUNTBACKUPPROCESS_CPP
#include <QFile>
#include <QtDebug>
#include "mountbackupprocess.h"
#include "../../common/mydefine.h"
#include "../../common/utils.h"
MountBackupProcess::MountBackupProcess(QObject* parent)
: QObject(parent)
, m_p(new QProcess(this))
{
connect(m_p, &QProcess::readyReadStandardError, this, [&]() {
QByteArray err = m_p->readAllStandardError();
qCritical("%s", err.data());
});
}
bool MountBackupProcess::Do(const QString& diskUuid)
{
QString rootPath(Utils::getSysRootPath());
// 若备份路径下的xml已经存在则说明已经挂载过了
QString backupXmlPath = rootPath + BACKUP_XML_PATH;
backupXmlPath.replace("//", "/");
QFile file(backupXmlPath);
if (file.exists())
return true;
QStringList arguments;
QString backupPartion = rootPath + BACKUP_PATH;
backupPartion.replace("//","/");
arguments << QString("-o") << QString("rw") << QString("/dev/disk/by-uuid/%1").arg(diskUuid) << backupPartion;
m_p->start("mount", arguments);
if (!m_p->waitForStarted()) {
qCritical("mount rw /backup process start failed!");
return false;
}
if (!m_p->waitForFinished()) {
qCritical("mount rw backup process end failed!");
return false;
}
return true;
}
bool MountBackupProcess::umountBackupPartition()
{
QStringList arguments;
arguments << QString("/backup");
m_p->start("umount", arguments);
if (!m_p->waitForStarted()) {
qCritical("umount /backup process start failed!");
return false;
}
if (!m_p->waitForFinished()) {
qCritical("umount /backup process end failed!");
return false;
}
return true;
}
/**
* @brief
* @param mountPath
* @return
*/
bool MountBackupProcess::umount(const QString& mountPath)
{
QStringList arguments;
arguments << mountPath;
m_p->start("umount", arguments);
if (!m_p->waitForStarted()) {
qCritical() << tr("umount %1 process start failed!").arg(mountPath);
return false;
}
if (!m_p->waitForFinished()) {
qCritical() << tr("umount %1 process end failed!").arg(mountPath);
return false;
}
return true;
}
/**
* @brief
* @param source
* @param target
* @return
*/
bool MountBackupProcess::mount(const QString& source, const QString& target, const QString& options)
{
QStringList arguments;
if (!options.isEmpty())
arguments << options;
arguments << source;
arguments << target;
m_p->start("mount", arguments);
if (!m_p->waitForStarted()) {
qCritical("mount process start failed!");
return false;
}
if (!m_p->waitForFinished()) {
qCritical("mount process end failed!");
return false;
}
return true;
}
#endif // MOUNTBACKUPPROCESS_CPP