add key attribute query, clean up some warning

This commit is contained in:
bunnie 2022-01-07 16:18:44 +08:00
parent fa7a9db348
commit 7d660a3592
5 changed files with 93 additions and 32 deletions

View file

@ -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::</*DICT_NAME_LEN*/ 111>, // pending https://github.com/rust-lang/rust/issues/90195
pub basis: xous_ipc::String::</* BASIS_NAME_LEN */ 64>, // 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::<DICT_NAME_LEN>::new(),
basis: xous_ipc::String::<BASIS_NAME_LEN>::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::<DICT_NAME_LEN>::from_str(&attr.dict),
basis: xous_ipc::String::<BASIS_NAME_LEN>::from_str(&attr.basis),
flags: attr.flags.0,
index: attr.index.get(),
token,
code: PddbRequestCode::NoErr,
}
}
}
#[cfg(test)]
mod tests {
use super::*;

View file

@ -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<String>,
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<KeyAttributes> {
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::<PddbKeyAttrIpc, _>().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")),
}
}
}

View file

@ -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::<PddbBuf>()),

View file

@ -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<xous_ipc::String::<{crate::api::PASSWORD_LEN}>>,
}
//#[derive(Eq)]
struct TokenRecord {
pub dict: String,
pub key: String,
pub basis: Option<String>,
pub conn: xous::CID, // callback connection
}
/*
impl Hash for TokenRecord {
fn hash<H: Hasher>(&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::<PddbKeyAttrIpc, _>().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
}

View file

@ -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<Option<String::<1024>>, xous::Error> {
fn process(&mut self, args: String::<1024>, _env: &mut CommonEnv) -> Result<Option<String::<1024>>, 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();