]> wimlib.net Git - wimlib/blob - src/error.c
New helper: wim_reshdr_to_desc_and_blob()
[wimlib] / src / error.c
1 /*
2  * error.c - logging and error code translation
3  */
4
5 /*
6  * Copyright (C) 2012, 2013, 2014 Eric Biggers
7  *
8  * This file is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License as published by the Free
10  * Software Foundation; either version 3 of the License, or (at your option) any
11  * later version.
12  *
13  * This file is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this file; if not, see http://www.gnu.org/licenses/.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25
26 /* Make sure the POSIX-compatible strerror_r() is declared, rather than the GNU
27  * version, which has a different return type. */
28 #ifdef _GNU_SOURCE
29 #  define GNU_SOURCE_WAS_DEFINED
30 #  undef _GNU_SOURCE
31 #  ifndef _POSIX_C_SOURCE
32 #    define _POSIX_C_SOURCE 200112L
33 #  endif
34 #endif
35 #include <string.h>
36 #ifdef GNU_SOURCE_WAS_DEFINED
37 #  define _GNU_SOURCE
38 #endif
39
40 #include <errno.h>
41 #include <stdarg.h>
42
43 #include "wimlib.h"
44 #include "wimlib/error.h"
45 #include "wimlib/util.h"
46 #include "wimlib/win32.h"
47
48 #ifdef ENABLE_ERROR_MESSAGES
49 bool wimlib_print_errors = false;
50 FILE *wimlib_error_file = NULL; /* Set in wimlib_global_init() */
51 static bool wimlib_owns_error_file = false;
52 #endif
53
54 #if defined(ENABLE_ERROR_MESSAGES) || defined(ENABLE_DEBUG)
55 static void
56 wimlib_vmsg(const tchar *tag, const tchar *format, va_list va, bool perror)
57 {
58 #ifndef ENABLE_DEBUG
59         if (!wimlib_print_errors)
60                 return;
61 #endif
62         int errno_save = errno;
63         fflush(stdout);
64         tfputs(tag, wimlib_error_file);
65         tvfprintf(wimlib_error_file, format, va);
66         if (perror && errno_save != 0) {
67                 tchar buf[64];
68                 int res;
69                 res = tstrerror_r(errno_save, buf, ARRAY_LEN(buf));
70                 if (res) {
71                         tsprintf(buf,
72                                  T("unknown error (errno=%d)"),
73                                  errno_save);
74                 }
75         #ifdef WIN32
76                 if (errno_save == EBUSY)
77                         tstrcpy(buf, T("Resource busy"));
78         #endif
79                 tfprintf(wimlib_error_file, T(": %"TS), buf);
80         }
81         tputc(T('\n'), wimlib_error_file);
82         fflush(wimlib_error_file);
83         errno = errno_save;
84 }
85 #endif
86
87 #ifdef ENABLE_ERROR_MESSAGES
88 void
89 wimlib_error(const tchar *format, ...)
90 {
91         va_list va;
92
93         va_start(va, format);
94         wimlib_vmsg(T("\r[ERROR] "), format, va, false);
95         va_end(va);
96 }
97
98 void
99 wimlib_error_with_errno(const tchar *format, ...)
100 {
101         va_list va;
102
103         va_start(va, format);
104         wimlib_vmsg(T("\r[ERROR] "), format, va, true);
105         va_end(va);
106 }
107
108 void
109 wimlib_warning(const tchar *format, ...)
110 {
111         va_list va;
112
113         va_start(va, format);
114         wimlib_vmsg(T("\r[WARNING] "), format, va, false);
115         va_end(va);
116 }
117
118 void
119 wimlib_warning_with_errno(const tchar *format, ...)
120 {
121         va_list va;
122
123         va_start(va, format);
124         wimlib_vmsg(T("\r[WARNING] "), format, va, true);
125         va_end(va);
126 }
127
128 #endif
129
130 #ifdef ENABLE_DEBUG
131 void wimlib_debug(const tchar *filename, int line, const char *func,
132                   const tchar *format, ...)
133 {
134         va_list va;
135         tchar buf[tstrlen(filename) + strlen(func) + 30];
136
137         tsprintf(buf, T("[%"TS" %d] %s(): "), filename, line, func);
138         va_start(va, format);
139         wimlib_vmsg(buf, format, va, false);
140         va_end(va);
141 }
142 #endif
143
144 void
145 print_byte_field(const u8 *field, size_t len, FILE *out)
146 {
147         while (len--)
148                 tfprintf(out, T("%02hhx"), *field++);
149 }
150
151 WIMLIBAPI int
152 wimlib_set_print_errors(bool show_error_messages)
153 {
154 #ifdef ENABLE_ERROR_MESSAGES
155         wimlib_print_errors = show_error_messages;
156 #else
157         if (show_error_messages)
158                 return WIMLIB_ERR_UNSUPPORTED;
159 #endif
160         return 0;
161 }
162
163 WIMLIBAPI int
164 wimlib_set_error_file(FILE *fp)
165 {
166 #ifdef ENABLE_ERROR_MESSAGES
167         if (wimlib_owns_error_file)
168                 fclose(wimlib_error_file);
169         wimlib_error_file = fp;
170         wimlib_print_errors = (fp != NULL);
171         wimlib_owns_error_file = false;
172         return 0;
173 #else
174         return WIMLIB_ERR_UNSUPPORTED;
175 #endif
176 }
177
178 WIMLIBAPI int
179 wimlib_set_error_file_by_name(const tchar *path)
180 {
181 #ifdef ENABLE_ERROR_MESSAGES
182         FILE *fp;
183
184 #ifdef __WIN32__
185         fp = win32_open_logfile(path);
186 #else
187         fp = fopen(path, "a");
188 #endif
189         if (!fp)
190                 return WIMLIB_ERR_OPEN;
191         wimlib_set_error_file(fp);
192         wimlib_owns_error_file = true;
193         return 0;
194 #else
195         return WIMLIB_ERR_UNSUPPORTED;
196 #endif
197 }
198
199 static const tchar * const error_strings[] = {
200         [WIMLIB_ERR_SUCCESS]
201                 = T("Success"),
202         [WIMLIB_ERR_ALREADY_LOCKED]
203                 = T("The WIM is already locked for writing"),
204         [WIMLIB_ERR_DECOMPRESSION]
205                 = T("Failed to decompress compressed data"),
206         [WIMLIB_ERR_FUSE]
207                 = T("An error was returned by fuse_main()"),
208         [WIMLIB_ERR_GLOB_HAD_NO_MATCHES]
209                 = T("The provided file glob did not match any files"),
210         [WIMLIB_ERR_ICONV_NOT_AVAILABLE]
211                 = T("The iconv() function does not seem to work. "
212                   "Maybe check to make sure the directory /usr/lib/gconv exists"),
213         [WIMLIB_ERR_IMAGE_COUNT]
214                 = T("Inconsistent image count among the metadata "
215                         "resources, the WIM header, and/or the XML data"),
216         [WIMLIB_ERR_IMAGE_NAME_COLLISION]
217                 = T("Tried to add an image with a name that is already in use"),
218         [WIMLIB_ERR_INSUFFICIENT_PRIVILEGES]
219                 = T("The user does not have sufficient privileges"),
220         [WIMLIB_ERR_INTEGRITY]
221                 = T("The WIM file is corrupted (failed integrity check)"),
222         [WIMLIB_ERR_INVALID_CAPTURE_CONFIG]
223                 = T("The contents of the capture configuration file were invalid"),
224         [WIMLIB_ERR_INVALID_CHUNK_SIZE]
225                 = T("The compression chunk size was unrecognized"),
226         [WIMLIB_ERR_INVALID_COMPRESSION_TYPE]
227                 = T("The compression type was unrecognized"),
228         [WIMLIB_ERR_INVALID_HEADER]
229                 = T("The WIM header was invalid"),
230         [WIMLIB_ERR_INVALID_IMAGE]
231                 = T("Tried to select an image that does not exist in the WIM"),
232         [WIMLIB_ERR_INVALID_INTEGRITY_TABLE]
233                 = T("The WIM's integrity table is invalid"),
234         [WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY]
235                 = T("An entry in the WIM's lookup table is invalid"),
236         [WIMLIB_ERR_INVALID_METADATA_RESOURCE]
237                 = T("The metadata resource is invalid"),
238         [WIMLIB_ERR_INVALID_MULTIBYTE_STRING]
239                 = T("A string was not valid in the current locale's character encoding"),
240         [WIMLIB_ERR_INVALID_OVERLAY]
241                 = T("Conflicting files in overlay when creating a WIM image"),
242         [WIMLIB_ERR_INVALID_PARAM]
243                 = T("An invalid parameter was given"),
244         [WIMLIB_ERR_INVALID_PART_NUMBER]
245                 = T("The part number or total parts of the WIM is invalid"),
246         [WIMLIB_ERR_INVALID_PIPABLE_WIM]
247                 = T("The pipable WIM is invalid"),
248         [WIMLIB_ERR_INVALID_REPARSE_DATA]
249                 = T("The reparse data of a reparse point was invalid"),
250         [WIMLIB_ERR_INVALID_RESOURCE_HASH]
251                 = T("The SHA-1 message digest of a WIM resource did not match the expected value"),
252         [WIMLIB_ERR_INVALID_UTF8_STRING]
253                 = T("A string provided as input by the user was not a valid UTF-8 string"),
254         [WIMLIB_ERR_INVALID_UTF16_STRING]
255                 = T("A string in a WIM dentry is not a valid UTF-16LE string"),
256         [WIMLIB_ERR_IS_DIRECTORY]
257                 = T("One of the specified paths to delete was a directory"),
258         [WIMLIB_ERR_IS_SPLIT_WIM]
259                 = T("The WIM is part of a split WIM, which is not supported for this operation"),
260         [WIMLIB_ERR_LIBXML_UTF16_HANDLER_NOT_AVAILABLE]
261                 = T("libxml2 was unable to find a character encoding conversion handler "
262                   "for UTF-16LE"),
263         [WIMLIB_ERR_LINK]
264                 = T("Failed to create a hard or symbolic link when extracting "
265                         "a file from the WIM"),
266         [WIMLIB_ERR_METADATA_NOT_FOUND]
267                 = T("A required metadata resource could not be located"),
268         [WIMLIB_ERR_MKDIR]
269                 = T("Failed to create a directory"),
270         [WIMLIB_ERR_MQUEUE]
271                 = T("Failed to create or use a POSIX message queue"),
272         [WIMLIB_ERR_NOMEM]
273                 = T("Ran out of memory"),
274         [WIMLIB_ERR_NOTDIR]
275                 = T("Expected a directory"),
276         [WIMLIB_ERR_NOTEMPTY]
277                 = T("Directory was not empty"),
278         [WIMLIB_ERR_NOT_A_REGULAR_FILE]
279                 = T("One of the specified paths to extract did not "
280                     "correspond to a regular file"),
281         [WIMLIB_ERR_NOT_A_WIM_FILE]
282                 = T("The file did not begin with the magic characters that "
283                         "identify a WIM file"),
284         [WIMLIB_ERR_NO_FILENAME]
285                 = T("The WIM is not identified with a filename"),
286         [WIMLIB_ERR_NOT_PIPABLE]
287                 = T("The WIM was not captured such that it can be "
288                     "applied from a pipe"),
289         [WIMLIB_ERR_NTFS_3G]
290                 = T("NTFS-3g encountered an error (check errno)"),
291         [WIMLIB_ERR_OPEN]
292                 = T("Failed to open a file"),
293         [WIMLIB_ERR_OPENDIR]
294                 = T("Failed to open a directory"),
295         [WIMLIB_ERR_PATH_DOES_NOT_EXIST]
296                 = T("The path does not exist in the WIM image"),
297         [WIMLIB_ERR_READ]
298                 = T("Could not read data from a file"),
299         [WIMLIB_ERR_READLINK]
300                 = T("Could not read the target of a symbolic link"),
301         [WIMLIB_ERR_RENAME]
302                 = T("Could not rename a file"),
303         [WIMLIB_ERR_REPARSE_POINT_FIXUP_FAILED]
304                 = T("Unable to complete reparse point fixup"),
305         [WIMLIB_ERR_RESOURCE_NOT_FOUND]
306                 = T("A file resource needed to complete the operation was missing from the WIM"),
307         [WIMLIB_ERR_RESOURCE_ORDER]
308                 = T("The components of the WIM were arranged in an unexpected order"),
309         [WIMLIB_ERR_SET_ATTRIBUTES]
310                 = T("Failed to set attributes on extracted file"),
311         [WIMLIB_ERR_SET_REPARSE_DATA]
312                 = T("Failed to set reparse data on extracted file"),
313         [WIMLIB_ERR_SET_SECURITY]
314                 = T("Failed to set file owner, group, or other permissions on extracted file"),
315         [WIMLIB_ERR_SET_SHORT_NAME]
316                 = T("Failed to set short name on extracted file"),
317         [WIMLIB_ERR_SET_TIMESTAMPS]
318                 = T("Failed to set timestamps on extracted file"),
319         [WIMLIB_ERR_SPLIT_INVALID]
320                 = T("The WIM is part of an invalid split WIM"),
321         [WIMLIB_ERR_STAT]
322                 = T("Could not read the metadata for a file or directory"),
323         [WIMLIB_ERR_UNEXPECTED_END_OF_FILE]
324                 = T("Unexpectedly reached the end of the file"),
325         [WIMLIB_ERR_UNICODE_STRING_NOT_REPRESENTABLE]
326                 = T("A Unicode string could not be represented in the current locale's encoding"),
327         [WIMLIB_ERR_UNKNOWN_VERSION]
328                 = T("The WIM file is marked with an unknown version number"),
329         [WIMLIB_ERR_UNSUPPORTED]
330                 = T("The requested operation is unsupported"),
331         [WIMLIB_ERR_UNSUPPORTED_FILE]
332                 = T("A file in the directory tree to archive was not of a supported type"),
333         [WIMLIB_ERR_WIM_IS_READONLY]
334                 = T("The WIM is read-only (file permissions, header flag, or split WIM)"),
335         [WIMLIB_ERR_WRITE]
336                 = T("Failed to write data to a file"),
337         [WIMLIB_ERR_XML]
338                 = T("The XML data of the WIM is invalid"),
339         [WIMLIB_ERR_WIM_IS_ENCRYPTED]
340                 = T("The WIM file (or parts of it) is encrypted"),
341         [WIMLIB_ERR_WIMBOOT]
342                 = T("Failed to set WIMBoot pointer data"),
343         [WIMLIB_ERR_ABORTED_BY_PROGRESS]
344                 = T("The operation was aborted by the library user"),
345         [WIMLIB_ERR_UNKNOWN_PROGRESS_STATUS]
346                 = T("The user-provided progress function returned an unrecognized value"),
347         [WIMLIB_ERR_MKNOD]
348                 = T("Unable to create a special file (e.g. device node or socket)"),
349         [WIMLIB_ERR_MOUNTED_IMAGE_IS_BUSY]
350                 = T("There are still files open on the mounted WIM image"),
351         [WIMLIB_ERR_NOT_A_MOUNTPOINT]
352                 = T("There is not a WIM image mounted on the directory"),
353         [WIMLIB_ERR_NOT_PERMITTED_TO_UNMOUNT]
354                 = T("The current user does not have permission to unmount the WIM image"),
355         [WIMLIB_ERR_FVE_LOCKED_VOLUME]
356                 = T("The volume must be unlocked before it can be used"),
357         [WIMLIB_ERR_UNABLE_TO_READ_CAPTURE_CONFIG]
358                 = T("The capture configuration file could not be read"),
359 };
360
361 WIMLIBAPI const tchar *
362 wimlib_get_error_string(enum wimlib_error_code _code)
363 {
364         unsigned int code = (unsigned int)_code;
365
366         if (code >= ARRAY_LEN(error_strings) || error_strings[code] == NULL)
367                 return T("Unknown error");
368
369         return error_strings[code];
370 }