mirror of
https://github.com/aljazceru/kata-containers.git
synced 2025-12-18 23:04:20 +01:00
agent: simplify the way to manage storage object
Simplify the way to manage storage objects, and introduce StorageStateCommon structures for coming extensions. Signed-off-by: Jiang Liu <gerry@linux.alibaba.com>
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
|
||||
use anyhow::{anyhow, Context, Error, Result};
|
||||
use std::convert::TryFrom;
|
||||
use std::fmt::Formatter;
|
||||
use std::{collections::HashMap, fs, path::PathBuf};
|
||||
|
||||
/// Prefix to mark a volume as Kata special.
|
||||
@@ -427,6 +428,43 @@ impl TryFrom<&NydusExtraOptions> for KataVirtualVolume {
|
||||
}
|
||||
}
|
||||
|
||||
/// An implementation of generic storage device.
|
||||
pub struct StorageDeviceGeneric {
|
||||
refcount: u32,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for StorageDeviceGeneric {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("StorageState")
|
||||
.field("refcount", &self.refcount)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl StorageDeviceGeneric {
|
||||
/// Create a new instance of `StorageStateCommon`.
|
||||
pub fn new() -> Self {
|
||||
StorageDeviceGeneric { refcount: 0 }
|
||||
}
|
||||
|
||||
/// Get reference count.
|
||||
pub fn ref_count(&self) -> u32 {
|
||||
self.refcount
|
||||
}
|
||||
|
||||
/// Decrease reference count and return true if it reaches zero.
|
||||
pub fn inc_ref_count(&mut self) {
|
||||
self.refcount += 1;
|
||||
}
|
||||
|
||||
/// Decrease reference count and return true if it reaches zero.
|
||||
pub fn dec_and_test_ref_count(&mut self) -> bool {
|
||||
assert!(self.refcount > 0);
|
||||
self.refcount -= 1;
|
||||
self.refcount == 0
|
||||
}
|
||||
}
|
||||
|
||||
/// Join user provided volume path with kata direct-volume root path.
|
||||
///
|
||||
/// The `volume_path` is base64-encoded and then safely joined to the `prefix`
|
||||
@@ -659,4 +697,18 @@ mod tests {
|
||||
);
|
||||
assert_eq!(volume.fs_type.as_str(), "rafsv6")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_storage_state_common() {
|
||||
let mut state = StorageDeviceGeneric::new();
|
||||
assert_eq!(state.ref_count(), 0);
|
||||
state.inc_ref_count();
|
||||
assert_eq!(state.ref_count(), 1);
|
||||
state.inc_ref_count();
|
||||
assert_eq!(state.ref_count(), 2);
|
||||
assert!(!state.dec_and_test_ref_count());
|
||||
assert_eq!(state.ref_count(), 1);
|
||||
assert!(state.dec_and_test_ref_count());
|
||||
assert_eq!(state.ref_count(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user