2004-03-24 04:54:16 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* open.c
|
|
|
|
* Win32 open() replacement
|
|
|
|
*
|
|
|
|
*
|
2019-01-02 18:44:25 +01:00
|
|
|
* Portions Copyright (c) 1996-2019, 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
|
|
|
|
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-11-17 09:30:11 +01:00
|
|
|
|
2004-04-19 19:42:59 +02:00
|
|
|
static int
|
|
|
|
openFlagsToCreateFileFlags(int openFlags)
|
2004-03-24 04:54:16 +01:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
switch (openFlags & (O_CREAT | O_TRUNC | O_EXCL))
|
2004-03-24 04:54:16 +01:00
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
/* O_EXCL is meaningless without O_CREAT */
|
2004-03-24 04:54:16 +01:00
|
|
|
case 0:
|
2004-08-29 07:07:03 +02:00
|
|
|
case O_EXCL:
|
|
|
|
return OPEN_EXISTING;
|
2004-03-24 04:54:16 +01:00
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
case O_CREAT:
|
|
|
|
return OPEN_ALWAYS;
|
2004-03-24 04:54:16 +01:00
|
|
|
|
2007-11-15 22:14:46 +01:00
|
|
|
/* O_EXCL is meaningless without O_CREAT */
|
2004-03-24 04:54:16 +01:00
|
|
|
case O_TRUNC:
|
2004-08-29 07:07:03 +02:00
|
|
|
case O_TRUNC | O_EXCL:
|
|
|
|
return TRUNCATE_EXISTING;
|
2004-03-24 04:54:16 +01:00
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
case O_CREAT | O_TRUNC:
|
|
|
|
return CREATE_ALWAYS;
|
2004-03-24 04:54:16 +01:00
|
|
|
|
2007-11-15 22:14:46 +01:00
|
|
|
/* O_TRUNC is meaningless with O_CREAT */
|
2004-08-29 07:07:03 +02:00
|
|
|
case O_CREAT | O_EXCL:
|
|
|
|
case O_CREAT | O_TRUNC | O_EXCL:
|
|
|
|
return CREATE_NEW;
|
2004-03-24 04:54:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* will never get here */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-08-29 07:07:03 +02:00
|
|
|
* - file attribute setting, based on fileMode?
|
2004-03-24 04:54:16 +01:00
|
|
|
*/
|
2004-08-29 07:07:03 +02:00
|
|
|
int
|
2006-06-25 02:18:24 +02:00
|
|
|
pgwin32_open(const char *fileName, int fileFlags,...)
|
2004-03-24 04:54:16 +01:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
int fd;
|
2007-12-20 21:27:53 +01:00
|
|
|
HANDLE h = INVALID_HANDLE_VALUE;
|
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
|
|
|
|
|
|
|
/* Check that we can handle the request */
|
2004-08-29 07:07:03 +02:00
|
|
|
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 |
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
(O_CREAT | O_TRUNC | O_EXCL) | (O_TEXT | O_BINARY))) == fileFlags);
|
2019-04-04 02:11:16 +02:00
|
|
|
#ifndef FRONTEND
|
|
|
|
Assert(pgwin32_signal_event != NULL); /* small chance of pg_usleep() */
|
|
|
|
#endif
|
2004-03-24 04:54:16 +01:00
|
|
|
|
2018-09-20 01:54:37 +02:00
|
|
|
#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-08-29 07:07:03 +02:00
|
|
|
sa.nLength = sizeof(sa);
|
|
|
|
sa.bInheritHandle = TRUE;
|
|
|
|
sa.lpSecurityDescriptor = NULL;
|
2004-03-24 04:54:16 +01:00
|
|
|
|
2007-12-20 21:27:53 +01:00
|
|
|
while ((h = CreateFile(fileName,
|
2004-08-29 07:07:03 +02:00
|
|
|
/* cannot use O_RDONLY, as it == 0 */
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
(fileFlags & O_RDWR) ? (GENERIC_WRITE | GENERIC_READ) :
|
|
|
|
((fileFlags & O_WRONLY) ? GENERIC_WRITE : GENERIC_READ),
|
2005-10-15 04:49:52 +02:00
|
|
|
/* These flags allow concurrent rename/unlink */
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
(FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE),
|
2009-06-11 16:49:15 +02:00
|
|
|
&sa,
|
|
|
|
openFlagsToCreateFileFlags(fileFlags),
|
|
|
|
FILE_ATTRIBUTE_NORMAL |
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02: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) |
|
|
|
|
((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),
|
2009-06-11 16:49:15 +02:00
|
|
|
NULL)) == INVALID_HANDLE_VALUE)
|
2004-03-24 04:54:16 +01:00
|
|
|
{
|
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
|
|
|
*/
|
2009-06-11 16:49:15 +02:00
|
|
|
DWORD err = GetLastError();
|
|
|
|
|
|
|
|
if (err == ERROR_SHARING_VIOLATION ||
|
2007-12-20 21:27:53 +01:00
|
|
|
err == ERROR_LOCK_VIOLATION)
|
|
|
|
{
|
|
|
|
#ifndef FRONTEND
|
|
|
|
if (loops == 50)
|
|
|
|
ereport(LOG,
|
2009-06-11 16:49:15 +02:00
|
|
|
(errmsg("could not open file \"%s\": %s", fileName,
|
|
|
|
(err == ERROR_SHARING_VIOLATION) ? _("sharing violation") : _("lock violation")),
|
|
|
|
errdetail("Continuing to retry for 30 seconds."),
|
|
|
|
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
|
|
|
|
* case we want to wait a bit and try again, giving up after 1 second
|
|
|
|
* (since this condition should never persist very long). However,
|
|
|
|
* there are other commonly-hit cases that return ERROR_ACCESS_DENIED,
|
|
|
|
* so care is needed. In particular that happens if we try to open a
|
|
|
|
* directory, or of course if there's an actual file-permissions
|
|
|
|
* problem. To distinguish these cases, try a stat(). In the
|
|
|
|
* delete-pending case, it will either also get STATUS_DELETE_PENDING,
|
|
|
|
* or it will see the file as gone and fail with ENOENT. In other
|
|
|
|
* cases it will usually succeed. The only somewhat-likely case where
|
|
|
|
* this coding will uselessly wait is if there's a permissions problem
|
|
|
|
* with a containing directory, which we hope will never happen in any
|
|
|
|
* performance-critical code paths.
|
2019-12-16 21:10:55 +01:00
|
|
|
*/
|
|
|
|
if (err == ERROR_ACCESS_DENIED)
|
|
|
|
{
|
|
|
|
if (loops < 10)
|
|
|
|
{
|
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
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat(fileName, &st) != 0)
|
|
|
|
{
|
|
|
|
pg_usleep(100000);
|
|
|
|
loops++;
|
|
|
|
continue;
|
|
|
|
}
|
2019-12-16 21:10:55 +01:00
|
|
|
}
|
2007-12-20 21:27:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_dosmaperr(err);
|
2004-03-24 04:54:16 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* _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-08-29 07:07:03 +02:00
|
|
|
CloseHandle(h); /* will not affect errno */
|
2006-10-03 22:44:18 +02:00
|
|
|
else if (fileFlags & (O_TEXT | O_BINARY) &&
|
2006-10-04 02:30:14 +02:00
|
|
|
_setmode(fd, fileFlags & (O_TEXT | O_BINARY)) < 0)
|
2006-10-03 22:44:18 +02:00
|
|
|
{
|
|
|
|
_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)
|
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
int openmode = 0;
|
|
|
|
int fd;
|
|
|
|
|
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
|