diff --git a/compat/mingw.c b/compat/mingw.c index 9b503a0eec..384b3b67a1 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -3,6 +3,26 @@ unsigned int _CRT_fmode = _O_BINARY; +#undef open +int mingw_open (const char *filename, int oflags, ...) +{ + va_list args; + unsigned mode; + va_start(args, oflags); + mode = va_arg(args, int); + va_end(args); + + if (!strcmp(filename, "/dev/null")) + filename = "nul"; + int fd = open(filename, oflags, mode); + if (fd < 0 && (oflags & O_CREAT) && errno == EACCES) { + DWORD attrs = GetFileAttributes(filename); + if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY)) + errno = EISDIR; + } + return fd; +} + static inline time_t filetime_to_time_t(const FILETIME *ft) { long long winTime = ((long long)ft->dwHighDateTime << 32) + ft->dwLowDateTime; diff --git a/git-compat-util.h b/git-compat-util.h index 1bbdb2f2fb..bfab97d057 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -500,9 +500,8 @@ static inline int git_unlink(const char *pathname) { } #define unlink git_unlink -#define open(P, F, M...) \ - (__builtin_constant_p(*(P)) && !strcmp(P, "/dev/null") ? \ - open("nul", F, ## M) : open(P, F, ## M)) +int mingw_open (const char *filename, int oflags, ...); +#define open mingw_open #include struct tm *gmtime_r(const time_t *timep, struct tm *result);