From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <s.reiter@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 3C2036CF99
 for <pbs-devel@lists.proxmox.com>; Wed, 31 Mar 2021 12:22:56 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 275D1DE44
 for <pbs-devel@lists.proxmox.com>; Wed, 31 Mar 2021 12:22:26 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [212.186.127.180])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id 01D26DDBC
 for <pbs-devel@lists.proxmox.com>; Wed, 31 Mar 2021 12:22:24 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 2FA7345983
 for <pbs-devel@lists.proxmox.com>; Wed, 31 Mar 2021 12:22:23 +0200 (CEST)
From: Stefan Reiter <s.reiter@proxmox.com>
To: pbs-devel@lists.proxmox.com
Date: Wed, 31 Mar 2021 12:21:43 +0200
Message-Id: <20210331102202.14767-2-s.reiter@proxmox.com>
X-Mailer: git-send-email 2.20.1
In-Reply-To: <20210331102202.14767-1-s.reiter@proxmox.com>
References: <20210331102202.14767-1-s.reiter@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.019 Adjusted score from AWL reputation of From: address
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 RCVD_IN_DNSWL_MED        -2.3 Sender listed at https://www.dnswl.org/,
 medium trust
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [aio.rs]
Subject: [pbs-devel] [PATCH v3 pxar 01/20] decoder/aio: add contents() and
 content_size() calls
X-BeenThere: pbs-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Backup Server development discussion
 <pbs-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/>
List-Post: <mailto:pbs-devel@lists.proxmox.com>
List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Wed, 31 Mar 2021 10:22:56 -0000

Returns a decoder::Contents without a wrapper type, since in this case
we don't want to hide the SeqRead implementation (as done in
decoder::sync). For conviencience also implement AsyncRead if "tokio-io"
is enabled.

Signed-off-by: Stefan Reiter <s.reiter@proxmox.com>
---

proxmox-backup requires a dependency bump on this!

v3:
* assume_init takes just 'n', already calculates offset correctly

v2:
* make contents() call available without tokio-io feature
* drop peek() implementation

 src/decoder/aio.rs | 36 +++++++++++++++++++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)

diff --git a/src/decoder/aio.rs b/src/decoder/aio.rs
index 82030b0..55e6464 100644
--- a/src/decoder/aio.rs
+++ b/src/decoder/aio.rs
@@ -5,7 +5,7 @@ use std::io;
 #[cfg(feature = "tokio-fs")]
 use std::path::Path;
 
-use crate::decoder::{self, SeqRead};
+use crate::decoder::{self, Contents, SeqRead};
 use crate::Entry;
 
 /// Asynchronous `pxar` decoder.
@@ -56,6 +56,16 @@ impl<T: SeqRead> Decoder<T> {
         self.inner.next_do().await.transpose()
     }
 
+    /// Get a reader for the contents of the current entry, if the entry has contents.
+    pub fn contents(&mut self) -> Option<Contents<T>> {
+        self.inner.content_reader()
+    }
+
+    /// Get the size of the current contents, if the entry has contents.
+    pub fn content_size(&self) -> Option<u64> {
+        self.inner.content_size()
+    }
+
     /// Include goodbye tables in iteration.
     pub fn enable_goodbye_entries(&mut self, on: bool) {
         self.inner.with_goodbye_tables = on;
@@ -67,6 +77,7 @@ mod tok {
     use std::io;
     use std::pin::Pin;
     use std::task::{Context, Poll};
+    use crate::decoder::{Contents, SeqRead};
 
     /// Read adapter for `futures::io::AsyncRead`
     pub struct TokioReader<T> {
@@ -93,6 +104,29 @@ mod tok {
             }
         }
     }
+
+    impl<'a, T: crate::decoder::SeqRead> tokio::io::AsyncRead for Contents<'a, T> {
+        fn poll_read(
+            self: Pin<&mut Self>,
+            cx: &mut Context<'_>,
+            buf: &mut tokio::io::ReadBuf<'_>,
+        ) -> Poll<io::Result<()>> {
+            unsafe {
+                // Safety: poll_seq_read will *probably* only write to the buffer, so we don't
+                // initialize it first, instead we treat is a &[u8] immediately and uphold the
+                // ReadBuf invariants in the conditional below.
+                let write_buf =
+                    &mut *(buf.unfilled_mut() as *mut [std::mem::MaybeUninit<u8>] as *mut [u8]);
+                let result = self.poll_seq_read(cx, write_buf);
+                if let Poll::Ready(Ok(n)) = result {
+                    // if we've written data, advance both initialized and filled bytes cursor
+                    buf.assume_init(n);
+                    buf.advance(n);
+                }
+                result.map(|_| Ok(()))
+            }
+        }
+    }
 }
 
 #[cfg(feature = "tokio-io")]
-- 
2.20.1