Skip to content

Commit

Permalink
Use littlefs2-core v0.1.0 instead of littlefs2
Browse files Browse the repository at this point in the history
  • Loading branch information
robin-nitrokey committed Oct 22, 2024
1 parent 52ae72e commit 634de1c
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 153 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ delog = "0.1.6"
hkdf = { version = "0.12", optional = true }
rand_core = { version = "0.6.4", default-features = false }
sha2 = { version = "0.10", default-features = false, optional = true }
littlefs2 = "0.4.0"
littlefs2-core = "0.1"
salty = { version = "0.3.0", default-features = false }
digest = { version = "0.10.7", default-features = false }
hex-literal = { version = "0.4.0", optional = true }
Expand Down Expand Up @@ -81,8 +81,9 @@ log-warn = []
log-error = []

[patch.crates-io]
trussed = { git = "https://github.com/nitrokey/trussed.git", rev = "540ad725ef44f0d6d3d2da7dd6ec0bacffaeb5bf" }
littlefs2 = { git = "https://github.com/trussed-dev/littlefs2.git", rev = "960e57d9fc0d209308c8e15dc26252bbe1ff6ba8" }
trussed = { git = "https://github.com/trussed-dev/trussed.git", branch = "littlefs2" }
littlefs2 = { git = "https://github.com/trussed-dev/littlefs2.git", branch = "release-0.5.0" }
littlefs2-core = { git = "https://github.com/trussed-dev/littlefs2.git", branch = "release-0.5.0" }

trussed-chunked = { path = "extensions/chunked" }
trussed-hkdf = { path = "extensions/hkdf" }
Expand Down
20 changes: 9 additions & 11 deletions src/chunked/store.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (C) Nitrokey GmbH
// SPDX-License-Identifier: Apache-2.0 or MIT

use littlefs2::fs::OpenOptions;
use littlefs2::io::SeekFrom;
use littlefs2::path;
use littlefs2_core::path;
use littlefs2_core::FileOpenFlags;
use littlefs2_core::SeekFrom;

use trussed::store::{create_directories, DynFile, DynFilesystem, Store};
use trussed::types::{Bytes, Location, Message, Path, PathBuf};
Expand All @@ -13,7 +13,7 @@ use serde::{Deserialize, Serialize};

