diff --git a/Cargo.toml b/Cargo.toml index 3763d61..22605c1 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 = "25ae084251b76bacfa8919eb8"} +trussed = { git = "https://github.com/sosthene-nitrokey/trussed", rev = "3eeda2a21106cb31120e08f5ae67a490b3d47bd6" } 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 ed2b4f2..5c6c58c 100644 --- a/src/state.rs +++ b/src/state.rs @@ -773,8 +773,8 @@ impl ContainerStorage { } } - pub fn save( - self, + fn save_inner( + &self, client: &mut impl trussed::Client, bytes: &[u8], storage: Location, @@ -789,10 +789,12 @@ impl ContainerStorage { }); msg = chunks.next().unwrap_or_default(); let mut written = msg.len(); - try_syscall!(client.write_file(storage, self.path(), msg, None)).map_err(|_err| { - error!("Failed to store data: {_err:?}"); - Status::UnspecifiedNonpersistentExecutionError - })?; + 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(); @@ -809,4 +811,24 @@ impl ContainerStorage { } 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 + } + } }