]> wimlib.net Git - wimlib/blob - src/error.c
win32_apply.c: better workaround for access denied bug when creating ADS
[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_ICONV_NOT_AVAILABLE]
191                 = T("The iconv() function does not seem to work. "
192                   "Maybe check to make sure the directory /usr/lib/gconv exists"),
193         [WIMLIB_ERR_IMAGE_COUNT]
194                 = T("Inconsistent image count among the metadata "
195                         "resources, the WIM header, and/or the XML data"),
196         [WIMLIB_ERR_IMAGE_NAME_COLLISION]
197                 = T("Tried to add an image with a name that is already in use"),
198         [WIMLIB_ERR_INSUFFICIENT_PRIVILEGES]
199                 = T("The user does not have sufficient privileges"),
200         [WIMLIB_ERR_INTEGRITY]
201                 = T("The WIM file is corrupted (failed integrity check)"),
202         [WIMLIB_ERR_INVALID_CAPTURE_CONFIG]
203                 = T("The contents of the capture configuration file were invalid"),
204         [WIMLIB_ERR_INVALID_CHUNK_SIZE]
205                 = T("The compression chunk size was unrecognized"),
206         [WIMLIB_ERR_INVALID_COMPRESSION_TYPE]
207                 = T("The compression type was unrecognized"),
208         [WIMLIB_ERR_INVALID_HEADER]
209                 = T("The WIM header was invalid"),
210         [WIMLIB_ERR_INVALID_IMAGE]
211                 = T("Tried to select an image that does not exist in the WIM"),
212         [WIMLIB_ERR_INVALID_INTEGRITY_TABLE]
213                 = T("The WIM's integrity table is invalid"),
214         [WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY]
215                 = T("An entry in the WIM's lookup table is invalid"),
216         [WIMLIB_ERR_INVALID_METADATA_RESOURCE]
217                 = T("The metadata resource is invalid"),
218         [WIMLIB_ERR_INVALID_MULTIBYTE_STRING]
219                 = T("A string was not valid in the current locale's character encoding"),
220         [WIMLIB_ERR_INVALID_OVERLAY]
221                 = T("Conflicting files in overlay when creating a WIM image"),
222         [WIMLIB_ERR_INVALID_PARAM]
223                 = T("An invalid parameter was given"),
224         [WIMLIB_ERR_INVALID_PART_NUMBER]
225                 = T("The part number or total parts of the WIM is invalid"),
226         [WIMLIB_ERR_INVALID_PIPABLE_WIM]
227                 = T("The pipable WIM is invalid"),
228         [WIMLIB_ERR_INVALID_REPARSE_DATA]
229                 = T("The reparse data of a reparse point was invalid"),
230         [WIMLIB_ERR_INVALID_RESOURCE_HASH]
231                 = T("The SHA-1 message digest of a WIM resource did not match the expected value"),
232         [WIMLIB_ERR_INVALID_UTF8_STRING]
233                 = T("A string provided as input by the user was not a valid UTF-8 string"),
234         [WIMLIB_ERR_INVALID_UTF16_STRING]
235                 = T("A string in a WIM dentry is not a valid UTF-16LE string"),
236         [WIMLIB_ERR_IS_DIRECTORY]
237                 = T("One of the specified paths to delete was a directory"),
238         [WIMLIB_ERR_IS_SPLIT_WIM]
239                 = T("The WIM is part of a split WIM, which is not supported for this operation"),
240         [WIMLIB_ERR_LINK]
241                 = T("Failed to create a hard or symbolic link when extracting "
242                         "a file from the WIM"),
243         [WIMLIB_ERR_METADATA_NOT_FOUND]
244                 = T("The WIM does not contain image metadata; it only contains file data"),
245         [WIMLIB_ERR_MKDIR]
246                 = T("Failed to create a directory"),
247         [WIMLIB_ERR_MQUEUE]
248                 = T("Failed to create or use a POSIX message queue"),
249         [WIMLIB_ERR_NOMEM]
250                 = T("Ran out of memory"),
251         [WIMLIB_ERR_NOTDIR]
252                 = T("Expected a directory"),
253         [WIMLIB_ERR_NOTEMPTY]
254                 = T("Directory was not empty"),
255         [WIMLIB_ERR_NOT_A_REGULAR_FILE]
256                 = T("One of the specified paths to extract did not "
257                     "correspond to a regular file"),
258         [WIMLIB_ERR_NOT_A_WIM_FILE]
259                 = T("The file did not begin with the magic characters that "
260                         "identify a WIM file"),
261         [WIMLIB_ERR_NO_FILENAME]
262                 = T("The WIM is not identified with a filename"),
263         [WIMLIB_ERR_NOT_PIPABLE]
264                 = T("The WIM was not captured such that it can be "
265                     "applied from a pipe"),
266         [WIMLIB_ERR_NTFS_3G]
267                 = T("NTFS-3G encountered an error (check errno)"),
268         [WIMLIB_ERR_OPEN]
269                 = T("Failed to open a file"),
270         [WIMLIB_ERR_OPENDIR]
271                 = T("Failed to open a directory"),
272         [WIMLIB_ERR_PATH_DOES_NOT_EXIST]
273                 = T("The path does not exist in the WIM image"),
274         [WIMLIB_ERR_READ]
275                 = T("Could not read data from a file"),
276         [WIMLIB_ERR_READLINK]
277                 = T("Could not read the target of a symbolic link"),
278         [WIMLIB_ERR_RENAME]
279                 = T("Could not rename a file"),
280         [WIMLIB_ERR_REPARSE_POINT_FIXUP_FAILED]
281                 = T("Unable to complete reparse point fixup"),
282         [WIMLIB_ERR_RESOURCE_NOT_FOUND]
283                 = T("A file resource needed to complete the operation was missing from the WIM"),
284         [WIMLIB_ERR_RESOURCE_ORDER]
285                 = T("The components of the WIM were arranged in an unexpected order"),
286         [WIMLIB_ERR_SET_ATTRIBUTES]
287                 = T("Failed to set attributes on extracted file"),
288         [WIMLIB_ERR_SET_REPARSE_DATA]
289                 = T("Failed to set reparse data on extracted file"),
290         [WIMLIB_ERR_SET_SECURITY]
291                 = T("Failed to set file owner, group, or other permissions on extracted file"),
292         [WIMLIB_ERR_SET_SHORT_NAME]
293                 = T("Failed to set short name on extracted file"),
294         [WIMLIB_ERR_SET_TIMESTAMPS]
295                 = T("Failed to set timestamps on extracted file"),
296         [WIMLIB_ERR_SPLIT_INVALID]
297                 = T("The WIM is part of an invalid split WIM"),
298         [WIMLIB_ERR_STAT]
299                 = T("Could not read the metadata for a file or directory"),
300         [WIMLIB_ERR_UNEXPECTED_END_OF_FILE]
301                 = T("Unexpectedly reached the end of the file"),
302         [WIMLIB_ERR_UNICODE_STRING_NOT_REPRESENTABLE]
303                 = T("A Unicode string could not be represented in the current locale's encoding"),
304         [WIMLIB_ERR_UNKNOWN_VERSION]
305                 = T("The WIM file is marked with an unknown version number"),
306         [WIMLIB_ERR_UNSUPPORTED]
307                 = T("The requested operation is unsupported"),
308         [WIMLIB_ERR_UNSUPPORTED_FILE]
309                 = T("A file in the directory tree to archive was not of a supported type"),
310         [WIMLIB_ERR_WIM_IS_READONLY]
311                 = T("The WIM is read-only (file permissions, header flag, or split WIM)"),
312         [WIMLIB_ERR_WRITE]
313                 = T("Failed to write data to a file"),
314         [WIMLIB_ERR_XML]
315                 = T("The XML data of the WIM is invalid"),
316         [WIMLIB_ERR_WIM_IS_ENCRYPTED]
317                 = T("The WIM file (or parts of it) is encrypted"),
318         [WIMLIB_ERR_WIMBOOT]
319                 = T("Failed to set WIMBoot pointer data"),
320         [WIMLIB_ERR_ABORTED_BY_PROGRESS]
321                 = T("The operation was aborted by the library user"),
322         [WIMLIB_ERR_UNKNOWN_PROGRESS_STATUS]
323                 = T("The user-provided progress function returned an unrecognized value"),
324         [WIMLIB_ERR_MKNOD]
325                 = T("Unable to create a special file (e.g. device node or socket)"),
326         [WIMLIB_ERR_MOUNTED_IMAGE_IS_BUSY]
327                 = T("There are still files open on the mounted WIM image"),
328         [WIMLIB_ERR_NOT_A_MOUNTPOINT]
329                 = T("There is not a WIM image mounted on the directory"),
330         [WIMLIB_ERR_NOT_PERMITTED_TO_UNMOUNT]
331                 = T("The current user does not have permission to unmount the WIM image"),
332         [WIMLIB_ERR_FVE_LOCKED_VOLUME]
333                 = T("The volume must be unlocked before it can be used"),
334         [WIMLIB_ERR_UNABLE_TO_READ_CAPTURE_CONFIG]
335                 = T("The capture configuration file could not be read"),
336         [WIMLIB_ERR_WIM_IS_INCOMPLETE]
337                 = T("The WIM file is incomplete"),
338         [WIMLIB_ERR_COMPACTION_NOT_POSSIBLE]
339                 = T("The WIM file cannot be compacted because of its format, "
340                     "its layout, or the write parameters specified by the user"),
341         [WIMLIB_ERR_IMAGE_HAS_MULTIPLE_REFERENCES]
342                 = T("The WIM image cannot be modified because it is currently "
343                     "referenced from multiple places"),
344         [WIMLIB_ERR_DUPLICATE_EXPORTED_IMAGE]
345                 = T("The destination WIM already contains one of the source images"),
346         [WIMLIB_ERR_CONCURRENT_MODIFICATION_DETECTED]
347                 = T("A file being added to a WIM image was concurrently modified"),
348         [WIMLIB_ERR_SNAPSHOT_FAILURE]
349                 = T("Unable to create a filesystem snapshot"),
350 #ifdef ENABLE_TEST_SUPPORT
351         [WIMLIB_ERR_IMAGES_ARE_DIFFERENT]
352                 = T("A difference was detected between the two images being compared"),
353 #endif
354 };
355
356 WIMLIBAPI const tchar *
357 wimlib_get_error_string(enum wimlib_error_code _code)
358 {
359         unsigned int code = (unsigned int)_code;
360
361         if (code >= ARRAY_LEN(error_strings) || error_strings[code] == NULL)
362                 return T("Unknown error");
363
364         return error_strings[code];
365 }