2004-03-24 04:54:16 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* open.c
|
|
|
|
* Win32 open() replacement
|
|
|
|
*
|
|
|
|
*
|
2018-01-03 05:30:12 +01:00
|
|
|
* Portions Copyright (c) 1996-2018, 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>
|
|
|
|
|
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);
|
2004-03-24 04:54:16 +01:00
|
|
|
|
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
|
2009-06-11 16:49:15 +02:00
|
|
|
* or similar software that's locking the file. Try again for 30
|
|
|
|
* seconds before giving up.
|
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)
|
|
|
|
{
|
|
|
|
pg_usleep(100000);
|
|
|
|
loops++;
|
|
|
|
|
|
|
|
#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)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
_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
|