@matheusbsbs / matheus-bagliano / commits / 19e125498b
Fork 0

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 6ac6f878. Signed-off-by: Heiko Voigt <hvoigt@hvoigt.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Heiko Voigt committed Feb 7, 2011 at 21:50 UTC 19e125498bc417e20f0b8c1014b808654e4acfe4
1 file changed +29 -2
compat/mingw.c
+29 -2
@@ -3,6 +3,8 @@
3 #include <conio.h>
4 #include "../strbuf.h"
5
6 +static const int delay[] = { 0, 1, 10, 20, 40 };
7 +
8 int err_win_to_posix(DWORD winerr)
9 {
10 int error = ENOSYS;
@@ -116,12 +118,38 @@ int err_win_to_posix(DWORD winerr)
118 return error;
119 }
120
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 +
132 #undef unlink
133 int mingw_unlink(const char *pathname)
134 {
135 + int ret, tries = 0;
136 +
137 /* read-only files cannot be removed */
138 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;
153 }
154
155 #undef open
@@ -1257,7 +1285,6 @@ int mingw_rename(const char *pold, const char *pnew)
1285 {
1286 DWORD attrs, gle;
1287 int tries = 0;
1260 - static const int delay[] = { 0, 1, 10, 20, 40 };
1288
1289 /*
1290 * Try native rename() first to get errno right.