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>
Heiko Voigt committed
Feb 7, 2011 at 21:52 UTC
4f288100ceed14c65a1e964b2db4aaee4f4199fc
2 files changed
+28
compat/mingw.c
+25
@@ -225,6 +225,31 @@ int mingw_unlink(const char *pathname)
225
return ret;
226
}
227
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
+
253
#undef open
254
int mingw_open (const char *filename, int oflags, ...)
255
{
compat/mingw.h
+3
@@ -175,6 +175,9 @@ int link(const char *oldpath, const char *newpath);
175
int mingw_unlink(const char *pathname);
176
#define unlink mingw_unlink
177
178
+int mingw_rmdir(const char *path);
179
+#define rmdir mingw_rmdir
180
+
181
int mingw_open (const char *filename, int oflags, ...);
182
#define open mingw_open
183