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/.
28 /* Make sure the POSIX-compatible strerror_r() is declared, rather than the GNU
29 * version, which has a different return type. */
34 #include "endianness.h"
35 #include "timestamp.h"
36 #include "wimlib_internal.h"
51 utf16le_strlen(const utf16lechar *s)
53 const utf16lechar *p = s;
56 return (p - s) * sizeof(utf16lechar);
60 # define wimlib_vfprintf vfwprintf
62 /* Handle %W for UTF16-LE printing.
64 * TODO: this is not yet done properly--- it's assumed that if the format string
65 * contains %W, then it contains no other format specifiers.
68 wimlib_vfprintf(FILE *fp, const tchar *format, va_list va)
73 for (p = format; *p; p++)
74 if (*p == T('%') && *(p + 1) == T('W'))
76 return tvfprintf(fp, format, va);
79 for (p = format; *p; p++) {
80 if (*p == T('%') && (*(p + 1) == T('W'))) {
84 utf16lechar *ucs = va_arg(va, utf16lechar*);
87 size_t ucs_nbytes = utf16le_strlen(ucs);
89 ret = utf16le_to_tstr(ucs, ucs_nbytes,
92 ret = tfprintf(fp, T("??????"));
94 ret = tfprintf(fp, T("%"TS), tstr);
102 n += tfprintf(fp, T("(null)"));
106 if (tputc(*p, fp) == EOF)
115 wimlib_printf(const tchar *format, ...)
120 va_start(va, format);
121 ret = wimlib_vfprintf(stdout, format, va);
127 wimlib_fprintf(FILE *fp, const tchar *format, ...)
132 va_start(va, format);
133 ret = wimlib_vfprintf(fp, format, va);
139 #if defined(ENABLE_ERROR_MESSAGES) || defined(ENABLE_DEBUG)
141 wimlib_vmsg(const tchar *tag, const tchar *format,
142 va_list va, bool perror)
145 if (wimlib_print_errors) {
147 int errno_save = errno;
150 wimlib_vfprintf(stderr, format, va);
151 if (perror && errno_save != 0) {
154 res = tstrerror_r(errno_save, buf, sizeof(buf));
157 T("unknown error (errno=%d)"),
160 tfprintf(stderr, T(": %"TS), buf);
162 tputc(T('\n'), stderr);
171 /* True if wimlib is to print an informational message when an error occurs.
172 * This can be turned off by calling wimlib_set_print_errors(false). */
173 #ifdef ENABLE_ERROR_MESSAGES
174 static bool wimlib_print_errors = false;
178 wimlib_error(const tchar *format, ...)
182 va_start(va, format);
183 wimlib_vmsg(T("\r[ERROR] "), format, va, false);
188 wimlib_error_with_errno(const tchar *format, ...)
192 va_start(va, format);
193 wimlib_vmsg(T("\r[ERROR] "), format, va, true);
198 wimlib_warning(const tchar *format, ...)
202 va_start(va, format);
203 wimlib_vmsg(T("\r[WARNING] "), format, va, false);
208 wimlib_warning_with_errno(const tchar *format, ...)
212 va_start(va, format);
213 wimlib_vmsg(T("\r[WARNING] "), format, va, true);
219 #if defined(ENABLE_DEBUG) || defined(ENABLE_MORE_DEBUG)
220 void wimlib_debug(const tchar *file, int line, const char *func,
221 const tchar *format, ...)
224 tchar buf[tstrlen(file) + strlen(func) + 30];
226 tsprintf(buf, T("[%"TS" %d] %s(): "), file, line, func);
228 va_start(va, format);
229 wimlib_vmsg(buf, format, va, false);
235 wimlib_set_print_errors(bool show_error_messages)
237 #ifdef ENABLE_ERROR_MESSAGES
238 wimlib_print_errors = show_error_messages;
241 if (show_error_messages)
242 return WIMLIB_ERR_UNSUPPORTED;
248 static const tchar *error_strings[] = {
251 [WIMLIB_ERR_ALREADY_LOCKED]
252 = T("The WIM is already locked for writing"),
253 [WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE]
254 = T("Lookup table is compressed"),
255 [WIMLIB_ERR_DECOMPRESSION]
256 = T("Failed to decompress compressed data"),
257 [WIMLIB_ERR_DELETE_STAGING_DIR]
258 = T("Failed to delete staging directory"),
259 [WIMLIB_ERR_FILESYSTEM_DAEMON_CRASHED]
260 = T("The process servicing the mounted WIM has crashed"),
262 = T("Failed to fork another process"),
264 = T("An error was returned by fuse_main()"),
265 [WIMLIB_ERR_FUSERMOUNT]
266 = T("Could not execute the `fusermount' program, or it exited "
267 "with a failure status"),
268 [WIMLIB_ERR_ICONV_NOT_AVAILABLE]
269 = T("The iconv() function does not seem to work. "
270 "Maybe check to make sure the directory /usr/lib/gconv exists"),
271 [WIMLIB_ERR_IMAGE_COUNT]
272 = T("Inconsistent image count among the metadata "
273 "resources, the WIM header, and/or the XML data"),
274 [WIMLIB_ERR_INSUFFICIENT_PRIVILEGES_TO_EXTRACT]
275 = T("User does not have sufficient privileges to correctly extract the data"),
276 [WIMLIB_ERR_IMAGE_NAME_COLLISION]
277 = T("Tried to add an image with a name that is already in use"),
278 [WIMLIB_ERR_INTEGRITY]
279 = T("The WIM failed an integrity check"),
280 [WIMLIB_ERR_INVALID_CAPTURE_CONFIG]
281 = T("The capture configuration string was invalid"),
282 [WIMLIB_ERR_INVALID_CHUNK_SIZE]
283 = T("The WIM is compressed but does not have a chunk "
285 [WIMLIB_ERR_INVALID_COMPRESSION_TYPE]
286 = T("The WIM is compressed, but is not marked as having LZX or "
287 "XPRESS compression"),
288 [WIMLIB_ERR_INVALID_DENTRY]
289 = T("A directory entry in the WIM was invalid"),
290 [WIMLIB_ERR_INVALID_HEADER_SIZE]
291 = T("The WIM header was not 208 bytes"),
292 [WIMLIB_ERR_INVALID_IMAGE]
293 = T("Tried to select an image that does not exist in the WIM"),
294 [WIMLIB_ERR_INVALID_INTEGRITY_TABLE]
295 = T("The WIM's integrity table is invalid"),
296 [WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY]
297 = T("An entry in the WIM's lookup table is invalid"),
298 [WIMLIB_ERR_INVALID_MULTIBYTE_STRING]
299 = T("A string was not valid in the current locale's character encoding"),
300 [WIMLIB_ERR_INVALID_OVERLAY]
301 = T("Conflicting files in overlay when creating a WIM image"),
302 [WIMLIB_ERR_INVALID_PARAM]
303 = T("An invalid parameter was given"),
304 [WIMLIB_ERR_INVALID_PART_NUMBER]
305 = T("The part number or total parts of the WIM is invalid"),
306 [WIMLIB_ERR_INVALID_REPARSE_DATA]
307 = T("The reparse data of a reparse point was invalid"),
308 [WIMLIB_ERR_INVALID_RESOURCE_HASH]
309 = T("The SHA1 message digest of a WIM resource did not match the expected value"),
310 [WIMLIB_ERR_INVALID_RESOURCE_SIZE]
311 = T("A resource entry in the WIM has an invalid size"),
312 [WIMLIB_ERR_INVALID_SECURITY_DATA]
313 = T("The table of security descriptors in the WIM is invalid"),
314 [WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE]
315 = T("The version of wimlib that has mounted a WIM image is incompatible with the "
316 "version being used to unmount it"),
317 [WIMLIB_ERR_INVALID_UTF8_STRING]
318 = T("A string provided as input by the user was not a valid UTF-8 string"),
319 [WIMLIB_ERR_INVALID_UTF16_STRING]
320 = T("A string in a WIM dentry is not a valid UTF-16LE string"),
321 [WIMLIB_ERR_LIBXML_UTF16_HANDLER_NOT_AVAILABLE]
322 = T("libxml2 was unable to find a character encoding conversion handler "
325 = T("Failed to create a hard or symbolic link when extracting "
326 "a file from the WIM"),
328 = T("Failed to create a directory"),
330 = T("Failed to create or use a POSIX message queue"),
332 = T("Ran out of memory"),
334 = T("Expected a directory"),
335 [WIMLIB_ERR_NOT_A_WIM_FILE]
336 = T("The file did not begin with the magic characters that "
337 "identify a WIM file"),
338 [WIMLIB_ERR_NO_FILENAME]
339 = T("The WIM is not identified with a filename"),
341 = T("NTFS-3g encountered an error (check errno)"),
343 = T("Failed to open a file"),
345 = T("Failed to open a directory"),
347 = T("Could not read data from a file"),
348 [WIMLIB_ERR_READLINK]
349 = T("Could not read the target of a symbolic link"),
351 = T("Could not rename a file"),
352 [WIMLIB_ERR_REPARSE_POINT_FIXUP_FAILED]
353 = T("Unable to complete reparse point fixup"),
354 [WIMLIB_ERR_RESOURCE_ORDER]
355 = T("The components of the WIM were arranged in an unexpected order"),
356 [WIMLIB_ERR_SPECIAL_FILE]
357 = T("Encountered a special file that cannot be archived"),
358 [WIMLIB_ERR_SPLIT_INVALID]
359 = T("The WIM is part of an invalid split WIM"),
360 [WIMLIB_ERR_SPLIT_UNSUPPORTED]
361 = T("The WIM is part of a split WIM, which is not supported for this operation"),
363 = T("Could not read the metadata for a file or directory"),
365 = T("Timed out while waiting for a message to arrive from another process"),
366 [WIMLIB_ERR_UNICODE_STRING_NOT_REPRESENTABLE]
367 = T("A Unicode string could not be represented in the current locale's encoding"),
368 [WIMLIB_ERR_UNKNOWN_VERSION]
369 = T("The WIM file is marked with an unknown version number"),
370 [WIMLIB_ERR_UNSUPPORTED]
371 = T("The requested operation is unsupported"),
372 [WIMLIB_ERR_VOLUME_LACKS_FEATURES]
373 = T("The volume did not support a feature necessary to complete the operation"),
375 = T("Failed to write data to a file"),
377 = T("The XML data of the WIM is invalid"),
380 WIMLIBAPI const tchar *
381 wimlib_get_error_string(enum wimlib_error_code code)
383 if (code < 0 || code >= ARRAY_LEN(error_strings))
386 return error_strings[code];
391 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
392 void *(*wimlib_malloc_func) (size_t) = malloc;
393 void (*wimlib_free_func) (void *) = free;
394 void *(*wimlib_realloc_func)(void *, size_t) = realloc;
397 wimlib_calloc(size_t nmemb, size_t size)
399 size_t total_size = nmemb * size;
400 void *p = MALLOC(total_size);
402 memset(p, 0, total_size);
407 wimlib_strdup(const char *str)
413 p = MALLOC(size + 1);
415 memcpy(p, str, size + 1);
421 wimlib_wcsdup(const wchar_t *str)
427 p = MALLOC((size + 1) * sizeof(wchar_t));
429 memcpy(p, str, (size + 1) * sizeof(wchar_t));
435 xml_set_memory_allocator(void *(*malloc_func)(size_t),
436 void (*free_func)(void *),
437 void *(*realloc_func)(void *, size_t));
441 wimlib_set_memory_allocator(void *(*malloc_func)(size_t),
442 void (*free_func)(void *),
443 void *(*realloc_func)(void *, size_t))
445 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
446 wimlib_malloc_func = malloc_func ? malloc_func : malloc;
447 wimlib_free_func = free_func ? free_func : free;
448 wimlib_realloc_func = realloc_func ? realloc_func : realloc;
450 xml_set_memory_allocator(wimlib_malloc_func, wimlib_free_func,
451 wimlib_realloc_func);
454 ERROR("Cannot set custom memory allocator functions:");
455 ERROR("wimlib was compiled with the --without-custom-memory-allocator "
457 return WIMLIB_ERR_UNSUPPORTED;
461 static bool seeded = false;
466 srand(time(NULL) * getpid());
470 /* Fills @n characters pointed to by @p with random alphanumeric characters. */
472 randomize_char_array_with_alnum(tchar p[], size_t n)
487 /* Fills @n bytes pointer to by @p with random numbers. */
489 randomize_byte_array(u8 *p, size_t n)
498 path_basename_with_len(const tchar *path, size_t len)
500 const tchar *p = &path[len] - 1;
502 /* Trailing slashes. */
511 while ((p != path - 1) && *p != T('/'))
517 /* Like the basename() function, but does not modify @path; it just returns a
520 path_basename(const tchar *path)
522 return path_basename_with_len(path, tstrlen(path));
526 * Returns a pointer to the part of @path following the first colon in the last
527 * path component, or NULL if the last path component does not contain a colon.
530 path_stream_name(const tchar *path)
532 const tchar *base = path_basename(path);
533 const tchar *stream_name = tstrchr(base, T(':'));
537 return stream_name + 1;
544 gettimeofday(&tv, NULL);
545 return timeval_to_wim_timestamp(tv);
549 wim_timestamp_to_str(u64 timestamp, tchar *buf, size_t len)
552 time_t t = wim_timestamp_to_unix(timestamp);
554 tstrftime(buf, len, T("%a %b %d %H:%M:%S %Y UTC"), &tm);
558 zap_backslashes(tchar *s)
561 while (*s != T('\0')) {
569 /* Like read(), but keep trying until everything has been written or we know for
570 * sure that there was an error (or end-of-file). */
572 full_read(int fd, void *buf, size_t count)
575 size_t bytes_remaining;
577 for (bytes_remaining = count;
578 bytes_remaining != 0;
579 bytes_remaining -= bytes_read, buf += bytes_read)
581 bytes_read = read(fd, buf, bytes_remaining);
582 if (bytes_read <= 0) {
585 else if (errno == EINTR)
590 return count - bytes_remaining;
593 /* Like write(), but keep trying until everything has been written or we know
594 * for sure that there was an error. */
596 full_write(int fd, const void *buf, size_t count)
598 ssize_t bytes_written;
599 size_t bytes_remaining;
601 for (bytes_remaining = count;
602 bytes_remaining != 0;
603 bytes_remaining -= bytes_written, buf += bytes_written)
605 bytes_written = write(fd, buf, bytes_remaining);
606 if (bytes_written < 0) {
612 return count - bytes_remaining;
615 /* Like pread(), but keep trying until everything has been read or we know for
616 * sure that there was an error (or end-of-file) */
618 full_pread(int fd, void *buf, size_t count, off_t offset)
621 size_t bytes_remaining;
623 for (bytes_remaining = count;
624 bytes_remaining != 0;
625 bytes_remaining -= bytes_read, buf += bytes_read,
626 offset += bytes_read)
628 bytes_read = pread(fd, buf, bytes_remaining, offset);
629 if (bytes_read <= 0) {
632 else if (errno == EINTR)
637 return count - bytes_remaining;
640 /* Like pwrite(), but keep trying until everything has been written or we know
641 * for sure that there was an error. */
643 full_pwrite(int fd, const void *buf, size_t count, off_t offset)
645 ssize_t bytes_written;
646 size_t bytes_remaining;
648 for (bytes_remaining = count;
649 bytes_remaining != 0;
650 bytes_remaining -= bytes_written, buf += bytes_written,
651 offset += bytes_written)
653 bytes_written = pwrite(fd, buf, bytes_remaining, offset);
654 if (bytes_written < 0) {
660 return count - bytes_remaining;
663 /* Like writev(), but keep trying until everything has been written or we know
664 * for sure that there was an error. */
666 full_writev(int fd, struct iovec *iov, int iovcnt)
668 size_t total_bytes_written = 0;
670 ssize_t bytes_written;
672 bytes_written = writev(fd, iov, iovcnt);
673 if (bytes_written < 0) {
678 total_bytes_written += bytes_written;
679 while (bytes_written) {
680 if (bytes_written >= iov[0].iov_len) {
681 bytes_written -= iov[0].iov_len;
685 iov[0].iov_base += bytes_written;
686 iov[0].iov_len -= bytes_written;
691 return total_bytes_written;
695 filedes_offset(filedes_t fd)
697 return lseek(fd, 0, SEEK_CUR);