165 lines
4.4 KiB
C++
165 lines
4.4 KiB
C++
#include "updatemanager.hpp"
|
|
#include <QRegularExpression>
|
|
|
|
namespace
|
|
{
|
|
constexpr int kMaxLogLines = 1000;
|
|
|
|
QString strip_ansi(const QString &text)
|
|
{
|
|
static const QRegularExpression ansi(QStringLiteral("\\x1b\\[[0-9;?]*[ -/]*[@-~]"));
|
|
QString result = text;
|
|
result.remove(ansi);
|
|
return result;
|
|
}
|
|
}
|
|
|
|
void UpdateManager::set_packages(const QVariantList& value)
|
|
{
|
|
const int oldCount = packageCount();
|
|
m_packages = value;
|
|
|
|
emit packagesChanged();
|
|
if (packageCount() != oldCount) emit packageCountChanged();
|
|
}
|
|
|
|
void UpdateManager::set_is_checking(bool value)
|
|
{
|
|
if (m_is_checking == value) return;
|
|
m_is_checking = value;
|
|
emit isCheckingChanged();
|
|
}
|
|
|
|
void UpdateManager::set_is_updating(bool value)
|
|
{
|
|
if (m_is_updating == value) return;
|
|
m_is_updating = value;
|
|
emit isUpdatingChanged();
|
|
}
|
|
|
|
void UpdateManager::on_process_finished(int exit_code, QProcess::ExitStatus status)
|
|
{
|
|
if (status == QProcess::CrashExit || (exit_code != 0 && exit_code != 2))
|
|
{
|
|
on_process_error(m_process.error());
|
|
return;
|
|
}
|
|
|
|
const QString output = QString::fromUtf8(m_process.readAllStandardOutput());
|
|
const QStringList lines = output.split(QLatin1Char('\n'), Qt::SkipEmptyParts);
|
|
|
|
QVariantList package_list;
|
|
package_list.reserve(lines.size());
|
|
|
|
for (const QString &line : lines)
|
|
{
|
|
const QStringList parts = line.split(QLatin1Char(' '), Qt::SkipEmptyParts);
|
|
if (parts.size() != 4 || parts.at(2) != QStringLiteral("->")) continue;
|
|
|
|
QVariantMap package;
|
|
package.insert(QStringLiteral("name"), parts.at(0));
|
|
package.insert(QStringLiteral("version"), parts.at(1) + QStringLiteral(" → ") + parts.at(3));
|
|
package.insert(QStringLiteral("updated"), false);
|
|
package.insert(QStringLiteral("updating"), false);
|
|
package_list.append(package);
|
|
}
|
|
|
|
set_packages(package_list);
|
|
set_is_checking(false);
|
|
}
|
|
|
|
void UpdateManager::on_process_error(QProcess::ProcessError error)
|
|
{
|
|
if (!m_is_checking) return;
|
|
|
|
QString reason;
|
|
if (error == QProcess::FailedToStart)
|
|
{
|
|
reason = QStringLiteral("failed to start: ") + m_process.errorString();
|
|
}
|
|
else
|
|
{
|
|
reason = QStringLiteral("failed, exit code %1").arg(m_process.exitCode());
|
|
const QString errText = QString::fromUtf8(m_process.readAllStandardError()).trimmed();
|
|
if (!errText.isEmpty()) reason += QStringLiteral(": ") + errText;
|
|
}
|
|
|
|
qWarning().noquote() << "checkupdates" << reason;
|
|
set_is_checking(false);
|
|
}
|
|
|
|
QString UpdateManager::create_ask_pass_helper(const QString& password)
|
|
{
|
|
m_askpass_dir = std::make_unique<QTemporaryDir>();
|
|
if (!m_askpass_dir->isValid())
|
|
{
|
|
qWarning() << "cannot create temporary dir for askpass";
|
|
m_askpass_dir.reset();
|
|
return {};
|
|
}
|
|
|
|
QFile::setPermissions(m_askpass_dir->path(), QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner);
|
|
|
|
const QString path = m_askpass_dir->filePath(QStringLiteral("askpass.sh"));
|
|
|
|
QString escaped = password;
|
|
escaped.replace(QLatin1Char('\''), QStringLiteral("'\\''"));
|
|
|
|
QFile script(path);
|
|
if (!script.open(QIODevice::WriteOnly | QIODevice::Truncate))
|
|
{
|
|
qWarning() << "cannot write askpass helper:" << script.errorString();
|
|
m_askpass_dir.reset();
|
|
return {};
|
|
}
|
|
|
|
script.write("#!/bin/sh\nprintf '%s\\n' '" + escaped.toUtf8() + "'\n");
|
|
script.close();
|
|
script.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner);
|
|
|
|
return path;
|
|
}
|
|
|
|
void UpdateManager::add_log_line(const QString& line)
|
|
{
|
|
const QString clean = strip_ansi(line).trimmed();
|
|
if (clean.isEmpty()) return;
|
|
|
|
qInfo().noquote() << clean;
|
|
|
|
if (m_update_log.size() >= kMaxLogLines) m_update_log.removeFirst();
|
|
m_update_log.append(clean);
|
|
emit updateLogChanged();
|
|
}
|
|
|
|
QProcessEnvironment UpdateManager::update_environment(const QString& askpass) const
|
|
{
|
|
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
|
|
|
env.insert(QStringLiteral("SUDO_ASKPASS"), askpass);
|
|
|
|
env.insert(QStringLiteral("SUDO_ASKPASS_REQUIRE"), QStringLiteral("force"));
|
|
|
|
env.insert(QStringLiteral("LC_ALL"), QStringLiteral("C"));
|
|
|
|
env.insert(QStringLiteral("COLUMNS"), QStringLiteral("300"));
|
|
|
|
return env;
|
|
}
|
|
|
|
void UpdateManager::begin_package_update()
|
|
{
|
|
QVariantList list;
|
|
list.reserve(m_packages.size());
|
|
|
|
for (const QVariant &item : m_packages)
|
|
{
|
|
QVariantMap package = item.toMap();
|
|
package.insert(QStringLiteral("updated"), false);
|
|
package.insert(QStringLiteral("updating"), true);
|
|
list.append(package);
|
|
}
|
|
|
|
set_packages(list);
|
|
}
|