Use trussed's util for writing large files

This commit is contained in:
Sosthène Guédon
2023-04-13 10:20:21 +02:00
parent e5fd7dfb9a
commit fffbe77614
2 changed files with 6 additions and 52 deletions
+1 -1
View File
@@ -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" }
+5 -51
View File
@@ -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
})
}
}