try and stop reinit segfaulting; doesn't really work

This commit is contained in:
Chris West (Faux) 2017-07-13 11:32:33 +01:00
parent e90ee73dc8
commit 7562f18141
4 changed files with 26 additions and 26 deletions

View File

@ -23,6 +23,8 @@ struct PPkgIterator {
};
extern "C" {
void init_config_system();
PCache *pkg_cache_create();
void pkg_cache_release(PCache *cache);
@ -38,10 +40,12 @@ extern "C" {
char *pkg_iter_pretty(PCache *cache, PPkgIterator *iterator);
}
PCache *pkg_cache_create() {
void init_config_system() {
pkgInitConfig(*_config);
pkgInitSystem(*_config, _system);
}
PCache *pkg_cache_create() {
pkgCacheFile *cache_file = new pkgCacheFile();
pkgCache *cache = cache_file->GetPkgCache();

View File

@ -7,29 +7,6 @@ pub mod sane;
mod tests {
use super::*;
use std::ffi::CStr;
// Leaks on panic.
#[test]
fn goin_in_raw() {
unsafe {
let cache = raw::pkg_cache_create();
let iter = raw::pkg_cache_pkg_iter(cache);
loop {
println!("{}", CStr::from_ptr(raw::pkg_iter_name(iter)).to_str().expect("package names are always valid utf-8, in that they're always valid low ascii"));
let pretty = raw::pkg_iter_pretty(cache, iter);
println!("{}", CStr::from_ptr(pretty).to_str().expect("package names are always valid utf-8, in that they're always valid low ascii"));
libc::free(pretty as *mut libc::c_void);
raw::pkg_iter_next(iter);
if raw::pkg_iter_end(iter) {
break;
}
}
raw::pkg_iter_release(iter);
raw::pkg_cache_release(cache);
}
}
#[test]
fn list_all() {
let mut cache = sane::Cache::new();

View File

@ -13,6 +13,10 @@ pub type PPkgIterator = *mut c_void;
#[link(name = "apt-pkg")]
#[link(name = "stdc++")]
extern {
/// Must be called exactly once, before anything else?
fn init_config_system();
/// I'm not convinced you can even call this multiple times.
pub fn pkg_cache_create() -> PCache;
pub fn pkg_cache_release(cache: PCache);
@ -25,3 +29,15 @@ extern {
pub fn pkg_iter_name(iterator: PPkgIterator) -> *const c_char;
pub fn pkg_iter_pretty(cache: PCache, iterator: PPkgIterator) -> *mut c_char;
}
static mut INIT_CONFIG_CALLED: bool = false;
pub unsafe fn init_config_system_once() {
if INIT_CONFIG_CALLED {
return;
}
INIT_CONFIG_CALLED = true;
init_config_system()
}

View File

@ -19,8 +19,11 @@ impl Drop for Cache {
impl Cache {
pub fn new() -> Cache {
Cache {
ptr: unsafe { raw::pkg_cache_create() }
unsafe {
raw::init_config_system_once();
Cache {
ptr: raw::pkg_cache_create()
}
}
}