forked from AdrianTM/mx-packageinstaller
-
Notifications
You must be signed in to change notification settings - Fork 11
/
aptcache.cpp
111 lines (98 loc) · 3.66 KB
/
aptcache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "aptcache.h"
#include <QDebug>
#include <QDirIterator>
#include <QRegularExpression>
#include "versionnumber.h"
AptCache::AptCache()
{
loadCacheFiles();
parseContent();
}
void AptCache::loadCacheFiles()
{
// Exclude Debian backports and MX testrepo and temp repos
const QString arch = getArch();
// Define include and exclude regex patterns
const QRegularExpression includeRegex(QString(R"(^.*binary-%1_Packages$)").arg(arch));
const QRegularExpression secondaryRegex(R"(^.*binary-.*_Packages$)");
const QRegularExpression excludeRegex(
R"((debian_.*-backports_.*_Packages)|(mx_testrepo.*_test_.*_Packages)|(mx_repo.*_temp_.*_Packages))");
QDirIterator it(dir.path(), QDir::Files);
while (it.hasNext()) {
const QString fileName = it.next();
if ((includeRegex.match(fileName).hasMatch() || secondaryRegex.match(fileName).hasMatch())
&& !excludeRegex.match(fileName).hasMatch()) {
if (!readFile(fileName)) {
qWarning() << "Error reading cache file:" << fileName;
}
}
}
}
QMap<QString, PackageInfo> AptCache::getCandidates() const
{
return candidates;
}
// Return DEB_BUILD_ARCH format which differs from what 'arch' or currentCpuArchitecture return
QString AptCache::getArch()
{
static const QString arch = arch_names.value(QSysInfo::currentCpuArchitecture());
return arch;
}
void AptCache::parseContent()
{
QTextStream stream(&files_content);
QString line;
QString package;
QString version;
QString description;
QString architecture;
const QString arch = getArch();
static const QRegularExpression re_arch(QStringLiteral(".*(%1|all).*").arg(arch));
bool match_arch = false;
static const QLatin1String packageStr("Package:");
static const QLatin1String archStr("Architecture:");
static const QLatin1String versionStr("Version:");
static const QLatin1String descStr("Description:");
static constexpr int packageSize = packageStr.size();
static constexpr int archSize = archStr.size();
static constexpr int versionSize = versionStr.size();
static constexpr int descSize = descStr.size();
// Code assumes Description: is the last matched line
while (stream.readLineInto(&line)) {
if (line.startsWith(packageStr)) {
package = line.mid(packageSize).trimmed();
// Reset state for new package
version.clear();
description.clear();
match_arch = false;
} else if (line.startsWith(archStr)) {
architecture = line.mid(archSize).trimmed();
match_arch = re_arch.match(architecture).hasMatch();
} else if (line.startsWith(versionStr)) {
version = line.mid(versionSize).trimmed();
} else if (line.startsWith(descStr)) {
description = line.mid(descSize).trimmed();
if (match_arch && !package.isEmpty() && !version.isEmpty()) {
auto it = candidates.find(package);
if (it == candidates.end()) {
candidates.insert(package, {version, description});
} else if (VersionNumber(it->version) < VersionNumber(version)) {
it->version = version;
it->description = description;
}
}
}
}
files_content.clear();
}
bool AptCache::readFile(const QString &file_name)
{
QFile file(dir.absoluteFilePath(file_name));
if (!file.open(QFile::ReadOnly)) {
qWarning() << "Could not open file: " << file.fileName();
return false;
}
files_content += QLatin1String("\n") + file.readAll();
file.close();
return true;
}