6 * Copyright (C) 2012, 2013 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/.
27 # define _GNU_SOURCE_DEFINED 1
30 /* Make sure the POSIX-compatible strerror_r() is declared, rather than the GNU
31 * version, which has a different return type. */
33 #ifdef _GNU_SOURCE_DEFINED
38 #include "wimlib/assert.h"
39 #include "wimlib/compiler.h"
40 #include "wimlib/encoding.h"
41 #include "wimlib/error.h"
42 #include "wimlib/types.h"
43 #include "wimlib/util.h"
44 #include "wimlib/xml.h"
47 # include "wimlib/win32.h" /* win32_strerror_r_replacement */
57 utf16le_strlen(const utf16lechar *s)
59 const utf16lechar *p = s;
62 return (p - s) * sizeof(utf16lechar);
65 #ifdef ENABLE_ERROR_MESSAGES
66 bool wimlib_print_errors = false;
67 FILE *wimlib_error_file = NULL; /* Set in wimlib_global_init() */
68 static bool wimlib_owns_error_file = false;
71 #if defined(ENABLE_ERROR_MESSAGES) || defined(ENABLE_DEBUG)
73 wimlib_vmsg(const tchar *tag, const tchar *format,
74 va_list va, bool perror)
76 #if !defined(ENABLE_DEBUG)
77 if (wimlib_print_errors)
80 int errno_save = errno;
82 tfputs(tag, wimlib_error_file);
83 tvfprintf(wimlib_error_file, format, va);
84 if (perror && errno_save != 0) {
87 res = tstrerror_r(errno_save, buf, ARRAY_LEN(buf));
90 T("unknown error (errno=%d)"),
94 if (errno_save == EBUSY)
95 tstrcpy(buf, T("Resource busy"));
97 tfprintf(wimlib_error_file, T(": %"TS), buf);
99 tputc(T('\n'), wimlib_error_file);
100 fflush(wimlib_error_file);
106 /* True if wimlib is to print an informational message when an error occurs.
107 * This can be turned off by calling wimlib_set_print_errors(false). */
108 #ifdef ENABLE_ERROR_MESSAGES
110 wimlib_error(const tchar *format, ...)
114 va_start(va, format);
115 wimlib_vmsg(T("\r[ERROR] "), format, va, false);
120 wimlib_error_with_errno(const tchar *format, ...)
124 va_start(va, format);
125 wimlib_vmsg(T("\r[ERROR] "), format, va, true);
130 wimlib_warning(const tchar *format, ...)
134 va_start(va, format);
135 wimlib_vmsg(T("\r[WARNING] "), format, va, false);
140 wimlib_warning_with_errno(const tchar *format, ...)
144 va_start(va, format);
145 wimlib_vmsg(T("\r[WARNING] "), format, va, true);
151 #if defined(ENABLE_DEBUG) || defined(ENABLE_MORE_DEBUG)
152 void wimlib_debug(const tchar *file, int line, const char *func,
153 const tchar *format, ...)
156 tchar buf[tstrlen(file) + strlen(func) + 30];
158 static bool debug_enabled = false;
159 if (!debug_enabled) {
160 char *value = getenv("WIMLIB_DEBUG");
161 if (!value || strcmp(value, "0"))
162 debug_enabled = true;
167 tsprintf(buf, T("[%"TS" %d] %s(): "), file, line, func);
169 va_start(va, format);
170 wimlib_vmsg(buf, format, va, false);
175 /* API function documented in wimlib.h */
177 wimlib_set_print_errors(bool show_error_messages)
179 #ifdef ENABLE_ERROR_MESSAGES
180 wimlib_print_errors = show_error_messages;
183 if (show_error_messages)
184 return WIMLIB_ERR_UNSUPPORTED;
191 wimlib_set_error_file(FILE *fp)
193 #ifdef ENABLE_ERROR_MESSAGES
194 if (wimlib_owns_error_file)
195 fclose(wimlib_error_file);
196 wimlib_error_file = fp;
197 wimlib_print_errors = (fp != NULL);
198 wimlib_owns_error_file = false;
201 return WIMLIB_ERR_UNSUPPORTED;
206 wimlib_set_error_file_by_name(const tchar *path)
208 #ifdef ENABLE_ERROR_MESSAGES
212 fp = win32_open_logfile(path);
214 fp = fopen(path, "a");
217 return WIMLIB_ERR_OPEN;
218 wimlib_set_error_file(fp);
219 wimlib_owns_error_file = true;
222 return WIMLIB_ERR_UNSUPPORTED;
226 static const tchar *error_strings[] = {
229 [WIMLIB_ERR_ALREADY_LOCKED]
230 = T("The WIM is already locked for writing"),
231 [WIMLIB_ERR_DECOMPRESSION]
232 = T("Failed to decompress compressed data"),
234 = T("An error was returned by fuse_main()"),
235 [WIMLIB_ERR_GLOB_HAD_NO_MATCHES]
236 = T("The provided file glob did not match any files"),
237 [WIMLIB_ERR_ICONV_NOT_AVAILABLE]
238 = T("The iconv() function does not seem to work. "
239 "Maybe check to make sure the directory /usr/lib/gconv exists"),
240 [WIMLIB_ERR_IMAGE_COUNT]
241 = T("Inconsistent image count among the metadata "
242 "resources, the WIM header, and/or the XML data"),
243 [WIMLIB_ERR_IMAGE_NAME_COLLISION]
244 = T("Tried to add an image with a name that is already in use"),
245 [WIMLIB_ERR_INSUFFICIENT_PRIVILEGES]
246 = T("The user does not have sufficient privileges"),
247 [WIMLIB_ERR_INTEGRITY]
248 = T("The WIM file is corrupted (failed integrity check)"),
249 [WIMLIB_ERR_INVALID_CAPTURE_CONFIG]
250 = T("The capture configuration string was invalid"),
251 [WIMLIB_ERR_INVALID_CHUNK_SIZE]
252 = T("The compression chunk size was unrecognized"),
253 [WIMLIB_ERR_INVALID_COMPRESSION_TYPE]
254 = T("The compression type was unrecognized"),
255 [WIMLIB_ERR_INVALID_HEADER]
256 = T("The WIM header was invalid"),
257 [WIMLIB_ERR_INVALID_IMAGE]
258 = T("Tried to select an image that does not exist in the WIM"),
259 [WIMLIB_ERR_INVALID_INTEGRITY_TABLE]
260 = T("The WIM's integrity table is invalid"),
261 [WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY]
262 = T("An entry in the WIM's lookup table is invalid"),
263 [WIMLIB_ERR_INVALID_METADATA_RESOURCE]
264 = T("The metadata resource is invalid"),
265 [WIMLIB_ERR_INVALID_MULTIBYTE_STRING]
266 = T("A string was not valid in the current locale's character encoding"),
267 [WIMLIB_ERR_INVALID_OVERLAY]
268 = T("Conflicting files in overlay when creating a WIM image"),
269 [WIMLIB_ERR_INVALID_PARAM]
270 = T("An invalid parameter was given"),
271 [WIMLIB_ERR_INVALID_PART_NUMBER]
272 = T("The part number or total parts of the WIM is invalid"),
273 [WIMLIB_ERR_INVALID_PIPABLE_WIM]
274 = T("The pipable WIM is invalid"),
275 [WIMLIB_ERR_INVALID_REPARSE_DATA]
276 = T("The reparse data of a reparse point was invalid"),
277 [WIMLIB_ERR_INVALID_RESOURCE_HASH]
278 = T("The SHA1 message digest of a WIM resource did not match the expected value"),
279 [WIMLIB_ERR_INVALID_UTF8_STRING]
280 = T("A string provided as input by the user was not a valid UTF-8 string"),
281 [WIMLIB_ERR_INVALID_UTF16_STRING]
282 = T("A string in a WIM dentry is not a valid UTF-16LE string"),
283 [WIMLIB_ERR_IS_DIRECTORY]
284 = T("One of the specified paths to delete was a directory"),
285 [WIMLIB_ERR_IS_SPLIT_WIM]
286 = T("The WIM is part of a split WIM, which is not supported for this operation"),
287 [WIMLIB_ERR_LIBXML_UTF16_HANDLER_NOT_AVAILABLE]
288 = T("libxml2 was unable to find a character encoding conversion handler "
291 = T("Failed to create a hard or symbolic link when extracting "
292 "a file from the WIM"),
293 [WIMLIB_ERR_METADATA_NOT_FOUND]
294 = T("A required metadata resource could not be located"),
296 = T("Failed to create a directory"),
298 = T("Failed to create or use a POSIX message queue"),
300 = T("Ran out of memory"),
302 = T("Expected a directory"),
303 [WIMLIB_ERR_NOTEMPTY]
304 = T("Directory was not empty"),
305 [WIMLIB_ERR_NOT_A_REGULAR_FILE]
306 = T("One of the specified paths to extract did not "
307 "correspond to a regular file"),
308 [WIMLIB_ERR_NOT_A_WIM_FILE]
309 = T("The file did not begin with the magic characters that "
310 "identify a WIM file"),
311 [WIMLIB_ERR_NO_FILENAME]
312 = T("The WIM is not identified with a filename"),
313 [WIMLIB_ERR_NOT_PIPABLE]
314 = T("The WIM was not captured such that it can be "
315 "applied from a pipe"),
317 = T("NTFS-3g encountered an error (check errno)"),
319 = T("Failed to open a file"),
321 = T("Failed to open a directory"),
322 [WIMLIB_ERR_PATH_DOES_NOT_EXIST]
323 = T("The path does not exist in the WIM image"),
325 = T("Could not read data from a file"),
326 [WIMLIB_ERR_READLINK]
327 = T("Could not read the target of a symbolic link"),
329 = T("Could not rename a file"),
330 [WIMLIB_ERR_REPARSE_POINT_FIXUP_FAILED]
331 = T("Unable to complete reparse point fixup"),
332 [WIMLIB_ERR_RESOURCE_NOT_FOUND]
333 = T("A file resource needed to complete the operation was missing from the WIM"),
334 [WIMLIB_ERR_RESOURCE_ORDER]
335 = T("The components of the WIM were arranged in an unexpected order"),
336 [WIMLIB_ERR_SET_ATTRIBUTES]
337 = T("Failed to set attributes on extracted file"),
338 [WIMLIB_ERR_SET_REPARSE_DATA]
339 = T("Failed to set reparse data on extracted file"),
340 [WIMLIB_ERR_SET_SECURITY]
341 = T("Failed to set file owner, group, or other permissions on extracted file"),
342 [WIMLIB_ERR_SET_SHORT_NAME]
343 = T("Failed to set short name on extracted file"),
344 [WIMLIB_ERR_SET_TIMESTAMPS]
345 = T("Failed to set timestamps on extracted file"),
346 [WIMLIB_ERR_SPLIT_INVALID]
347 = T("The WIM is part of an invalid split WIM"),
349 = T("Could not read the metadata for a file or directory"),
350 [WIMLIB_ERR_UNEXPECTED_END_OF_FILE]
351 = T("Unexpectedly reached the end of the file"),
352 [WIMLIB_ERR_UNICODE_STRING_NOT_REPRESENTABLE]
353 = T("A Unicode string could not be represented in the current locale's encoding"),
354 [WIMLIB_ERR_UNKNOWN_VERSION]
355 = T("The WIM file is marked with an unknown version number"),
356 [WIMLIB_ERR_UNSUPPORTED]
357 = T("The requested operation is unsupported"),
358 [WIMLIB_ERR_UNSUPPORTED_FILE]
359 = T("A file in the directory tree to archive was not of a supported type"),
360 [WIMLIB_ERR_WIM_IS_READONLY]
361 = T("The WIM is read-only (file permissions, header flag, or split WIM)"),
363 = T("Failed to write data to a file"),
365 = T("The XML data of the WIM is invalid"),
366 [WIMLIB_ERR_WIM_IS_ENCRYPTED]
367 = T("The WIM file (or parts of it) is encrypted"),
369 = T("Failed to set WIMBoot pointer data"),
370 [WIMLIB_ERR_ABORTED_BY_PROGRESS]
371 = T("The operation was aborted by the library user"),
372 [WIMLIB_ERR_UNKNOWN_PROGRESS_STATUS]
373 = T("The user-provided progress function returned an unrecognized value"),
375 = T("Unable to create a special file (e.g. device node or socket)"),
376 [WIMLIB_ERR_MOUNTED_IMAGE_IS_BUSY]
377 = T("There are still files open on the mounted WIM image"),
378 [WIMLIB_ERR_NOT_A_MOUNTPOINT]
379 = T("There is not a WIM image mounted on the directory"),
380 [WIMLIB_ERR_NOT_PERMITTED_TO_UNMOUNT]
381 = T("The current user does not have permission to unmount the WIM image"),
382 [WIMLIB_ERR_FVE_LOCKED_VOLUME]
383 = T("The volume must be unlocked before it can be used"),
386 /* API function documented in wimlib.h */
387 WIMLIBAPI const tchar *
388 wimlib_get_error_string(enum wimlib_error_code _code)
390 unsigned int code = (unsigned int)_code;
392 if (code >= ARRAY_LEN(error_strings) || error_strings[code] == NULL)
393 return T("Unknown error");
395 return error_strings[code];
400 static void *(*wimlib_malloc_func) (size_t) = malloc;
401 static void (*wimlib_free_func) (void *) = free;
402 static void *(*wimlib_realloc_func)(void *, size_t) = realloc;
405 wimlib_malloc(size_t size)
410 ptr = (*wimlib_malloc_func)(size);
411 if (unlikely(!ptr)) {
416 ERROR("memory exhausted");
422 wimlib_free_memory(void *ptr)
424 (*wimlib_free_func)(ptr);
428 wimlib_realloc(void *ptr, size_t size)
432 ptr = (*wimlib_realloc_func)(ptr, size);
434 ERROR("memory exhausted");
439 wimlib_calloc(size_t nmemb, size_t size)
441 size_t total_size = nmemb * size;
442 void *p = MALLOC(total_size);
444 p = memset(p, 0, total_size);
449 wimlib_strdup(const char *str)
455 p = MALLOC(size + 1);
457 p = memcpy(p, str, size + 1);
463 wimlib_wcsdup(const wchar_t *str)
469 p = MALLOC((size + 1) * sizeof(wchar_t));
471 p = wmemcpy(p, str, size + 1);
477 wimlib_aligned_malloc(size_t size, size_t alignment)
483 wimlib_assert(alignment != 0 && is_power_of_2(alignment) &&
485 mask = alignment - 1;
487 raw_ptr = MALLOC(size + alignment - 1 + sizeof(size_t));
491 ptr = (u8 *)raw_ptr + sizeof(size_t);
492 while ((uintptr_t)ptr & mask)
494 *((size_t *)ptr - 1) = (ptr - raw_ptr);
500 wimlib_aligned_free(void *ptr)
503 FREE((u8 *)ptr - *((size_t *)ptr - 1));
507 memdup(const void *mem, size_t size)
509 void *ptr = MALLOC(size);
511 ptr = memcpy(ptr, mem, size);
515 /* API function documented in wimlib.h */
517 wimlib_set_memory_allocator(void *(*malloc_func)(size_t),
518 void (*free_func)(void *),
519 void *(*realloc_func)(void *, size_t))
521 wimlib_malloc_func = malloc_func ? malloc_func : malloc;
522 wimlib_free_func = free_func ? free_func : free;
523 wimlib_realloc_func = realloc_func ? realloc_func : realloc;
525 xml_set_memory_allocator(wimlib_malloc_func, wimlib_free_func,
526 wimlib_realloc_func);
530 static bool seeded = false;
535 srand(time(NULL) * getpid());
539 /* Fills @n characters pointed to by @p with random alphanumeric characters. */
541 randomize_char_array_with_alnum(tchar p[], size_t n)
556 /* Fills @n bytes pointer to by @p with random numbers. */
558 randomize_byte_array(u8 *p, size_t n)
567 void print_byte_field(const u8 field[], size_t len, FILE *out)
570 tfprintf(out, T("%02hhx"), *field++);
574 void *mempcpy(void *dst, const void *src, size_t n)
576 return memcpy(dst, src, n) + n;