Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ pub mod request {
ReadDirFilesFirst:
- location: Location
- dir: PathBuf
- user_attribute: Option<UserAttribute>

ReadDirFilesNext:

Expand Down Expand Up @@ -279,8 +278,7 @@ pub mod request {
WriteFile:
- location: Location
- path: PathBuf
- data: Message
- user_attribute: Option<UserAttribute>
- data: LargeMessage

UnsafeInjectKey:
- mechanism: Mechanism // -> implies key type
Expand Down Expand Up @@ -339,7 +337,7 @@ pub mod request {

WriteCertificate:
- location: Location
- der: Message
- der: LargeMessage

SerdeExtension:
- id: u8
Expand Down Expand Up @@ -429,7 +427,7 @@ pub mod reply {
- entry: Option<DirEntry>

ReadFile:
- data: Message
- data: LargeMessage

Metadata:
- metadata: Option<crate::types::Metadata>
Expand Down Expand Up @@ -490,7 +488,7 @@ pub mod reply {
DeleteCertificate:

ReadCertificate:
- der: Message
- der: LargeMessage

WriteCertificate:
- id: CertId
Expand Down
13 changes: 3 additions & 10 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ pub trait CertificateClient: PollClient {
location: Location,
der: &[u8],
) -> ClientResult<'_, reply::WriteCertificate, Self> {
let der = Message::from_slice(der).map_err(|_| ClientError::DataTooLarge)?;
let der = LargeMessage::from_slice(der).map_err(|_| ClientError::DataTooLarge)?;
self.request(request::WriteCertificate { location, der })
}
}
Expand Down Expand Up @@ -577,13 +577,8 @@ pub trait FilesystemClient: PollClient {
&mut self,
location: Location,
dir: PathBuf,
user_attribute: Option<UserAttribute>,
) -> ClientResult<'_, reply::ReadDirFilesFirst, Self> {
self.request(request::ReadDirFilesFirst {
dir,
location,
user_attribute,
})
self.request(request::ReadDirFilesFirst { dir, location })
}

fn read_dir_files_next(&mut self) -> ClientResult<'_, reply::ReadDirFilesNext, Self> {
Expand Down Expand Up @@ -650,14 +645,12 @@ pub trait FilesystemClient: PollClient {
&mut self,
location: Location,
path: PathBuf,
data: Message,
user_attribute: Option<UserAttribute>,
data: LargeMessage,
) -> ClientResult<'_, reply::WriteFile, Self> {
self.request(request::WriteFile {
location,
path,
data,
user_attribute,
})
}
}
Expand Down
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use littlefs2::consts;
pub type MAX_APPLICATION_NAME_LENGTH = consts::U256;
pub const MAX_LONG_DATA_LENGTH: usize = 1024;
pub const MAX_MESSAGE_LENGTH: usize = 1024;
pub const MAX_LARGE_MESSAGE_LENGTH: usize = 2048;
pub type MAX_OBJECT_HANDLES = consts::U16;
pub type MAX_LABEL_LENGTH = consts::U256;
pub const MAX_MEDIUM_DATA_LENGTH: usize = 256;
Expand Down
2 changes: 1 addition & 1 deletion src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ impl<P: Platform> ServiceResources<P> {
}

Request::ReadDirFilesFirst(request) => {
let maybe_data = match filestore.read_dir_files_first(&request.dir, request.location, request.user_attribute.clone())? {
let maybe_data = match filestore.read_dir_files_first(&request.dir, request.location)? {
Some((data, state)) => {
ctx.read_dir_files_state = Some(state);
data
Expand Down
12 changes: 6 additions & 6 deletions src/store/certstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use littlefs2::path::PathBuf;
use crate::{
error::{Error, Result},
store::{self, Store},
types::{CertId, Location, Message},
types::{CertId, LargeMessage, Location},
};

pub struct ClientCertstore<S>
Expand All @@ -18,11 +18,11 @@ where

pub trait Certstore {
fn delete_certificate(&mut self, id: CertId) -> Result<()>;
fn read_certificate(&mut self, id: CertId) -> Result<Message>;
fn read_certificate(&mut self, id: CertId) -> Result<LargeMessage>;
/// TODO: feels a bit heavy-weight to pass in the ClientCounterstore here
/// just to ensure the next global counter ("counter zero") is used, and
/// not something random.
fn write_certificate(&mut self, location: Location, der: &Message) -> Result<CertId>;
fn write_certificate(&mut self, location: Location, der: &[u8]) -> Result<CertId>;
}

impl<S: Store> Certstore for ClientCertstore<S> {
Expand All @@ -36,7 +36,7 @@ impl<S: Store> Certstore for ClientCertstore<S> {
.ok_or(Error::NoSuchKey)
}

fn read_certificate(&mut self, id: CertId) -> Result<Message> {
fn read_certificate(&mut self, id: CertId) -> Result<LargeMessage> {
let path = self.cert_path(id);
let locations = [Location::Internal, Location::External, Location::Volatile];
locations
Expand All @@ -45,10 +45,10 @@ impl<S: Store> Certstore for ClientCertstore<S> {
.ok_or(Error::NoSuchCertificate)
}

fn write_certificate(&mut self, location: Location, der: &Message) -> Result<CertId> {
fn write_certificate(&mut self, location: Location, der: &[u8]) -> Result<CertId> {
let id = CertId::new(&mut self.rng);
let path = self.cert_path(id);
store::store(self.store, location, &path, der.as_slice())?;
store::store(self.store, location, &path, der)?;
Ok(id)
}
}
Expand Down
54 changes: 5 additions & 49 deletions src/store/filestore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
error::{Error, Result},
// service::ReadDirState,
store::{self, Store},
types::{Location, Message, UserAttribute},
types::{Location, Message},
Bytes,
};

Expand All @@ -17,7 +17,6 @@ pub struct ReadDirFilesState {
real_dir: PathBuf,
last: usize,
location: Location,
user_attribute: Option<UserAttribute>,
}

use littlefs2::{
Expand Down Expand Up @@ -119,7 +118,6 @@ pub trait Filestore {
&mut self,
clients_dir: &PathBuf,
location: Location,
user_attribute: Option<UserAttribute>,
) -> Result<Option<(Option<Message>, ReadDirFilesState)>>;

/// Continuation of `read_dir_files_first`.
Expand Down Expand Up @@ -271,7 +269,6 @@ impl<S: Store> Filestore for ClientFilestore<S> {
&mut self,
clients_dir: &PathBuf,
location: Location,
user_attribute: Option<UserAttribute>,
) -> Result<Option<(Option<Message>, ReadDirFilesState)>> {
if location != Location::Internal {
return Err(Error::RequestNotAvailable);
Expand All @@ -289,35 +286,13 @@ impl<S: Store> Filestore for ClientFilestore<S> {
//
// Option<usize, Result<DirEntry>> -> ??
.map(|(i, entry)| (i, entry.unwrap()))
// skip over directories (including `.` and `..`)
.filter(|(_, entry)| entry.file_type().is_file())
// take first entry that meets requirements
.find(|(_, entry)| {
if let Some(user_attribute) = user_attribute.as_ref() {
let mut path = dir.clone();
path.push(entry.file_name());
let attribute = fs
.attribute(&path, crate::config::USER_ATTRIBUTE_NUMBER)
.unwrap();

if let Some(attribute) = attribute {
user_attribute == attribute.data()
} else {
false
}
} else {
true
}
})
// if there is an entry, construct the state that needs storing out of it,
// and return the file's contents.
// the client, and return both the entry and the state
// skip over directories (including `.` and `..`) and take first file
.find(|(_, entry)| entry.file_type().is_file())
.map(|(i, entry)| {
let read_dir_files_state = ReadDirFilesState {
real_dir: dir.clone(),
last: i,
location,
user_attribute,
};
// The semantics is that for a non-existent file, we return None (not an error)
let data = store::read(self.store, location, entry.path()).ok();
Expand All @@ -340,7 +315,6 @@ impl<S: Store> Filestore for ClientFilestore<S> {
real_dir,
last,
location,
user_attribute,
} = state;
let fs = self.store.ifs();

Expand All @@ -353,31 +327,13 @@ impl<S: Store> Filestore for ClientFilestore<S> {
.skip(last + 1)
// entry is still a Result :/ (see question in `read_dir_first`)
.map(|(i, entry)| (i, entry.unwrap()))
// skip over directories (including `.` and `..`)
.filter(|(_, entry)| entry.file_type().is_file())
// take first entry that meets requirements
.find(|(_, entry)| {
if let Some(user_attribute) = user_attribute.as_ref() {
let mut path = real_dir.clone();
path.push(entry.file_name());
let attribute = fs
.attribute(&path, crate::config::USER_ATTRIBUTE_NUMBER)
.unwrap();
if let Some(attribute) = attribute {
user_attribute == attribute.data()
} else {
false
}
} else {
true
}
})
// skip over directories (including `.` and `..`) and take first file
.find(|(_, entry)| entry.file_type().is_file())
.map(|(i, entry)| {
let read_dir_files_state = ReadDirFilesState {
real_dir: real_dir.clone(),
last: i,
location,
user_attribute,
};
// The semantics is that for a non-existent file, we return None (not an error)
let data = store::read(self.store, location, entry.path()).ok();
Expand Down
7 changes: 1 addition & 6 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,12 +528,7 @@ fn filesystem() {

let data = Bytes::from_slice(&[0; 20]).unwrap();
block!(client
.write_file(
Location::Internal,
PathBuf::from("test_file"),
data.clone(),
None,
)
.write_file(Location::Internal, PathBuf::from("test_file"), data.clone())
.expect("no client error"))
.expect("no errors");

Expand Down
3 changes: 1 addition & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ pub type ShortData = Bytes<MAX_SHORT_DATA_LENGTH>;

pub type Message = Bytes<MAX_MESSAGE_LENGTH>;
pub type SerializedKey = Bytes<MAX_KEY_MATERIAL_LENGTH>;
pub type LargeMessage = Bytes<MAX_LARGE_MESSAGE_LENGTH>;

#[derive(Copy, Clone, Eq, PartialEq, Debug, Serialize, Deserialize)]
pub enum KeySerialization {
Expand All @@ -615,5 +616,3 @@ pub enum SignatureSerialization {
Raw,
// Sec1,
}

pub type UserAttribute = Bytes<MAX_USER_ATTRIBUTE_LENGTH>;
4 changes: 2 additions & 2 deletions tests/backends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use trussed::{
error::Error,
platform,
service::{Service, ServiceResources},
types::{Context, CoreContext, Location, Message, PathBuf},
types::{Context, CoreContext, LargeMessage, Location, PathBuf},
virt::{self, Ram},
ClientImplementation,
};
Expand Down Expand Up @@ -61,7 +61,7 @@ impl backend::Backend for TestBackend {
) -> Result<Reply, Error> {
match request {
Request::ReadFile(_) => {
let mut data = Message::new();
let mut data = LargeMessage::new();
data.push(0xff).unwrap();
Ok(Reply::ReadFile(ReadFile { data }))
}
Expand Down
2 changes: 1 addition & 1 deletion tests/virt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn run_test(data: u8) {

// ensure that no other client is messing with our filesystem
while syscall!(client.uptime()).uptime < Duration::from_secs(1) {
syscall!(client.write_file(location, path.clone(), write_data.clone(), None));
syscall!(client.write_file(location, path.clone(), write_data.clone()));
let read_data = syscall!(client.read_file(location, path.clone())).data;
assert_eq!(write_data, read_data);
}
Expand Down