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 #if defined(ENABLE_ERROR_MESSAGES) || defined(ENABLE_DEBUG)
56 wimlib_vmsg(const tchar *tag, const tchar *format, va_list va, bool perror)
59 if (!wimlib_print_errors)
62 int errno_save = errno;
64 tfputs(tag, wimlib_error_file);
65 tvfprintf(wimlib_error_file, format, va);
66 if (perror && errno_save != 0) {
69 res = tstrerror_r(errno_save, buf, ARRAY_LEN(buf));
72 T("unknown error (errno=%d)"),
76 if (errno_save == EBUSY)
77 tstrcpy(buf, T("Resource busy"));
79 tfprintf(wimlib_error_file, T(": %"TS), buf);
81 tputc(T('\n'), wimlib_error_file);
82 fflush(wimlib_error_file);
87 #ifdef ENABLE_ERROR_MESSAGES
89 wimlib_error(const tchar *format, ...)
94 wimlib_vmsg(T("\r[ERROR] "), format, va, false);
99 wimlib_error_with_errno(const tchar *format, ...)
103 va_start(va, format);
104 wimlib_vmsg(T("\r[ERROR] "), format, va, true);
109 wimlib_warning(const tchar *format, ...)
113 va_start(va, format);
114 wimlib_vmsg(T("\r[WARNING] "), format, va, false);
119 wimlib_warning_with_errno(const tchar *format, ...)
123 va_start(va, format);
124 wimlib_vmsg(T("\r[WARNING] "), format, va, true);
131 void wimlib_debug(const tchar *filename, int line, const char *func,
132 const tchar *format, ...)
135 tchar buf[tstrlen(filename) + strlen(func) + 30];
137 tsprintf(buf, T("[%"TS" %d] %s(): "), filename, line, func);
138 va_start(va, format);
139 wimlib_vmsg(buf, format, va, false);
145 print_byte_field(const u8 *field, size_t len, FILE *out)
148 tfprintf(out, T("%02hhx"), *field++);
152 wimlib_set_print_errors(bool show_error_messages)
154 #ifdef ENABLE_ERROR_MESSAGES
155 wimlib_print_errors = show_error_messages;
157 if (show_error_messages)
158 return WIMLIB_ERR_UNSUPPORTED;
164 wimlib_set_error_file(FILE *fp)
166 #ifdef ENABLE_ERROR_MESSAGES
167 if (wimlib_owns_error_file)
168 fclose(wimlib_error_file);
169 wimlib_error_file = fp;
170 wimlib_print_errors = (fp != NULL);
171 wimlib_owns_error_file = false;
174 return WIMLIB_ERR_UNSUPPORTED;
179 wimlib_set_error_file_by_name(const tchar *path)
181 #ifdef ENABLE_ERROR_MESSAGES
185 fp = win32_open_logfile(path);
187 fp = fopen(path, "a");
190 return WIMLIB_ERR_OPEN;
191 wimlib_set_error_file(fp);
192 wimlib_owns_error_file = true;
195 return WIMLIB_ERR_UNSUPPORTED;
199 static const tchar * const error_strings[] = {
202 [WIMLIB_ERR_ALREADY_LOCKED]
203 = T("The WIM is already locked for writing"),
204 [WIMLIB_ERR_DECOMPRESSION]
205 = T("Failed to decompress compressed data"),
207 = T("An error was returned by fuse_main()"),
208 [WIMLIB_ERR_GLOB_HAD_NO_MATCHES]
209 = T("The provided file glob did not match any files"),
210 [WIMLIB_ERR_ICONV_NOT_AVAILABLE]
211 = T("The iconv() function does not seem to work. "
212 "Maybe check to make sure the directory /usr/lib/gconv exists"),
213 [WIMLIB_ERR_IMAGE_COUNT]
214 = T("Inconsistent image count among the metadata "
215 "resources, the WIM header, and/or the XML data"),
216 [WIMLIB_ERR_IMAGE_NAME_COLLISION]
217 = T("Tried to add an image with a name that is already in use"),
218 [WIMLIB_ERR_INSUFFICIENT_PRIVILEGES]
219 = T("The user does not have sufficient privileges"),
220 [WIMLIB_ERR_INTEGRITY]
221 = T("The WIM file is corrupted (failed integrity check)"),
222 [WIMLIB_ERR_INVALID_CAPTURE_CONFIG]
223 = T("The capture configuration string was invalid"),
224 [WIMLIB_ERR_INVALID_CHUNK_SIZE]
225 = T("The compression chunk size was unrecognized"),
226 [WIMLIB_ERR_INVALID_COMPRESSION_TYPE]
227 = T("The compression type was unrecognized"),
228 [WIMLIB_ERR_INVALID_HEADER]
229 = T("The WIM header was invalid"),
230 [WIMLIB_ERR_INVALID_IMAGE]
231 = T("Tried to select an image that does not exist in the WIM"),
232 [WIMLIB_ERR_INVALID_INTEGRITY_TABLE]
233 = T("The WIM's integrity table is invalid"),
234 [WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY]
235 = T("An entry in the WIM's lookup table is invalid"),
236 [WIMLIB_ERR_INVALID_METADATA_RESOURCE]
237 = T("The metadata resource is invalid"),
238 [WIMLIB_ERR_INVALID_MULTIBYTE_STRING]
239 = T("A string was not valid in the current locale's character encoding"),
240 [WIMLIB_ERR_INVALID_OVERLAY]
241 = T("Conflicting files in overlay when creating a WIM image"),
242 [WIMLIB_ERR_INVALID_PARAM]
243 = T("An invalid parameter was given"),
244 [WIMLIB_ERR_INVALID_PART_NUMBER]
245 = T("The part number or total parts of the WIM is invalid"),
246 [WIMLIB_ERR_INVALID_PIPABLE_WIM]
247 = T("The pipable WIM is invalid"),
248 [WIMLIB_ERR_INVALID_REPARSE_DATA]
249 = T("The reparse data of a reparse point was invalid"),
250 [WIMLIB_ERR_INVALID_RESOURCE_HASH]
251 = T("The SHA-1 message digest of a WIM resource did not match the expected value"),
252 [WIMLIB_ERR_INVALID_UTF8_STRING]
253 = T("A string provided as input by the user was not a valid UTF-8 string"),
254 [WIMLIB_ERR_INVALID_UTF16_STRING]
255 = T("A string in a WIM dentry is not a valid UTF-16LE string"),
256 [WIMLIB_ERR_IS_DIRECTORY]
257 = T("One of the specified paths to delete was a directory"),
258 [WIMLIB_ERR_IS_SPLIT_WIM]
259 = T("The WIM is part of a split WIM, which is not supported for this operation"),
260 [WIMLIB_ERR_LIBXML_UTF16_HANDLER_NOT_AVAILABLE]
261 = T("libxml2 was unable to find a character encoding conversion handler "
264 = T("Failed to create a hard or symbolic link when extracting "
265 "a file from the WIM"),
266 [WIMLIB_ERR_METADATA_NOT_FOUND]
267 = T("A required metadata resource could not be located"),
269 = T("Failed to create a directory"),
271 = T("Failed to create or use a POSIX message queue"),
273 = T("Ran out of memory"),
275 = T("Expected a directory"),
276 [WIMLIB_ERR_NOTEMPTY]
277 = T("Directory was not empty"),
278 [WIMLIB_ERR_NOT_A_REGULAR_FILE]
279 = T("One of the specified paths to extract did not "
280 "correspond to a regular file"),
281 [WIMLIB_ERR_NOT_A_WIM_FILE]
282 = T("The file did not begin with the magic characters that "
283 "identify a WIM file"),
284 [WIMLIB_ERR_NO_FILENAME]
285 = T("The WIM is not identified with a filename"),
286 [WIMLIB_ERR_NOT_PIPABLE]
287 = T("The WIM was not captured such that it can be "
288 "applied from a pipe"),
290 = T("NTFS-3g encountered an error (check errno)"),
292 = T("Failed to open a file"),
294 = T("Failed to open a directory"),
295 [WIMLIB_ERR_PATH_DOES_NOT_EXIST]
296 = T("The path does not exist in the WIM image"),
298 = T("Could not read data from a file"),
299 [WIMLIB_ERR_READLINK]
300 = T("Could not read the target of a symbolic link"),
302 = T("Could not rename a file"),
303 [WIMLIB_ERR_REPARSE_POINT_FIXUP_FAILED]
304 = T("Unable to complete reparse point fixup"),
305 [WIMLIB_ERR_RESOURCE_NOT_FOUND]
306 = T("A file resource needed to complete the operation was missing from the WIM"),
307 [WIMLIB_ERR_RESOURCE_ORDER]
308 = T("The components of the WIM were arranged in an unexpected order"),
309 [WIMLIB_ERR_SET_ATTRIBUTES]
310 = T("Failed to set attributes on extracted file"),
311 [WIMLIB_ERR_SET_REPARSE_DATA]
312 = T("Failed to set reparse data on extracted file"),
313 [WIMLIB_ERR_SET_SECURITY]
314 = T("Failed to set file owner, group, or other permissions on extracted file"),
315 [WIMLIB_ERR_SET_SHORT_NAME]
316 = T("Failed to set short name on extracted file"),
317 [WIMLIB_ERR_SET_TIMESTAMPS]
318 = T("Failed to set timestamps on extracted file"),
319 [WIMLIB_ERR_SPLIT_INVALID]
320 = T("The WIM is part of an invalid split WIM"),
322 = T("Could not read the metadata for a file or directory"),
323 [WIMLIB_ERR_UNEXPECTED_END_OF_FILE]
324 = T("Unexpectedly reached the end of the file"),
325 [WIMLIB_ERR_UNICODE_STRING_NOT_REPRESENTABLE]
326 = T("A Unicode string could not be represented in the current locale's encoding"),
327 [WIMLIB_ERR_UNKNOWN_VERSION]
328 = T("The WIM file is marked with an unknown version number"),
329 [WIMLIB_ERR_UNSUPPORTED]
330 = T("The requested operation is unsupported"),
331 [WIMLIB_ERR_UNSUPPORTED_FILE]
332 = T("A file in the directory tree to archive was not of a supported type"),
333 [WIMLIB_ERR_WIM_IS_READONLY]
334 = T("The WIM is read-only (file permissions, header flag, or split WIM)"),
336 = T("Failed to write data to a file"),
338 = T("The XML data of the WIM is invalid"),
339 [WIMLIB_ERR_WIM_IS_ENCRYPTED]
340 = T("The WIM file (or parts of it) is encrypted"),
342 = T("Failed to set WIMBoot pointer data"),
343 [WIMLIB_ERR_ABORTED_BY_PROGRESS]
344 = T("The operation was aborted by the library user"),
345 [WIMLIB_ERR_UNKNOWN_PROGRESS_STATUS]
346 = T("The user-provided progress function returned an unrecognized value"),
348 = T("Unable to create a special file (e.g. device node or socket)"),
349 [WIMLIB_ERR_MOUNTED_IMAGE_IS_BUSY]
350 = T("There are still files open on the mounted WIM image"),
351 [WIMLIB_ERR_NOT_A_MOUNTPOINT]
352 = T("There is not a WIM image mounted on the directory"),
353 [WIMLIB_ERR_NOT_PERMITTED_TO_UNMOUNT]
354 = T("The current user does not have permission to unmount the WIM image"),
355 [WIMLIB_ERR_FVE_LOCKED_VOLUME]
356 = T("The volume must be unlocked before it can be used"),
359 WIMLIBAPI const tchar *
360 wimlib_get_error_string(enum wimlib_error_code _code)
362 unsigned int code = (unsigned int)_code;
364 if (code >= ARRAY_LEN(error_strings) || error_strings[code] == NULL)
365 return T("Unknown error");
367 return error_strings[code];