container_device_interface/
default_cache.rs1use anyhow::Result;
2use std::collections::HashMap;
3use std::error::Error;
4use std::sync::{Arc, Mutex};
5
6use oci_spec::runtime::Spec;
7use once_cell::sync::OnceCell;
8
9use crate::cache::{new_cache, with_auto_refresh, Cache, CdiOption};
10
11fn get_or_create_default_cache(_options: &[CdiOption]) -> Arc<Mutex<Cache>> {
12 let mut cache: OnceCell<Arc<Mutex<Cache>>> = OnceCell::new();
13 cache.get_or_init(|| {
14 let options: Vec<CdiOption> = vec![with_auto_refresh(true)];
15 new_cache(options)
16 });
17 cache.take().unwrap()
18}
19
20pub fn get_default_cache() -> Arc<Mutex<Cache>> {
21 get_or_create_default_cache(vec![].as_ref())
22}
23
24pub fn configure(options: Vec<CdiOption>) -> Result<()> {
25 let cache = get_or_create_default_cache(&options);
26 let mut cache = cache.lock().unwrap();
27 if options.is_empty() {
28 return Ok(());
29 }
30 cache.configure(options);
31 Ok(())
32}
33
34pub fn refresh() -> Result<(), Box<dyn Error>> {
35 let cache = get_default_cache();
36 let mut cache = cache.lock().unwrap();
37 cache.refresh()
38}
39
40pub fn inject_devices(
41 oci_spec: &mut Spec,
42 devices: Vec<String>,
43) -> Result<Vec<String>, Box<dyn Error + Send + Sync + 'static>> {
44 let cache = get_default_cache();
45 let mut cache = cache.lock().unwrap();
46 cache.inject_devices(Some(oci_spec), devices)
47}
48
49pub fn list_devices() -> Vec<String> {
50 let cache = get_default_cache();
51 let mut cache = cache.lock().unwrap();
52 cache.list_devices()
53}
54
55pub fn get_errors() -> HashMap<String, Vec<anyhow::Error>> {
56 let cache = get_default_cache();
57 let cache = cache.lock().unwrap();
58 cache.get_errors()
59}