From 3142f2493ef4ea02409cd3d11165af0162f9fd98 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 26 May 2010 12:42:50 +0200 Subject: [PATCH] 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 --- compat/mingw.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/compat/mingw.c b/compat/mingw.c index 8258094799..37ee0cc5b2 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -233,14 +233,42 @@ int mingw_unlink(const char *pathname) return ret; } +static int is_dir_empty(const char *path) +{ + struct strbuf buf = STRBUF_INIT; + WIN32_FIND_DATAA findbuf; + HANDLE handle; + + strbuf_addf(&buf, "%s\\*", path); + handle = FindFirstFileA(buf.buf, &findbuf); + if (handle == INVALID_HANDLE_VALUE) { + strbuf_release(&buf); + return GetLastError() == ERROR_NO_MORE_FILES; + } + + while (!strcmp(findbuf.cFileName, ".") || + !strcmp(findbuf.cFileName, "..")) + if (!FindNextFile(handle, &findbuf)) { + strbuf_release(&buf); + return GetLastError() == ERROR_NO_MORE_FILES; + } + FindClose(handle); + strbuf_release(&buf); + return 0; +} + #undef rmdir int mingw_rmdir(const char *pathname) { - int ret, tries = 0; + int ret, tries = 0; while ((ret = rmdir(pathname)) == -1 && tries < ARRAY_SIZE(delay)) { if (errno != EACCES) break; + if (!is_dir_empty(pathname)) { + errno = ENOTEMPTY; + break; + } /* * We assume that some other process had the source or * destination file open at the wrong moment and retry. -- 2.11.4.GIT