]> wimlib.net Git - wimlib/blob - src/error.c
Forbid modifying multi-referenced images
[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 #include "wimlib/win32.h"
47
48 #ifdef ENABLE_ERROR_MESSAGES
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 #endif /* ENABLE_ERROR_MESSAGES */
122
123 void
124 print_byte_field(const u8 *field, size_t len, FILE *out)
125 {
126         while (len--)
127                 tfprintf(out, T("%02hhx"), *field++);
128 }
129
130 WIMLIBAPI int
131 wimlib_set_print_errors(bool show_error_messages)
132 {
133 #ifdef ENABLE_ERROR_MESSAGES
134         wimlib_print_errors = show_error_messages;
135 #else
136         if (show_error_messages)
137                 return WIMLIB_ERR_UNSUPPORTED;
138 #endif
139         return 0;
140 }
141
142 WIMLIBAPI int
143 wimlib_set_error_file(FILE *fp)
144 {
145 #ifdef ENABLE_ERROR_MESSAGES
146         if (wimlib_owns_error_file)
147                 fclose(wimlib_error_file);
148         wimlib_error_file = fp;
149         wimlib_print_errors = (fp != NULL);
150         wimlib_owns_error_file = false;
151         return 0;
152 #else
153         return WIMLIB_ERR_UNSUPPORTED;
154 #endif
155 }
156
157 WIMLIBAPI int
158 wimlib_set_error_file_by_name(const tchar *path)
159 {
160 #ifdef ENABLE_ERROR_MESSAGES
161         FILE *fp;
162
163 #ifdef __WIN32__
164         fp = win32_open_logfile(path);
165 #else
166         fp = fopen(path, "a");
167 #endif
168         if (!fp)
169                 return WIMLIB_ERR_OPEN;
170         wimlib_set_error_file(fp);
171         wimlib_owns_error_file = true;
172         return 0;
173 #else
174         return WIMLIB_ERR_UNSUPPORTED;
175 #endif
176 }
177
178 static const tchar * const error_strings[] = {
179         [WIMLIB_ERR_SUCCESS]
180                 = T("Success"),
181         [WIMLIB_ERR_ALREADY_LOCKED]
182                 = T("The WIM is already locked for writing"),
183         [WIMLIB_ERR_DECOMPRESSION]
184                 = T("Failed to decompress compressed data"),
185         [WIMLIB_ERR_FUSE]
186                 = T("An error was returned by fuse_main()"),
187         [WIMLIB_ERR_GLOB_HAD_NO_MATCHES]
188                 = T("The provided file glob did not match any files"),
189         [WIMLIB_ERR_ICONV_NOT_AVAILABLE]
190                 = T("The iconv() function does not seem to work. "
191                   "Maybe check to make sure the directory /usr/lib/gconv exists"),
192         [WIMLIB_ERR_IMAGE_COUNT]
193                 = T("Inconsistent image count among the metadata "
194                         "resources, the WIM header, and/or the XML data"),
195         [WIMLIB_ERR_IMAGE_NAME_COLLISION]
196                 = T("Tried to add an image with a name that is already in use"),
197         [WIMLIB_ERR_INSUFFICIENT_PRIVILEGES]
198                 = T("The user does not have sufficient privileges"),
199         [WIMLIB_ERR_INTEGRITY]
200                 = T("The WIM file is corrupted (failed integrity check)"),
201         [WIMLIB_ERR_INVALID_CAPTURE_CONFIG]
202                 = T("The contents of the capture configuration file were invalid"),
203         [WIMLIB_ERR_INVALID_CHUNK_SIZE]
204                 = T("The compression chunk size was unrecognized"),
205         [WIMLIB_ERR_INVALID_COMPRESSION_TYPE]
206                 = T("The compression type was unrecognized"),
207         [WIMLIB_ERR_INVALID_HEADER]
208                 = T("The WIM header was invalid"),
209         [WIMLIB_ERR_INVALID_IMAGE]
210                 = T("Tried to select an image that does not exist in the WIM"),
211         [WIMLIB_ERR_INVALID_INTEGRITY_TABLE]
212                 = T("The WIM's integrity table is invalid"),
213         [WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY]
214                 = T("An entry in the WIM's lookup table is invalid"),
215         [WIMLIB_ERR_INVALID_METADATA_RESOURCE]
216                 = T("The metadata resource is invalid"),
217         [WIMLIB_ERR_INVALID_MULTIBYTE_STRING]
218                 = T("A string was not valid in the current locale's character encoding"),
219         [WIMLIB_ERR_INVALID_OVERLAY]
220                 = T("Conflicting files in overlay when creating a WIM image"),
221         [WIMLIB_ERR_INVALID_PARAM]
222                 = T("An invalid parameter was given"),
223         [WIMLIB_ERR_INVALID_PART_NUMBER]
224                 = T("The part number or total parts of the WIM is invalid"),
225         [WIMLIB_ERR_INVALID_PIPABLE_WIM]
226                 = T("The pipable WIM is invalid"),
227         [WIMLIB_ERR_INVALID_REPARSE_DATA]
228                 = T("The reparse data of a reparse point was invalid"),
229         [WIMLIB_ERR_INVALID_RESOURCE_HASH]
230                 = T("The SHA-1 message digest of a WIM resource did not match the expected value"),
231         [WIMLIB_ERR_INVALID_UTF8_STRING]
232                 = T("A string provided as input by the user was not a valid UTF-8 string"),
233         [WIMLIB_ERR_INVALID_UTF16_STRING]
234                 = T("A string in a WIM dentry is not a valid UTF-16LE string"),
235         [WIMLIB_ERR_IS_DIRECTORY]
236                 = T("One of the specified paths to delete was a directory"),
237         [WIMLIB_ERR_IS_SPLIT_WIM]
238                 = T("The WIM is part of a split WIM, which is not supported for this operation"),
239         [WIMLIB_ERR_LINK]
240                 = T("Failed to create a hard or symbolic link when extracting "
241                         "a file from the WIM"),
242         [WIMLIB_ERR_METADATA_NOT_FOUND]
243                 = T("A required metadata resource could not be located"),
244         [WIMLIB_ERR_MKDIR]
245                 = T("Failed to create a directory"),
246         [WIMLIB_ERR_MQUEUE]
247                 = T("Failed to create or use a POSIX message queue"),
248         [WIMLIB_ERR_NOMEM]
249                 = T("Ran out of memory"),
250         [WIMLIB_ERR_NOTDIR]
251                 = T("Expected a directory"),
252         [WIMLIB_ERR_NOTEMPTY]
253                 = T("Directory was not empty"),
254         [WIMLIB_ERR_NOT_A_REGULAR_FILE]
255                 = T("One of the specified paths to extract did not "
256                     "correspond to a regular file"),
257         [WIMLIB_ERR_NOT_A_WIM_FILE]
258                 = T("The file did not begin with the magic characters that "
259                         "identify a WIM file"),
260         [WIMLIB_ERR_NO_FILENAME]
261                 = T("The WIM is not identified with a filename"),
262         [WIMLIB_ERR_NOT_PIPABLE]
263                 = T("The WIM was not captured such that it can be "
264                     "applied from a pipe"),
265         [WIMLIB_ERR_NTFS_3G]
266                 = T("NTFS-3g encountered an error (check errno)"),
267         [WIMLIB_ERR_OPEN]
268                 = T("Failed to open a file"),
269         [WIMLIB_ERR_OPENDIR]
270                 = T("Failed to open a directory"),
271         [WIMLIB_ERR_PATH_DOES_NOT_EXIST]
272                 = T("The path does not exist in the WIM image"),
273         [WIMLIB_ERR_READ]
274                 = T("Could not read data from a file"),
275         [WIMLIB_ERR_READLINK]
276                 = T("Could not read the target of a symbolic link"),
277         [WIMLIB_ERR_RENAME]
278                 = T("Could not rename a file"),
279         [WIMLIB_ERR_REPARSE_POINT_FIXUP_FAILED]
280                 = T("Unable to complete reparse point fixup"),
281         [WIMLIB_ERR_RESOURCE_NOT_FOUND]
282                 = T("A file resource needed to complete the operation was missing from the WIM"),
283         [WIMLIB_ERR_RESOURCE_ORDER]
284                 = T("The components of the WIM were arranged in an unexpected order"),
285         [WIMLIB_ERR_SET_ATTRIBUTES]
286                 = T("Failed to set attributes on extracted file"),
287         [WIMLIB_ERR_SET_REPARSE_DATA]
288                 = T("Failed to set reparse data on extracted file"),
289         [WIMLIB_ERR_SET_SECURITY]
290                 = T("Failed to set file owner, group, or other permissions on extracted file"),
291         [WIMLIB_ERR_SET_SHORT_NAME]
292                 = T("Failed to set short name on extracted file"),
293         [WIMLIB_ERR_SET_TIMESTAMPS]
294                 = T("Failed to set timestamps on extracted file"),
295         [WIMLIB_ERR_SPLIT_INVALID]
296                 = T("The WIM is part of an invalid split WIM"),
297         [WIMLIB_ERR_STAT]
298                 = T("Could not read the metadata for a file or directory"),
299         [WIMLIB_ERR_UNEXPECTED_END_OF_FILE]
300                 = T("Unexpectedly reached the end of the file"),
301         [WIMLIB_ERR_UNICODE_STRING_NOT_REPRESENTABLE]
302                 = T("A Unicode string could not be represented in the current locale's encoding"),
303         [WIMLIB_ERR_UNKNOWN_VERSION]
304                 = T("The WIM file is marked with an unknown version number"),
305         [WIMLIB_ERR_UNSUPPORTED]
306                 = T("The requested operation is unsupported"),
307         [WIMLIB_ERR_UNSUPPORTED_FILE]
308                 = T("A file in the directory tree to archive was not of a supported type"),
309         [WIMLIB_ERR_WIM_IS_READONLY]
310                 = T("The WIM is read-only (file permissions, header flag, or split WIM)"),
311         [WIMLIB_ERR_WRITE]
312                 = T("Failed to write data to a file"),
313         [WIMLIB_ERR_XML]
314                 = T("The XML data of the WIM is invalid"),
315         [WIMLIB_ERR_WIM_IS_ENCRYPTED]
316                 = T("The WIM file (or parts of it) is encrypted"),
317         [WIMLIB_ERR_WIMBOOT]
318                 = T("Failed to set WIMBoot pointer data"),
319         [WIMLIB_ERR_ABORTED_BY_PROGRESS]
320                 = T("The operation was aborted by the library user"),
321         [WIMLIB_ERR_UNKNOWN_PROGRESS_STATUS]
322                 = T("The user-provided progress function returned an unrecognized value"),
323         [WIMLIB_ERR_MKNOD]
324                 = T("Unable to create a special file (e.g. device node or socket)"),
325         [WIMLIB_ERR_MOUNTED_IMAGE_IS_BUSY]
326                 = T("There are still files open on the mounted WIM image"),
327         [WIMLIB_ERR_NOT_A_MOUNTPOINT]
328                 = T("There is not a WIM image mounted on the directory"),
329         [WIMLIB_ERR_NOT_PERMITTED_TO_UNMOUNT]
330                 = T("The current user does not have permission to unmount the WIM image"),
331         [WIMLIB_ERR_FVE_LOCKED_VOLUME]
332                 = T("The volume must be unlocked before it can be used"),
333         [WIMLIB_ERR_UNABLE_TO_READ_CAPTURE_CONFIG]
334                 = T("The capture configuration file could not be read"),
335         [WIMLIB_ERR_WIM_IS_INCOMPLETE]
336                 = T("The WIM file is incomplete"),
337         [WIMLIB_ERR_COMPACTION_NOT_POSSIBLE]
338                 = T("The WIM file cannot be compacted because of its format, "
339                     "its layout, or the write parameters specified by the user"),
340         [WIMLIB_ERR_IMAGE_HAS_MULTIPLE_REFERENCES]
341                 = T("The WIM image cannot be modified because it is currently "
342                     "referenced from multiple places"),
343 };
344
345 WIMLIBAPI const tchar *
346 wimlib_get_error_string(enum wimlib_error_code _code)
347 {
348         unsigned int code = (unsigned int)_code;
349
350         if (code >= ARRAY_LEN(error_strings) || error_strings[code] == NULL)
351                 return T("Unknown error");
352
353         return error_strings[code];
354 }