From 704ff0b2f25b62cce26e573eafa96ed7e5b6f04e Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Sun, 29 Aug 2004 00:38:03 +0000
Subject: [PATCH] >>> I understand your disliking of non-posix stuff. OTOH,
 >>GetLastError will >>> give much more details than errno. >> >>How much
 more, really?  That mapping table gave me the impression that >>the win32
 error codes aren't all that much more detailed than errno... > >The mapping
 table is not complete. My winerror.h from the SDK >lists 2209 >error codes,
 whereas errno.h lists 42... > >I still don't think we'll get that much more
 stuff. Right now, >the Win32 >code paths that actually use the more advanced
 functions already write >out the error number in case something happens. We
 can keep doing that >for the other paths (ereport the error *number* when the
 mapping does >not have a match). The map to errno will catch almost all
 cases, I >think. And in the corner cases we can do with just the number, and
 use >"net helpmsg" to get the actual message when checking...

Here's an attempt on this. new file goes in backend/port/win32.

Magnus Hagander
---
 src/backend/port/win32/Makefile    |  4 +-
 src/backend/port/win32/error.c     | 92 ++++++++++++++++++++++++++++++
 src/backend/port/win32/shmem.c     |  5 +-
 src/backend/postmaster/syslogger.c |  3 +-
 src/include/port/win32.h           |  6 +-
 5 files changed, 104 insertions(+), 6 deletions(-)
 create mode 100644 src/backend/port/win32/error.c

diff --git a/src/backend/port/win32/Makefile b/src/backend/port/win32/Makefile
index 8069d590fd4..bb12ae375c9 100644
--- a/src/backend/port/win32/Makefile
+++ b/src/backend/port/win32/Makefile
@@ -4,7 +4,7 @@
 #    Makefile for port/win32
 #
 # IDENTIFICATION
-#    $PostgreSQL: pgsql/src/backend/port/win32/Makefile,v 1.5 2004/06/24 21:02:42 tgl Exp $
+#    $PostgreSQL: pgsql/src/backend/port/win32/Makefile,v 1.6 2004/08/29 00:38:03 momjian Exp $
 #
 #-------------------------------------------------------------------------
 
@@ -12,7 +12,7 @@ subdir = src/backend/port/win32
 top_builddir = ../../../..
 include $(top_builddir)/src/Makefile.global
 
-OBJS = sema.o shmem.o timer.o socket.o signal.o security.o
+OBJS = sema.o shmem.o timer.o socket.o signal.o security.o error.o
 
 all: SUBSYS.o
 
