From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <f.schauer@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 19DDC939A4
 for <pbs-devel@lists.proxmox.com>; Tue,  9 Apr 2024 14:15:41 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id F3DBD1D175
 for <pbs-devel@lists.proxmox.com>; Tue,  9 Apr 2024 14:15:10 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (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 firstgate.proxmox.com (Proxmox) with ESMTPS
 for <pbs-devel@lists.proxmox.com>; Tue,  9 Apr 2024 14:15:09 +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 DBC6E41856
 for <pbs-devel@lists.proxmox.com>; Tue,  9 Apr 2024 14:15:08 +0200 (CEST)
From: Filip Schauer <f.schauer@proxmox.com>
To: pbs-devel@lists.proxmox.com
Date: Tue,  9 Apr 2024 14:14:20 +0200
Message-Id: <20240409121423.168627-7-f.schauer@proxmox.com>
X-Mailer: git-send-email 2.39.2
In-Reply-To: <20240409121423.168627-1-f.schauer@proxmox.com>
References: <20240409121423.168627-1-f.schauer@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.079 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
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pbs-devel] [PATCH v7 vma-to-pbs 6/9] refactor error handling
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: Tue, 09 Apr 2024 12:15:41 -0000

Signed-off-by: Filip Schauer <f.schauer@proxmox.com>
---
 src/main.rs |  4 ++--
 src/vma.rs  | 36 ++++++++++++++++++------------------
 2 files changed, 20 insertions(+), 20 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index cce9718..e2aed8e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,4 +1,4 @@
-use anyhow::{Context, Result};
+use anyhow::{Context, Error};
 use clap::{command, Arg, ArgAction};
 use proxmox_sys::linux::tty;
 
@@ -6,7 +6,7 @@ mod vma;
 mod vma2pbs;
 use vma2pbs::{backup_vma_to_pbs, BackupVmaToPbsArgs};
 
