mingw_rmdir: set errno=ENOTEMPTY when appropriate
On Windows, EACCES overrules ENOTEMPTY when calling rmdir(). But if the directory is busy, we only want to retry deleting the directory if it is empty, so test specifically for that case and set ENOTEMPTY rather than EACCES. Noticed by Greg Hazel. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Heiko Voigt <hvoigt@hvoigt.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Feb 7, 2011 at 21:54 UTC
ab1a11be7858e1f92ee2e4ee1d70fabe7d5fe0ee
1 file changed
+28
compat/mingw.c
+28
@@ -225,6 +225,30 @@ int mingw_unlink(const char *pathname)
225
return ret;
226
}
227
228
+static int is_dir_empty(const char *path)
229
+{
230
+ struct strbuf buf = STRBUF_INIT;
231
+ WIN32_FIND_DATAA findbuf;
232
+ HANDLE handle;
233
+
234
+ strbuf_addf(&buf, "%s\\*", path);
235
+ handle = FindFirstFileA(buf.buf, &findbuf);
236
+ if (handle == INVALID_HANDLE_VALUE) {
237
+ strbuf_release(&buf);
238
+ return GetLastError() == ERROR_NO_MORE_FILES;
239
+ }
240
+
241
+ while (!strcmp(findbuf.cFileName, ".") ||
242
+ !strcmp(findbuf.cFileName, ".."))
243
+ if (!FindNextFile(handle, &findbuf)) {
244
+ strbuf_release(&buf);
245
+ return GetLastError() == ERROR_NO_MORE_FILES;
246
+ }
247
+ FindClose(handle);
248
+ strbuf_release(&buf);
249
+ return 0;
250
+}
251
+
252
#undef rmdir
253
int mingw_rmdir(const char *pathname)
254
{
@@ -233,6 +257,10 @@ int mingw_rmdir(const char *pathname)
257
while ((ret = rmdir(pathname)) == -1 && tries < ARRAY_SIZE(delay)) {
258
if (!is_file_in_use_error(GetLastError()))
259
break;
260
+ if (!is_dir_empty(pathname)) {
261
+ errno = ENOTEMPTY;
262
+ break;
263
+ }
264
/*
265
* We assume that some other process had the source or
266
* destination file open at the wrong moment and retry.