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