wimlib/error.rs
1use {crate::sys, std::fmt::Display};
2
3/// Create a result from C return code
4pub const fn result_from_raw(code: i32) -> Result<(), Error> {
5 let code = code as u32;
6
7 if code == sys::wimlib_error_code_WIMLIB_ERR_SUCCESS {
8 Ok(())
9 } else {
10 Err(Error::from_raw(code))
11 }
12}
13
14macro_rules! define_error_enum {
15 {
16 $(
17 $(#[$attr:meta])*
18 $variant:ident
19 ),*
20 $(,)?
21 } => {
22 paste::paste! {
23 /// Possible values of the error code returned by many functions in wimlib
24 #[allow(missing_docs)]
25 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
26 #[repr(u32)]
27 #[non_exhaustive]
28 pub enum Error {
29 $(
30 $(#[$attr])*
31 $variant =
32 sys::[<wimlib_error_code_WIMLIB_ERR_ $variant:snake:upper>],
33 )*
34
35 /// NTFS-3G encountered an error (check errno)
36 Ntfs3G = sys::wimlib_error_code_WIMLIB_ERR_NTFS_3G,
37
38 #[doc(hidden)]
39 __Unknown = u32::MAX,
40 }
41
42 impl Error {
43 /// Create error from C library's status code
44 pub const fn from_raw(code: u32) -> Self {
45 match code {
46 $(
47 sys::[<wimlib_error_code_WIMLIB_ERR_ $variant:snake:upper>]
48 => Self::$variant,
49 )*
50 sys::wimlib_error_code_WIMLIB_ERR_NTFS_3G => Self::Ntfs3G,
51 _ => Self::__Unknown,
52 }
53 }
54 }
55 }
56 };
57}
58
59impl std::error::Error for Error {}
60impl Display for Error {
61 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62 let error_str = unsafe {
63 let str_ptr = sys::wimlib_get_error_string(*self as u32);
64 crate::string::TStr::from_ptr(str_ptr)
65 };
66
67 write!(f, "{error_str}")
68 }
69}
70
71define_error_enum! {
72 /// The WIM is already locked for writing
73 AlreadyLocked,
74 /// The WIM contains invalid compressed data
75 Decompression,
76 /// An error was returned by `fuse_main`
77 Fuse,
78 /// The provided file glob did not match any files
79 GlobHadNoMatches,
80 /// Inconsistent image count among the metadata
81 /// resources, the WIM header, and/or the XML data
82 ImageCount,
83 /// Tried to add an image with a name that is already in use
84 ImageNameCollision,
85 /// The user does not have sufficient privileges
86 InsufficientPrivileges,
87 /// The WIM file is corrupted (failed integrity check)
88 Integrity,
89 /// The contents of the capture configuration file were invalid
90 InvalidCaptureConfig,
91 /// The compression chunk size was unrecognized
92 InvalidChunkSize,
93 /// The compression type was unrecognized
94 InvalidCompressionType,
95 /// The WIM header was invalid
96 InvalidHeader,
97 /// Tried to select an image that does not exist in the WIM
98 InvalidImage,
99 /// The WIM's integrity table is invalid
100 InvalidIntegrityTable,
101 /// An entry in the WIM's lookup table is invalid
102 InvalidLookupTableEntry,
103 /// The metadata resource is invalid
104 InvalidMetadataResource,
105 /// Conflicting files in overlay when creating a WIM image
106 InvalidOverlay,
107 /// An invalid parameter was given
108 InvalidParam,
109 /// The part number or total parts of the WIM is invalid
110 InvalidPartNumber,
111 /// The pipable WIM is invalid
112 InvalidPipableWim,
113 /// The reparse data of a reparse point was invalid
114 InvalidReparseData,
115 /// The SHA-1 message digest of a WIM resource did not match the expected value
116 InvalidResourceHash,
117 /// A string was not a valid UTF-8 string
118 InvalidUtf8String,
119 /// A string was not a valid UTF-16 string
120 InvalidUtf16String,
121 /// One of the specified paths to delete was a directory
122 IsDirectory,
123 /// The WIM is part of a split WIM, which is not supported for this operation
124 IsSplitWim,
125 /// Failed to create a hard or symbolic link when extracting a file from the WIM
126 Link,
127 /// The WIM does not contain image metadata; it only contains file data
128 MetadataNotFound,
129 /// Failed to create a directory
130 Mkdir,
131 /// Failed to create or use a POSIX message queue
132 Mqueue,
133 /// Ran out of memory
134 Nomem,
135 /// Expected a directory
136 Notdir,
137 /// Directory was not empty
138 Notempty,
139 /// One of the specified paths to extract did not correspond to a regular file
140 NotARegularFile,
141 /// The file did not begin with the magic characters that identify a WIM file
142 NotAWimFile,
143 /// The WIM is not identified with a filename
144 NoFilename,
145 /// The WIM was not captured such that it can be applied from a pipe
146 NotPipable,
147 /// Failed to open a file
148 Open,
149 /// Failed to open a directory
150 Opendir,
151 /// The path does not exist in the WIM image
152 PathDoesNotExist,
153 /// Could not read data from a file
154 Read,
155 /// Could not read the target of a symbolic link
156 Readlink,
157 /// Could not rename a file
158 Rename,
159 /// Unable to complete reparse point fixup
160 ReparsePointFixupFailed,
161 /// A file resource needed to complete the operation was missing from the WIM
162 ResourceNotFound,
163 /// The components of the WIM were arranged in an unexpected order
164 ResourceOrder,
165 /// Failed to set attributes on extracted file
166 SetAttributes,
167 /// Failed to set reparse data on extracted file
168 SetReparseData,
169 /// Failed to set file owner, group, or other permissions on extracted file
170 SetSecurity,
171 /// Failed to set short name on extracted file
172 SetShortName,
173 /// Failed to set timestamps on extracted file
174 SetTimestamps,
175 /// The WIM is part of an invalid split WIM
176 SplitInvalid,
177 /// Could not read the metadata for a file or directory
178 Stat,
179 /// Unexpectedly reached the end of the file
180 UnexpectedEndOfFile,
181 /// A Unicode string could not be represented in the current locale's encoding
182 UnicodeStringNotRepresentable,
183 /// The WIM file is marked with an unknown version number
184 UnknownVersion,
185 /// The requested operation is unsupported
186 Unsupported,
187 /// A file in the directory tree to archive was not of a supported type
188 UnsupportedFile,
189 /// The WIM is read-only (file permissions, header flag, or split WIM)
190 WimIsReadonly,
191 /// Failed to write data to a file
192 Write,
193 /// The XML data of the WIM is invalid
194 Xml,
195 /// The WIM file (or parts of it) is encrypted
196 WimIsEncrypted,
197 /// Failed to set WIMBoot pointer data
198 Wimboot,
199 /// The operation was aborted by the library user
200 AbortedByProgress,
201 /// The user-provided progress function returned an unrecognized value
202 UnknownProgressStatus,
203 /// Unable to create a special file (e.g. device node or socket)
204 Mknod,
205 /// There are still files open on the mounted WIM image
206 MountedImageIsBusy,
207 /// There is not a WIM image mounted on the directory
208 NotAMountpoint,
209 /// The current user does not have permission to unmount the WIM image
210 NotPermittedToUnmount,
211 /// The volume must be unlocked before it can be used
212 FveLockedVolume,
213 /// The capture configuration file could not be read
214 UnableToReadCaptureConfig,
215 /// The WIM file is incomplete
216 WimIsIncomplete,
217 /// The WIM file cannot be compacted because of its format,
218 /// its layout, or the write parameters specified by the user
219 CompactionNotPossible,
220 /// The WIM image cannot be modified because it is currently
221 /// referenced from multiple places
222 ImageHasMultipleReferences,
223 /// The destination WIM already contains one of the source images
224 DuplicateExportedImage,
225 /// A file being added to a WIM image was concurrently modified
226 ConcurrentModificationDetected,
227 /// Unable to create a filesystem snapshot
228 SnapshotFailure,
229 /// An extended attribute entry in the WIM image is invalid
230 InvalidXattr,
231 /// Failed to set an extended attribute on an extracted file
232 SetXattr,
233}