Skip to content

Commit 5b13950

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 2e33e4d commit 5b13950

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

compat/mingw.c

+18-1
Original file line numberDiff line numberDiff line change
@@ -946,9 +946,26 @@ int mingw_lstat(const char *file_name, struct stat *buf)
946946
{
947947
return do_lstat(0, file_name, buf);
948948
}
949+
949950
int mingw_stat(const char *file_name, struct stat *buf)
950951
{
951-
return do_lstat(1, file_name, buf);
952+
wchar_t wfile_name[MAX_LONG_PATH];
953+
HANDLE hnd;
954+
int result;
955+
956+
/* open the file and let Windows resolve the links */
957+
if (xutftowcs_long_path(wfile_name, file_name) < 0)
958+
return -1;
959+
hnd = CreateFileW(wfile_name, 0,
960+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
961+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
962+
if (hnd == INVALID_HANDLE_VALUE) {
963+
errno = err_win_to_posix(GetLastError());
964+
return -1;
965+
}
966+
result = get_file_info_by_handle(hnd, buf);
967+
CloseHandle(hnd);
968+
return result;
952969
}
953970

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

0 commit comments

Comments
 (0)