Skip to content
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
114 changes: 114 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Copyright (C) 2016 Olivier Goffart <ogoffart@woboq.com>
#
# You may use this file under the terms of the 3-clause BSD license.
# See the file LICENSE from this package for details.

# This is the clang-format configuration style to be used by Qt,
# based on the rules from https://wiki.qt.io/Qt_Coding_Style and
# https://wiki.qt.io/Coding_Conventions

---
# Webkit style was loosely based on the Qt style
BasedOnStyle: WebKit

Standard: c++17

# Column width is limited to 100 in accordance with Qt Coding Style.
# https://wiki.qt.io/Qt_Coding_Style
# Note that this may be changed at some point in the future.
ColumnLimit: 100
# How much weight do extra characters after the line length limit have.
# PenaltyExcessCharacter: 4

# Disable reflow of some specific comments
# qdoc comments: indentation rules are different.
# Translation comments and SPDX license identifiers are also excluded.
CommentPragmas: "^!|^:|^ SPDX-License-Identifier:"

# We want a space between the type and the star for pointer types.
PointerBindsToType: false

# We generally use "template <" with space.
SpaceAfterTemplateKeyword: true

# We want to break before the operators, but not before a '='.
BreakBeforeBinaryOperators: NonAssignment

# Braces are usually attached, but not after functions or class declarations.
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: true
AfterControlStatement: false
AfterEnum: false
AfterFunction: true
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: true
AfterUnion: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false

# When constructor initializers do not fit on one line, put them each on a new line.
ConstructorInitializerAllOnOneLineOrOnePerLine: true
# Indent initializers by 4 spaces
ConstructorInitializerIndentWidth: 4

# Indent width for line continuations.
ContinuationIndentWidth: 8

# No indentation for namespaces.
NamespaceIndentation: None

# Allow indentation for preprocessing directives (if/ifdef/endif). https://reviews.llvm.org/rL312125
IndentPPDirectives: AfterHash
# We only indent with 2 spaces for preprocessor directives
PPIndentWidth: 2

# Horizontally align arguments after an open bracket.
# The coding style does not specify the following, but this is what gives
# results closest to the existing code.
AlignAfterOpenBracket: true
AlwaysBreakTemplateDeclarations: true

# Ideally we should also allow less short function in a single line, but
# clang-format does not handle that.
AllowShortFunctionsOnASingleLine: Inline

# The coding style specifies some include order categories, but also tells to
# separate categories with an empty line. It does not specify the order within
# the categories. Since the SortInclude feature of clang-format does not
# re-order includes separated by empty lines, the feature is not used.
SortIncludes: false

# macros for which the opening brace stays attached.
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH, forever, Q_FOREVER, QBENCHMARK, QBENCHMARK_ONCE ]

# Break constructor initializers before the colon and after the commas.
BreakConstructorInitializers: BeforeColon

# Add "// namespace <namespace>" comments on closing brace for a namespace
# Ignored for namespaces that qualify as a short namespace,
# see 'ShortNamespaceLines'
FixNamespaceComments: true

# Definition of how short a short namespace is, default 1
ShortNamespaceLines: 1

# When escaping newlines in a macro attach the '\' as far left as possible, e.g.
##define a \
# something; \
# other; \
# thelastlineislong;
AlignEscapedNewlines: Left

# Avoids the addition of a space between an identifier and the
# initializer list in list-initialization.
SpaceBeforeCpp11BracedList: false

---
# Use the Google-based style for .proto files.
Language: Proto
BasedOnStyle: Google
IndentWidth: 4
ColumnLimit: 100
48 changes: 25 additions & 23 deletions src/lastore-upgrade-query/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#include <string>
#include <nlohmann/json.hpp>

