From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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) server-digest SHA256) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 33BEA630A6 for ; Tue, 9 Feb 2021 10:37:02 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 28A162ADF8 for ; Tue, 9 Feb 2021 10:37:02 +0100 (CET) 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 19F182ADDF for ; Tue, 9 Feb 2021 10:37:01 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id D506145F8C for ; Tue, 9 Feb 2021 10:37:00 +0100 (CET) From: Dominik Csapak To: pbs-devel@lists.proxmox.com Date: Tue, 9 Feb 2021 10:36:59 +0100 Message-Id: <20210209093700.30531-2-d.csapak@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210209093700.30531-1-d.csapak@proxmox.com> References: <20210209093700.30531-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.230 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. [mod.rs] Subject: [pbs-devel] [PATCH proxmox v3 1/1] proxmox: add sparse_copy(_async) to tools::io X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 09 Feb 2021 09:37:02 -0000 this is able to seek the target instead of writing zeroes, which generates sparse files where supported Signed-off-by: Dominik Csapak --- proxmox/src/tools/io/mod.rs | 70 +++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/proxmox/src/tools/io/mod.rs b/proxmox/src/tools/io/mod.rs index 2e92ebb..299d19c 100644 --- a/proxmox/src/tools/io/mod.rs +++ b/proxmox/src/tools/io/mod.rs @@ -3,8 +3,78 @@ //! The [`ReadExt`] trait provides additional operations for handling byte buffers for types //! implementing [`Read`](std::io::Read). +use std::io::{self, Read, Write, Seek, SeekFrom, ErrorKind}; + mod read; pub use read::*; mod write; pub use write::*; + +fn buffer_is_zero(buf: &[u8]) -> bool { + !buf + .chunks(128) + .map(|aa| + aa.iter().fold(0, |a, b| a|b) != 0 + ).any(|a| a) +} + +/// copy similar to io::copy, but seeks the target when encountering +/// zero bytes instead of writing them +pub fn sparse_copy( + reader: &mut R, + writer: &mut W, +) -> Result { + let mut buf = crate::tools::byte_buffer::ByteBuffer::new(); + let mut written = 0; + loop { + let len = match buf.read_from(reader) { + Ok(0) => return Ok(written), + Ok(len) => len, + Err(ref e) if e.kind() == ErrorKind::Interrupted => continue, + Err(e) => return Err(e), + }; + + if buffer_is_zero(&buf[..]) { + writer.seek(SeekFrom::Current(len as i64))?; + } else { + writer.write_all(&buf[..])?; + } + buf.clear(); + written += len as u64; + } +} + +#[cfg(feature = "tokio")] +use tokio::io::{AsyncReadExt, AsyncWriteExt, AsyncSeekExt}; + +#[cfg(feature = "tokio")] +/// copy similar to tokio::io::copy, but seeks the target when encountering +/// zero bytes instead of writing them +pub async fn sparse_copy_async( + reader: &mut R, + writer: &mut W, +) -> Result +where + R: AsyncReadExt + Unpin, + W: AsyncWriteExt + AsyncSeekExt + Unpin, +{ + let mut buf = crate::tools::byte_buffer::ByteBuffer::new(); + let mut written = 0; + loop { + let len = match buf.read_from_async(reader).await { + Ok(0) => return Ok(written), + Ok(len) => len, + Err(ref e) if e.kind() == ErrorKind::Interrupted => continue, + Err(e) => return Err(e), + }; + + if buffer_is_zero(&buf[..]) { + writer.seek(SeekFrom::Current(len as i64)).await?; + } else { + writer.write_all(&buf[..]).await?; + } + buf.clear(); + written += len as u64; + } +} -- 2.20.1