Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helpers for backend stores #119

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
77 changes: 75 additions & 2 deletions src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@
//! Backends can also implement API extensions to provide additional syscalls (see the
//! [`serde_extensions`][`crate::serde_extensions`] module).

use littlefs2::consts::PATH_MAX_PLUS_ONE;

use crate::{
api::{Reply, Request},
error::Error,
error::{Error, Result},
platform::Platform,
service::ServiceResources,
types::{Context, CoreContext},
store::{
certstore::ClientCertstore, counterstore::ClientCounterstore, filestore::ClientFilestore,
keystore::ClientKeystore,
},
types::{Context, CoreContext, PathBuf},
};

/// The ID of a backend.
Expand Down Expand Up @@ -91,3 +97,70 @@ impl TryFrom<u8> for NoId {
Err(Error::InternalError)
}
}

/// Global and per-client resources for a backend.
///
/// This struct provides access to the store implementations for a backend, similar to
/// [`ServiceResources`][]. It adds the backend ID to all paths so that the global stores are
/// located at `backend-<id>/<store>` and the client stores are located at
/// `<client>/backend-<id>/<store>`.
pub struct BackendResources<'a, P: Platform> {
resources: &'a mut ServiceResources<P>,
global_ctx: CoreContext,
}

impl<'a, P: Platform> BackendResources<'a, P> {
pub fn new(resources: &'a mut ServiceResources<P>, id: &str) -> Self {
const PREFIX: &[u8] = "backend-".as_bytes();

let id = id.as_bytes();
let n = PREFIX.len();
let m = id.len();
let mut path = [0; PATH_MAX_PLUS_ONE];
path[..n].copy_from_slice(PREFIX);
path[n..n + m].copy_from_slice(id);

Self {
resources,
global_ctx: CoreContext::new(PathBuf::from(&path[..n + m])),
}
}

pub fn global_certstore(&mut self) -> Result<ClientCertstore<P::S>> {
self.resources.certstore(&self.global_ctx)
}

pub fn global_counterstore(&mut self) -> Result<ClientCounterstore<P::S>> {
self.resources.counterstore(&self.global_ctx)
}

pub fn global_filestore(&mut self) -> ClientFilestore<P::S> {
self.resources.filestore(&self.global_ctx)
}

pub fn global_keystore(&mut self) -> Result<ClientKeystore<P::S>> {
self.resources.keystore(&self.global_ctx)
}

fn client_ctx(&self, ctx: &CoreContext) -> CoreContext {
let mut path = ctx.path.clone();
path.push(&self.global_ctx.path);
CoreContext::new(path)
}

pub fn client_certstore(&mut self, ctx: &CoreContext) -> Result<ClientCertstore<P::S>> {
self.resources.certstore(&self.client_ctx(ctx))
}

pub fn client_counterstore(&mut self, ctx: &CoreContext) -> Result<ClientCounterstore<P::S>> {
self.resources.counterstore(&self.client_ctx(ctx))
}

pub fn client_filestore(&mut self, ctx: &CoreContext) -> ClientFilestore<P::S> {
self.resources.filestore(&self.client_ctx(ctx))
}

pub fn client_keystore(&mut self, ctx: &CoreContext) -> Result<ClientKeystore<P::S>> {
self.resources.keystore(&self.client_ctx(ctx))
}
}
6 changes: 5 additions & 1 deletion src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rand_chacha::ChaCha8Rng;
pub use rand_core::{RngCore, SeedableRng};

use crate::api::*;
use crate::backend::{BackendId, CoreOnly, Dispatch};
use crate::backend::{BackendId, BackendResources, CoreOnly, Dispatch};
use crate::client::{ClientBuilder, ClientImplementation};
use crate::config::*;
use crate::error::{Error, Result};
Expand Down Expand Up @@ -91,6 +91,10 @@ where
unsafe impl<P: Platform, D: Dispatch> Send for Service<P, D> {}

impl<P: Platform> ServiceResources<P> {
pub fn backend<'a>(&'a mut self, id: &str) -> BackendResources<'a, P> {
BackendResources::new(self, id)
}

pub fn certstore(&mut self, ctx: &CoreContext) -> Result<ClientCertstore<P::S>> {
self.rng()
.map(|rng| ClientCertstore::new(ctx.path.clone(), rng, self.platform.store()))
Expand Down