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;
110 static const char *error_strings[] = {
113 [WIMLIB_ERR_ALREADY_LOCKED]
114 = "The WIM is already locked for writing",
115 [WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE]
116 = "Lookup table is compressed",
117 [WIMLIB_ERR_DECOMPRESSION]
118 = "Failed to decompress compressed data",
119 [WIMLIB_ERR_DELETE_STAGING_DIR]
120 = "Failed to delete staging directory",
122 = "Failed to fork another process",
124 = "An error was returned by fuse_main()",
125 [WIMLIB_ERR_FUSERMOUNT]
126 = "Could not execute the `fusermount' program, or it exited "
127 "with a failure status",
128 [WIMLIB_ERR_IMAGE_COUNT]
129 = "Inconsistent image count among the metadata "
130 "resources, the WIM header, and/or the XML data",
131 [WIMLIB_ERR_IMAGE_NAME_COLLISION]
132 = "Tried to add an image with a name that is already in use",
133 [WIMLIB_ERR_INTEGRITY]
134 = "The WIM failed an integrity check",
135 [WIMLIB_ERR_INVALID_CAPTURE_CONFIG]
136 = "The capture configuration string was invalid",
137 [WIMLIB_ERR_INVALID_CHUNK_SIZE]
138 = "The WIM is compressed but does not have a chunk "
140 [WIMLIB_ERR_INVALID_COMPRESSION_TYPE]
141 = "The WIM is compressed, but is not marked as having LZX or "
142 "XPRESS compression",
143 [WIMLIB_ERR_INVALID_DENTRY]
144 = "A directory entry in the WIM was invalid",
145 [WIMLIB_ERR_INVALID_HEADER_SIZE]
146 = "The WIM header was not 208 bytes",
147 [WIMLIB_ERR_INVALID_IMAGE]
148 = "Tried to select an image that does not exist in the WIM",
149 [WIMLIB_ERR_INVALID_INTEGRITY_TABLE]
150 = "The WIM's integrity table is invalid",
151 [WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY]
152 = "An entry in the WIM's lookup table is invalid",
153 [WIMLIB_ERR_INVALID_PARAM]
154 = "An invalid parameter was given",
155 [WIMLIB_ERR_INVALID_RESOURCE_HASH]
156 = "The SHA1 message digest of a WIM resource did not match the expected value",
157 [WIMLIB_ERR_INVALID_RESOURCE_SIZE]
158 = "A resource entry in the WIM has an invalid size",
160 = "Failed to create a hard or symbolic link when extracting "
161 "a file from the WIM",
163 = "Failed to create a directory",
165 = "Failed to create or use a POSIX message queue",
167 = "Ran out of memory",
169 = "Expected a directory",
170 [WIMLIB_ERR_NOT_A_WIM_FILE]
171 = "The file did not begin with the magic characters that "
172 "identify a WIM file",
173 [WIMLIB_ERR_NO_FILENAME]
174 = "The WIM is not identified with a filename",
176 = "NTFS-3g encountered an error (check errno)",
178 = "Failed to open a file",
180 = "Failed to open a directory",
182 = "Could not read data from a file",
183 [WIMLIB_ERR_READLINK]
184 = "Could not read the target of a symbolic link",
186 = "Could not rename a file",
188 = "Could not re-open the WIM after overwriting it",
189 [WIMLIB_ERR_RESOURCE_ORDER]
190 = "The components of the WIM were arranged in an unexpected order",
191 [WIMLIB_ERR_SPECIAL_FILE]
192 = "Encountered a special file that cannot be archived",
193 [WIMLIB_ERR_SPLIT_INVALID]
194 = "The WIM is part of an invalid split WIM",
195 [WIMLIB_ERR_SPLIT_UNSUPPORTED]
196 = "The WIM is part of a split WIM, which is not supported for this operation",
198 = "Could not read the metadata for a file or directory",
201 [WIMLIB_ERR_UNKNOWN_VERSION]
202 = "The WIM file is marked with an unknown version number",
203 [WIMLIB_ERR_UNSUPPORTED]
204 = "The requested operation is unsupported",
206 = "Failed to write data to a file",
208 = "The XML data of the WIM is invalid",
211 WIMLIBAPI const char *wimlib_get_error_string(enum wimlib_error_code code)
213 if (code < WIMLIB_ERR_SUCCESS || code > WIMLIB_ERR_XML)
216 return error_strings[code];
221 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
222 void *(*wimlib_malloc_func) (size_t) = malloc;
223 void (*wimlib_free_func) (void *) = free;
224 void *(*wimlib_realloc_func)(void *, size_t) = realloc;
226 void *wimlib_calloc(size_t nmemb, size_t size)
228 size_t total_size = nmemb * size;
229 void *p = MALLOC(total_size);
231 memset(p, 0, total_size);
235 char *wimlib_strdup(const char *str)
241 p = MALLOC(size + 1);
243 memcpy(p, str, size + 1);
247 extern void xml_set_memory_allocator(void *(*malloc_func)(size_t),
248 void (*free_func)(void *),
249 void *(*realloc_func)(void *, size_t));
252 WIMLIBAPI int wimlib_set_memory_allocator(void *(*malloc_func)(size_t),
253 void (*free_func)(void *),
254 void *(*realloc_func)(void *, size_t))
256 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
257 wimlib_malloc_func = malloc_func ? malloc_func : malloc;
258 wimlib_free_func = free_func ? free_func : free;
259 wimlib_realloc_func = realloc_func ? realloc_func : realloc;
261 xml_set_memory_allocator(wimlib_malloc_func, wimlib_free_func,
262 wimlib_realloc_func);
265 ERROR("Cannot set custom memory allocator functions:");
266 ERROR("wimlib was compiled with the --without-custom-memory-allocator "
268 return WIMLIB_ERR_UNSUPPORTED;
274 static iconv_t cd_utf16_to_utf8 = (iconv_t)(-1);
276 /* Converts a string in the UTF-16 encoding to a newly allocated string in the
278 char *utf16_to_utf8(const char *utf16_str, size_t utf16_len,
279 size_t *utf8_len_ret)
281 if (cd_utf16_to_utf8 == (iconv_t)(-1)) {
282 cd_utf16_to_utf8 = iconv_open("UTF-8", "UTF-16LE");
283 if (cd_utf16_to_utf8 == (iconv_t)-1) {
284 ERROR_WITH_ERRNO("Failed to get conversion descriptor "
285 "for converting UTF-16LE to UTF-8");
289 size_t utf16_bytes_left = utf16_len;
290 size_t utf8_bytes_left = utf16_len;
292 char *utf8_str = MALLOC(utf8_bytes_left);
296 char *orig_utf8_str = utf8_str;
298 size_t num_chars_converted = iconv(cd_utf16_to_utf8, (char**)&utf16_str,
299 &utf16_bytes_left, &utf8_str, &utf8_bytes_left);
301 if (num_chars_converted == (size_t)(-1)) {
302 ERROR_WITH_ERRNO("Failed to convert UTF-16LE string to UTF-8 "
308 size_t utf8_len = utf16_len - utf8_bytes_left;
310 *utf8_len_ret = utf8_len;
311 orig_utf8_str[utf8_len] = '\0';
312 return orig_utf8_str;
315 static iconv_t cd_utf8_to_utf16 = (iconv_t)(-1);
317 /* Converts a string in the UTF-8 encoding to a newly allocated string in the
318 * UTF-16 encoding. */
319 char *utf8_to_utf16(const char *utf8_str, size_t utf8_len,
320 size_t *utf16_len_ret)
322 if (cd_utf8_to_utf16 == (iconv_t)(-1)) {
323 cd_utf8_to_utf16 = iconv_open("UTF-16LE", "UTF-8");
324 if (cd_utf8_to_utf16 == (iconv_t)-1) {
325 ERROR_WITH_ERRNO("Failed to get conversion descriptor "
326 "for converting UTF-8 to UTF-16LE");
331 size_t utf8_bytes_left = utf8_len;
332 size_t utf16_capacity = utf8_len * 4;
333 size_t utf16_bytes_left = utf16_capacity;
335 char *utf16_str = MALLOC(utf16_capacity + 2);
339 char *orig_utf16_str = utf16_str;
341 size_t num_chars_converted = iconv(cd_utf8_to_utf16, (char**)&utf8_str,
342 &utf8_bytes_left, &utf16_str, &utf16_bytes_left);
344 if (num_chars_converted == (size_t)(-1)) {
345 ERROR_WITH_ERRNO("Failed to convert UTF-8 string to UTF-16LE "
347 FREE(orig_utf16_str);
351 size_t utf16_len = utf16_capacity - utf16_bytes_left;
353 *utf16_len_ret = utf16_len;
354 orig_utf16_str[utf16_len] = '\0';
355 orig_utf16_str[utf16_len + 1] = '\0';
356 return orig_utf16_str;
359 /* Write @n bytes from @buf to the file descriptor @fd, retrying on interupt and
362 * Returns short count and set errno on failure. */
363 ssize_t full_write(int fd, const void *buf, size_t n)
370 ret = write(fd, p, n);
384 static bool seeded = false;
386 static void seed_random()
388 srand(time(NULL) * getpid());
392 /* Fills @n bytes pointed to by @p with random alphanumeric characters. */
393 void randomize_char_array_with_alnum(char p[], size_t n)
408 /* Fills @n bytes pointer to by @p with random numbers. */
409 void randomize_byte_array(u8 *p, size_t n)
417 /* Takes in a path of length @len in @buf, and transforms it into a string for
418 * the path of its parent directory. */
419 void to_parent_name(char buf[], size_t len)
421 ssize_t i = (ssize_t)len - 1;
422 while (i >= 0 && buf[i] == '/')
424 while (i >= 0 && buf[i] != '/')
426 while (i >= 0 && buf[i] == '/')
431 /* Like the basename() function, but does not modify @path; it just returns a
433 const char *path_basename(const char *path)
435 const char *p = path;
440 /* Trailing slashes. */
449 while ((p != path - 1) && *p != '/')
456 * Returns a pointer to the part of @path following the first colon in the last
457 * path component, or NULL if the last path component does not contain a colon.
459 const char *path_stream_name(const char *path)
461 const char *base = path_basename(path);
462 const char *stream_name = strchr(base, ':');
466 return stream_name + 1;
470 * Splits a file path into the part before the first '/', or the entire name if
471 * there is no '/', and the part after the first sequence of '/' characters.
473 * @path: The file path to split.
474 * @first_part_len_ret: A pointer to a `size_t' into which the length of the
475 * first part of the path will be returned.
476 * @return: A pointer to the next part of the path, after the first
477 * sequence of '/', or a pointer to the terminating
478 * null byte in the case of a path without any '/'.
480 const char *path_next_part(const char *path, size_t *first_part_len_ret)
483 const char *next_part;
486 while (path[i] != '/' && path[i] != '\0')
488 if (first_part_len_ret)
489 *first_part_len_ret = i;
490 next_part = &path[i];
491 while (*next_part == '/')
496 /* Returns the number of components of @path. */
497 int get_num_path_components(const char *path)
499 int num_components = 0;
505 while (*path && *path != '/')
508 return num_components;
513 * Prints a string. Printable characters are printed as-is, while unprintable
514 * characters are printed as their octal escape codes.
516 void print_string(const void *string, size_t len)
518 const u8 *p = string;
524 printf("\\%03hho", *p);
529 u64 get_wim_timestamp()
532 gettimeofday(&tv, NULL);
533 return timeval_to_wim_timestamp(&tv);