diff -cprN head/src/port/open.c eventlog/src/port/open.c
*** head/src/port/open.c	2009-06-11 23:49:15.000000000 +0900
--- eventlog/src/port/open.c	2009-09-15 12:31:24.556451161 +0900
***************
*** 23,28 ****
--- 23,31 ----
  #include <fcntl.h>
  #include <assert.h>
  
+ #ifndef FRONTEND
+ #include "mb/pg_wchar.h"
+ #endif
  
  static int
  openFlagsToCreateFileFlags(int openFlags)
*************** pgwin32_open(const char *fileName, int f
*** 65,70 ****
--- 68,78 ----
  	HANDLE		h = INVALID_HANDLE_VALUE;
  	SECURITY_ATTRIBUTES sa;
  	int			loops = 0;
+ 	DWORD		dwDesiredAccess;
+ 	DWORD		dwShareMode;
+ 	DWORD		dwCreationDisposition;
+ 	DWORD		dwFlagsAndAttributes;
+ 	WCHAR	   *wFileName = NULL;
  
  	/* Check that we can handle the request */
  	assert((fileFlags & ((O_RDONLY | O_WRONLY | O_RDWR) | O_APPEND |
*************** pgwin32_open(const char *fileName, int f
*** 72,97 ****
  						 _O_SHORT_LIVED | O_DSYNC | O_DIRECT |
  		  (O_CREAT | O_TRUNC | O_EXCL) | (O_TEXT | O_BINARY))) == fileFlags);
  
- 	sa.nLength = sizeof(sa);
- 	sa.bInheritHandle = TRUE;
- 	sa.lpSecurityDescriptor = NULL;
- 
- 	while ((h = CreateFile(fileName,
  	/* cannot use O_RDONLY, as it == 0 */
! 					  (fileFlags & O_RDWR) ? (GENERIC_WRITE | GENERIC_READ) :
! 					 ((fileFlags & O_WRONLY) ? GENERIC_WRITE : GENERIC_READ),
  	/* These flags allow concurrent rename/unlink */
! 					(FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE),
! 						   &sa,
! 						   openFlagsToCreateFileFlags(fileFlags),
! 						   FILE_ATTRIBUTE_NORMAL |
  					 ((fileFlags & O_RANDOM) ? FILE_FLAG_RANDOM_ACCESS : 0) |
  			   ((fileFlags & O_SEQUENTIAL) ? FILE_FLAG_SEQUENTIAL_SCAN : 0) |
  			  ((fileFlags & _O_SHORT_LIVED) ? FILE_ATTRIBUTE_TEMPORARY : 0) |
  				((fileFlags & O_TEMPORARY) ? FILE_FLAG_DELETE_ON_CLOSE : 0) |
  					  ((fileFlags & O_DIRECT) ? FILE_FLAG_NO_BUFFERING : 0) |
! 					   ((fileFlags & O_DSYNC) ? FILE_FLAG_WRITE_THROUGH : 0),
! 						   NULL)) == INVALID_HANDLE_VALUE)
  	{
  		/*
  		 * Sharing violation or locking error can indicate antivirus, backup
--- 80,126 ----
  						 _O_SHORT_LIVED | O_DSYNC | O_DIRECT |
  		  (O_CREAT | O_TRUNC | O_EXCL) | (O_TEXT | O_BINARY))) == fileFlags);
  
  	/* cannot use O_RDONLY, as it == 0 */
! 	dwDesiredAccess = (fileFlags & O_RDWR) ? (GENERIC_WRITE | GENERIC_READ) :
! 						((fileFlags & O_WRONLY) ? GENERIC_WRITE : GENERIC_READ);
  	/* These flags allow concurrent rename/unlink */
! 	dwShareMode = (FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE);
!     dwCreationDisposition = openFlagsToCreateFileFlags(fileFlags);
!     dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL |
  					 ((fileFlags & O_RANDOM) ? FILE_FLAG_RANDOM_ACCESS : 0) |
  			   ((fileFlags & O_SEQUENTIAL) ? FILE_FLAG_SEQUENTIAL_SCAN : 0) |
  			  ((fileFlags & _O_SHORT_LIVED) ? FILE_ATTRIBUTE_TEMPORARY : 0) |
  				((fileFlags & O_TEMPORARY) ? FILE_FLAG_DELETE_ON_CLOSE : 0) |
  					  ((fileFlags & O_DIRECT) ? FILE_FLAG_NO_BUFFERING : 0) |
! 					   ((fileFlags & O_DSYNC) ? FILE_FLAG_WRITE_THROUGH : 0);
! 
! 	sa.nLength = sizeof(sa);
! 	sa.bInheritHandle = TRUE;
! 	sa.lpSecurityDescriptor = NULL;
! 
! #ifndef FRONTEND
! 	/*
! 	 * Use wide-character file name only if the database encoding doesn't match
! 	 * to the platform encoding and the path contains any multi-byte characters.
! 	 */
! 	if (GetDatabaseEncoding() != GetPlatformEncoding())
! 	{
! 		int		len;
! 		bool	hasMBChar = false;
! 
! 		for (len = 0; fileName[len]; len++)
! 			hasMBChar |= IS_HIGHBIT_SET(fileName[len]);
! 		if (hasMBChar)
! 			wFileName = pgwin32_toUTF16(fileName, len, NULL);
! 	}
! #endif
! 
! 	while ((h = (wFileName != NULL
! 				? CreateFileW(wFileName, dwDesiredAccess, dwShareMode, &sa,
! 						dwCreationDisposition, dwFlagsAndAttributes, NULL)
! 				: CreateFileA(fileName, dwDesiredAccess, dwShareMode, &sa,
! 						dwCreationDisposition, dwFlagsAndAttributes, NULL))
! 		   ) == INVALID_HANDLE_VALUE)
  	{
  		/*
  		 * Sharing violation or locking error can indicate antivirus, backup
*************** pgwin32_open(const char *fileName, int f
*** 119,128 ****
--- 148,166 ----
  				continue;
  		}
  
+ #ifndef FRONTEND
+ 		if (wFileName)
+ 			pfree(wFileName);
+ #endif
  		_dosmaperr(err);
  		return -1;
  	}
  
+ #ifndef FRONTEND
+ 	if (wFileName)
+ 		pfree(wFileName);
+ #endif
+ 
  	/* _open_osfhandle will, on error, set errno accordingly */
  	if ((fd = _open_osfhandle((long) h, fileFlags & O_APPEND)) < 0)
  		CloseHandle(h);			/* will not affect errno */