| 1 |
#pragma once |
| 2 |
#include <apt-pkg/cachefile.h> |
| 3 |
#include <apt-pkg/debfile.h> |
| 4 |
#include <apt-pkg/error.h> |
| 5 |
#include <apt-pkg/fileutl.h> |
| 6 |
#include <apt-pkg/indexfile.h> |
| 7 |
#include <apt-pkg/pkgcache.h> |
| 8 |
#include <apt-pkg/policy.h> |
| 9 |
#include <apt-pkg/sourcelist.h> |
| 10 |
#include <apt-pkg/update.h> |
| 11 |
#include "rust/cxx.h" |
| 12 |
|
| 13 |
// Defines the callbacks code that's generated for progress |
| 14 |
#include "rust-apt/src/acquire.rs" |
| 15 |
|
| 16 |
#include "depcache.h" |
| 17 |
#include "records.h" |
| 18 |
#include "types.h" |
| 19 |
|
| 20 |
struct PkgCacheFile : public pkgCacheFile { |
| 21 |
// Maybe we use this if we don't want pin_mut() all over the place in Rust. |
| 22 |
PkgCacheFile* unconst() const { return const_cast<PkgCacheFile*>(this); } |
| 23 |
|
| 24 |
/// Update the package lists, handle errors and return a Result. |
| 25 |
void update(AcqTextStatus& progress) const { |
| 26 |
ListUpdate( |
| 27 |
progress, *this->unconst()->GetSourceList(), progress.callback->pulse_interval() |
| 28 |
); |
| 29 |
handle_errors(); |
| 30 |
} |
| 31 |
|
| 32 |
// Return a package by name. |
| 33 |
UniquePtr<PkgIterator> find_pkg(str name) const { |
| 34 |
return std::make_unique<PkgIterator>( |
| 35 |
this->unconst()->GetPkgCache()->FindPkg(APT::StringView(name.begin(), name.length())) |
| 36 |
); |
| 37 |
} |
| 38 |
|
| 39 |
UniquePtr<PkgIterator> begin() const { |
| 40 |
return std::make_unique<PkgIterator>(this->unconst()->GetPkgCache()->PkgBegin()); |
| 41 |
} |
| 42 |
|
| 43 |
/// The priority of the package as shown in `apt policy`. |
| 44 |
int32_t priority(const VerIterator& ver) const { |
| 45 |
return this->unconst()->GetPolicy()->GetPriority(ver); |
| 46 |
} |
| 47 |
|
| 48 |
UniquePtr<PkgDepCache> create_depcache() const { |
| 49 |
return std::make_unique<PkgDepCache>(this->unconst()->GetDepCache()); |
| 50 |
} |
| 51 |
|
| 52 |
UniquePtr<PkgRecords> create_records() const { |
| 53 |
return std::make_unique<PkgRecords>(this->unconst()); |
| 54 |
} |
| 55 |
|
| 56 |
UniquePtr<IndexFile> find_index(const PkgFileIterator& file) const { |
| 57 |
pkgIndexFile* index; |
| 58 |
if (!this->unconst()->GetSourceList()->FindIndex(file, index)) { |
| 59 |
_system->FindIndex(file, index); |
| 60 |
} |
| 61 |
return std::make_unique<IndexFile>(index); |
| 62 |
} |
| 63 |
|
| 64 |
bool get_indexes(const PkgAcquire& fetcher) const { |
| 65 |
return this->unconst()->GetSourceList()->GetIndexes(fetcher.ptr, true); |
| 66 |
} |
| 67 |
|
| 68 |
PkgCacheFile() : pkgCacheFile(){}; |
| 69 |
}; |
| 70 |
|
| 71 |
inline UniquePtr<PkgCacheFile> create_cache(rust::Slice<const str> volatile_files) { |
| 72 |
UniquePtr<PkgCacheFile> cache = std::make_unique<PkgCacheFile>(); |
| 73 |
|
| 74 |
for (auto file_str : volatile_files) { |
| 75 |
std::string file_string(file_str); |
| 76 |
// Add the file to the cache. |
| 77 |
if (!cache->GetSourceList()->AddVolatileFile(file_string)) { |
| 78 |
_error->Error("%s", ("Couldn't add '" + file_string + "' to the cache.").c_str()); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
// Building the pkg caches can cause an error that might not |
| 83 |
// Get propagated until you get a pkg which shouldn't have errors. |
| 84 |
// See https://gitlab.com/volian/rust-apt/-/issues/24 |
| 85 |
cache->GetPkgCache(); |
| 86 |
handle_errors(); |
| 87 |
|
| 88 |
return cache; |
| 89 |
} |