Skip to content

Commit c27933b

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 2acda4f commit c27933b

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
@@ -1043,9 +1043,26 @@ int mingw_lstat(const char *file_name, struct stat *buf)
10431043
{
10441044
return do_lstat(0, file_name, buf);
10451045
}
1046+
10461047
int mingw_stat(const char *file_name, struct stat *buf)
10471048
{
1048-
return do_lstat(1, file_name, buf);
1049+
wchar_t wfile_name[MAX_LONG_PATH];
1050+
HANDLE hnd;
1051+
int result;
1052+
1053+
/* open the file and let Windows resolve the links */
1054+
if (xutftowcs_long_path(wfile_name, file_name) < 0)
1055+
return -1;
1056+
hnd = CreateFileW(wfile_name, 0,
1057+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1058+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1059+
if (hnd == INVALID_HANDLE_VALUE) {
1060+
errno = err_win_to_posix(GetLastError());
1061+
return -1;
1062+
}
1063+
result = get_file_info_by_handle(hnd, buf);
1064+
CloseHandle(hnd);
1065+
return result;
10491066
}
10501067

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

0 commit comments

Comments
 (0)