Skip to content

Commit 799d925

Browse files
committed
feat(lastore-upgrade-query): Add package download URI information
- The UpgradePackage::Valid() function does not validate the Site field. - Add .clang-format
1 parent 277d3a4 commit 799d925

4 files changed

Lines changed: 172 additions & 41 deletions

File tree

.clang-format

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Copyright (C) 2016 Olivier Goffart <ogoffart@woboq.com>
2+
#
3+
# You may use this file under the terms of the 3-clause BSD license.
4+
# See the file LICENSE from this package for details.
5+
6+
# This is the clang-format configuration style to be used by Qt,
7+
# based on the rules from https://wiki.qt.io/Qt_Coding_Style and
8+
# https://wiki.qt.io/Coding_Conventions
9+
10+
---
11+
# Webkit style was loosely based on the Qt style
12+
BasedOnStyle: WebKit
13+
14+
Standard: c++17
15+
16+
# Column width is limited to 100 in accordance with Qt Coding Style.
17+
# https://wiki.qt.io/Qt_Coding_Style
18+
# Note that this may be changed at some point in the future.
19+
ColumnLimit: 100
20+
# How much weight do extra characters after the line length limit have.
21+
# PenaltyExcessCharacter: 4
22+
23+
# Disable reflow of some specific comments
24+
# qdoc comments: indentation rules are different.
25+
# Translation comments and SPDX license identifiers are also excluded.
26+
CommentPragmas: "^!|^:|^ SPDX-License-Identifier:"
27+
28+
# We want a space between the type and the star for pointer types.
29+
PointerBindsToType: false
30+
31+
# We generally use "template <" with space.
32+
SpaceAfterTemplateKeyword: true
33+
34+
# We want to break before the operators, but not before a '='.
35+
BreakBeforeBinaryOperators: NonAssignment
36+
37+
# Braces are usually attached, but not after functions or class declarations.
38+
BreakBeforeBraces: Custom
39+
BraceWrapping:
40+
AfterClass: true
41+
AfterControlStatement: false
42+
AfterEnum: false
43+
AfterFunction: true
44+
AfterNamespace: false
45+
AfterObjCDeclaration: false
46+
AfterStruct: true
47+
AfterUnion: false
48+
BeforeCatch: false
49+
BeforeElse: false
50+
IndentBraces: false
51+
52+
# When constructor initializers do not fit on one line, put them each on a new line.
53+
ConstructorInitializerAllOnOneLineOrOnePerLine: true
54+
# Indent initializers by 4 spaces
55+
ConstructorInitializerIndentWidth: 4
56+
57+
# Indent width for line continuations.
58+
ContinuationIndentWidth: 8
59+
60+
# No indentation for namespaces.
61+
NamespaceIndentation: None
62+
63+
# Allow indentation for preprocessing directives (if/ifdef/endif). https://reviews.llvm.org/rL312125
64+
IndentPPDirectives: AfterHash
65+
# We only indent with 2 spaces for preprocessor directives
66+
PPIndentWidth: 2
67+
68+
# Horizontally align arguments after an open bracket.
69+
# The coding style does not specify the following, but this is what gives
70+
# results closest to the existing code.
71+
AlignAfterOpenBracket: true
72+
AlwaysBreakTemplateDeclarations: true
73+
74+
# Ideally we should also allow less short function in a single line, but
75+
# clang-format does not handle that.
76+
AllowShortFunctionsOnASingleLine: Inline
77+
78+
# The coding style specifies some include order categories, but also tells to
79+
# separate categories with an empty line. It does not specify the order within
80+
# the categories. Since the SortInclude feature of clang-format does not
81+
# re-order includes separated by empty lines, the feature is not used.
82+
SortIncludes: false
83+
84+
# macros for which the opening brace stays attached.
85+
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH, forever, Q_FOREVER, QBENCHMARK, QBENCHMARK_ONCE ]
86+
87+
# Break constructor initializers before the colon and after the commas.
88+
BreakConstructorInitializers: BeforeColon
89+
90+
# Add "// namespace <namespace>" comments on closing brace for a namespace
91+
# Ignored for namespaces that qualify as a short namespace,
92+
# see 'ShortNamespaceLines'
93+
FixNamespaceComments: true
94+
95+
# Definition of how short a short namespace is, default 1
96+
ShortNamespaceLines: 1
97+
98+
# When escaping newlines in a macro attach the '\' as far left as possible, e.g.
99+
##define a \
100+
# something; \
101+
# other; \
102+
# thelastlineislong;
103+
AlignEscapedNewlines: Left
104+
105+
# Avoids the addition of a space between an identifier and the
106+
# initializer list in list-initialization.
107+
SpaceBeforeCpp11BracedList: false
108+
109+
---
110+
# Use the Google-based style for .proto files.
111+
Language: Proto
112+
BasedOnStyle: Google
113+
IndentWidth: 4
114+
ColumnLimit: 100