-fn main() -> Result<()> {
+fn main() -> Result<(), Error> {
     let matches = command!()
         .arg(
             Arg::new("repository")
diff --git a/src/vma.rs b/src/vma.rs
index 14f6e95..518de8a 100644
--- a/src/vma.rs
+++ b/src/vma.rs
@@ -3,7 +3,7 @@ use std::io::Read;
 use std::mem::size_of;
 use std::str;
 
-use anyhow::{anyhow, bail, Result};
+use anyhow::{bail, format_err, Context, Error};
 use bincode::Options;
 use serde::{Deserialize, Serialize};
 use serde_big_array::BigArray;
@@ -111,7 +111,7 @@ pub struct VmaReader<T> {
 }
 
 impl<T: Read> VmaReader<T> {
-    pub fn new(mut vma_file: T) -> Result<Self> {
+    pub fn new(mut vma_file: T) -> Result<Self, Error> {
         let vma_header = Self::read_header(&mut vma_file)?;
         let configs = Self::read_configs(&vma_header)?;
 
@@ -124,7 +124,7 @@ impl<T: Read> VmaReader<T> {
         Ok(instance)
     }
 
-    fn read_header(vma_file: &mut T) -> Result<VmaHeader> {
+    fn read_header(vma_file: &mut T) -> Result<VmaHeader, Error> {
         let mut buffer = vec![0; VMA_HEADER_SIZE_NO_BLOB_BUFFER];
         vma_file.read_exact(&mut buffer)?;
 
@@ -135,11 +135,11 @@ impl<T: Read> VmaReader<T> {
         let mut vma_header: VmaHeader = bincode_options.deserialize(&buffer)?;
 
         if vma_header.magic != VMA_HEADER_MAGIC {
-            return Err(anyhow!("Invalid magic number"));
+            bail!("Invalid magic number");
         }
 
         if vma_header.version != 1 {
-            return Err(anyhow!("Invalid VMA version {}", vma_header.version));
+            bail!("Invalid VMA version {}", vma_header.version);
         }
 
         buffer.resize(vma_header.header_size as usize, 0);
@@ -150,7 +150,7 @@ impl<T: Read> VmaReader<T> {
         let computed_md5sum: [u8; 16] = md5::compute(&buffer).into();
 
         if vma_header.md5sum != computed_md5sum {
-            return Err(anyhow!("Wrong VMA header checksum"));
+            bail!("Wrong VMA header checksum");
         }
 
         let blob_buffer = &buffer[VMA_HEADER_SIZE_NO_BLOB_BUFFER..vma_header.header_size as usize];
@@ -159,7 +159,7 @@ impl<T: Read> VmaReader<T> {
         Ok(vma_header)
     }
 
-    fn read_string(buffer: &[u8]) -> Result<String> {
+    fn read_string(buffer: &[u8]) -> Result<String, Error> {
         let size_bytes: [u8; 2] = buffer[0..2].try_into()?;
         let size = u16::from_le_bytes(size_bytes) as usize;
         let string_bytes: &[u8] = &buffer[2..1 + size];
@@ -168,7 +168,7 @@ impl<T: Read> VmaReader<T> {
         Ok(String::from(string))
     }
 
-    fn read_configs(vma_header: &VmaHeader) -> Result<HashSet<VmaConfig>> {
+    fn read_configs(vma_header: &VmaHeader) -> Result<HashSet<VmaConfig>, Error> {
         let mut configs = HashSet::new();
 
         for i in 0..VMA_MAX_CONFIGS {
@@ -199,7 +199,7 @@ impl<T: Read> VmaReader<T> {
         self.vma_header.dev_info[device_id as usize].device_name_offset != 0
     }
 
-    pub fn get_device_name(&self, device_id: VmaDeviceId) -> Result<String> {
+    pub fn get_device_name(&self, device_id: VmaDeviceId) -> Result<String, Error> {
         let device_name_offset =
             self.vma_header.dev_info[device_id as usize].device_name_offset as usize;
 
@@ -212,7 +212,7 @@ impl<T: Read> VmaReader<T> {
         Ok(device_name)
     }
 
-    pub fn get_device_size(&self, device_id: VmaDeviceId) -> Result<VmaDeviceSize> {
+    pub fn get_device_size(&self, device_id: VmaDeviceId) -> Result<VmaDeviceSize, Error> {
         let dev_info = &self.vma_header.dev_info[device_id as usize];
 
         if dev_info.device_name_offset == 0 {
@@ -222,7 +222,7 @@ impl<T: Read> VmaReader<T> {
         Ok(dev_info.device_size)
     }
 
-    fn read_extent_header(mut vma_file: impl Read) -> Result<VmaExtentHeader> {
+    fn read_extent_header(mut vma_file: impl Read) -> Result<VmaExtentHeader, Error> {
         let mut buffer = vec![0; size_of::<VmaExtentHeader>()];
         vma_file.read_exact(&mut buffer)?;
 
@@ -233,7 +233,7 @@ impl<T: Read> VmaReader<T> {
         let vma_extent_header: VmaExtentHeader = bincode_options.deserialize(&buffer)?;
 
         if vma_extent_header.magic != VMA_EXTENT_HEADER_MAGIC {
-            return Err(anyhow!("Invalid magic number"));
+            bail!("Invalid magic number");
         }
 
         // Fill the MD5 sum field with zeros to compute the MD5 sum
@@ -241,15 +241,15 @@ impl<T: Read> VmaReader<T> {
         let computed_md5sum: [u8; 16] = md5::compute(&buffer).into();
 
         if vma_extent_header.md5sum != computed_md5sum {
-            return Err(anyhow!("Wrong VMA extent header checksum"));
+            bail!("Wrong VMA extent header checksum");
         }
 
         Ok(vma_extent_header)
     }
 
-    fn restore_extent<F>(&mut self, callback: F) -> Result<()>
+    fn restore_extent<F>(&mut self, callback: F) -> Result<(), Error>
     where
-        F: Fn(VmaDeviceId, VmaDeviceOffset, Option<Vec<u8>>) -> Result<()>,
+        F: Fn(VmaDeviceId, VmaDeviceOffset, Option<Vec<u8>>) -> Result<(), Error>,
     {
         let vma_extent_header = Self::read_extent_header(&mut self.vma_file)?;
 
@@ -291,9 +291,9 @@ impl<T: Read> VmaReader<T> {
         Ok(())
     }
 
-    pub fn restore<F>(&mut self, callback: F) -> Result<()>
+    pub fn restore<F>(&mut self, callback: F) -> Result<(), Error>
     where
-        F: Fn(VmaDeviceId, VmaDeviceOffset, Option<Vec<u8>>) -> Result<()>,
+        F: Fn(VmaDeviceId, VmaDeviceOffset, Option<Vec<u8>>) -> Result<(), Error>,
     {
         loop {
             match self.restore_extent(&callback) {
@@ -303,7 +303,7 @@ impl<T: Read> VmaReader<T> {
                         if ioerr.kind() == std::io::ErrorKind::UnexpectedEof {
                             break; // Break out of the loop since the end of the file was reached.
                         } else {
-                            return Err(anyhow!("Failed to read VMA file: {}", ioerr));
+                            return Err(format_err!(e)).context("Failed to read VMA file");
                         }
                     }
                     _ => bail!(e),
-- 
2.39.2