Debian Codegraph @

apt-pkg-c/pkgmanager.h

raw symbols SCIP
1 #pragma once
2 #include <apt-pkg/acquire.h>
3 #include <apt-pkg/algorithms.h>
4 #include <apt-pkg/cachefile.h>
5 #include <apt-pkg/install-progress.h>
6 #include <apt-pkg/packagemanager.h>
7 #include <apt-pkg/pkgsystem.h>
8 #include <apt-pkg/sourcelist.h>
9 #include <memory>
10
11 #include "cache.h"
12 #include "rust-apt/src/progress.rs"
13
14 struct PackageManager {
15 pkgPackageManager mutable* pkgmanager;
16
17 void get_archives(
18 const PkgCacheFile& cache,
19 const PkgRecords& records,
20 AcqTextStatus& archive_progress
21 ) const {
22 pkgAcquire acquire(&archive_progress);
23
24 // We probably need to let the user set their own pkgSourcePkgCacheFileList,
25 // but there hasn't been a need to expose such in the Rust interface
26 // yet. pkgSourceList sourcelist = *cache->GetSourceList();
27
28 if (!pkgmanager->GetArchives(
29 &acquire, cache.unconst()->GetSourceList(), &records.records
30 )) {
31 handle_errors();
32 throw std::runtime_error(
33 "Internal Issue with rust-apt in pkgmanager_get_archives."
34 " Please report this as an issue."
35 );
36 }
37
38 pkgAcquire::RunResult result = acquire.Run(archive_progress.callback->pulse_interval());
39
40 if (result != pkgAcquire::Continue) {
41 // The other variants are either Failed or Cancelled
42 // Failed will always have an error for us to handle
43 // It's unsure if Cancelled would even require a bool
44 // I believe this may be a Keyboard Interrupt situation
45 handle_errors();
46 }
47 }
48
49 void do_install(InstallProgress& callback) const {
50 PackageManagerWrapper install_progress(callback);
51 pkgPackageManager::OrderResult res = pkgmanager->DoInstall(&install_progress);
52
53 if (res == pkgPackageManager::OrderResult::Completed) {
54 return;
55 } else if (res == pkgPackageManager::OrderResult::Failed) {
56 handle_errors();
57 throw std::runtime_error(
58 "Internal Issue with rust-apt in pkgmanager_do_install."
59 " DoInstall has failed but there was no error from apt."
60 " Please report this as an issue."
61 );
62 } else if (res == pkgPackageManager::OrderResult::Incomplete) {
63 // It's not clear that there would be any apt errors here,
64 // But we'll try anyway. This is believed to be only for media swapping
65 handle_errors();
66 throw std::runtime_error(
67 "Internal Issue with rust-apt in pkgmanager_do_install."
68 " DoInstall returned Incomplete, media swaps are unsupported."
69 " Please request media swapping as a feature."
70 );
71 } else {
72 // If for whatever reason we manage to make it here (We shouldn't)
73 // Attempt to handle any apt errors
74 // And then fallback with a message to report with the result code.
75 handle_errors();
76 throw std::runtime_error(
77 "Internal Issue with rust-apt in pkgmanager_do_install."
78 " Please report this as an issue. OrderResult: " +
79 res
80 );
81 }
82 }
83
84 PackageManager(pkgDepCache* depcache) : pkgmanager(_system->CreatePM(depcache)){};
85 };
86
87 struct ProblemResolver {
88 pkgProblemResolver mutable resolver;
89
90 /// Mark a package as protected, i.e. don't let its installation/removal state change when
91 /// modifying packages during resolution.
92 void protect(const PkgIterator& pkg) const { resolver.Protect(pkg); }
93
94 /// Try to resolve dependency problems by marking packages for installation and removal.
95 void resolve(bool fix_broken, OperationProgress& callback) const {
96 OpProgressWrapper op_progress(callback);
97 resolver.Resolve(fix_broken, &op_progress);
98 handle_errors();
99 }
100
101 ProblemResolver(pkgDepCache* depcache) : resolver(depcache){};
102 };
103
104 /// Create the problem resolver.
105 UniquePtr<ProblemResolver> create_problem_resolver(const PkgDepCache& cache) {
106 return std::make_unique<ProblemResolver>(cache.ptr);
107 }
108
109 UniquePtr<PackageManager> create_pkgmanager(const PkgDepCache& cache) {
110 // Package Manager needs the DepCache initialized or else invalid memory reference.
111 return std::make_unique<PackageManager>(cache.ptr);
112 }