src/lastore-upgrade-query/main.cc

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
#include <string>
55
#include <nlohmann/json.hpp>
66

7-
int main(int argc, char **argv) {
7+
int main(int argc, char **argv)
8+
{
89
std::string sourcelist = "";
910
std::string sourcepart = "";
1011
bool outputJson = false;
@@ -33,9 +34,10 @@ int main(int argc, char **argv) {
3334
}
3435
}
3536

36-
std::vector<UpgradePackage> packages = GetUpgradePackages(sourcelist, sourcepart, allowDowngrades);
37+
std::vector<UpgradePackage> packages =
38+
GetUpgradePackages(sourcelist, sourcepart, allowDowngrades);
3739

38-
for (const auto& pkgItem : packages) {
40+
for (const auto &pkgItem : packages) {
3941
if (!pkgItem.Valid()) {
4042
std::cerr << "Invalid package: " << pkgItem.Name << std::endl;
4143
return 1;
@@ -44,36 +46,35 @@ int main(int argc, char **argv) {
4446

4547
if (outputJson) {
4648
nlohmann::json json_array = nlohmann::json::array();
47-
48-
for (const auto& pkg : packages) {
49-
json_array.push_back({
50-
{"name", pkg.Name},
51-
{"installed_version", pkg.InstalledVersion},
52-
{"candidate_version", pkg.CandidateVersion},
53-
{"architecture", pkg.Architecture},
54-
{"codename", pkg.Codename},
55-
{"component", pkg.Component},
56-
{"site", pkg.Site},
57-
{"filename", pkg.Filename},
58-
{"size", pkg.Size},
59-
{"installed_size", pkg.InstalledSize},
60-
{"hash", pkg.Hash}
61-
});
49+
50+
for (const auto &pkg : packages) {
51+
json_array.push_back({ { "name", pkg.Name },
52+
{ "installed_version", pkg.InstalledVersion },
53+
{ "candidate_version", pkg.CandidateVersion },
54+
{ "architecture", pkg.Architecture },
55+
{ "codename", pkg.Codename },
56+
{ "component", pkg.Component },
57+
{ "site", pkg.Site },
58+
{ "filename", pkg.Filename },
59+
{ "size", pkg.Size },
60+
{ "installed_size", pkg.InstalledSize },
61+
{ "hash", pkg.Hash },
62+
{ "uri", pkg.Uri } });
6263
}
63-
64+
6465
std::cout << json_array.dump() << '\n';
6566
} else {
6667
std::cout << "\n=== Retrieved Package Information ===\n"
6768
<< "Total " << packages.size() << " packages found\n";
6869

69-
for (const auto& pkg : packages) {
70+
for (const auto &pkg : packages) {
7071
std::cout << "\nname: " << pkg.Name << "\n"
7172
<< "candidate_version: " << pkg.CandidateVersion << "\n";
72-
73+
7374
if (!pkg.InstalledVersion.empty()) {
7475
std::cout << "installed_version: " << pkg.InstalledVersion << "\n";
7576
}
76-
77+
7778
std::cout << "architecture: " << pkg.Architecture << "\n"
7879
<< "codename: " << pkg.Codename << "\n"
7980
<< "component: " << pkg.Component << "\n"
@@ -83,7 +84,8 @@ int main(int argc, char **argv) {
8384
<< pkg.Size / 1024.0 / 1024.0 << " MB\n"
8485
<< "installed_size: " << std::fixed << std::setprecision(2)
8586
<< pkg.InstalledSize / 1024.0 / 1024.0 << " MB\n"
86-
<< "hash: " << pkg.Hash << "\n";
87+
<< "hash: " << pkg.Hash << "\n"
88+
<< "uri: " << pkg.Uri << "\n";
8789
}
8890
}
8991

src/lastore-upgrade-query/upgrade_query.cc

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <apt-pkg/upgrade.h>
1010
#include <apt-pkg/depcache.h>
1111
#include <apt-pkg/sourcelist.h>
12+
#include <apt-pkg/indexfile.h>
1213
#include <apt-pkg/policy.h>
1314
#include <apt-pkg/pkgrecords.h>
1415
#include <apt-pkg/hashes.h>
@@ -21,26 +22,24 @@
2122

2223
#include "upgrade_query.h"
2324

24-
bool UpgradePackage::Valid() const {
25-
if (Name.empty() ||
26-
CandidateVersion.empty() ||
27-
Architecture.empty() ||
28-
Codename.empty() ||
29-
Site.empty() ||
30-
Filename.empty() ||
31-
Hash.empty()) {
25+
bool UpgradePackage::Valid() const
26+
{
27+
if (Name.empty() || CandidateVersion.empty() || Architecture.empty() || Codename.empty()
28+
|| Filename.empty() || Hash.empty()) {
3229
return false;
3330
}
34-
31+
3532
// Check uint64_t fields - they should not be 0
3633
if (Size == 0) {
3734
return false;
3835
}
39-
36+
4037
return true;
4138
}
4239

43-
UpgradePackage GetUpgradePackage(pkgCacheFile &Cache, pkgRecords &Recs, const pkgCache::PkgIterator &pkg) {
40+
UpgradePackage GetUpgradePackage(pkgCacheFile &Cache, pkgRecords &Recs,
41+
const pkgSourceList *srcList, const pkgCache::PkgIterator &pkg)
42+
{
4443
UpgradePackage result;
4544
pkgCache::VerIterator candVer = Cache->GetCandidateVersion(pkg);
4645

@@ -71,9 +70,17 @@ UpgradePackage GetUpgradePackage(pkgCacheFile &Cache, pkgRecords &Recs, const pk
7170
}
7271

7372
// Use pkgRecords to get detailed download information
74-
pkgRecords::Parser& parser = Recs.Lookup(vf);
73+
pkgRecords::Parser &parser = Recs.Lookup(vf);
7574
result.Filename = parser.FileName();
7675

76+
// Construct URI using pkgIndexFile
77+
if (srcList) {
78+
pkgIndexFile *index;
79+
if (srcList->FindIndex(pf, index) && index) {
80+
result.Uri = index->ArchiveURI(parser.FileName());
81+
}
82+
}
83+
7784
// Get hash information
7885
HashStringList hashes = parser.Hashes();
7986
if (!hashes.empty()) {
@@ -88,7 +95,10 @@ UpgradePackage GetUpgradePackage(pkgCacheFile &Cache, pkgRecords &Recs, const pk
8895
return result;
8996
}
9097

91-
std::vector<UpgradePackage> GetUpgradePackages(const std::string &sourcelist, const std::string &sourceparts, bool allow_downgrades) {
98+
std::vector<UpgradePackage> GetUpgradePackages(const std::string &sourcelist,
99+
const std::string &sourceparts,
100+
bool allow_downgrades)
101+
{
92102
std::vector<UpgradePackage> result;
93103
if (!pkgInitConfig(*_config)) {
94104
std::cerr << "Failed to initialize APT config" << std::endl;
@@ -125,15 +135,16 @@ std::vector<UpgradePackage> GetUpgradePackages(const std::string &sourcelist, co
125135
Cache->MarkAndSweep();
126136

127137
pkgRecords Recs(Cache);
138+
pkgSourceList *srcList = Cache.GetSourceList();
128139
result.reserve(Cache->Head().PackageCount / 4); // Reserve approximate space
129140

130141
for (pkgCache::PkgIterator pkg = Cache->PkgBegin(); !pkg.end(); ++pkg) {
131-
const pkgDepCache::StateCache& state = (*Cache)[pkg];
142+
const pkgDepCache::StateCache &state = (*Cache)[pkg];
132143

133144
if (state.NewInstall() || state.Upgrade() || state.Downgrade()) {
134-
result.emplace_back(GetUpgradePackage(Cache, Recs, pkg));
145+
result.emplace_back(GetUpgradePackage(Cache, Recs, srcList, pkg));
135146
}
136147
}
137-
148+
138149
return result;
139150
}

src/lastore-upgrade-query/upgrade_query.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
#include <string>
55
#include <cstdint>
66

7-
class UpgradePackage {
7+
class UpgradePackage
8+
{
89
public:
910
std::string Name;
1011
std::string InstalledVersion;
@@ -17,8 +18,11 @@ class UpgradePackage {
1718
uint64_t Size = 0;
1819
uint64_t InstalledSize = 0;
1920
std::string Hash;
21+
std::string Uri;
2022

2123
bool Valid() const;
2224
};
2325

24-
std::vector<UpgradePackage> GetUpgradePackages(const std::string &sourcelist, const std::string &sourceparts, bool allow_downgrades = false);
26+
std::vector<UpgradePackage> GetUpgradePackages(const std::string &sourcelist,
27+
const std::string &sourceparts,
28+
bool allow_downgrades = false);

0 commit comments

Comments
 (0)