From 9225cbaba31928132e6d14c46ea47127f7b7b48e Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 20 Sep 2022 23:06:22 +0800 Subject: [PATCH] resolve issue #206 by adding check for existing basis --- apps/vault/locales/i18n.json | 6 ++++++ apps/vault/src/actions.rs | 3 +++ services/pddb/src/api.rs | 1 + services/pddb/src/backend/basis.rs | 5 +++++ services/pddb/src/lib.rs | 1 + services/pddb/src/main.rs | 7 ++++++- 6 files changed, 22 insertions(+), 1 deletion(-) diff --git a/apps/vault/locales/i18n.json b/apps/vault/locales/i18n.json index 2a9c8a53c..79e86f01f 100644 --- a/apps/vault/locales/i18n.json +++ b/apps/vault/locales/i18n.json @@ -29,6 +29,12 @@ "zh": "选择要卸载的基础:", "en-tts": "Select the Bases to unmount:" }, + "vault.basis.already_exists": { + "en": "Basis already exists! No action taken.", + "ja": "ベースはすでに存在します! アクションは実行されません。", + "zh": "基础已经存在! 不采取行动。", + "en-tts": "Basis already exists! No action taken." + }, "vault.edit_dialog": { "en": "Make your changes and press enter.\nUse the d-pad to navigate entries.", "ja": "変更を加えてEnterキーを押します。 D-PADを使用してエントリをナビゲートします。", diff --git a/apps/vault/src/actions.rs b/apps/vault/src/actions.rs index 9696b5692..c99aa8fa3 100644 --- a/apps/vault/src/actions.rs +++ b/apps/vault/src/actions.rs @@ -938,6 +938,9 @@ impl ActionManager { match self.pddb.borrow().unlock_basis(&name, Some(BasisRetentionPolicy::Persist)) { Ok(_) => log::debug!("Basis {} unlocked", name), Err(e) => match e.kind() { + ErrorKind::AlreadyExists => { + self.modals.show_notification(t!("vault.basis.already_exists", xous::LANG), None).ok(); + } ErrorKind::PermissionDenied => { self.report_err(t!("vault.error.basis_unlock_error", xous::LANG), None::) }, diff --git a/services/pddb/src/api.rs b/services/pddb/src/api.rs index a3bf10cd0..cf83969bc 100644 --- a/services/pddb/src/api.rs +++ b/services/pddb/src/api.rs @@ -246,6 +246,7 @@ pub enum PddbRequestCode { InternalError = 8, AccessDenied = 9, Uninit = 10, + DuplicateEntry = 11, } #[derive(Copy, Clone, Eq, PartialEq, Debug)] #[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] diff --git a/services/pddb/src/backend/basis.rs b/services/pddb/src/backend/basis.rs index ca14c9c5a..eb7ce009c 100644 --- a/services/pddb/src/backend/basis.rs +++ b/services/pddb/src/backend/basis.rs @@ -851,6 +851,11 @@ impl BasisCache { }; let basis_key = hw.basis_derive_key(name, password); + + if let Some(_basis_map) = hw.pt_scan_key(&basis_key.pt, &basis_key.data, name) { + return Err(Error::new(ErrorKind::AlreadyExists, "Basis already exists")); + } + let mut basis_v2p_map = HashMap::::new(); let basis_root = BasisRoot { magic: PDDB_MAGIC, diff --git a/services/pddb/src/lib.rs b/services/pddb/src/lib.rs index e18c269f3..8bb214888 100644 --- a/services/pddb/src/lib.rs +++ b/services/pddb/src/lib.rs @@ -221,6 +221,7 @@ impl Pddb { PddbRequestCode::NoErr => Ok(()), PddbRequestCode::NoFreeSpace => Err(Error::new(ErrorKind::OutOfMemory, "No free space to create basis")), PddbRequestCode::InternalError => Err(Error::new(ErrorKind::Other, "Internal error creating basis")), + PddbRequestCode::DuplicateEntry => Err(Error::new(ErrorKind::AlreadyExists, "Basis already exists")), _ => { log::error!("Invalid return code"); panic!("Invalid return code"); diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index 0a0c1d48b..42a187165 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -769,7 +769,12 @@ fn wrapped_main() -> ! { log::info!("{}PDDB.CREATEOK,{},{}", xous::BOOKEND_START, mgmt.name.as_str().unwrap(), xous::BOOKEND_END); mgmt.code = PddbRequestCode::NoErr }, - _ => mgmt.code = PddbRequestCode::InternalError, + Err(e) => match e.kind() { + ErrorKind::AlreadyExists => { + mgmt.code = PddbRequestCode::DuplicateEntry; + } + _ => mgmt.code = PddbRequestCode::InternalError, + } } } else { mgmt.code = PddbRequestCode::InternalError;