qcow/
dynamic_qcow.rs

1use crate::{Qcow1, Qcow2, Snapshot};
2
3/// An enum representing a qcow of any version
4#[derive(Debug)]
5pub enum DynamicQcow {
6    /// Legacy version 1 qcow
7    Qcow1(Qcow1),
8
9    /// A qcow of version >= 2
10    Qcow2(Qcow2),
11}
12
13impl DynamicQcow {
14    /// Get the list of snapshots present within the qcow
15    pub fn snapshots(&self) -> &[Snapshot] {
16        #[allow(unreachable_patterns)]
17        match self {
18            Self::Qcow2(qcow) => &qcow.snapshots,
19            _ => &[],
20        }
21    }
22
23    /// Get the version of the qcow file
24    pub fn version(&self) -> u32 {
25        match self {
26            Self::Qcow2(qcow) => qcow.header.version,
27            Self::Qcow1(_) => 1,
28        }
29    }
30
31    /// Get the size of a cluster in bytes from the qcow
32    pub fn cluster_size(&self) -> u64 {
33        match self {
34            Self::Qcow2(qcow) => qcow.cluster_size(),
35            Self::Qcow1(qcow) => qcow.cluster_size(),
36        }
37    }
38
39    /// Gets the string representing the backing file of this qcow, if any.
40    pub fn backing_file(&self) -> Option<String> {
41        match self {
42            Self::Qcow2(qcow) => qcow.header.backing_file.clone(),
43            Self::Qcow1(qcow) => qcow.header.backing_file.clone(),
44        }
45    }
46
47    /// Unwrap the qcow into a version 1 qcow, panicking if the qcow is not version 1.
48    #[track_caller]
49    pub fn unwrap_qcow1(self) -> Qcow1 {
50        match self {
51            Self::Qcow1(qcow) => qcow,
52            _ => panic!("Expected a version 1 qcow"),
53        }
54    }
55
56    /// Unwrap the qcow as a qcow2, representing a version 2 or higher qcow, panicking if
57    /// the qcow is not version 2+.
58    #[track_caller]
59    pub fn unwrap_qcow2(self) -> Qcow2 {
60        #[allow(unreachable_patterns)]
61        match self {
62            Self::Qcow2(qcow) => qcow,
63            _ => panic!("Expected a version 2+ qcow"),
64        }
65    }
66}