Skip to content

Commit

Permalink
Merge branch 'main' into merge-upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey committed Jun 8, 2023
2 parents 686aa70 + 676ed80 commit 1926f08
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 60 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
`ui::Status` non-exhaustive.
- Made `postcard_deserialize`, `postcard_serialize` and
`postcard_serialize_bytes` private.
- Changed `&PathBuf` to `&Path` where possible.

### Fixed

Expand Down
35 changes: 19 additions & 16 deletions src/service.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use littlefs2::path::PathBuf;
use littlefs2::{
path,
path::{Path, PathBuf},
};
use rand_chacha::ChaCha8Rng;
pub use rand_core::{RngCore, SeedableRng};

Expand Down Expand Up @@ -297,7 +300,7 @@ impl<P: Platform> ServiceResources<P> {
},

Request::LocateFile(request) => {
let path = filestore.locate_file(request.location, request.dir.clone(), request.filename.clone())?;
let path = filestore.locate_file(request.location, request.dir.as_deref(), &request.filename)?;

Ok(Reply::LocateFile(reply::LocateFile { path }) )
}
Expand All @@ -307,14 +310,14 @@ impl<P: Platform> ServiceResources<P> {
Request::DebugDumpStore(_request) => {

info_now!(":: PERSISTENT");
recursively_list(self.platform.store().ifs(), PathBuf::from("/"));
recursively_list(self.platform.store().ifs(), path!("/"));

info_now!(":: VOLATILE");
recursively_list(self.platform.store().vfs(), PathBuf::from("/"));
recursively_list(self.platform.store().vfs(), path!("/"));

fn recursively_list<S: 'static + crate::types::LfsStorage>(fs: &'static crate::store::Fs<S>, path: PathBuf) {
fn recursively_list<S: 'static + crate::types::LfsStorage>(fs: &'static crate::store::Fs<S>, path: &Path) {
// let fs = store.vfs();
fs.read_dir_and_then(&path, |dir| {
fs.read_dir_and_then(path, |dir| {
for (i, entry) in dir.enumerate() {
let entry = entry.unwrap();
if i < 2 {
Expand All @@ -323,7 +326,7 @@ impl<P: Platform> ServiceResources<P> {
}
info_now!("{:?} p({:?})", entry.path(), &path);
if entry.file_type().is_dir() {
recursively_list(fs, PathBuf::from(entry.path()));
recursively_list(fs, entry.path());
}
if entry.file_type().is_file() {
let _contents: Vec<u8, 256> = fs.read(entry.path()).unwrap();
Expand All @@ -340,7 +343,7 @@ impl<P: Platform> ServiceResources<P> {
}

Request::ReadDirFirst(request) => {
let maybe_entry = match filestore.read_dir_first(&request.dir, request.location, request.not_before_filename.as_ref())? {
let maybe_entry = match filestore.read_dir_first(&request.dir, request.location, request.not_before_filename.as_deref())? {
Some((entry, read_dir_state)) => {
ctx.read_dir_state = Some(read_dir_state);
Some(entry)
Expand Down Expand Up @@ -620,15 +623,15 @@ impl<P: Platform> ServiceResources<P> {
None => {
let mut filestore = self.trussed_filestore();

let path = PathBuf::from("rng-state.bin");
let path = path!("rng-state.bin");

// Load previous seed, e.g., externally injected entropy on first run.
// Else, default to zeros - will mix in new HW RNG entropy next
let mixin_seed = if !filestore.exists(&path, Location::Internal) {
let mixin_seed = if !filestore.exists(path, Location::Internal) {
[0u8; 32]
} else {
// Use the last saved state.
let mixin_bytes: Bytes<32> = filestore.read(&path, Location::Internal)?;
let mixin_bytes: Bytes<32> = filestore.read(path, Location::Internal)?;
let mut mixin_seed = [0u8; 32];
mixin_seed.clone_from_slice(&mixin_bytes);
mixin_seed
Expand Down Expand Up @@ -668,7 +671,7 @@ impl<P: Platform> ServiceResources<P> {
let mut seed_to_store = [0u8; 32];
rng.fill_bytes(&mut seed_to_store);
filestore
.write(&path, Location::Internal, seed_to_store.as_ref())
.write(path, Location::Internal, seed_to_store.as_ref())
.unwrap();

// 5. Finish
Expand Down Expand Up @@ -750,7 +753,7 @@ impl<P: Platform, D: Dispatch> Service<P, D> {
backends: &'static [BackendId<D::BackendId>],
) -> Result<(), Error> {
let core_ctx = core_ctx.into();
if core_ctx.path == PathBuf::from("trussed") {
if &*core_ctx.path == path!("trussed") {
panic!("trussed is a reserved client ID");
}
self.eps
Expand All @@ -764,10 +767,10 @@ impl<P: Platform, D: Dispatch> Service<P, D> {

pub fn set_seed_if_uninitialized(&mut self, seed: &[u8; 32]) {
let mut filestore = self.resources.trussed_filestore();
let path = PathBuf::from("rng-state.bin");
if !filestore.exists(&path, Location::Internal) {
let path = path!("rng-state.bin");
if !filestore.exists(path, Location::Internal) {
filestore
.write(&path, Location::Internal, seed.as_ref())
.write(path, Location::Internal, seed.as_ref())
.unwrap();
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/store/certstore.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use littlefs2::path::PathBuf;
use littlefs2::{
path,
path::{Path, PathBuf},
};
use rand_chacha::ChaCha8Rng;

use crate::{
Expand Down Expand Up @@ -65,7 +68,7 @@ impl<S: Store> ClientCertstore<S> {
fn cert_path(&self, id: CertId) -> PathBuf {
let mut path = PathBuf::new();
path.push(&self.client_id);
path.push(&PathBuf::from("x5c"));
path.push(path!("x5c"));
path.push(&PathBuf::from(id.hex().as_slice()));
path
}
Expand Down
7 changes: 5 additions & 2 deletions src/store/counterstore.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use littlefs2::path::PathBuf;
use littlefs2::{
path,
path::{Path, PathBuf},
};
use rand_chacha::ChaCha8Rng;

use crate::{
Expand Down Expand Up @@ -30,7 +33,7 @@ impl<S: Store> ClientCounterstore<S> {
fn counter_path(&self, id: CounterId) -> PathBuf {
let mut path = PathBuf::new();
path.push(&self.client_id);
path.push(&PathBuf::from("ctr"));
path.push(path!("ctr"));
path.push(&PathBuf::from(id.hex().as_slice()));
path
}
Expand Down
88 changes: 53 additions & 35 deletions src/store/filestore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
types::{LfsStorage, Location, Message, UserAttribute},
Bytes,
};
use littlefs2::path;

#[derive(Clone)]
pub struct ReadDirState {
Expand Down Expand Up @@ -43,15 +44,15 @@ impl<S: Store> ClientFilestore<S> {
}

/// Client files are store below `/<client_id>/dat/`.
pub fn actual_path(&self, client_path: &PathBuf) -> Result<PathBuf> {
pub fn actual_path(&self, client_path: &Path) -> Result<PathBuf> {
// Clients must not escape their namespace
if client_path.as_ref().contains("..") {
return Err(Error::InvalidPath);
}

let mut path = PathBuf::new();
path.push(&self.client_id);
path.push(&PathBuf::from("dat"));
path.push(path!("dat"));
path.push(client_path);
Ok(path)
}
Expand All @@ -75,18 +76,24 @@ impl<S: Store> ClientFilestore<S> {
}

pub trait Filestore {
fn read<const N: usize>(&mut self, path: &PathBuf, location: Location) -> Result<Bytes<N>>;
fn write(&mut self, path: &PathBuf, location: Location, data: &[u8]) -> Result<()>;
fn exists(&mut self, path: &PathBuf, location: Location) -> bool;
fn metadata(&mut self, path: &PathBuf, location: Location) -> Result<Option<Metadata>>;
fn remove_file(&mut self, path: &PathBuf, location: Location) -> Result<()>;
fn remove_dir(&mut self, path: &PathBuf, location: Location) -> Result<()>;
fn remove_dir_all(&mut self, path: &PathBuf, location: Location) -> Result<usize>;
fn read<const N: usize>(&mut self, path: &Path, location: Location) -> Result<Bytes<N>>;
fn write(&mut self, path: &Path, location: Location, data: &[u8]) -> Result<()>;
fn exists(&mut self, path: &Path, location: Location) -> bool;
fn metadata(&mut self, path: &Path, location: Location) -> Result<Option<Metadata>>;
fn remove_file(&mut self, path: &Path, location: Location) -> Result<()>;
fn remove_dir(&mut self, path: &Path, location: Location) -> Result<()>;
fn remove_dir_all(&mut self, path: &Path, location: Location) -> Result<usize>;
fn remove_dir_all_where(
&mut self,
path: &Path,
location: Location,
predicate: impl Fn(&DirEntry) -> bool,
) -> Result<usize>;
fn locate_file(
&mut self,
location: Location,
underneath: Option<PathBuf>,
filename: PathBuf,
underneath: Option<&Path>,
filename: &Path,
) -> Result<Option<PathBuf>>;

/// Iterate over entries of a directory (both file and directory entries).
Expand All @@ -100,9 +107,9 @@ pub trait Filestore {
/// call to `read_dir_next` can resume operation.
fn read_dir_first(
&mut self,
dir: &PathBuf,
dir: &Path,
location: Location,
not_before: Option<&PathBuf>,
not_before: Option<&Path>,
) -> Result<Option<(DirEntry, ReadDirState)>>;

/// Continue iterating over entries of a directory.
Expand All @@ -120,7 +127,7 @@ pub trait Filestore {
/// Additionally, files may optionally be filtered via attributes.
fn read_dir_files_first(
&mut self,
clients_dir: &PathBuf,
clients_dir: &Path,
location: Location,
user_attribute: Option<UserAttribute>,
) -> Result<Option<(Option<Message>, ReadDirFilesState)>>;
Expand All @@ -136,9 +143,9 @@ pub trait Filestore {
impl<S: Store> ClientFilestore<S> {
fn read_dir_first_impl<F: LfsStorage + 'static>(
&mut self,
clients_dir: &PathBuf,
clients_dir: &Path,
location: Location,
not_before: Option<&PathBuf>,
not_before: Option<&Path>,
fs: &'static Fs<F>,
) -> Result<Option<(DirEntry, ReadDirState)>> {
let dir = self.actual_path(clients_dir)?;
Expand Down Expand Up @@ -228,7 +235,7 @@ impl<S: Store> ClientFilestore<S> {
}
fn read_dir_files_first_impl<F: LfsStorage + 'static>(
&mut self,
clients_dir: &PathBuf,
clients_dir: &Path,
location: Location,
user_attribute: Option<UserAttribute>,
fs: &'static Fs<F>,
Expand Down Expand Up @@ -346,30 +353,30 @@ impl<S: Store> ClientFilestore<S> {
}

impl<S: Store> Filestore for ClientFilestore<S> {
fn read<const N: usize>(&mut self, path: &PathBuf, location: Location) -> Result<Bytes<N>> {
fn read<const N: usize>(&mut self, path: &Path, location: Location) -> Result<Bytes<N>> {
let path = self.actual_path(path)?;

store::read(self.store, location, &path)
}

fn write(&mut self, path: &PathBuf, location: Location, data: &[u8]) -> Result<()> {
fn write(&mut self, path: &Path, location: Location, data: &[u8]) -> Result<()> {
let path = self.actual_path(path)?;
store::store(self.store, location, &path, data)
}

fn exists(&mut self, path: &PathBuf, location: Location) -> bool {
fn exists(&mut self, path: &Path, location: Location) -> bool {
if let Ok(path) = self.actual_path(path) {
store::exists(self.store, location, &path)
} else {
false
}
}
fn metadata(&mut self, path: &PathBuf, location: Location) -> Result<Option<Metadata>> {
fn metadata(&mut self, path: &Path, location: Location) -> Result<Option<Metadata>> {
let path = self.actual_path(path)?;
store::metadata(self.store, location, &path)
}

fn remove_file(&mut self, path: &PathBuf, location: Location) -> Result<()> {
fn remove_file(&mut self, path: &Path, location: Location) -> Result<()> {
let path = self.actual_path(path)?;

match store::delete(self.store, location, &path) {
Expand All @@ -378,7 +385,7 @@ impl<S: Store> Filestore for ClientFilestore<S> {
}
}

fn remove_dir(&mut self, path: &PathBuf, location: Location) -> Result<()> {
fn remove_dir(&mut self, path: &Path, location: Location) -> Result<()> {
let path = self.actual_path(path)?;

match store::delete(self.store, location, &path) {
Expand All @@ -387,18 +394,29 @@ impl<S: Store> Filestore for ClientFilestore<S> {
}
}

fn remove_dir_all(&mut self, path: &PathBuf, location: Location) -> Result<usize> {
fn remove_dir_all(&mut self, path: &Path, location: Location) -> Result<usize> {
let path = self.actual_path(path)?;

store::remove_dir_all_where(self.store, location, &path, |_| true)
.map_err(|_| Error::InternalError)
}
fn remove_dir_all_where(
&mut self,
path: &Path,
location: Location,
predicate: impl Fn(&DirEntry) -> bool,
) -> Result<usize> {
let path = self.actual_path(path)?;

store::remove_dir_all_where(self.store, location, &path, predicate)
.map_err(|_| Error::InternalError)
}

fn read_dir_first(
&mut self,
clients_dir: &PathBuf,
clients_dir: &Path,
location: Location,
not_before: Option<&PathBuf>,
not_before: Option<&Path>,
) -> Result<Option<(DirEntry, ReadDirState)>> {
match location {
Location::Internal => {
Expand All @@ -423,7 +441,7 @@ impl<S: Store> Filestore for ClientFilestore<S> {

fn read_dir_files_first(
&mut self,
clients_dir: &PathBuf,
clients_dir: &Path,
location: Location,
user_attribute: Option<UserAttribute>,
) -> Result<Option<(Option<Message>, ReadDirFilesState)>> {
Expand Down Expand Up @@ -463,25 +481,25 @@ impl<S: Store> Filestore for ClientFilestore<S> {
fn locate_file(
&mut self,
location: Location,
underneath: Option<PathBuf>,
filename: PathBuf,
underneath: Option<&Path>,
filename: &Path,
) -> Result<Option<PathBuf>> {
if location != Location::Internal {
return Err(Error::RequestNotAvailable);
}

let clients_dir = underneath.unwrap_or_else(|| PathBuf::from("/"));
let dir = self.actual_path(&clients_dir)?;
let clients_dir = underneath.unwrap_or_else(|| path!("/"));
let dir = self.actual_path(clients_dir)?;
let fs = self.store.ifs();

info_now!("base dir {:?}", &dir);

fn recursively_locate<S: 'static + crate::types::LfsStorage>(
fs: &'static crate::store::Fs<S>,
dir: PathBuf,
dir: &Path,
filename: &Path,
) -> Option<PathBuf> {
fs.read_dir_and_then(&dir, |it| {
fs.read_dir_and_then(dir, |it| {
it.map(|entry| entry.unwrap())
.skip(2)
.filter_map(|entry| {
Expand All @@ -493,7 +511,7 @@ impl<S: Store> Filestore for ClientFilestore<S> {
None
}
} else {
recursively_locate(fs, PathBuf::from(entry.path()), filename)
recursively_locate(fs, entry.path(), filename)
}
})
.next()
Expand All @@ -502,7 +520,7 @@ impl<S: Store> Filestore for ClientFilestore<S> {
.ok()
}

let path = recursively_locate(fs, dir, &filename).map(|path| self.client_path(&path));
let path = recursively_locate(fs, &dir, filename).map(|path| self.client_path(&path));

Ok(path)
}
Expand Down
Loading

0 comments on commit 1926f08

Please sign in to comment.