|
22 | 22 | #include <windows.h>
|
23 | 23 | #include <fcntl.h>
|
24 | 24 | #include <assert.h>
|
| 25 | +#include <sys/stat.h> |
25 | 26 |
|
26 | 27 |
|
27 | 28 | static int
|
@@ -121,18 +122,33 @@ pgwin32_open(const char *fileName, int fileFlags,...)
|
121 | 122 | }
|
122 | 123 |
|
123 | 124 | /*
|
124 |
| - * ERROR_ACCESS_DENIED can be returned if the file is deleted but not |
125 |
| - * yet gone (Windows NT status code is STATUS_DELETE_PENDING). Wait a |
126 |
| - * bit and try again, giving up after 1 second (since this condition |
127 |
| - * should never persist very long). |
| 125 | + * ERROR_ACCESS_DENIED is returned if the file is deleted but not yet |
| 126 | + * gone (Windows NT status code is STATUS_DELETE_PENDING). In that |
| 127 | + * case we want to wait a bit and try again, giving up after 1 second |
| 128 | + * (since this condition should never persist very long). However, |
| 129 | + * there are other commonly-hit cases that return ERROR_ACCESS_DENIED, |
| 130 | + * so care is needed. In particular that happens if we try to open a |
| 131 | + * directory, or of course if there's an actual file-permissions |
| 132 | + * problem. To distinguish these cases, try a stat(). In the |
| 133 | + * delete-pending case, it will either also get STATUS_DELETE_PENDING, |
| 134 | + * or it will see the file as gone and fail with ENOENT. In other |
| 135 | + * cases it will usually succeed. The only somewhat-likely case where |
| 136 | + * this coding will uselessly wait is if there's a permissions problem |
| 137 | + * with a containing directory, which we hope will never happen in any |
| 138 | + * performance-critical code paths. |
128 | 139 | */
|
129 | 140 | if (err == ERROR_ACCESS_DENIED)
|
130 | 141 | {
|
131 | 142 | if (loops < 10)
|
132 | 143 | {
|
133 |
| - pg_usleep(100000); |
134 |
| - loops++; |
135 |
| - continue; |
| 144 | + struct stat st; |
| 145 | + |
| 146 | + if (stat(fileName, &st) != 0) |
| 147 | + { |
| 148 | + pg_usleep(100000); |
| 149 | + loops++; |
| 150 | + continue; |
| 151 | + } |
136 | 152 | }
|
137 | 153 | }
|
138 | 154 |
|
|
0 commit comments