From fffbe7761464fd6ef060d10c2fe30e167124d390 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Tue, 4 Apr 2023 14:26:21 +0200 Subject: [PATCH] Use trussed's util for writing large files --- Cargo.toml | 2 +- src/state.rs | 56 +++++----------------------------------------------- 2 files changed, 6 insertions(+), 52 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 22605c1..986595d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -74,7 +74,7 @@ log-warn = [] log-error = [] [patch.crates-io] -trussed = { git = "https://github.com/sosthene-nitrokey/trussed", rev = "3eeda2a21106cb31120e08f5ae67a490b3d47bd6" } +trussed = { git = "https://github.com/sosthene-nitrokey/trussed", rev = "fa26fa984008276909d426b9d902f5fa05c36f1e" } trussed-auth = { git = "https://github.com/trussed-dev/trussed-auth", tag = "v0.2.1"} littlefs2 = { git = "https://github.com/Nitrokey/littlefs2", tag = "v0.3.2-nitrokey-2" } diff --git a/src/state.rs b/src/state.rs index 5c6c58c..0009a3f 100644 --- a/src/state.rs +++ b/src/state.rs @@ -14,6 +14,7 @@ use trussed::{ config::MAX_MESSAGE_LENGTH, syscall, try_syscall, types::{KeyId, KeySerialization, Location, Mechanism, PathBuf, StorageAttributes}, + utils, }; use trussed_auth::AuthClient; @@ -773,62 +774,15 @@ impl ContainerStorage { } } - fn save_inner( - &self, - client: &mut impl trussed::Client, - bytes: &[u8], - storage: Location, - ) -> Result<(), Status> { - let mut msg = Bytes::new(); - let chunk_size = msg.capacity(); - let mut chunks = bytes.chunks(chunk_size).map(|chunk| { - Bytes::from( - heapless::Vec::try_from(chunk) - .expect("Iteration over chunks yields maximum of chunk_size"), - ) - }); - msg = chunks.next().unwrap_or_default(); - let mut written = msg.len(); - try_syscall!(client.start_chunked_write(storage, self.path(), msg, None)).map_err( - |_err| { - error!("Failed to store data: {_err:?}"); - Status::UnspecifiedNonpersistentExecutionError - }, - )?; - for chunk in chunks { - let off = written; - written += chunk.len(); - try_syscall!(client.write_file_chunk( - storage, - self.path(), - chunk, - OpenSeekFrom::Start(off as u32) - )) - .map_err(|_err| { - error!("Failed to store data: {_err:?}"); - Status::UnspecifiedNonpersistentExecutionError - })?; - } - Ok(()) - } - pub fn save( self, client: &mut impl trussed::Client, bytes: &[u8], storage: Location, ) -> Result<(), Status> { - let res = self.save_inner(client, bytes, storage); - if res.is_ok() { - try_syscall!(client.flush_chunks(storage, self.path())) - .map(drop) - .map_err(|_err| { - error!("Failed to flush data: {_err:?}"); - Status::UnspecifiedNonpersistentExecutionError - }) - } else { - syscall!(client.abort_chunked_write(storage, self.path())); - res - } + utils::write_all(client, storage, self.path(), bytes, None).map_err(|_err| { + error!("Failed to write data object: {:?}", _err); + Status::UnspecifiedNonpersistentExecutionError + }) } }