PostgreSQL Source Code  git master
win32error.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * win32error.c
4  * Map win32 error codes to errno values
5  *
6  * Portions Copyright (c) 1996-2024, 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  ERROR_INVALID_NAME, ENOENT
170  },
171  {
172  ERROR_CANT_RESOLVE_FILENAME, ENOENT
173  }
174 };
175 
176 void
177 _dosmaperr(unsigned long e)
178 {
179  int i;
180 
181  if (e == 0)
182  {
183  errno = 0;
184  return;
185  }
186 
187  for (i = 0; i < lengthof(doserrors); i++)
188  {
189  if (doserrors[i].winerr == e)
190  {
191  int doserr = doserrors[i].doserr;
192 
193 #ifndef FRONTEND
194  ereport(DEBUG5,
195  (errmsg_internal("mapped win32 error code %lu to %d",
196  e, doserr)));
197 #elif defined(FRONTEND_DEBUG)
198  fprintf(stderr, "mapped win32 error code %lu to %d", e, doserr);
199 #endif
200  errno = doserr;
201  return;
202  }
203  }
204 
205 #ifndef FRONTEND
206  ereport(LOG,
207  (errmsg_internal("unrecognized win32 error code: %lu",
208  e)));
209 #else
210  fprintf(stderr, "unrecognized win32 error code: %lu", e);
211 #endif
212 
213  errno = EINVAL;
214 }
#define lengthof(array)
Definition: c.h:742
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1157
#define LOG
Definition: elog.h:31
#define ereport(elevel,...)
Definition: elog.h:149
#define DEBUG5
Definition: elog.h:26
int i
Definition: isn.c:72
#define fprintf
Definition: port.h:242
e
Definition: preproc-init.c:82
#define EAGAIN
Definition: win32_port.h:372
int doserr
Definition: win32error.c:23
void _dosmaperr(unsigned long e)
Definition: win32error.c:177
static const struct @187 doserrors[]
DWORD winerr
Definition: win32error.c:22