Skip to content

Commit

Permalink
Add filesystem info extension and backend implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey committed Jul 24, 2024
1 parent e016b25 commit b167a68
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 4 deletions.
11 changes: 7 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: CC0-1.0

[workspace]
members = ["extensions/chunked", "extensions/hkdf", "extensions/manage", "extensions/wrap-key-to-file"]
members = ["extensions/chunked", "extensions/fs-info", "extensions/hkdf", "extensions/manage", "extensions/wrap-key-to-file"]

[workspace.package]
authors = ["Nitrokey GmbH <info@nitrokey.com>"]
Expand Down Expand Up @@ -40,19 +40,21 @@ trussed-chunked = { version = "0.1.0", optional = true }
trussed-hkdf = { version = "0.2.0", optional = true }
trussed-manage = { version = "0.1.0", optional = true }
trussed-wrap-key-to-file = { version = "0.1.0", optional = true }
trussed-fs-info = { version = "0.1.0", optional = true }

[dev-dependencies]
hex-literal = "0.3.4"
hmac = "0.12.0"
trussed = { workspace = true, features = ["virt"] }

[features]
default = []
default = ["fs-info"]

chunked = ["trussed-chunked", "chacha20poly1305/stream"]
hkdf = ["trussed-hkdf", "dep:hkdf", "dep:sha2"]
manage = ["trussed-manage"]
wrap-key-to-file = ["chacha20poly1305", "trussed-wrap-key-to-file"]
fs-info = ["trussed-fs-info"]

virt = ["std", "trussed/virt"]
std = []
Expand All @@ -66,10 +68,11 @@ log-warn = []
log-error = []

[patch.crates-io]
trussed = { git = "https://github.com/trussed-dev/trussed.git", rev = "45ed62ba97d994aa6e05e2b61cea013ef131caa4" }
littlefs2 = { git = "https://github.com/trussed-dev/littlefs2.git", rev = "ebd27e49ca321089d01d8c9b169c4aeb58ceeeca" }
trussed = { git = "https://github.com/trussed-dev/trussed.git", rev = "a055e4f79a10122c8c0c882161442e6e02f0c5c6" }
littlefs2 = { git = "https://github.com/trussed-dev/littlefs2.git", rev = "960e57d9fc0d209308c8e15dc26252bbe1ff6ba8" }

trussed-chunked = { path = "extensions/chunked" }
trussed-hkdf = { path = "extensions/hkdf" }
trussed-manage = { path = "extensions/manage" }
trussed-wrap-key-to-file = { path = "extensions/wrap-key-to-file" }
trussed-fs-info= { path = "extensions/fs-info" }
15 changes: 15 additions & 0 deletions extensions/fs-info/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright (C) Nitrokey GmbH
# SPDX-License-Identifier: CC0-1.0

[package]
name = "trussed-fs-info"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
repository.workspace = true
license.workspace = true

[dependencies]
serde.workspace = true
serde-byte-array.workspace = true
trussed.workspace = true
76 changes: 76 additions & 0 deletions extensions/fs-info/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (C) Nitrokey GmbH
// SPDX-License-Identifier: CC0-1.0

#![no_std]
#![warn(non_ascii_idents, trivial_casts, unused, unused_qualifications)]
#![deny(unsafe_code)]

use serde::{Deserialize, Serialize};
use trussed::{serde_extensions::Extension, types::Location, Error};

pub struct FsInfoExtension;

#[derive(Serialize, Deserialize)]
pub enum FsInfoExtensionRequest {
FsInfo(FsInfoRequest),
}

#[derive(Serialize, Deserialize)]
pub struct FsInfoRequest {
pub location: Location,
}

impl From<FsInfoRequest> for FsInfoExtensionRequest {
fn from(value: FsInfoRequest) -> Self {
Self::FsInfo(value)
}
}

impl TryFrom<FsInfoExtensionRequest> for FsInfoRequest {
type Error = Error;

fn try_from(value: FsInfoExtensionRequest) -> Result<Self, Self::Error> {
match value {
FsInfoExtensionRequest::FsInfo(v) => Ok(v),
}
}
}

#[derive(Serialize, Deserialize)]
pub enum FsInfoExtensionReply {
FsInfo(FsInfoReply),
}

#[derive(Serialize, Deserialize)]
pub struct FsInfoReply {
pub block_info: Option<BlockInfo>,
pub available_space: usize,
}

#[derive(Serialize, Deserialize)]
pub struct BlockInfo {
pub size: usize,
pub total: usize,
pub available: usize,
}

impl From<FsInfoReply> for FsInfoExtensionReply {
fn from(value: FsInfoReply) -> Self {
Self::FsInfo(value)
}
}

impl TryFrom<FsInfoExtensionReply> for FsInfoReply {
type Error = Error;

fn try_from(value: FsInfoExtensionReply) -> Result<Self, Self::Error> {
match value {
FsInfoExtensionReply::FsInfo(v) => Ok(v),
}
}
}

impl Extension for FsInfoExtension {
type Request = FsInfoExtensionRequest;
type Reply = FsInfoExtensionReply;
}
37 changes: 37 additions & 0 deletions src/fs_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (C) Nitrokey GmbH
// SPDX-License-Identifier: CC0-1.0

use trussed::{
platform::Store, serde_extensions::ExtensionImpl, service::ServiceResources,
types::CoreContext, Error, Platform,
};
use trussed_fs_info::{
BlockInfo, FsInfoExtension, FsInfoExtensionReply, FsInfoExtensionRequest, FsInfoReply,
};

impl ExtensionImpl<FsInfoExtension> for super::StagingBackend {
fn extension_request<P: Platform>(
&mut self,
_core_ctx: &mut CoreContext,
_backend_ctx: &mut Self::Context,
request: &FsInfoExtensionRequest,
resources: &mut ServiceResources<P>,
) -> Result<FsInfoExtensionReply, Error> {
match request {
FsInfoExtensionRequest::FsInfo(req) => {
let platform = resources.platform();
let store = platform.store();
let fs = store.fs(req.location);
Ok(FsInfoReply {
block_info: Some(BlockInfo {
total: fs.total_blocks(),
available: fs.available_blocks().map_err(|_| Error::InternalError)?,
size: fs.total_space() / fs.total_blocks(),
}),
available_space: fs.available_space().map_err(|_| Error::InternalError)?,
}
.into())
}
}
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub mod virt;
#[cfg(feature = "wrap-key-to-file")]
mod wrap_key_to_file;

#[cfg(feature = "fs-info")]
mod fs_info;

#[cfg(feature = "chunked")]
mod chunked;

Expand Down

0 comments on commit b167a68

Please sign in to comment.