Skip to content

Commit 14010b3

Browse files
kbleesdscho
authored andcommitted
Win32: implement stat() with symlink support
With respect to symlinks, the current stat() implementation is almost the same as lstat(): except for the file type (st_mode & S_IFMT), it returns information about the link rather than the target. Implement stat by opening the file with as little permissions as possible and calling GetFileInformationByHandle on it. This way, all link resoltion is handled by the Windows file system layer. If symlinks are disabled, use lstat() as before, but fail with ELOOP if a symlink would have to be resolved. Signed-off-by: Karsten Blees <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 6a58f12 commit 14010b3

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

compat/mingw.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1054,9 +1054,26 @@ int mingw_lstat(const char *file_name, struct stat *buf)
10541054
{
10551055
return do_lstat(0, file_name, buf);
10561056
}
1057+
10571058
int mingw_stat(const char *file_name, struct stat *buf)
10581059
{
1059-
return do_lstat(1, file_name, buf);
1060+
wchar_t wfile_name[MAX_LONG_PATH];
1061+
HANDLE hnd;
1062+
int result;
1063+
1064+
/* open the file and let Windows resolve the links */
1065+
if (xutftowcs_long_path(wfile_name, file_name) < 0)
1066+
return -1;
1067+
hnd = CreateFileW(wfile_name, 0,
1068+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1069+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1070+
if (hnd == INVALID_HANDLE_VALUE) {
1071+
errno = err_win_to_posix(GetLastError());
1072+
return -1;
1073+
}
1074+
result = get_file_info_by_handle(hnd, buf);
1075+
CloseHandle(hnd);
1076+
return result;
10601077
}
10611078

10621079
int mingw_fstat(int fd, struct stat *buf)

0 commit comments

Comments
 (0)