Skip to content

Commit 4f28810

Browse files
hvoigtgitster
authored andcommitted
mingw: add fallback for rmdir in case directory is in use
The same logic as for unlink and rename also applies to rmdir. For example in case you have a shell open in a git controlled folder. This will easily fail. So lets be nice for such cases as well. Signed-off-by: Heiko Voigt <heiko.voigt@mahr.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent c9b7840 commit 4f28810

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

compat/mingw.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,31 @@ int mingw_unlink(const char *pathname)
225225
return ret;
226226
}
227227

228+
#undef rmdir
229+
int mingw_rmdir(const char *pathname)
230+
{
231+
int ret, tries = 0;
232+
233+
while ((ret = rmdir(pathname)) == -1 && tries < ARRAY_SIZE(delay)) {
234+
if (!is_file_in_use_error(GetLastError()))
235+
break;
236+
/*
237+
* We assume that some other process had the source or
238+
* destination file open at the wrong moment and retry.
239+
* In order to give the other process a higher chance to
240+
* complete its operation, we give up our time slice now.
241+
* If we have to retry again, we do sleep a bit.
242+
*/
243+
Sleep(delay[tries]);
244+
tries++;
245+
}
246+
while (ret == -1 && is_file_in_use_error(GetLastError()) &&
247+
ask_yes_no_if_possible("Deletion of directory '%s' failed. "
248+
"Should I try again?", pathname))
249+
ret = rmdir(pathname);
250+
return ret;
251+
}
252+
228253
#undef open
229254
int mingw_open (const char *filename, int oflags, ...)
230255
{

compat/mingw.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ int link(const char *oldpath, const char *newpath);
175175
int mingw_unlink(const char *pathname);
176176
#define unlink mingw_unlink
177177

178+
int mingw_rmdir(const char *path);
179+
#define rmdir mingw_rmdir
180+
178181
int mingw_open (const char *filename, int oflags, ...);
179182
#define open mingw_open
180183

0 commit comments

Comments
 (0)