int main(int argc, char **argv) {
int main(int argc, char **argv)
{
std::string sourcelist = "";
std::string sourcepart = "";
bool outputJson = false;
Expand Down Expand Up @@ -33,9 +34,10 @@ int main(int argc, char **argv) {
}
}

std::vector<UpgradePackage> packages = GetUpgradePackages(sourcelist, sourcepart, allowDowngrades);
std::vector<UpgradePackage> packages =
GetUpgradePackages(sourcelist, sourcepart, allowDowngrades);

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

if (outputJson) {
nlohmann::json json_array = nlohmann::json::array();

for (const auto& pkg : packages) {
json_array.push_back({
{"name", pkg.Name},
{"installed_version", pkg.InstalledVersion},
{"candidate_version", pkg.CandidateVersion},
{"architecture", pkg.Architecture},
{"codename", pkg.Codename},
{"component", pkg.Component},
{"site", pkg.Site},
{"filename", pkg.Filename},
{"size", pkg.Size},
{"installed_size", pkg.InstalledSize},
{"hash", pkg.Hash}
});

for (const auto &pkg : packages) {
json_array.push_back({ { "name", pkg.Name },
{ "installed_version", pkg.InstalledVersion },
{ "candidate_version", pkg.CandidateVersion },
{ "architecture", pkg.Architecture },
{ "codename", pkg.Codename },
{ "component", pkg.Component },
{ "site", pkg.Site },
{ "filename", pkg.Filename },
{ "size", pkg.Size },
{ "installed_size", pkg.InstalledSize },
{ "hash", pkg.Hash },
{ "uri", pkg.Uri } });
}

std::cout << json_array.dump() << '\n';
} else {
std::cout << "\n=== Retrieved Package Information ===\n"
<< "Total " << packages.size() << " packages found\n";

for (const auto& pkg : packages) {
for (const auto &pkg : packages) {
std::cout << "\nname: " << pkg.Name << "\n"
<< "candidate_version: " << pkg.CandidateVersion << "\n";

if (!pkg.InstalledVersion.empty()) {
std::cout << "installed_version: " << pkg.InstalledVersion << "\n";
}

std::cout << "architecture: " << pkg.Architecture << "\n"
<< "codename: " << pkg.Codename << "\n"
<< "component: " << pkg.Component << "\n"
Expand All @@ -83,7 +84,8 @@ int main(int argc, char **argv) {
<< pkg.Size / 1024.0 / 1024.0 << " MB\n"
<< "installed_size: " << std::fixed << std::setprecision(2)
<< pkg.InstalledSize / 1024.0 / 1024.0 << " MB\n"
<< "hash: " << pkg.Hash << "\n";
<< "hash: " << pkg.Hash << "\n"
<< "uri: " << pkg.Uri << "\n";
}
}

Expand Down
43 changes: 27 additions & 16 deletions src/lastore-upgrade-query/upgrade_query.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <apt-pkg/upgrade.h>
#include <apt-pkg/depcache.h>
#include <apt-pkg/sourcelist.h>
#include <apt-pkg/indexfile.h>
#include <apt-pkg/policy.h>
#include <apt-pkg/pkgrecords.h>
#include <apt-pkg/hashes.h>
Expand All @@ -21,26 +22,24 @@

#include "upgrade_query.h"

bool UpgradePackage::Valid() const {
if (Name.empty() ||
CandidateVersion.empty() ||
Architecture.empty() ||
Codename.empty() ||
Site.empty() ||
Filename.empty() ||
Hash.empty()) {
bool UpgradePackage::Valid() const
{
if (Name.empty() || CandidateVersion.empty() || Architecture.empty() || Codename.empty()
|| Filename.empty() || Hash.empty()) {
return false;
}

// Check uint64_t fields - they should not be 0
if (Size == 0) {
return false;
}

return true;
}

UpgradePackage GetUpgradePackage(pkgCacheFile &Cache, pkgRecords &Recs, const pkgCache::PkgIterator &pkg) {
UpgradePackage GetUpgradePackage(pkgCacheFile &Cache, pkgRecords &Recs,
const pkgSourceList *srcList, const pkgCache::PkgIterator &pkg)
{
UpgradePackage result;
pkgCache::VerIterator candVer = Cache->GetCandidateVersion(pkg);

Expand Down Expand Up @@ -71,9 +70,17 @@ UpgradePackage GetUpgradePackage(pkgCacheFile &Cache, pkgRecords &Recs, const pk
}

// Use pkgRecords to get detailed download information
pkgRecords::Parser& parser = Recs.Lookup(vf);
pkgRecords::Parser &parser = Recs.Lookup(vf);
result.Filename = parser.FileName();

// Construct URI using pkgIndexFile
if (srcList) {
pkgIndexFile *index;
if (srcList->FindIndex(pf, index) && index) {
result.Uri = index->ArchiveURI(parser.FileName());
}
}

// Get hash information
HashStringList hashes = parser.Hashes();
if (!hashes.empty()) {
Expand All @@ -88,7 +95,10 @@ UpgradePackage GetUpgradePackage(pkgCacheFile &Cache, pkgRecords &Recs, const pk
return result;
}

std::vector<UpgradePackage> GetUpgradePackages(const std::string &sourcelist, const std::string &sourceparts, bool allow_downgrades) {
std::vector<UpgradePackage> GetUpgradePackages(const std::string &sourcelist,
const std::string &sourceparts,
bool allow_downgrades)
{
std::vector<UpgradePackage> result;
if (!pkgInitConfig(*_config)) {
std::cerr << "Failed to initialize APT config" << std::endl;
Expand Down Expand Up @@ -125,15 +135,16 @@ std::vector<UpgradePackage> GetUpgradePackages(const std::string &sourcelist, co
Cache->MarkAndSweep();

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

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

if (state.NewInstall() || state.Upgrade() || state.Downgrade()) {
result.emplace_back(GetUpgradePackage(Cache, Recs, pkg));
result.emplace_back(GetUpgradePackage(Cache, Recs, srcList, pkg));
}
}

return result;
}
8 changes: 6 additions & 2 deletions src/lastore-upgrade-query/upgrade_query.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#include <string>
#include <cstdint>

class UpgradePackage {
class UpgradePackage
{
public:
std::string Name;
std::string InstalledVersion;
Expand All @@ -17,8 +18,11 @@ class UpgradePackage {
uint64_t Size = 0;
uint64_t InstalledSize = 0;
std::string Hash;
std::string Uri;

bool Valid() const;
};

std::vector<UpgradePackage> GetUpgradePackages(const std::string &sourcelist, const std::string &sourceparts, bool allow_downgrades = false);
std::vector<UpgradePackage> GetUpgradePackages(const std::string &sourcelist,
const std::string &sourceparts,
bool allow_downgrades = false);
Loading