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/.
25 /* Make sure the POSIX-compatible strerror_r() is declared, rather than the GNU
26 * version, which has a different return type. */
27 #define _POSIX_C_SOURCE 200112
31 #include "wimlib_internal.h"
32 #include "endianness.h"
33 #include "timestamp.h"
39 #include <unistd.h> /* for getpid() */
41 /* True if wimlib is to print an informational message when an error occurs.
42 * This can be turned off by calling wimlib_set_print_errors(false). */
43 #ifdef ENABLE_ERROR_MESSAGES
45 static bool wimlib_print_errors = false;
47 static void wimlib_vmsg(const char *tag, const char *format,
48 va_list va, bool perror)
50 if (wimlib_print_errors) {
51 int errno_save = errno;
54 vfprintf(stderr, format, va);
55 if (perror && errno_save != 0) {
57 int res = strerror_r(errno_save, buf, sizeof(buf));
59 snprintf(buf, sizeof(buf),
60 "unknown error (errno=%d)", errno_save);
62 fprintf(stderr, ": %s", buf);
69 void wimlib_error(const char *format, ...)
74 wimlib_vmsg("[ERROR] ", format, va, false);
78 void wimlib_error_with_errno(const char *format, ...)
83 wimlib_vmsg("[ERROR] ", format, va, true);
87 void wimlib_warning(const char *format, ...)
92 wimlib_vmsg("[WARNING] ", format, va, false);
96 void wimlib_warning_with_errno(const char *format, ...)
100 va_start(va, format);
101 wimlib_vmsg("[WARNING] ", format, va, true);
107 WIMLIBAPI int wimlib_set_print_errors(bool show_error_messages)
109 #ifdef ENABLE_ERROR_MESSAGES
110 wimlib_print_errors = show_error_messages;
113 if (show_error_messages)
114 return WIMLIB_ERR_UNSUPPORTED;
120 static const char *error_strings[] = {
123 [WIMLIB_ERR_ALREADY_LOCKED]
124 = "The WIM is already locked for writing",
125 [WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE]
126 = "Lookup table is compressed",
127 [WIMLIB_ERR_DECOMPRESSION]
128 = "Failed to decompress compressed data",
129 [WIMLIB_ERR_DELETE_STAGING_DIR]
130 = "Failed to delete staging directory",
131 [WIMLIB_ERR_FILESYSTEM_DAEMON_CRASHED]
132 = "The process servicing the mounted WIM has crashed",
134 = "Failed to fork another process",
136 = "An error was returned by fuse_main()",
137 [WIMLIB_ERR_FUSERMOUNT]
138 = "Could not execute the `fusermount' program, or it exited "
139 "with a failure status",
140 [WIMLIB_ERR_ICONV_NOT_AVAILABLE]
141 = "The iconv() function does not seem to work. "
142 "Maybe check to make sure the directory /usr/lib/gconv exists",
143 [WIMLIB_ERR_IMAGE_COUNT]
144 = "Inconsistent image count among the metadata "
145 "resources, the WIM header, and/or the XML data",
146 [WIMLIB_ERR_IMAGE_NAME_COLLISION]
147 = "Tried to add an image with a name that is already in use",
148 [WIMLIB_ERR_INTEGRITY]
149 = "The WIM failed an integrity check",
150 [WIMLIB_ERR_INVALID_CAPTURE_CONFIG]
151 = "The capture configuration string was invalid",
152 [WIMLIB_ERR_INVALID_CHUNK_SIZE]
153 = "The WIM is compressed but does not have a chunk "
155 [WIMLIB_ERR_INVALID_COMPRESSION_TYPE]
156 = "The WIM is compressed, but is not marked as having LZX or "
157 "XPRESS compression",
158 [WIMLIB_ERR_INVALID_DENTRY]
159 = "A directory entry in the WIM was invalid",
160 [WIMLIB_ERR_INVALID_HEADER_SIZE]
161 = "The WIM header was not 208 bytes",
162 [WIMLIB_ERR_INVALID_IMAGE]
163 = "Tried to select an image that does not exist in the WIM",
164 [WIMLIB_ERR_INVALID_INTEGRITY_TABLE]
165 = "The WIM's integrity table is invalid",
166 [WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY]
167 = "An entry in the WIM's lookup table is invalid",
168 [WIMLIB_ERR_INVALID_OVERLAY]
169 = "Conflicting files in overlay when creating a WIM image",
170 [WIMLIB_ERR_INVALID_PARAM]
171 = "An invalid parameter was given",
172 [WIMLIB_ERR_INVALID_PART_NUMBER]
173 = "The part number or total parts of the WIM is invalid",
174 [WIMLIB_ERR_INVALID_RESOURCE_HASH]
175 = "The SHA1 message digest of a WIM resource did not match the expected value",
176 [WIMLIB_ERR_INVALID_RESOURCE_SIZE]
177 = "A resource entry in the WIM has an invalid size",
178 [WIMLIB_ERR_INVALID_SECURITY_DATA]
179 = "The table of security descriptors in the WIM is invalid",
180 [WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE]
181 = "The version of wimlib that has mounted a WIM image is incompatible with the "
182 "version being used to unmount it",
183 [WIMLIB_ERR_INVALID_UTF8_STRING]
184 = "A string provided as input by the user was not a valid UTF-8 string",
185 [WIMLIB_ERR_INVALID_UTF16_STRING]
186 = "A string in a WIM dentry is not a valid UTF-16LE string",
187 [WIMLIB_ERR_LIBXML_UTF16_HANDLER_NOT_AVAILABLE]
188 = "libxml2 was unable to find a character encoding conversion handler "
191 = "Failed to create a hard or symbolic link when extracting "
192 "a file from the WIM",
194 = "Failed to create a directory",
196 = "Failed to create or use a POSIX message queue",
198 = "Ran out of memory",
200 = "Expected a directory",
201 [WIMLIB_ERR_NOT_A_WIM_FILE]
202 = "The file did not begin with the magic characters that "
203 "identify a WIM file",
204 [WIMLIB_ERR_NO_FILENAME]
205 = "The WIM is not identified with a filename",
207 = "NTFS-3g encountered an error (check errno)",
209 = "Failed to open a file",
211 = "Failed to open a directory",
213 = "Could not read data from a file",
214 [WIMLIB_ERR_READLINK]
215 = "Could not read the target of a symbolic link",
217 = "Could not rename a file",
219 = "Could not re-open the WIM after overwriting it",
220 [WIMLIB_ERR_RESOURCE_ORDER]
221 = "The components of the WIM were arranged in an unexpected order",
222 [WIMLIB_ERR_SPECIAL_FILE]
223 = "Encountered a special file that cannot be archived",
224 [WIMLIB_ERR_SPLIT_INVALID]
225 = "The WIM is part of an invalid split WIM",
226 [WIMLIB_ERR_SPLIT_UNSUPPORTED]
227 = "The WIM is part of a split WIM, which is not supported for this operation",
229 = "Could not read the metadata for a file or directory",
231 = "Timed out while waiting for a message to arrive from another process",
232 [WIMLIB_ERR_UNKNOWN_VERSION]
233 = "The WIM file is marked with an unknown version number",
234 [WIMLIB_ERR_UNSUPPORTED]
235 = "The requested operation is unsupported",
237 = "Failed to write data to a file",
239 = "The XML data of the WIM is invalid",
242 WIMLIBAPI const char *wimlib_get_error_string(enum wimlib_error_code code)
244 if (code < WIMLIB_ERR_SUCCESS || code > WIMLIB_ERR_XML)
247 return error_strings[code];
252 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
253 void *(*wimlib_malloc_func) (size_t) = malloc;
254 void (*wimlib_free_func) (void *) = free;
255 void *(*wimlib_realloc_func)(void *, size_t) = realloc;
257 void *wimlib_calloc(size_t nmemb, size_t size)
259 size_t total_size = nmemb * size;
260 void *p = MALLOC(total_size);
262 memset(p, 0, total_size);
266 char *wimlib_strdup(const char *str)
272 p = MALLOC(size + 1);
274 memcpy(p, str, size + 1);
278 extern void xml_set_memory_allocator(void *(*malloc_func)(size_t),
279 void (*free_func)(void *),
280 void *(*realloc_func)(void *, size_t));
283 WIMLIBAPI int wimlib_set_memory_allocator(void *(*malloc_func)(size_t),
284 void (*free_func)(void *),
285 void *(*realloc_func)(void *, size_t))
287 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
288 wimlib_malloc_func = malloc_func ? malloc_func : malloc;
289 wimlib_free_func = free_func ? free_func : free;
290 wimlib_realloc_func = realloc_func ? realloc_func : realloc;
292 xml_set_memory_allocator(wimlib_malloc_func, wimlib_free_func,
293 wimlib_realloc_func);
296 ERROR("Cannot set custom memory allocator functions:");
297 ERROR("wimlib was compiled with the --without-custom-memory-allocator "
299 return WIMLIB_ERR_UNSUPPORTED;
303 static bool seeded = false;
305 static void seed_random()
307 srand(time(NULL) * getpid());
311 /* Fills @n bytes pointed to by @p with random alphanumeric characters. */
312 void randomize_char_array_with_alnum(char p[], size_t n)
327 /* Fills @n bytes pointer to by @p with random numbers. */
328 void randomize_byte_array(u8 *p, size_t n)
336 /* Takes in a path of length @len in @buf, and transforms it into a string for
337 * the path of its parent directory. */
338 void to_parent_name(char buf[], size_t len)
340 ssize_t i = (ssize_t)len - 1;
341 while (i >= 0 && buf[i] == '/')
343 while (i >= 0 && buf[i] != '/')
345 while (i >= 0 && buf[i] == '/')
350 /* Like the basename() function, but does not modify @path; it just returns a
352 const char *path_basename(const char *path)
354 const char *p = path;
359 /* Trailing slashes. */
368 while ((p != path - 1) && *p != '/')
375 * Returns a pointer to the part of @path following the first colon in the last
376 * path component, or NULL if the last path component does not contain a colon.
378 const char *path_stream_name(const char *path)
380 const char *base = path_basename(path);
381 const char *stream_name = strchr(base, ':');
385 return stream_name + 1;
389 * Splits a file path into the part before the first '/', or the entire name if
390 * there is no '/', and the part after the first sequence of '/' characters.
392 * @path: The file path to split.
393 * @first_part_len_ret: A pointer to a `size_t' into which the length of the
394 * first part of the path will be returned.
395 * @return: A pointer to the next part of the path, after the first
396 * sequence of '/', or a pointer to the terminating
397 * null byte in the case of a path without any '/'.
399 const char *path_next_part(const char *path, size_t *first_part_len_ret)
402 const char *next_part;
405 while (path[i] != '/' && path[i] != '\0')
407 if (first_part_len_ret)
408 *first_part_len_ret = i;
409 next_part = &path[i];
410 while (*next_part == '/')
415 /* Returns the number of components of @path. */
416 int get_num_path_components(const char *path)
418 int num_components = 0;
424 while (*path && *path != '/')
427 return num_components;
432 * Prints a string. Printable characters are printed as-is, while unprintable
433 * characters are printed as their octal escape codes.
435 void print_string(const void *string, size_t len)
437 const u8 *p = string;
443 printf("\\%03hho", *p);
448 u64 get_wim_timestamp()
451 gettimeofday(&tv, NULL);
452 return timeval_to_wim_timestamp(&tv);
455 void wim_timestamp_to_str(u64 timestamp, char *buf, size_t len)
458 time_t t = wim_timestamp_to_unix(timestamp);
460 strftime(buf, len, "%a %b %d %H:%M:%S %Y UTC", &tm);