6 * Copyright (C) 2012, 2013 Eric Biggers
8 * This file is part of wimlib, a library for working with WIM files.
10 * wimlib is free software; you can redistribute it and/or modify it under the
11 * terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 3 of the License, or (at your option)
15 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 * A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with wimlib; if not, see http://www.gnu.org/licenses/.
29 # define _GNU_SOURCE_DEFINED 1
32 /* Make sure the POSIX-compatible strerror_r() is declared, rather than the GNU
33 * version, which has a different return type. */
35 #ifdef _GNU_SOURCE_DEFINED
40 #include "wimlib/compiler.h"
41 #include "wimlib/encoding.h"
42 #include "wimlib/error.h"
43 #include "wimlib/types.h"
44 #include "wimlib/util.h"
45 #include "wimlib/xml.h"
48 # include "wimlib/win32.h" /* win32_strerror_r_replacement */
58 utf16le_strlen(const utf16lechar *s)
60 const utf16lechar *p = s;
63 return (p - s) * sizeof(utf16lechar);
67 # define wimlib_vfprintf vfwprintf
69 /* Handle %W for UTF16-LE printing.
71 * TODO: this is not yet done properly--- it's assumed that if the format string
72 * contains %W, then it contains no other format specifiers.
75 wimlib_vfprintf(FILE *fp, const tchar *format, va_list va)
80 for (p = format; *p; p++)
81 if (*p == T('%') && *(p + 1) == T('W'))
83 return tvfprintf(fp, format, va);
86 for (p = format; *p; p++) {
87 if (*p == T('%') && (*(p + 1) == T('W'))) {
91 utf16lechar *ucs = va_arg(va, utf16lechar*);
94 size_t ucs_nbytes = utf16le_strlen(ucs);
96 ret = utf16le_to_tstr(ucs, ucs_nbytes,
99 ret = tfprintf(fp, T("??????"));
101 ret = tfprintf(fp, T("%"TS), tstr);
109 n += tfprintf(fp, T("(null)"));
113 if (tputc(*p, fp) == EOF)
122 wimlib_printf(const tchar *format, ...)
127 va_start(va, format);
128 ret = wimlib_vfprintf(stdout, format, va);
134 wimlib_fprintf(FILE *fp, const tchar *format, ...)
139 va_start(va, format);
140 ret = wimlib_vfprintf(fp, format, va);
145 #endif /* __WIN32__ */
147 #ifdef ENABLE_ERROR_MESSAGES
148 bool wimlib_print_errors = false;
151 #if defined(ENABLE_ERROR_MESSAGES) || defined(ENABLE_DEBUG)
153 wimlib_vmsg(const tchar *tag, const tchar *format,
154 va_list va, bool perror)
157 if (wimlib_print_errors) {
159 int errno_save = errno;
162 wimlib_vfprintf(stderr, format, va);
163 if (perror && errno_save != 0) {
166 res = tstrerror_r(errno_save, buf, sizeof(buf));
169 T("unknown error (errno=%d)"),
172 tfprintf(stderr, T(": %"TS), buf);
174 tputc(T('\n'), stderr);
183 /* True if wimlib is to print an informational message when an error occurs.
184 * This can be turned off by calling wimlib_set_print_errors(false). */
185 #ifdef ENABLE_ERROR_MESSAGES
187 wimlib_error(const tchar *format, ...)
191 va_start(va, format);
192 wimlib_vmsg(T("\r[ERROR] "), format, va, false);
197 wimlib_error_with_errno(const tchar *format, ...)
201 va_start(va, format);
202 wimlib_vmsg(T("\r[ERROR] "), format, va, true);
207 wimlib_warning(const tchar *format, ...)
211 va_start(va, format);
212 wimlib_vmsg(T("\r[WARNING] "), format, va, false);
217 wimlib_warning_with_errno(const tchar *format, ...)
221 va_start(va, format);
222 wimlib_vmsg(T("\r[WARNING] "), format, va, true);
228 #if defined(ENABLE_DEBUG) || defined(ENABLE_MORE_DEBUG)
229 void wimlib_debug(const tchar *file, int line, const char *func,
230 const tchar *format, ...)
233 tchar buf[tstrlen(file) + strlen(func) + 30];
235 static bool debug_enabled = false;
236 if (!debug_enabled) {
237 char *value = getenv("WIMLIB_DEBUG");
238 if (!value || strcmp(value, "0"))
239 debug_enabled = true;
244 tsprintf(buf, T("[%"TS" %d] %s(): "), file, line, func);
246 va_start(va, format);
247 wimlib_vmsg(buf, format, va, false);
252 /* API function documented in wimlib.h */
254 wimlib_set_print_errors(bool show_error_messages)
256 #ifdef ENABLE_ERROR_MESSAGES
257 wimlib_print_errors = show_error_messages;
260 if (show_error_messages)
261 return WIMLIB_ERR_UNSUPPORTED;
267 static const tchar *error_strings[] = {
270 [WIMLIB_ERR_ALREADY_LOCKED]
271 = T("The WIM is already locked for writing"),
272 [WIMLIB_ERR_DECOMPRESSION]
273 = T("Failed to decompress compressed data"),
274 [WIMLIB_ERR_DELETE_STAGING_DIR]
275 = T("Failed to delete staging directory"),
276 [WIMLIB_ERR_FILESYSTEM_DAEMON_CRASHED]
277 = T("The process servicing the mounted WIM has crashed"),
279 = T("Failed to fork another process"),
281 = T("An error was returned by fuse_main()"),
282 [WIMLIB_ERR_FUSERMOUNT]
283 = T("Could not execute the `fusermount' program, or it exited "
284 "with a failure status"),
285 [WIMLIB_ERR_ICONV_NOT_AVAILABLE]
286 = T("The iconv() function does not seem to work. "
287 "Maybe check to make sure the directory /usr/lib/gconv exists"),
288 [WIMLIB_ERR_IMAGE_COUNT]
289 = T("Inconsistent image count among the metadata "
290 "resources, the WIM header, and/or the XML data"),
291 [WIMLIB_ERR_IMAGE_NAME_COLLISION]
292 = T("Tried to add an image with a name that is already in use"),
293 [WIMLIB_ERR_INSUFFICIENT_PRIVILEGES]
294 = T("The user does not have sufficient privileges"),
295 [WIMLIB_ERR_INTEGRITY]
296 = T("The WIM failed an integrity check"),
297 [WIMLIB_ERR_INVALID_CAPTURE_CONFIG]
298 = T("The capture configuration string was invalid"),
299 [WIMLIB_ERR_INVALID_CHUNK_SIZE]
300 = T("The WIM is compressed but does not have a chunk "
302 [WIMLIB_ERR_INVALID_COMPRESSION_TYPE]
303 = T("The WIM is compressed, but is not marked as having LZX or "
304 "XPRESS compression"),
305 [WIMLIB_ERR_INVALID_HEADER]
306 = T("The WIM header was invalid"),
307 [WIMLIB_ERR_INVALID_IMAGE]
308 = T("Tried to select an image that does not exist in the WIM"),
309 [WIMLIB_ERR_INVALID_INTEGRITY_TABLE]
310 = T("The WIM's integrity table is invalid"),
311 [WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY]
312 = T("An entry in the WIM's lookup table is invalid"),
313 [WIMLIB_ERR_INVALID_MULTIBYTE_STRING]
314 = T("A string was not valid in the current locale's character encoding"),
315 [WIMLIB_ERR_INVALID_OVERLAY]
316 = T("Conflicting files in overlay when creating a WIM image"),
317 [WIMLIB_ERR_INVALID_PARAM]
318 = T("An invalid parameter was given"),
319 [WIMLIB_ERR_INVALID_PART_NUMBER]
320 = T("The part number or total parts of the WIM is invalid"),
321 [WIMLIB_ERR_INVALID_PIPABLE_WIM]
322 = T("The pipable WIM is invalid"),
323 [WIMLIB_ERR_INVALID_REPARSE_DATA]
324 = T("The reparse data of a reparse point was invalid"),
325 [WIMLIB_ERR_INVALID_RESOURCE_HASH]
326 = T("The SHA1 message digest of a WIM resource did not match the expected value"),
327 [WIMLIB_ERR_INVALID_METADATA_RESOURCE]
328 = T("The metadata resource is invalid"),
329 [WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE]
330 = T("The version of wimlib that has mounted a WIM image is incompatible with the "
331 "version being used to unmount it"),
332 [WIMLIB_ERR_INVALID_UTF8_STRING]
333 = T("A string provided as input by the user was not a valid UTF-8 string"),
334 [WIMLIB_ERR_INVALID_UTF16_STRING]
335 = T("A string in a WIM dentry is not a valid UTF-16LE string"),
336 [WIMLIB_ERR_IS_DIRECTORY]
337 = T("One of the specified paths to delete was a directory"),
338 [WIMLIB_ERR_LIBXML_UTF16_HANDLER_NOT_AVAILABLE]
339 = T("libxml2 was unable to find a character encoding conversion handler "
342 = T("Failed to create a hard or symbolic link when extracting "
343 "a file from the WIM"),
345 = T("Failed to create a directory"),
347 = T("Failed to create or use a POSIX message queue"),
349 = T("Ran out of memory"),
351 = T("Expected a directory"),
352 [WIMLIB_ERR_NOTEMPTY]
353 = T("Directory was not empty"),
354 [WIMLIB_ERR_NOT_A_WIM_FILE]
355 = T("The file did not begin with the magic characters that "
356 "identify a WIM file"),
357 [WIMLIB_ERR_NOT_A_REGULAR_FILE]
358 = T("One of the specified paths to extract did not "
359 "correspond to a regular file"),
360 [WIMLIB_ERR_NO_FILENAME]
361 = T("The WIM is not identified with a filename"),
362 [WIMLIB_ERR_NOT_PIPABLE]
363 = T("The WIM was not captured such that it can be "
364 "applied from a pipe"),
366 = T("NTFS-3g encountered an error (check errno)"),
368 = T("Failed to open a file"),
370 = T("Failed to open a directory"),
371 [WIMLIB_ERR_PATH_DOES_NOT_EXIST]
372 = T("The path does not exist in the WIM image"),
374 = T("Could not read data from a file"),
375 [WIMLIB_ERR_READLINK]
376 = T("Could not read the target of a symbolic link"),
378 = T("Could not rename a file"),
379 [WIMLIB_ERR_REPARSE_POINT_FIXUP_FAILED]
380 = T("Unable to complete reparse point fixup"),
381 [WIMLIB_ERR_RESOURCE_NOT_FOUND]
382 = T("A file resource needed to complete the operation was missing from the WIM"),
383 [WIMLIB_ERR_RESOURCE_ORDER]
384 = T("The components of the WIM were arranged in an unexpected order"),
385 [WIMLIB_ERR_SET_ATTRIBUTES]
386 = T("Failed to set attributes on extracted file"),
387 [WIMLIB_ERR_SET_REPARSE_DATA]
388 = T("Failed to set reparse data on extracted file"),
389 [WIMLIB_ERR_SET_SECURITY]
390 = T("Failed to set file owner, group, or other permissions on extracted file"),
391 [WIMLIB_ERR_SET_SHORT_NAME]
392 = T("Failed to set short name on extracted file"),
393 [WIMLIB_ERR_SET_TIMESTAMPS]
394 = T("Failed to set timestamps on extracted file"),
395 [WIMLIB_ERR_SPLIT_INVALID]
396 = T("The WIM is part of an invalid split WIM"),
397 [WIMLIB_ERR_SPLIT_UNSUPPORTED]
398 = T("The WIM is part of a split WIM, which is not supported for this operation"),
400 = T("Could not read the metadata for a file or directory"),
402 = T("Timed out while waiting for a message to arrive from another process"),
403 [WIMLIB_ERR_UNEXPECTED_END_OF_FILE]
404 = T("Unexpectedly reached the end of the file"),
405 [WIMLIB_ERR_UNICODE_STRING_NOT_REPRESENTABLE]
406 = T("A Unicode string could not be represented in the current locale's encoding"),
407 [WIMLIB_ERR_UNKNOWN_VERSION]
408 = T("The WIM file is marked with an unknown version number"),
409 [WIMLIB_ERR_UNSUPPORTED]
410 = T("The requested operation is unsupported"),
411 [WIMLIB_ERR_UNSUPPORTED_FILE]
412 = T("A file in the directory tree to archive was not of a supported type"),
413 [WIMLIB_ERR_VOLUME_LACKS_FEATURES]
414 = T("The volume did not support a feature necessary to complete the operation"),
415 [WIMLIB_ERR_WIM_IS_READONLY]
416 = T("The WIM is read-only (file permissions, header flag, or split WIM)"),
418 = T("Failed to write data to a file"),
420 = T("The XML data of the WIM is invalid"),
423 /* API function documented in wimlib.h */
424 WIMLIBAPI const tchar *
425 wimlib_get_error_string(enum wimlib_error_code code)
427 if ((int)code < 0 || code >= ARRAY_LEN(error_strings))
430 return error_strings[code];
435 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
436 static void *(*wimlib_malloc_func) (size_t) = malloc;
437 static void (*wimlib_free_func) (void *) = free;
438 static void *(*wimlib_realloc_func)(void *, size_t) = realloc;
441 wimlib_malloc(size_t size)
445 void *ptr = (*wimlib_malloc_func)(size);
447 ERROR("memory exhausted");
452 wimlib_free_memory(void *ptr)
454 (*wimlib_free_func)(ptr);
458 wimlib_realloc(void *ptr, size_t size)
462 ptr = (*wimlib_realloc_func)(ptr, size);
464 ERROR("memory exhausted");
469 wimlib_calloc(size_t nmemb, size_t size)
471 size_t total_size = nmemb * size;
472 void *p = MALLOC(total_size);
474 p = memset(p, 0, total_size);
479 wimlib_strdup(const char *str)
485 p = MALLOC(size + 1);
487 p = memcpy(p, str, size + 1);
493 wimlib_wcsdup(const wchar_t *str)
499 p = MALLOC((size + 1) * sizeof(wchar_t));
501 p = wmemcpy(p, str, size + 1);
506 #endif /* ENABLE_CUSTOM_MEMORY_ALLOCATOR */
509 memdup(const void *mem, size_t size)
511 void *ptr = MALLOC(size);
513 ptr = memcpy(ptr, mem, size);
517 /* API function documented in wimlib.h */
519 wimlib_set_memory_allocator(void *(*malloc_func)(size_t),
520 void (*free_func)(void *),
521 void *(*realloc_func)(void *, size_t))
523 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
524 wimlib_malloc_func = malloc_func ? malloc_func : malloc;
525 wimlib_free_func = free_func ? free_func : free;
526 wimlib_realloc_func = realloc_func ? realloc_func : realloc;
528 xml_set_memory_allocator(wimlib_malloc_func, wimlib_free_func,
529 wimlib_realloc_func);
532 ERROR("Cannot set custom memory allocator functions:");
533 ERROR("wimlib was compiled with the --without-custom-memory-allocator "
535 return WIMLIB_ERR_UNSUPPORTED;
539 static bool seeded = false;
544 srand(time(NULL) * getpid());
548 /* Fills @n characters pointed to by @p with random alphanumeric characters. */
550 randomize_char_array_with_alnum(tchar p[], size_t n)
565 /* Fills @n bytes pointer to by @p with random numbers. */
567 randomize_byte_array(u8 *p, size_t n)
576 void print_byte_field(const u8 field[], size_t len, FILE *out)
579 tfprintf(out, T("%02hhx"), *field++);
583 void *mempcpy(void *dst, const void *src, size_t n)
585 return memcpy(dst, src, n) + n;