diff --git a/Cargo.toml b/Cargo.toml index 8fd85c9..be5e35b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/apt-pkg-c/lib.cpp b/apt-pkg-c/lib.cpp index dedcf4e..a3cb1f9 100644 --- a/apt-pkg-c/lib.cpp +++ b/apt-pkg-c/lib.cpp @@ -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; diff --git a/src/lib.rs b/src/lib.rs index adcf00c..2da3cec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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(); + } } diff --git a/src/raw.rs b/src/raw.rs index e123573..31e094b 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -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 = { diff --git a/src/sane.rs b/src/sane.rs index 385c7a9..f26583d 100644 --- a/src/sane.rs +++ b/src/sane.rs @@ -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.