1 /* Copyright (c) 2008 MySQL AB, 2009 Sun Microsystems, Inc.
2    Use is subject to license terms.
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
7 
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335  USA */
16 
17 /*
18   Convert Windows API error (GetLastError() to Posix equivalent (errno)
19   The exported function  my_osmaperr() is modelled after and borrows
20   heavily from undocumented _dosmaperr()(found of the static Microsoft C runtime).
21 */
22 
23 #include <my_global.h>
24 #include <my_sys.h>
25 
26 
27 struct errentry
28 {
29   unsigned long oscode;   /* OS return value */
30   int sysv_errno;  /* System V error code */
31 };
32 
33 static struct errentry errtable[]= {
34   {  ERROR_INVALID_FUNCTION,       EINVAL    },  /* 1 */
35   {  ERROR_FILE_NOT_FOUND,         ENOENT    },  /* 2 */
36   {  ERROR_PATH_NOT_FOUND,         ENOENT    },  /* 3 */
37   {  ERROR_TOO_MANY_OPEN_FILES,    EMFILE    },  /* 4 */
38   {  ERROR_ACCESS_DENIED,          EACCES    },  /* 5 */
39   {  ERROR_INVALID_HANDLE,         EBADF     },  /* 6 */
40   {  ERROR_ARENA_TRASHED,          ENOMEM    },  /* 7 */
41   {  ERROR_NOT_ENOUGH_MEMORY,      ENOMEM    },  /* 8 */
42   {  ERROR_INVALID_BLOCK,          ENOMEM    },  /* 9 */
43   {  ERROR_BAD_ENVIRONMENT,        E2BIG     },  /* 10 */
44   {  ERROR_BAD_FORMAT,             ENOEXEC   },  /* 11 */
45   {  ERROR_INVALID_ACCESS,         EINVAL    },  /* 12 */
46   {  ERROR_INVALID_DATA,           EINVAL    },  /* 13 */
47   {  ERROR_INVALID_DRIVE,          ENOENT    },  /* 15 */
48   {  ERROR_CURRENT_DIRECTORY,      EACCES    },  /* 16 */
49   {  ERROR_NOT_SAME_DEVICE,        EXDEV     },  /* 17 */
50   {  ERROR_NO_MORE_FILES,          ENOENT    },  /* 18 */
51   {  ERROR_LOCK_VIOLATION,         EACCES    },  /* 33 */
52   {  ERROR_BAD_NETPATH,            ENOENT    },  /* 53 */
53   {  ERROR_NETWORK_ACCESS_DENIED,  EACCES    },  /* 65 */
54   {  ERROR_BAD_NET_NAME,           ENOENT    },  /* 67 */
55   {  ERROR_FILE_EXISTS,            EEXIST    },  /* 80 */
56   {  ERROR_CANNOT_MAKE,            EACCES    },  /* 82 */
57   {  ERROR_FAIL_I24,               EACCES    },  /* 83 */
58   {  ERROR_INVALID_PARAMETER,      EINVAL    },  /* 87 */
59   {  ERROR_NO_PROC_SLOTS,          EAGAIN    },  /* 89 */
60   {  ERROR_DRIVE_LOCKED,           EACCES    },  /* 108 */
61   {  ERROR_BROKEN_PIPE,            EPIPE     },  /* 109 */
62   {  ERROR_DISK_FULL,              ENOSPC    },  /* 112 */
63   {  ERROR_INVALID_TARGET_HANDLE,  EBADF     },  /* 114 */
64   {  ERROR_INVALID_HANDLE,         EINVAL    },  /* 124 */
65   {  ERROR_WAIT_NO_CHILDREN,       ECHILD    },  /* 128 */
66   {  ERROR_CHILD_NOT_COMPLETE,     ECHILD    },  /* 129 */
67   {  ERROR_DIRECT_ACCESS_HANDLE,   EBADF     },  /* 130 */
68   {  ERROR_NEGATIVE_SEEK,          EINVAL    },  /* 131 */
69   {  ERROR_SEEK_ON_DEVICE,         EACCES    },  /* 132 */
70   {  ERROR_DIR_NOT_EMPTY,          ENOTEMPTY },  /* 145 */
71   {  ERROR_NOT_LOCKED,             EACCES    },  /* 158 */
72   {  ERROR_BAD_PATHNAME,           ENOENT    },  /* 161 */
73   {  ERROR_MAX_THRDS_REACHED,      EAGAIN    },  /* 164 */
74   {  ERROR_LOCK_FAILED,            EACCES    },  /* 167 */
75   {  ERROR_ALREADY_EXISTS,         EEXIST    },  /* 183 */
76   {  ERROR_FILENAME_EXCED_RANGE,   ENOENT    },  /* 206 */
77   {  ERROR_NESTING_NOT_ALLOWED,    EAGAIN    },  /* 215 */
78   {  ERROR_FILE_SYSTEM_LIMITATION, EFBIG     },  /* 665 */
79   {  ERROR_NO_SYSTEM_RESOURCES,    ENOMEM    },  /* 1450 */
80   {  ERROR_NOT_ENOUGH_QUOTA,       ENOMEM    }   /* 1816 */
81 };
82 
83 /* size of the table */
84 #define ERRTABLESIZE (sizeof(errtable)/sizeof(errtable[0]))
85 
86 /* The following two constants must be the minimum and maximum
87 values in the (contiguous) range of Exec Failure errors. */
88 #define MIN_EXEC_ERROR ERROR_INVALID_STARTING_CODESEG
89 #define MAX_EXEC_ERROR ERROR_INFLOOP_IN_RELOC_CHAIN
90 
91 /* These are the low and high value in the range of errors that are
92 access violations */
93 #define MIN_EACCES_RANGE ERROR_WRITE_PROTECT
94 #define MAX_EACCES_RANGE ERROR_SHARING_BUFFER_EXCEEDED
95 
96 
get_errno_from_oserr(unsigned long oserrno)97 static int get_errno_from_oserr(unsigned long oserrno)
98 {
99   size_t i;
100 
101   /* check the table for the OS error code */
102   for (i= 0; i < ERRTABLESIZE; ++i)
103   {
104     if (oserrno == errtable[i].oscode)
105     {
106       return  errtable[i].sysv_errno;
107     }
108   }
109 
110   /* The error code wasn't in the table.  We check for a range of */
111   /* EACCES errors or exec failure errors (ENOEXEC).  Otherwise   */
112   /* EINVAL is returned.                                          */
113 
114   if (oserrno >= MIN_EACCES_RANGE && oserrno <= MAX_EACCES_RANGE)
115     return EACCES;
116   else if (oserrno >= MIN_EXEC_ERROR && oserrno <= MAX_EXEC_ERROR)
117     return ENOEXEC;
118   else
119     return EINVAL;
120 }
121 
122 /* Set errno corresponding to GetLastError() value */
my_osmaperr(unsigned long oserrno)123 void my_osmaperr ( unsigned long oserrno)
124 {
125     errno= get_errno_from_oserr(oserrno);
126 }
127