29openFlagsToCreateFileFlags(
int openFlags)
31 switch (openFlags & (O_CREAT | O_TRUNC | O_EXCL))
43 case O_TRUNC | O_EXCL:
44 return TRUNCATE_EXISTING;
46 case O_CREAT | O_TRUNC:
50 case O_CREAT | O_EXCL:
51 case O_CREAT | O_TRUNC | O_EXCL:
65pgwin32_open_handle(
const char *fileName,
int fileFlags,
bool backup_semantics)
68 SECURITY_ATTRIBUTES
sa;
72 return INVALID_HANDLE_VALUE;
75 assert((fileFlags & ((O_RDONLY | O_WRONLY | O_RDWR) | O_APPEND |
76 (O_RANDOM | O_SEQUENTIAL | O_TEMPORARY) |
78 (O_CREAT | O_TRUNC | O_EXCL) | (O_TEXT | O_BINARY))) == fileFlags);
80 sa.nLength =
sizeof(
sa);
81 sa.lpSecurityDescriptor = NULL;
94 while ((h = CreateFile(fileName,
96 (fileFlags & O_RDWR) ? (GENERIC_WRITE | GENERIC_READ) :
97 ((fileFlags & O_WRONLY) ? GENERIC_WRITE : GENERIC_READ),
99 (FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE),
101 openFlagsToCreateFileFlags(fileFlags),
102 FILE_ATTRIBUTE_NORMAL |
103 (backup_semantics ? FILE_FLAG_BACKUP_SEMANTICS : 0) |
104 ((fileFlags & O_RANDOM) ? FILE_FLAG_RANDOM_ACCESS : 0) |
105 ((fileFlags & O_SEQUENTIAL) ? FILE_FLAG_SEQUENTIAL_SCAN : 0) |
106 ((fileFlags & _O_SHORT_LIVED) ? FILE_ATTRIBUTE_TEMPORARY : 0) |
107 ((fileFlags & O_TEMPORARY) ? FILE_FLAG_DELETE_ON_CLOSE : 0) |
108 ((fileFlags &
O_DIRECT) ? FILE_FLAG_NO_BUFFERING : 0) |
109 ((fileFlags &
O_DSYNC) ? FILE_FLAG_WRITE_THROUGH : 0),
110 NULL)) == INVALID_HANDLE_VALUE)
117 DWORD
err = GetLastError();
119 if (
err == ERROR_SHARING_VIOLATION ||
120 err == ERROR_LOCK_VIOLATION)
125 (
errmsg(
"could not open file \"%s\": %s", fileName,
126 (
err == ERROR_SHARING_VIOLATION) ?
_(
"sharing violation") :
_(
"lock violation")),
127 errdetail(
"Continuing to retry for 30 seconds."),
128 errhint(
"You might have antivirus, backup, or similar software interfering with the database system.")));
151 if (
err == ERROR_ACCESS_DENIED &&
154 if (fileFlags & O_CREAT)
155 err = ERROR_FILE_EXISTS;
157 err = ERROR_FILE_NOT_FOUND;
161 return INVALID_HANDLE_VALUE;
168pgwin32_open(
const char *fileName,
int fileFlags,...)
173 h = pgwin32_open_handle(fileName, fileFlags,
false);
174 if (h == INVALID_HANDLE_VALUE)
187 if ((fileFlags & O_BINARY) == 0)
192 if ((
fd = _open_osfhandle((intptr_t) h, fileFlags & O_APPEND)) < 0)
194 else if (fileFlags & (O_TEXT | O_BINARY) &&
195 _setmode(
fd, fileFlags & (O_TEXT | O_BINARY)) < 0)
205pgwin32_fopen(
const char *fileName,
const char *
mode)
210 if (strstr(
mode,
"r+"))
212 else if (strchr(
mode,
'r'))
213 openmode |= O_RDONLY;
214 if (strstr(
mode,
"w+"))
215 openmode |= O_RDWR | O_CREAT | O_TRUNC;
216 else if (strchr(
mode,
'w'))
217 openmode |= O_WRONLY | O_CREAT | O_TRUNC;
218 if (strchr(
mode,
'a'))
219 openmode |= O_WRONLY | O_CREAT | O_APPEND;
221 if (strchr(
mode,
'b'))
222 openmode |= O_BINARY;
223 if (strchr(
mode,
't'))
226 fd = pgwin32_open(fileName, openmode);
int errdetail(const char *fmt,...)
int errhint(const char *fmt,...)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
void err(int eval, const char *fmt,...)
static PgChecksumMode mode
static int fd(const char *x, int i)
void pg_usleep(long microsec)
void _dosmaperr(unsigned long)
int initialize_ntdll(void)
PGDLLIMPORT RtlGetLastNtStatus_t pg_RtlGetLastNtStatus