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

93 lines
2.5 KiB
C++
Raw Normal View History

2021-11-20 12:09:26 +08:00
#include "mksquashfsprocess.h"
#include <QDebug>
MkSquashFSProcess::MkSquashFSProcess(QObject* parent)
: QObject(parent)
, m_p(new QProcess(this))
, m_syncProcess(new QProcess(this))
{
connect(m_p, &QProcess::readyRead, this, [&]() {
QString str = QString(m_p->readAll());
2021-11-20 17:48:23 +08:00
qDebug() << str;
2021-11-20 12:09:26 +08:00
if (str.contains("]") && str.contains("%")) {
if (str.split("%").at(0).length() < 3)
return;
int tmpRate = str.split("%").at(0).right(3).toInt();
if (m_currentRate == tmpRate)
return;
m_currentRate = tmpRate;
emit progress(m_currentRate);
}
});
connect(m_p, &QProcess::readyReadStandardError, this, [&]() {
QByteArray err = m_p->readAllStandardError();
qCritical() << err;
});
connect(m_p, &QProcess::errorOccurred, this, [&](QProcess::ProcessError error) {
Q_UNUSED(error)
m_p->kill();
});
connect(m_p, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(mksquashfs_finished(int, QProcess::ExitStatus)));
connect(m_syncProcess, &QProcess::errorOccurred, this, [&](QProcess::ProcessError error) {
Q_UNUSED(error)
m_syncProcess->kill();
});
connect(m_syncProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(sync_finished(int, QProcess::ExitStatus)));
}
MkSquashFSProcess::~MkSquashFSProcess()
{
if (!m_p && m_p->state() != QProcess::NotRunning) {
m_p->kill();
}
if (!m_syncProcess && m_syncProcess->state() != QProcess::NotRunning) {
m_syncProcess->kill();
}
}
bool MkSquashFSProcess::start(const QStringList &args)
{
2021-11-20 17:48:23 +08:00
QString cmd("mksquashfs ");
for (const QString& item : args) {
cmd += " ";
cmd += item;
}
qDebug() << cmd;
2021-11-20 12:09:26 +08:00
m_p->start("mksquashfs", args);
if (!m_p->waitForStarted()) {
qCritical("mksquashfs start failed!");
return false;
}
return true;
}
void MkSquashFSProcess::mksquashfs_finished(int exitCode, QProcess::ExitStatus)
{
if (exitCode == QProcess::NormalExit) {
m_syncProcess->start("sync");
if (!m_syncProcess->waitForStarted()) {
emit finished(false);
}
} else {
emit finished(false);
}
}
void MkSquashFSProcess::sync_finished(int exitCode, QProcess::ExitStatus)
{
if (exitCode == QProcess::NormalExit) {
2021-11-20 17:48:23 +08:00
emit progress(100);
2021-11-20 12:09:26 +08:00
emit finished(true);
} else {
emit finished(false);
}
}