diff options
Diffstat (limited to 'compat/mingw.c')
-rw-r--r-- | compat/mingw.c | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/compat/mingw.c b/compat/mingw.c index aa647b367b..03af369b2b 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -8,6 +8,8 @@ #include "win32/lazyload.h" #include "../config.h" #include "dir.h" +#define SECURITY_WIN32 +#include <sspi.h> #define HCAST(type, handle) ((type)(intptr_t)handle) @@ -341,6 +343,27 @@ int mingw_rmdir(const char *pathname) { int ret, tries = 0; wchar_t wpathname[MAX_PATH]; + struct stat st; + + /* + * Contrary to Linux' `rmdir()`, Windows' _wrmdir() and _rmdir() + * (and `RemoveDirectoryW()`) will attempt to remove the target of a + * symbolic link (if it points to a directory). + * + * This behavior breaks the assumption of e.g. `remove_path()` which + * upon successful deletion of a file will attempt to remove its parent + * directories recursively until failure (which usually happens when + * the directory is not empty). + * + * Therefore, before calling `_wrmdir()`, we first check if the path is + * a symbolic link. If it is, we exit and return the same error as + * Linux' `rmdir()` would, i.e. `ENOTDIR`. + */ + if (!mingw_lstat(pathname, &st) && S_ISLNK(st.st_mode)) { + errno = ENOTDIR; + return -1; + } + if (xutftowcs_path(wpathname, pathname) < 0) return -1; @@ -987,7 +1010,7 @@ size_t mingw_strftime(char *s, size_t max, /* a pointer to the original strftime in case we can't find the UCRT version */ static size_t (*fallback)(char *, size_t, const char *, const struct tm *) = strftime; size_t ret; - DECLARE_PROC_ADDR(ucrtbase.dll, size_t, strftime, char *, size_t, + DECLARE_PROC_ADDR(ucrtbase.dll, size_t, __cdecl, strftime, char *, size_t, const char *, const struct tm *); if (INIT_PROC_ADDR(strftime)) @@ -1062,6 +1085,7 @@ int pipe(int filedes[2]) return 0; } +#ifndef __MINGW64__ struct tm *gmtime_r(const time_t *timep, struct tm *result) { if (gmtime_s(result, timep) == 0) @@ -1075,6 +1099,7 @@ struct tm *localtime_r(const time_t *timep, struct tm *result) return result; return NULL; } +#endif char *mingw_getcwd(char *pointer, int len) { @@ -1102,6 +1127,10 @@ char *mingw_getcwd(char *pointer, int len) } if (!ret || ret >= ARRAY_SIZE(wpointer)) return NULL; + if (GetFileAttributesW(wpointer) == INVALID_FILE_ATTRIBUTES) { + errno = ENOENT; + return NULL; + } if (xwcstoutf(pointer, wpointer, len) < 0) return NULL; convert_slashes(pointer); @@ -2162,7 +2191,7 @@ enum EXTENDED_NAME_FORMAT { static char *get_extended_user_info(enum EXTENDED_NAME_FORMAT type) { - DECLARE_PROC_ADDR(secur32.dll, BOOL, GetUserNameExW, + DECLARE_PROC_ADDR(secur32.dll, BOOL, SEC_ENTRY, GetUserNameExW, enum EXTENDED_NAME_FORMAT, LPCWSTR, PULONG); static wchar_t wbuffer[1024]; DWORD len; |