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/cachefile.h>
2017-07-20 14:45:30 +03:00
# include <apt-pkg/pkgcache.h>
# include <apt-pkg/version.h>
2017-07-12 23:18:34 +03:00
2019-12-30 00:36:49 +03:00
# include <apt-pkg/init.h>
# include <apt-pkg/pkgsystem.h>
# include <apt-pkg/policy.h>
2017-07-12 23:18:34 +03:00
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 ;
2020-07-13 18:17:48 +03:00
// Owned by us.
pkgRecords * records ;
2017-07-13 12:04:18 +03:00
} ;
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-14 13:48:56 +03:00
struct PVerFileIterator {
// Owned by us.
pkgCache : : VerFileIterator iterator ;
2020-07-13 18:17:48 +03:00
// Borrow of "static" PCache.
PCache * cache ;
2017-07-14 13:48:56 +03:00
} ;
struct PPkgFileIterator {
// Owned by us.
pkgCache : : PkgFileIterator iterator ;
} ;
2020-07-13 18:17:48 +03:00
struct PVerFileParser {
pkgRecords : : Parser * parser ;
} ;
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-11-17 12:53:01 +03:00
void pkg_cache_release ( PCache * cache ) ;
2017-07-12 23:18:34 +03:00
2017-07-20 14:45:30 +03:00
int32_t pkg_cache_compare_versions ( PCache * cache , const char * left , const char * right ) ;
2017-07-14 13:48:56 +03:00
// pkg_iter creation and deletion
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-14 13:48:56 +03:00
// pkg_iter mutation
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
2017-07-14 13:48:56 +03:00
// pkg_iter access
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
2017-07-14 13:48:56 +03:00
// ver_iter creation and deletion
2017-07-13 16:44:14 +03:00
PVerIterator * pkg_iter_ver_iter ( PPkgIterator * iterator ) ;
void ver_iter_release ( PVerIterator * iterator ) ;
2017-07-14 13:48:56 +03:00
// ver_iter mutation
2017-07-13 16:44:14 +03:00
void ver_iter_next ( PVerIterator * iterator ) ;
bool ver_iter_end ( PVerIterator * iterator ) ;
2017-07-14 13:48:56 +03:00
// ver_iter access
2017-07-13 16:44:14 +03:00
const char * ver_iter_version ( PVerIterator * iterator ) ;
const char * ver_iter_section ( PVerIterator * iterator ) ;
2017-12-12 23:29:27 +03:00
const char * ver_iter_arch ( PVerIterator * iterator ) ;
2020-07-13 18:16:19 +03:00
const char * ver_iter_priority_type ( PVerIterator * wrapper ) ;
2017-12-12 23:29:27 +03:00
# ifndef YE_OLDE_APT
2017-07-13 16:44:14 +03:00
const char * ver_iter_source_package ( PVerIterator * iterator ) ;
const char * ver_iter_source_version ( PVerIterator * iterator ) ;
2017-07-13 17:31:11 +03:00
int32_t ver_iter_priority ( PVerIterator * iterator ) ;
2017-12-12 23:29:27 +03:00
# endif
2017-07-14 13:48:56 +03:00
// ver_file_iter creation and deletion
PVerFileIterator * ver_iter_ver_file_iter ( PVerIterator * iterator ) ;
void ver_file_iter_release ( PVerFileIterator * iterator ) ;
// ver_file_iter mutation
void ver_file_iter_next ( PVerFileIterator * iterator ) ;
bool ver_file_iter_end ( PVerFileIterator * iterator ) ;
2020-07-13 18:17:48 +03:00
// ver_file_parser creation
PVerFileParser * ver_file_iter_get_parser ( PVerFileIterator * iterator ) ;
// ver_file_parser access
const char * ver_file_parser_short_desc ( PVerFileParser * parser ) ;
const char * ver_file_parser_long_desc ( PVerFileParser * parser ) ;
const char * ver_file_parser_maintainer ( PVerFileParser * parser ) ;
const char * ver_file_parser_homepage ( PVerFileParser * parser ) ;
2017-07-14 13:48:56 +03:00
// ver_file_iter has no accessors, only the creation of pkg_file_iter
// pkg_file_iter creation
PPkgFileIterator * ver_file_iter_pkg_file_iter ( PVerFileIterator * iterator ) ;
void pkg_file_iter_release ( PPkgFileIterator * iterator ) ;
// pkg_file_iter mutation
void pkg_file_iter_next ( PPkgFileIterator * iterator ) ;
bool pkg_file_iter_end ( PPkgFileIterator * iterator ) ;
// pkg_file_iter access
const char * pkg_file_iter_file_name ( PPkgFileIterator * iterator ) ;
const char * pkg_file_iter_archive ( PPkgFileIterator * iterator ) ;
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 ) ;
const char * pkg_file_iter_architecture ( PPkgFileIterator * iterator ) ;
const char * pkg_file_iter_index_type ( PPkgFileIterator * 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 ( ) ;
2020-07-13 18:17:48 +03:00
pkgRecords * records = new pkgRecords ( * cache ) ;
2017-07-12 23:18:34 +03:00
PCache * ret = new PCache ( ) ;
ret - > cache_file = cache_file ;
ret - > cache = cache ;
2020-07-13 18:17:48 +03:00
ret - > records = records ;
2017-07-12 23:18:34 +03:00
return ret ;
}
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?
2020-07-13 18:17:48 +03:00
delete cache - > records ;
2017-07-12 23:18:34 +03:00
delete cache - > cache_file ;
delete cache ;
}
2017-07-20 14:45:30 +03:00
int32_t pkg_cache_compare_versions ( PCache * cache , const char * left , const char * right ) {
// an int is returned here; presumably it will always be -1, 0 or 1.
return cache - > cache - > VS - > DoCmpVersion ( left , left + strlen ( left ) , right , right + strlen ( right ) ) ;
}
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 ) {
2017-07-13 23:50:53 +03:00
pkgCache : : VerIterator it = wrapper - > cache - > cache_file - > GetPolicy ( ) - > GetCandidateVer ( wrapper - > iterator ) ;
if ( it . end ( ) ) {
return nullptr ;
}
return it . VerStr ( ) ;
2017-07-13 11:36:38 +03:00
}
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 ( ) ;
}
2020-07-13 18:16:19 +03:00
const char * ver_iter_priority_type ( PVerIterator * wrapper ) {
return wrapper - > iterator . PriorityType ( ) ;
}
2017-12-12 23:29:27 +03:00
# ifndef YE_OLDE_APT
2017-07-13 16:44:14 +03:00
const char * ver_iter_source_package ( PVerIterator * wrapper ) {
return wrapper - > iterator . SourcePkgName ( ) ;
}
const char * ver_iter_source_version ( PVerIterator * wrapper ) {
return wrapper - > iterator . SourceVerStr ( ) ;
}
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 ) ;
}
2017-07-14 13:48:56 +03:00
2017-12-12 23:29:27 +03:00
# endif
const char * ver_iter_arch ( PVerIterator * wrapper ) {
return wrapper - > iterator . Arch ( ) ;
}
2017-07-14 13:48:56 +03:00
PVerFileIterator * ver_iter_ver_file_iter ( PVerIterator * wrapper ) {
PVerFileIterator * new_wrapper = new PVerFileIterator ( ) ;
new_wrapper - > iterator = wrapper - > iterator . FileList ( ) ;
2020-07-13 18:17:48 +03:00
new_wrapper - > cache = wrapper - > cache ;
2017-07-14 13:48:56 +03:00
return new_wrapper ;
}
void ver_file_iter_release ( PVerFileIterator * wrapper ) {
delete wrapper ;
}
void ver_file_iter_next ( PVerFileIterator * wrapper ) {
+ + wrapper - > iterator ;
}
2020-07-13 18:17:48 +03:00
PVerFileParser * ver_file_iter_get_parser ( PVerFileIterator * wrapper ) {
PVerFileParser * parser = new PVerFileParser ( ) ;
parser - > parser = & wrapper - > cache - > records - > Lookup ( wrapper - > iterator ) ;
return parser ;
}
const char * to_c_string ( std : : string s ) {
char * cstr = new char [ s . length ( ) + 1 ] ;
std : : strcpy ( cstr , s . c_str ( ) ) ;
return cstr ;
}
const char * ver_file_parser_short_desc ( PVerFileParser * parser ) {
std : : string desc = parser - > parser - > ShortDesc ( ) ;
return to_c_string ( desc ) ;
}
const char * ver_file_parser_long_desc ( PVerFileParser * parser ) {
std : : string desc = parser - > parser - > LongDesc ( ) ;
return to_c_string ( desc ) ;
}
const char * ver_file_parser_maintainer ( PVerFileParser * parser ) {
std : : string maint = parser - > parser - > Maintainer ( ) ;
return to_c_string ( maint ) ;
}
const char * ver_file_parser_homepage ( PVerFileParser * parser ) {
std : : string hp = parser - > parser - > Homepage ( ) ;
return to_c_string ( hp ) ;
}
2017-07-14 13:48:56 +03:00
bool ver_file_iter_end ( PVerFileIterator * wrapper ) {
return wrapper - > iterator . end ( ) ;
}
PPkgFileIterator * ver_file_iter_pkg_file_iter ( PVerFileIterator * wrapper ) {
PPkgFileIterator * new_wrapper = new PPkgFileIterator ( ) ;
new_wrapper - > iterator = wrapper - > iterator . File ( ) ;
return new_wrapper ;
}
void pkg_file_iter_release ( PPkgFileIterator * wrapper ) {
delete wrapper ;
}
void pkg_file_iter_next ( PPkgFileIterator * wrapper ) {
+ + wrapper - > iterator ;
}
bool pkg_file_iter_end ( PPkgFileIterator * wrapper ) {
return wrapper - > iterator . end ( ) ;
}
const char * pkg_file_iter_file_name ( PPkgFileIterator * wrapper ) {
return wrapper - > iterator . FileName ( ) ;
}
const char * pkg_file_iter_archive ( PPkgFileIterator * wrapper ) {
return wrapper - > iterator . Archive ( ) ;
}
const char * pkg_file_iter_version ( PPkgFileIterator * wrapper ) {
return wrapper - > iterator . Version ( ) ;
}
const char * pkg_file_iter_origin ( PPkgFileIterator * wrapper ) {
return wrapper - > iterator . Origin ( ) ;
}
const char * pkg_file_iter_codename ( PPkgFileIterator * wrapper ) {
return wrapper - > iterator . Codename ( ) ;
}
const char * pkg_file_iter_label ( PPkgFileIterator * wrapper ) {
return wrapper - > iterator . Label ( ) ;
}
const char * pkg_file_iter_site ( PPkgFileIterator * wrapper ) {
return wrapper - > iterator . Site ( ) ;
}
const char * pkg_file_iter_component ( PPkgFileIterator * wrapper ) {
return wrapper - > iterator . Component ( ) ;
}
const char * pkg_file_iter_architecture ( PPkgFileIterator * wrapper ) {
return wrapper - > iterator . Architecture ( ) ;
}
const char * pkg_file_iter_index_type ( PPkgFileIterator * wrapper ) {
return wrapper - > iterator . IndexType ( ) ;
}