| 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 |
|
| 25 |
|
| 26 |
|
| 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 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 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 |
|
| 64 |
|
| 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 |
|
| 73 |
|
| 74 |
|
| 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 |
|
| 91 |
|
| 92 |
void protect(const PkgIterator& pkg) const { resolver.Protect(pkg); } |
| 93 |
|
| 94 |
|
| 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 |
|
| 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 |
|
| 111 |
return std::make_unique<PackageManager>(cache.ptr); |
| 112 |
} |