kata-types: Replace StorageHandlerManager with type alias

Removed the `StorageHandlerManager` struct and its associated implementations and
introduced a type alias `StorageHandlerManager` for `HandlerManager` to simplify the code.
The new type alias maintains the same functionality while reducing redundancy.

Signed-off-by: ChengyuZhu6 <chengyu.zhu@intel.com>
This commit is contained in:
ChengyuZhu6 2024-08-27 05:54:41 +08:00
parent 281f0d7f29
commit 0b3ad2f830
2 changed files with 23 additions and 60 deletions

View File

@ -12,13 +12,19 @@ pub struct HandlerManager<H> {
handlers: HashMap<String, H>,
}
impl<H> Default for HandlerManager<H> {
impl<H> Default for HandlerManager<H>
where
H: Clone,
{
fn default() -> Self {
Self::new()
}
}
impl<H> HandlerManager<H> {
impl<H> HandlerManager<H>
where
H: Clone,
{
/// Create a new instance of `HandlerManager`.
pub fn new() -> Self {
Self {
@ -27,14 +33,18 @@ impl<H> HandlerManager<H> {
}
/// Register a handler.
pub fn add_handler(&mut self, id: &str, handler: H) -> Result<()> {
match self.handlers.entry(id.to_string()) {
Entry::Occupied(_) => Err(anyhow!("handler for {} already exists", id)),
Entry::Vacant(entry) => {
entry.insert(handler);
Ok(())
pub fn add_handler(&mut self, ids: &[&str], handler: H) -> Result<()> {
for &id in ids {
match self.handlers.entry(id.to_string()) {
Entry::Occupied(_) => {
return Err(anyhow!("handler for {} already exists", id));
}
Entry::Vacant(entry) => {
entry.insert(handler.clone());
}
}
}
Ok(())
}
/// Get handler with specified `id`.

View File

@ -5,10 +5,11 @@
//
use anyhow::{anyhow, Context, Error, Result};
use std::collections::hash_map::Entry;
use std::convert::TryFrom;
use std::{collections::HashMap, fs, path::PathBuf};
use crate::handler::HandlerManager;
/// Prefix to mark a volume as Kata special.
pub const KATA_VOLUME_TYPE_PREFIX: &str = "kata:";
@ -59,6 +60,9 @@ pub const KATA_VIRTUAL_VOLUME_LAYER_NYDUS_FS: &str = "layer_nydus_fs";
/// Download and extra container image inside guest vm.
pub const KATA_VIRTUAL_VOLUME_IMAGE_GUEST_PULL: &str = "image_guest_pull";
/// Manager to manage registered storage device handlers.
pub type StorageHandlerManager<H> = HandlerManager<H>;
/// Information about a mount.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct Mount {
@ -441,57 +445,6 @@ pub trait StorageDevice: Send + Sync {
fn cleanup(&self) -> Result<()>;
}
/// Manager to manage registered storage device handlers.
pub struct StorageHandlerManager<H> {
handlers: HashMap<String, H>,
}
impl<H> Default for StorageHandlerManager<H>
where
H: Clone,
{
fn default() -> Self {
Self::new()
}
}
impl<H> StorageHandlerManager<H>
where
H: Clone,
{
/// Create a new instance of `StorageHandlerManager`.
pub fn new() -> Self {
Self {
handlers: HashMap::new(),
}
}
/// Register a storage device handler.
pub fn add_handler(&mut self, ids: &[&str], handler: H) -> Result<()> {
for &id in ids {
match self.handlers.entry(id.to_string()) {
Entry::Occupied(_) => {
return Err(anyhow!("storage handler for {} already exists", id));
}
Entry::Vacant(entry) => {
entry.insert(handler.clone());
}
}
}
Ok(())
}
/// Get storage handler with specified `id`.
pub fn handler(&self, id: &str) -> Option<&H> {
self.handlers.get(id)
}
/// Get names of registered handlers.
pub fn get_handlers(&self) -> Vec<String> {
self.handlers.keys().map(|v| v.to_string()).collect()
}
}
/// Join user provided volume path with kata direct-volume root path.
///
/// The `volume_path` is base64-url-encoded and then safely joined to the `prefix`