add a reload() (#1)

This commit is contained in:
Chris West (Faux) 2017-11-17 09:53:01 +00:00
parent 45741e2de1
commit b104724ccf
5 changed files with 31 additions and 2 deletions

View File

@ -9,7 +9,7 @@ license = "MIT"
name = "apt-pkg-native"
readme = "README.md"
repository = "https://github.com/FauxFaux/apt-pkg-native-rs"
version = "0.2.1"
version = "0.3.0"
[badges.travis-ci]
repository = "FauxFaux/apt-pkg-native-rs"

View File

@ -48,6 +48,7 @@ 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);
@ -133,7 +134,6 @@ PCache *pkg_cache_create() {
return ret;
}
// TODO: we don't expose this so we always leak the wrapper.
void pkg_cache_release(PCache *cache) {
// TODO: is cache->cache cleaned up with cache->cache_file?
delete cache->cache_file;

View File

@ -87,4 +87,13 @@ mod tests {
assert_eq!(Ordering::Greater, cache.compare_versions("3.1", "3.0"));
assert_eq!(Ordering::Equal, cache.compare_versions("3.0", "3.0"));
}
#[test]
fn reload() {
let mut cache = Cache::get_singleton();
cache.reload();
cache.reload();
cache.reload();
cache.reload();
}
}

View File

@ -20,6 +20,7 @@ extern "C" {
/// Must be called exactly once, before anything else?
fn init_config_system();
fn pkg_cache_create() -> PCache;
fn pkg_cache_release(cache: PCache);
pub fn pkg_cache_compare_versions(
cache: PCache,
@ -108,6 +109,15 @@ pub struct CacheHolder {
unsafe impl Send for CacheHolder {}
impl CacheHolder {
pub fn re_up(&mut self) {
unsafe {
pkg_cache_release(self.ptr);
self.ptr = pkg_cache_create();
}
}
}
lazy_static! {
#[derive(Debug)]
pub static ref CACHE_SINGLETON: Mutex<CacheHolder> = {

View File

@ -24,6 +24,16 @@ impl Cache {
}
}
/// Drop the cache, and re-create it from scratch.
///
/// It's super important that there are no other outstanding
/// references to the cache at this point. Again, I remind you
/// not to try and outsmart the borrow checker. It doesn't know
/// how much trouble there is in here.
pub fn reload(&mut self) {
self.ptr_mutex.lock().expect("poisoned mutex").re_up()
}
/// Walk through all of the packages, in a random order.
///
/// If there are multiple architectures, multiple architectures will be returned.