diff --git a/src/backend/port/win32/error.c b/src/backend/port/win32/error.c
new file mode 100644
index 00000000000..6a8901a642f
--- /dev/null
+++ b/src/backend/port/win32/error.c
@@ -0,0 +1,92 @@
+/*-------------------------------------------------------------------------
+ *
+ * error.c
+ *    Map win32 error codes to errno values
+ *
+ * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ *	  $PostgreSQL: pgsql/src/backend/port/win32/error.c,v 1.1 2004/08/29 00:38:03 momjian Exp $
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "postgres.h"
+
+static struct { DWORD winerr; int doserr;} doserrors[] =
+{
+	{  ERROR_INVALID_FUNCTION,       EINVAL    },
+	{  ERROR_FILE_NOT_FOUND,         ENOENT    },
+	{  ERROR_PATH_NOT_FOUND,         ENOENT    },
+	{  ERROR_TOO_MANY_OPEN_FILES,    EMFILE    },
+	{  ERROR_ACCESS_DENIED,          EACCES    },
+	{  ERROR_INVALID_HANDLE,         EBADF     },
+	{  ERROR_ARENA_TRASHED,          ENOMEM    },
+	{  ERROR_NOT_ENOUGH_MEMORY,      ENOMEM    },
+	{  ERROR_INVALID_BLOCK,          ENOMEM    },
+	{  ERROR_BAD_ENVIRONMENT,        E2BIG     },
+	{  ERROR_BAD_FORMAT,             ENOEXEC   },
+	{  ERROR_INVALID_ACCESS,         EINVAL    },
+	{  ERROR_INVALID_DATA,           EINVAL    },
+	{  ERROR_INVALID_DRIVE,          ENOENT    },
+	{  ERROR_CURRENT_DIRECTORY,      EACCES    },
+	{  ERROR_NOT_SAME_DEVICE,        EXDEV     },
+	{  ERROR_NO_MORE_FILES,          ENOENT    },
+	{  ERROR_LOCK_VIOLATION,         EACCES    },
+	{  ERROR_BAD_NETPATH,            ENOENT    },
+	{  ERROR_NETWORK_ACCESS_DENIED,  EACCES    },
+	{  ERROR_BAD_NET_NAME,           ENOENT    },
+	{  ERROR_FILE_EXISTS,            EEXIST    },
+	{  ERROR_CANNOT_MAKE,            EACCES    },
+	{  ERROR_FAIL_I24,               EACCES    },
+	{  ERROR_INVALID_PARAMETER,      EINVAL    },
+	{  ERROR_NO_PROC_SLOTS,          EAGAIN    },
+	{  ERROR_DRIVE_LOCKED,           EACCES    },
+	{  ERROR_BROKEN_PIPE,            EPIPE     },
+	{  ERROR_DISK_FULL,              ENOSPC    },
+	{  ERROR_INVALID_TARGET_HANDLE,  EBADF     },
+	{  ERROR_INVALID_HANDLE,         EINVAL    },
+	{  ERROR_WAIT_NO_CHILDREN,       ECHILD    },
+	{  ERROR_CHILD_NOT_COMPLETE,     ECHILD    },
+	{  ERROR_DIRECT_ACCESS_HANDLE,   EBADF     },
+	{  ERROR_NEGATIVE_SEEK,          EINVAL    },
+	{  ERROR_SEEK_ON_DEVICE,         EACCES    },
+	{  ERROR_DIR_NOT_EMPTY,          ENOTEMPTY },
+	{  ERROR_NOT_LOCKED,             EACCES    },
+	{  ERROR_BAD_PATHNAME,           ENOENT    },
+	{  ERROR_MAX_THRDS_REACHED,      EAGAIN    },
+	{  ERROR_LOCK_FAILED,            EACCES    },
+	{  ERROR_ALREADY_EXISTS,         EEXIST    },
+	{  ERROR_FILENAME_EXCED_RANGE,   ENOENT    },
+	{  ERROR_NESTING_NOT_ALLOWED,    EAGAIN    },
+	{  ERROR_NOT_ENOUGH_QUOTA,       ENOMEM    }
+};
+
+void _dosmaperr(unsigned long e)
+{
+	int i;
+
+	if (e == 0)
+	{
+		errno = 0;
+		return;
+	}
+
+	for (i=0; i<sizeof(doserrors)/sizeof(doserrors[0]); i++)
+	{
+		if (doserrors[i].winerr == e)
+		{
+			errno = doserrors[i].doserr;
+			ereport(DEBUG5,
+					(errmsg_internal("Mapped win32 error code %i to %i",
+									 (int)e, errno)));
+			return;
+		}
+	}
+
+	ereport(DEBUG4,
+			(errmsg_internal("Unknown win32 error code: %i",
+							 (int)e)));
+	errno = EINVAL;
+	return;
+}
diff --git a/src/backend/port/win32/shmem.c b/src/backend/port/win32/shmem.c
index 89bcadc8cdc..99e4b467e40 100644
--- a/src/backend/port/win32/shmem.c
+++ b/src/backend/port/win32/shmem.c
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/port/win32/shmem.c,v 1.5 2004/04/19 17:42:58 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/port/win32/shmem.c,v 1.6 2004/08/29 00:38:03 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -40,7 +40,7 @@ shmat(int memId, void *shmaddr, int flag)
 	if (lpmem == NULL)
 	{
 		lpmem = (void *) -1;
-		errno = GetLastError();
+		_dosmaperr(GetLastError());
 	}
 
 	return lpmem;
@@ -123,6 +123,7 @@ shmget(int memKey, int size, int flag)
 	else if (!hmap)
 	{
 		/* Unable to get shared memory */
+		_dosmaperr(GetLastError());
 		return -1;
 	}
 
diff --git a/src/backend/postmaster/syslogger.c b/src/backend/postmaster/syslogger.c
index 7f98c1ec01d..fd9acf4d380 100644
--- a/src/backend/postmaster/syslogger.c
+++ b/src/backend/postmaster/syslogger.c
@@ -18,7 +18,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/postmaster/syslogger.c,v 1.5 2004/08/09 20:28:48 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/postmaster/syslogger.c,v 1.6 2004/08/29 00:38:03 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -706,6 +706,7 @@ pipeThread(void *arg)
 			if (error == ERROR_HANDLE_EOF ||
 				error == ERROR_BROKEN_PIPE)
 				break;
+			_dosmaperr(error);
 			ereport(LOG,
 					(errcode_for_file_access(),
 					 errmsg("could not read from logger pipe: %m")));
diff --git a/src/include/port/win32.h b/src/include/port/win32.h
index 6a70bcc095d..bd2e69c0433 100644
--- a/src/include/port/win32.h
+++ b/src/include/port/win32.h
@@ -1,4 +1,4 @@
-/* $PostgreSQL: pgsql/src/include/port/win32.h,v 1.27 2004/07/23 01:58:36 momjian Exp $ */
+/* $PostgreSQL: pgsql/src/include/port/win32.h,v 1.28 2004/08/29 00:38:03 momjian Exp $ */
 
 /* undefine and redefine after #include */
 #undef mkdir
@@ -147,6 +147,9 @@ extern int pgwin32_is_admin(void);
 extern int pgwin32_is_service(void);
 #endif
 
+/* in backend/port/win32/error.c */
+void _dosmaperr(unsigned long);
+
 
 #define WEXITSTATUS(w)  (((w) >> 8) & 0xff)
 #define WIFEXITED(w)    (((w) & 0xff) == 0)
@@ -222,3 +225,4 @@ int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue
 #define ECONNREFUSED WSAECONNREFUSED
 #define EBADFD WSAENOTSOCK
 #define EOPNOTSUPP WSAEOPNOTSUPP
+
-- 
GitLab