2 * error.c: Error reporting
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002 Ximian, Inc.
16 #include "mono/io-layer/wapi.h"
17 #include "mono/io-layer/wapi-private.h"
18 #include "mono/utils/mono-lazy-init.h"
20 static pthread_key_t error_key;
21 static mono_lazy_init_t error_key_once = MONO_LAZY_INIT_STATUS_NOT_INITIALIZED;
23 static void error_init(void)
27 ret = pthread_key_create(&error_key, NULL);
31 static void error_cleanup (void)
35 ret = pthread_key_delete (error_key);
39 void _wapi_error_cleanup (void)
41 mono_lazy_cleanup (&error_key_once, error_cleanup);
47 * Retrieves the last error that occurred in the calling thread.
49 * Return value: The error code for the last error that happened on
52 guint32 GetLastError(void)
57 if (_wapi_has_shut_down)
59 mono_lazy_initialize(&error_key_once, error_init);
60 errptr=pthread_getspecific(error_key);
61 err=GPOINTER_TO_UINT(errptr);
68 * @code: The error code.
70 * Sets the error code in the calling thread.
72 void SetLastError(guint32 code)
76 if (_wapi_has_shut_down)
78 /* Set the thread-local error code */
79 mono_lazy_initialize(&error_key_once, error_init);
80 ret = pthread_setspecific(error_key, GUINT_TO_POINTER(code));
85 _wapi_get_win32_file_error (gint err)
88 /* mapping ideas borrowed from wine. they may need some work */
91 case EACCES: case EPERM: case EROFS:
92 ret = ERROR_ACCESS_DENIED;
96 ret = ERROR_SHARING_VIOLATION;
100 ret = ERROR_LOCK_VIOLATION;
104 ret = ERROR_FILE_EXISTS;
107 case EINVAL: case ESPIPE:
112 ret = ERROR_CANNOT_MAKE;
115 case ENFILE: case EMFILE:
116 ret = ERROR_TOO_MANY_OPEN_FILES;
119 case ENOENT: case ENOTDIR:
120 ret = ERROR_FILE_NOT_FOUND;
124 ret = ERROR_HANDLE_DISK_FULL;
128 ret = ERROR_DIR_NOT_EMPTY;
132 ret = ERROR_BAD_FORMAT;
136 ret = ERROR_FILENAME_EXCED_RANGE;
141 ret = ERROR_IO_PENDING;
146 ret = ERROR_NOT_SUPPORTED;
150 ret = ERROR_INVALID_HANDLE;
154 ret = ERROR_INVALID_HANDLE;
158 ret = ERROR_IO_PENDING; /* best match I could find */
162 ret = ERROR_WRITE_FAULT;
166 g_message ("Unknown errno: %s\n", g_strerror (err));
167 ret = ERROR_GEN_FAILURE;