mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-19 09:00:54 +00:00
add key attribute query, clean up some warning
This commit is contained in:
parent
fa7a9db348
commit
7d660a3592
5 changed files with 93 additions and 32 deletions
|
|
@ -76,6 +76,7 @@ pub(crate) enum Opcode {
|
||||||
DeleteBasis,
|
DeleteBasis,
|
||||||
DeleteKey,
|
DeleteKey,
|
||||||
DeleteDict,
|
DeleteDict,
|
||||||
|
KeyAttributes,
|
||||||
|
|
||||||
/// primary method for accessing the database
|
/// primary method for accessing the database
|
||||||
KeyRequest,
|
KeyRequest,
|
||||||
|
|
@ -200,6 +201,61 @@ pub struct KeyAttributes {
|
||||||
pub index: NonZeroU32,
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
||||||
|
|
@ -4,16 +4,9 @@ use xous_ipc::Buffer;
|
||||||
|
|
||||||
use num_traits::*;
|
use num_traits::*;
|
||||||
use std::io::{Result, Error, ErrorKind};
|
use std::io::{Result, Error, ErrorKind};
|
||||||
use std::path::{Path, Component};
|
|
||||||
use std::format;
|
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
|
|
||||||
use std::string::String;
|
|
||||||
|
|
||||||
pub struct PddbKey<'a> {
|
pub struct PddbKey<'a> {
|
||||||
pub(crate) dict: String,
|
|
||||||
pub(crate) key: String,
|
|
||||||
pub(crate) basis: Option<String>,
|
|
||||||
pub(crate) token: ApiToken,
|
pub(crate) token: ApiToken,
|
||||||
/// position in the key's data "stream"
|
/// position in the key's data "stream"
|
||||||
pub(crate) pos: u64,
|
pub(crate) pos: u64,
|
||||||
|
|
@ -26,8 +19,16 @@ impl<'a> PddbKey<'a> {
|
||||||
pub fn volatile_clear(&mut self) {
|
pub fn volatile_clear(&mut self) {
|
||||||
self.buf.volatile_clear();
|
self.buf.volatile_clear();
|
||||||
}
|
}
|
||||||
pub fn attributes(&self) -> KeyAttributes {
|
pub fn attributes(&self) -> Result<KeyAttributes> {
|
||||||
unimplemented!()
|
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")),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -269,9 +269,10 @@ impl Pddb {
|
||||||
}
|
}
|
||||||
REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
|
REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
|
||||||
let pk = PddbKey {
|
let pk = PddbKey {
|
||||||
dict: String::from(dict_name),
|
// i think these fields are redundant, let's save the storage and remove them for now...
|
||||||
key: String::from(key_name),
|
//dict: String::from(dict_name),
|
||||||
basis: if basis_name.is_some() {Some(String::from(bname.as_str().unwrap()))} else {None},
|
//key: String::from(key_name),
|
||||||
|
//basis: if basis_name.is_some() {Some(String::from(bname.as_str().unwrap()))} else {None},
|
||||||
pos: 0,
|
pos: 0,
|
||||||
token,
|
token,
|
||||||
buf: Buffer::new(core::mem::size_of::<PddbBuf>()),
|
buf: Buffer::new(core::mem::size_of::<PddbBuf>()),
|
||||||
|
|
|
||||||
|
|
@ -384,7 +384,6 @@ use core::cell::RefCell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::hash::{Hash, Hasher};
|
|
||||||
use std::io::ErrorKind;
|
use std::io::ErrorKind;
|
||||||
use core::fmt::Write;
|
use core::fmt::Write;
|
||||||
|
|
||||||
|
|
@ -396,25 +395,12 @@ pub(crate) struct BasisRequestPassword {
|
||||||
plaintext_pw: Option<xous_ipc::String::<{crate::api::PASSWORD_LEN}>>,
|
plaintext_pw: Option<xous_ipc::String::<{crate::api::PASSWORD_LEN}>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
//#[derive(Eq)]
|
|
||||||
struct TokenRecord {
|
struct TokenRecord {
|
||||||
pub dict: String,
|
pub dict: String,
|
||||||
pub key: String,
|
pub key: String,
|
||||||
pub basis: Option<String>,
|
pub basis: Option<String>,
|
||||||
pub conn: xous::CID, // callback connection
|
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]
|
#[xous::xous_main]
|
||||||
fn xmain() -> ! {
|
fn xmain() -> ! {
|
||||||
|
|
@ -857,6 +843,26 @@ fn xmain() -> ! {
|
||||||
}
|
}
|
||||||
buffer.replace(req).unwrap();
|
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) => {
|
Some(Opcode::ReadKey) => {
|
||||||
// placeholder
|
// placeholder
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ pub struct PddbCmd {
|
||||||
pddb: pddb::Pddb,
|
pddb: pddb::Pddb,
|
||||||
}
|
}
|
||||||
impl PddbCmd {
|
impl PddbCmd {
|
||||||
pub fn new(xns: &xous_names::XousNames) -> PddbCmd {
|
pub fn new(_xns: &xous_names::XousNames) -> PddbCmd {
|
||||||
PddbCmd {
|
PddbCmd {
|
||||||
pddb: pddb::Pddb::new(),
|
pddb: pddb::Pddb::new(),
|
||||||
}
|
}
|
||||||
|
|
@ -15,15 +15,12 @@ impl PddbCmd {
|
||||||
impl<'a> ShellCmdApi<'a> for PddbCmd {
|
impl<'a> ShellCmdApi<'a> for PddbCmd {
|
||||||
cmd_api!(pddb); // inserts boilerplate for command API
|
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;
|
use core::fmt::Write;
|
||||||
let mut ret = String::<1024>::new();
|
let mut ret = String::<1024>::new();
|
||||||
let helpstring = "pddb [basislist]";
|
let helpstring = "pddb [basislist]";
|
||||||
|
|
||||||
let mut tokens = args.as_str().unwrap().split(' ');
|
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() {
|
if let Some(sub_cmd) = tokens.next() {
|
||||||
match sub_cmd {
|
match sub_cmd {
|
||||||
"basislist" => {
|
"basislist" => {
|
||||||
|
|
@ -31,7 +28,7 @@ impl<'a> ShellCmdApi<'a> for PddbCmd {
|
||||||
for basis in bases {
|
for basis in bases {
|
||||||
write!(ret, "{}\n", basis).unwrap();
|
write!(ret, "{}\n", basis).unwrap();
|
||||||
}
|
}
|
||||||
/*
|
/* // example of using .get with a callback
|
||||||
self.pddb.get("foo", "bar", None, false, false,
|
self.pddb.get("foo", "bar", None, false, false,
|
||||||
Some({
|
Some({
|
||||||
let cid = cid.clone();
|
let cid = cid.clone();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue