Skip to content

Commit 19e1254

Browse files
hvoigtgitster
authored andcommitted
mingw: work around irregular failures of unlink on windows
If a file is opened by another process (e.g. indexing of an IDE) for reading it is not allowed to be deleted. So in case unlink fails retry after waiting for some time. This extends the workaround from 6ac6f87. Signed-off-by: Heiko Voigt <hvoigt@hvoigt.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 337967f commit 19e1254

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

compat/mingw.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <conio.h>
44
#include "../strbuf.h"
55

6+
static const int delay[] = { 0, 1, 10, 20, 40 };
7+
68
int err_win_to_posix(DWORD winerr)
79
{
810
int error = ENOSYS;
@@ -116,12 +118,38 @@ int err_win_to_posix(DWORD winerr)
116118
return error;
117119
}
118120

121+
static inline int is_file_in_use_error(DWORD errcode)
122+
{
123+
switch (errcode) {
124+
case ERROR_SHARING_VIOLATION:
125+
case ERROR_ACCESS_DENIED:
126+
return 1;
127+
}
128+
129+
return 0;
130+
}
131+
119132
#undef unlink
120133
int mingw_unlink(const char *pathname)
121134
{
135+
int ret, tries = 0;
136+
122137
/* read-only files cannot be removed */
123138
chmod(pathname, 0666);
124-
return unlink(pathname);
139+
while ((ret = unlink(pathname)) == -1 && tries < ARRAY_SIZE(delay)) {
140+
if (!is_file_in_use_error(GetLastError()))
141+
break;
142+
/*
143+
* We assume that some other process had the source or
144+
* destination file open at the wrong moment and retry.
145+
* In order to give the other process a higher chance to
146+
* complete its operation, we give up our time slice now.
147+
* If we have to retry again, we do sleep a bit.
148+
*/
149+
Sleep(delay[tries]);
150+
tries++;
151+
}
152+
return ret;
125153
}
126154

127155
#undef open
@@ -1257,7 +1285,6 @@ int mingw_rename(const char *pold, const char *pnew)
12571285
{
12581286
DWORD attrs, gle;
12591287
int tries = 0;
1260-
static const int delay[] = { 0, 1, 10, 20, 40 };
12611288

12621289
/*
12631290
* Try native rename() first to get errno right.

0 commit comments

Comments
 (0)