PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
open.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * open.c
4  * Win32 open() replacement
5  *
6  *
7  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
8  *
9  * src/port/open.c
10  *
11  *-------------------------------------------------------------------------
12  */
13 
14 #ifdef WIN32
15 
16 #ifndef FRONTEND
17 #include "postgres.h"
18 #else
19 #include "postgres_fe.h"
20 #endif
21 
22 #include <fcntl.h>
23 #include <assert.h>
24 
25 
26 static int
27 openFlagsToCreateFileFlags(int openFlags)
28 {
29  switch (openFlags & (O_CREAT | O_TRUNC | O_EXCL))
30  {
31  /* O_EXCL is meaningless without O_CREAT */
32  case 0:
33  case O_EXCL:
34  return OPEN_EXISTING;
35 
36  case O_CREAT:
37  return OPEN_ALWAYS;
38 
39  /* O_EXCL is meaningless without O_CREAT */
40  case O_TRUNC:
41  case O_TRUNC | O_EXCL:
42  return TRUNCATE_EXISTING;
43 
44  case O_CREAT | O_TRUNC:
45  return CREATE_ALWAYS;
46 
47  /* O_TRUNC is meaningless with O_CREAT */
48  case O_CREAT | O_EXCL:
49  case O_CREAT | O_TRUNC | O_EXCL:
50  return CREATE_NEW;
51  }
52 
53  /* will never get here */
54  return 0;
55 }
56 
57 /*
58  * - file attribute setting, based on fileMode?
59  */
60 int
61 pgwin32_open(const char *fileName, int fileFlags,...)
62 {
63  int fd;
64  HANDLE h = INVALID_HANDLE_VALUE;
65  SECURITY_ATTRIBUTES sa;
66  int loops = 0;
67 
68  /* Check that we can handle the request */
69  assert((fileFlags & ((O_RDONLY | O_WRONLY | O_RDWR) | O_APPEND |
70  (O_RANDOM | O_SEQUENTIAL | O_TEMPORARY) |
71  _O_SHORT_LIVED | O_DSYNC | O_DIRECT |
72  (O_CREAT | O_TRUNC | O_EXCL) | (O_TEXT | O_BINARY))) == fileFlags);
73 
74  sa.nLength = sizeof(sa);
75  sa.bInheritHandle = TRUE;
76  sa.lpSecurityDescriptor = NULL;
77 
78  while ((h = CreateFile(fileName,
79  /* cannot use O_RDONLY, as it == 0 */
80  (fileFlags & O_RDWR) ? (GENERIC_WRITE | GENERIC_READ) :
81  ((fileFlags & O_WRONLY) ? GENERIC_WRITE : GENERIC_READ),
82  /* These flags allow concurrent rename/unlink */
83  (FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE),
84  &sa,
85  openFlagsToCreateFileFlags(fileFlags),
86  FILE_ATTRIBUTE_NORMAL |
87  ((fileFlags & O_RANDOM) ? FILE_FLAG_RANDOM_ACCESS : 0) |
88  ((fileFlags & O_SEQUENTIAL) ? FILE_FLAG_SEQUENTIAL_SCAN : 0) |
89  ((fileFlags & _O_SHORT_LIVED) ? FILE_ATTRIBUTE_TEMPORARY : 0) |
90  ((fileFlags & O_TEMPORARY) ? FILE_FLAG_DELETE_ON_CLOSE : 0) |
91  ((fileFlags & O_DIRECT) ? FILE_FLAG_NO_BUFFERING : 0) |
92  ((fileFlags & O_DSYNC) ? FILE_FLAG_WRITE_THROUGH : 0),
93  NULL)) == INVALID_HANDLE_VALUE)
94  {
95  /*
96  * Sharing violation or locking error can indicate antivirus, backup
97  * or similar software that's locking the file. Try again for 30
98  * seconds before giving up.
99  */
100  DWORD err = GetLastError();
101 
102  if (err == ERROR_SHARING_VIOLATION ||
103  err == ERROR_LOCK_VIOLATION)
104  {
105  pg_usleep(100000);
106  loops++;
107 
108 #ifndef FRONTEND
109  if (loops == 50)
110  ereport(LOG,
111  (errmsg("could not open file \"%s\": %s", fileName,
112  (err == ERROR_SHARING_VIOLATION) ? _("sharing violation") : _("lock violation")),
113  errdetail("Continuing to retry for 30 seconds."),
114  errhint("You might have antivirus, backup, or similar software interfering with the database system.")));
115 #endif
116 
117  if (loops < 300)
118  continue;
119  }
120 
121  _dosmaperr(err);
122  return -1;
123  }
124 
125  /* _open_osfhandle will, on error, set errno accordingly */
126  if ((fd = _open_osfhandle((intptr_t) h, fileFlags & O_APPEND)) < 0)
127  CloseHandle(h); /* will not affect errno */
128  else if (fileFlags & (O_TEXT | O_BINARY) &&
129  _setmode(fd, fileFlags & (O_TEXT | O_BINARY)) < 0)
130  {
131  _close(fd);
132  return -1;
133  }
134 
135  return fd;
136 }
137 
138 FILE *
139 pgwin32_fopen(const char *fileName, const char *mode)
140 {
141  int openmode = 0;
142  int fd;
143 
144  if (strstr(mode, "r+"))
145  openmode |= O_RDWR;
146  else if (strchr(mode, 'r'))
147  openmode |= O_RDONLY;
148  if (strstr(mode, "w+"))
149  openmode |= O_RDWR | O_CREAT | O_TRUNC;
150  else if (strchr(mode, 'w'))
151  openmode |= O_WRONLY | O_CREAT | O_TRUNC;
152  if (strchr(mode, 'a'))
153  openmode |= O_WRONLY | O_CREAT | O_APPEND;
154 
155  if (strchr(mode, 'b'))
156  openmode |= O_BINARY;
157  if (strchr(mode, 't'))
158  openmode |= O_TEXT;
159 
160  fd = pgwin32_open(fileName, openmode);
161  if (fd == -1)
162  return NULL;
163  return _fdopen(fd, mode);
164 }
165 
166 #endif
int errhint(const char *fmt,...)
Definition: elog.c:987
#define LOG
Definition: elog.h:26
static int fd(const char *x, int i)
Definition: preproc-init.c:105
void pg_usleep(long microsec)
Definition: signal.c:53
#define O_DSYNC
Definition: win32.h:270
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define assert(TEST)
Definition: imath.c:37
#define ereport(elevel, rest)
Definition: elog.h:122
void _dosmaperr(unsigned long)
Definition: win32error.c:171
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define TRUE
Definition: c.h:215
#define _(x)
Definition: elog.c:84