[pbs-devel] [PATCH v3 pxar 3/24] fix #3174: encoder: calc filename + metadata byte size
Christian Ebner
c.ebner at proxmox.com
Fri Nov 3 14:37:08 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 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 | 37 +++++++++++++++++++++++++++++++++++++
src/encoder/sync.rs | 9 ++++++++-
2 files changed, 45 insertions(+), 1 deletion(-)
diff --git a/src/encoder/mod.rs b/src/encoder/mod.rs
index 0d342ec..a209ee7 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,
@@ -833,6 +851,25 @@ impl<'a, T: SeqWrite + 'a> EncoderImpl<'a, T> {
}
}
+impl EncoderImpl<'_, SeqSink> {
+ /// Calculate the encoded byte len of filename and metadata struct
+ async fn byte_len(filename: &std::ffi::CStr, metadata: &Metadata) -> io::Result<u64> {
+ let mut this = Self {
+ 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())
+ }
+}
+
/// Writer for a file object in a directory.
pub(crate) struct FileImpl<'a, S: SeqWrite> {
output: &'a mut S,
diff --git a/src/encoder/sync.rs b/src/encoder/sync.rs
index 1ec91b8..ac0025c 100644
--- a/src/encoder/sync.rs
+++ b/src/encoder/sync.rs
@@ -6,7 +6,7 @@ use std::pin::Pin;
use std::task::{Context, Poll};
use crate::decoder::sync::StandardReader;
-use crate::encoder::{self, LinkOffset, SeqWrite};
+use crate::encoder::{self, LinkOffset, SeqSink, SeqWrite};
use crate::format;
use crate::util::poll_result_once;
use crate::Metadata;
@@ -165,6 +165,13 @@ impl<'a, T: SeqWrite + 'a> Encoder<'a, T> {
}
}
+impl<'a> Encoder<'a, SeqSink> {
+ /// Calculate the encoded byte len of filename and metadata struct
+ pub fn byte_len(filename: &std::ffi::CStr, metadata: &Metadata) -> io::Result<u64> {
+ poll_result_once(encoder::EncoderImpl::byte_len(filename, metadata))
+ }
+}
+
/// This is a "file" inside a pxar archive, to which the initially declared amount of data should
/// be written.
///
--
2.39.2
More information about the pbs-devel
mailing list