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