Skip to content

Commit ab1a11b

Browse files
dschogitster
authored andcommitted
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>
1 parent 4f28810 commit ab1a11b

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

compat/mingw.c

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

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+
228252
#undef rmdir
229253
int mingw_rmdir(const char *pathname)
230254
{
@@ -233,6 +257,10 @@ int mingw_rmdir(const char *pathname)
233257
while ((ret = rmdir(pathname)) == -1 && tries < ARRAY_SIZE(delay)) {
234258
if (!is_file_in_use_error(GetLastError()))
235259
break;
260+
if (!is_dir_empty(pathname)) {
261+
errno = ENOTEMPTY;
262+
break;
263+
}
236264
/*
237265
* We assume that some other process had the source or
238266
* destination file open at the wrong moment and retry.

0 commit comments

Comments
 (0)