From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id E2A5E1FF17C for ; Wed, 9 Jul 2025 14:34:20 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id C1211B0F0; Wed, 9 Jul 2025 14:35:01 +0200 (CEST) From: Filip Schauer To: pve-devel@lists.proxmox.com Date: Wed, 9 Jul 2025 14:34:18 +0200 Message-ID: <20250709123435.64796-2-f.schauer@proxmox.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250709123435.64796-1-f.schauer@proxmox.com> References: <20250709123435.64796-1-f.schauer@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL -0.016 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. 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. [lib.rs] Subject: [pve-devel] [PATCH proxmox v3 01/13] io: introduce RangeReader for bounded reads X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox VE development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-devel" Signed-off-by: Filip Schauer --- Introduced in v3 proxmox-io/src/lib.rs | 3 ++ proxmox-io/src/range_reader.rs | 94 ++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 proxmox-io/src/range_reader.rs diff --git a/proxmox-io/src/lib.rs b/proxmox-io/src/lib.rs index 1be005ff..a05b9232 100644 --- a/proxmox-io/src/lib.rs +++ b/proxmox-io/src/lib.rs @@ -6,6 +6,9 @@ #![deny(unsafe_op_in_unsafe_fn)] #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] +mod range_reader; +pub use range_reader::RangeReader; + mod read; pub use read::ReadExt; diff --git a/proxmox-io/src/range_reader.rs b/proxmox-io/src/range_reader.rs new file mode 100644 index 00000000..307bfe27 --- /dev/null +++ b/proxmox-io/src/range_reader.rs @@ -0,0 +1,94 @@ +use std::io::{Read, Seek, SeekFrom}; +use std::ops::Range; + +pub struct RangeReader { + reader: R, + + /// Range inside `R`. + range: Range, + + /// Relative position inside `range` + position: u64, + + /// True once the initial seek has been performed + ready: bool, +} + +impl RangeReader { + pub fn new(reader: R, range: Range) -> Self { + Self { + reader, + range, + position: 0, + ready: false, + } + } + + pub fn into_inner(self) -> R { + self.reader + } + + pub fn size(&self) -> usize { + (self.range.end - self.range.start) as usize + } + + pub fn remaining(&self) -> usize { + self.size() - self.position as usize + } +} + +impl Read for RangeReader { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + let max_read = buf.len().min(self.remaining()); + let limited_buf = &mut buf[..max_read]; + + if !self.ready { + self.reader + .seek(SeekFrom::Start(self.range.start + self.position))?; + self.ready = true; + } + + let bytes_read = self.reader.read(limited_buf)?; + self.position += bytes_read.min(max_read) as u64; + + Ok(bytes_read) + } +} + +impl Seek for RangeReader { + fn seek(&mut self, pos: SeekFrom) -> std::io::Result { + self.position = match pos { + SeekFrom::Start(position) => position.min(self.size() as u64), + SeekFrom::End(offset) => { + if offset > self.size() as i64 { + return Err(std::io::Error::new( + std::io::ErrorKind::InvalidInput, + "Tried to seek before the beginning of the file", + )); + } + + (if offset <= 0 { + self.size() + } else { + self.size() - offset as usize + }) as u64 + } + SeekFrom::Current(offset) => { + if let Some(position) = self.position.checked_add_signed(offset) { + position.min(self.size() as u64) + } else { + return Err(std::io::Error::new( + std::io::ErrorKind::InvalidInput, + "Tried to seek before the beginning of the file", + )); + } + } + }; + + self.reader + .seek(SeekFrom::Start(self.range.start + self.position))?; + self.ready = true; + + Ok(self.position) + } +} -- 2.47.2 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel