2004-03-24 04:54:16 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* open.c
|
|
|
|
* Win32 open() replacement
|
|
|
|
*
|
|
|
|
*
|
2024-01-04 02:49:05 +01:00
|
|
|
* Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
|
2004-03-24 04:54:16 +01:00
|
|
|
*
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/port/open.c
|
2004-03-24 04:54:16 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
2007-12-20 21:27:53 +01:00
|
|
|
#ifndef FRONTEND
|
|
|
|
#include "postgres.h"
|
|
|
|
#else
|
|
|
|
#include "postgres_fe.h"
|
|
|
|
#endif
|
2005-07-28 06:03:14 +02:00
|
|
|
|
2021-12-10 04:13:14 +01:00
|
|
|
#include "port/win32ntdll.h"
|
|
|
|
|
2004-03-24 04:54:16 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <assert.h>
|
In pgwin32_open, loop after ERROR_ACCESS_DENIED only if we can't stat.
This fixes a performance problem introduced by commit 6d7547c21.
ERROR_ACCESS_DENIED is returned in some other cases besides the
delete-pending case considered by that commit; notably, if the
given path names a directory instead of a plain file. In that
case we'll uselessly loop for 1 second before returning the
failure condition. That slows down some usage scenarios enough
to cause test timeout failures on our Windows buildfarm critters.
To fix, try to stat() the file, and sleep/loop only if that fails.
It will fail in the delete-pending case, and also in the case where
the deletion completed before we could stat(), so we have the cases
where we want to loop covered. In the directory case, the stat()
should succeed, letting us exit without a wait.
One case where we'll still wait uselessly is if the access-denied
problem pertains to a directory in the given pathname. But we don't
expect that to happen in any performance-critical code path.
There might be room to refine this further, but I'll push it now
in hopes of making the buildfarm green again.
Back-patch, like the preceding commit.
Alexander Lakhin and Tom Lane
Discussion: https://postgr.es/m/23073.1576626626@sss.pgh.pa.us
2019-12-21 23:39:36 +01:00
|
|
|
#include <sys/stat.h>
|
2004-03-24 04:54:16 +01:00
|
|
|
|
2004-04-19 19:42:59 +02:00
|
|
|
static int
|
|
|
|
openFlagsToCreateFileFlags(int openFlags)
|
2004-03-24 04:54:16 +01:00
|
|
|
{
|
|
|
|
switch (openFlags & (O_CREAT | O_TRUNC | O_EXCL))
|
|
|
|
{
|
2007-02-13 03:06:22 +01:00
|
|
|
/* O_EXCL is meaningless without O_CREAT */
|
2004-03-24 04:54:16 +01:00
|
|
|
case 0:
|
|
|
|
case O_EXCL:
|
|
|
|
return OPEN_EXISTING;
|
|
|
|
|
|
|
|
case O_CREAT:
|
|
|
|
return OPEN_ALWAYS;
|
|
|
|
|
2007-02-13 03:06:22 +01:00
|
|
|
/* O_EXCL is meaningless without O_CREAT */
|
2004-03-24 04:54:16 +01:00
|
|
|
case O_TRUNC:
|
|
|
|
case O_TRUNC | O_EXCL:
|
|
|
|
return TRUNCATE_EXISTING;
|
|
|
|
|
|
|
|
case O_CREAT | O_TRUNC:
|
|
|
|
return CREATE_ALWAYS;
|
|
|
|
|
2007-02-13 03:06:22 +01:00
|
|
|
/* O_TRUNC is meaningless with O_CREAT */
|
2004-03-24 04:54:16 +01:00
|
|
|
case O_CREAT | O_EXCL:
|
|
|
|
case O_CREAT | O_TRUNC | O_EXCL:
|
|
|
|
return CREATE_NEW;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* will never get here */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-12-10 04:13:14 +01:00
|
|
|
* Internal function used by pgwin32_open() and _pgstat64(). When
|
|
|
|
* backup_semantics is true, directories may be opened (for limited uses). On
|
|
|
|
* failure, INVALID_HANDLE_VALUE is returned and errno is set.
|
2004-03-24 04:54:16 +01:00
|
|
|
*/
|
2021-12-10 04:13:14 +01:00
|
|
|
HANDLE
|
|
|
|
pgwin32_open_handle(const char *fileName, int fileFlags, bool backup_semantics)
|
2004-03-24 04:54:16 +01:00
|
|
|
{
|
2021-12-10 04:13:14 +01:00
|
|
|
HANDLE h;
|
2004-03-24 04:54:16 +01:00
|
|
|
SECURITY_ATTRIBUTES sa;
|
2007-12-20 21:27:53 +01:00
|
|
|
int loops = 0;
|
2004-03-24 04:54:16 +01:00
|
|
|
|
2021-12-10 04:13:14 +01:00
|
|
|
if (initialize_ntdll() < 0)
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
|
2004-03-24 04:54:16 +01:00
|
|
|
/* Check that we can handle the request */
|
|
|
|
assert((fileFlags & ((O_RDONLY | O_WRONLY | O_RDWR) | O_APPEND |
|
|
|
|
(O_RANDOM | O_SEQUENTIAL | O_TEMPORARY) |
|
2007-04-13 12:30:30 +02:00
|
|
|
_O_SHORT_LIVED | O_DSYNC | O_DIRECT |
|
2004-03-24 04:54:16 +01:00
|
|
|
(O_CREAT | O_TRUNC | O_EXCL) | (O_TEXT | O_BINARY))) == fileFlags);
|
2018-09-20 01:54:37 +02:00
|
|
|
|
2004-03-24 04:54:16 +01:00
|
|
|
sa.nLength = sizeof(sa);
|
|
|
|
sa.bInheritHandle = TRUE;
|
|
|
|
sa.lpSecurityDescriptor = NULL;
|
|
|
|
|
2007-12-20 21:27:53 +01:00
|
|
|
while ((h = CreateFile(fileName,
|
2004-03-24 04:54:16 +01:00
|
|
|
/* cannot use O_RDONLY, as it == 0 */
|
|
|
|
(fileFlags & O_RDWR) ? (GENERIC_WRITE | GENERIC_READ) :
|
|
|
|
((fileFlags & O_WRONLY) ? GENERIC_WRITE : GENERIC_READ),
|
2004-10-18 01:53:30 +02:00
|
|
|
/* These flags allow concurrent rename/unlink */
|
2004-03-24 04:54:16 +01:00
|
|
|
(FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE),
|
|
|
|
&sa,
|
|
|
|
openFlagsToCreateFileFlags(fileFlags),
|
|
|
|
FILE_ATTRIBUTE_NORMAL |
|
2021-12-10 04:13:14 +01:00
|
|
|
(backup_semantics ? FILE_FLAG_BACKUP_SEMANTICS : 0) |
|
2004-03-24 04:54:16 +01:00
|
|
|
((fileFlags & O_RANDOM) ? FILE_FLAG_RANDOM_ACCESS : 0) |
|
|
|
|
((fileFlags & O_SEQUENTIAL) ? FILE_FLAG_SEQUENTIAL_SCAN : 0) |
|
|
|
|
((fileFlags & _O_SHORT_LIVED) ? FILE_ATTRIBUTE_TEMPORARY : 0) |
|
2005-02-27 01:53:29 +01:00
|
|
|
((fileFlags & O_TEMPORARY) ? FILE_FLAG_DELETE_ON_CLOSE : 0) |
|
2007-04-13 12:30:30 +02:00
|
|
|
((fileFlags & O_DIRECT) ? FILE_FLAG_NO_BUFFERING : 0) |
|
|
|
|
((fileFlags & O_DSYNC) ? FILE_FLAG_WRITE_THROUGH : 0),
|
2004-03-24 04:54:16 +01:00
|
|
|
NULL)) == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2007-12-20 21:27:53 +01:00
|
|
|
/*
|
|
|
|
* Sharing violation or locking error can indicate antivirus, backup
|
2019-12-16 21:10:55 +01:00
|
|
|
* or similar software that's locking the file. Wait a bit and try
|
|
|
|
* again, giving up after 30 seconds.
|
2007-12-20 21:27:53 +01:00
|
|
|
*/
|
|
|
|
DWORD err = GetLastError();
|
2009-06-11 16:49:15 +02:00
|
|
|
|
2007-12-20 21:27:53 +01:00
|
|
|
if (err == ERROR_SHARING_VIOLATION ||
|
|
|
|
err == ERROR_LOCK_VIOLATION)
|
|
|
|
{
|
|
|
|
#ifndef FRONTEND
|
|
|
|
if (loops == 50)
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("could not open file \"%s\": %s", fileName,
|
|
|
|
(err == ERROR_SHARING_VIOLATION) ? _("sharing violation") : _("lock violation")),
|
|
|
|
errdetail("Continuing to retry for 30 seconds."),
|
2007-12-28 13:32:56 +01:00
|
|
|
errhint("You might have antivirus, backup, or similar software interfering with the database system.")));
|
2007-12-20 21:27:53 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (loops < 300)
|
2019-12-16 21:10:55 +01:00
|
|
|
{
|
|
|
|
pg_usleep(100000);
|
|
|
|
loops++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
In pgwin32_open, loop after ERROR_ACCESS_DENIED only if we can't stat.
This fixes a performance problem introduced by commit 6d7547c21.
ERROR_ACCESS_DENIED is returned in some other cases besides the
delete-pending case considered by that commit; notably, if the
given path names a directory instead of a plain file. In that
case we'll uselessly loop for 1 second before returning the
failure condition. That slows down some usage scenarios enough
to cause test timeout failures on our Windows buildfarm critters.
To fix, try to stat() the file, and sleep/loop only if that fails.
It will fail in the delete-pending case, and also in the case where
the deletion completed before we could stat(), so we have the cases
where we want to loop covered. In the directory case, the stat()
should succeed, letting us exit without a wait.
One case where we'll still wait uselessly is if the access-denied
problem pertains to a directory in the given pathname. But we don't
expect that to happen in any performance-critical code path.
There might be room to refine this further, but I'll push it now
in hopes of making the buildfarm green again.
Back-patch, like the preceding commit.
Alexander Lakhin and Tom Lane
Discussion: https://postgr.es/m/23073.1576626626@sss.pgh.pa.us
2019-12-21 23:39:36 +01:00
|
|
|
* ERROR_ACCESS_DENIED is returned if the file is deleted but not yet
|
|
|
|
* gone (Windows NT status code is STATUS_DELETE_PENDING). In that
|
2021-12-10 04:13:14 +01:00
|
|
|
* case, we'd better ask for the NT status too so we can translate it
|
|
|
|
* to a more Unix-like error. We hope that nothing clobbers the NT
|
|
|
|
* status in between the internal NtCreateFile() call and CreateFile()
|
|
|
|
* returning.
|
|
|
|
*
|
|
|
|
* If there's no O_CREAT flag, then we'll pretend the file is
|
|
|
|
* invisible. With O_CREAT, we have no choice but to report that
|
|
|
|
* there's a file in the way (which wouldn't happen on Unix).
|
2019-12-16 21:10:55 +01:00
|
|
|
*/
|
2021-12-10 04:13:14 +01:00
|
|
|
if (err == ERROR_ACCESS_DENIED &&
|
|
|
|
pg_RtlGetLastNtStatus() == STATUS_DELETE_PENDING)
|
2019-12-16 21:10:55 +01:00
|
|
|
{
|
2021-12-10 04:13:14 +01:00
|
|
|
if (fileFlags & O_CREAT)
|
|
|
|
err = ERROR_FILE_EXISTS;
|
|
|
|
else
|
|
|
|
err = ERROR_FILE_NOT_FOUND;
|
2007-12-20 21:27:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_dosmaperr(err);
|
2021-12-10 04:13:14 +01:00
|
|
|
return INVALID_HANDLE_VALUE;
|
2004-03-24 04:54:16 +01:00
|
|
|
}
|
|
|
|
|
2021-12-10 04:13:14 +01:00
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
pgwin32_open(const char *fileName, int fileFlags,...)
|
|
|
|
{
|
|
|
|
HANDLE h;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
h = pgwin32_open_handle(fileName, fileFlags, false);
|
|
|
|
if (h == INVALID_HANDLE_VALUE)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
#ifdef FRONTEND
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Since PostgreSQL 12, those concurrent-safe versions of open() and
|
|
|
|
* fopen() can be used by frontends, having as side-effect to switch the
|
|
|
|
* file-translation mode from O_TEXT to O_BINARY if none is specified.
|
|
|
|
* Caller may want to enforce the binary or text mode, but if nothing is
|
|
|
|
* defined make sure that the default mode maps with what versions older
|
|
|
|
* than 12 have been doing.
|
|
|
|
*/
|
|
|
|
if ((fileFlags & O_BINARY) == 0)
|
|
|
|
fileFlags |= O_TEXT;
|
|
|
|
#endif
|
|
|
|
|
2004-03-24 04:54:16 +01:00
|
|
|
/* _open_osfhandle will, on error, set errno accordingly */
|
2010-01-02 13:00:08 +01:00
|
|
|
if ((fd = _open_osfhandle((intptr_t) h, fileFlags & O_APPEND)) < 0)
|
2004-03-24 04:54:16 +01:00
|
|
|
CloseHandle(h); /* will not affect errno */
|
2006-10-03 22:44:18 +02:00
|
|
|
else if (fileFlags & (O_TEXT | O_BINARY) &&
|
|
|
|
_setmode(fd, fileFlags & (O_TEXT | O_BINARY)) < 0)
|
|
|
|
{
|
|
|
|
_close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-03-24 04:54:16 +01:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2006-08-30 20:06:27 +02:00
|
|
|
FILE *
|
|
|
|
pgwin32_fopen(const char *fileName, const char *mode)
|
|
|
|
{
|
|
|
|
int openmode = 0;
|
|
|
|
int fd;
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2006-08-30 20:06:27 +02:00
|
|
|
if (strstr(mode, "r+"))
|
|
|
|
openmode |= O_RDWR;
|
|
|
|
else if (strchr(mode, 'r'))
|
|
|
|
openmode |= O_RDONLY;
|
|
|
|
if (strstr(mode, "w+"))
|
|
|
|
openmode |= O_RDWR | O_CREAT | O_TRUNC;
|
|
|
|
else if (strchr(mode, 'w'))
|
|
|
|
openmode |= O_WRONLY | O_CREAT | O_TRUNC;
|
|
|
|
if (strchr(mode, 'a'))
|
2006-09-24 19:19:53 +02:00
|
|
|
openmode |= O_WRONLY | O_CREAT | O_APPEND;
|
2006-08-30 20:06:27 +02:00
|
|
|
|
|
|
|
if (strchr(mode, 'b'))
|
|
|
|
openmode |= O_BINARY;
|
|
|
|
if (strchr(mode, 't'))
|
|
|
|
openmode |= O_TEXT;
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2006-08-30 20:06:27 +02:00
|
|
|
fd = pgwin32_open(fileName, openmode);
|
|
|
|
if (fd == -1)
|
|
|
|
return NULL;
|
|
|
|
return _fdopen(fd, mode);
|
|
|
|
}
|
|
|
|
|
2004-03-24 04:54:16 +01:00
|
|
|
#endif
|