diff --git a/services/pddb/src/api.rs b/services/pddb/src/api.rs index 5f56735f2..2bd5db353 100644 --- a/services/pddb/src/api.rs +++ b/services/pddb/src/api.rs @@ -139,6 +139,45 @@ pub(crate) enum Opcode { #[cfg(all(feature="pddbtest", feature="autobasis"))] BasisTesting = 26, + ListBasisStd = 26, + CreateBasisStd = 27, + + ListDictStd = 28, + ListKeyStd = 29, + + /// libstd equivalent of `KeyRequest` + OpenKeyStd = 30, + + /// Read a number of bytes from the current offset. + ReadKeyStd = 31, + + /// Write a number of bytes at the current offset + WriteKeyStd = 32, + + /// libstd equivalent of `KeyDrop` + CloseKeyStd = 34, + + /// Remove a key from the database. + DeleteKeyStd = 35, + + /// Return the latest basis. Useful for resolving `::` as a path. + LatestBasisStd = 36, + + /// List all keys and dicts under a given colon-delimited path + ListPathStd = 37, + + /// Get information about a file path + StatPathStd = 38, + + /// Seek a file descriptor + SeekKeyStd = 39, + + /// Create a dict + CreateDictStd = 40, + + /// Remove an empty dict + DeleteDictStd = 41, + /// This key type could not be decoded InvalidOpcode = u32::MAX as _, } diff --git a/services/pddb/src/libstd/mod.rs b/services/pddb/src/libstd/mod.rs new file mode 100644 index 000000000..9ed8062a9 --- /dev/null +++ b/services/pddb/src/libstd/mod.rs @@ -0,0 +1,900 @@ +mod senres; +mod utils; + +use crate::backend::BasisCache; +use crate::backend::PddbOs; +use crate::FileHandle; + +use senres::{Senres, SenresMut}; + +fn get_fd( + fds: &mut Vec>, + fd: usize, +) -> Result<&mut FileHandle, crate::PddbRetcode> { + let fd = fds + .get_mut(fd) + .ok_or_else(|| { + log::info!("file handle {} is out of range", fd); + crate::PddbRetcode::UnexpectedEof + })? + .as_mut() + .ok_or_else(|| { + log::info!("file handle {} was closed already", fd); + crate::PddbRetcode::UnexpectedEof + })?; + if fd.deleted { + return Err(crate::PddbRetcode::BasisLost); + } + Ok(fd) +} + +pub(crate) fn stat_path( + mem: &mut xous::MemoryMessage, + pddb_os: &mut PddbOs, + basis_cache: &mut BasisCache, +) -> Result<(), crate::PddbRetcode> { + // Convert the memory to a Senres buffer + let mut backing = senres::Message::from_mut_slice(mem.buf.as_slice_mut()) + .or(Err(crate::PddbRetcode::InternalError))?; + + let reader = backing + .reader(*b"StaQ") + .ok_or(crate::PddbRetcode::InternalError)?; + let path = reader + .try_get_ref_from::() + .or(Err(crate::PddbRetcode::InternalError))? + .to_owned(); + + // TODO: use the internal cache inside `basis_cache` to avoid cloning + let (basis, remainder) = + utils::split_basis_and_dict(&path, || basis_cache.basis_latest().map(|m| m.to_owned())) + .or(Err(crate::PddbRetcode::InternalError))?; + core::mem::drop(reader); + + let mut writer = backing + .writer(*b"StaR") + .ok_or(crate::PddbRetcode::InternalError)?; + + // The only time these two are empty is when we want to list the default basis, which + // itself is not a valid path + if basis.is_none() && remainder.is_none() { + writer.append(4u8); // None + log::error!( + "path {} has no basis and no remainder, and therefore does not exist", + path + ); + return Ok(()); + } + + if let Some(ref basis) = basis { + if remainder.is_none() { + let basis_list = basis_cache.basis_list(); + + // If the basis matches our name, indicate this is a valid basis + for s in &basis_list { + if s == basis { + // Kind + writer.append(0u8); + // Length + writer.append(0u64); + return Ok(()); + } + } + + // Otherwise, indicate it's nothing + log::error!("remainder is empty on {} and basis doesn't exist", path); + writer.append(4u8); + return Ok(()); + } + } + + let stripped_path = remainder.as_deref().unwrap_or(""); + + // The root is a dict + if stripped_path == "" { + writer.append(1u8); // Dict + return Ok(()); + } + + // Find all dicts that match this string + let dict_list = basis_cache.dict_list(pddb_os, basis.as_deref()); + let is_dict = dict_list.contains(stripped_path); + let mut is_key = false; + + // Find all keys that are in this dict. Ignore errors, since sometimes + // the dict doesn't exist, which is fine. + if let Some((dict_path, key_path)) = stripped_path.rsplit_once(std::path::MAIN_SEPARATOR) { + if let Some(key_list) = basis_cache + .key_list(pddb_os, dict_path, basis.as_deref()) + .map_err(|e| { + // log::error!("unable to get key list: {:?}", e); + e + }) + .ok() + { + if key_list.contains(key_path) { + is_key = true; + } + } + } + + // Add the count of entries + let val = match (is_dict, is_key) { + (true, false) => 1u8, + (false, true) => 2u8, + (true, true) => 3u8, + (false, false) => 4u8, + }; + if val == 4 { + log::error!("path {} couldn't be found", path); + } + writer.append(val); + // Placeholder for file length + writer.append(0u64); + + Ok(()) +} + +pub(crate) fn list_path( + mem: &mut xous::MemoryMessage, + pddb_os: &mut PddbOs, + basis_cache: &mut BasisCache, +) -> Result<(), crate::PddbRetcode> { + // Convert the memory to a Senres buffer + let mut backing = senres::Message::from_mut_slice(mem.buf.as_slice_mut()) + .or(Err(crate::PddbRetcode::InternalError))?; + + let reader = backing + .reader(*b"PthQ") + .ok_or(crate::PddbRetcode::InternalError)?; + + let path = reader + .try_get_ref_from() + .or(Err(crate::PddbRetcode::InternalError))?; + let (basis, dict) = + utils::split_basis_and_dict(path, || basis_cache.basis_latest().map(|m| m.to_owned())) + .or(Err(crate::PddbRetcode::InternalError))?; + + core::mem::drop(path); + core::mem::drop(reader); + + let mut writer = backing + .writer(*b"PthR") + .ok_or(crate::PddbRetcode::InternalError)?; + + // The only time these two are empty is when we want to list the default basis. + if basis.is_none() && dict.is_none() { + let basis_list = basis_cache.basis_list(); + + // Add the count of bases + writer.append(basis_list.len() as u32); + + // Add a list of all bases + for s in &basis_list { + // Entry name + writer.append(s.as_str()); + // Entry kind + writer.append(0u8); + } + + return Ok(()); + } + + let dict = dict.as_deref().unwrap_or(""); + + // Keep a space at the start of the list for us to log the number of elements. + let entry_len_pos = writer.delayed_append(); + + // Find all dicts that match this string + let dict_list = basis_cache.dict_list(pddb_os, basis.as_deref()); + // Find all keys that are in this dict. Ignore errors, since sometimes + // the dict doesn't exist, which is fine. + let key_list = basis_cache + .key_list(pddb_os, dict, basis.as_deref()) + .map_err(|e| { + // log::error!("unable to get key list: {:?}", e); + e + }) + .unwrap_or_default(); + + let mut entries_count = 0u32; + + for key in key_list.iter() { + // If the entry exists, turn it into a DictKey + let kind = if dict_list.contains(key) { 3u8 } else { 2u8 }; + // Entry name + writer.append(key.as_str()); + // Entry kind + writer.append(kind); + entries_count += 1; + } + + for dict in dict_list + .iter() + .filter(|needle| utils::get_path(&needle, dict).is_some()) + .filter(|needle| !key_list.contains(needle.as_str())) + { + if let Some((_, end)) = dict.rsplit_once(':') { + // Entry name + writer.append(end); + // Entry kind + writer.append(1u8); + entries_count += 1; + } + } + + // Add the count of entries + writer.do_delayed_append(entry_len_pos, entries_count); + + Ok(()) +} + +pub(crate) fn list_basis( + mem: &mut xous::MemoryMessage, + basis_cache: &mut BasisCache, +) -> Result<(), crate::PddbRetcode> { + let basis_list = basis_cache.basis_list(); + + // Convert the memory to a Senres buffer + let mut backing = senres::Message::from_mut_slice(mem.buf.as_slice_mut()) + .or(Err(crate::PddbRetcode::InternalError))?; + + { + backing + .reader(*b"basQ") + .ok_or(crate::PddbRetcode::InternalError)?; + } + let mut writer = backing + .writer(*b"basR") + .ok_or(crate::PddbRetcode::InternalError)?; + + // Add the count of bases + writer.append(basis_list.len() as u32); + + // Add a list of all bases + for s in &basis_list { + writer.append(s.as_str()); + } + + Ok(()) +} + +/// Also called `KeyRequest` for non-libstd calls +pub(crate) fn open_key( + mem: &mut xous::MemoryMessage, + pddb_os: &mut PddbOs, + basis_cache: &mut BasisCache, + fds: &mut Vec>, +) -> Result<(), crate::PddbRetcode> { + // Convert the memory to a Senres buffer + let mut backing = senres::Message::from_mut_slice(mem.buf.as_slice_mut()) + .or(Err(crate::PddbRetcode::InternalError))?; + + let create_file: bool; + let create_path: bool; + let create_new: bool; + let append: bool; + let truncate: bool; + let alloc_hint: usize; + let cb_sid: Option; + let (requested_basis, requested_dict) = { + let reader = backing + .reader(*b"KyOQ") + .ok_or(crate::PddbRetcode::InternalError)?; + + let path = reader + .try_get_ref_from() + .or(Err(crate::PddbRetcode::InternalError))?; + create_file = reader + .try_get_from() + .or(Err(crate::PddbRetcode::InternalError))?; + + create_path = reader + .try_get_from() + .or(Err(crate::PddbRetcode::InternalError))?; + + create_new = reader + .try_get_from() + .or(Err(crate::PddbRetcode::InternalError))?; + + append = reader + .try_get_from() + .or(Err(crate::PddbRetcode::InternalError))?; + + truncate = reader + .try_get_from() + .or(Err(crate::PddbRetcode::InternalError))?; + + alloc_hint = reader + .try_get_from::() + .or(Err(crate::PddbRetcode::InternalError))? as usize; + + cb_sid = if let Some(sid) = reader + .try_get_from() + .or(Err(crate::PddbRetcode::InternalError))? + { + Some(xous::SID::from_array(sid)) + } else { + None + }; + utils::split_basis_and_dict(path, || basis_cache.basis_latest().map(|m| m.to_owned())) + .or(Err(crate::PddbRetcode::BasisLost))? + }; + + // Ensure the user passed a valid dict + let requested_dict = requested_dict.ok_or_else(|| { + log::error!("no dict was specified"); + crate::PddbRetcode::BasisLost + })?; + + // Split the final ":key" off from the dict. If there are no ":" then this will fail + // with the "no key was specified" error. + let (requested_dict, requested_key) = requested_dict + .rsplit_once(std::path::MAIN_SEPARATOR) + .ok_or_else(|| { + log::error!("no key was specified"); + crate::PddbRetcode::AccessDenied + })?; + + + let mut writer = backing + .writer(*b"KyOR") + .ok_or(crate::PddbRetcode::InternalError)?; + + // Behavior for opening files: + // 1. If `bname` is defined, then we only loop once. + for basis in basis_cache.access_list().iter() { + if requested_basis.is_some() && Some(basis.as_str()) != requested_basis.as_deref() { + continue; + } + let bname = Some(basis.as_str()); + + // If the dictionary wasn't found, either add it or move on + if basis_cache + .dict_attributes(pddb_os, requested_dict, bname) + .is_err() + { + if !create_path { + continue; + } + basis_cache + .dict_add(pddb_os, requested_dict, bname) + .map_err(|e| { + log::error!( + "unable to add dict {} to basis {}: {:?}", + requested_dict, + bname.unwrap_or("internal_error"), + e + ); + crate::PddbRetcode::InternalError + })?; + } + + let mut len = 0; + + // If the file doesn't exist in this basis, create it (if necessary) + // or mvoe on to the next basis. + if basis_cache + .key_attributes(pddb_os, requested_dict, requested_key, bname) + .map(|attr| len = attr.len as u64) + .is_err() + { + if !create_file { + continue; + } + // create an empty key placeholder + basis_cache + .key_update( + pddb_os, + requested_dict, + requested_key, + &[], + None, + if alloc_hint > 0 { + Some(alloc_hint) + } else { + None + }, + bname, + true, + ) + .map_err(|e| { + log::error!( + "unable to add dict {} to basis {}: {:?}", + requested_dict, + bname.unwrap_or("internal_error"), + e, + ); + crate::PddbRetcode::InternalError + })?; + len = 0; + } else if create_new { + log::error!( + "user requested to create {}{}{} with `create_new` set, but that file already exists", + requested_dict, std::path::MAIN_SEPARATOR, requested_key + ); + return Err(crate::PddbRetcode::DiskFull); + } else if truncate { + // Truncate the file, which we know exists + basis_cache + .key_update( + pddb_os, + requested_dict, + requested_key, + &[], + None, + if alloc_hint > 0 { + Some(alloc_hint) + } else { + None + }, + bname, + true, + ) + .map_err(|e| { + log::error!( + "unable to add dict {} to basis {}: {:?}", + requested_dict, + bname.unwrap_or("internal_error"), + e, + ); + crate::PddbRetcode::InternalError + })?; + } + + // The basis exists for sure. + let conn = + cb_sid.map(|cb_sid| xous::connect(cb_sid).expect("couldn't connect for callback")); + let file_handle = crate::FileHandle { + dict: String::from(requested_dict), + key: String::from(requested_key), + basis: bname.map(|s| s.to_owned()), + offset: if append { len } else { 0 }, + length: len, + deleted: false, + conn, + alloc_hint: if alloc_hint > 0 { + Some(alloc_hint) + } else { + None + }, + }; + + // Look for the first `None` record in the file descriptor list. If there + // isn't one, push it to the end. + let mut fd = None; + let mut token_record = Some(file_handle); + for (index, item) in fds.iter_mut().enumerate() { + if item.is_none() { + *item = token_record.take(); + fd = Some(index); + break; + } + } + let fd = fd.unwrap_or_else(|| { + fds.push(token_record.take()); + fds.len() - 1 + }); + + // Add the file descriptor number + writer.append(fd as u16); + + // Add the file length + writer.append(len); + + return Ok(()); + } + + log::error!( + "unable to find key {} in dict {}", + requested_key, + requested_dict + ); + + // If we fall off the end, then the dict/key do not exist + Err(crate::PddbRetcode::BasisLost) +} + +pub(crate) fn close_key( + fds: &mut Vec>, + fd: usize, +) -> Result<(), crate::PddbRetcode> { + let file = fds.get_mut(fd).ok_or_else(|| { + log::info!("file handle {} is out of range", fd); + crate::PddbRetcode::UnexpectedEof + })?; + + // Remove the file from the list, replacing it with None + let conn = match file.take() { + Some(s) => s.conn, + None => { + log::info!("file handle {} was closed already", fd); + return Err(crate::PddbRetcode::UnexpectedEof); + } + }; + core::mem::drop(file); + + // If this SID is unused, close the connection. + if let Some(cid) = conn { + let mut found_duplicate = false; + for entry in fds.iter() { + if let Some(other_fd) = entry { + if other_fd.conn == conn { + found_duplicate = true; + break; + } + } + } + if !found_duplicate { + unsafe { xous::disconnect(cid).or(Err(crate::PddbRetcode::InternalError))? }; + } + } + + // Shorten the list if we just removed the final element + if fd == fds.len() { + fds.pop(); + } + + Ok(()) +} + +pub(crate) fn delete_key( + mem: &mut xous::MemoryMessage, + pddb_os: &mut PddbOs, + basis_cache: &mut BasisCache, + fds: &mut Vec>, +) -> Result<(), crate::PddbRetcode> { + // Convert the memory to a Senres buffer + let backing = senres::Message::from_mut_slice(mem.buf.as_slice_mut()) + .or(Err(crate::PddbRetcode::InternalError))?; + + let reader = backing + .reader(*b"RmKQ") + .ok_or(crate::PddbRetcode::InternalError)?; + + let full_path = reader + .try_get_ref_from::() + .or(Err(crate::PddbRetcode::BasisLost))?; + + let (basis, path) = utils::split_basis_and_dict(full_path, || { + basis_cache.basis_latest().map(|m| m.to_owned()) + }) + .or(Err(crate::PddbRetcode::AccessDenied))?; + let path = path.ok_or(crate::PddbRetcode::AccessDenied)?; + let bname = basis.as_deref(); + let (dict, key) = path + .rsplit_once(std::path::MAIN_SEPARATOR) + .ok_or(crate::PddbRetcode::AccessDenied)?; + + // Perform the actual removal + basis_cache + .key_remove(pddb_os, dict, key, bname, false) + .or_else(|e| { + log::error!( + "unable to delete key {} in dict {} (basis {:?}): {:?}", + key, + dict, + bname, + e + ); + Err(crate::PddbRetcode::UnexpectedEof) + })?; + + // Mark the entries as deleted in all remaining file handles + for fd in fds + .iter_mut() + .filter(|f| f.is_some()) + .map(|f| f.as_mut().unwrap()) + { + if fd.basis == basis && fd.key == key && fd.dict == dict { + fd.deleted = true; + } + } + + Ok(()) +} + +pub(crate) fn write_key( + mem: &mut xous::MemoryMessage, + pddb_os: &mut PddbOs, + basis_cache: &mut BasisCache, + fds: &mut Vec>, + fd: usize, +) -> Result<(), crate::PddbRetcode> { + let file = get_fd(fds, fd)?; + let mut retcode = crate::PddbRetcode::InternalError; + + for basis in basis_cache.access_list().iter() { + log::debug!( + "write (spec: {:?}){:?} {}", + file.basis, + file.basis.as_ref().unwrap_or(basis), + file.key + ); + let length_to_write = mem.valid.map(|v| v.get()).unwrap_or_default(); + if basis_cache + .key_update( + pddb_os, + &file.dict, + &file.key, + &mem.buf.as_slice_mut()[0..length_to_write], + // &mut pbuf.data[..pbuf.len as usize], + Some(file.offset as usize), + file.alloc_hint, + // this is a bit inefficient because if a specific basis is specified *and* the key does not exist, + // it'll retry the same basis for a number of times equal to the number of bases open. + // However, usually, there's only 1-2 bases open, and usually, if you specify a basis, + // the key will be a hit, so, we let it stand. + Some(file.basis.as_ref().unwrap_or(basis)), + false, + ) + .map_err(|e| match e.kind() { + std::io::ErrorKind::NotFound => retcode = crate::PddbRetcode::BasisLost, + std::io::ErrorKind::UnexpectedEof => retcode = crate::PddbRetcode::UnexpectedEof, + std::io::ErrorKind::OutOfMemory => retcode = crate::PddbRetcode::DiskFull, + _ => retcode = crate::PddbRetcode::InternalError, + }) + .is_ok() + { + file.offset += length_to_write as u64; + mem.valid = xous::MemorySize::new(length_to_write); + return Ok(()); + } + } + + Err(retcode) +} + +pub(crate) fn seek_key( + seek_type: usize, + by_offset: u64, + fds: &mut Vec>, + fd: usize, +) -> Result { + let file = get_fd(fds, fd)?; + + fn seek_from_point( + this: &mut FileHandle, + point: u64, + by: i64, + ) -> Result { + let by64 = by as u64; + // Note that it's possible to seek past the end of a key, and in this case + // the `offset` will be greater than the `len`. This is fine, and `len` will + // be updated as soon as `write()` is called. + if by < 0 { + this.offset = point.checked_sub(by64).ok_or_else(|| { + // std::io::Error::new(std::io::ErrorKind::InvalidInput, "cannot seek before 0") + log::error!("cannot seek before 0"); + crate::PddbRetcode::UnexpectedEof + })?; + } else { + this.offset = point.checked_add(by64).ok_or_else(|| { + // std::io::Error::new(std::io::ErrorKind::InvalidInput, "seek overflowed") + log::error!("seek overflowed"); + crate::PddbRetcode::UnexpectedEof + })?; + } + Ok(this.offset) + } + + match seek_type { + // SeekFrom::Start(offset) + 0 => seek_from_point(file, 0, by_offset as i64), + // SeekFrom::Current(by) + 1 => seek_from_point(file, file.offset, by_offset as i64), + // SeekFrom::End(by) + 2 => seek_from_point(file, file.length, by_offset as i64), + _ => Err(crate::PddbRetcode::UnexpectedEof), + } +} + +pub(crate) fn read_key( + mem: &mut xous::MemoryMessage, + pddb_os: &mut PddbOs, + basis_cache: &mut BasisCache, + fds: &mut Vec>, + fd: usize, +) -> Result<(), crate::PddbRetcode> { + let file = get_fd(fds, fd)?; + + let mut retcode = crate::PddbRetcode::Ok; + for basis in basis_cache.access_list().iter() { + log::debug!( + "read (spec: {:?}){:?} {}", + file.basis, + file.basis.as_ref().unwrap_or(basis), + file.key + ); + if let Ok(readlen) = basis_cache + .key_read( + pddb_os, + &file.dict, + &file.key, + &mut mem.buf.as_slice_mut()[0..mem.valid.map(|v| v.get()).unwrap_or_default()], + Some(file.offset as usize), + // this is a bit inefficient because if a specific basis is specified *and* the key does not exist, + // it'll retry the same basis for a number of times equal to the number of bases open. + // However, usually, there's only 1-2 bases open, and usually, if you specify a basis, + // the key will be a hit, so, we let it stand. + Some(file.basis.as_ref().unwrap_or(basis)), + ) + .map_err(|e| { + match e.kind() { + std::io::ErrorKind::NotFound => retcode = crate::PddbRetcode::BasisLost, + std::io::ErrorKind::UnexpectedEof => { + retcode = crate::PddbRetcode::UnexpectedEof + } + std::io::ErrorKind::OutOfMemory => retcode = crate::PddbRetcode::DiskFull, + _ => retcode = crate::PddbRetcode::InternalError, + }; + }) + { + file.offset += readlen as u64; + mem.valid = xous::MemorySize::new(readlen); + + return Ok(()); + } + } + log::error!( + "couldn't find file {} in dict {} in basis {:?}", + file.key, + file.dict, + file.basis + ); + Err(retcode) +} + +pub(crate) fn list_dict( + mem: &mut xous::MemoryMessage, + pddb_os: &mut PddbOs, + basis_cache: &mut BasisCache, +) -> Result<(), crate::PddbRetcode> { + let mut backing = senres::Message::from_mut_slice(mem.buf.as_slice_mut()) + .or(Err(crate::PddbRetcode::InternalError))?; + let bname; + { + let reader = backing + .reader(*b"LiDQ") + .ok_or(crate::PddbRetcode::InternalError)?; + bname = reader + .try_get_from::>() + .map_err(|_| crate::PddbRetcode::InternalError)?; + } + + let mut writer = backing + .writer(*b"LiDR") + .ok_or(crate::PddbRetcode::InternalError)?; + + let dict_list = basis_cache.dict_list(pddb_os, bname.as_deref()); + writer.append(dict_list.len() as u32); + for dict in dict_list.iter() { + writer.append(dict.as_str()); + } + + Ok(()) +} + +pub(crate) fn list_key( + mem: &mut xous::MemoryMessage, + pddb_os: &mut PddbOs, + basis_cache: &mut BasisCache, +) -> Result<(), crate::PddbRetcode> { + let mut backing = senres::Message::from_mut_slice(mem.buf.as_slice_mut()) + .or(Err(crate::PddbRetcode::InternalError))?; + let bname; + let key; + { + let reader = backing + .reader(*b"LiKQ") + .ok_or(crate::PddbRetcode::InternalError)?; + bname = reader + .try_get_from::>() + .map_err(|_| crate::PddbRetcode::InternalError)?; + key = reader + .try_get_ref_from::() + .map_err(|_| crate::PddbRetcode::InternalError)? + .to_owned(); + } + + let mut writer = backing + .writer(*b"LiKR") + .ok_or(crate::PddbRetcode::InternalError)?; + + let key_list = basis_cache + .key_list(pddb_os, &key, bname.as_deref()) + .or_else(|e| { + log::error!( + "unable to get key list of dict {} in basis {:?}: {:?}", + key, + bname, + e + ); + Err(crate::PddbRetcode::InternalError) + })?; + + writer.append(key_list.len() as u32); + for dict in key_list.iter() { + writer.append(dict.as_str()); + } + + Ok(()) +} + +pub(crate) fn delete_dict( + mem: &mut xous::MemoryMessage, + pddb_os: &mut PddbOs, + basis_cache: &mut BasisCache, +) -> Result<(), crate::PddbRetcode> { + let backing = senres::Message::from_mut_slice(mem.buf.as_slice_mut()) + .or(Err(crate::PddbRetcode::InternalError))?; + let reader = backing + .reader(*b"RmDQ") + .ok_or(crate::PddbRetcode::InternalError)?; + + let path = reader + .try_get_ref_from::() + .or(Err(crate::PddbRetcode::InternalError))?; + let (bname, dict) = + utils::split_basis_and_dict(path, || basis_cache.basis_latest().map(|m| m.to_owned())) + .or(Err(crate::PddbRetcode::InternalError))?; + let dict = dict.ok_or(crate::PddbRetcode::InternalError)?; + + if let Some(key_list) = basis_cache + .key_list(pddb_os, &dict, bname.as_deref()) + .map_err(|e| { + // log::error!("unable to get key list: {:?}", e); + e + }) + .ok() + { + if !key_list.is_empty() { + log::error!("directory {} is not empty", dict); + return Err(crate::PddbRetcode::DiskFull); + } + } + + if basis_cache + .dict_remove(pddb_os, &dict, bname.as_deref(), false) + .is_err() + { + log::error!("error removing dict {} in basis {:?}", dict, bname); + return Err(crate::PddbRetcode::InternalError); + } + + Ok(()) +} + +pub(crate) fn create_dict( + mem: &mut xous::MemoryMessage, + pddb_os: &mut PddbOs, + basis_cache: &mut BasisCache, +) -> Result<(), crate::PddbRetcode> { + let backing = senres::Message::from_mut_slice(mem.buf.as_slice_mut()) + .or(Err(crate::PddbRetcode::InternalError))?; + let reader = backing + .reader(*b"NuDQ") + .ok_or(crate::PddbRetcode::InternalError)?; + + let path = reader + .try_get_ref_from::() + .or(Err(crate::PddbRetcode::InternalError))?; + let (basis, dict) = + utils::split_basis_and_dict(path, || basis_cache.basis_latest().map(|m| m.to_owned())) + .or(Err(crate::PddbRetcode::InternalError))?; + let dict = dict.ok_or(crate::PddbRetcode::InternalError)?; + + basis_cache + .dict_add(pddb_os, &dict, basis.as_deref()) + .map_err(|e| { + log::error!( + "unable to add dict {} to basis {}: {:?}", + dict, + basis.as_deref().unwrap_or("internal_error"), + e + ); + crate::PddbRetcode::InternalError + })?; + + Ok(()) +} diff --git a/services/pddb/src/libstd/senres.rs b/services/pddb/src/libstd/senres.rs new file mode 100644 index 000000000..8d4c5eff9 --- /dev/null +++ b/services/pddb/src/libstd/senres.rs @@ -0,0 +1,711 @@ +#![allow(unused)] +use core::cell::Cell; +use core::convert::TryInto; + +/// Senres V1 always begins with the number 0x344cb6ca to indicate it's valid. +/// This number will change on subsequent versions. +const SENRES_V1_MAGIC: u32 = 0x344cb6ca; + +#[cfg(target_os = "xous")] +/// Copies of these invocation types here for when we're running +/// in environments without libxous. +pub enum InvokeType { + LendMut = 1, + Lend = 2, + Move = 3, + Scalar = 4, + BlockingScalar = 5, +} + +#[cfg(target_os = "xous")] +/// Copies of these invocation types here for when we're running +/// in environments without libxous. +pub enum Syscall { + SendMessage = 16, + ReturnMemory = 20, +} + +#[cfg(target_os = "xous")] +/// Copies of these invocation types here for when we're running +/// in environments without libxous. +pub enum SyscallResult { + Scalar1 = 14, + Scalar2 = 15, + MemoryReturned = 18, +} + +/// A struct to send and receive data. This struct must be page-aligned +/// in order to be sendable across processes. +#[repr(C, align(4096))] +pub struct Stack { + data: [u8; N], +} + +/// A version of the message on the receiving side, reconstituted from +/// a slice from a message. +pub struct Message<'a> { + message_id: usize, + auto_return: bool, + data: &'a [u8], +} + +/// A version of the message on the receiving side, reconstituted from +/// a slice from a message. +pub struct MutableMessage<'a> { + message_id: usize, + mutable: bool, + auto_return: bool, + data: &'a mut [u8], +} + +impl<'a> Message<'a> { + pub fn new(message_id: usize, data: usize, len: usize, mutable: bool) -> Result { + if data & 4095 != 0 || len & 4095 != 0 { + return Err(()); + } + Ok(Message { + message_id, + auto_return: true, + data: unsafe { core::slice::from_raw_parts_mut(data as *mut u8, len) }, + }) + } + // Need to figure out how to make the non-mutable version work, since `data` + // must be a mutable vec. + pub fn from_slice(data: &'a [u8]) -> Result { + Ok(Message { + message_id: 0, + auto_return: false, + data, + }) + } + pub fn from_mut_slice(data: &'a mut [u8]) -> Result, ()> { + Ok(MutableMessage { + message_id: 0, + mutable: true, + auto_return: false, + data, + }) + } + pub fn auto_return(&mut self, enable: bool) { + self.auto_return = enable; + } +} + +#[cfg(target_os = "xous")] +fn return_memory_message(message_id: usize, data_addr: usize, data_len: usize) -> usize { + let a0 = Syscall::ReturnMemory as usize; + + // "Offset" + let a4 = 0; + + // "Valid" + let a5 = 0; + + let mut result: usize; + + unsafe { + core::arch::asm!( + "ecall", + inlateout("a0") a0 => result, + inlateout("a1") message_id => _, + inlateout("a2") data_addr => _, + inlateout("a3") data_len=> _, + inlateout("a4") a4 => _, + inlateout("a5") a5 => _, + out("a6") _, + out("a7") _, + ) + }; + result +} + +impl<'a> Drop for Message<'a> { + fn drop(&mut self) { + if self.auto_return { + #[cfg(target_os = "xous")] + return_memory_message( + self.message_id, + self.data.as_ptr() as usize, + self.data.len(), + ); + } + } +} + +impl<'a> SenresMut for MutableMessage<'a> { + fn as_mut_slice(&mut self) -> &mut [u8] { + self.data + } + fn as_mut_ptr(&mut self) -> *mut u8 { + self as *mut _ as *mut u8 + } +} + +impl<'a> Senres for MutableMessage<'a> { + fn as_slice(&self) -> &[u8] { + self.data + } + fn len(&self) -> usize { + self.data.len() + } + fn as_ptr(&self) -> *const u8 { + self as *const _ as *const u8 + } + fn can_create_writer(&self) -> bool { + self.mutable + } +} + +impl<'a> Senres for Message<'a> { + fn as_slice(&self) -> &[u8] { + self.data + } + fn len(&self) -> usize { + self.data.len() + } + fn as_ptr(&self) -> *const u8 { + self as *const _ as *const u8 + } + fn can_create_writer(&self) -> bool { + false + } +} + +pub trait Senres { + fn as_slice(&self) -> &[u8]; + fn as_ptr(&self) -> *const u8; + fn len(&self) -> usize; + + fn can_create_writer(&self) -> bool { + true + } + + fn reader(&self, fourcc: [u8; 4]) -> Option> + where + Self: core::marker::Sized, + { + let reader = Reader { + backing: self, + offset: core::cell::Cell::new(0), + }; + if SENRES_V1_MAGIC != reader.try_get_from().ok()? { + return None; + } + let target_fourcc: [u8; 4] = reader.try_get_from().ok()?; + if target_fourcc != fourcc { + return None; + } + Some(reader) + } + + #[cfg(not(target_os = "xous"))] + fn lend(&self, _connection: u32, _opcode: usize) -> Result<(), ()> { + Ok(()) + } + + #[cfg(target_os = "xous")] + fn lend(&self, connection: u32, opcode: usize) -> Result<(), ()> { + let mut a0 = Syscall::SendMessage as usize; + let a1: usize = connection.try_into().unwrap(); + let a2 = InvokeType::Lend as usize; + let a3 = opcode; + let a4 = self.as_ptr() as usize; + let a5 = self.len(); + + unsafe { + core::arch::asm!( + "ecall", + inlateout("a0") a0, + inlateout("a1") a1 => _, + inlateout("a2") a2 => _, + inlateout("a3") a3 => _, + inlateout("a4") a4 => _, + inlateout("a5") a5 => _, + out("a6") _, + out("a7") _, + ) + }; + + let result = a0; + if result == SyscallResult::MemoryReturned as usize { + Ok(()) + } else { + println!("Unexpected memory return value: {}", result); + Err(()) + } + } +} + +pub trait SenresMut: Senres { + fn as_mut_slice(&mut self) -> &mut [u8]; + fn as_mut_ptr(&mut self) -> *mut u8; + fn writer(&mut self, fourcc: [u8; 4]) -> Option> + where + Self: core::marker::Sized, + { + if !self.can_create_writer() { + return None; + } + let mut writer = Writer { + backing: self, + offset: 0, + }; + writer.append(SENRES_V1_MAGIC); + writer.append(fourcc); + Some(writer) + } + #[cfg(not(target_os = "xous"))] + fn lend_mut(&mut self, _connection: u32, _opcode: usize) -> Result<(), ()> { + Ok(()) + } + #[cfg(target_os = "xous")] + fn lend_mut(&mut self, connection: u32, opcode: usize) -> Result<(), ()> { + let mut a0 = Syscall::SendMessage as usize; + let mut a1: usize = connection.try_into().unwrap(); + let a2 = InvokeType::LendMut as usize; + let a3 = opcode; + let a4 = self.as_mut_ptr() as usize; + let a5 = self.len(); + + unsafe { + core::arch::asm!( + "ecall", + inlateout("a0") a0, + inlateout("a1") a1, + inlateout("a2") a2 => _, + inlateout("a3") a3 => _, + inlateout("a4") a4 => _, + inlateout("a5") a5 => _, + out("a6") _, + out("a7") _, + ) + }; + + let result = a0; + // let offset = a1; + // let valid = a2; + + if result == SyscallResult::MemoryReturned as usize { + Ok(()) + } else { + println!("Unexpected memory return value: {} ({})", result, a1); + Err(()) + } + } +} + + +pub struct Writer<'a, Backing: SenresMut> { + backing: &'a mut Backing, + offset: usize, +} + +pub struct DelayedWriter> { + offset: usize, + _kind: core::marker::PhantomData, + _backing: core::marker::PhantomData, +} + +pub struct Reader<'a, Backing: Senres> { + backing: &'a Backing, + offset: Cell, +} + +pub trait SenSer { + fn append_to(&self, senres: &mut Writer); +} + +pub trait RecDes { + fn try_get_from(senres: &Reader) -> Result + where + Self: core::marker::Sized; +} + +pub trait RecDesRef<'a, Backing: Senres> { + fn try_get_ref_from(senres: &'a Reader) -> Result<&'a Self, ()>; +} + +impl Stack { + /// Ensure that `N` is a multiple of 4096. This constant should + /// be evaluated in the constructor function. + const CHECK_ALIGNED: () = if N & 4095 != 0 { + panic!("Senres size must be a multiple of 4096") + }; + + pub const fn new() -> Self { + // Ensure the `N` that was specified is a multiple of 4096 + #[allow(clippy::no_effect, clippy::let_unit_value)] + let _ = Self::CHECK_ALIGNED; + Stack { data: [0u8; N] } + } +} + +impl SenresMut for Stack { + fn as_mut_slice(&mut self) -> &mut [u8] { + self.data.as_mut_slice() + } + fn as_mut_ptr(&mut self) -> *mut u8 { + &mut self.data as *mut _ as *mut u8 + } +} + +impl Senres for Stack { + fn as_slice(&self) -> &[u8] { + self.data.as_slice() + } + fn len(&self) -> usize { + N + } + fn as_ptr(&self) -> *const u8 { + &self.data as *const _ as *const u8 + } +} + +impl<'a, Backing: SenresMut> Writer<'a, Backing> { + pub fn append>(&mut self, other: T) { + other.append_to(self); + } + + pub fn delayed_append>(&mut self) -> DelayedWriter { + let delayed_writer = DelayedWriter { + offset: self.offset, + _backing: core::marker::PhantomData::, + _kind: core::marker::PhantomData::, + }; + self.offset += core::mem::size_of::(); + delayed_writer + } + + pub fn do_delayed_append>( + &mut self, + delayed_writer: DelayedWriter, + other: T, + ) { + let current_offset = self.offset; + self.offset = delayed_writer.offset; + other.append_to(self); + if self.offset != delayed_writer.offset + core::mem::size_of::() { + panic!("writer incorrectly increased offset"); + } + self.offset = current_offset; + } + + pub fn align_to(&mut self, alignment: usize) { + while self.offset & (alignment - 1) != 0 { + self.offset += 1; + } + } +} + +impl<'a, Backing: Senres> Reader<'a, Backing> { + pub fn try_get_from>(&self) -> Result { + T::try_get_from(self) + } + + pub fn try_get_ref_from + ?Sized>(&'a self) -> Result<&T, ()> { + T::try_get_ref_from(self) + } + + fn align_to(&self, alignment: usize) { + while self.offset.get() & (alignment - 1) != 0 { + self.offset.set(self.offset.get() + 1); + } + } +} + +macro_rules! primitive_impl { + ($SelfT:ty) => { + impl SenSer for $SelfT { + fn append_to(&self, senres: &mut Writer) { + senres.align_to(core::mem::align_of::()); + for (src, dest) in self + .to_le_bytes() + .iter() + .zip(senres.backing.as_mut_slice()[senres.offset..].iter_mut()) + { + *dest = *src; + senres.offset += 1; + } + } + } + + impl RecDes for $SelfT { + fn try_get_from(senres: &Reader) -> Result { + senres.align_to(core::mem::align_of::()); + let my_size = core::mem::size_of::(); + let offset = senres.offset.get(); + if offset + my_size > senres.backing.as_slice().len() { + return Err(()); + } + let val = Self::from_le_bytes( + senres.backing.as_slice()[offset..offset + my_size] + .try_into() + .unwrap(), + ); + senres.offset.set(offset + my_size); + Ok(val) + } + } + }; +} + +impl SenSer for bool { + fn append_to(&self, senres: &mut Writer) { + senres.align_to(core::mem::align_of::()); + senres.backing.as_mut_slice()[senres.offset] = if *self { 1 } else { 0 }; + senres.offset += 1; + } +} + +impl RecDes for bool { + fn try_get_from(senres: &Reader) -> Result { + senres.align_to(core::mem::align_of::()); + let my_size = core::mem::size_of::(); + let offset = senres.offset.get(); + if offset + my_size > senres.backing.as_slice().len() { + return Err(()); + } + let val = match senres.backing.as_slice()[offset] { + 0 => Ok(false), + 1 => Ok(true), + _ => Err(()), + }; + senres.offset.set(offset + my_size); + val + } +} + +impl, Backing: SenresMut> SenSer for Option { + fn append_to(&self, senres: &mut Writer) { + if let Some(val) = self { + senres.append(1u8); + val.append_to(senres); + } else { + senres.append(0u8); + } + } +} + +impl, Backing: Senres> RecDes for Option { + fn try_get_from(senres: &Reader) -> Result { + if senres.offset.get() + 1 > senres.backing.as_slice().len() { + return Err(()); + } + let check = senres.try_get_from::()?; + if check == 0 { + return Ok(None); + } + if check != 1 { + return Err(()); + } + let my_size = core::mem::size_of::(); + if senres.offset.get() + my_size > senres.backing.as_slice().len() { + return Err(()); + } + Ok(Some(T::try_get_from(senres)?)) + } +} + +primitive_impl! {u8} +primitive_impl! {i8} +primitive_impl! {u16} +primitive_impl! {i16} +primitive_impl! {u32} +primitive_impl! {i32} +primitive_impl! {u64} +primitive_impl! {i64} + +impl, Backing: SenresMut> SenSer for &[T] { + fn append_to(&self, senres: &mut Writer) { + senres.append(self.len() as u32); + for entry in self.iter() { + entry.append_to(senres) + } + } +} + +impl, Backing: SenresMut, const N: usize> SenSer for [T; N] { + fn append_to(&self, senres: &mut Writer) { + // senres.append(self.len() as u32); + senres.align_to(core::mem::align_of::()); + for entry in self.iter() { + entry.append_to(senres) + } + } +} + +impl, Backing: Senres, const N: usize> RecDes for [T; N] { + fn try_get_from(senres: &Reader) -> Result { + let len = core::mem::size_of::(); + senres.align_to(core::mem::align_of::()); + let offset = senres.offset.get(); + if offset + len > senres.backing.as_slice().len() { + return Err(()); + } + + // See https://github.com/rust-lang/rust/issues/61956 for why this + // is awful + let mut output: [core::mem::MaybeUninit; N] = + unsafe { core::mem::MaybeUninit::uninit().assume_init() }; + for elem in &mut output[..] { + elem.write(T::try_get_from(senres)?); + } + + // Using &mut as an assertion of unique "ownership" + let ptr = &mut output as *mut _ as *mut [T; N]; + let res = unsafe { ptr.read() }; + core::mem::forget(output); + Ok(res) + } +} + +impl SenSer for str { + fn append_to(&self, senres: &mut Writer) { + senres.append(self.len() as u32); + for (src, dest) in self + .as_bytes() + .iter() + .zip(senres.backing.as_mut_slice()[senres.offset..].iter_mut()) + { + *dest = *src; + senres.offset += 1; + } + } +} + +impl SenSer for &str { + fn append_to(&self, senres: &mut Writer) { + senres.append(self.len() as u32); + for (src, dest) in self + .as_bytes() + .iter() + .zip(senres.backing.as_mut_slice()[senres.offset..].iter_mut()) + { + *dest = *src; + senres.offset += 1; + } + } +} + +impl RecDes for String { + fn try_get_from(senres: &Reader) -> Result { + let len = senres.try_get_from::()? as usize; + let offset = senres.offset.get(); + if offset + len > senres.backing.as_slice().len() { + return Err(()); + } + core::str::from_utf8(&senres.backing.as_slice()[offset..offset + len]) + .or(Err(())) + .map(|e| { + senres.offset.set(offset + len); + e.to_owned() + }) + } +} + +impl<'a, Backing: Senres> RecDesRef<'a, Backing> for str { + fn try_get_ref_from(senres: &'a Reader) -> Result<&'a Self, ()> { + let len = senres.try_get_from::()? as usize; + let offset = senres.offset.get(); + if offset + len > senres.backing.as_slice().len() { + return Err(()); + } + core::str::from_utf8(&senres.backing.as_slice()[offset..offset + len]) + .or(Err(())) + .map(|e| { + senres.offset.set(offset + len); + e + }) + } +} + +impl<'a, Backing: Senres, T: RecDes> RecDesRef<'a, Backing> for [T] { + fn try_get_ref_from(senres: &'a Reader) -> Result<&'a Self, ()> { + let len = senres.try_get_from::()? as usize; + let offset = senres.offset.get(); + if offset + (len * core::mem::size_of::()) > senres.backing.as_slice().len() { + return Err(()); + } + let ret = unsafe { + core::slice::from_raw_parts( + senres.backing.as_slice().as_ptr().add(offset) as *const T, + len, + ) + }; + senres.offset.set(offset + len * core::mem::size_of::()); + Ok(ret) + } +} + +impl Default for Stack { + fn default() -> Self { + Self::new() + } +} + +fn do_stuff(_r: &Stack) { + println!("Stuff!"); +} + +#[test] +fn smoke_test() { + let mut sr1 = Stack::<4096>::new(); + // let sr2 = Senres::<4097>::new(); + let sr3 = Stack::<8192>::new(); + // let sr4 = Senres::<4098>::new(); + let sr5 = Stack::new(); + + do_stuff(&sr5); + println!("Size of sr1: {}", core::mem::size_of_val(&sr1)); + println!("Size of sr3: {}", core::mem::size_of_val(&sr3)); + println!("Size of sr5: {}", core::mem::size_of_val(&sr5)); + + { + let mut writer = sr1.writer(*b"test").unwrap(); + writer.append(16777215u32); + writer.append(u64::MAX); + writer.append("Hello, world!"); + writer.append("String2"); + writer.append::>(None); + writer.append::>(Some(42)); + writer.append(96u8); + writer.append([1i32, 2, 3, 4, 5].as_slice()); + writer.append([5u8, 4, 3, 2].as_slice()); + writer.append([5u16, 4, 2]); + writer.append(["Hi", "There", "123456789"]); + // writer.append(["Hello".to_owned(), "There".to_owned(), "World".to_owned()].as_slice()); + } + // println!("sr1: {:?}", sr1); + + { + let reader = sr1.reader(*b"test").expect("couldn't get reader"); + let val: u32 = reader.try_get_from().expect("couldn't get the u32 value"); + println!("u32 val: {}", val); + let val: u64 = reader.try_get_from().expect("couldn't get the u64 value"); + println!("u64 val: {:x}", val); + let val: &str = reader + .try_get_ref_from() + .expect("couldn't get string value"); + println!("String val: {}", val); + let val: String = reader.try_get_from().expect("couldn't get string2 value"); + println!("String2 val: {}", val); + let val: Option = reader.try_get_from().expect("couldn't get Option"); + println!("Option val: {:?}", val); + let val: Option = reader.try_get_from().expect("couldn't get Option"); + println!("Option val: {:?}", val); + + let val: u8 = reader + .try_get_from() + .expect("couldn't get u8 weird padding"); + println!("u8 val: {}", val); + + let val: &[i32] = reader.try_get_ref_from().expect("couldn't get &[i32]"); + println!("&[i32] val: {:?}", val); + let val: &[u8] = reader.try_get_ref_from().expect("couldn't get &[u8]"); + println!("&[u8] val: {:?}", val); + let val: [u16; 3] = reader.try_get_from().expect("couldn't get [u16; 3]"); + println!("[u16; 3] val: {:?}", val); + let val: [String; 3] = reader.try_get_from().expect("couldn't get [String; 3]"); + println!("[String; 3] val: {:?}", val); + } +} diff --git a/services/pddb/src/libstd/utils.rs b/services/pddb/src/libstd/utils.rs new file mode 100644 index 000000000..cece3aa78 --- /dev/null +++ b/services/pddb/src/libstd/utils.rs @@ -0,0 +1,174 @@ +use std::io; +pub const MAIN_SEP: char = ':'; + +pub fn get_path<'a>(s: &'a str, prefix: &'a str) -> Option<&'a str> { + // Empty strings are invalid + if s.is_empty() { + return None; + } + // The "" prefix indicates the root + if prefix.is_empty() { + let mut s_iter = s.split(':'); + let base = s_iter.next(); + let remainder = s_iter.next(); + if remainder.is_some() { + return None; + } + return base; + } + let without_prefix = s.strip_prefix(prefix)?.strip_prefix(':')?; + let mut path_split = without_prefix.split(':'); + let parent = path_split.next(); + if path_split.next().is_some() { + None + } else { + parent + } +} + +/// Split a path into its constituant Basis and Dict, if the path is legal. +pub fn split_basis_and_dict Option>(src: &str, mut default: F) -> io::Result<(Option, Option)> { + let mut basis = None; + let dict; + if let Some(src) = src.strip_prefix(MAIN_SEP) { + if let Some((maybe_basis, maybe_dict)) = src.split_once(MAIN_SEP) { + if !maybe_basis.is_empty() { + basis = Some(maybe_basis.to_owned()); + } else { + basis = default(); + } + + if maybe_dict.is_empty() { + dict = None; + } else { + dict = Some(maybe_dict.to_owned()); + } + } else { + if !src.is_empty() { + basis = Some(src.to_owned()); + } + dict = None; + } + } else { + if src.is_empty() { + return Ok((basis, Some("".to_owned()))); + } + dict = Some(src.to_owned()); + } + + if let Some(basis) = &basis { + if basis.ends_with(MAIN_SEP) { + return Err(io::Error::new(io::ErrorKind::Other, "invalid path")); + } + } + if let Some(dict) = &dict { + if dict.ends_with(MAIN_SEP) { + return Err(io::Error::new(io::ErrorKind::Other, "invalid path")); + } + } + Ok((basis, dict)) +} + +#[cfg(test)] +fn default_path() -> Option { + Some("{DEFAULT}".to_owned()) +} + +#[test] +fn empty_string() { + assert_eq!(split_basis_and_dict("", default_path).unwrap(), (None, Some("".to_owned()))); +} + +#[test] +fn bare_dict() { + assert_eq!(split_basis_and_dict("one", default_path).unwrap(), (None, Some("one".to_owned()))); +} + +#[test] +fn dict_with_colon() { + assert_eq!(split_basis_and_dict("one:two", default_path).unwrap(), (None, Some("one:two".to_owned()))); +} + +#[test] +fn dict_with_two_colons() { + assert_eq!(split_basis_and_dict("one:two:three", default_path).unwrap(), (None, Some("one:two:three".to_owned()))); +} + +#[test] +#[should_panic] +fn dict_with_trailing_colon() { + split_basis_and_dict("one:", default_path).unwrap(); +} + +#[test] +#[should_panic] +fn two_dicts_with_trailing_colon() { + split_basis_and_dict("one:two:", default_path).unwrap(); +} + +#[test] +#[should_panic] +fn basis_with_dict_with_trailing_colon() { + split_basis_and_dict(":one:two:", default_path).unwrap(); +} + +#[test] +#[should_panic] +fn basis_with_two_dicts_with_trailing_colon() { + split_basis_and_dict(":one:two:three:", default_path).unwrap(); +} + +#[test] +fn basis_missing_colon() { + assert_eq!(split_basis_and_dict(":one", default_path).unwrap(), (Some("one".to_owned()), None)); +} + +#[test] +fn basis_with_one_dict() { + assert_eq!(split_basis_and_dict(":one:two", default_path).unwrap(), (Some("one".to_owned()), Some("two".to_owned()))); +} + +#[test] +fn basis_with_two_dicts() { + assert_eq!(split_basis_and_dict(":one:two:three", default_path).unwrap(), (Some("one".to_owned()), Some("two:three".to_owned()))); +} +#[test] +fn double_colon() { + let default = default_path(); + assert_eq!(split_basis_and_dict("::", default_path).unwrap(), (default, None)); +} + +#[test] +fn single_colon() { + assert_eq!(split_basis_and_dict(":", default_path).unwrap(), (None, None)); +} + +#[test] +fn double_colon_two_keys() { + let default = default_path(); + assert_eq!(split_basis_and_dict("::foo:bar", default_path).unwrap(), (default, Some("foo:bar".to_owned()))); +} + +#[test] +fn double_colon_three_keys() { + let default = default_path(); + assert_eq!(split_basis_and_dict("::foo:bar:baz", default_path).unwrap(), (default, Some("foo:bar:baz".to_owned()))); +} + +#[test] +#[should_panic] +fn double_colon_three_keys_trailing_colon() { + split_basis_and_dict("::foo:bar:baz:", default_path).unwrap(); +} + +#[test] +#[should_panic] +fn dict_with_two_keys_two_trailing_colons() { + split_basis_and_dict("foo:bar::", default_path).unwrap(); +} + +#[test] +#[should_panic] +fn dict_with_two_keys_three_trailing_colons() { + split_basis_and_dict("foo:bar:::", default_path).unwrap(); +} \ No newline at end of file diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index 47d16e1b4..0bffc070a 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -373,6 +373,8 @@ use ux::*; mod menu; use menu::*; +mod libstd; + #[cfg(not(any(target_os = "none", target_os = "xous")))] mod tests; #[cfg(not(any(target_os = "none", target_os = "xous")))] @@ -413,6 +415,21 @@ struct TokenRecord { pub alloc_hint: Option, pub conn: Option, // callback connection, if one was specified } + +struct FileHandle { + pub dict: String, + pub key: String, + pub basis: Option, + pub alloc_hint: Option, + pub offset: u64, + pub length: u64, + pub conn: Option, // callback connection, if one was specified + + /// This is set to `true` when a file is removed in order to prevent + /// other operations from functioning. + pub deleted: bool, +} + fn main () -> ! { let stack_size = 1024 * 1024; std::thread::Builder::new() @@ -422,6 +439,7 @@ fn main () -> ! { .join() .unwrap() } + fn wrapped_main() -> ! { log_server::init_wait().unwrap(); log::set_max_level(log::LevelFilter::Info); @@ -457,6 +475,9 @@ fn wrapped_main() -> ! { // storage for the token lookup: given an ApiToken, return a dict/key/basis set. Basis can be None or specified. let mut token_dict = HashMap::::new(); + // Process-indexed map of file descriptors to token records + let mut fd_mapping = HashMap::, Vec>>::new(); + // mount poller thread let is_mounted = Arc::new(AtomicBool::new(false)); let _ = thread::spawn({ @@ -466,7 +487,7 @@ fn wrapped_main() -> ! { let poller_sid = xns.register_name(api::SERVER_NAME_PDDB_POLLER, None).expect("can't register server"); loop { let msg = xous::receive_message(poller_sid).unwrap(); - match FromPrimitive::from_usize(msg.body.id()) { + match FromPrimitive::from_usize(msg.body.id() & 0xffff) { Some(PollOp::Poll) => xous::msg_blocking_scalar_unpack!(msg, _, _, _, _, { if is_mounted.load(Ordering::SeqCst) { xous::return_scalar(msg.sender, 1).unwrap(); @@ -576,7 +597,8 @@ fn wrapped_main() -> ! { Opcode::SuspendResume as u32, my_cid).expect("couldn't create suspend/resume object"); loop { let mut msg = xous::receive_message(pddb_sid).unwrap(); - match FromPrimitive::from_usize(msg.body.id()).unwrap_or(Opcode::InvalidOpcode) { + // log::error!("got msg: {:x?}", msg); + match FromPrimitive::from_usize(msg.body.id() & 0xffff).unwrap_or(Opcode::InvalidOpcode) { Opcode::SuspendResume => xous::msg_scalar_unpack!(msg, token, _, _, _, { basis_cache.suspend(&mut pddb_os); susres.suspend_until_resume(token).expect("couldn't execute suspend/resume"); @@ -672,6 +694,30 @@ fn wrapped_main() -> ! { list_ipc.num = basis_list.len() as u32; buffer.replace(list_ipc).unwrap(); } + Opcode::ListBasisStd => { + if let Some(mem) = msg.body.memory_message_mut() { + mem.offset = None; + if let Err(e) = libstd::list_basis(mem, &mut basis_cache) { + mem.offset = xous::MemoryAddress::new(e as usize); + } + } + } + Opcode::ListPathStd => { + if let Some(mem) = msg.body.memory_message_mut() { + mem.offset = None; + if let Err(e) = libstd::list_path(mem, &mut pddb_os, &mut basis_cache) { + mem.offset = xous::MemoryAddress::new(e as usize); + } + } + } + Opcode::StatPathStd => { + if let Some(mem) = msg.body.memory_message_mut() { + mem.offset = None; + if let Err(e) = libstd::stat_path(mem, &mut pddb_os, &mut basis_cache) { + mem.offset = xous::MemoryAddress::new(e as usize); + } + } + } Opcode::LatestBasis => { let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut mgmt = buffer.to_original::().unwrap(); @@ -684,6 +730,20 @@ fn wrapped_main() -> ! { } buffer.replace(mgmt).unwrap(); } + Opcode::LatestBasisStd => { + if let Some(mem) = msg.body.memory_message_mut() { + mem.offset = None; + if let Some(name) = basis_cache.basis_latest() { + for (src, dest) in name.as_bytes().iter().zip(mem.buf.as_slice_mut().iter_mut()) { + *dest = *src; + } + mem.offset = xous::MemorySize::new(name.len().min(mem.buf.len())); + } + } + } + Opcode::CreateBasisStd => { + unimplemented!() + } Opcode::CreateBasis => { let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut mgmt = buffer.to_original::().unwrap(); @@ -880,6 +940,15 @@ fn wrapped_main() -> ! { break; // if we got here, entry was found, stop searching } } + Opcode::OpenKeyStd => { + if let Some(mem) = msg.body.memory_message_mut() { + mem.offset = None; + if let Err(e) = libstd::open_key(mem, &mut pddb_os, &mut basis_cache, fd_mapping.entry(msg.sender.pid()).or_default()) { + mem.offset = xous::MemoryAddress::new(e as usize); + } + } + } + Opcode::KeyDrop => msg_blocking_scalar_unpack!(msg, t0, t1, t2, _, { let token: ApiToken = [t0 as u32, t1 as u32, t2 as u32]; if let Some(rec) = token_dict.remove(&token) { @@ -903,9 +972,26 @@ fn wrapped_main() -> ! { } else { // if there was no/never a connection allocated, there's no connection to remove. do nothing. } + xous::return_scalar(msg.sender, PddbRetcode::Ok as usize).expect("couldn't ack KeyDrop"); + } else { + xous::return_scalar(msg.sender, PddbRetcode::BasisLost as usize).expect("couldn't ack KeyDrop"); } - xous::return_scalar(msg.sender, 1).expect("couldn't ack KeyDrop"); }), + + Opcode::CloseKeyStd => { + let fd = (msg.body.id() >> 16) & 0xffff; + if msg.body.scalar_message().is_some() { + let result = libstd::close_key(fd_mapping.entry(msg.sender.pid()).or_default(), fd); + if msg.body.is_blocking() { + if let Err(e) = result { + xous::return_scalar(msg.sender, e as usize) + } else { + xous::return_scalar2(msg.sender, 0, 0) + }.ok(); + } + } + } + Opcode::DeleteKey => { let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut req: PddbKeyRequest = buffer.to_original::().unwrap(); @@ -956,6 +1042,14 @@ fn wrapped_main() -> ! { } buffer.replace(req).unwrap(); } + Opcode::DeleteKeyStd => { + if let Some(mem) = msg.body.memory_message_mut() { + mem.offset = None; + if let Err(err) = libstd::delete_key(mem, &mut pddb_os, &mut basis_cache, fd_mapping.entry(msg.sender.pid()).or_default()) { + mem.offset = xous::MemoryAddress::new(err as usize); + } + } + } Opcode::DeleteDict => { let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut req: PddbKeyRequest = buffer.to_original::().unwrap(); @@ -1006,6 +1100,22 @@ fn wrapped_main() -> ! { } buffer.replace(req).unwrap(); } + Opcode::DeleteDictStd => { + if let Some(mem) = msg.body.memory_message_mut() { + mem.offset = None; + if let Err(err) = libstd::delete_dict(mem, &mut pddb_os, &mut basis_cache) { + mem.offset = xous::MemoryAddress::new(err as usize); + } + } + } + Opcode::CreateDictStd => { + if let Some(mem) = msg.body.memory_message_mut() { + mem.offset = None; + if let Err(err) = libstd::create_dict(mem, &mut pddb_os, &mut basis_cache) { + mem.offset = xous::MemoryAddress::new(err as usize); + } + } + } Opcode::KeyAttributes => { let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut req = buffer.to_original::().unwrap(); @@ -1099,6 +1209,14 @@ fn wrapped_main() -> ! { } buffer.replace(req).unwrap(); } + Opcode::ListKeyStd => { + if let Some(mem) = msg.body.memory_message_mut() { + mem.offset = None; + if let Err(e) = libstd::list_key(mem, &mut pddb_os, &mut basis_cache) { + mem.offset = xous::MemoryAddress::new(e as usize); + } + } + } Opcode::DictCountInBasis => { let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut req = buffer.to_original::().unwrap(); @@ -1151,6 +1269,14 @@ fn wrapped_main() -> ! { } buffer.replace(req).unwrap(); } + Opcode::ListDictStd => { + if let Some(mem) = msg.body.memory_message_mut() { + mem.offset = None; + if let Err(e) = libstd::list_dict(mem, &mut pddb_os, &mut basis_cache) { + mem.offset = xous::MemoryAddress::new(e as usize); + } + } + } Opcode::ReadKey => { let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let pbuf = PddbBuf::from_slice_mut(buffer.as_mut()); // direct translation, no serialization necessary for performance @@ -1185,6 +1311,31 @@ fn wrapped_main() -> ! { } // we don't nede a "replace" operation because all ops happen in-place } + + Opcode::SeekKeyStd => { + let fd = (msg.body.id() >> 16) & 0xffff; + if let Some(scalar) = msg.body.scalar_message() { + let seek_by = (((scalar.arg2 as u32) as u64) << 32) | ((scalar.arg3 as u32) as u64); + let result = libstd::seek_key(scalar.arg1, seek_by, fd_mapping.entry(msg.sender.pid()).or_default(), fd); + if msg.body.is_blocking() { + match result { + Ok(offset) => xous::return_scalar2(msg.sender, offset as usize, (offset >> 32) as usize).ok(), + Err(e) =>xous::return_scalar(msg.sender, e as usize).ok(), + }; + } + } + } + + Opcode::ReadKeyStd => { + let fd = (msg.body.id() >> 16) & 0xffff; + if let Some(mem) = msg.body.memory_message_mut() { + mem.offset = None; + if let Err(e) = libstd::read_key(mem, &mut pddb_os, &mut basis_cache, fd_mapping.entry(msg.sender.pid()).or_default(), fd) { + mem.offset = xous::MemoryAddress::new(e as usize); + } + } + } + Opcode::WriteKey => { let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let pbuf = PddbBuf::from_slice_mut(buffer.as_mut()); // direct translation, no serialization necessary for performance @@ -1219,6 +1370,17 @@ fn wrapped_main() -> ! { // for now, do an expensive sync operation after every write to ensure data integrity basis_cache.sync(&mut pddb_os, None).expect("couldn't sync basis"); } + + Opcode::WriteKeyStd => { + let fd = (msg.body.id() >> 16) & 0xffff; + if let Some(mem) = msg.body.memory_message_mut() { + mem.offset = None; + if let Err(e) = libstd::write_key(mem, &mut pddb_os, &mut basis_cache, fd_mapping.entry(msg.sender.pid()).or_default(), fd) { + mem.offset = xous::MemoryAddress::new(e as usize); + } + } + } + Opcode::WriteKeyFlush => msg_blocking_scalar_unpack!(msg, _, _, _, _, { match basis_cache.sync(&mut pddb_os, None) { Ok(_) => xous::return_scalar(msg.sender, PddbRetcode::Ok.to_usize().unwrap()).unwrap(), @@ -1229,6 +1391,7 @@ fn wrapped_main() -> ! { } }; }), + Opcode::MenuListBasis => { let bases = basis_cache.basis_list(); let mut note = String::from(t!("pddb.menu.listbasis_response", xous::LANG));