Commit 72999509 authored by Ahmad Samir's avatar Ahmad Samir
Browse files

ChecksumsUtils: split the implementation to .cpp file

parent d337737e
Pipeline #178118 passed with stage
in 2 minutes and 59 seconds
......@@ -184,6 +184,7 @@ set(_kleopatra_SRCS
crypto/signemailcontroller.cpp
crypto/createchecksumscontroller.cpp
crypto/verifychecksumscontroller.cpp
crypto/checksumsutils_p.cpp
crypto/gui/wizard.cpp
crypto/gui/wizardpage.cpp
......
/* -*- mode: c++; c-basic-offset:4 -*-
crypto/checksumsutils_p.cpp
This file is part of Kleopatra, the KDE keymanager
SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "checksumsutils_p.h"
#include <Libkleo/ChecksumDefinition>
#include "kleopatra_debug.h"
#include <QFile>
#include <QTextStream>
std::vector<QRegularExpression> ChecksumsUtils::get_patterns(const std::vector<std::shared_ptr<Kleo::ChecksumDefinition>> &checksumDefinitions)
{
std::vector<QRegularExpression> result;
for (const auto &cd : checksumDefinitions) {
if (!cd) {
continue;
}
const QStringList &patterns = cd->patterns();
result.reserve(result.size() + patterns.size());
std::transform(patterns.cbegin(), patterns.cend(), std::back_inserter(result), [](const QString &pattern) {
return QRegularExpression(QRegularExpression::anchoredPattern(pattern), s_regex_cs);
});
}
return result;
}
static QString decode(const QString &encoded)
{
QString decoded;
decoded.reserve(encoded.size());
bool shift = false;
for (const QChar ch : encoded)
if (shift) {
switch (ch.toLatin1()) {
case '\\': decoded += QLatin1Char('\\'); break;
case 'n': decoded += QLatin1Char('\n'); break;
default:
qCDebug(KLEOPATRA_LOG) << "invalid escape sequence" << '\\' << ch << "(interpreted as '" << ch << "')";
decoded += ch;
break;
}
shift = false;
} else {
if (ch == QLatin1Char('\\')) {
shift = true;
} else {
decoded += ch;
}
}
return decoded;
}
std::vector<ChecksumsUtils::File> ChecksumsUtils::parse_sum_file(const QString &fileName)
{
std::vector<File> files;
QFile f(fileName);
if (!f.open(QIODevice::ReadOnly)) {
return {};
}
QTextStream s(&f);
static const QRegularExpression rx(QRegularExpression::anchoredPattern(uR"((\\?)([a-f0-9A-F]+) ([ *])([^\\n]+)\\n*)"));
while (!s.atEnd()) {
const QString line = s.readLine();
QRegularExpressionMatch match = rx.match(line);
if (!match.hasMatch()) {
continue;
}
Q_ASSERT(!match.capturedView(4).endsWith(QLatin1Char('\n')));
const File file = {
match.capturedView(1) == QLatin1Char('\\') ? decode(match.captured(4)) : match.captured(4),
match.capturedView(2).toLatin1(),
match.capturedView(3) == QLatin1Char('*'),
};
files.push_back(file);
}
return files;
}
std::shared_ptr<Kleo::ChecksumDefinition> ChecksumsUtils::filename2definition(const QString &fileName,
const std::vector<std::shared_ptr<Kleo::ChecksumDefinition>> &checksumDefinitions)
{
auto matchFileName = [&fileName](const std::shared_ptr<Kleo::ChecksumDefinition> &cd) {
if (!cd) {
return false;
}
const QStringList &patterns = cd->patterns();
return std::any_of(patterns.cbegin(), patterns.cend(), [&fileName](const QString &pattern) {
const QRegularExpression re(QRegularExpression::anchoredPattern(pattern), s_regex_cs);
return re.match(fileName).hasMatch();
});
};
auto it = std::find_if(checksumDefinitions.cbegin(), checksumDefinitions.cend(), matchFileName);
return it != checksumDefinitions.cend() ? *it : std::shared_ptr<Kleo::ChecksumDefinition>{};
}
......@@ -9,14 +9,16 @@
#pragma once
#include <Libkleo/ChecksumDefinition>
#include "kleopatra_debug.h"
#include <QFile>
#include <QRegularExpression>
#include <QTextStream>
namespace Kleo
{
class ChecksumDefinition;
}
namespace ChecksumsUtils {
#ifdef Q_OS_UNIX
// Can we use QAbstractFileEngine::caseSensitive()?
static const Qt::CaseSensitivity fs_cs = Qt::CaseSensitive;
......@@ -27,22 +29,7 @@ static const Qt::CaseSensitivity fs_cs = Qt::CaseInsensitive;
static const QRegularExpression::PatternOption s_regex_cs = QRegularExpression::CaseInsensitiveOption;
#endif
static std::vector<QRegularExpression> get_patterns(const std::vector<std::shared_ptr<Kleo::ChecksumDefinition>> &checksumDefinitions)
{
std::vector<QRegularExpression> result;
for (const auto &cd : checksumDefinitions) {
if (!cd) {
continue;
}
const QStringList &patterns = cd->patterns();
result.reserve(result.size() + patterns.size());
std::transform(patterns.cbegin(), patterns.cend(), std::back_inserter(result), [](const QString &pattern) {
return QRegularExpression(QRegularExpression::anchoredPattern(pattern), s_regex_cs);
});
}
return result;
}
std::vector<QRegularExpression> get_patterns(const std::vector<std::shared_ptr<Kleo::ChecksumDefinition>> &checksumDefinitions);
struct matches_any : std::unary_function<QString, bool> {
const std::vector<QRegularExpression> m_regexps;
......@@ -60,77 +47,9 @@ struct File {
bool binary;
};
static QString decode(const QString &encoded)
{
QString decoded;
decoded.reserve(encoded.size());
bool shift = false;
for (const QChar ch : encoded)
if (shift) {
switch (ch.toLatin1()) {
case '\\': decoded += QLatin1Char('\\'); break;
case 'n': decoded += QLatin1Char('\n'); break;
default:
qCDebug(KLEOPATRA_LOG) << "invalid escape sequence" << '\\' << ch << "(interpreted as '" << ch << "')";
decoded += ch;
break;
}
shift = false;
} else {
if (ch == QLatin1Char('\\')) {
shift = true;
} else {
decoded += ch;
}
}
return decoded;
}
static std::vector<File> parse_sum_file(const QString &fileName)
{
std::vector<File> files;
QFile f(fileName);
if (!f.open(QIODevice::ReadOnly)) {
return {};
}
QTextStream s(&f);
static const QRegularExpression rx(QRegularExpression::anchoredPattern(uR"((\\?)([a-f0-9A-F]+) ([ *])([^\\n]+)\\n*)"));
while (!s.atEnd()) {
const QString line = s.readLine();
QRegularExpressionMatch match = rx.match(line);
if (!match.hasMatch()) {
continue;
}
std::vector<File> parse_sum_file(const QString &fileName);
Q_ASSERT(!match.capturedView(4).endsWith(QLatin1Char('\n')));
const File file = {
match.capturedView(1) == QLatin1Char('\\') ? decode(match.captured(4)) : match.captured(4),
match.capturedView(2).toLatin1(),
match.capturedView(3) == QLatin1Char('*'),
};
files.push_back(file);
}
return files;
}
std::shared_ptr<Kleo::ChecksumDefinition> filename2definition(const QString &fileName,
const std::vector<std::shared_ptr<Kleo::ChecksumDefinition>> &checksumDefinitions);
static std::shared_ptr<Kleo::ChecksumDefinition> filename2definition(const QString &fileName,
const std::vector<std::shared_ptr<Kleo::ChecksumDefinition>> &checksumDefinitions)
{
auto matchFileName = [&fileName](const std::shared_ptr<Kleo::ChecksumDefinition> &cd) {
if (!cd) {
return false;
}
const QStringList &patterns = cd->patterns();
return std::any_of(patterns.cbegin(), patterns.cend(), [&fileName](const QString &pattern) {
const QRegularExpression re(QRegularExpression::anchoredPattern(pattern), s_regex_cs);
return re.match(fileName).hasMatch();
});
};
auto it = std::find_if(checksumDefinitions.cbegin(), checksumDefinitions.cend(), matchFileName);
return it != checksumDefinitions.cend() ? *it : std::shared_ptr<Kleo::ChecksumDefinition>{};
}
} // namespace ChecksumsUtils
......@@ -17,6 +17,7 @@
#include <utils/kleo_assert.h>
#include <Libkleo/Stl_Util>
#include <Libkleo/ChecksumDefinition>
#include <Libkleo/Classify>
#include <KLocalizedString>
......@@ -135,7 +136,7 @@ private:
static QStringList fs_sort(QStringList l)
{
std::sort(l.begin(), l.end(), [](const QString &lhs, const QString &rhs) {
return QString::compare(lhs, rhs, fs_cs) < 0;
return QString::compare(lhs, rhs, ChecksumsUtils::fs_cs) < 0;
});
return l;
}
......@@ -149,7 +150,7 @@ static QStringList fs_intersect(QStringList l1, QStringList l2)
l2.begin(), l2.end(),
std::back_inserter(result),
[](const QString &lhs, const QString &rhs) {
return QString::compare(lhs, rhs, fs_cs) < 0;
return QString::compare(lhs, rhs, ChecksumsUtils::fs_cs) < 0;
});
return result;
}
......@@ -260,9 +261,9 @@ void CreateChecksumsController::setFiles(const QStringList &files)
{
kleo_assert(!d->isRunning());
kleo_assert(!files.empty());
const std::vector<QRegularExpression> patterns = get_patterns(d->checksumDefinitions);
if (!std::all_of(files.cbegin(), files.cend(), matches_any(patterns)) &&
!std::none_of(files.cbegin(), files.cend(), matches_any(patterns))) {
const std::vector<QRegularExpression> patterns = ChecksumsUtils::get_patterns(d->checksumDefinitions);
if (!std::all_of(files.cbegin(), files.cend(), ChecksumsUtils::matches_any(patterns)) &&
!std::none_of(files.cbegin(), files.cend(), ChecksumsUtils::matches_any(patterns))) {
throw Exception(gpg_error(GPG_ERR_INV_ARG), i18n("Create Checksums: input files must be either all checksum files or all files to be checksummed, not a mixture of both."));
}
const QMutexLocker locker(&d->mutex);
......@@ -353,7 +354,7 @@ static std::vector<Dir> find_dirs_by_sum_files(const QStringList &files, bool al
const std::vector< std::shared_ptr<ChecksumDefinition> > &checksumDefinitions)
{
const std::vector<QRegularExpression> patterns = get_patterns(checksumDefinitions);
const std::vector<QRegularExpression> patterns = ChecksumsUtils::get_patterns(checksumDefinitions);
std::vector<Dir> dirs;
dirs.reserve(files.size());
......@@ -370,11 +371,11 @@ static std::vector<Dir> find_dirs_by_sum_files(const QStringList &files, bool al
if (allowAddition) {
inputFiles = entries;
} else {
const std::vector<File> parsed = parse_sum_file(fi.absoluteFilePath());
const std::vector<ChecksumsUtils::File> parsed = ChecksumsUtils::parse_sum_file(fi.absoluteFilePath());
QStringList oldInputFiles;
oldInputFiles.reserve(parsed.size());
std::transform(parsed.cbegin(), parsed.cend(), std::back_inserter(oldInputFiles),
std::mem_fn(&File::name));
std::mem_fn(&ChecksumsUtils::File::name));
inputFiles = fs_intersect(oldInputFiles, entries);
}
......@@ -383,7 +384,7 @@ static std::vector<Dir> find_dirs_by_sum_files(const QStringList &files, bool al
fi.fileName(),
inputFiles,
aggregate_size(dir, inputFiles),
filename2definition(fi.fileName(), checksumDefinitions)
ChecksumsUtils::filename2definition(fi.fileName(), checksumDefinitions)
};
dirs.push_back(item);
......@@ -401,7 +402,7 @@ namespace
struct less_dir : std::binary_function<QDir, QDir, bool> {
bool operator()(const QDir &lhs, const QDir &rhs) const
{
return QString::compare(lhs.absolutePath(), rhs.absolutePath(), fs_cs) < 0;
return QString::compare(lhs.absolutePath(), rhs.absolutePath(), ChecksumsUtils::fs_cs) < 0;
}
};
}
......@@ -415,7 +416,7 @@ static std::vector<Dir> find_dirs_by_input_files(const QStringList &files, const
return std::vector<Dir>();
}
const std::vector<QRegularExpression> patterns = get_patterns(checksumDefinitions);
const std::vector<QRegularExpression> patterns = ChecksumsUtils::get_patterns(checksumDefinitions);
std::map<QDir, QStringList, less_dir> dirs2files;
......@@ -548,7 +549,7 @@ void CreateChecksumsController::Private::run()
const QString scanning = i18n("Scanning directories...");
Q_EMIT progress(0, 0, scanning);
const bool haveSumFiles = std::all_of(files.cbegin(), files.cend(), matches_any(get_patterns(checksumDefinitions)));
const bool haveSumFiles = std::all_of(files.cbegin(), files.cend(), ChecksumsUtils::matches_any(ChecksumsUtils::get_patterns(checksumDefinitions)));
const auto progressCb = [this, &scanning](int c) { Q_EMIT progress(c, 0, scanning); };
const std::vector<Dir> dirs = haveSumFiles
? find_dirs_by_sum_files(files, allowAddition, progressCb, checksumDefinitions)
......
......@@ -21,6 +21,7 @@
#include <utils/kleo_assert.h>
#include <Libkleo/Stl_Util>
#include <Libkleo/ChecksumDefinition>
#include <Libkleo/Classify>
#include <KLocalizedString>
......@@ -51,7 +52,7 @@ static QStringList fs_sort(QStringList l)
int (*QString_compare)(const QString &, const QString &, Qt::CaseSensitivity) = &QString::compare;
std::sort(l.begin(), l.end(),
[](const QString &lhs, const QString &rhs) {
return QString::compare(lhs, rhs, fs_cs) < 0;
return QString::compare(lhs, rhs, ChecksumsUtils::fs_cs) < 0;
});
return l;
}
......@@ -66,7 +67,7 @@ static QStringList fs_intersect(QStringList l1, QStringList l2)
l2.begin(), l2.end(),
std::back_inserter(result),
[](const QString &lhs, const QString &rhs) {
return QString::compare(lhs, rhs, fs_cs) < 0;
return QString::compare(lhs, rhs, ChecksumsUtils::fs_cs) < 0;
});
return result;
}
......@@ -239,13 +240,13 @@ namespace
struct less_dir : std::binary_function<QDir, QDir, bool> {
bool operator()(const QDir &lhs, const QDir &rhs) const
{
return QString::compare(lhs.absolutePath(), rhs.absolutePath(), fs_cs) < 0;
return QString::compare(lhs.absolutePath(), rhs.absolutePath(), ChecksumsUtils::fs_cs) < 0;
}
};
struct less_file : std::binary_function<QString, QString, bool> {
bool operator()(const QString &lhs, const QString &rhs) const
{
return QString::compare(lhs, rhs, fs_cs) < 0;
return QString::compare(lhs, rhs, ChecksumsUtils::fs_cs) < 0;
}
};
struct sumfile_contains_file : std::unary_function<QString, bool> {
......@@ -255,11 +256,11 @@ struct sumfile_contains_file : std::unary_function<QString, bool> {
: dir(dir_), fileName(fileName_) {}
bool operator()(const QString &sumFile) const
{
const std::vector<File> files = parse_sum_file(dir.absoluteFilePath(sumFile));
const std::vector<ChecksumsUtils::File> files = ChecksumsUtils::parse_sum_file(dir.absoluteFilePath(sumFile));
qCDebug(KLEOPATRA_LOG) << "find_sums_by_input_files: found " << files.size()
<< " files listed in " << qPrintable(dir.absoluteFilePath(sumFile));
for (const File &file : files) {
const bool isSameFileName = (QString::compare(file.name, fileName, fs_cs) == 0);
for (const ChecksumsUtils::File &file : files) {
const bool isSameFileName = (QString::compare(file.name, fileName, ChecksumsUtils::fs_cs) == 0);
qCDebug(KLEOPATRA_LOG) << "find_sums_by_input_files: "
<< qPrintable(file.name) << " == "
<< qPrintable(fileName) << " ? "
......@@ -323,9 +324,9 @@ static std::vector<SumFile> find_sums_by_input_files(const QStringList &files, Q
const std::function<void(int)> &progress,
const std::vector< std::shared_ptr<ChecksumDefinition> > &checksumDefinitions)
{
const std::vector<QRegularExpression> patterns = get_patterns(checksumDefinitions);
const std::vector<QRegularExpression> patterns = ChecksumsUtils::get_patterns(checksumDefinitions);
const matches_any is_sum_file(patterns);
const ChecksumsUtils::matches_any is_sum_file(patterns);
std::map<QDir, std::set<QString, less_file>, less_dir> dirs2sums;
......@@ -392,16 +393,16 @@ static std::vector<SumFile> find_sums_by_input_files(const QStringList &files, Q
for (const QString &sumFileName : std::as_const(it->second)) {
const std::vector<File> summedfiles = parse_sum_file(dir.absoluteFilePath(sumFileName));
const std::vector<ChecksumsUtils::File> summedfiles = ChecksumsUtils::parse_sum_file(dir.absoluteFilePath(sumFileName));
QStringList files;
files.reserve(summedfiles.size());
std::transform(summedfiles.cbegin(), summedfiles.cend(),
std::back_inserter(files), std::mem_fn(&File::name));
std::back_inserter(files), std::mem_fn(&ChecksumsUtils::File::name));
const SumFile sumFile = {
it->first,
sumFileName,
aggregate_size(it->first, files),
filename2definition(sumFileName, checksumDefinitions),
ChecksumsUtils::filename2definition(sumFileName, checksumDefinitions),
};
sumfiles.push_back(sumFile);
......@@ -417,7 +418,7 @@ static std::vector<SumFile> find_sums_by_input_files(const QStringList &files, Q
static QStringList c_lang_environment()
{
static const QRegularExpression re(QRegularExpression::anchoredPattern(u"LANG=.*"), s_regex_cs);
static const QRegularExpression re(QRegularExpression::anchoredPattern(u"LANG=.*"), ChecksumsUtils::s_regex_cs);
QStringList env = QProcess::systemEnvironment();
env.erase(std::remove_if(env.begin(), env.end(),
[](const QString &str) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment