|
| 1 | +#include <apt-pkg/init.h> |
| 2 | +#include <apt-pkg/cachefile.h> |
| 3 | +#include <apt-pkg/pkgcache.h> |
| 4 | +#include <apt-pkg/algorithms.h> |
| 5 | +#include <apt-pkg/pkgsystem.h> |
| 6 | +#include <apt-pkg/progress.h> |
| 7 | +#include <apt-pkg/error.h> |
| 8 | +#include <apt-pkg/acquire.h> |
| 9 | +#include <apt-pkg/upgrade.h> |
| 10 | +#include <apt-pkg/depcache.h> |
| 11 | +#include <apt-pkg/sourcelist.h> |
| 12 | +#include <apt-pkg/policy.h> |
| 13 | +#include <apt-pkg/pkgrecords.h> |
| 14 | +#include <apt-pkg/hashes.h> |
| 15 | + |
| 16 | +#include <iostream> |
| 17 | +#include <vector> |
| 18 | +#include <string> |
| 19 | +#include <cstring> |
| 20 | +#include <cstdlib> |
| 21 | + |
| 22 | +#include "upgrade_query.h" |
| 23 | + |
| 24 | +bool UpgradePackage::Valid() const { |
| 25 | + if (Name.empty() || |
| 26 | + CandidateVersion.empty() || |
| 27 | + Architecture.empty() || |
| 28 | + Codename.empty() || |
| 29 | + Component.empty() || |
| 30 | + Site.empty() || |
| 31 | + Filename.empty() || |
| 32 | + Hash.empty()) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + |
| 36 | + // Check uint64_t fields - they should not be 0 |
| 37 | + if (Size == 0 || InstalledSize == 0) { |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
| 41 | + return true; |
| 42 | +} |
| 43 | + |
| 44 | +UpgradePackage GetUpgradePackage(pkgCacheFile &Cache, pkgRecords &Recs, const pkgCache::PkgIterator &pkg) { |
| 45 | + UpgradePackage result; |
| 46 | + pkgCache::VerIterator candVer = Cache->GetCandidateVersion(pkg); |
| 47 | + |
| 48 | + result.Name = pkg.Name(); |
| 49 | + |
| 50 | + // Fill installed version if present |
| 51 | + pkgCache::VerIterator curVer = pkg.CurrentVer(); |
| 52 | + if (!curVer.end()) { |
| 53 | + result.InstalledVersion = curVer.VerStr(); |
| 54 | + } |
| 55 | + |
| 56 | + if (!candVer.end()) { |
| 57 | + result.CandidateVersion = candVer.VerStr(); |
| 58 | + result.Architecture = candVer.Arch(); |
| 59 | + result.Size = candVer->Size; |
| 60 | + result.InstalledSize = candVer->InstalledSize; |
| 61 | + |
| 62 | + // Get file list to find repository information |
| 63 | + pkgCache::VerFileIterator vf = candVer.FileList(); |
| 64 | + if (!vf.end()) { |
| 65 | + result.Codename = vf.File().Codename() ? vf.File().Codename() : ""; |
| 66 | + result.Component = vf.File().Component() ? vf.File().Component() : ""; |
| 67 | + |
| 68 | + // Get download information |
| 69 | + pkgCache::PkgFileIterator pf = vf.File(); |
| 70 | + if (!pf.end()) { |
| 71 | + result.Site = pf.Site() ? pf.Site() : ""; |
| 72 | + } |
| 73 | + |
| 74 | + // Use pkgRecords to get detailed download information |
| 75 | + pkgRecords::Parser& parser = Recs.Lookup(vf); |
| 76 | + result.Filename = parser.FileName(); |
| 77 | + |
| 78 | + // Get hash information |
| 79 | + HashStringList hashes = parser.Hashes(); |
| 80 | + if (!hashes.empty()) { |
| 81 | + const HashString *hash = hashes.find(NULL); |
| 82 | + if (hash) { |
| 83 | + result.Hash = hash->toStr(); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return result; |
| 90 | +} |
| 91 | + |
| 92 | +std::vector<UpgradePackage> GetUpgradePackages(const std::string &sourcelist, const std::string &sourceparts, bool allow_downgrades) { |
| 93 | + std::vector<UpgradePackage> result; |
| 94 | + if (!pkgInitConfig(*_config)) { |
| 95 | + std::cerr << "Failed to initialize APT config" << std::endl; |
| 96 | + return result; |
| 97 | + } |
| 98 | + |
| 99 | + if (!pkgInitSystem(*_config, _system)) { |
| 100 | + std::cerr << "Failed to initialize APT system" << std::endl; |
| 101 | + return result; |
| 102 | + } |
| 103 | + |
| 104 | + // disable debug output |
| 105 | + _config->CndSet("quiet::NoProgress", true); |
| 106 | + _config->Set("quiet", 1); |
| 107 | + |
| 108 | + _config->Set("APT::Get::allow-downgrades", allow_downgrades); |
| 109 | + |
| 110 | + // If custom paths are provided, set configuration items |
| 111 | + if (!sourcelist.empty()) { |
| 112 | + _config->Set("Dir::Etc::sourcelist", sourcelist); |
| 113 | + } |
| 114 | + if (!sourceparts.empty()) { |
| 115 | + _config->Set("Dir::Etc::sourceparts", sourceparts); |
| 116 | + } |
| 117 | + |
| 118 | + pkgCacheFile Cache; |
| 119 | + OpTextProgress Prog(*_config); |
| 120 | + if (!Cache.Open(&Prog, false)) { |
| 121 | + std::cerr << "Could not open cache" << std::endl; |
| 122 | + return result; |
| 123 | + } |
| 124 | + |
| 125 | + APT::Upgrade::Upgrade(*Cache, APT::Upgrade::ALLOW_EVERYTHING); |
| 126 | + Cache->MarkAndSweep(); |
| 127 | + |
| 128 | + pkgRecords Recs(Cache); |
| 129 | + result.reserve(Cache->Head().PackageCount / 4); // Reserve approximate space |
| 130 | + |
| 131 | + for (pkgCache::PkgIterator pkg = Cache->PkgBegin(); !pkg.end(); ++pkg) { |
| 132 | + const pkgDepCache::StateCache& state = (*Cache)[pkg]; |
| 133 | + |
| 134 | + if (state.NewInstall() || state.Upgrade()) { |
| 135 | + result.emplace_back(GetUpgradePackage(Cache, Recs, pkg)); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return result; |
| 140 | +} |
0 commit comments