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