Debian Codegraph @

lib/error.c

raw symbols SCIP
1 /*
2 Copyright (C) 2013-2017 Brazil
3 Copyright (C) 2020-2024 Sutou Kouhei <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "grn_error.h"
21 #include "grn_str.h"
22 #include "grn_windows.h"
23
24 #ifdef HAVE_ERRNO_H
25 # include <errno.h>
26 #endif /* HAVE_ERRNO_H */
27
28 #include <string.h>
29
30 static uint32_t grn_error_cancel_interval = 0;
31 static uint32_t grn_error_cancel_counter = 0;
32
33 void
34 grn_error_init_from_env(void)
35 {
36 {
37 char grn_error_cancel_interval_env[GRN_ENV_BUFFER_SIZE];
38 grn_getenv("GRN_ERROR_CANCEL_INTERVAL",
39 grn_error_cancel_interval_env,
40 GRN_ENV_BUFFER_SIZE);
41 const char *end =
42 grn_error_cancel_interval_env + strlen(grn_error_cancel_interval_env);
43 const char *rest;
44 uint32_t interval = grn_atoui(grn_error_cancel_interval_env, end, &rest);
45 if (end == rest) {
46 grn_error_cancel_interval = interval;
47 }
48 }
49 }
50
51 void
52 grn_error_cancel(grn_ctx *ctx)
53 {
54 if (grn_error_cancel_interval == 0) {
55 return;
56 }
57 if ((++grn_error_cancel_counter % grn_error_cancel_interval) == 0) {
58 ctx->rc = GRN_CANCEL;
59 }
60 }
61
62 #ifdef WIN32
63
64 grn_rc
65 grn_windows_error_code_to_rc(int error_code)
66 {
67 grn_rc rc;
68
69 switch (error_code) {
70 case ERROR_FILE_NOT_FOUND:
71 case ERROR_PATH_NOT_FOUND:
72 rc = GRN_NO_SUCH_FILE_OR_DIRECTORY;
73 break;
74 case ERROR_TOO_MANY_OPEN_FILES:
75 rc = GRN_TOO_MANY_OPEN_FILES;
76 break;
77 case ERROR_ACCESS_DENIED:
78 rc = GRN_PERMISSION_DENIED;
79 break;
80 case ERROR_INVALID_HANDLE:
81 rc = GRN_INVALID_ARGUMENT;
82 break;
83 case ERROR_ARENA_TRASHED:
84 rc = GRN_ADDRESS_IS_NOT_AVAILABLE;
85 break;
86 case ERROR_NOT_ENOUGH_MEMORY:
87 rc = GRN_NO_MEMORY_AVAILABLE;
88 break;
89 case ERROR_INVALID_BLOCK:
90 case ERROR_BAD_ENVIRONMENT:
91 rc = GRN_INVALID_ARGUMENT;
92 break;
93 case ERROR_BAD_FORMAT:
94 rc = GRN_INVALID_FORMAT;
95 break;
96 case ERROR_INVALID_DATA:
97 rc = GRN_INVALID_ARGUMENT;
98 break;
99 case ERROR_OUTOFMEMORY:
100 rc = GRN_NO_MEMORY_AVAILABLE;
101 break;
102 case ERROR_INVALID_DRIVE:
103 rc = GRN_INVALID_ARGUMENT;
104 break;
105 case ERROR_WRITE_PROTECT:
106 rc = GRN_PERMISSION_DENIED;
107 break;
108 case ERROR_BAD_LENGTH:
109 rc = GRN_INVALID_ARGUMENT;
110 break;
111 case ERROR_SEEK:
112 rc = GRN_INVALID_SEEK;
113 break;
114 case ERROR_NOT_SUPPORTED:
115 rc = GRN_OPERATION_NOT_SUPPORTED;
116 break;
117 case ERROR_NETWORK_ACCESS_DENIED:
118 rc = GRN_OPERATION_NOT_PERMITTED;
119 break;
120 case ERROR_FILE_EXISTS:
121 rc = GRN_FILE_EXISTS;
122 break;
123 case ERROR_INVALID_PARAMETER:
124 rc = GRN_INVALID_ARGUMENT;
125 break;
126 case ERROR_BROKEN_PIPE:
127 rc = GRN_BROKEN_PIPE;
128 break;
129 case ERROR_CALL_NOT_IMPLEMENTED:
130 rc = GRN_FUNCTION_NOT_IMPLEMENTED;
131 break;
132 case ERROR_INVALID_NAME:
133 rc = GRN_INVALID_ARGUMENT;
134 break;
135 case ERROR_BUSY_DRIVE:
136 case ERROR_PATH_BUSY:
137 rc = GRN_RESOURCE_BUSY;
138 break;
139 case ERROR_BAD_ARGUMENTS:
140 rc = GRN_INVALID_ARGUMENT;
141 break;
142 case ERROR_BUSY:
143 rc = GRN_RESOURCE_BUSY;
144 break;
145 case ERROR_ALREADY_EXISTS:
146 rc = GRN_FILE_EXISTS;
147 break;
148 case ERROR_BAD_EXE_FORMAT:
149 rc = GRN_EXEC_FORMAT_ERROR;
150 break;
151 case ERROR_NO_SYSTEM_RESOURCES:
152 rc = GRN_RESOURCE_TEMPORARILY_UNAVAILABLE;
153 break;
154 default:
155 rc = GRN_UNKNOWN_ERROR;
156 break;
157 }
158
159 return rc;
160 }
161
162 # define LANG_ID_NEUTRAL() MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL)
163 # define LANG_ID_USER_DEFAULT() MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT)
164 # define LANG_ID_SYSTEM_DEFAULT() MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT)
165
166 const char *
167 grn_error_get_current_system_message(void)
168 {
169 # define ERROR_MESSAGE_BUFFER_SIZE 4096
170 int error_code = GetLastError();
171 static WCHAR utf16_message[ERROR_MESSAGE_BUFFER_SIZE];
172 DWORD written_utf16_chars;
173 static char message[ERROR_MESSAGE_BUFFER_SIZE];
174
175 written_utf16_chars =
176 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
177 NULL,
178 error_code,
179 LANG_ID_USER_DEFAULT(),
180 utf16_message,
181 ERROR_MESSAGE_BUFFER_SIZE,
182 NULL);
183 if (written_utf16_chars >= 2) {
184 if (utf16_message[written_utf16_chars - 1] == L'\n') {
185 utf16_message[written_utf16_chars - 1] = L'\0';
186 written_utf16_chars--;
187 }
188 if (utf16_message[written_utf16_chars - 1] == L'\r') {
189 utf16_message[written_utf16_chars - 1] = L'\0';
190 written_utf16_chars--;
191 }
192 }
193
194 {
195 UINT code_page;
196 DWORD convert_flags = 0;
197 int written_bytes;
198
199 code_page = grn_windows_encoding_to_code_page(grn_get_default_encoding());
200 written_bytes = WideCharToMultiByte(code_page,
201 convert_flags,
202 utf16_message,
203 written_utf16_chars,
204 message,
205 ERROR_MESSAGE_BUFFER_SIZE,
206 NULL,
207 NULL);
208 }
209
210 return message;
211
212 # undef ERROR_MESSAGE_BUFFER_SIZE
213 }
214 #else
215 grn_rc
216 grn_windows_error_code_to_rc(int error_code)
217 {
218 return GRN_UNKNOWN_ERROR;
219 }
220
221 const char *
222 grn_error_get_current_system_message(void)
223 {
224 return strerror(errno);
225 }
226 #endif
227
228 const char *
229 grn_strerror(int error_code)
230 {
231 #ifdef WIN32
232 # define MESSAGE_BUFFER_SIZE 1024
233 static char message[MESSAGE_BUFFER_SIZE];
234 strerror_s(message, MESSAGE_BUFFER_SIZE, error_code);
235 return message;
236 # undef MESSAGE_BUFFER_SIZE
237 #else /* WIN32 */
238 return strerror(error_code);
239 #endif /* WIN32 */
240 }
241
242 const char *
243 grn_rc_to_string(grn_rc rc)
244 {
245 const char *message = "invalid grn_rc";
246
247 switch (rc) {
248 case GRN_SUCCESS:
249 message = "success";
250 break;
251 case GRN_END_OF_DATA:
252 message = "end of data";
253 break;
254 case GRN_UNKNOWN_ERROR:
255 message = "unknown error";
256 break;
257 case GRN_OPERATION_NOT_PERMITTED:
258 message = "operation not permitted";
259 break;
260 case GRN_NO_SUCH_FILE_OR_DIRECTORY:
261 message = "no such file or directory";
262 break;
263 case GRN_NO_SUCH_PROCESS:
264 message = "no such process";
265 break;
266 case GRN_INTERRUPTED_FUNCTION_CALL:
267 message = "interrupted function call";
268 break;
269 case GRN_INPUT_OUTPUT_ERROR:
270 message = "input output error";
271 break;
272 case GRN_NO_SUCH_DEVICE_OR_ADDRESS:
273 message = "no such device or address";
274 break;
275 case GRN_ARG_LIST_TOO_LONG:
276 message = "argument list is too long";
277 break;
278 case GRN_EXEC_FORMAT_ERROR:
279 message = "exec format error";
280 break;
281 case GRN_BAD_FILE_DESCRIPTOR:
282 message = "bad file descriptor";
283 break;
284 case GRN_NO_CHILD_PROCESSES:
285 message = "no child processes";
286 break;
287 case GRN_RESOURCE_TEMPORARILY_UNAVAILABLE:
288 message = "resource temporarily unavailable";
289 break;
290 case GRN_NOT_ENOUGH_SPACE:
291 message = "not enough space";
292 break;
293 case GRN_PERMISSION_DENIED:
294 message = "permission denied";
295 break;
296 case GRN_BAD_ADDRESS:
297 message = "bad address";
298 break;
299 case GRN_RESOURCE_BUSY:
300 message = "resource busy";
301 break;
302 case GRN_FILE_EXISTS:
303 message = "file exists";
304 break;
305 case GRN_IMPROPER_LINK:
306 message = "improper link";
307 break;
308 case GRN_NO_SUCH_DEVICE:
309 message = "no such device";
310 break;
311 case GRN_NOT_A_DIRECTORY:
312 message = "not a directory";
313 break;
314 case GRN_IS_A_DIRECTORY:
315 message = "is a directory";
316 break;
317 case GRN_INVALID_ARGUMENT:
318 message = "invalid argument";
319 break;
320 case GRN_TOO_MANY_OPEN_FILES_IN_SYSTEM:
321 message = "too many open files in system";
322 break;
323 case GRN_TOO_MANY_OPEN_FILES:
324 message = "too many open files";
325 break;
326 case GRN_INAPPROPRIATE_I_O_CONTROL_OPERATION:
327 message = "inappropriate I/O control operation";
328 break;
329 case GRN_FILE_TOO_LARGE:
330 message = "file too large";
331 break;
332 case GRN_NO_SPACE_LEFT_ON_DEVICE:
333 message = "no space left on device";
334 break;
335 case GRN_INVALID_SEEK:
336 message = "invalid seek";
337 break;
338 case GRN_READ_ONLY_FILE_SYSTEM:
339 message = "read only file system";
340 break;
341 case GRN_TOO_MANY_LINKS:
342 message = "too many links";
343 break;
344 case GRN_BROKEN_PIPE:
345 message = "broken pipe";
346 break;
347 case GRN_DOMAIN_ERROR:
348 message = "domain error";
349 break;
350 case GRN_RESULT_TOO_LARGE:
351 message = "result too large";
352 break;
353 case GRN_RESOURCE_DEADLOCK_AVOIDED:
354 message = "resource deadlock avoided";
355 break;
356 case GRN_NO_MEMORY_AVAILABLE:
357 message = "no memory available";
358 break;
359 case GRN_FILENAME_TOO_LONG:
360 message = "filename too long";
361 break;
362 case GRN_NO_LOCKS_AVAILABLE:
363 message = "no locks available";
364 break;
365 case GRN_FUNCTION_NOT_IMPLEMENTED:
366 message = "function not implemented";
367 break;
368 case GRN_DIRECTORY_NOT_EMPTY:
369 message = "directory not empty";
370 break;
371 case GRN_ILLEGAL_BYTE_SEQUENCE:
372 message = "illegal byte sequence";
373 break;
374 case GRN_SOCKET_NOT_INITIALIZED:
375 message = "socket not initialized";
376 break;
377 case GRN_OPERATION_WOULD_BLOCK:
378 message = "operation would block";
379 break;
380 case GRN_ADDRESS_IS_NOT_AVAILABLE:
381 message = "address is not available";
382 break;
383 case GRN_NETWORK_IS_DOWN:
384 message = "network is down";
385 break;
386 case GRN_NO_BUFFER:
387 message = "no buffer";
388 break;
389 case GRN_SOCKET_IS_ALREADY_CONNECTED:
390 message = "socket is already connected";
391 break;
392 case GRN_SOCKET_IS_NOT_CONNECTED:
393 message = "socket is not connected";
394 break;
395 case GRN_SOCKET_IS_ALREADY_SHUTDOWNED:
396 message = "socket is already shutdowned";
397 break;
398 case GRN_OPERATION_TIMEOUT:
399 message = "operation timeout";
400 break;
401 case GRN_CONNECTION_REFUSED:
402 message = "connection refused";
403 break;
404 case GRN_RANGE_ERROR:
405 message = "range error";
406 break;
407 case GRN_TOKENIZER_ERROR:
408 message = "tokenizer error";
409 break;
410 case GRN_FILE_CORRUPT:
411 message = "file corrupt";
412 break;
413 case GRN_INVALID_FORMAT:
414 message = "invalid format";
415 break;
416 case GRN_OBJECT_CORRUPT:
417 message = "object corrupt";
418 break;
419 case GRN_TOO_MANY_SYMBOLIC_LINKS:
420 message = "too many symbolic links";
421 break;
422 case GRN_NOT_SOCKET:
423 message = "not socket";
424 break;
425 case GRN_OPERATION_NOT_SUPPORTED:
426 message = "operation not supported";
427 break;
428 case GRN_ADDRESS_IS_IN_USE:
429 message = "address is in use";
430 break;
431 case GRN_ZLIB_ERROR:
432 message = "zlib error";
433 break;
434 case GRN_LZ4_ERROR:
435 message = "LZ4 error";
436 break;
437 case GRN_STACK_OVER_FLOW:
438 message = "stack over flow";
439 break;
440 case GRN_SYNTAX_ERROR:
441 message = "syntax error";
442 break;
443 case GRN_RETRY_MAX:
444 message = "retry max";
445 break;
446 case GRN_INCOMPATIBLE_FILE_FORMAT:
447 message = "incompatible file format";
448 break;
449 case GRN_UPDATE_NOT_ALLOWED:
450 message = "update not allowed";
451 break;
452 case GRN_TOO_SMALL_OFFSET:
453 message = "too small offset";
454 break;
455 case GRN_TOO_LARGE_OFFSET:
456 message = "too large offset";
457 break;
458 case GRN_TOO_SMALL_LIMIT:
459 message = "too small limit";
460 break;
461 case GRN_CAS_ERROR:
462 message = "cas error";
463 break;
464 case GRN_UNSUPPORTED_COMMAND_VERSION:
465 message = "unsupported command version";
466 break;
467 case GRN_NORMALIZER_ERROR:
468 message = "normalizer error";
469 break;
470 case GRN_TOKEN_FILTER_ERROR:
471 message = "token filter error";
472 break;
473 case GRN_COMMAND_ERROR:
474 message = "command error";
475 break;
476 case GRN_PLUGIN_ERROR:
477 message = "plugin error";
478 break;
479 case GRN_SCORER_ERROR:
480 message = "scorer error";
481 break;
482 case GRN_CANCEL:
483 message = "cancel";
484 break;
485 case GRN_WINDOW_FUNCTION_ERROR:
486 message = "window function error";
487 break;
488 case GRN_ZSTD_ERROR:
489 message = "Zstandard error";
490 break;
491 case GRN_CONNECTION_RESET:
492 message = "connection reset";
493 break;
494 case GRN_BLOSC_ERROR:
495 message = "Blosc error";
496 break;
497 case GRN_OPENZL_ERROR:
498 message = "OpenZL error";
499 break;
500 case GRN_EXTRACTOR_ERROR:
501 message = "extractor error";
502 break;
503 }
504
505 return message;
506 }