dev #1

Merged
kozorizki merged 7 commits from dev into altlinux 2024-10-25 15:07:01 +03:00
4 changed files with 183 additions and 16 deletions

4
.gitignore vendored
View File

@ -5,3 +5,7 @@ Cargo.lock
*.iml
apt-c/lib.o
apt-c/libapt-c.a
.clangd
compile_commands.json
.vscode

View File

@ -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;
@ -81,7 +85,11 @@ extern "C" {
// pkg_iter creation and deletion
PPkgIterator *pkg_cache_pkg_iter(PCache *cache);
PPkgIterator *pkg_cache_find_name(PCache *cache, const char *name);
#ifndef ALT_LINUX
PPkgIterator *pkg_cache_find_name_arch(PCache *cache, const char *name, const char *arch);
#else
PPkgIterator *pkg_cache_find_name_arch(PCache *cache, const char *name);
#endif
void pkg_iter_release(PPkgIterator *iterator);
// pkg_iter mutation
@ -94,7 +102,6 @@ extern "C" {
const char *pkg_iter_current_version(PPkgIterator *iterator);
const char *pkg_iter_candidate_version(PPkgIterator *iterator);
// ver_iter creation and deletion
PVerIterator *pkg_iter_ver_iter(PPkgIterator *iterator);
void ver_iter_release(PVerIterator *iterator);
@ -108,7 +115,7 @@ extern "C" {
const char *ver_iter_section(PVerIterator *iterator);
const char *ver_iter_arch(PVerIterator *iterator);
const char *ver_iter_priority_type(PVerIterator *wrapper);
#ifndef YE_OLDE_APT
const char *ver_iter_source_package(PVerIterator *iterator);
const char *ver_iter_source_version(PVerIterator *iterator);
@ -163,6 +170,7 @@ extern "C" {
const char *pkg_file_iter_version(PPkgFileIterator *iterator);
const char *pkg_file_iter_origin(PPkgFileIterator *iterator);
const char *pkg_file_iter_codename(PPkgFileIterator *iterator);
const char *pkg_file_iter_label(PPkgFileIterator *iterator);
const char *pkg_file_iter_site(PPkgFileIterator *iterator);
const char *pkg_file_iter_component(PPkgFileIterator *iterator);
@ -191,10 +199,38 @@ PCache *pkg_cache_create() {
}
#else
const char *to_c_string(std::string s);
PCache *pkg_cache_create(const bool WithLock, OpProgress &Progress) {
pkgCacheFile *cache_file = new pkgCacheFile(WithLock);
pkgCache *cache = cache_file->GetPkgCache(Progress);
#include <unistd.h>
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 can_commit() for now)
// We should lock for installation, etc.
// For read-only access no locking needed
// In apt-shell, WithLock is always set according to (geteuid() == 0).
const bool WithLock = can_commit();
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);
PCache *ret = new PCache();
@ -289,16 +325,50 @@ const char *pkg_iter_candidate_version(PPkgIterator *wrapper) {
#else
// const char *pkg_iter_arch(PPkgIterator *wrapper) {
// return wrapper->iterator.Arch();
// }
const char *pkg_iter_arch(PPkgIterator *wrapper) {
pkgCache::VerIterator it = wrapper->iterator.VersionList();
// const char *pkg_iter_current_version(PPkgIterator *wrapper) {
// return wrapper->iterator.CurVersion();
// }
if (it.end()) {
// 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();
}
}
const char *pkg_iter_current_version(PPkgIterator *wrapper) {
pkgCache::VerIterator it = wrapper->iterator.CurrentVer();
if (it.end()) {
return nullptr;
}
return it.VerStr();
}
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 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);
const char *pkg_iter_candidate_version(PPkgIterator *wrapper, OpProgress &Progress) {
pkgCache::VerIterator it = wrapper->cache->cache_file->GetPolicy(Progress)->GetCandidateVer(wrapper->iterator);
if (it.end()) {
return nullptr;
}
@ -360,6 +430,33 @@ int32_t ver_iter_priority(PVerIterator *wrapper) {
#endif
#endif
#ifdef ALT_LINUX
const char *ver_iter_source_package(PVerIterator *wrapper) {
pkgRecords *recs = wrapper->cache->records;
pkgRecords::Parser &parse = recs->Lookup(wrapper->iterator.FileList());
// It's okay to return raw pointer to heap memory, due to convention in Rust-caller
// (it will be freed by an owning 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) {
return wrapper->iterator.VerStr();
}
int32_t ver_iter_priority(PVerIterator *wrapper) {
// return wrapper->cache->cache_file->GetPolicy(Progress)->GetPriority(*wrapper->pkg);
// Getting rid of GetPolicy call by creating PkgPolicy object with 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);
return plcy.GetPriority(*wrapper->pkg);
}
#endif
const char *ver_iter_arch(PVerIterator *wrapper) {
return wrapper->iterator.Arch();
}
@ -449,12 +546,23 @@ const char *ver_file_parser_maintainer(PVerFileParser *parser) {
#ifndef ALT_LINUX
const char *ver_file_parser_homepage(PVerFileParser *parser) {
std::string hp = parser->parser->Homepage();
return to_c_string(hp);
std::string hp = parser->parser->Homepage();
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)
// Not sure in use of nullptr due to possible absense of checks from caller
// So return dummy string
return to_c_string("Not Specified");
}
#endif
bool ver_file_iter_end(PVerFileIterator *wrapper) {
return wrapper->iterator.end();
}
@ -481,9 +589,20 @@ const char *pkg_file_iter_file_name(PPkgFileIterator *wrapper) {
return wrapper->iterator.FileName();
}
#ifndef ALT_LINUX
const char *pkg_file_iter_archive(PPkgFileIterator *wrapper) {
return wrapper->iterator.Archive();
}
#else
const char *pkg_file_iter_archive(PPkgFileIterator *wrapper) {
const char *archive = wrapper->iterator.Archive();
if (archive == nullptr) {
// Should be non-null
return to_c_string("Not Specified");
}
return archive;
}
#endif
const char *pkg_file_iter_version(PPkgFileIterator *wrapper) {
return wrapper->iterator.Version();
@ -496,9 +615,16 @@ const char *pkg_file_iter_origin(PPkgFileIterator *wrapper) {
#ifndef ALT_LINUX
const char *pkg_file_iter_codename(PPkgFileIterator *wrapper) {
return wrapper->iterator.Codename();
return wrapper->iterator.Codename();
}
#else
const char *pkg_file_iter_codename(PPkgFileIterator *wrapper) {
// From Debian docs:
/* "The Codename field shall describe the codename of the release.*/
// Analogues in ALT is Branch name.
return wrapper->iterator.Label();
}
#endif
const char *pkg_file_iter_label(PPkgFileIterator *wrapper) {
@ -509,14 +635,37 @@ const char *pkg_file_iter_site(PPkgFileIterator *wrapper) {
return wrapper->iterator.Site();
}
#ifndef ALT_LINUX
const char *pkg_file_iter_component(PPkgFileIterator *wrapper) {
return wrapper->iterator.Component();
}
#else
const char *pkg_file_iter_component(PPkgFileIterator *wrapper) {
const char *component = wrapper->iterator.Component();
if (component == nullptr) {
// Should be non-null
return to_c_string("Not Specified");
}
return component;
}
#endif
const char *pkg_file_iter_architecture(PPkgFileIterator *wrapper) {
return wrapper->iterator.Architecture();
}
#ifndef ALT_LINUX
const char *pkg_file_iter_index_type(PPkgFileIterator *wrapper) {
return wrapper->iterator.IndexType();
}
#else
const char *pkg_file_iter_index_type(PPkgFileIterator *wrapper) {
const char *index_type = wrapper->iterator.IndexType();
if (index_type == nullptr) {
// Should be non-null
return to_c_string("Not Specified");
}
return index_type;
}
#endif

View File

@ -35,11 +35,19 @@ extern "C" {
pub fn pkg_cache_pkg_iter(cache: PCache) -> PPkgIterator;
pub fn pkg_cache_find_name(cache: PCache, name: *const c_char) -> PPkgIterator;
#[cfg(not(feature = "alt-linux"))]
pub fn pkg_cache_find_name_arch(
cache: PCache,
name: *const c_char,
arch: *const c_char,
) -> PPkgIterator;
#[cfg(feature = "alt-linux")]
pub fn pkg_cache_find_name_arch(
cache: PCache,
name: *const c_char,
) -> PPkgIterator;
pub fn pkg_iter_release(iterator: PPkgIterator);
pub fn pkg_iter_next(iterator: PPkgIterator);

View File

@ -66,7 +66,13 @@ impl Cache {
unsafe {
let name = ffi::CString::new(name).unwrap();
let arch = ffi::CString::new(arch).unwrap();
#[cfg(feature = "alt-linux")]
let ptr = raw::pkg_cache_find_name_arch(lock.ptr, name.as_ptr());
#[cfg(not(feature = "alt-linux"))]
let ptr = raw::pkg_cache_find_name_arch(lock.ptr, name.as_ptr(), arch.as_ptr());
PkgIterator::new(lock, ptr)
}
}