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"
26 #include "timestamp.h"
32 #include <unistd.h> /* for getpid() */
34 /* True if wimlib is to print an informational message when an error occurs.
35 * This can be turned off by calling wimlib_set_print_errors(false). */
36 #ifdef ENABLE_ERROR_MESSAGES
38 bool __wimlib_print_errors = false;
40 void wimlib_error(const char *format, ...)
42 if (__wimlib_print_errors) {
48 fputs("[ERROR] ", stderr);
49 vfprintf(stderr, format, va);
56 void wimlib_error_with_errno(const char *format, ...)
58 if (__wimlib_print_errors) {
65 fputs("[ERROR] ", stderr);
66 vfprintf(stderr, format, va);
68 fprintf(stderr, ": %s", strerror(errno_save));
75 void wimlib_warning(const char *format, ...)
77 if (__wimlib_print_errors) {
84 fputs("[WARNING] ", stderr);
85 vfprintf(stderr, format, va);
94 WIMLIBAPI int wimlib_set_print_errors(bool show_error_messages)
96 #ifdef ENABLE_ERROR_MESSAGES
97 __wimlib_print_errors = show_error_messages;
100 if (show_error_messages)
101 return WIMLIB_ERR_UNSUPPORTED;
107 static const char *error_strings[] = {
110 [WIMLIB_ERR_ALREADY_LOCKED]
111 = "The WIM is already locked for writing",
112 [WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE]
113 = "Lookup table is compressed",
114 [WIMLIB_ERR_DECOMPRESSION]
115 = "Failed to decompress compressed data",
116 [WIMLIB_ERR_DELETE_STAGING_DIR]
117 = "Failed to delete staging directory",
118 [WIMLIB_ERR_FILESYSTEM_DAEMON_CRASHED]
119 = "The process servicing the mounted WIM has crashed",
121 = "Failed to fork another process",
123 = "An error was returned by fuse_main()",
124 [WIMLIB_ERR_FUSERMOUNT]
125 = "Could not execute the `fusermount' program, or it exited "
126 "with a failure status",
127 [WIMLIB_ERR_IMAGE_COUNT]
128 = "Inconsistent image count among the metadata "
129 "resources, the WIM header, and/or the XML data",
130 [WIMLIB_ERR_IMAGE_NAME_COLLISION]
131 = "Tried to add an image with a name that is already in use",
132 [WIMLIB_ERR_INTEGRITY]
133 = "The WIM failed an integrity check",
134 [WIMLIB_ERR_INVALID_CAPTURE_CONFIG]
135 = "The capture configuration string was invalid",
136 [WIMLIB_ERR_INVALID_CHUNK_SIZE]
137 = "The WIM is compressed but does not have a chunk "
139 [WIMLIB_ERR_INVALID_COMPRESSION_TYPE]
140 = "The WIM is compressed, but is not marked as having LZX or "
141 "XPRESS compression",
142 [WIMLIB_ERR_INVALID_DENTRY]
143 = "A directory entry in the WIM was invalid",
144 [WIMLIB_ERR_INVALID_HEADER_SIZE]
145 = "The WIM header was not 208 bytes",
146 [WIMLIB_ERR_INVALID_IMAGE]
147 = "Tried to select an image that does not exist in the WIM",
148 [WIMLIB_ERR_INVALID_INTEGRITY_TABLE]
149 = "The WIM's integrity table is invalid",
150 [WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY]
151 = "An entry in the WIM's lookup table is invalid",
152 [WIMLIB_ERR_INVALID_PARAM]
153 = "An invalid parameter was given",
154 [WIMLIB_ERR_INVALID_PART_NUMBER]
155 = "The part number or total parts of the WIM is invalid",
156 [WIMLIB_ERR_INVALID_RESOURCE_HASH]
157 = "The SHA1 message digest of a WIM resource did not match the expected value",
158 [WIMLIB_ERR_ICONV_NOT_AVAILABLE]
159 = "The iconv() function does not seem to work. "
160 "Maybe check to make sure the directory /usr/lib/gconv exists",
161 [WIMLIB_ERR_INVALID_RESOURCE_SIZE]
162 = "A resource entry in the WIM has an invalid size",
163 [WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE]
164 = "The version of wimlib that has mounted a WIM image is incompatible with the "
165 "version being used to unmount it",
166 [WIMLIB_ERR_INVALID_UTF8_STRING]
167 = "A string provided as input by the user was not a valid UTF-8 string",
168 [WIMLIB_ERR_INVALID_UTF16_STRING]
169 = "A string in a WIM dentry is not a valid UTF-16LE string",
170 [WIMLIB_ERR_LIBXML_UTF16_HANDLER_NOT_AVAILABLE]
171 = "libxml2 was unable to find a character encoding conversion handler "
174 = "Failed to create a hard or symbolic link when extracting "
175 "a file from the WIM",
177 = "Failed to create a directory",
179 = "Failed to create or use a POSIX message queue",
181 = "Ran out of memory",
183 = "Expected a directory",
184 [WIMLIB_ERR_NOT_A_WIM_FILE]
185 = "The file did not begin with the magic characters that "
186 "identify a WIM file",
187 [WIMLIB_ERR_NO_FILENAME]
188 = "The WIM is not identified with a filename",
190 = "NTFS-3g encountered an error (check errno)",
192 = "Failed to open a file",
194 = "Failed to open a directory",
196 = "Could not read data from a file",
197 [WIMLIB_ERR_READLINK]
198 = "Could not read the target of a symbolic link",
200 = "Could not rename a file",
202 = "Could not re-open the WIM after overwriting it",
203 [WIMLIB_ERR_RESOURCE_ORDER]
204 = "The components of the WIM were arranged in an unexpected order",
205 [WIMLIB_ERR_SPECIAL_FILE]
206 = "Encountered a special file that cannot be archived",
207 [WIMLIB_ERR_SPLIT_INVALID]
208 = "The WIM is part of an invalid split WIM",
209 [WIMLIB_ERR_SPLIT_UNSUPPORTED]
210 = "The WIM is part of a split WIM, which is not supported for this operation",
212 = "Could not read the metadata for a file or directory",
213 [WIMLIB_ERR_UNKNOWN_VERSION]
214 = "The WIM file is marked with an unknown version number",
215 [WIMLIB_ERR_UNSUPPORTED]
216 = "The requested operation is unsupported",
218 = "Failed to write data to a file",
220 = "The XML data of the WIM is invalid",
223 WIMLIBAPI const char *wimlib_get_error_string(enum wimlib_error_code code)
225 if (code < WIMLIB_ERR_SUCCESS || code > WIMLIB_ERR_XML)
228 return error_strings[code];
233 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
234 void *(*wimlib_malloc_func) (size_t) = malloc;
235 void (*wimlib_free_func) (void *) = free;
236 void *(*wimlib_realloc_func)(void *, size_t) = realloc;
238 void *wimlib_calloc(size_t nmemb, size_t size)
240 size_t total_size = nmemb * size;
241 void *p = MALLOC(total_size);
243 memset(p, 0, total_size);
247 char *wimlib_strdup(const char *str)
253 p = MALLOC(size + 1);
255 memcpy(p, str, size + 1);
259 extern void xml_set_memory_allocator(void *(*malloc_func)(size_t),
260 void (*free_func)(void *),
261 void *(*realloc_func)(void *, size_t));
264 WIMLIBAPI int wimlib_set_memory_allocator(void *(*malloc_func)(size_t),
265 void (*free_func)(void *),
266 void *(*realloc_func)(void *, size_t))
268 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
269 wimlib_malloc_func = malloc_func ? malloc_func : malloc;
270 wimlib_free_func = free_func ? free_func : free;
271 wimlib_realloc_func = realloc_func ? realloc_func : realloc;
273 xml_set_memory_allocator(wimlib_malloc_func, wimlib_free_func,
274 wimlib_realloc_func);
277 ERROR("Cannot set custom memory allocator functions:");
278 ERROR("wimlib was compiled with the --without-custom-memory-allocator "
280 return WIMLIB_ERR_UNSUPPORTED;
284 static bool seeded = false;
286 static void seed_random()
288 srand(time(NULL) * getpid());
292 /* Fills @n bytes pointed to by @p with random alphanumeric characters. */
293 void randomize_char_array_with_alnum(char p[], size_t n)
308 /* Fills @n bytes pointer to by @p with random numbers. */
309 void randomize_byte_array(u8 *p, size_t n)
317 /* Takes in a path of length @len in @buf, and transforms it into a string for
318 * the path of its parent directory. */
319 void to_parent_name(char buf[], size_t len)
321 ssize_t i = (ssize_t)len - 1;
322 while (i >= 0 && buf[i] == '/')
324 while (i >= 0 && buf[i] != '/')
326 while (i >= 0 && buf[i] == '/')
331 /* Like the basename() function, but does not modify @path; it just returns a
333 const char *path_basename(const char *path)
335 const char *p = path;
340 /* Trailing slashes. */
349 while ((p != path - 1) && *p != '/')
356 * Returns a pointer to the part of @path following the first colon in the last
357 * path component, or NULL if the last path component does not contain a colon.
359 const char *path_stream_name(const char *path)
361 const char *base = path_basename(path);
362 const char *stream_name = strchr(base, ':');
366 return stream_name + 1;
370 * Splits a file path into the part before the first '/', or the entire name if
371 * there is no '/', and the part after the first sequence of '/' characters.
373 * @path: The file path to split.
374 * @first_part_len_ret: A pointer to a `size_t' into which the length of the
375 * first part of the path will be returned.
376 * @return: A pointer to the next part of the path, after the first
377 * sequence of '/', or a pointer to the terminating
378 * null byte in the case of a path without any '/'.
380 const char *path_next_part(const char *path, size_t *first_part_len_ret)
383 const char *next_part;
386 while (path[i] != '/' && path[i] != '\0')
388 if (first_part_len_ret)
389 *first_part_len_ret = i;
390 next_part = &path[i];
391 while (*next_part == '/')
396 /* Returns the number of components of @path. */
397 int get_num_path_components(const char *path)
399 int num_components = 0;
405 while (*path && *path != '/')
408 return num_components;
413 * Prints a string. Printable characters are printed as-is, while unprintable
414 * characters are printed as their octal escape codes.
416 void print_string(const void *string, size_t len)
418 const u8 *p = string;
424 printf("\\%03hho", *p);
429 u64 get_wim_timestamp()
432 gettimeofday(&tv, NULL);
433 return timeval_to_wim_timestamp(&tv);
436 void wim_timestamp_to_str(u64 timestamp, char *buf, size_t len)
439 time_t t = wim_timestamp_to_unix(timestamp);
441 strftime(buf, len, "%a %b %d %H:%M:%S %Y UTC", &tm);