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