mirror of
https://github.com/AmbiML/sparrow-kata-full.git
synced 2025-09-03 06:24:12 +00:00
StorageManager: more rust cleanups
- add From traits for mapping return status - no more need for StorageError::Success w/ switch to Result's - narrow unsafe blocks where possible Change-Id: I92e0666e2651eb3647ac4e351d14bf55bc76bbb0 GitOrigin-RevId: 24416448d1c326632f556e224fcca7ac38397dc6
This commit is contained in:
@@ -10,7 +10,6 @@ extern crate kata_panic;
|
|||||||
use kata_allocator;
|
use kata_allocator;
|
||||||
use kata_logger::KataLogger;
|
use kata_logger::KataLogger;
|
||||||
use kata_storage_interface::KeyValueData;
|
use kata_storage_interface::KeyValueData;
|
||||||
use kata_storage_interface::StorageError;
|
|
||||||
use kata_storage_interface::StorageManagerError;
|
use kata_storage_interface::StorageManagerError;
|
||||||
use kata_storage_interface::StorageManagerInterface;
|
use kata_storage_interface::StorageManagerInterface;
|
||||||
use kata_storage_manager::KATA_STORAGE;
|
use kata_storage_manager::KATA_STORAGE;
|
||||||
@@ -35,17 +34,6 @@ pub extern "C" fn pre_init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn map_storage_error(se: StorageError, def: StorageManagerError) -> StorageManagerError {
|
|
||||||
match se {
|
|
||||||
StorageError::Success => StorageManagerError::SmeSuccess,
|
|
||||||
StorageError::BundleNotFound => StorageManagerError::SmeBundleNotFound,
|
|
||||||
StorageError::KeyNotFound => StorageManagerError::SmeKeyNotFound,
|
|
||||||
StorageError::KeyInvalid => StorageManagerError::SmeKeyInvalid,
|
|
||||||
StorageError::ValueInvalid => StorageManagerError::SmeValueInvalid,
|
|
||||||
_ => def,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// StorageInterface glue stubs.
|
// StorageInterface glue stubs.
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn storage_read(
|
pub extern "C" fn storage_read(
|
||||||
@@ -57,18 +45,15 @@ pub extern "C" fn storage_read(
|
|||||||
Ok(key) => {
|
Ok(key) => {
|
||||||
// TODO(sleffler): de-badge reply cap to get bundle_id
|
// TODO(sleffler): de-badge reply cap to get bundle_id
|
||||||
match KATA_STORAGE.read("fubar", key) {
|
match KATA_STORAGE.read("fubar", key) {
|
||||||
// NB: no serialization, returns raw data
|
|
||||||
Ok(value) => {
|
Ok(value) => {
|
||||||
|
// NB: no serialization, returns raw data
|
||||||
(*c_raw_value).copy_from_slice(&value);
|
(*c_raw_value).copy_from_slice(&value);
|
||||||
StorageManagerError::SmeSuccess
|
StorageManagerError::SmeSuccess
|
||||||
}
|
}
|
||||||
Err(e) => map_storage_error(e, StorageManagerError::SmeReadFailed),
|
Err(e) => StorageManagerError::from(e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(_) => StorageManagerError::SmeKeyInvalid,
|
||||||
trace!("read: keyinvalid {:?}", e);
|
|
||||||
StorageManagerError::SmeKeyInvalid
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -79,42 +64,29 @@ pub extern "C" fn storage_write(
|
|||||||
c_raw_value_len: usize,
|
c_raw_value_len: usize,
|
||||||
c_raw_value: *const u8,
|
c_raw_value: *const u8,
|
||||||
) -> StorageManagerError {
|
) -> StorageManagerError {
|
||||||
unsafe {
|
match unsafe { CStr::from_ptr(c_key).to_str() } {
|
||||||
match CStr::from_ptr(c_key).to_str() {
|
Ok(key) => {
|
||||||
Ok(key) => {
|
// TODO(sleffler): de-badge reply cap to get bundle_id
|
||||||
// TODO(sleffler): de-badge reply cap to get bundle_id
|
unsafe {
|
||||||
match KATA_STORAGE.write(
|
KATA_STORAGE.write(
|
||||||
"fubar",
|
"fubar",
|
||||||
key,
|
key,
|
||||||
slice::from_raw_parts(c_raw_value, c_raw_value_len),
|
slice::from_raw_parts(c_raw_value, c_raw_value_len),
|
||||||
) {
|
)
|
||||||
Ok(_) => StorageManagerError::SmeSuccess,
|
|
||||||
Err(e) => map_storage_error(e, StorageManagerError::SmeWriteFailed),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
trace!("write: keyinvalid {:?}", e);
|
|
||||||
StorageManagerError::SmeKeyInvalid
|
|
||||||
}
|
}
|
||||||
|
.into()
|
||||||
}
|
}
|
||||||
|
Err(_) => StorageManagerError::SmeKeyInvalid,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn storage_delete(c_key: *const cstr_core::c_char) -> StorageManagerError {
|
pub extern "C" fn storage_delete(c_key: *const cstr_core::c_char) -> StorageManagerError {
|
||||||
unsafe {
|
match unsafe { CStr::from_ptr(c_key).to_str() } {
|
||||||
match CStr::from_ptr(c_key).to_str() {
|
Ok(key) => {
|
||||||
Ok(key) => {
|
// TODO(sleffler): de-badge reply cap to get bundle_id
|
||||||
// TODO(sleffler): de-badge reply cap to get bundle_id
|
unsafe { KATA_STORAGE.delete("fubar", key) }.into()
|
||||||
match KATA_STORAGE.delete("fubar", key) {
|
|
||||||
Ok(_) => StorageManagerError::SmeSuccess,
|
|
||||||
Err(e) => map_storage_error(e, StorageManagerError::SmeDeleteFailed),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
trace!("delete: keyinvalid {:?}", e);
|
|
||||||
StorageManagerError::SmeKeyInvalid
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Err(_) => StorageManagerError::SmeKeyInvalid,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -16,8 +16,7 @@ pub type KeyValueData = [u8; KEY_VALUE_DATA_SIZE];
|
|||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub enum StorageError {
|
pub enum StorageError {
|
||||||
Success = 0,
|
BundleNotFound = 0,
|
||||||
BundleNotFound,
|
|
||||||
KeyNotFound,
|
KeyNotFound,
|
||||||
KeyInvalid,
|
KeyInvalid,
|
||||||
ValueInvalid,
|
ValueInvalid,
|
||||||
@@ -29,27 +28,28 @@ pub enum StorageError {
|
|||||||
DeleteFailed,
|
DeleteFailed,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<postcard::Error> for StorageError {
|
|
||||||
fn from(_err: postcard::Error) -> StorageError {
|
|
||||||
StorageError::SerializeFailed
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl From<SecurityRequestError> for StorageError {
|
impl From<SecurityRequestError> for StorageError {
|
||||||
fn from(err: SecurityRequestError) -> StorageError {
|
fn from(err: SecurityRequestError) -> StorageError {
|
||||||
match err {
|
match err {
|
||||||
SecurityRequestError::SreSuccess => StorageError::Success,
|
|
||||||
SecurityRequestError::SreBundleNotFound => StorageError::BundleNotFound,
|
SecurityRequestError::SreBundleNotFound => StorageError::BundleNotFound,
|
||||||
SecurityRequestError::SreKeyNotFound => StorageError::KeyNotFound,
|
SecurityRequestError::SreKeyNotFound => StorageError::KeyNotFound,
|
||||||
SecurityRequestError::SreValueInvalid => StorageError::ValueInvalid,
|
SecurityRequestError::SreValueInvalid => StorageError::ValueInvalid,
|
||||||
SecurityRequestError::SreKeyInvalid => StorageError::KeyInvalid,
|
SecurityRequestError::SreKeyInvalid => StorageError::KeyInvalid,
|
||||||
|
SecurityRequestError::SreSerializeFailed => StorageError::SerializeFailed,
|
||||||
SecurityRequestError::SreReadFailed => StorageError::ReadFailed,
|
SecurityRequestError::SreReadFailed => StorageError::ReadFailed,
|
||||||
SecurityRequestError::SreWriteFailed => StorageError::WriteFailed,
|
SecurityRequestError::SreWriteFailed => StorageError::WriteFailed,
|
||||||
SecurityRequestError::SreDeleteFailed => StorageError::DeleteFailed,
|
SecurityRequestError::SreDeleteFailed => StorageError::DeleteFailed,
|
||||||
_ => StorageError::UnknownSecurityError, // NB: cannot happen
|
_ => StorageError::UnknownSecurityError, // NB: cannot happen
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<postcard::Error> for StorageError {
|
||||||
|
fn from(_err: postcard::Error) -> StorageError {
|
||||||
|
StorageError::SerializeFailed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub trait StorageManagerInterface {
|
pub trait StorageManagerInterface {
|
||||||
fn read(&self, bundle_id: &str, key: &str) -> Result<KeyValueData, StorageError>;
|
fn read(&self, bundle_id: &str, key: &str) -> Result<KeyValueData, StorageError>;
|
||||||
fn write(&self, bundle_id: &str, key: &str, value: &[u8]) -> Result<(), StorageError>;
|
fn write(&self, bundle_id: &str, key: &str, value: &[u8]) -> Result<(), StorageError>;
|
||||||
@@ -73,6 +73,31 @@ pub enum StorageManagerError {
|
|||||||
SmeReadFailed,
|
SmeReadFailed,
|
||||||
SmeWriteFailed,
|
SmeWriteFailed,
|
||||||
SmeDeleteFailed,
|
SmeDeleteFailed,
|
||||||
|
SmeUnknownError,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<StorageError> for StorageManagerError {
|
||||||
|
fn from(err: StorageError) -> StorageManagerError {
|
||||||
|
match err {
|
||||||
|
StorageError::BundleNotFound => StorageManagerError::SmeBundleNotFound,
|
||||||
|
StorageError::KeyNotFound => StorageManagerError::SmeKeyNotFound,
|
||||||
|
StorageError::KeyInvalid => StorageManagerError::SmeKeyInvalid,
|
||||||
|
StorageError::ValueInvalid => StorageManagerError::SmeValueInvalid,
|
||||||
|
StorageError::ReadFailed => StorageManagerError::SmeReadFailed,
|
||||||
|
StorageError::WriteFailed => StorageManagerError::SmeWriteFailed,
|
||||||
|
StorageError::DeleteFailed => StorageManagerError::SmeDeleteFailed,
|
||||||
|
_ => StorageManagerError::SmeUnknownError,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Result<(), StorageError>> for StorageManagerError {
|
||||||
|
fn from(result: Result<(), StorageError>) -> StorageManagerError {
|
||||||
|
result.map_or_else(
|
||||||
|
|e| StorageManagerError::from(e),
|
||||||
|
|_v| StorageManagerError::SmeSuccess,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<cstr_core::NulError> for StorageManagerError {
|
impl From<cstr_core::NulError> for StorageManagerError {
|
||||||
|
Reference in New Issue
Block a user