6 * Copyright (C) 2012 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/.
24 #include "wimlib_internal.h"
25 #include "endianness.h"
27 #include "timestamp.h"
39 /* True if wimlib is to print an informational message when an error occurs.
40 * This can be turned off by calling wimlib_set_print_errors(false). */
41 #ifdef ENABLE_ERROR_MESSAGES
43 bool __wimlib_print_errors = false;
45 void wimlib_error(const char *format, ...)
47 if (__wimlib_print_errors) {
53 fputs("[ERROR] ", stderr);
54 vfprintf(stderr, format, va);
61 void wimlib_error_with_errno(const char *format, ...)
63 if (__wimlib_print_errors) {
70 fputs("[ERROR] ", stderr);
71 vfprintf(stderr, format, va);
72 fprintf(stderr, ": %s\n", strerror(errno_save));
78 void wimlib_warning(const char *format, ...)
80 if (__wimlib_print_errors) {
87 fputs("[WARNING] ", stderr);
88 vfprintf(stderr, format, va);
97 WIMLIBAPI int wimlib_set_print_errors(bool show_error_messages)
99 #ifdef ENABLE_ERROR_MESSAGES
100 __wimlib_print_errors = show_error_messages;
103 if (show_error_messages)
104 return WIMLIB_ERR_UNSUPPORTED;
108 static const char *error_strings[] = {
111 [WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE]
112 = "Lookup table is compressed",
113 [WIMLIB_ERR_DECOMPRESSION]
114 = "Failed to decompress compressed data",
115 [WIMLIB_ERR_DELETE_STAGING_DIR]
116 = "Failed to delete staging directory",
118 = "Failed to fork another process",
120 = "An error was returned by fuse_main()",
121 [WIMLIB_ERR_FUSERMOUNT]
122 = "Could not execute the `fusermount' program, or it exited "
123 "with a failure status",
124 [WIMLIB_ERR_IMAGE_COUNT]
125 = "Inconsistent image count among the metadata "
126 "resources, the WIM header, and/or the XML data",
127 [WIMLIB_ERR_IMAGE_NAME_COLLISION]
128 = "Tried to add an image with a name that is already in use",
129 [WIMLIB_ERR_INTEGRITY]
130 = "The WIM failed an integrity check",
131 [WIMLIB_ERR_INVALID_CAPTURE_CONFIG]
132 = "The capture configuration string was invalid",
133 [WIMLIB_ERR_INVALID_CHUNK_SIZE]
134 = "The WIM is compressed but does not have a chunk "
136 [WIMLIB_ERR_INVALID_COMPRESSION_TYPE]
137 = "The WIM is compressed, but is not marked as having LZX or "
138 "XPRESS compression",
139 [WIMLIB_ERR_INVALID_DENTRY]
140 = "A directory entry in the WIM was invalid",
141 [WIMLIB_ERR_INVALID_HEADER_SIZE]
142 = "The WIM header was not 208 bytes",
143 [WIMLIB_ERR_INVALID_IMAGE]
144 = "Tried to select an image that does not exist in the WIM",
145 [WIMLIB_ERR_INVALID_INTEGRITY_TABLE]
146 = "The WIM's integrity table is invalid",
147 [WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY]
148 = "An entry in the WIM's lookup table is invalid",
149 [WIMLIB_ERR_INVALID_PARAM]
150 = "An invalid parameter was given",
151 [WIMLIB_ERR_INVALID_RESOURCE_HASH]
152 = "The SHA1 message digest of a WIM resource did not match the expected value",
153 [WIMLIB_ERR_INVALID_RESOURCE_SIZE]
154 = "A resource entry in the WIM has an invalid size",
156 = "Failed to create a hard or symbolic link when extracting "
157 "a file from the WIM",
159 = "Failed to create a directory",
161 = "Failed to create or use a POSIX message queue",
163 = "Ran out of memory",
165 = "Expected a directory",
166 [WIMLIB_ERR_NOT_A_WIM_FILE]
167 = "The file did not begin with the magic characters that "
168 "identify a WIM file",
169 [WIMLIB_ERR_NO_FILENAME]
170 = "The WIM is not identified with a filename",
172 = "NTFS-3g encountered an error (check errno)",
174 = "Failed to open a file",
176 = "Failed to open a directory",
178 = "Could not read data from a file",
179 [WIMLIB_ERR_READLINK]
180 = "Could not read the target of a symbolic link",
182 = "Could not rename a file",
183 [WIMLIB_ERR_SPECIAL_FILE]
184 = "Encountered a special file that cannot be archived",
185 [WIMLIB_ERR_SPLIT_INVALID]
186 = "The WIM is part of an invalid split WIM",
187 [WIMLIB_ERR_SPLIT_UNSUPPORTED]
188 = "The WIM is part of a split WIM, which is not supported for this operation",
190 = "Could not read the metadata for a file or directory",
193 [WIMLIB_ERR_UNKNOWN_VERSION]
194 = "The WIM file is marked with an unknown version number",
195 [WIMLIB_ERR_UNSUPPORTED]
196 = "The requested operation is unsupported",
198 = "Failed to write data to a file",
200 = "The XML data of the WIM is invalid",
203 WIMLIBAPI const char *wimlib_get_error_string(enum wimlib_error_code code)
205 if (code < WIMLIB_ERR_SUCCESS || code > WIMLIB_ERR_XML)
208 return error_strings[code];
213 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
214 void *(*wimlib_malloc_func) (size_t) = malloc;
215 void (*wimlib_free_func) (void *) = free;
216 void *(*wimlib_realloc_func)(void *, size_t) = realloc;
218 void *wimlib_calloc(size_t nmemb, size_t size)
220 size_t total_size = nmemb * size;
221 void *p = MALLOC(total_size);
223 memset(p, 0, total_size);
227 char *wimlib_strdup(const char *str)
233 p = MALLOC(size + 1);
235 memcpy(p, str, size + 1);
239 extern void xml_set_memory_allocator(void *(*malloc_func)(size_t),
240 void (*free_func)(void *),
241 void *(*realloc_func)(void *, size_t));
244 WIMLIBAPI int wimlib_set_memory_allocator(void *(*malloc_func)(size_t),
245 void (*free_func)(void *),
246 void *(*realloc_func)(void *, size_t))
248 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
249 wimlib_malloc_func = malloc_func ? malloc_func : malloc;
250 wimlib_free_func = free_func ? free_func : free;
251 wimlib_realloc_func = realloc_func ? realloc_func : realloc;
253 xml_set_memory_allocator(wimlib_malloc_func, wimlib_free_func,
254 wimlib_realloc_func);
257 ERROR("Cannot set custom memory allocator functions:");
258 ERROR("wimlib was compiled with the --without-custom-memory-allocator "
260 return WIMLIB_ERR_UNSUPPORTED;
266 static iconv_t cd_utf16_to_utf8 = (iconv_t)(-1);
268 /* Converts a string in the UTF-16 encoding to a newly allocated string in the
270 char *utf16_to_utf8(const char *utf16_str, size_t utf16_len,
271 size_t *utf8_len_ret)
273 if (cd_utf16_to_utf8 == (iconv_t)(-1)) {
274 cd_utf16_to_utf8 = iconv_open("UTF-8", "UTF-16LE");
275 if (cd_utf16_to_utf8 == (iconv_t)-1) {
276 ERROR_WITH_ERRNO("Failed to get conversion descriptor "
277 "for converting UTF-16LE to UTF-8");
281 size_t utf16_bytes_left = utf16_len;
282 size_t utf8_bytes_left = utf16_len;
284 char *utf8_str = MALLOC(utf8_bytes_left);
288 char *orig_utf8_str = utf8_str;
290 size_t num_chars_converted = iconv(cd_utf16_to_utf8, (char**)&utf16_str,
291 &utf16_bytes_left, &utf8_str, &utf8_bytes_left);
293 if (num_chars_converted == (size_t)(-1)) {
294 ERROR_WITH_ERRNO("Failed to convert UTF-16LE string to UTF-8 "
300 size_t utf8_len = utf16_len - utf8_bytes_left;
302 *utf8_len_ret = utf8_len;
303 orig_utf8_str[utf8_len] = '\0';
304 return orig_utf8_str;
307 static iconv_t cd_utf8_to_utf16 = (iconv_t)(-1);
309 /* Converts a string in the UTF-8 encoding to a newly allocated string in the
310 * UTF-16 encoding. */
311 char *utf8_to_utf16(const char *utf8_str, size_t utf8_len,
312 size_t *utf16_len_ret)
314 if (cd_utf8_to_utf16 == (iconv_t)(-1)) {
315 cd_utf8_to_utf16 = iconv_open("UTF-16LE", "UTF-8");
316 if (cd_utf8_to_utf16 == (iconv_t)-1) {
317 ERROR_WITH_ERRNO("Failed to get conversion descriptor "
318 "for converting UTF-8 to UTF-16LE");
323 size_t utf8_bytes_left = utf8_len;
324 size_t utf16_capacity = utf8_len * 4;
325 size_t utf16_bytes_left = utf16_capacity;
327 char *utf16_str = MALLOC(utf16_capacity + 2);
331 char *orig_utf16_str = utf16_str;
333 size_t num_chars_converted = iconv(cd_utf8_to_utf16, (char**)&utf8_str,
334 &utf8_bytes_left, &utf16_str, &utf16_bytes_left);
336 if (num_chars_converted == (size_t)(-1)) {
337 ERROR_WITH_ERRNO("Failed to convert UTF-8 string to UTF-16LE "
339 FREE(orig_utf16_str);
343 size_t utf16_len = utf16_capacity - utf16_bytes_left;
345 *utf16_len_ret = utf16_len;
346 orig_utf16_str[utf16_len] = '\0';
347 orig_utf16_str[utf16_len + 1] = '\0';
348 return orig_utf16_str;
351 /* Write @n bytes from @buf to the file descriptor @fd, retrying on interupt and
354 * Returns short count and set errno on failure. */
355 ssize_t full_write(int fd, const void *buf, size_t n)
362 ret = write(fd, p, n);
376 static bool seeded = false;
378 /* Fills @n bytes pointed to by @p with random alphanumeric characters. */
379 void randomize_char_array_with_alnum(char p[], size_t n)
398 /* Fills @n bytes pointer to by @p with random numbers. */
399 void randomize_byte_array(u8 *p, size_t n)
409 /* Takes in a path of length @len in @buf, and transforms it into a string for
410 * the path of its parent directory. */
411 void to_parent_name(char buf[], size_t len)
413 ssize_t i = (ssize_t)len - 1;
414 while (i >= 0 && buf[i] == '/')
416 while (i >= 0 && buf[i] != '/')
418 while (i >= 0 && buf[i] == '/')
423 /* Like the basename() function, but does not modify @path; it just returns a
425 const char *path_basename(const char *path)
427 const char *p = path;
432 /* Trailing slashes. */
441 while ((p != path - 1) && *p != '/')
448 * Returns a pointer to the part of @path following the first colon in the last
449 * path component, or NULL if the last path component does not contain a colon.
451 const char *path_stream_name(const char *path)
453 const char *base = path_basename(path);
454 const char *stream_name = strchr(base, ':');
458 return stream_name + 1;
462 * Splits a file path into the part before the first '/', or the entire name if
463 * there is no '/', and the part after the first sequence of '/' characters.
465 * @path: The file path to split.
466 * @first_part_len_ret: A pointer to a `size_t' into which the length of the
467 * first part of the path will be returned.
468 * @return: A pointer to the next part of the path, after the first
469 * sequence of '/', or a pointer to the terminating
470 * null byte in the case of a path without any '/'.
472 const char *path_next_part(const char *path, size_t *first_part_len_ret)
475 const char *next_part;
478 while (path[i] != '/' && path[i] != '\0')
480 if (first_part_len_ret)
481 *first_part_len_ret = i;
482 next_part = &path[i];
483 while (*next_part == '/')
488 /* Returns the number of components of @path. */
489 int get_num_path_components(const char *path)
491 int num_components = 0;
497 while (*path && *path != '/')
500 return num_components;
505 * Prints a string. Printable characters are printed as-is, while unprintable
506 * characters are printed as their octal escape codes.
508 void print_string(const void *string, size_t len)
510 const u8 *p = string;
516 printf("\\%03hho", *p);
521 u64 get_wim_timestamp()
524 gettimeofday(&tv, NULL);
525 return timeval_to_wim_timestamp(&tv);