From 35f3cc43cd4be13d1ee84d91c59b3dc3588dc7bd Mon Sep 17 00:00:00 2001 From: Cai Xincheng Date: Sat, 12 Oct 2024 16:41:26 +0800 Subject: [PATCH] catch exist error in get_existing_key_alias instead of return it to the caller Signed-off-by: Cai Xincheng Change-Id: I2221ae8ae7482b9529c1b2584045d9eb972445c7 --- services/crypto_manager/src/secret_key.rs | 45 +++++++++++-------- .../db_operator/src/database_file_upgrade.rs | 4 +- services/db_operator/src/table.rs | 4 +- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/services/crypto_manager/src/secret_key.rs b/services/crypto_manager/src/secret_key.rs index a79dcc3..47b5bed 100644 --- a/services/crypto_manager/src/secret_key.rs +++ b/services/crypto_manager/src/secret_key.rs @@ -86,48 +86,57 @@ fn get_existing_key_alias( auth_type: AuthType, access_type: Accessibility, require_password_set: bool, -) -> Result { +) -> KeyAliasVersion { let new_alias = calculate_key_alias(calling_info, auth_type, access_type, require_password_set, true); let prefixed_new_alias = [ALIAS_PREFIX.to_vec(), new_alias.clone()].concat(); - let key = SecretKey { + let ret = SecretKey { user_id: calling_info.user_id(), auth_type, access_type, require_password_set, alias: prefixed_new_alias.clone(), - }; - if key.exists()? { - logi!("[INFO][{access_type}]-typed secret key with version 3 alias exists."); - return Ok(KeyAliasVersion::V3); + } + .exists(); + if let Ok(true) = ret { + logi!("[INFO][{access_type}]-typed secret key with v3 alias exists."); + return KeyAliasVersion::V3; + } else if let Err(e) = ret { + loge!("[Fatal]Can not determine whether [{access_type}]-typed secret key with v3 alias exists. err is {e}."); } - let key = SecretKey { + let ret = SecretKey { user_id: calling_info.user_id(), auth_type, access_type, require_password_set, alias: new_alias.clone(), - }; - if key.exists()? { - logi!("[INFO][{access_type}]-typed secret key with version 2 alias exists."); - return Ok(KeyAliasVersion::V2(new_alias)); + } + .exists(); + if let Ok(true) = ret { + logi!("[INFO][{access_type}]-typed secret key with v2 alias exists."); + return KeyAliasVersion::V2(new_alias); + } else if let Err(e) = ret { + loge!("[Fatal]Can not determine whether [{access_type}]-typed secret key with v2 alias exists. err is {e}."); } let old_alias = calculate_key_alias(calling_info, auth_type, access_type, require_password_set, false); - let key = SecretKey { + let ret = SecretKey { user_id: calling_info.user_id(), auth_type, access_type, require_password_set, alias: old_alias.clone(), - }; - if key.exists()? { - logi!("[INFO][{access_type}]-typed secret key with version 1 alias exists."); - return Ok(KeyAliasVersion::V1(old_alias)); + } + .exists(); + if let Ok(true) = ret { + logi!("[INFO][{access_type}]-typed secret key with v1 alias exists."); + return KeyAliasVersion::V1(old_alias); + } else if let Err(e) = ret { + loge!("[Fatal]Can not determine whether [{access_type}]-typed secret key with v1 alias exists. err is {e}."); } loge!("[INFO][{access_type}]-typed secret key does not exist."); - Ok(KeyAliasVersion::None) + KeyAliasVersion::None } fn huks_rename_key_alias( @@ -159,7 +168,7 @@ pub fn rename_key_alias( access_type: Accessibility, require_password_set: bool, ) -> Result { - match get_existing_key_alias(calling_info, auth_type, access_type, require_password_set)? { + match get_existing_key_alias(calling_info, auth_type, access_type, require_password_set) { KeyAliasVersion::V3 => { logi!("[INFO]Alias of [{access_type}]-typed secret key has already been renamed successfully."); Ok(true) diff --git a/services/db_operator/src/database_file_upgrade.rs b/services/db_operator/src/database_file_upgrade.rs index ca44a5e..97353dd 100644 --- a/services/db_operator/src/database_file_upgrade.rs +++ b/services/db_operator/src/database_file_upgrade.rs @@ -25,8 +25,8 @@ use asset_log::logi; use crate::{ database::{ - fmt_backup_path, fmt_de_db_path_with_name, get_db, get_split_db_lock_by_user_id, Database, DE_ROOT_PATH, - CE_ROOT_PATH, OLD_DB_NAME, + fmt_backup_path, fmt_de_db_path_with_name, get_db, get_split_db_lock_by_user_id, Database, CE_ROOT_PATH, + DE_ROOT_PATH, OLD_DB_NAME, }, types::{column, DbMap, QueryOptions, DB_UPGRADE_VERSION, DB_UPGRADE_VERSION_V3}, }; diff --git a/services/db_operator/src/table.rs b/services/db_operator/src/table.rs index c8d002b..fe96676 100644 --- a/services/db_operator/src/table.rs +++ b/services/db_operator/src/table.rs @@ -26,7 +26,9 @@ use crate::{ database::Database, statement::Statement, transaction::Transaction, - types::{ColumnInfo, DbMap, QueryOptions, UpgradeColumnInfo, DB_UPGRADE_VERSION, DB_UPGRADE_VERSION_V3, SQLITE_ROW}, + types::{ + ColumnInfo, DbMap, QueryOptions, UpgradeColumnInfo, DB_UPGRADE_VERSION, DB_UPGRADE_VERSION_V3, SQLITE_ROW, + }, }; extern "C" {