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/.
27 /* Make sure the POSIX-compatible strerror_r() is declared, rather than the GNU
28 * version, which has a different return type. */
29 #define _POSIX_C_SOURCE 200112
33 #include "wimlib_internal.h"
34 #include "endianness.h"
35 #include "timestamp.h"
42 #include <unistd.h> /* for getpid() */
49 utf16le_strlen(const utf16lechar *s)
51 const utf16lechar *p = s;
54 return (p - s) * sizeof(utf16lechar);
58 # define wimlib_vfprintf vfwprintf
60 /* Handle %W for UTF16-LE printing.
62 * TODO: this is not yet done properly--- it's assumed that if the format string
63 * contains %W, then it contains no other format specifiers.
66 wimlib_vfprintf(FILE *fp, const tchar *format, va_list va)
71 for (p = format; *p; p++)
72 if (*p == T('%') && *(p + 1) == T('W'))
74 return tvfprintf(fp, format, va);
77 for (p = format; *p; p++) {
78 if (*p == T('%') && (*(p + 1) == T('W'))) {
82 utf16lechar *ucs = va_arg(va, utf16lechar*);
83 size_t ucs_nbytes = utf16le_strlen(ucs);
85 ret = utf16le_to_tstr(ucs, ucs_nbytes,
88 ret = tfprintf(fp, T("??????"));
90 ret = tfprintf(fp, T("%"TS), tstr);
99 if (tputc(*p, fp) == EOF)
108 wimlib_printf(const tchar *format, ...)
113 va_start(va, format);
114 ret = wimlib_vfprintf(stdout, format, va);
120 wimlib_fprintf(FILE *fp, const tchar *format, ...)
125 va_start(va, format);
126 ret = wimlib_vfprintf(fp, format, va);
132 #if defined(ENABLE_ERROR_MESSAGES) || defined(ENABLE_DEBUG)
134 wimlib_vmsg(const tchar *tag, const tchar *format,
135 va_list va, bool perror)
138 if (wimlib_print_errors) {
140 int errno_save = errno;
143 wimlib_vfprintf(stderr, format, va);
144 if (perror && errno_save != 0) {
147 res = tstrerror_r(errno_save, buf, sizeof(buf));
150 T("unknown error (errno=%d)"),
153 tfprintf(stderr, T(": %"TS), buf);
155 tputc(T('\n'), stderr);
163 /* True if wimlib is to print an informational message when an error occurs.
164 * This can be turned off by calling wimlib_set_print_errors(false). */
165 #ifdef ENABLE_ERROR_MESSAGES
166 static bool wimlib_print_errors = false;
170 wimlib_error(const tchar *format, ...)
174 va_start(va, format);
175 wimlib_vmsg(T("[ERROR] "), format, va, false);
180 wimlib_error_with_errno(const tchar *format, ...)
184 va_start(va, format);
185 wimlib_vmsg(T("[ERROR] "), format, va, true);
190 wimlib_warning(const tchar *format, ...)
194 va_start(va, format);
195 wimlib_vmsg(T("[WARNING] "), format, va, false);
200 wimlib_warning_with_errno(const tchar *format, ...)
204 va_start(va, format);
205 wimlib_vmsg(T("[WARNING] "), format, va, true);
211 #if defined(ENABLE_DEBUG) || defined(ENABLE_MORE_DEBUG)
212 void wimlib_debug(const tchar *file, int line, const char *func,
213 const tchar *format, ...)
217 tchar buf[tstrlen(file) + strlen(func) + 30];
219 tsprintf(buf, T("[%"TS" %d] %s(): "), file, line, func);
221 va_start(va, format);
222 wimlib_vmsg(buf, format, va, false);
228 wimlib_set_print_errors(bool show_error_messages)
230 #ifdef ENABLE_ERROR_MESSAGES
231 wimlib_print_errors = show_error_messages;
234 if (show_error_messages)
235 return WIMLIB_ERR_UNSUPPORTED;
241 static const tchar *error_strings[] = {
244 [WIMLIB_ERR_ALREADY_LOCKED]
245 = T("The WIM is already locked for writing"),
246 [WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE]
247 = T("Lookup table is compressed"),
248 [WIMLIB_ERR_DECOMPRESSION]
249 = T("Failed to decompress compressed data"),
250 [WIMLIB_ERR_DELETE_STAGING_DIR]
251 = T("Failed to delete staging directory"),
252 [WIMLIB_ERR_FILESYSTEM_DAEMON_CRASHED]
253 = T("The process servicing the mounted WIM has crashed"),
255 = T("Failed to fork another process"),
257 = T("An error was returned by fuse_main()"),
258 [WIMLIB_ERR_FUSERMOUNT]
259 = T("Could not execute the `fusermount' program, or it exited "
260 "with a failure status"),
261 [WIMLIB_ERR_ICONV_NOT_AVAILABLE]
262 = T("The iconv() function does not seem to work. "
263 "Maybe check to make sure the directory /usr/lib/gconv exists"),
264 [WIMLIB_ERR_IMAGE_COUNT]
265 = T("Inconsistent image count among the metadata "
266 "resources, the WIM header, and/or the XML data"),
267 [WIMLIB_ERR_IMAGE_NAME_COLLISION]
268 = T("Tried to add an image with a name that is already in use"),
269 [WIMLIB_ERR_INTEGRITY]
270 = T("The WIM failed an integrity check"),
271 [WIMLIB_ERR_INVALID_CAPTURE_CONFIG]
272 = T("The capture configuration string was invalid"),
273 [WIMLIB_ERR_INVALID_CHUNK_SIZE]
274 = T("The WIM is compressed but does not have a chunk "
276 [WIMLIB_ERR_INVALID_COMPRESSION_TYPE]
277 = T("The WIM is compressed, but is not marked as having LZX or "
278 "XPRESS compression"),
279 [WIMLIB_ERR_INVALID_DENTRY]
280 = T("A directory entry in the WIM was invalid"),
281 [WIMLIB_ERR_INVALID_HEADER_SIZE]
282 = T("The WIM header was not 208 bytes"),
283 [WIMLIB_ERR_INVALID_IMAGE]
284 = T("Tried to select an image that does not exist in the WIM"),
285 [WIMLIB_ERR_INVALID_INTEGRITY_TABLE]
286 = T("The WIM's integrity table is invalid"),
287 [WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY]
288 = T("An entry in the WIM's lookup table is invalid"),
289 [WIMLIB_ERR_INVALID_MULTIBYTE_STRING]
290 = T("A string was not valid in the current locale's character encoding"),
291 [WIMLIB_ERR_INVALID_OVERLAY]
292 = T("Conflicting files in overlay when creating a WIM image"),
293 [WIMLIB_ERR_INVALID_PARAM]
294 = T("An invalid parameter was given"),
295 [WIMLIB_ERR_INVALID_PART_NUMBER]
296 = T("The part number or total parts of the WIM is invalid"),
297 [WIMLIB_ERR_INVALID_RESOURCE_HASH]
298 = T("The SHA1 message digest of a WIM resource did not match the expected value"),
299 [WIMLIB_ERR_INVALID_RESOURCE_SIZE]
300 = T("A resource entry in the WIM has an invalid size"),
301 [WIMLIB_ERR_INVALID_SECURITY_DATA]
302 = T("The table of security descriptors in the WIM is invalid"),
303 [WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE]
304 = T("The version of wimlib that has mounted a WIM image is incompatible with the "
305 "version being used to unmount it"),
306 [WIMLIB_ERR_INVALID_UTF8_STRING]
307 = T("A string provided as input by the user was not a valid UTF-8 string"),
308 [WIMLIB_ERR_INVALID_UTF16_STRING]
309 = T("A string in a WIM dentry is not a valid UTF-16LE string"),
310 [WIMLIB_ERR_LIBXML_UTF16_HANDLER_NOT_AVAILABLE]
311 = T("libxml2 was unable to find a character encoding conversion handler "
314 = T("Failed to create a hard or symbolic link when extracting "
315 "a file from the WIM"),
317 = T("Failed to create a directory"),
319 = T("Failed to create or use a POSIX message queue"),
321 = T("Ran out of memory"),
323 = T("Expected a directory"),
324 [WIMLIB_ERR_NOT_A_WIM_FILE]
325 = T("The file did not begin with the magic characters that "
326 "identify a WIM file"),
327 [WIMLIB_ERR_NO_FILENAME]
328 = T("The WIM is not identified with a filename"),
330 = T("NTFS-3g encountered an error (check errno)"),
332 = T("Failed to open a file"),
334 = T("Failed to open a directory"),
336 = T("Could not read data from a file"),
337 [WIMLIB_ERR_READLINK]
338 = T("Could not read the target of a symbolic link"),
340 = T("Could not rename a file"),
342 = T("Could not re-open the WIM after overwriting it"),
343 [WIMLIB_ERR_RESOURCE_ORDER]
344 = T("The components of the WIM were arranged in an unexpected order"),
345 [WIMLIB_ERR_SPECIAL_FILE]
346 = T("Encountered a special file that cannot be archived"),
347 [WIMLIB_ERR_SPLIT_INVALID]
348 = T("The WIM is part of an invalid split WIM"),
349 [WIMLIB_ERR_SPLIT_UNSUPPORTED]
350 = T("The WIM is part of a split WIM, which is not supported for this operation"),
352 = T("Could not read the metadata for a file or directory"),
354 = T("Timed out while waiting for a message to arrive from another process"),
355 [WIMLIB_ERR_UNICODE_STRING_NOT_REPRESENTABLE]
356 = T("A Unicode string could not be represented in the current locale's encoding"),
357 [WIMLIB_ERR_UNKNOWN_VERSION]
358 = T("The WIM file is marked with an unknown version number"),
359 [WIMLIB_ERR_UNSUPPORTED]
360 = T("The requested operation is unsupported"),
361 [WIMLIB_ERR_VOLUME_LACKS_FEATURES]
362 = T("The volume did not support a feature necessary to complete the operation"),
364 = T("Failed to write data to a file"),
366 = T("The XML data of the WIM is invalid"),
369 WIMLIBAPI const tchar *
370 wimlib_get_error_string(enum wimlib_error_code code)
372 if (code < 0 || code >= ARRAY_LEN(error_strings))
375 return error_strings[code];
380 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
381 void *(*wimlib_malloc_func) (size_t) = malloc;
382 void (*wimlib_free_func) (void *) = free;
383 void *(*wimlib_realloc_func)(void *, size_t) = realloc;
386 wimlib_calloc(size_t nmemb, size_t size)
388 size_t total_size = nmemb * size;
389 void *p = MALLOC(total_size);
391 memset(p, 0, total_size);
396 wimlib_strdup(const char *str)
402 p = MALLOC(size + 1);
404 memcpy(p, str, size + 1);
410 wimlib_wcsdup(const wchar_t *str)
416 p = MALLOC((size + 1) * sizeof(wchar_t));
418 memcpy(p, str, (size + 1) * sizeof(wchar_t));
424 xml_set_memory_allocator(void *(*malloc_func)(size_t),
425 void (*free_func)(void *),
426 void *(*realloc_func)(void *, size_t));
430 wimlib_set_memory_allocator(void *(*malloc_func)(size_t),
431 void (*free_func)(void *),
432 void *(*realloc_func)(void *, size_t))
434 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
435 wimlib_malloc_func = malloc_func ? malloc_func : malloc;
436 wimlib_free_func = free_func ? free_func : free;
437 wimlib_realloc_func = realloc_func ? realloc_func : realloc;
439 xml_set_memory_allocator(wimlib_malloc_func, wimlib_free_func,
440 wimlib_realloc_func);
443 ERROR("Cannot set custom memory allocator functions:");
444 ERROR("wimlib was compiled with the --without-custom-memory-allocator "
446 return WIMLIB_ERR_UNSUPPORTED;
450 static bool seeded = false;
455 srand(time(NULL) * getpid());
459 /* Fills @n characters pointed to by @p with random alphanumeric characters. */
461 randomize_char_array_with_alnum(tchar p[], size_t n)
476 /* Fills @n bytes pointer to by @p with random numbers. */
478 randomize_byte_array(u8 *p, size_t n)
487 path_basename_with_len(const tchar *path, size_t len)
489 const tchar *p = &path[len] - 1;
491 /* Trailing slashes. */
500 while ((p != path - 1) && *p != T('/'))
506 /* Like the basename() function, but does not modify @path; it just returns a
509 path_basename(const tchar *path)
511 return path_basename_with_len(path, tstrlen(path));
515 * Returns a pointer to the part of @path following the first colon in the last
516 * path component, or NULL if the last path component does not contain a colon.
519 path_stream_name(const tchar *path)
521 const tchar *base = path_basename(path);
522 const tchar *stream_name = tstrchr(base, T(':'));
526 return stream_name + 1;
533 gettimeofday(&tv, NULL);
534 return timeval_to_wim_timestamp(tv);
538 wim_timestamp_to_str(u64 timestamp, tchar *buf, size_t len)
541 time_t t = wim_timestamp_to_unix(timestamp);
543 tstrftime(buf, len, T("%a %b %d %H:%M:%S %Y UTC"), &tm);
547 zap_backslashes(tchar *s)
550 while (*s != T('\0')) {