diff --git a/apt-pkg-c/lib.cpp b/apt-pkg-c/lib.cpp index c8635ff..136112c 100644 --- a/apt-pkg-c/lib.cpp +++ b/apt-pkg-c/lib.cpp @@ -14,7 +14,11 @@ struct PCache { // Owned by us. + #ifndef ALT_LINUX pkgCacheFile *cache_file; + #else + CacheFile *cache_file; + #endif // Borrowed from cache_file. pkgCache *cache; @@ -74,9 +78,6 @@ extern "C" { void init_config_system(); PCache *pkg_cache_create(); - - - void pkg_cache_release(PCache *cache); int32_t pkg_cache_compare_versions(PCache *cache, const char *left, const char *right); @@ -89,7 +90,6 @@ extern "C" { #else PPkgIterator *pkg_cache_find_name_arch(PCache *cache, const char *name); #endif - void pkg_iter_release(PPkgIterator *iterator); // pkg_iter mutation @@ -98,7 +98,6 @@ extern "C" { // pkg_iter access const char *pkg_iter_name(PPkgIterator *iterator); - const char *pkg_iter_arch(PPkgIterator *iterator); const char *pkg_iter_current_version(PPkgIterator *iterator); const char *pkg_iter_candidate_version(PPkgIterator *iterator); @@ -200,17 +199,37 @@ PCache *pkg_cache_create() { } #else +const char *to_c_string(std::string s); + +#include +bool can_commit() { + // Allow user with effective SU to acquire lock + return geteuid() == 0; +} + + +// See ALT's apt/cmdline +// Used to print out the progress +// Set to stdout for now +ostream progress_stream(0); PCache *pkg_cache_create() { // Maybe should be set to false sometimes - // (Set to true always for now) + // (Set to can_commit() for now) // We should lock for installation, etc. // For read-only access no locking needed - // Ideally locking semantic should be simillar to ALT's apt CLI - const bool WithLock = true; + // In apt-shell, WithLock is always set according to (geteuid() == 0). + const bool WithLock = can_commit(); - pkgCacheFile *cache_file = new pkgCacheFile(WithLock); - // there is a cast operator from pkgCacheFile to pkgCache* + CacheFile *cache_file = new CacheFile(progress_stream, WithLock); + cache_file->Open(); + + if (cache_file->CheckDeps(true) == false) { + delete cache_file; + return nullptr; + } + + // cast operator from CacheFile to pkgCache* pkgCache *cache = *cache_file; pkgRecords *records = new pkgRecords(*cache); @@ -306,28 +325,36 @@ const char *pkg_iter_candidate_version(PPkgIterator *wrapper) { #else - -// Creates PVerIterator using PPkgIterator data -// (implementation taken from pkg_iter_ver_iter()) -PVerIterator create_ver_iter(PPkgIterator *pkg_wrapper) { - PVerIterator ver_wrapper; - ver_wrapper.iterator = pkg_wrapper->iterator.VersionList(); - ver_wrapper.pkg = &pkg_wrapper->iterator; - ver_wrapper.cache = pkg_wrapper->cache; - return ver_wrapper; -} - const char *pkg_iter_arch(PPkgIterator *wrapper) { - pkgCache::VerIterator it = create_ver_iter(wrapper).iterator; + pkgCache::VerIterator it = wrapper->iterator.VersionList(); if (it.end()) { - return nullptr; + // It's a virtual package + // Let's take some of providers and take it's arch + pkgCache::PrvIterator provide_list = wrapper->iterator.ProvidesList(); + if (provide_list.end()) { + // Return 'something' in case of broken packages + // I would prefer returning nullptr, + // but Rust-side requires it to be non-null + return to_c_string("Undefined"); + } + pkgCache::PkgIterator provide = provide_list.OwnerPkg(); + if (provide.end()) { + return to_c_string("Undefined"); + } + + PPkgIterator new_wrapper; + new_wrapper.iterator = provide; + new_wrapper.cache = wrapper->cache; + + return pkg_iter_arch(&new_wrapper); + } else { + return it.Arch(); } - return it.Arch(); } const char *pkg_iter_current_version(PPkgIterator *wrapper) { - pkgCache::VerIterator it = create_ver_iter(wrapper).iterator; + pkgCache::VerIterator it = wrapper->iterator.CurrentVer(); if (it.end()) { return nullptr; @@ -337,7 +364,7 @@ const char *pkg_iter_current_version(PPkgIterator *wrapper) { const char *pkg_iter_candidate_version(PPkgIterator *wrapper) { // pkgCache::VerIterator it = wrapper->cache->cache_file->GetPolicy(Progress)->GetCandidateVer(wrapper->iterator); - // Getting rid of GetPolicy call by creating PkgPolicy object with constructor (no need in OpProgress) + // Getting rid of GetPolicy call by creating PkgPolicy object via constructor (no need in OpProgress) // (This is done this way in cmdline apt utility (see ALT's apt/cmdline sources)) pkgPolicy plcy(wrapper->cache->cache); pkgCache::VerIterator it = plcy.GetCandidateVer(wrapper->iterator); @@ -404,7 +431,6 @@ int32_t ver_iter_priority(PVerIterator *wrapper) { #endif #ifdef ALT_LINUX -const char *to_c_string(std::string s); const char *ver_iter_source_package(PVerIterator *wrapper) { pkgRecords *recs = wrapper->cache->records; @@ -415,9 +441,10 @@ const char *ver_iter_source_package(PVerIterator *wrapper) { return to_c_string(parse.SourcePkg()); } +// SHOULD BE REMOVED FROM ALT'S VERSION! MEANINGLESS! +// Apt only works with rpms and not src.rpm const char *ver_iter_source_version(PVerIterator *wrapper) { - // TODO: haven't found a way to get source version yet! - return to_c_string("Not Specified"); + return wrapper->iterator.VerStr(); } int32_t ver_iter_priority(PVerIterator *wrapper) { @@ -523,6 +550,9 @@ const char *ver_file_parser_homepage(PVerFileParser *parser) { return to_c_string(hp); } #else + +// Should be erased from Alt's version or Alt's Apt should provide +// functionality for getting homepage address const char *ver_file_parser_homepage(PVerFileParser *parser) { // Unimplementable in ALT via PVerFileParser // Maybe should remove it in ALT version of lib (or just return nullptr)