2 * error.c - logging and error code translation
6 * Copyright (C) 2012, 2013, 2014 Eric Biggers
8 * This file is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU Lesser General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at your option) any
13 * This file is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this file; if not, see http://www.gnu.org/licenses/.
26 /* Make sure the POSIX-compatible strerror_r() is declared, rather than the GNU
27 * version, which has a different return type. */
29 # define GNU_SOURCE_WAS_DEFINED
31 # ifndef _POSIX_C_SOURCE
32 # define _POSIX_C_SOURCE 200112L
36 #ifdef GNU_SOURCE_WAS_DEFINED
44 #include "wimlib/error.h"
45 #include "wimlib/util.h"
46 #include "wimlib/win32.h"
48 #ifdef ENABLE_ERROR_MESSAGES
49 bool wimlib_print_errors = false;
50 FILE *wimlib_error_file = NULL; /* Set in wimlib_global_init() */
51 static bool wimlib_owns_error_file = false;
54 wimlib_vmsg(const tchar *tag, const tchar *format, va_list va, bool perror)
56 if (!wimlib_print_errors)
58 int errno_save = errno;
60 tfputs(tag, wimlib_error_file);
61 tvfprintf(wimlib_error_file, format, va);
62 if (perror && errno_save != 0) {
65 res = tstrerror_r(errno_save, buf, ARRAY_LEN(buf));
68 T("unknown error (errno=%d)"),
72 if (errno_save == EBUSY)
73 tstrcpy(buf, T("Resource busy"));
75 tfprintf(wimlib_error_file, T(": %"TS), buf);
77 tputc(T('\n'), wimlib_error_file);
78 fflush(wimlib_error_file);
83 wimlib_error(const tchar *format, ...)
88 wimlib_vmsg(T("\r[ERROR] "), format, va, false);
93 wimlib_error_with_errno(const tchar *format, ...)
98 wimlib_vmsg(T("\r[ERROR] "), format, va, true);
103 wimlib_warning(const tchar *format, ...)
107 va_start(va, format);
108 wimlib_vmsg(T("\r[WARNING] "), format, va, false);
113 wimlib_warning_with_errno(const tchar *format, ...)
117 va_start(va, format);
118 wimlib_vmsg(T("\r[WARNING] "), format, va, true);
121 #endif /* ENABLE_ERROR_MESSAGES */
124 print_byte_field(const u8 *field, size_t len, FILE *out)
127 tfprintf(out, T("%02hhx"), *field++);
131 wimlib_set_print_errors(bool show_error_messages)
133 #ifdef ENABLE_ERROR_MESSAGES
134 wimlib_print_errors = show_error_messages;
136 if (show_error_messages)
137 return WIMLIB_ERR_UNSUPPORTED;
143 wimlib_set_error_file(FILE *fp)
145 #ifdef ENABLE_ERROR_MESSAGES
146 if (wimlib_owns_error_file)
147 fclose(wimlib_error_file);
148 wimlib_error_file = fp;
149 wimlib_print_errors = (fp != NULL);
150 wimlib_owns_error_file = false;
153 return WIMLIB_ERR_UNSUPPORTED;
158 wimlib_set_error_file_by_name(const tchar *path)
160 #ifdef ENABLE_ERROR_MESSAGES
164 fp = win32_open_logfile(path);
166 fp = fopen(path, "a");
169 return WIMLIB_ERR_OPEN;
170 wimlib_set_error_file(fp);
171 wimlib_owns_error_file = true;
174 return WIMLIB_ERR_UNSUPPORTED;
178 static const tchar * const error_strings[] = {
181 [WIMLIB_ERR_ALREADY_LOCKED]
182 = T("The WIM is already locked for writing"),
183 [WIMLIB_ERR_DECOMPRESSION]
184 = T("Failed to decompress compressed data"),
186 = T("An error was returned by fuse_main()"),
187 [WIMLIB_ERR_GLOB_HAD_NO_MATCHES]
188 = T("The provided file glob did not match any files"),
189 [WIMLIB_ERR_ICONV_NOT_AVAILABLE]
190 = T("The iconv() function does not seem to work. "
191 "Maybe check to make sure the directory /usr/lib/gconv exists"),
192 [WIMLIB_ERR_IMAGE_COUNT]
193 = T("Inconsistent image count among the metadata "
194 "resources, the WIM header, and/or the XML data"),
195 [WIMLIB_ERR_IMAGE_NAME_COLLISION]
196 = T("Tried to add an image with a name that is already in use"),
197 [WIMLIB_ERR_INSUFFICIENT_PRIVILEGES]
198 = T("The user does not have sufficient privileges"),
199 [WIMLIB_ERR_INTEGRITY]
200 = T("The WIM file is corrupted (failed integrity check)"),
201 [WIMLIB_ERR_INVALID_CAPTURE_CONFIG]
202 = T("The contents of the capture configuration file were invalid"),
203 [WIMLIB_ERR_INVALID_CHUNK_SIZE]
204 = T("The compression chunk size was unrecognized"),
205 [WIMLIB_ERR_INVALID_COMPRESSION_TYPE]
206 = T("The compression type was unrecognized"),
207 [WIMLIB_ERR_INVALID_HEADER]
208 = T("The WIM header was invalid"),
209 [WIMLIB_ERR_INVALID_IMAGE]
210 = T("Tried to select an image that does not exist in the WIM"),
211 [WIMLIB_ERR_INVALID_INTEGRITY_TABLE]
212 = T("The WIM's integrity table is invalid"),
213 [WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY]
214 = T("An entry in the WIM's lookup table is invalid"),
215 [WIMLIB_ERR_INVALID_METADATA_RESOURCE]
216 = T("The metadata resource is invalid"),
217 [WIMLIB_ERR_INVALID_MULTIBYTE_STRING]
218 = T("A string was not valid in the current locale's character encoding"),
219 [WIMLIB_ERR_INVALID_OVERLAY]
220 = T("Conflicting files in overlay when creating a WIM image"),
221 [WIMLIB_ERR_INVALID_PARAM]
222 = T("An invalid parameter was given"),
223 [WIMLIB_ERR_INVALID_PART_NUMBER]
224 = T("The part number or total parts of the WIM is invalid"),
225 [WIMLIB_ERR_INVALID_PIPABLE_WIM]
226 = T("The pipable WIM is invalid"),
227 [WIMLIB_ERR_INVALID_REPARSE_DATA]
228 = T("The reparse data of a reparse point was invalid"),
229 [WIMLIB_ERR_INVALID_RESOURCE_HASH]
230 = T("The SHA-1 message digest of a WIM resource did not match the expected value"),
231 [WIMLIB_ERR_INVALID_UTF8_STRING]
232 = T("A string provided as input by the user was not a valid UTF-8 string"),
233 [WIMLIB_ERR_INVALID_UTF16_STRING]
234 = T("A string in a WIM dentry is not a valid UTF-16LE string"),
235 [WIMLIB_ERR_IS_DIRECTORY]
236 = T("One of the specified paths to delete was a directory"),
237 [WIMLIB_ERR_IS_SPLIT_WIM]
238 = T("The WIM is part of a split WIM, which is not supported for this operation"),
240 = T("Failed to create a hard or symbolic link when extracting "
241 "a file from the WIM"),
242 [WIMLIB_ERR_METADATA_NOT_FOUND]
243 = T("A required metadata resource could not be located"),
245 = T("Failed to create a directory"),
247 = T("Failed to create or use a POSIX message queue"),
249 = T("Ran out of memory"),
251 = T("Expected a directory"),
252 [WIMLIB_ERR_NOTEMPTY]
253 = T("Directory was not empty"),
254 [WIMLIB_ERR_NOT_A_REGULAR_FILE]
255 = T("One of the specified paths to extract did not "
256 "correspond to a regular file"),
257 [WIMLIB_ERR_NOT_A_WIM_FILE]
258 = T("The file did not begin with the magic characters that "
259 "identify a WIM file"),
260 [WIMLIB_ERR_NO_FILENAME]
261 = T("The WIM is not identified with a filename"),
262 [WIMLIB_ERR_NOT_PIPABLE]
263 = T("The WIM was not captured such that it can be "
264 "applied from a pipe"),
266 = T("NTFS-3g encountered an error (check errno)"),
268 = T("Failed to open a file"),
270 = T("Failed to open a directory"),
271 [WIMLIB_ERR_PATH_DOES_NOT_EXIST]
272 = T("The path does not exist in the WIM image"),
274 = T("Could not read data from a file"),
275 [WIMLIB_ERR_READLINK]
276 = T("Could not read the target of a symbolic link"),
278 = T("Could not rename a file"),
279 [WIMLIB_ERR_REPARSE_POINT_FIXUP_FAILED]
280 = T("Unable to complete reparse point fixup"),
281 [WIMLIB_ERR_RESOURCE_NOT_FOUND]
282 = T("A file resource needed to complete the operation was missing from the WIM"),
283 [WIMLIB_ERR_RESOURCE_ORDER]
284 = T("The components of the WIM were arranged in an unexpected order"),
285 [WIMLIB_ERR_SET_ATTRIBUTES]
286 = T("Failed to set attributes on extracted file"),
287 [WIMLIB_ERR_SET_REPARSE_DATA]
288 = T("Failed to set reparse data on extracted file"),
289 [WIMLIB_ERR_SET_SECURITY]
290 = T("Failed to set file owner, group, or other permissions on extracted file"),
291 [WIMLIB_ERR_SET_SHORT_NAME]
292 = T("Failed to set short name on extracted file"),
293 [WIMLIB_ERR_SET_TIMESTAMPS]
294 = T("Failed to set timestamps on extracted file"),
295 [WIMLIB_ERR_SPLIT_INVALID]
296 = T("The WIM is part of an invalid split WIM"),
298 = T("Could not read the metadata for a file or directory"),
299 [WIMLIB_ERR_UNEXPECTED_END_OF_FILE]
300 = T("Unexpectedly reached the end of the file"),
301 [WIMLIB_ERR_UNICODE_STRING_NOT_REPRESENTABLE]
302 = T("A Unicode string could not be represented in the current locale's encoding"),
303 [WIMLIB_ERR_UNKNOWN_VERSION]
304 = T("The WIM file is marked with an unknown version number"),
305 [WIMLIB_ERR_UNSUPPORTED]
306 = T("The requested operation is unsupported"),
307 [WIMLIB_ERR_UNSUPPORTED_FILE]
308 = T("A file in the directory tree to archive was not of a supported type"),
309 [WIMLIB_ERR_WIM_IS_READONLY]
310 = T("The WIM is read-only (file permissions, header flag, or split WIM)"),
312 = T("Failed to write data to a file"),
314 = T("The XML data of the WIM is invalid"),
315 [WIMLIB_ERR_WIM_IS_ENCRYPTED]
316 = T("The WIM file (or parts of it) is encrypted"),
318 = T("Failed to set WIMBoot pointer data"),
319 [WIMLIB_ERR_ABORTED_BY_PROGRESS]
320 = T("The operation was aborted by the library user"),
321 [WIMLIB_ERR_UNKNOWN_PROGRESS_STATUS]
322 = T("The user-provided progress function returned an unrecognized value"),
324 = T("Unable to create a special file (e.g. device node or socket)"),
325 [WIMLIB_ERR_MOUNTED_IMAGE_IS_BUSY]
326 = T("There are still files open on the mounted WIM image"),
327 [WIMLIB_ERR_NOT_A_MOUNTPOINT]
328 = T("There is not a WIM image mounted on the directory"),
329 [WIMLIB_ERR_NOT_PERMITTED_TO_UNMOUNT]
330 = T("The current user does not have permission to unmount the WIM image"),
331 [WIMLIB_ERR_FVE_LOCKED_VOLUME]
332 = T("The volume must be unlocked before it can be used"),
333 [WIMLIB_ERR_UNABLE_TO_READ_CAPTURE_CONFIG]
334 = T("The capture configuration file could not be read"),
335 [WIMLIB_ERR_WIM_IS_INCOMPLETE]
336 = T("The WIM file is incomplete"),
337 [WIMLIB_ERR_COMPACTION_NOT_POSSIBLE]
338 = T("The WIM file cannot be compacted because of its format, "
339 "its layout, or the write parameters specified by the user"),
340 [WIMLIB_ERR_IMAGE_HAS_MULTIPLE_REFERENCES]
341 = T("The WIM image cannot be modified because it is currently "
342 "referenced from multiple places"),
343 [WIMLIB_ERR_DUPLICATE_EXPORTED_IMAGE]
344 = T("The destination WIM already contains one of the source images"),
345 [WIMLIB_ERR_CONCURRENT_MODIFICATION_DETECTED]
346 = T("A file being added to a WIM image was concurrently modified"),
349 WIMLIBAPI const tchar *
350 wimlib_get_error_string(enum wimlib_error_code _code)
352 unsigned int code = (unsigned int)_code;
354 if (code >= ARRAY_LEN(error_strings) || error_strings[code] == NULL)
355 return T("Unknown error");
357 return error_strings[code];