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>
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}
0 commit comments