2017-07-13 11:36:38 +03:00
# include <sstream>
2017-07-13 17:31:11 +03:00
# include <cstdint>
2017-07-13 11:36:38 +03:00
# include <assert.h>
2017-07-12 23:18:34 +03:00
# include <apt-pkg/pkgcache.h>
2017-07-13 11:36:38 +03:00
# include <apt-pkg/prettyprinters.h>
2017-07-12 23:18:34 +03:00
# include <apt-pkg/cachefile.h>
struct PCache {
2017-07-13 12:04:18 +03:00
// Owned by us.
2017-07-12 23:18:34 +03:00
pkgCacheFile * cache_file ;
2017-07-13 12:04:18 +03:00
// Borrowed from cache_file.
pkgCache * cache ;
} ;
struct PPkgIterator {
// Owned by us.
pkgCache : : PkgIterator iterator ;
2017-07-13 17:31:11 +03:00
// Borrow of "static" PCache.
PCache * cache ;
2017-07-12 23:18:34 +03:00
} ;
2017-07-13 16:44:14 +03:00
struct PVerIterator {
// Owned by us.
pkgCache : : VerIterator iterator ;
// Borrowed from PCache.
pkgCache : : PkgIterator * pkg ;
2017-07-13 17:31:11 +03:00
// Borrow of "static" PCache.
PCache * cache ;
2017-07-13 16:44:14 +03:00
} ;
2017-07-12 23:18:34 +03:00
extern " C " {
2017-07-13 13:32:33 +03:00
void init_config_system ( ) ;
2017-07-13 12:04:18 +03:00
PCache * pkg_cache_create ( ) ;
2017-07-12 23:18:34 +03:00
2017-07-13 12:04:18 +03:00
PPkgIterator * pkg_cache_pkg_iter ( PCache * cache ) ;
2017-07-13 15:24:54 +03:00
PPkgIterator * pkg_cache_find_name ( PCache * cache , const char * name ) ;
PPkgIterator * pkg_cache_find_name_arch ( PCache * cache , const char * name , const char * arch ) ;
2017-07-13 12:04:18 +03:00
void pkg_iter_release ( PPkgIterator * iterator ) ;
2017-07-13 13:03:31 +03:00
void pkg_iter_next ( PPkgIterator * iterator ) ;
bool pkg_iter_end ( PPkgIterator * iterator ) ;
2017-07-13 12:04:18 +03:00
const char * pkg_iter_name ( PPkgIterator * iterator ) ;
2017-07-13 13:57:29 +03:00
const char * pkg_iter_arch ( PPkgIterator * iterator ) ;
const char * pkg_iter_current_version ( PPkgIterator * iterator ) ;
2017-07-13 17:31:11 +03:00
const char * pkg_iter_candidate_version ( PPkgIterator * iterator ) ;
2017-07-13 11:36:38 +03:00
// freed by caller
2017-07-13 12:04:18 +03:00
char * pkg_iter_pretty ( PCache * cache , PPkgIterator * iterator ) ;
2017-07-13 13:57:29 +03:00
2017-07-13 16:44:14 +03:00
PVerIterator * pkg_iter_ver_iter ( PPkgIterator * iterator ) ;
void ver_iter_release ( PVerIterator * iterator ) ;
void ver_iter_next ( PVerIterator * iterator ) ;
bool ver_iter_end ( PVerIterator * iterator ) ;
const char * ver_iter_version ( PVerIterator * iterator ) ;
const char * ver_iter_section ( PVerIterator * iterator ) ;
const char * ver_iter_source_package ( PVerIterator * iterator ) ;
const char * ver_iter_source_version ( PVerIterator * iterator ) ;
const char * ver_iter_arch ( PVerIterator * iterator ) ;
2017-07-13 17:31:11 +03:00
int32_t ver_iter_priority ( PVerIterator * iterator ) ;
2017-07-12 23:18:34 +03:00
}
2017-07-13 13:32:33 +03:00
void init_config_system ( ) {
2017-07-12 23:18:34 +03:00
pkgInitConfig ( * _config ) ;
pkgInitSystem ( * _config , _system ) ;
2017-07-13 13:32:33 +03:00
}
2017-07-12 23:18:34 +03:00
2017-07-13 13:32:33 +03:00
PCache * pkg_cache_create ( ) {
2017-07-12 23:18:34 +03:00
pkgCacheFile * cache_file = new pkgCacheFile ( ) ;
pkgCache * cache = cache_file - > GetPkgCache ( ) ;
PCache * ret = new PCache ( ) ;
ret - > cache_file = cache_file ;
ret - > cache = cache ;
return ret ;
}
2017-07-13 16:44:14 +03:00
// TODO: we don't expose this so we always leak the wrapper.
2017-07-13 12:04:18 +03:00
void pkg_cache_release ( PCache * cache ) {
2017-07-13 11:36:38 +03:00
// TODO: is cache->cache cleaned up with cache->cache_file?
2017-07-12 23:18:34 +03:00
delete cache - > cache_file ;
delete cache ;
}
2017-07-13 12:04:18 +03:00
PPkgIterator * pkg_cache_pkg_iter ( PCache * cache ) {
PPkgIterator * wrapper = new PPkgIterator ( ) ;
wrapper - > iterator = cache - > cache - > PkgBegin ( ) ;
2017-07-13 17:31:11 +03:00
wrapper - > cache = cache ;
2017-07-13 12:04:18 +03:00
return wrapper ;
}
2017-07-13 15:24:54 +03:00
PPkgIterator * pkg_cache_find_name ( PCache * cache , const char * name ) {
PPkgIterator * wrapper = new PPkgIterator ( ) ;
wrapper - > iterator = cache - > cache - > FindPkg ( name ) ;
2017-07-13 17:31:11 +03:00
wrapper - > cache = cache ;
2017-07-13 15:24:54 +03:00
return wrapper ;
}
PPkgIterator * pkg_cache_find_name_arch ( PCache * cache , const char * name , const char * arch ) {
PPkgIterator * wrapper = new PPkgIterator ( ) ;
wrapper - > iterator = cache - > cache - > FindPkg ( name , arch ) ;
2017-07-13 17:31:11 +03:00
wrapper - > cache = cache ;
2017-07-13 15:24:54 +03:00
return wrapper ;
}
2017-07-13 12:04:18 +03:00
void pkg_iter_release ( PPkgIterator * wrapper ) {
delete wrapper ;
}
2017-07-12 23:18:34 +03:00
2017-07-13 13:03:31 +03:00
void pkg_iter_next ( PPkgIterator * wrapper ) {
2017-07-13 12:04:18 +03:00
+ + wrapper - > iterator ;
2017-07-13 13:03:31 +03:00
}
bool pkg_iter_end ( PPkgIterator * wrapper ) {
2017-07-13 17:31:11 +03:00
return wrapper - > cache - > cache - > PkgEnd ( ) = = wrapper - > iterator ;
2017-07-12 23:18:34 +03:00
}
2017-07-13 11:36:38 +03:00
2017-07-13 12:04:18 +03:00
const char * pkg_iter_name ( PPkgIterator * wrapper ) {
return wrapper - > iterator . Name ( ) ;
2017-07-13 11:36:38 +03:00
}
2017-07-13 13:57:29 +03:00
const char * pkg_iter_arch ( PPkgIterator * wrapper ) {
return wrapper - > iterator . Arch ( ) ;
}
const char * pkg_iter_current_version ( PPkgIterator * wrapper ) {
return wrapper - > iterator . CurVersion ( ) ;
}
2017-07-13 17:31:11 +03:00
const char * pkg_iter_candidate_version ( PPkgIterator * wrapper ) {
return wrapper - > cache - > cache_file - > GetPolicy ( ) - > GetCandidateVer ( wrapper - > iterator ) . VerStr ( ) ;
}
2017-07-13 12:04:18 +03:00
char * pkg_iter_pretty ( PCache * cache , PPkgIterator * wrapper ) {
2017-07-13 11:36:38 +03:00
assert ( cache ) ;
2017-07-13 12:04:18 +03:00
assert ( wrapper ) ;
2017-07-13 11:36:38 +03:00
std : : stringstream ss ;
2017-07-13 12:04:18 +03:00
ss < < APT : : PrettyPkg ( cache - > cache_file - > GetDepCache ( ) , wrapper - > iterator ) ;
2017-07-13 11:36:38 +03:00
return strdup ( ss . str ( ) . c_str ( ) ) ;
}
2017-07-13 16:44:14 +03:00
PVerIterator * pkg_iter_ver_iter ( PPkgIterator * wrapper ) {
PVerIterator * new_wrapper = new PVerIterator ( ) ;
new_wrapper - > iterator = wrapper - > iterator . VersionList ( ) ;
new_wrapper - > pkg = & wrapper - > iterator ;
2017-07-13 17:31:11 +03:00
new_wrapper - > cache = wrapper - > cache ;
2017-07-13 16:44:14 +03:00
return new_wrapper ;
}
void ver_iter_release ( PVerIterator * wrapper ) {
delete wrapper ;
}
void ver_iter_next ( PVerIterator * wrapper ) {
+ + wrapper - > iterator ;
}
bool ver_iter_end ( PVerIterator * wrapper ) {
return wrapper - > iterator . end ( ) ;
}
const char * ver_iter_version ( PVerIterator * wrapper ) {
return wrapper - > iterator . VerStr ( ) ;
}
const char * ver_iter_section ( PVerIterator * wrapper ) {
return wrapper - > iterator . Section ( ) ;
}
const char * ver_iter_source_package ( PVerIterator * wrapper ) {
return wrapper - > iterator . SourcePkgName ( ) ;
}
const char * ver_iter_source_version ( PVerIterator * wrapper ) {
return wrapper - > iterator . SourceVerStr ( ) ;
}
const char * ver_iter_arch ( PVerIterator * wrapper ) {
return wrapper - > iterator . Arch ( ) ;
}
2017-07-13 17:31:11 +03:00
int32_t ver_iter_priority ( PVerIterator * wrapper ) {
// The priority is a "short", which is roughly a (signed) int16_t;
// going bigger just in case
return wrapper - > cache - > cache_file - > GetPolicy ( ) - > GetPriority ( wrapper - > iterator ) ;
}