From 500539c6ff04ccd4f43c5bb6085d17699edb482c Mon Sep 17 00:00:00 2001 From: Orgad Shaneh Date: Tue, 8 May 2018 10:07:59 +0300 Subject: [PATCH] mingw: Fix unlink for open files Fixes #1653 Signed-off-by: Orgad Shaneh --- compat/mingw.c | 23 ++++++++++++++++++++++- config.mak.uname | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 502c1961ac856b..1bcc276483ce0e 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "../strbuf.h" #include "../run-command.h" #include "../cache.h" @@ -13,6 +14,10 @@ #define HCAST(type, handle) ((type)(intptr_t)handle) +#ifndef ERROR_TRANSACTIONAL_CONFLICT +#define ERROR_TRANSACTIONAL_CONFLICT 6800 +#endif + int err_win_to_posix(DWORD winerr) { int error = ENOSYS; @@ -127,6 +132,7 @@ int err_win_to_posix(DWORD winerr) case ERROR_WAIT_NO_CHILDREN: error = ECHILD; break; case ERROR_WRITE_FAULT: error = EIO; break; case ERROR_WRITE_PROTECT: error = EROFS; break; + case ERROR_TRANSACTIONAL_CONFLICT: error = EPERM; break; } return error; } @@ -136,6 +142,7 @@ static inline int is_file_in_use_error(DWORD errcode) switch (errcode) { case ERROR_SHARING_VIOLATION: case ERROR_ACCESS_DENIED: + case ERROR_TRANSACTIONAL_CONFLICT: return 1; } @@ -383,6 +390,20 @@ static wchar_t *normalize_ntpath(wchar_t *wbuf) return wbuf; } +static int do_unlink(const wchar_t *wpathname) +{ +#if _WIN32_WINNT >= 0x0600 + HANDLE transaction = CreateTransaction(NULL, 0, 0, 0, 0, 0, NULL); + BOOL result = DeleteFileTransactedW(wpathname, transaction); + if (result) + CommitTransaction(transaction); + CloseHandle(transaction); + return result ? 0 : -1; +#else + return _wunlink(wpathname); +#endif +} + int mingw_unlink(const char *pathname) { int tries = 0; @@ -393,7 +414,7 @@ int mingw_unlink(const char *pathname) do { /* read-only files cannot be removed */ _wchmod(wpathname, 0666); - if (!_wunlink(wpathname)) + if (!do_unlink(wpathname)) return 0; if (!is_file_in_use_error(GetLastError())) break; diff --git a/config.mak.uname b/config.mak.uname index c377a63c7182b0..0e1676f1403b57 100644 --- a/config.mak.uname +++ b/config.mak.uname @@ -582,7 +582,7 @@ ifneq (,$(findstring MINGW,$(uname_S))) compat/win32/pthread.o compat/win32/syslog.o \ compat/win32/dirent.o compat/win32/fscache.o BASIC_CFLAGS += -DPROTECT_NTFS_DEFAULT=1 - EXTLIBS += -lws2_32 + EXTLIBS += -lws2_32 -lktmw32 GITLIBS += git.res PTHREAD_LIBS = RC = windres -O coff