From 1b61a02b5139a47e2e3ac8be5afca325d7b3cea7 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Fri, 12 Feb 2021 14:07:55 +0000 Subject: win32: allow symlinks to directories to be unlinked Windows distinguishes between symlinks to directories and files. A symlink to a directory must be deleted by calling rmdir(2) rather than unlink(2). --- win32/mingw.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/win32/mingw.c b/win32/mingw.c index 15529ed58..41dba9857 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -1316,9 +1316,21 @@ int fcntl(int fd, int cmd, ...) #undef unlink int mingw_unlink(const char *pathname) { + int ret; + struct stat st; + /* read-only files cannot be removed */ chmod(pathname, 0666); - return unlink(pathname); + + ret = unlink(pathname); + if (ret == -1 && errno == EACCES) { + /* a symlink to a directory needs to be removed by calling rmdir */ + if (lstat(pathname, &st) == 0 && S_ISLNK(st.st_mode)) { + return rmdir(pathname); + } + errno = EACCES; + } + return ret; } #undef strftime -- cgit v1.2.3-55-g6feb