block: qcow: Add From<qcow::Error> 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 <anbelski@linux.microsoft.com>
This commit is contained in:
Anatol Belski
2026-03-14 09:45:38 +01:00
committed by Rob Bradford
parent 0c3249b14f
commit d77e3e7ca2

View File

@@ -231,4 +231,56 @@ impl From<io::Error> 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<crate::qcow::Error> 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<T> = Result<T, BlockError>;