From ddc67d75709d24549f0646bb036922fe5dfffaa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Wed, 29 Mar 2023 17:48:25 +0200 Subject: [PATCH] Implement writing of large data objects --- src/state.rs | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/state.rs b/src/state.rs index b2cf0f3..043da1e 100644 --- a/src/state.rs +++ b/src/state.rs @@ -776,14 +776,34 @@ impl ContainerStorage { bytes: &[u8], storage: Location, ) -> Result<(), Status> { - let msg = Bytes::from(heapless::Vec::try_from(bytes).map_err(|_| { - error!("Buffer full"); - Status::IncorrectDataParameter - })?); + 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.write_file(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(()) } }