1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
/*-------------------------------------------------------------------------
*
* dirmod.c
* rename/unlink()
*
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* These are replacement versions of unlink and rename that work on
* Win32 (NT, Win2k, XP). replace() doesn't work on Win95/98/Me.
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/port/dirmod.c,v 1.10 2004/02/02 22:20:33 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#ifndef TEST_VERSION
#if defined(WIN32) || defined(CYGWIN)
#ifndef FRONTEND
#include "postgres.h"
#else
#include "postgres_fe.h"
#endif
#undef rename
#undef unlink
int
pgrename(const char *from, const char *to)
{
int loops = 0;
#ifdef WIN32
while (!MoveFileEx(from, to, MOVEFILE_REPLACE_EXISTING))
#endif
#ifdef CYGWIN
while (rename(from, to) < 0)
#endif
{
#ifdef WIN32
if (GetLastError() != ERROR_ACCESS_DENIED)
#endif
#ifdef CYGWIN
if (errno != EACCES)
#endif
/* set errno? */
return -1;
Sleep(100); /* ms */
if (loops == 30)
#ifndef FRONTEND
elog(LOG, "could not rename \"%s\" to \"%s\", continuing to try",
from, to);
#else
fprintf(stderr, "could not rename \"%s\" to \"%s\", continuing to try\n",
from, to);
#endif
loops++;
}
if (loops > 30)
#ifndef FRONTEND
elog(LOG, "completed rename of \"%s\" to \"%s\"", from, to);
#else
fprintf(stderr, "completed rename of \"%s\" to \"%s\"\n", from, to);
#endif
return 0;
}
int
pgunlink(const char *path)
{
int loops = 0;
while (unlink(path))
{
if (errno != EACCES)
/* set errno? */
return -1;
Sleep(100); /* ms */
if (loops == 30)
#ifndef FRONTEND
elog(LOG, "could not unlink \"%s\", continuing to try",
path);
#else
fprintf(stderr, "could not unlink \"%s\", continuing to try\n",
path);
#endif
loops++;
}
if (loops > 30)
#ifndef FRONTEND
elog(LOG, "completed unlink of \"%s\"", path);
#else
fprintf(stderr, "completed unlink of \"%s\"\n", path);
#endif
return 0;
}
#endif
#else
/*
* Illustrates problem with Win32 rename() and unlink()
* under concurrent access.
*
* Run with arg '1', then less than 5 seconds later, run with
* arg '2' (rename) or '3'(unlink) to see the problem.
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <windows.h>
#define halt(str) \
do { \
fputs(str, stderr); \
exit(1); \
} while (0)
int
main(int argc, char *argv[])
{
FILE *fd;
if (argc != 2)
halt("Arg must be '1' (test), '2' (rename), or '3' (unlink)\n"
"Run '1' first, then less than 5 seconds later, run\n"
"'2' to test rename, or '3' to test unlink.\n");
if (atoi(argv[1]) == 1)
{
if ((fd = fopen("/rtest.txt", "w")) == NULL)
halt("Can not create file\n");
fclose(fd);
if ((fd = fopen("/rtest.txt", "r")) == NULL)
halt("Can not open file\n");
Sleep(5000);
}
else if (atoi(argv[1]) == 2)
{
unlink("/rtest.new");
if ((fd = fopen("/rtest.new", "w")) == NULL)
halt("Can not create file\n");
fclose(fd);
while (!MoveFileEx("/rtest.new", "/rtest.txt", MOVEFILE_REPLACE_EXISTING))
{
if (GetLastError() != ERROR_ACCESS_DENIED)
halt("Unknown failure\n");
else
fprintf(stderr, "move failed\n");
Sleep(500);
}
halt("move successful\n");
}
else if (atoi(argv[1]) == 3)
{
while (unlink("/rtest.txt"))
{
if (errno != EACCES)
halt("Unknown failure\n");
else
fprintf(stderr, "unlink failed\n");
Sleep(500);
}
halt("unlink successful\n");
}
else
halt("invalid arg\n");
return 0;
}
#endif
|