forked from trussed-dev/trussed-staging
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request trussed-dev#27 from trussed-dev/fs-info
Add filesystem info extension and backend implementation
- Loading branch information
Showing
6 changed files
with
170 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[email protected]>"] | ||
|
@@ -40,6 +40,7 @@ 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" | ||
|
@@ -53,6 +54,7 @@ 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 = [] | ||
|
@@ -73,3 +75,4 @@ 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" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// 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, ExtensionClient, ExtensionResult}, | ||
types::Location, | ||
Error, | ||
}; | ||
|
||
pub struct FsInfoExtension; | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] | ||
pub enum FsInfoExtensionRequest { | ||
FsInfo(FsInfoRequest), | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] | ||
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, Debug, Clone, PartialEq, Eq)] | ||
pub enum FsInfoExtensionReply { | ||
FsInfo(FsInfoReply), | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] | ||
pub struct FsInfoReply { | ||
pub block_info: Option<BlockInfo>, | ||
pub available_space: usize, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] | ||
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; | ||
} | ||
|
||
pub type FsInfoResult<'a, R, C> = ExtensionResult<'a, FsInfoExtension, R, C>; | ||
|
||
pub trait FsInfoClient: ExtensionClient<FsInfoExtension> { | ||
fn fs_info(&mut self, location: Location) -> FsInfoResult<'_, FsInfoReply, Self> { | ||
self.extension(FsInfoRequest { location }) | ||
} | ||
} | ||
impl<C: ExtensionClient<FsInfoExtension>> FsInfoClient for C {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters