Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix pigz special character problem #193

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions 3rdparty/libpigzplugin/libpigzplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ PluginFinishType LibPigzPlugin::addFiles(const QList<FileEntry> &files, const Co
m_isProcessKilled = false;

// 压缩命令tar -cvf - filename | pigz -p 2 -5> filename.tar.gz
m_process = new QProcess;
m_process = new KPtyProcess;
m_process->setPtyChannels(KPtyProcess::StdinChannel);
m_process->setOutputChannelMode(KProcess::MergedChannels);
m_process->setNextOpenMode(QIODevice::ReadWrite | QIODevice::Unbuffered | QIODevice::Text);
//m_process->setProcessChannelMode(QProcess::MergedChannels);

QString strFileName;
Expand Down Expand Up @@ -116,19 +119,34 @@ PluginFinishType LibPigzPlugin::addFiles(const QList<FileEntry> &files, const Co
strTmparchive.replace(strold[n], strnew[n]);
}

QString command = QString("tar --use-compress-program=\"pigz -k -p%1 -%2\" -cvf %3 %4")
.arg(options.iCPUTheadNum)
.arg(options.iCompressionLevel)
.arg(strTmparchive)
.arg(strFileName);
QString strTemp = QString("tar cvfz - %1 | pigz -p %2 -%3 > %4").arg(strFileName).arg(options.iCPUTheadNum).arg(options.iCompressionLevel).arg(strTmparchive);
if (0 == options.iCompressionLevel) {
strTemp = QString("tar cvf - %1 | pigz -p %2 -%3 > %4").arg(strFileName).arg(options.iCPUTheadNum).arg(options.iCompressionLevel).arg(strTmparchive);
}

m_scriptPath = QDir::tempPath() + "/tempScript_" + QString::number(QDateTime::currentDateTime().toMSecsSinceEpoch()) + ".sh";
QFile scriptFile(m_scriptPath);
if (scriptFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&scriptFile);
out << "#!/bin/bash\n";

out << strTemp << "\n";
scriptFile.close();
QProcess::execute("chmod", { "+x", m_scriptPath });
}
m_process->setProgram(m_scriptPath, QStringList());
connect(m_process, &QProcess::readyReadStandardOutput, this, [=] {
readStdout();
});

connect(m_process, &QProcess::readyReadStandardError, this, [=] {
QByteArray errorOutput = m_process->readAllStandardError();
qDebug() << "Error Output:" << QString::fromLocal8Bit(errorOutput);
});

connect(m_process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processFinished(int, QProcess::ExitStatus)));

m_process->start(command, QIODevice::ReadWrite | QIODevice::Unbuffered | QIODevice::Text);
m_process->start();

if (m_process->waitForStarted()) {
m_childProcessId.clear();
Expand Down Expand Up @@ -259,6 +277,9 @@ void LibPigzPlugin::deleteProcess()
m_process->blockSignals(true); // delete m_process之前需要断开所有m_process信号,防止重复处理
delete m_process;
m_process = nullptr;
if (!m_scriptPath.isEmpty()) {
QFile::remove(m_scriptPath);
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion 3rdparty/libpigzplugin/libpigzplugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,14 @@ private Q_SLOTS:
void processFinished(int exitCode, QProcess::ExitStatus exitStatus);

private:
QProcess *m_process = nullptr; // 工作进程
KPtyProcess *m_process = nullptr; // 工作进程
bool m_isProcessKilled = false; // 进程已经结束
QByteArray m_stdOutData; // 存储命令行输出数据
qint64 m_processId; // 进程Id
QVector<qint64> m_childProcessId; // 压缩tar.gz文件的子进程Id
qlonglong m_qTotalSize; // 源文件总大小
QTimer *m_timer = nullptr;
QString m_scriptPath; // 脚本路径
};

#endif // libPigzPlugin_H
Loading