Skip to main content

disk_vmdk/
errors.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Errors returned while parsing descriptors and opening VMDK disks.
5
6use crate::MAX_DESCRIPTOR_SIZE;
7use std::io;
8use std::path::PathBuf;
9use thiserror::Error;
10
11/// An error parsing or validating the contents of a VMDK descriptor.
12#[derive(Debug, Error)]
13#[non_exhaustive]
14pub enum ParseError {
15    /// The descriptor is not UTF-8 text.
16    #[error("VMDK descriptor is not valid UTF-8")]
17    InvalidEncoding(#[from] std::str::Utf8Error),
18    /// A required descriptor field is missing.
19    #[error("VMDK descriptor is missing required field '{0}'")]
20    MissingField(&'static str),
21    /// The descriptor content is invalid or unsupported.
22    #[error("invalid VMDK descriptor: {0}")]
23    Invalid(String),
24}
25
26/// An error opening a VMDK or validating its descriptor, sources, and extent map.
27#[derive(Debug, Error)]
28#[non_exhaustive]
29pub enum OpenError {
30    /// The requested VMDK does not support writable access.
31    #[error("the requested VMDK does not support writable access")]
32    ReadWriteUnsupported,
33    /// The descriptor file could not be opened.
34    #[error("failed to open VMDK descriptor '{}'", path.display())]
35    OpenDescriptor {
36        /// Descriptor path.
37        path: PathBuf,
38        /// I/O error.
39        #[source]
40        source: io::Error,
41    },
42    /// Descriptor metadata could not be read.
43    #[error("failed to read metadata for VMDK descriptor '{}'", path.display())]
44    ReadDescriptorMetadata {
45        /// Descriptor path.
46        path: PathBuf,
47        /// I/O error.
48        #[source]
49        source: io::Error,
50    },
51    /// The descriptor is not a regular file.
52    #[error("VMDK descriptor '{}' is not a regular file", .0.display())]
53    DescriptorNotRegularFile(PathBuf),
54    /// The descriptor is larger than the accepted limit.
55    #[error("VMDK descriptor is larger than {MAX_DESCRIPTOR_SIZE} bytes")]
56    DescriptorTooLarge,
57    /// The descriptor file could not be read.
58    #[error("failed to read VMDK descriptor '{}'", path.display())]
59    ReadDescriptor {
60        /// Descriptor path.
61        path: PathBuf,
62        /// I/O error.
63        #[source]
64        source: io::Error,
65    },
66    /// The descriptor content is invalid or unsupported.
67    #[error(transparent)]
68    Parse(#[from] ParseError),
69    /// A referenced extent file could not be opened.
70    #[error("failed to open VMDK extent '{}'", path.display())]
71    OpenExtent {
72        /// Extent path.
73        path: PathBuf,
74        /// I/O error.
75        #[source]
76        source: io::Error,
77    },
78    /// Extent metadata could not be read.
79    #[error("failed to read metadata for VMDK source file {index}")]
80    ReadExtentMetadata {
81        /// Source file index.
82        index: usize,
83        /// I/O error.
84        #[source]
85        source: io::Error,
86    },
87    /// A referenced extent is not a regular file.
88    #[error("VMDK source file {0} is not a regular file")]
89    ExtentNotRegularFile(usize),
90    /// The supplied extent map is invalid.
91    #[error("invalid VMDK extent map: {0}")]
92    InvalidExtentMap(String),
93}