From d77e3e7ca25257443fc20d424359fcce592c92ed Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Sat, 14 Mar 2026 09:45:38 +0100 Subject: [PATCH] block: qcow: Add From for BlockError Temporary From impl that classifies each qcow::Error variant into the appropriate BlockErrorKind. This enables an incremental migration of qcow functions from qcow::Result to BlockResult, where each subsequent commit replaces bare ? sites with explicit BlockError::new calls until this impl can be removed. The mapping assigns InvalidFormat for structural header violations, UnsupportedFeature for version and feature mismatches, CorruptImage for internal inconsistencies, Overflow for nesting depth and Io for everything else. Signed-off-by: Anatol Belski --- block/src/error.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/block/src/error.rs b/block/src/error.rs index ebaa33ec5..afd4b5533 100644 --- a/block/src/error.rs +++ b/block/src/error.rs @@ -231,4 +231,56 @@ impl From for BlockError { } } +/// Temporary scaffolding: classify a `qcow::Error` into the appropriate +/// `BlockErrorKind`. +/// +/// This impl exists only to allow an incremental migration of the qcow +/// parse/construct chain from `qcow::Result` to `BlockResult`. Each +/// subsequent commit replaces bare `?` sites with explicit +/// `BlockError::new(kind, e)` calls. Once every site is migrated this +/// impl will be removed. +impl From for BlockError { + fn from(e: crate::qcow::Error) -> Self { + use crate::qcow::Error as E; + let kind = match &e { + // Structural / format violations + E::InvalidMagic + | E::BackingFileTooLong(_) + | E::InvalidBackingFileName(_) + | E::InvalidClusterSize + | E::InvalidL1TableSize(_) + | E::InvalidL1TableOffset + | E::InvalidOffset(_) + | E::InvalidRefcountTableOffset + | E::InvalidRefcountTableSize(_) + | E::FileTooBig(_) + | E::NoRefcountClusters + | E::RefcountTableOffEnd + | E::RefcountTableTooLarge + | E::TooManyL1Entries(_) + | E::TooManyRefcounts(_) + | E::SizeTooSmallForNumberOfClusters => BlockErrorKind::InvalidFormat, + + // Unsupported features / versions + E::UnsupportedVersion(_) + | E::UnsupportedFeature(_) + | E::UnsupportedCompressionType + | E::UnsupportedBackingFileFormat(_) + | E::UnsupportedRefcountOrder + | E::BackingFilesDisabled + | E::ShrinkNotSupported => BlockErrorKind::UnsupportedFeature, + + // Corrupt image + E::CorruptImage => BlockErrorKind::CorruptImage, + + // Nesting depth overflow + E::MaxNestingDepthExceeded => BlockErrorKind::Overflow, + + // Everything else is I/O + _ => BlockErrorKind::Io, + }; + Self::new(kind, e) + } +} + pub type BlockResult = Result;