fn do_reload_slots()

in src/vtok_p11/src/backend/device.rs [143:186]


    fn do_reload_slots(&mut self) -> bool {
        let config = match Config::load_ro() {
            Ok(config) => config,
            Err(_) => {
                error!("Failed to reload slots: Unable to load configuration.");
                return false;
            }
        };

        for (slot_id, new_slot_config) in config.slots().iter().enumerate() {
            match (new_slot_config, self.slot(slot_id as u64)) {
                // The token is not in use anymore.
                (None, Some(_token_config)) => {
                    self.close_all_slot_sessions(slot_id as u64);
                    self.slots[slot_id] = Slot::new(slot_id as u64);
                }
                // A new token is available in the database.
                (Some(new_token_config), None) => {
                    if let Ok(new_token) =
                        Token::from_config(slot_id as pkcs11::CK_SLOT_ID, &new_token_config)
                    {
                        Slot::new_with_token(slot_id as pkcs11::CK_SLOT_ID, new_token);
                    } else {
                        error!("Could not create a new slot at index {}!", slot_id);
                    }
                }
                // The token exists both in the slot data and the database.
                // Check if a refresh is needed.
                (Some(new_token_config), Some(_token_config)) => {
                    if let Ok(token) =
                        self.token_unchecked_mut(slot_id as pkcs11::CK_SLOT_ID)
                    {
                        //TODO: Implement an update/try_update method as well.
                        token.refresh(new_token_config.expiry_ts);
                    }
                }
                (_, _) => {
                    // The slot is up-to-date.
                }
            }
        }

        true
    }