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/.
26 #define MINGW_HAS_SECURE_API
29 /* Make sure the POSIX-compatible strerror_r() is declared, rather than the GNU
30 * version, which has a different return type. */
31 #define _POSIX_C_SOURCE 200112
35 #include "wimlib_internal.h"
36 #include "endianness.h"
37 #include "timestamp.h"
44 #include <unistd.h> /* for getpid() */
46 /* Windoze compatibility */
48 # define strerror_r(errnum, buf, bufsize) strerror_s(buf, bufsize, errnum)
51 static size_t utf16le_strlen(const utf16lechar *s)
53 const utf16lechar *p = s;
56 return (p - s) / sizeof(utf16lechar);
59 /* Handle %W for UTF16-LE printing and %U for UTF-8 printing.
61 * WARNING: this is not yet done properly--- it's assumed that if the format
62 * string contains %W and/or %U, then it contains no other format specifiers.
65 wimlib_vfprintf(FILE *fp, const char *format, va_list va)
69 for (p = format; *p; p++)
70 if (*p == '%' && ((*p + 1) == 'W' || *(p + 1) == 'U'))
72 return vfprintf(fp, format, va);
76 for (p = format; *p; p++) {
77 if (*p == '%' && ((*p + 1) == 'W' || *(p + 1) == 'U')) {
82 if (*(p + 1) == 'W') {
83 utf16lechar *ucs = va_arg(va, utf16lechar*);
84 size_t ucs_nbytes = utf16le_strlen(ucs);
85 ret = utf16le_to_mbs(ucs, ucs_nbytes,
88 utf8char *ucs = va_arg(va, utf8char*);
89 size_t ucs_nbytes = strlen(ucs);
90 ret = utf8_to_mbs(ucs, ucs_nbytes,
94 ret = fprintf(fp, "???");
96 ret = fprintf(fp, "%s", mbs);
104 if (putc(*p, fp) == EOF)
113 wimlib_printf(const char *format, ...)
118 va_start(va, format);
119 ret = wimlib_vfprintf(stdout, format, va);
125 wimlib_fprintf(FILE *fp, const char *format, ...)
130 va_start(va, format);
131 ret = wimlib_vfprintf(fp, format, va);
136 /* True if wimlib is to print an informational message when an error occurs.
137 * This can be turned off by calling wimlib_set_print_errors(false). */
138 #ifdef ENABLE_ERROR_MESSAGES
140 static bool wimlib_print_errors = false;
143 wimlib_vmsg(const char *tag, const char *format,
144 va_list va, bool perror)
146 if (wimlib_print_errors) {
147 int errno_save = errno;
150 wimlib_vfprintf(stderr, format, va);
151 if (perror && errno_save != 0) {
154 res = strerror_r(errno_save, buf, sizeof(buf));
156 snprintf(buf, sizeof(buf),
157 "unknown error (errno=%d)", errno_save);
159 fprintf(stderr, ": %s", buf);
167 wimlib_error(const char *format, ...)
171 va_start(va, format);
172 wimlib_vmsg("[ERROR] ", format, va, false);
177 wimlib_error_with_errno(const char *format, ...)
181 va_start(va, format);
182 wimlib_vmsg("[ERROR] ", format, va, true);
187 wimlib_warning(const char *format, ...)
191 va_start(va, format);
192 wimlib_vmsg("[WARNING] ", format, va, false);
197 wimlib_warning_with_errno(const char *format, ...)
201 va_start(va, format);
202 wimlib_vmsg("[WARNING] ", format, va, true);
209 wimlib_set_print_errors(bool show_error_messages)
211 #ifdef ENABLE_ERROR_MESSAGES
212 wimlib_print_errors = show_error_messages;
215 if (show_error_messages)
216 return WIMLIB_ERR_UNSUPPORTED;
222 static const mbchar *error_strings[] = {
225 [WIMLIB_ERR_ALREADY_LOCKED]
226 = "The WIM is already locked for writing",
227 [WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE]
228 = "Lookup table is compressed",
229 [WIMLIB_ERR_DECOMPRESSION]
230 = "Failed to decompress compressed data",
231 [WIMLIB_ERR_DELETE_STAGING_DIR]
232 = "Failed to delete staging directory",
233 [WIMLIB_ERR_FILESYSTEM_DAEMON_CRASHED]
234 = "The process servicing the mounted WIM has crashed",
236 = "Failed to fork another process",
238 = "An error was returned by fuse_main()",
239 [WIMLIB_ERR_FUSERMOUNT]
240 = "Could not execute the `fusermount' program, or it exited "
241 "with a failure status",
242 [WIMLIB_ERR_ICONV_NOT_AVAILABLE]
243 = "The iconv() function does not seem to work. "
244 "Maybe check to make sure the directory /usr/lib/gconv exists",
245 [WIMLIB_ERR_IMAGE_COUNT]
246 = "Inconsistent image count among the metadata "
247 "resources, the WIM header, and/or the XML data",
248 [WIMLIB_ERR_IMAGE_NAME_COLLISION]
249 = "Tried to add an image with a name that is already in use",
250 [WIMLIB_ERR_INTEGRITY]
251 = "The WIM failed an integrity check",
252 [WIMLIB_ERR_INVALID_CAPTURE_CONFIG]
253 = "The capture configuration string was invalid",
254 [WIMLIB_ERR_INVALID_CHUNK_SIZE]
255 = "The WIM is compressed but does not have a chunk "
257 [WIMLIB_ERR_INVALID_COMPRESSION_TYPE]
258 = "The WIM is compressed, but is not marked as having LZX or "
259 "XPRESS compression",
260 [WIMLIB_ERR_INVALID_DENTRY]
261 = "A directory entry in the WIM was invalid",
262 [WIMLIB_ERR_INVALID_HEADER_SIZE]
263 = "The WIM header was not 208 bytes",
264 [WIMLIB_ERR_INVALID_IMAGE]
265 = "Tried to select an image that does not exist in the WIM",
266 [WIMLIB_ERR_INVALID_INTEGRITY_TABLE]
267 = "The WIM's integrity table is invalid",
268 [WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY]
269 = "An entry in the WIM's lookup table is invalid",
270 [WIMLIB_ERR_INVALID_MULTIBYTE_STRING]
271 = "A string was not valid in the current locale's character encoding",
272 [WIMLIB_ERR_INVALID_OVERLAY]
273 = "Conflicting files in overlay when creating a WIM image",
274 [WIMLIB_ERR_INVALID_PARAM]
275 = "An invalid parameter was given",
276 [WIMLIB_ERR_INVALID_PART_NUMBER]
277 = "The part number or total parts of the WIM is invalid",
278 [WIMLIB_ERR_INVALID_RESOURCE_HASH]
279 = "The SHA1 message digest of a WIM resource did not match the expected value",
280 [WIMLIB_ERR_INVALID_RESOURCE_SIZE]
281 = "A resource entry in the WIM has an invalid size",
282 [WIMLIB_ERR_INVALID_SECURITY_DATA]
283 = "The table of security descriptors in the WIM is invalid",
284 [WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE]
285 = "The version of wimlib that has mounted a WIM image is incompatible with the "
286 "version being used to unmount it",
287 [WIMLIB_ERR_INVALID_UTF8_STRING]
288 = "A string provided as input by the user was not a valid UTF-8 string",
289 [WIMLIB_ERR_INVALID_UTF16_STRING]
290 = "A string in a WIM dentry is not a valid UTF-16LE string",
291 [WIMLIB_ERR_LIBXML_UTF16_HANDLER_NOT_AVAILABLE]
292 = "libxml2 was unable to find a character encoding conversion handler "
295 = "Failed to create a hard or symbolic link when extracting "
296 "a file from the WIM",
298 = "Failed to create a directory",
300 = "Failed to create or use a POSIX message queue",
302 = "Ran out of memory",
304 = "Expected a directory",
305 [WIMLIB_ERR_NOT_A_WIM_FILE]
306 = "The file did not begin with the magic characters that "
307 "identify a WIM file",
308 [WIMLIB_ERR_NO_FILENAME]
309 = "The WIM is not identified with a filename",
311 = "NTFS-3g encountered an error (check errno)",
313 = "Failed to open a file",
315 = "Failed to open a directory",
317 = "Could not read data from a file",
318 [WIMLIB_ERR_READLINK]
319 = "Could not read the target of a symbolic link",
321 = "Could not rename a file",
323 = "Could not re-open the WIM after overwriting it",
324 [WIMLIB_ERR_RESOURCE_ORDER]
325 = "The components of the WIM were arranged in an unexpected order",
326 [WIMLIB_ERR_SPECIAL_FILE]
327 = "Encountered a special file that cannot be archived",
328 [WIMLIB_ERR_SPLIT_INVALID]
329 = "The WIM is part of an invalid split WIM",
330 [WIMLIB_ERR_SPLIT_UNSUPPORTED]
331 = "The WIM is part of a split WIM, which is not supported for this operation",
333 = "Could not read the metadata for a file or directory",
335 = "Timed out while waiting for a message to arrive from another process",
336 [WIMLIB_ERR_UNICODE_STRING_NOT_REPRESENTABLE]
337 = "A Unicode string could not be represented in the current locale's encoding",
338 [WIMLIB_ERR_UNKNOWN_VERSION]
339 = "The WIM file is marked with an unknown version number",
340 [WIMLIB_ERR_UNSUPPORTED]
341 = "The requested operation is unsupported",
343 = "Failed to write data to a file",
345 = "The XML data of the WIM is invalid",
348 WIMLIBAPI const mbchar *
349 wimlib_get_error_string(enum wimlib_error_code code)
351 if (code < WIMLIB_ERR_SUCCESS || code > WIMLIB_ERR_XML)
354 return error_strings[code];
359 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
360 void *(*wimlib_malloc_func) (size_t) = malloc;
361 void (*wimlib_free_func) (void *) = free;
362 void *(*wimlib_realloc_func)(void *, size_t) = realloc;
365 wimlib_calloc(size_t nmemb, size_t size)
367 size_t total_size = nmemb * size;
368 void *p = MALLOC(total_size);
370 memset(p, 0, total_size);
375 wimlib_strdup(const char *str)
381 p = MALLOC(size + 1);
383 memcpy(p, str, size + 1);
388 xml_set_memory_allocator(void *(*malloc_func)(size_t),
389 void (*free_func)(void *),
390 void *(*realloc_func)(void *, size_t));
394 wimlib_set_memory_allocator(void *(*malloc_func)(size_t),
395 void (*free_func)(void *),
396 void *(*realloc_func)(void *, size_t))
398 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
399 wimlib_malloc_func = malloc_func ? malloc_func : malloc;
400 wimlib_free_func = free_func ? free_func : free;
401 wimlib_realloc_func = realloc_func ? realloc_func : realloc;
403 xml_set_memory_allocator(wimlib_malloc_func, wimlib_free_func,
404 wimlib_realloc_func);
407 ERROR("Cannot set custom memory allocator functions:");
408 ERROR("wimlib was compiled with the --without-custom-memory-allocator "
410 return WIMLIB_ERR_UNSUPPORTED;
414 static bool seeded = false;
419 srand(time(NULL) * getpid());
423 /* Fills @n bytes pointed to by @p with random alphanumeric characters. */
425 randomize_char_array_with_alnum(char p[], size_t n)
440 /* Fills @n bytes pointer to by @p with random numbers. */
442 randomize_byte_array(u8 *p, size_t n)
450 /* Takes in a path of length @len in @buf, and transforms it into a string for
451 * the path of its parent directory. */
453 to_parent_name(char buf[], size_t len)
455 ssize_t i = (ssize_t)len - 1;
456 while (i >= 0 && buf[i] == '/')
458 while (i >= 0 && buf[i] != '/')
460 while (i >= 0 && buf[i] == '/')
465 /* Like the basename() function, but does not modify @path; it just returns a
468 path_basename(const char *path)
470 const char *p = path;
475 /* Trailing slashes. */
484 while ((p != path - 1) && *p != '/')
491 * Returns a pointer to the part of @path following the first colon in the last
492 * path component, or NULL if the last path component does not contain a colon.
495 path_stream_name(const char *path)
497 const char *base = path_basename(path);
498 const char *stream_name = strchr(base, ':');
502 return stream_name + 1;
506 * Splits a file path into the part before the first '/', or the entire name if
507 * there is no '/', and the part after the first sequence of '/' characters.
509 * @path: The file path to split.
510 * @first_part_len_ret: A pointer to a `size_t' into which the length of the
511 * first part of the path will be returned.
512 * @return: A pointer to the next part of the path, after the first
513 * sequence of '/', or a pointer to the terminating
514 * null byte in the case of a path without any '/'.
517 path_next_part(const char *path, size_t *first_part_len_ret)
520 const char *next_part;
523 while (path[i] != '/' && path[i] != '\0')
525 if (first_part_len_ret)
526 *first_part_len_ret = i;
527 next_part = &path[i];
528 while (*next_part == '/')
533 /* Returns the number of components of @path. */
535 get_num_path_components(const char *path)
537 int num_components = 0;
543 while (*path && *path != '/')
546 return num_components;
551 * Prints a string. Printable characters are printed as-is, while unprintable
552 * characters are printed as their octal escape codes.
555 print_string(const void *string, size_t len)
557 const u8 *p = string;
563 printf("\\%03hho", *p);
572 gettimeofday(&tv, NULL);
573 return timeval_to_wim_timestamp(tv);
577 wim_timestamp_to_str(u64 timestamp, char *buf, size_t len)
580 time_t t = wim_timestamp_to_unix(timestamp);
582 strftime(buf, len, "%a %b %d %H:%M:%S %Y UTC", &tm);