/// Enumeration of possible methods to seek within an file that was just opened
/// Used in the [`read_chunk`](crate::store::read_chunk) and [`write_chunk`](crate::store::write_chunk) calls,
/// Where [`SeekFrom::Current`](littlefs2::io::SeekFrom::Current) would not make sense.
/// Where [`SeekFrom::Current`](littlefs2_core::SeekFrom::Current) would not make sense.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum OpenSeekFrom {
Start(u32),
Expand Down Expand Up @@ -69,8 +69,8 @@ pub fn fs_write_chunk(
contents: &[u8],
pos: OpenSeekFrom,
) -> Result<(), Error> {
fs.open_file_with_options_and_then(
&|options| options.read(true).write(true),
fs.open_file_with_flags_and_then(
FileOpenFlags::READ | FileOpenFlags::WRITE,
path,
&mut |file| {
file.seek(pos.into())?;
Expand Down Expand Up @@ -129,8 +129,6 @@ pub fn move_file(
store
.fs(from_location)
.open_file_and_then(from_path, &mut |from_file| {
let mut options = OpenOptions::new();
options.write(true).create(true).truncate(true);
store
.fs(to_location)
.create_file_and_then(to_path, &mut |to_file| copy_file_data(from_file, to_file))
Expand All @@ -141,7 +139,7 @@ pub fn move_file(
})
}

fn copy_file_data(from: &dyn DynFile, to: &dyn DynFile) -> Result<(), littlefs2::io::Error> {
fn copy_file_data(from: &dyn DynFile, to: &dyn DynFile) -> Result<(), littlefs2_core::Error> {
let mut buf = [0; 1024];
loop {
let read = from.read(&mut buf)?;
Expand Down Expand Up @@ -270,8 +268,8 @@ pub fn append_file(
let path = actual_path(client_id, path)?;
store
.fs(location)
.open_file_with_options_and_then(
&|options| options.write(true).append(true),
.open_file_with_flags_and_then(
FileOpenFlags::WRITE | FileOpenFlags::APPEND,
&path,
&mut |file| {
file.write_all(data)?;
Expand Down
2 changes: 1 addition & 1 deletion src/manage.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (C) Nitrokey GmbH
// SPDX-License-Identifier: Apache-2.0 or MIT

use littlefs2::{fs::DirEntry, path, path::Path};
use littlefs2_core::{path, DirEntry, Path};
use trussed::{
serde_extensions::{Extension, ExtensionImpl},
store::Store,
Expand Down
71 changes: 41 additions & 30 deletions tests/chunked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

#![cfg(all(feature = "virt", feature = "chunked"))]

use littlefs2::path::PathBuf;
use littlefs2_core::{path, PathBuf};
use trussed::{client::FilesystemClient, syscall, try_syscall, types::Location, Bytes};
use trussed_chunked::{utils, ChunkedClient};
use trussed_staging::virt::with_ram_client;

fn test_write_all(location: Location) {
with_ram_client("test chunked", |mut client| {
let path = PathBuf::from("foo");
let path = PathBuf::from(path!("foo"));
utils::write_all(&mut client, location, path.clone(), &[48; 1234], None, None).unwrap();

let data = syscall!(client.start_chunked_read(location, path)).data;
Expand All @@ -22,7 +22,7 @@ fn test_write_all(location: Location) {

fn test_write_all_small(location: Location) {
with_ram_client("test chunked", |mut client| {
let path = PathBuf::from("foo2");
let path = PathBuf::from(path!("foo2"));
utils::write_all(&mut client, location, path.clone(), &[48; 1023], None, None).unwrap();

let data = syscall!(client.start_chunked_read(location, path)).data;
Expand Down Expand Up @@ -51,27 +51,28 @@ fn write_all_internal() {
#[test]
fn filesystem() {
with_ram_client("chunked-tests", |mut client| {
assert!(
syscall!(client.entry_metadata(Location::Internal, PathBuf::from("test_file")))
.metadata
.is_none(),
);
assert!(syscall!(
client.entry_metadata(Location::Internal, PathBuf::from(path!("test_file")))
)
.metadata
.is_none(),);

let data = Bytes::from_slice(b"test data").unwrap();
syscall!(client.write_file(
Location::Internal,
PathBuf::from("test_file"),
PathBuf::from(path!("test_file")),
data.clone(),
None,
));

let recv_data =
syscall!(client.read_file(Location::Internal, PathBuf::from("test_file"))).data;
syscall!(client.read_file(Location::Internal, PathBuf::from(path!("test_file")))).data;
assert_eq!(data, recv_data);

// ======== CHUNKED READS ========
let first_data =
syscall!(client.start_chunked_read(Location::Internal, PathBuf::from("test_file"),));
let first_data = syscall!(
client.start_chunked_read(Location::Internal, PathBuf::from(path!("test_file")))
);
assert_eq!(&first_data.data, &data);
assert_eq!(first_data.len, data.len());

Expand All @@ -83,16 +84,21 @@ fn filesystem() {
let large_data2 = Bytes::from_slice(&[1; 1024]).unwrap();
let more_data = Bytes::from_slice(&[2; 42]).unwrap();
// ======== CHUNKED WRITES ========
syscall!(client.start_chunked_write(Location::Internal, PathBuf::from("test_file"), None));
syscall!(client.start_chunked_write(
Location::Internal,
PathBuf::from(path!("test_file")),
None
));

syscall!(client.write_file_chunk(large_data.clone()));
syscall!(client.write_file_chunk(large_data2.clone()));
syscall!(client.write_file_chunk(more_data.clone()));

// ======== CHUNKED READS ========
let full_len = large_data.len() + large_data2.len() + more_data.len();
let first_data =
syscall!(client.start_chunked_read(Location::Internal, PathBuf::from("test_file"),));
let first_data = syscall!(
client.start_chunked_read(Location::Internal, PathBuf::from(path!("test_file")))
);
assert_eq!(&first_data.data, &large_data);
assert_eq!(first_data.len, full_len);

Expand All @@ -109,40 +115,45 @@ fn filesystem() {
assert_eq!(empty_data.len, full_len);

let metadata =
syscall!(client.entry_metadata(Location::Internal, PathBuf::from("test_file")))
syscall!(client.entry_metadata(Location::Internal, PathBuf::from(path!("test_file"))))
.metadata
.unwrap();
assert!(metadata.is_file());

// ======== ABORTED CHUNKED WRITES ========
syscall!(client.start_chunked_write(Location::Internal, PathBuf::from("test_file"), None));
syscall!(client.start_chunked_write(
Location::Internal,
PathBuf::from(path!("test_file")),
None
));

syscall!(client.write_file_chunk(large_data.clone()));
syscall!(client.write_file_chunk(large_data2));
syscall!(client.abort_chunked_write());

// Old data is still there after abort
let partial_data =
syscall!(client.start_chunked_read(Location::Internal, PathBuf::from("test_file")));
let partial_data = syscall!(
client.start_chunked_read(Location::Internal, PathBuf::from(path!("test_file")))
);
assert_eq!(&partial_data.data, &large_data);
assert_eq!(partial_data.len, full_len);

// This returns an error because the name doesn't exist
assert!(
try_syscall!(client.remove_file(Location::Internal, PathBuf::from("bad_name")))
.is_err()
);
assert!(try_syscall!(
client.remove_file(Location::Internal, PathBuf::from(path!("bad_name")))
)
.is_err());
let metadata =
syscall!(client.entry_metadata(Location::Internal, PathBuf::from("test_file")))
syscall!(client.entry_metadata(Location::Internal, PathBuf::from(path!("test_file"))))
.metadata
.unwrap();
assert!(metadata.is_file());

syscall!(client.remove_file(Location::Internal, PathBuf::from("test_file")));
assert!(
syscall!(client.entry_metadata(Location::Internal, PathBuf::from("test_file")))
.metadata
.is_none(),
);
syscall!(client.remove_file(Location::Internal, PathBuf::from(path!("test_file"))));
assert!(syscall!(
client.entry_metadata(Location::Internal, PathBuf::from(path!("test_file")))
)
.metadata
.is_none(),);
})
}
51 changes: 21 additions & 30 deletions tests/encrypted-chunked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#![cfg(all(feature = "virt", feature = "chunked"))]

use littlefs2::path::PathBuf;
use littlefs2_core::{path, PathBuf};
use serde_byte_array::ByteArray;
use trussed::{
client::CryptoClient, client::FilesystemClient, syscall, try_syscall, types::Location, Bytes,
Expand All @@ -18,7 +18,7 @@ use trussed_staging::virt::with_ram_client;
fn test_write_all(location: Location) {
with_ram_client("test chunked", |mut client| {
let key = syscall!(client.generate_secret_key(32, Location::Volatile)).key;
let path = PathBuf::from("foo");
let path = PathBuf::from(path!("foo"));
utils::write_all(
&mut client,
location,
Expand All @@ -40,7 +40,7 @@ fn test_write_all(location: Location) {
fn test_write_all_small(location: Location) {
with_ram_client("test chunked", |mut client| {
let key = syscall!(client.generate_secret_key(32, Location::Volatile)).key;
let path = PathBuf::from("foo2");
let path = PathBuf::from(path!("foo2"));
utils::write_all(
&mut client,
location,
Expand Down Expand Up @@ -78,10 +78,11 @@ fn write_all_internal() {
#[test]
fn encrypted_filesystem() {
with_ram_client("chunked-tests", |mut client| {
let path = PathBuf::from(path!("test_file"));
let key = syscall!(client.generate_secret_key(32, Location::Volatile)).key;

assert!(
syscall!(client.entry_metadata(Location::Internal, PathBuf::from("test_file")))
syscall!(client.entry_metadata(Location::Internal, path.clone()))
.metadata
.is_none(),
);
Expand All @@ -92,7 +93,7 @@ fn encrypted_filesystem() {
// ======== CHUNKED WRITES ========
syscall!(client.start_encrypted_chunked_write(
Location::Internal,
PathBuf::from("test_file"),
path.clone(),
key,
Some(ByteArray::from([0; 8])),
None
Expand All @@ -104,11 +105,7 @@ fn encrypted_filesystem() {

// ======== CHUNKED READS ========
let full_len = large_data.len() + large_data2.len() + more_data.len();
syscall!(client.start_encrypted_chunked_read(
Location::Internal,
PathBuf::from("test_file"),
key
));
syscall!(client.start_encrypted_chunked_read(Location::Internal, path.clone(), key));
let first_data = syscall!(client.read_file_chunk());
assert_eq!(&first_data.data, &large_data);
assert_eq!(first_data.len, full_len);
Expand All @@ -126,16 +123,15 @@ fn encrypted_filesystem() {
Err(Error::MechanismNotAvailable)
);

let metadata =
syscall!(client.entry_metadata(Location::Internal, PathBuf::from("test_file")))
.metadata
.unwrap();
let metadata = syscall!(client.entry_metadata(Location::Internal, path.clone()))
.metadata
.unwrap();
assert!(metadata.is_file());

// ======== ABORTED CHUNKED WRITES ========
syscall!(client.start_encrypted_chunked_write(
Location::Internal,
PathBuf::from("test_file"),
path.clone(),
key,
Some(ByteArray::from([1; 8])),
None
Expand All @@ -146,11 +142,7 @@ fn encrypted_filesystem() {
syscall!(client.abort_chunked_write());

// Old data is still there after abort
syscall!(client.start_encrypted_chunked_read(
Location::Internal,
PathBuf::from("test_file"),
key
));
syscall!(client.start_encrypted_chunked_read(Location::Internal, path.clone(), key));
let first_data = syscall!(client.read_file_chunk());
assert_eq!(&first_data.data, &large_data);
assert_eq!(first_data.len, full_len);
Expand All @@ -169,19 +161,18 @@ fn encrypted_filesystem() {
);

// This returns an error because the name doesn't exist
assert!(
try_syscall!(client.remove_file(Location::Internal, PathBuf::from("bad_name")))
.is_err()
);
let metadata =
syscall!(client.entry_metadata(Location::Internal, PathBuf::from("test_file")))
.metadata
.unwrap();
assert!(try_syscall!(
client.remove_file(Location::Internal, PathBuf::from(path!("bad_name")))
)
.is_err());
let metadata = syscall!(client.entry_metadata(Location::Internal, path.clone()))
.metadata
.unwrap();
assert!(metadata.is_file());

syscall!(client.remove_file(Location::Internal, PathBuf::from("test_file")));
syscall!(client.remove_file(Location::Internal, path.clone()));
assert!(
syscall!(client.entry_metadata(Location::Internal, PathBuf::from("test_file")))
syscall!(client.entry_metadata(Location::Internal, path.clone()))
.metadata
.is_none(),
);
Expand Down
2 changes: 1 addition & 1 deletion tests/hpke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#![cfg(all(feature = "virt", feature = "hpke"))]

use littlefs2::path;
use littlefs2_core::path;
use trussed::client::{CryptoClient, X255};
use trussed::{
syscall,
Expand Down
Loading

0 comments on commit 634de1c

Please sign in to comment.