6 * Copyright (C) 2010 Carl Thijssen
7 * Copyright (C) 2012 Eric Biggers
9 * This file is part of wimlib, a library for working with WIM files.
11 * wimlib is free software; you can redistribute it and/or modify it under the
12 * terms of the GNU Lesser General Public License as published by the Free
13 * Software Foundation; either version 2.1 of the License, or (at your option)
16 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
17 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with wimlib; if not, see http://www.gnu.org/licenses/.
25 #include "wimlib_internal.h"
26 #include "endianness.h"
28 #include "timestamp.h"
40 /* True if wimlib is to print an informational message when an error occurs.
41 * This can be turned off by calling wimlib_set_print_errors(false). */
42 #ifdef ENABLE_ERROR_MESSAGES
44 bool __wimlib_print_errors = false;
46 void wimlib_error(const char *format, ...)
48 if (__wimlib_print_errors) {
54 fputs("[ERROR] ", stderr);
55 vfprintf(stderr, format, va);
62 void wimlib_error_with_errno(const char *format, ...)
64 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) {
86 fputs("[WARNING] ", stderr);
87 vfprintf(stderr, format, va);
96 WIMLIBAPI int wimlib_set_print_errors(bool show_error_messages)
98 #ifdef ENABLE_ERROR_MESSAGES
99 __wimlib_print_errors = show_error_messages;
102 if (show_error_messages)
103 return WIMLIB_ERR_UNSUPPORTED;
107 static const char *error_strings[] = {
110 [WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE]
111 = "Lookup table is compressed",
112 [WIMLIB_ERR_DECOMPRESSION]
113 = "Failed to decompress compressed data",
114 [WIMLIB_ERR_DELETE_STAGING_DIR]
115 = "Failed to delete staging directory",
117 = "Failed to fork another process",
119 = "An error was returned by fuse_main()",
120 [WIMLIB_ERR_FUSERMOUNT]
121 = "Could not execute the `fusermount' program, or it exited "
122 "with a failure status",
123 [WIMLIB_ERR_IMAGE_COUNT]
124 = "Inconsistent image count among the metadata "
125 "resources, the WIM header, and/or the XML data",
126 [WIMLIB_ERR_IMAGE_NAME_COLLISION]
127 = "Tried to add an image with a name that is already in use",
128 [WIMLIB_ERR_INTEGRITY]
129 = "The WIM failed an integrity check",
130 [WIMLIB_ERR_INVALID_CHUNK_SIZE]
131 = "The WIM is compressed but does not have a chunk "
133 [WIMLIB_ERR_INVALID_COMPRESSION_TYPE]
134 = "The WIM is compressed, but is not marked as having LZX or "
135 "XPRESS compression",
136 [WIMLIB_ERR_INVALID_DENTRY]
137 = "A directory entry in the WIM was invalid",
138 [WIMLIB_ERR_INVALID_HEADER_SIZE]
139 = "The WIM header was not 208 bytes",
140 [WIMLIB_ERR_INVALID_IMAGE]
141 = "Tried to select an image that does not exist in the WIM",
142 [WIMLIB_ERR_INVALID_INTEGRITY_TABLE]
143 = "The WIM's integrity table is invalid",
144 [WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY]
145 = "An entry in the WIM's lookup table is invalid",
146 [WIMLIB_ERR_INVALID_PARAM]
147 = "An invalid parameter was given",
148 [WIMLIB_ERR_INVALID_RESOURCE_HASH]
149 = "The SHA1 message digest of a WIM resource did not match the expected value",
150 [WIMLIB_ERR_INVALID_RESOURCE_SIZE]
151 = "A resource entry in the WIM is invalid",
153 = "Failed to create a hard or symbolic link when extracting "
154 "a file from the WIM",
156 = "Failed to create a directory",
158 = "Failed to create or use a POSIX message queue",
160 = "Ran out of memory",
162 = "Expected a directory",
163 [WIMLIB_ERR_NOT_A_WIM_FILE]
164 = "The file did not begin with the magic characters that "
165 "identify a WIM file",
166 [WIMLIB_ERR_NO_FILENAME]
167 = "The WIM is not identified with a filename",
168 [WIMLIB_ERR_NOT_ROOT]
169 = "Root privileges are required for this operation",
171 = "NTFS-3g encountered an error (check errno)",
173 = "Failed to open a file",
175 = "Failed to open a directory",
177 = "Could not read data from a file",
178 [WIMLIB_ERR_READLINK]
179 = "Could not read the target of a symbolic link",
181 = "Could not rename a file",
182 [WIMLIB_ERR_SPECIAL_FILE]
183 = "Encountered a special file that cannot be archived",
184 [WIMLIB_ERR_SPLIT_INVALID]
185 = "The WIM is part of an invalid split WIM",
186 [WIMLIB_ERR_SPLIT_UNSUPPORTED]
187 = "The WIM is part of a split WIM, which is not supported for this operation",
189 = "Could not read the metadata for a file or directory",
192 [WIMLIB_ERR_UNKNOWN_VERSION]
193 = "The WIM file is marked with an unknown version number",
194 [WIMLIB_ERR_UNSUPPORTED]
195 = "The requested operation is unsupported",
197 = "Failed to write data to a file",
199 = "The XML data of the WIM is invalid",
202 WIMLIBAPI const char *wimlib_get_error_string(enum wimlib_error_code code)
204 if (code < WIMLIB_ERR_SUCCESS || code > WIMLIB_ERR_XML)
207 return error_strings[code];
212 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
213 void *(*wimlib_malloc_func) (size_t) = malloc;
214 void (*wimlib_free_func) (void *) = free;
215 void *(*wimlib_realloc_func)(void *, size_t) = realloc;
217 void *wimlib_calloc(size_t nmemb, size_t size)
219 size_t total_size = nmemb * size;
220 void *p = MALLOC(total_size);
222 memset(p, 0, total_size);
226 char *wimlib_strdup(const char *str)
232 p = MALLOC(size + 1);
234 memcpy(p, str, size + 1);
238 extern void xml_set_memory_allocator(void *(*malloc_func)(size_t),
239 void (*free_func)(void *),
240 void *(*realloc_func)(void *, size_t));
243 WIMLIBAPI int wimlib_set_memory_allocator(void *(*malloc_func)(size_t),
244 void (*free_func)(void *),
245 void *(*realloc_func)(void *, size_t))
247 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
248 wimlib_malloc_func = malloc_func ? malloc_func : malloc;
249 wimlib_free_func = free_func ? free_func : free;
250 wimlib_realloc_func = realloc_func ? realloc_func : realloc;
252 xml_set_memory_allocator(wimlib_malloc_func, wimlib_free_func,
253 wimlib_realloc_func);
256 ERROR("Cannot set custom memory allocator functions:");
257 ERROR("wimlib was compiled with the --without-custom-memory-allocator "
259 return WIMLIB_ERR_UNSUPPORTED;
265 static iconv_t cd_utf16_to_utf8 = (iconv_t)(-1);
267 /* Converts a string in the UTF-16 encoding to a newly allocated string in the
269 char *utf16_to_utf8(const char *utf16_str, size_t utf16_len,
270 size_t *utf8_len_ret)
272 if (cd_utf16_to_utf8 == (iconv_t)(-1)) {
273 cd_utf16_to_utf8 = iconv_open("UTF-8", "UTF-16LE");
274 if (cd_utf16_to_utf8 == (iconv_t)-1) {
275 ERROR_WITH_ERRNO("Failed to get conversion descriptor "
276 "for converting UTF-16LE to UTF-8");
280 size_t utf16_bytes_left = utf16_len;
281 size_t utf8_bytes_left = utf16_len;
283 char *utf8_str = MALLOC(utf8_bytes_left);
287 char *orig_utf8_str = utf8_str;
289 size_t num_chars_converted = iconv(cd_utf16_to_utf8, (char**)&utf16_str,
290 &utf16_bytes_left, &utf8_str, &utf8_bytes_left);
292 if (num_chars_converted == (size_t)(-1)) {
293 ERROR_WITH_ERRNO("Failed to convert UTF-16LE string to UTF-8 "
299 size_t utf8_len = utf16_len - utf8_bytes_left;
301 *utf8_len_ret = utf8_len;
302 orig_utf8_str[utf8_len] = '\0';
303 return orig_utf8_str;
306 static iconv_t cd_utf8_to_utf16 = (iconv_t)(-1);
308 /* Converts a string in the UTF-8 encoding to a newly allocated string in the
309 * UTF-16 encoding. */
310 char *utf8_to_utf16(const char *utf8_str, size_t utf8_len,
311 size_t *utf16_len_ret)
313 if (cd_utf8_to_utf16 == (iconv_t)(-1)) {
314 cd_utf8_to_utf16 = iconv_open("UTF-16LE", "UTF-8");
315 if (cd_utf8_to_utf16 == (iconv_t)-1) {
316 ERROR_WITH_ERRNO("Failed to get conversion descriptor "
317 "for converting UTF-8 to UTF-16LE");
322 size_t utf8_bytes_left = utf8_len;
323 size_t utf16_capacity = utf8_len * 4;
324 size_t utf16_bytes_left = utf16_capacity;
326 char *utf16_str = MALLOC(utf16_capacity + 2);
330 char *orig_utf16_str = utf16_str;
332 size_t num_chars_converted = iconv(cd_utf8_to_utf16, (char**)&utf8_str,
333 &utf8_bytes_left, &utf16_str, &utf16_bytes_left);
335 if (num_chars_converted == (size_t)(-1)) {
336 ERROR_WITH_ERRNO("Failed to convert UTF-8 string to UTF-16LE "
338 FREE(orig_utf16_str);
342 size_t utf16_len = utf16_capacity - utf16_bytes_left;
344 *utf16_len_ret = utf16_len;
345 orig_utf16_str[utf16_len] = '\0';
346 orig_utf16_str[utf16_len + 1] = '\0';
347 return orig_utf16_str;
350 /* Write @n bytes from @buf to the file descriptor @fd, retrying on interupt and
353 * Returns short count and set errno on failure. */
354 ssize_t full_write(int fd, const void *buf, size_t n)
361 ret = write(fd, p, n);
375 static bool seeded = false;
377 /* Fills @n bytes pointed to by @p with random alphanumeric characters. */
378 void randomize_char_array_with_alnum(char p[], size_t n)
397 /* Fills @n bytes pointer to by @p with random numbers. */
398 void randomize_byte_array(u8 *p, size_t n)
408 /* Takes in a path of length @len in @buf, and transforms it into a string for
409 * the path of its parent directory. */
410 void to_parent_name(char buf[], size_t len)
412 ssize_t i = (ssize_t)len - 1;
413 while (i >= 0 && buf[i] == '/')
415 while (i >= 0 && buf[i] != '/')
417 while (i >= 0 && buf[i] == '/')
422 /* Like the basename() function, but does not modify @path; it just returns a
424 const char *path_basename(const char *path)
426 const char *p = path;
431 /* Trailing slashes. */
432 while ((p != path - 1) && *p == '/')
435 while ((p != path - 1) && *p != '/')
442 * Returns a pointer to the part of @path following the first colon in the last
443 * path component, or NULL if the last path component does not contain a colon.
445 const char *path_stream_name(const char *path)
447 const char *base = path_basename(path);
448 const char *stream_name = strchr(base, ':');
452 return stream_name + 1;
456 * Splits a file path into the part before the first '/', or the entire name if
457 * there is no '/', and the part after the first sequence of '/' characters.
459 * @path: The file path to split.
460 * @first_part_len_ret: A pointer to a `size_t' into which the length of the
461 * first part of the path will be returned.
462 * @return: A pointer to the next part of the path, after the first
463 * sequence of '/', or a pointer to the terminating
464 * null byte in the case of a path without any '/'.
466 const char *path_next_part(const char *path, size_t *first_part_len_ret)
469 const char *next_part;
472 while (path[i] != '/' && path[i] != '\0')
474 if (first_part_len_ret)
475 *first_part_len_ret = i;
476 next_part = &path[i];
477 while (*next_part == '/')
482 /* Returns the number of components of @path. */
483 int get_num_path_components(const char *path)
485 int num_components = 0;
491 while (*path && *path != '/')
494 return num_components;
499 * Prints a string. Printable characters are printed as-is, while unprintable
500 * characters are printed as their octal escape codes.
502 void print_string(const void *string, size_t len)
504 const u8 *p = string;
510 printf("\\%03hho", *p);
515 u64 get_wim_timestamp()
518 gettimeofday(&tv, NULL);
519 return timeval_to_wim_timestamp(&tv);