types: make StorageDevice::cleanup() return possible error code

Make StorageDevice::cleanup() return possible error code.

Fixes: #7818

Signed-off-by: Jiang Liu <gerry@linux.alibaba.com>
This commit is contained in:
Jiang Liu 2023-08-31 22:33:30 +08:00
parent 3a3d77b3b5
commit 0c63453e28
3 changed files with 25 additions and 24 deletions

View File

@ -72,7 +72,7 @@ impl StorageState {
}
}
pub fn path(&self) -> &str {
pub fn path(&self) -> Option<&str> {
self.device.path()
}

View File

@ -5,7 +5,6 @@
//
use std::collections::HashMap;
use std::fmt::Formatter;
use std::fs;
use std::os::unix::fs::{MetadataExt, PermissionsExt};
use std::path::Path;
@ -55,31 +54,26 @@ pub struct StorageContext<'a> {
}
/// An implementation of generic storage device.
#[derive(Default, Debug)]
pub struct StorageDeviceGeneric {
path: String,
}
impl std::fmt::Debug for StorageDeviceGeneric {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("StorageDeviceGeneric")
.field("path", &self.path)
.finish()
}
path: Option<String>,
}
impl StorageDeviceGeneric {
/// Create a new instance of `StorageStateCommon`.
pub fn new(path: String) -> Self {
StorageDeviceGeneric { path }
StorageDeviceGeneric { path: Some(path) }
}
}
impl StorageDevice for StorageDeviceGeneric {
fn path(&self) -> &str {
&self.path
fn path(&self) -> Option<&str> {
self.path.as_ref().map(|v| v.as_str())
}
fn cleanup(&self) {}
fn cleanup(&self) -> Result<()> {
Ok(())
}
}
/// Trait object to handle storage device.
@ -130,9 +124,10 @@ pub async fn add_storages(
let path = storage.mount_point.clone();
let state = sandbox.lock().await.add_sandbox_storage(&path).await;
if state.ref_count().await > 1 {
let path = state.path();
if !path.is_empty() {
mount_list.push(path.to_string());
if let Some(path) = state.path() {
if !path.is_empty() {
mount_list.push(path.to_string());
}
}
// The device already exists.
continue;
@ -155,9 +150,10 @@ pub async fn add_storages(
.update_sandbox_storage(&path, device.clone())
{
Ok(d) => {
let path = device.path().to_string();
if !path.is_empty() {
mount_list.push(path.clone());
if let Some(path) = device.path() {
if !path.is_empty() {
mount_list.push(path.to_string());
}
}
drop(d);
}
@ -167,7 +163,12 @@ pub async fn add_storages(
{
warn!(logger, "failed to remove dummy sandbox storage {:?}", e);
}
device.cleanup();
if let Err(e) = device.cleanup() {
error!(
logger,
"failed to clean state for storage device {}, {}", path, e
);
}
return Err(anyhow!("failed to update device for storage"));
}
}

View File

@ -431,10 +431,10 @@ impl TryFrom<&NydusExtraOptions> for KataVirtualVolume {
/// Trait object for storage device.
pub trait StorageDevice: Send + Sync {
/// Path
fn path(&self) -> &str;
fn path(&self) -> Option<&str>;
/// Clean up resources related to the storage device.
fn cleanup(&self);
fn cleanup(&self) -> Result<()>;
}
/// Manager to manage registered storage device handlers.