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);
66 #ifdef ENABLE_ERROR_MESSAGES
67 bool wimlib_print_errors = false;
70 #if defined(ENABLE_ERROR_MESSAGES) || defined(ENABLE_DEBUG)
72 wimlib_vmsg(const tchar *tag, const tchar *format,
73 va_list va, bool perror)
75 #if !defined(ENABLE_DEBUG)
76 if (wimlib_print_errors)
79 int errno_save = errno;
82 tvfprintf(stderr, format, va);
83 if (perror && errno_save != 0) {
86 res = tstrerror_r(errno_save, buf, ARRAY_LEN(buf));
89 T("unknown error (errno=%d)"),
93 if (errno_save == EBUSY)
94 tstrcpy(buf, T("Resource busy"));
96 tfprintf(stderr, T(": %"TS), buf);
98 tputc(T('\n'), stderr);
105 /* True if wimlib is to print an informational message when an error occurs.
106 * This can be turned off by calling wimlib_set_print_errors(false). */
107 #ifdef ENABLE_ERROR_MESSAGES
109 wimlib_error(const tchar *format, ...)
113 va_start(va, format);
114 wimlib_vmsg(T("\r[ERROR] "), format, va, false);
119 wimlib_error_with_errno(const tchar *format, ...)
123 va_start(va, format);
124 wimlib_vmsg(T("\r[ERROR] "), format, va, true);
129 wimlib_warning(const tchar *format, ...)
133 va_start(va, format);
134 wimlib_vmsg(T("\r[WARNING] "), format, va, false);
139 wimlib_warning_with_errno(const tchar *format, ...)
143 va_start(va, format);
144 wimlib_vmsg(T("\r[WARNING] "), format, va, true);
150 #if defined(ENABLE_DEBUG) || defined(ENABLE_MORE_DEBUG)
151 void wimlib_debug(const tchar *file, int line, const char *func,
152 const tchar *format, ...)
155 tchar buf[tstrlen(file) + strlen(func) + 30];
157 static bool debug_enabled = false;
158 if (!debug_enabled) {
159 char *value = getenv("WIMLIB_DEBUG");
160 if (!value || strcmp(value, "0"))
161 debug_enabled = true;
166 tsprintf(buf, T("[%"TS" %d] %s(): "), file, line, func);
168 va_start(va, format);
169 wimlib_vmsg(buf, format, va, false);
174 /* API function documented in wimlib.h */
176 wimlib_set_print_errors(bool show_error_messages)
178 #ifdef ENABLE_ERROR_MESSAGES
179 wimlib_print_errors = show_error_messages;
182 if (show_error_messages)
183 return WIMLIB_ERR_UNSUPPORTED;
189 static const tchar *error_strings[] = {
192 [WIMLIB_ERR_ALREADY_LOCKED]
193 = T("The WIM is already locked for writing"),
194 [WIMLIB_ERR_DECOMPRESSION]
195 = T("Failed to decompress compressed data"),
197 = T("An error was returned by fuse_main()"),
198 [WIMLIB_ERR_GLOB_HAD_NO_MATCHES]
199 = T("The provided file glob did not match any files"),
200 [WIMLIB_ERR_ICONV_NOT_AVAILABLE]
201 = T("The iconv() function does not seem to work. "
202 "Maybe check to make sure the directory /usr/lib/gconv exists"),
203 [WIMLIB_ERR_IMAGE_COUNT]
204 = T("Inconsistent image count among the metadata "
205 "resources, the WIM header, and/or the XML data"),
206 [WIMLIB_ERR_IMAGE_NAME_COLLISION]
207 = T("Tried to add an image with a name that is already in use"),
208 [WIMLIB_ERR_INSUFFICIENT_PRIVILEGES]
209 = T("The user does not have sufficient privileges"),
210 [WIMLIB_ERR_INTEGRITY]
211 = T("The WIM failed an integrity check"),
212 [WIMLIB_ERR_INVALID_CAPTURE_CONFIG]
213 = T("The capture configuration string was invalid"),
214 [WIMLIB_ERR_INVALID_CHUNK_SIZE]
215 = T("The WIM chunk size was invalid"),
216 [WIMLIB_ERR_INVALID_COMPRESSION_TYPE]
217 = T("The WIM compression type was invalid"),
218 [WIMLIB_ERR_INVALID_HEADER]
219 = T("The WIM header was invalid"),
220 [WIMLIB_ERR_INVALID_IMAGE]
221 = T("Tried to select an image that does not exist in the WIM"),
222 [WIMLIB_ERR_INVALID_INTEGRITY_TABLE]
223 = T("The WIM's integrity table is invalid"),
224 [WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY]
225 = T("An entry in the WIM's lookup table is invalid"),
226 [WIMLIB_ERR_INVALID_METADATA_RESOURCE]
227 = T("The metadata resource is invalid"),
228 [WIMLIB_ERR_INVALID_MULTIBYTE_STRING]
229 = T("A string was not valid in the current locale's character encoding"),
230 [WIMLIB_ERR_INVALID_OVERLAY]
231 = T("Conflicting files in overlay when creating a WIM image"),
232 [WIMLIB_ERR_INVALID_PARAM]
233 = T("An invalid parameter was given"),
234 [WIMLIB_ERR_INVALID_PART_NUMBER]
235 = T("The part number or total parts of the WIM is invalid"),
236 [WIMLIB_ERR_INVALID_PIPABLE_WIM]
237 = T("The pipable WIM is invalid"),
238 [WIMLIB_ERR_INVALID_REPARSE_DATA]
239 = T("The reparse data of a reparse point was invalid"),
240 [WIMLIB_ERR_INVALID_RESOURCE_HASH]
241 = T("The SHA1 message digest of a WIM resource did not match the expected value"),
242 [WIMLIB_ERR_INVALID_UTF8_STRING]
243 = T("A string provided as input by the user was not a valid UTF-8 string"),
244 [WIMLIB_ERR_INVALID_UTF16_STRING]
245 = T("A string in a WIM dentry is not a valid UTF-16LE string"),
246 [WIMLIB_ERR_IS_DIRECTORY]
247 = T("One of the specified paths to delete was a directory"),
248 [WIMLIB_ERR_IS_SPLIT_WIM]
249 = T("The WIM is part of a split WIM, which is not supported for this operation"),
250 [WIMLIB_ERR_LIBXML_UTF16_HANDLER_NOT_AVAILABLE]
251 = T("libxml2 was unable to find a character encoding conversion handler "
254 = T("Failed to create a hard or symbolic link when extracting "
255 "a file from the WIM"),
256 [WIMLIB_ERR_METADATA_NOT_FOUND]
257 = T("A required metadata resource could not be located"),
259 = T("Failed to create a directory"),
261 = T("Failed to create or use a POSIX message queue"),
263 = T("Ran out of memory"),
265 = T("Expected a directory"),
266 [WIMLIB_ERR_NOTEMPTY]
267 = T("Directory was not empty"),
268 [WIMLIB_ERR_NOT_A_REGULAR_FILE]
269 = T("One of the specified paths to extract did not "
270 "correspond to a regular file"),
271 [WIMLIB_ERR_NOT_A_WIM_FILE]
272 = T("The file did not begin with the magic characters that "
273 "identify a WIM file"),
274 [WIMLIB_ERR_NO_FILENAME]
275 = T("The WIM is not identified with a filename"),
276 [WIMLIB_ERR_NOT_PIPABLE]
277 = T("The WIM was not captured such that it can be "
278 "applied from a pipe"),
280 = T("NTFS-3g encountered an error (check errno)"),
282 = T("Failed to open a file"),
284 = T("Failed to open a directory"),
285 [WIMLIB_ERR_PATH_DOES_NOT_EXIST]
286 = T("The path does not exist in the WIM image"),
288 = T("Could not read data from a file"),
289 [WIMLIB_ERR_READLINK]
290 = T("Could not read the target of a symbolic link"),
292 = T("Could not rename a file"),
293 [WIMLIB_ERR_REPARSE_POINT_FIXUP_FAILED]
294 = T("Unable to complete reparse point fixup"),
295 [WIMLIB_ERR_RESOURCE_NOT_FOUND]
296 = T("A file resource needed to complete the operation was missing from the WIM"),
297 [WIMLIB_ERR_RESOURCE_ORDER]
298 = T("The components of the WIM were arranged in an unexpected order"),
299 [WIMLIB_ERR_SET_ATTRIBUTES]
300 = T("Failed to set attributes on extracted file"),
301 [WIMLIB_ERR_SET_REPARSE_DATA]
302 = T("Failed to set reparse data on extracted file"),
303 [WIMLIB_ERR_SET_SECURITY]
304 = T("Failed to set file owner, group, or other permissions on extracted file"),
305 [WIMLIB_ERR_SET_SHORT_NAME]
306 = T("Failed to set short name on extracted file"),
307 [WIMLIB_ERR_SET_TIMESTAMPS]
308 = T("Failed to set timestamps on extracted file"),
309 [WIMLIB_ERR_SPLIT_INVALID]
310 = T("The WIM is part of an invalid split WIM"),
312 = T("Could not read the metadata for a file or directory"),
313 [WIMLIB_ERR_UNEXPECTED_END_OF_FILE]
314 = T("Unexpectedly reached the end of the file"),
315 [WIMLIB_ERR_UNICODE_STRING_NOT_REPRESENTABLE]
316 = T("A Unicode string could not be represented in the current locale's encoding"),
317 [WIMLIB_ERR_UNKNOWN_VERSION]
318 = T("The WIM file is marked with an unknown version number"),
319 [WIMLIB_ERR_UNSUPPORTED]
320 = T("The requested operation is unsupported"),
321 [WIMLIB_ERR_UNSUPPORTED_FILE]
322 = T("A file in the directory tree to archive was not of a supported type"),
323 [WIMLIB_ERR_WIM_IS_READONLY]
324 = T("The WIM is read-only (file permissions, header flag, or split WIM)"),
326 = T("Failed to write data to a file"),
328 = T("The XML data of the WIM is invalid"),
329 [WIMLIB_ERR_WIM_IS_ENCRYPTED]
330 = T("The WIM file (or parts of it) is encrypted"),
332 = T("Failed to set WIMBoot pointer data"),
333 [WIMLIB_ERR_ABORTED_BY_PROGRESS]
334 = T("The operation was aborted by the library user"),
335 [WIMLIB_ERR_UNKNOWN_PROGRESS_STATUS]
336 = T("The user-provided progress function returned an unrecognized value"),
338 = T("Unable to create a special file (e.g. device node or socket)"),
339 [WIMLIB_ERR_MOUNTED_IMAGE_IS_BUSY]
340 = T("There are still files open on the mounted WIM image"),
341 [WIMLIB_ERR_NOT_A_MOUNTPOINT]
342 = T("There is not a WIM image mounted on the directory"),
343 [WIMLIB_ERR_NOT_PERMITTED_TO_UNMOUNT]
344 = T("The current user does not have permission to unmount the WIM image"),
347 /* API function documented in wimlib.h */
348 WIMLIBAPI const tchar *
349 wimlib_get_error_string(enum wimlib_error_code code)
351 if ((int)code < 0 || code >= ARRAY_LEN(error_strings))
354 return error_strings[code];
359 static void *(*wimlib_malloc_func) (size_t) = malloc;
360 static void (*wimlib_free_func) (void *) = free;
361 static void *(*wimlib_realloc_func)(void *, size_t) = realloc;
364 wimlib_malloc(size_t size)
369 ptr = (*wimlib_malloc_func)(size);
370 if (unlikely(!ptr)) {
375 ERROR("memory exhausted");
381 wimlib_free_memory(void *ptr)
383 (*wimlib_free_func)(ptr);
387 wimlib_realloc(void *ptr, size_t size)
391 ptr = (*wimlib_realloc_func)(ptr, size);
393 ERROR("memory exhausted");
398 wimlib_calloc(size_t nmemb, size_t size)
400 size_t total_size = nmemb * size;
401 void *p = MALLOC(total_size);
403 p = memset(p, 0, total_size);
408 wimlib_strdup(const char *str)
414 p = MALLOC(size + 1);
416 p = memcpy(p, str, size + 1);
422 wimlib_wcsdup(const wchar_t *str)
428 p = MALLOC((size + 1) * sizeof(wchar_t));
430 p = wmemcpy(p, str, size + 1);
436 memdup(const void *mem, size_t size)
438 void *ptr = MALLOC(size);
440 ptr = memcpy(ptr, mem, size);
444 /* API function documented in wimlib.h */
446 wimlib_set_memory_allocator(void *(*malloc_func)(size_t),
447 void (*free_func)(void *),
448 void *(*realloc_func)(void *, size_t))
450 wimlib_malloc_func = malloc_func ? malloc_func : malloc;
451 wimlib_free_func = free_func ? free_func : free;
452 wimlib_realloc_func = realloc_func ? realloc_func : realloc;
454 xml_set_memory_allocator(wimlib_malloc_func, wimlib_free_func,
455 wimlib_realloc_func);
459 static bool seeded = false;
464 srand(time(NULL) * getpid());
468 /* Fills @n characters pointed to by @p with random alphanumeric characters. */
470 randomize_char_array_with_alnum(tchar p[], size_t n)
485 /* Fills @n bytes pointer to by @p with random numbers. */
487 randomize_byte_array(u8 *p, size_t n)
496 void print_byte_field(const u8 field[], size_t len, FILE *out)
499 tfprintf(out, T("%02hhx"), *field++);
503 void *mempcpy(void *dst, const void *src, size_t n)
505 return memcpy(dst, src, n) + n;