Skip to content

Commit d6b4e11

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

3 files changed

Lines changed: 32 additions & 18 deletions

File tree

src/lastore-upgrade-query/main.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ int main(int argc, char **argv) {
5757
{"filename", pkg.Filename},
5858
{"size", pkg.Size},
5959
{"installed_size", pkg.InstalledSize},
60-
{"hash", pkg.Hash}
60+
{"hash", pkg.Hash},
61+
{"uri", pkg.Uri}
6162
});
6263
}
6364

@@ -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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class UpgradePackage {
1717
uint64_t Size = 0;
1818
uint64_t InstalledSize = 0;
1919
std::string Hash;
20+
std::string Uri;
2021

2122
bool Valid() const;
2223
};

0 commit comments

Comments
 (0)