2021-08-19 19:24:49 +08:00
|
|
|
|
#include "calcbackupsize.h"
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
|
|
CalcBackupSize::CalcBackupSize(QObject* parent) :
|
|
|
|
|
QObject(parent),
|
|
|
|
|
m_process(new QProcess(this))
|
|
|
|
|
{
|
2021-08-24 18:08:18 +08:00
|
|
|
|
connect(m_process, &QProcess::readyRead, this, [&]() {
|
|
|
|
|
parseResult();
|
|
|
|
|
});
|
|
|
|
|
|
2021-08-19 19:24:49 +08:00
|
|
|
|
connect(m_process, &QProcess::readyReadStandardError, this, [&]() {
|
|
|
|
|
QByteArray err = m_process->readAllStandardError();
|
|
|
|
|
qCritical("backup process error: %s", err.data());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
connect(m_process, &QProcess::errorOccurred, this, [&](QProcess::ProcessError error) {
|
2021-08-26 20:12:48 +08:00
|
|
|
|
Q_UNUSED(error)
|
2021-08-19 19:24:49 +08:00
|
|
|
|
m_process->kill();
|
|
|
|
|
});
|
|
|
|
|
|
2021-08-24 18:08:18 +08:00
|
|
|
|
connect(m_process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processFinish(int, QProcess::ExitStatus)));
|
2021-08-19 19:24:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CalcBackupSize::~CalcBackupSize()
|
|
|
|
|
{
|
2021-08-24 18:08:18 +08:00
|
|
|
|
if (!m_process && m_process->state() != QProcess::NotRunning) {
|
|
|
|
|
m_process->kill();
|
|
|
|
|
}
|
2021-08-19 19:24:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 计算备份大小,单位字节
|
|
|
|
|
* @param args rsync参数列表
|
|
|
|
|
* @param block 是否阻塞计算模式。默认true——阻塞计算模式
|
|
|
|
|
* @return block为true时返回值为待备份数据大小,单位字节;block为false时,默认返回0;
|
|
|
|
|
*/
|
2021-08-24 18:08:18 +08:00
|
|
|
|
qint64 CalcBackupSize::start(QStringList args, bool block)
|
2021-08-19 19:24:49 +08:00
|
|
|
|
{
|
2021-08-24 18:08:18 +08:00
|
|
|
|
qDebug() << "CalcBackupSize::calcBackupSize invoke begin";
|
|
|
|
|
|
2021-08-19 19:24:49 +08:00
|
|
|
|
QString cmd("rsync ");
|
|
|
|
|
for (const QString& item : args) {
|
|
|
|
|
cmd += " ";
|
|
|
|
|
cmd += item;
|
|
|
|
|
}
|
|
|
|
|
qDebug() << cmd;
|
|
|
|
|
|
|
|
|
|
m_size = 0;
|
|
|
|
|
|
|
|
|
|
m_process->start("rsync", args);
|
|
|
|
|
if (!m_process->waitForStarted()) {
|
|
|
|
|
qCritical("rsync start failed");
|
|
|
|
|
return m_size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (block) {
|
|
|
|
|
m_process->waitForFinished();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-24 18:08:18 +08:00
|
|
|
|
qDebug() << "CalcBackupSize::calcBackupSize invoke end";
|
2021-08-19 19:24:49 +08:00
|
|
|
|
return m_size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-08-24 18:08:18 +08:00
|
|
|
|
* @brief 解析process的输出结果
|
2021-08-19 19:24:49 +08:00
|
|
|
|
*/
|
2021-08-24 18:08:18 +08:00
|
|
|
|
void CalcBackupSize::parseResult()
|
2021-08-19 19:24:49 +08:00
|
|
|
|
{
|
|
|
|
|
// 解析结果,结果内容样例如下:
|
|
|
|
|
/*
|
2021-08-24 18:08:18 +08:00
|
|
|
|
Number of files: 256,274 (reg: 207,101, dir: 23,764, link: 25,407, special: 2)
|
|
|
|
|
Number of created files: 256,274 (reg: 207,101, dir: 23,764, link: 25,407, special: 2)
|
2021-08-19 19:24:49 +08:00
|
|
|
|
Number of deleted files: 0
|
2021-08-24 18:08:18 +08:00
|
|
|
|
Number of regular files transferred: 207,101
|
|
|
|
|
Total file size: 12,160,363,663 bytes
|
|
|
|
|
Total transferred file size: 12,159,780,794 bytes
|
2021-08-19 19:24:49 +08:00
|
|
|
|
Literal data: 0 bytes
|
|
|
|
|
Matched data: 0 bytes
|
2021-08-24 18:08:18 +08:00
|
|
|
|
File list size: 786,254
|
2021-08-19 19:24:49 +08:00
|
|
|
|
File list generation time: 0.001 seconds
|
|
|
|
|
File list transfer time: 0.000 seconds
|
2021-08-24 18:08:18 +08:00
|
|
|
|
Total bytes sent: 8,273,515
|
|
|
|
|
Total bytes received: 794,093
|
|
|
|
|
|
|
|
|
|
sent 8,273,515 bytes received 794,093 bytes 1,209,014.40 bytes/sec
|
|
|
|
|
total size is 12,160,363,663 speedup is 1,341.08 (DRY RUN)
|
2021-08-19 19:24:49 +08:00
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
QString out(m_process->readAll());
|
|
|
|
|
QStringList lines = out.split("\n");
|
2022-03-10 15:59:33 +08:00
|
|
|
|
qDebug() << out;
|
2022-03-11 17:08:43 +08:00
|
|
|
|
m_size = 0;
|
2021-08-19 19:24:49 +08:00
|
|
|
|
for (QString& line : lines) {
|
|
|
|
|
// 获取文件夹数目
|
2022-03-11 17:08:43 +08:00
|
|
|
|
if (line.startsWith("Number of files:")) {
|
|
|
|
|
int indexOfDir = line.indexOf("dir: ");
|
|
|
|
|
indexOfDir += 5;
|
|
|
|
|
int indexOfDirEnd = line.indexOf(QRegularExpression("[ )]"), indexOfDir);
|
|
|
|
|
int numOfDirs = line.mid(indexOfDir, indexOfDirEnd-indexOfDir).replace(",","").trimmed().toInt();
|
|
|
|
|
// 每个目录下还都有.和..,故总数还要*3
|
|
|
|
|
numOfDirs *= 3;
|
|
|
|
|
// ext4格式的目录本身要占用4K空间(4096)
|
|
|
|
|
m_size += numOfDirs * 4096;
|
|
|
|
|
}
|
2021-08-24 18:08:18 +08:00
|
|
|
|
if (line.startsWith("Total transferred file size: ")) {
|
2021-08-26 20:12:48 +08:00
|
|
|
|
m_size += line.replace("Total transferred file size: ", "").replace("bytes", "").replace(",","").trimmed().toLongLong();
|
2021-08-19 19:24:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-24 18:08:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief process结束
|
|
|
|
|
* @param exitCode
|
|
|
|
|
*/
|
|
|
|
|
void CalcBackupSize::processFinish(int exitCode, QProcess::ExitStatus)
|
|
|
|
|
{
|
|
|
|
|
qDebug() << "CalcBackupSize::getCalcResult invoke begin";
|
2021-08-26 20:12:48 +08:00
|
|
|
|
Q_UNUSED(exitCode)
|
2021-08-24 18:08:18 +08:00
|
|
|
|
|
2021-11-20 12:09:26 +08:00
|
|
|
|
if (exitCode == QProcess::NormalExit)
|
|
|
|
|
parseResult();
|
|
|
|
|
|
2021-08-24 18:08:18 +08:00
|
|
|
|
emit finished(m_size);
|
2021-08-19 19:24:49 +08:00
|
|
|
|
|
2021-08-24 18:08:18 +08:00
|
|
|
|
qDebug() << "CalcBackupSize::getCalcResult invoke end";
|
2021-08-19 19:24:49 +08:00
|
|
|
|
}
|