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