1 /*-------------------------------------------------------------------------
2 *
3 * win32error.c
4 * Map win32 error codes to errno values
5 *
6 * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
7 *
8 * IDENTIFICATION
9 * src/port/win32error.c
10 *
11 *-------------------------------------------------------------------------
12 */
13
14 #ifndef FRONTEND
15 #include "postgres.h"
16 #else
17 #include "postgres_fe.h"
18 #endif
19
20 static const struct
21 {
22 DWORD winerr;
23 int doserr;
24 } doserrors[] =
25
26 {
27 {
28 ERROR_INVALID_FUNCTION, EINVAL
29 },
30 {
31 ERROR_FILE_NOT_FOUND, ENOENT
32 },
33 {
34 ERROR_PATH_NOT_FOUND, ENOENT
35 },
36 {
37 ERROR_TOO_MANY_OPEN_FILES, EMFILE
38 },
39 {
40 ERROR_ACCESS_DENIED, EACCES
41 },
42 {
43 ERROR_INVALID_HANDLE, EBADF
44 },
45 {
46 ERROR_ARENA_TRASHED, ENOMEM
47 },
48 {
49 ERROR_NOT_ENOUGH_MEMORY, ENOMEM
50 },
51 {
52 ERROR_INVALID_BLOCK, ENOMEM
53 },
54 {
55 ERROR_BAD_ENVIRONMENT, E2BIG
56 },
57 {
58 ERROR_BAD_FORMAT, ENOEXEC
59 },
60 {
61 ERROR_INVALID_ACCESS, EINVAL
62 },
63 {
64 ERROR_INVALID_DATA, EINVAL
65 },
66 {
67 ERROR_INVALID_DRIVE, ENOENT
68 },
69 {
70 ERROR_CURRENT_DIRECTORY, EACCES
71 },
72 {
73 ERROR_NOT_SAME_DEVICE, EXDEV
74 },
75 {
76 ERROR_NO_MORE_FILES, ENOENT
77 },
78 {
79 ERROR_LOCK_VIOLATION, EACCES
80 },
81 {
82 ERROR_SHARING_VIOLATION, EACCES
83 },
84 {
85 ERROR_BAD_NETPATH, ENOENT
86 },
87 {
88 ERROR_NETWORK_ACCESS_DENIED, EACCES
89 },
90 {
91 ERROR_BAD_NET_NAME, ENOENT
92 },
93 {
94 ERROR_FILE_EXISTS, EEXIST
95 },
96 {
97 ERROR_CANNOT_MAKE, EACCES
98 },
99 {
100 ERROR_FAIL_I24, EACCES
101 },
102 {
103 ERROR_INVALID_PARAMETER, EINVAL
104 },
105 {
106 ERROR_NO_PROC_SLOTS, EAGAIN
107 },
108 {
109 ERROR_DRIVE_LOCKED, EACCES
110 },
111 {
112 ERROR_BROKEN_PIPE, EPIPE
113 },
114 {
115 ERROR_DISK_FULL, ENOSPC
116 },
117 {
118 ERROR_INVALID_TARGET_HANDLE, EBADF
119 },
120 {
121 ERROR_INVALID_HANDLE, EINVAL
122 },
123 {
124 ERROR_WAIT_NO_CHILDREN, ECHILD
125 },
126 {
127 ERROR_CHILD_NOT_COMPLETE, ECHILD
128 },
129 {
130 ERROR_DIRECT_ACCESS_HANDLE, EBADF
131 },
132 {
133 ERROR_NEGATIVE_SEEK, EINVAL
134 },
135 {
136 ERROR_SEEK_ON_DEVICE, EACCES
137 },
138 {
139 ERROR_DIR_NOT_EMPTY, ENOTEMPTY
140 },
141 {
142 ERROR_NOT_LOCKED, EACCES
143 },
144 {
145 ERROR_BAD_PATHNAME, ENOENT
146 },
147 {
148 ERROR_MAX_THRDS_REACHED, EAGAIN
149 },
150 {
151 ERROR_LOCK_FAILED, EACCES
152 },
153 {
154 ERROR_ALREADY_EXISTS, EEXIST
155 },
156 {
157 ERROR_FILENAME_EXCED_RANGE, ENOENT
158 },
159 {
160 ERROR_NESTING_NOT_ALLOWED, EAGAIN
161 },
162 {
163 ERROR_NOT_ENOUGH_QUOTA, ENOMEM
164 },
165 {
166 ERROR_DELETE_PENDING, ENOENT
167 }
168 };
169
170 void
_dosmaperr(unsigned long e)171 _dosmaperr(unsigned long e)
172 {
173 int i;
174
175 if (e == 0)
176 {
177 errno = 0;
178 return;
179 }
180
181 for (i = 0; i < lengthof(doserrors); i++)
182 {
183 if (doserrors[i].winerr == e)
184 {
185 int doserr = doserrors[i].doserr;
186
187 #ifndef FRONTEND
188 ereport(DEBUG5,
189 (errmsg_internal("mapped win32 error code %lu to %d",
190 e, doserr)));
191 #elif FRONTEND_DEBUG
192 fprintf(stderr, "mapped win32 error code %lu to %d", e, doserr);
193 #endif
194 errno = doserr;
195 return;
196 }
197 }
198
199 #ifndef FRONTEND
200 ereport(LOG,
201 (errmsg_internal("unrecognized win32 error code: %lu",
202 e)));
203 #else
204 fprintf(stderr, "unrecognized win32 error code: %lu", e);
205 #endif
206
207 errno = EINVAL;
208 return;
209 }
210