Skip to content

mingw: Fix unlink for open files #1665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion compat/mingw.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <conio.h>
#include <wchar.h>
#include <winioctl.h>
#include <ktmw32.h>
#include "../strbuf.h"
#include "../run-command.h"
#include "../cache.h"
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion config.mak.uname
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down