Skip to content

Commit

Permalink
ClientFilestore: make the dat folder optional
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey committed Feb 14, 2024
1 parent 55a5ad6 commit 011c057
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ impl<P: Platform> ServiceResources<P> {
ClientFilestore::new(client_id, self.platform.store())
}

pub fn raw_filestore(&mut self, client_id: PathBuf) -> ClientFilestore<P::S> {
ClientFilestore::without_dat(client_id, self.platform.store())
}

pub fn trussed_filestore(&mut self) -> ClientFilestore<P::S> {
ClientFilestore::new(PathBuf::from("trussed"), self.platform.store())
}
Expand Down
25 changes: 17 additions & 8 deletions src/store/filestore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,30 @@ use littlefs2::{
path::{Path, PathBuf},
};

pub type ClientId = PathBuf;

pub struct ClientFilestore<S>
where
S: Store,
{
client_id: ClientId,
base: PathBuf,
store: S,
}

impl<S: Store> ClientFilestore<S> {
pub fn new(client_id: ClientId, store: S) -> Self {
Self { client_id, store }
/// Create a filestore that stores files in `<client_id>/dat/<file_path>`
pub fn new(client_id: PathBuf, store: S) -> Self {
let mut base = client_id;
base.push(path!("dat"));
Self { base, store }
}

/// Create a filestore that stores files in `<client_id>/<file_path>`
///
/// Unlike [`ClientFilestore::new`](), it does not have the `dat` intermediary.
/// It is meant to be used by custom backends to save space in case the `dat` folder is not used and only wastes a littlefs block.
pub fn without_dat(client_id: PathBuf, store: S) -> Self {
let mut base = client_id;
base.push(path!("dat"));
Self { base, store }
}

/// Client files are store below `/<client_id>/dat/`.
Expand All @@ -51,9 +62,7 @@ impl<S: Store> ClientFilestore<S> {
return Err(Error::InvalidPath);
}

let mut path = PathBuf::new();
path.push(&self.client_id);
path.push(path!("dat"));
let mut path = self.base.clone();
path.push(client_path);
Ok(path)
}
Expand Down

0 comments on commit 011c057

Please sign in to comment.