]> git.proxmox.com Git - pxar.git/commitdiff
format: add payload stream start marker
authorChristian Ebner <c.ebner@proxmox.com>
Thu, 21 Mar 2024 18:24:13 +0000 (19:24 +0100)
committerChristian Ebner <c.ebner@proxmox.com>
Wed, 5 Jun 2024 07:24:22 +0000 (09:24 +0200)
Mark the beginning of the payload stream with a magic number. Allows for
version and file type detection.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
examples/mk-format-hashes.rs
src/accessor/mod.rs
src/decoder/mod.rs
src/encoder/mod.rs
src/format/mod.rs

index de73df0043d5569c922160c364378f1d063a205c..35cff997847fb426c247bb7fcfccf979b7ea8e33 100644 (file)
@@ -56,6 +56,11 @@ const CONSTANTS: &[(&str, &str, &str)] = &[
         "PXAR_GOODBYE_TAIL_MARKER",
         "__PROXMOX_FORMAT_PXAR_GOODBYE_TAIL_MARKER__",
     ),
+    (
+        "The start marker used in the separate payload stream",
+        "PXAR_PAYLOAD_START_MARKER",
+        "__PROXMOX_FORMAT_PXAR_PAYLOAD_START_MARKER__",
+    ),
     (
         "The end marker used in the separate payload stream",
         "PXAR_PAYLOAD_TAIL_MARKER",
index 51e846ae6993aa993921f0629ac948f8d3bfa71d..c3a5e14ae436cc1e83b82170e8abd79a143a0a91 100644 (file)
@@ -238,7 +238,7 @@ async fn get_decoder<T: ReadAt>(
         |input| SeqReadAtAdapter::new(input, entry_range.clone()),
         |(payload_input, range)| SeqReadAtAdapter::new(payload_input, range),
     );
-    DecoderImpl::new_full(input, path, true).await
+    DecoderImpl::new_full(input, path, true, 0).await
 }
 
 // NOTE: This performs the Decoder::read_next_item() behavior! Keep in mind when changing!
index 0fc36982cc87bc301ce873d6f652d0c3212cf48b..19b1b5cb6e74d56f9f944ab46356adb275d2b2d9 100644 (file)
@@ -205,8 +205,21 @@ pub(crate) enum ItemResult {
 }
 
 impl<I: SeqRead> DecoderImpl<I> {
-    pub async fn new(input: PxarVariant<I, I>) -> io::Result<Self> {
-        Self::new_full(input, "/".into(), false).await
+    pub async fn new(mut input: PxarVariant<I, I>) -> io::Result<Self> {
+        let payload_consumed = if let Some(payload_input) = input.payload_mut() {
+            let header: Header = seq_read_entry(payload_input).await?;
+            if header.htype != format::PXAR_PAYLOAD_START_MARKER {
+                io_bail!(
+                    "unexpected header in payload input: expected {:#x?} , got {header:#x?}",
+                    format::PXAR_PAYLOAD_START_MARKER,
+                );
+            }
+            header.full_size()
+        } else {
+            0
+        };
+
+        Self::new_full(input, "/".into(), false, payload_consumed).await
     }
 
     pub(crate) fn input(&self) -> &I {
@@ -217,8 +230,9 @@ impl<I: SeqRead> DecoderImpl<I> {
         input: PxarVariant<I, I>,
         path: PathBuf,
         eof_after_entry: bool,
+        payload_consumed: u64,
     ) -> io::Result<Self> {
-        let this = DecoderImpl {
+        Ok(DecoderImpl {
             input,
             current_header: unsafe { mem::zeroed() },
             entry: Entry {
@@ -229,13 +243,9 @@ impl<I: SeqRead> DecoderImpl<I> {
             path_lengths: Vec::new(),
             state: State::Begin,
             with_goodbye_tables: false,
-            payload_consumed: 0,
+            payload_consumed,
             eof_after_entry,
-        };
-
-        // this.read_next_entry().await?;
-
-        Ok(this)
+        })
     }
 
     /// Get the next file entry, recursing into directories.
index 54e147d2ecf3b33fb119540a6c3b11d326f27794..b579e18517e9ff0b0dc7a36f9cda1e0b5d4ad3ae 100644 (file)
@@ -342,15 +342,23 @@ impl<'a, T: SeqWrite + 'a> Drop for EncoderImpl<'a, T> {
 
 impl<'a, T: SeqWrite + 'a> EncoderImpl<'a, T> {
     pub async fn new(
-        output: PxarVariant<EncoderOutput<'a, T>, T>,
+        mut output: PxarVariant<EncoderOutput<'a, T>, T>,
         metadata: &Metadata,
     ) -> io::Result<EncoderImpl<'a, T>> {
         if !metadata.is_dir() {
             io_bail!("directory metadata must contain the directory mode flag");
         }
+
+        let mut state = EncoderState::default();
+        if let Some(payload_output) = output.payload_mut() {
+            let header = format::Header::with_content_size(format::PXAR_PAYLOAD_START_MARKER, 0);
+            header.check_header_size()?;
+            seq_write_struct(payload_output, header, &mut state.payload_write_position).await?;
+        }
+
         let mut this = Self {
             output,
-            state: vec![EncoderState::default()],
+            state: vec![state],
             finished: false,
             file_copy_buffer: Arc::new(Mutex::new(unsafe {
                 crate::util::vec_new_uninitialized(1024 * 1024)
index e451b0f0f453529f0905ffe51417bde30b8ba7d2..6519bfc1caebe34849afb8e5441b97fb318fe3d1 100644 (file)
@@ -106,6 +106,8 @@ pub const PXAR_PAYLOAD_REF: u64 = 0x419d3d6bc4ba977e;
 pub const PXAR_GOODBYE: u64 = 0x2fec4fa642d5731d;
 /// The end marker used in the GOODBYE object
 pub const PXAR_GOODBYE_TAIL_MARKER: u64 = 0xef5eed5b753e1555;
+/// The start marker used in the separate payload stream
+pub const PXAR_PAYLOAD_START_MARKER: u64 = 0x834c68c2194a4ed2;
 /// The end marker used in the separate payload stream
 pub const PXAR_PAYLOAD_TAIL_MARKER: u64 = 0x6c72b78b984c81b5;