From 7d660a35920dd5d1706c1a120d0328debe8fb444 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 7 Jan 2022 16:18:44 +0800 Subject: [PATCH] add key attribute query, clean up some warning --- services/pddb/src/api.rs | 56 +++++++++++++++++++++++++ services/pddb/src/frontend/pddbkey.rs | 19 +++++---- services/pddb/src/lib.rs | 7 ++-- services/pddb/src/main.rs | 34 ++++++++------- services/shellchat/src/cmds/pddb_cmd.rs | 9 ++-- 5 files changed, 93 insertions(+), 32 deletions(-) diff --git a/services/pddb/src/api.rs b/services/pddb/src/api.rs index 282094a92..724de0bcb 100644 --- a/services/pddb/src/api.rs +++ b/services/pddb/src/api.rs @@ -76,6 +76,7 @@ pub(crate) enum Opcode { DeleteBasis, DeleteKey, DeleteDict, + KeyAttributes, /// primary method for accessing the database KeyRequest, @@ -200,6 +201,61 @@ pub struct KeyAttributes { pub index: NonZeroU32, } +#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] +/// serializeable version of the attributes structure +pub struct PddbKeyAttrIpc { + pub len: u64, + pub reserved: u64, + pub age: u64, + pub dict: xous_ipc::String::, // pending https://github.com/rust-lang/rust/issues/90195 + pub basis: xous_ipc::String::, // pending https://github.com/rust-lang/rust/issues/90195 + pub flags: u32, + pub index: u32, + pub token: ApiToken, + pub code: PddbRequestCode, +} +impl PddbKeyAttrIpc { + pub fn new(token: ApiToken) -> PddbKeyAttrIpc { + PddbKeyAttrIpc { + len: 0, + reserved: 0, + age: 0, + dict: xous_ipc::String::::new(), + basis: xous_ipc::String::::new(), + flags: 0, + index: 0, + token, + code: PddbRequestCode::Uninit, + } + } + #[allow(dead_code)] + pub fn to_attributes(&self) -> KeyAttributes { + KeyAttributes { + len: self.len as usize, + reserved: self.reserved as usize, + age: self.age as usize, + dict: String::from(self.dict.as_str().unwrap()), + basis: String::from(self.basis.as_str().unwrap()), + flags: KeyFlags(self.flags), + index: NonZeroU32::new(self.index).unwrap(), + } + } + pub fn from_attributes(attr: KeyAttributes, token: ApiToken) -> PddbKeyAttrIpc { + PddbKeyAttrIpc { + len: attr.len as u64, + reserved: attr.reserved as u64, + age: attr.age as u64, + dict: xous_ipc::String::::from_str(&attr.dict), + basis: xous_ipc::String::::from_str(&attr.basis), + flags: attr.flags.0, + index: attr.index.get(), + token, + code: PddbRequestCode::NoErr, + } + } +} + + #[cfg(test)] mod tests { use super::*; diff --git a/services/pddb/src/frontend/pddbkey.rs b/services/pddb/src/frontend/pddbkey.rs index a770ea3fd..6ba98d433 100644 --- a/services/pddb/src/frontend/pddbkey.rs +++ b/services/pddb/src/frontend/pddbkey.rs @@ -4,16 +4,9 @@ use xous_ipc::Buffer; use num_traits::*; use std::io::{Result, Error, ErrorKind}; -use std::path::{Path, Component}; -use std::format; use std::io::{Read, Write}; -use std::string::String; - pub struct PddbKey<'a> { - pub(crate) dict: String, - pub(crate) key: String, - pub(crate) basis: Option, pub(crate) token: ApiToken, /// position in the key's data "stream" pub(crate) pos: u64, @@ -26,8 +19,16 @@ impl<'a> PddbKey<'a> { pub fn volatile_clear(&mut self) { self.buf.volatile_clear(); } - pub fn attributes(&self) -> KeyAttributes { - unimplemented!() + pub fn attributes(&self) -> Result { + let req = PddbKeyAttrIpc::new(self.token); + let mut buf = Buffer::into_buf(req).expect("Couldn't convert memory structure"); + buf.lend_mut(self.conn, Opcode::KeyAttributes.to_u32().unwrap()).expect("couldn't execute KeyAttributes opcode"); + let ret = buf.to_original::().expect("couldn't restore req structure"); + match ret.code { + PddbRequestCode::NoErr => Ok(ret.to_attributes()), + PddbRequestCode::NotFound => Err(Error::new(ErrorKind::NotFound, "Key not found")), + _ => Err(Error::new(ErrorKind::Other, "Internal error requesting key attributes")), + } } } diff --git a/services/pddb/src/lib.rs b/services/pddb/src/lib.rs index cd00b01ee..d65e924e1 100644 --- a/services/pddb/src/lib.rs +++ b/services/pddb/src/lib.rs @@ -269,9 +269,10 @@ impl Pddb { } REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); let pk = PddbKey { - dict: String::from(dict_name), - key: String::from(key_name), - basis: if basis_name.is_some() {Some(String::from(bname.as_str().unwrap()))} else {None}, + // i think these fields are redundant, let's save the storage and remove them for now... + //dict: String::from(dict_name), + //key: String::from(key_name), + //basis: if basis_name.is_some() {Some(String::from(bname.as_str().unwrap()))} else {None}, pos: 0, token, buf: Buffer::new(core::mem::size_of::()), diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index ad0d607c2..9c912eecd 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -384,7 +384,6 @@ use core::cell::RefCell; use std::rc::Rc; use std::thread; use std::collections::HashMap; -use std::hash::{Hash, Hasher}; use std::io::ErrorKind; use core::fmt::Write; @@ -396,25 +395,12 @@ pub(crate) struct BasisRequestPassword { plaintext_pw: Option>, } -//#[derive(Eq)] struct TokenRecord { pub dict: String, pub key: String, pub basis: Option, pub conn: xous::CID, // callback connection } -/* -impl Hash for TokenRecord { - fn hash(&self, state: &mut H) { - self.token.hash(state); - } -} -impl PartialEq for TokenRecord { - fn eq(&self, other: &Self) -> bool { - self.token == other.token - } -}*/ - #[xous::xous_main] fn xmain() -> ! { @@ -857,6 +843,26 @@ fn xmain() -> ! { } buffer.replace(req).unwrap(); } + Some(Opcode::KeyAttributes) => { + let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; + let mut req = buffer.to_original::().unwrap(); + if let Some(token_record) = token_dict.get(&req.token) { + let bname = if let Some(name) = &token_record.basis { + Some(name.as_str()) + } else { + None + }; + match basis_cache.key_attributes(&mut pddb_os, &token_record.dict, &token_record.key, bname) { + Ok(attr) => { + buffer.replace(PddbKeyAttrIpc::from_attributes(attr, req.token)).unwrap(); + } + _ => { + req.code = PddbRequestCode::NotFound; + buffer.replace(req).unwrap(); + } + } + } + } Some(Opcode::ReadKey) => { // placeholder } diff --git a/services/shellchat/src/cmds/pddb_cmd.rs b/services/shellchat/src/cmds/pddb_cmd.rs index 9d4d99b00..28821ef78 100644 --- a/services/shellchat/src/cmds/pddb_cmd.rs +++ b/services/shellchat/src/cmds/pddb_cmd.rs @@ -5,7 +5,7 @@ pub struct PddbCmd { pddb: pddb::Pddb, } impl PddbCmd { - pub fn new(xns: &xous_names::XousNames) -> PddbCmd { + pub fn new(_xns: &xous_names::XousNames) -> PddbCmd { PddbCmd { pddb: pddb::Pddb::new(), } @@ -15,15 +15,12 @@ impl PddbCmd { impl<'a> ShellCmdApi<'a> for PddbCmd { cmd_api!(pddb); // inserts boilerplate for command API - fn process(&mut self, args: String::<1024>, env: &mut CommonEnv) -> Result>, xous::Error> { + fn process(&mut self, args: String::<1024>, _env: &mut CommonEnv) -> Result>, xous::Error> { use core::fmt::Write; let mut ret = String::<1024>::new(); let helpstring = "pddb [basislist]"; let mut tokens = args.as_str().unwrap().split(' '); - let sid = xous::create_server().unwrap(); - let cid = xous::connect(sid).unwrap(); - if let Some(sub_cmd) = tokens.next() { match sub_cmd { "basislist" => { @@ -31,7 +28,7 @@ impl<'a> ShellCmdApi<'a> for PddbCmd { for basis in bases { write!(ret, "{}\n", basis).unwrap(); } - /* + /* // example of using .get with a callback self.pddb.get("foo", "bar", None, false, false, Some({ let cid = cid.clone();