[pbs-devel] [PATCH v5 pxar 3/28] fix #3174: encoder: calc filename + metadata byte size
Christian Ebner
c.ebner at proxmox.com
Wed Nov 15 16:47:48 CET 2023
Introduce SeqSink and impl SeqWrite in order to create an encoder
implementation which instead of writing data to a stream, consumes
the encoded stream and returns the consumed bytes for that stream.
Based on this, implement a helper function `byte_len` which returns the
byte size of the filename entry and metadata entry as encoded by the
archive.
Signed-off-by: Christian Ebner <c.ebner at proxmox.com>
---
Changes since v4:
- Refactor and rename `bytes_len` to `encoded_size`
Changes since v3:
- no changes
Changes since v2:
- no changes
Changes since v1:
- Instead of calculating the metadata size based on the known encoding
sizes, implement an Encoder instance which counts the encoded bytes.
src/encoder/mod.rs | 35 +++++++++++++++++++++++++++++++++++
src/encoder/sync.rs | 5 +++++
2 files changed, 40 insertions(+)
diff --git a/src/encoder/mod.rs b/src/encoder/mod.rs
index 0d342ec..860c21f 100644
--- a/src/encoder/mod.rs
+++ b/src/encoder/mod.rs
@@ -85,6 +85,24 @@ where
}
}
+#[derive(Default)]
+/// Sink to consume sequential byte stream
+pub struct SeqSink;
+
+impl SeqWrite for SeqSink {
+ fn poll_seq_write(
+ self: Pin<&mut Self>,
+ _cx: &mut Context,
+ buf: &[u8],
+ ) -> Poll<io::Result<usize>> {
+ Poll::Ready(Ok(buf.len()))
+ }
+
+ fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context) -> Poll<io::Result<()>> {
+ Poll::Ready(Ok(()))
+ }
+}
+
/// awaitable verison of `poll_seq_write`.
async fn seq_write<T: SeqWrite + ?Sized>(
output: &mut T,
@@ -183,6 +201,23 @@ where
seq_write_pxar_entry(output, htype, buf, position).await
}
+/// Calculate the encoded byte len of filename and metadata struct
+pub async fn encoded_size(filename: &std::ffi::CStr, metadata: &Metadata) -> io::Result<u64> {
+ let mut this = EncoderImpl {
+ output: EncoderOutput::Owned(SeqSink::default()),
+ state: EncoderState::default(),
+ parent: None,
+ finished: false,
+ file_copy_buffer: Arc::new(Mutex::new(unsafe {
+ crate::util::vec_new_uninitialized(1024 * 1024)
+ })),
+ };
+
+ this.start_file_do(Some(metadata), filename.to_bytes())
+ .await?;
+ Ok(this.position())
+}
+
/// Error conditions caused by wrong usage of this crate.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum EncodeError {
diff --git a/src/encoder/sync.rs b/src/encoder/sync.rs
index 1ec91b8..93c3b2c 100644
--- a/src/encoder/sync.rs
+++ b/src/encoder/sync.rs
@@ -228,3 +228,8 @@ impl<T: io::Write> SeqWrite for StandardWriter<T> {
Poll::Ready(self.pin_to_inner().and_then(|inner| inner.flush()))
}
}
+
+/// Calculate the encoded byte len of filename and metadata struct
+pub fn encoded_size(filename: &std::ffi::CStr, metadata: &Metadata) -> io::Result<u64> {
+ poll_result_once(crate::encoder::encoded_size(filename, metadata))
+}
--
2.39.2
More information about the pbs-devel
mailing list